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> |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 16 | #include <linux/page_idle.h> |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 17 | #include <linux/shmem_fs.h> |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 18 | #include <linux/mm_inline.h> |
Minchan Kim | 01fdd71 | 2013-05-09 16:21:28 +0900 | [diff] [blame] | 19 | #include <linux/ctype.h> |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/elf.h> |
| 22 | #include <asm/uaccess.h> |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 23 | #include <asm/tlbflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "internal.h" |
| 25 | |
Eric W. Biederman | df5f831 | 2008-02-08 04:18:33 -0800 | [diff] [blame] | 26 | void task_mem(struct seq_file *m, struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 28 | unsigned long text, lib, swap, ptes, pmds, anon, file, shmem; |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 29 | unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss; |
| 30 | |
Jerome Marchand | 8cee852 | 2016-01-14 15:19:29 -0800 | [diff] [blame] | 31 | anon = get_mm_counter(mm, MM_ANONPAGES); |
| 32 | file = get_mm_counter(mm, MM_FILEPAGES); |
| 33 | shmem = get_mm_counter(mm, MM_SHMEMPAGES); |
| 34 | |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 35 | /* |
| 36 | * Note: to minimize their overhead, mm maintains hiwater_vm and |
| 37 | * hiwater_rss only when about to *lower* total_vm or rss. Any |
| 38 | * collector of these hiwater stats must therefore get total_vm |
| 39 | * and rss too, which will usually be the higher. Barriers? not |
| 40 | * worth the effort, such snapshots can always be inconsistent. |
| 41 | */ |
| 42 | hiwater_vm = total_vm = mm->total_vm; |
| 43 | if (hiwater_vm < mm->hiwater_vm) |
| 44 | hiwater_vm = mm->hiwater_vm; |
Jerome Marchand | 8cee852 | 2016-01-14 15:19:29 -0800 | [diff] [blame] | 45 | hiwater_rss = total_rss = anon + file + shmem; |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 46 | if (hiwater_rss < mm->hiwater_rss) |
| 47 | hiwater_rss = mm->hiwater_rss; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10; |
| 50 | lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text; |
KAMEZAWA Hiroyuki | b084d43 | 2010-03-05 13:41:42 -0800 | [diff] [blame] | 51 | swap = get_mm_counter(mm, MM_SWAPENTS); |
Kirill A. Shutemov | dc6c9a3 | 2015-02-11 15:26:50 -0800 | [diff] [blame] | 52 | ptes = PTRS_PER_PTE * sizeof(pte_t) * atomic_long_read(&mm->nr_ptes); |
| 53 | pmds = PTRS_PER_PMD * sizeof(pmd_t) * mm_nr_pmds(mm); |
Eric W. Biederman | df5f831 | 2008-02-08 04:18:33 -0800 | [diff] [blame] | 54 | seq_printf(m, |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 55 | "VmPeak:\t%8lu kB\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | "VmSize:\t%8lu kB\n" |
| 57 | "VmLck:\t%8lu kB\n" |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 58 | "VmPin:\t%8lu kB\n" |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 59 | "VmHWM:\t%8lu kB\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | "VmRSS:\t%8lu kB\n" |
Jerome Marchand | 8cee852 | 2016-01-14 15:19:29 -0800 | [diff] [blame] | 61 | "RssAnon:\t%8lu kB\n" |
| 62 | "RssFile:\t%8lu kB\n" |
| 63 | "RssShmem:\t%8lu kB\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | "VmData:\t%8lu kB\n" |
| 65 | "VmStk:\t%8lu kB\n" |
| 66 | "VmExe:\t%8lu kB\n" |
| 67 | "VmLib:\t%8lu kB\n" |
KAMEZAWA Hiroyuki | b084d43 | 2010-03-05 13:41:42 -0800 | [diff] [blame] | 68 | "VmPTE:\t%8lu kB\n" |
Kirill A. Shutemov | dc6c9a3 | 2015-02-11 15:26:50 -0800 | [diff] [blame] | 69 | "VmPMD:\t%8lu kB\n" |
KAMEZAWA Hiroyuki | b084d43 | 2010-03-05 13:41:42 -0800 | [diff] [blame] | 70 | "VmSwap:\t%8lu kB\n", |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 71 | hiwater_vm << (PAGE_SHIFT-10), |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 72 | total_vm << (PAGE_SHIFT-10), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | mm->locked_vm << (PAGE_SHIFT-10), |
Christoph Lameter | bc3e53f | 2011-10-31 17:07:30 -0700 | [diff] [blame] | 74 | mm->pinned_vm << (PAGE_SHIFT-10), |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 75 | hiwater_rss << (PAGE_SHIFT-10), |
| 76 | total_rss << (PAGE_SHIFT-10), |
Jerome Marchand | 8cee852 | 2016-01-14 15:19:29 -0800 | [diff] [blame] | 77 | anon << (PAGE_SHIFT-10), |
| 78 | file << (PAGE_SHIFT-10), |
| 79 | shmem << (PAGE_SHIFT-10), |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 80 | mm->data_vm << (PAGE_SHIFT-10), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | mm->stack_vm << (PAGE_SHIFT-10), text, lib, |
Kirill A. Shutemov | dc6c9a3 | 2015-02-11 15:26:50 -0800 | [diff] [blame] | 82 | ptes >> 10, |
| 83 | pmds >> 10, |
KAMEZAWA Hiroyuki | b084d43 | 2010-03-05 13:41:42 -0800 | [diff] [blame] | 84 | swap << (PAGE_SHIFT-10)); |
Naoya Horiguchi | 5d317b2 | 2015-11-05 18:47:14 -0800 | [diff] [blame] | 85 | hugetlb_report_usage(m, mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | unsigned long task_vsize(struct mm_struct *mm) |
| 89 | { |
| 90 | return PAGE_SIZE * mm->total_vm; |
| 91 | } |
| 92 | |
Alexey Dobriyan | a2ade7b | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 93 | unsigned long task_statm(struct mm_struct *mm, |
| 94 | unsigned long *shared, unsigned long *text, |
| 95 | unsigned long *data, unsigned long *resident) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
Jerome Marchand | eca56ff | 2016-01-14 15:19:26 -0800 | [diff] [blame] | 97 | *shared = get_mm_counter(mm, MM_FILEPAGES) + |
| 98 | get_mm_counter(mm, MM_SHMEMPAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) |
| 100 | >> PAGE_SHIFT; |
Konstantin Khlebnikov | 8463833 | 2016-01-14 15:22:07 -0800 | [diff] [blame] | 101 | *data = mm->data_vm + mm->stack_vm; |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 102 | *resident = *shared + get_mm_counter(mm, MM_ANONPAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | return mm->total_vm; |
| 104 | } |
| 105 | |
KAMEZAWA Hiroyuki | 9e78144 | 2012-10-19 17:00:55 +0900 | [diff] [blame] | 106 | #ifdef CONFIG_NUMA |
| 107 | /* |
Oleg Nesterov | 498f237 | 2014-10-09 15:27:52 -0700 | [diff] [blame] | 108 | * Save get_task_policy() for show_numa_map(). |
KAMEZAWA Hiroyuki | 9e78144 | 2012-10-19 17:00:55 +0900 | [diff] [blame] | 109 | */ |
| 110 | static void hold_task_mempolicy(struct proc_maps_private *priv) |
| 111 | { |
| 112 | struct task_struct *task = priv->task; |
| 113 | |
| 114 | task_lock(task); |
Oleg Nesterov | 498f237 | 2014-10-09 15:27:52 -0700 | [diff] [blame] | 115 | priv->task_mempolicy = get_task_policy(task); |
KAMEZAWA Hiroyuki | 9e78144 | 2012-10-19 17:00:55 +0900 | [diff] [blame] | 116 | mpol_get(priv->task_mempolicy); |
| 117 | task_unlock(task); |
| 118 | } |
| 119 | static void release_task_mempolicy(struct proc_maps_private *priv) |
| 120 | { |
| 121 | mpol_put(priv->task_mempolicy); |
| 122 | } |
| 123 | #else |
| 124 | static void hold_task_mempolicy(struct proc_maps_private *priv) |
| 125 | { |
| 126 | } |
| 127 | static void release_task_mempolicy(struct proc_maps_private *priv) |
| 128 | { |
| 129 | } |
| 130 | #endif |
| 131 | |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 132 | static void seq_print_vma_name(struct seq_file *m, struct vm_area_struct *vma) |
| 133 | { |
| 134 | const char __user *name = vma_get_anon_name(vma); |
| 135 | struct mm_struct *mm = vma->vm_mm; |
| 136 | |
| 137 | unsigned long page_start_vaddr; |
| 138 | unsigned long page_offset; |
| 139 | unsigned long num_pages; |
| 140 | unsigned long max_len = NAME_MAX; |
| 141 | int i; |
| 142 | |
| 143 | page_start_vaddr = (unsigned long)name & PAGE_MASK; |
| 144 | page_offset = (unsigned long)name - page_start_vaddr; |
| 145 | num_pages = DIV_ROUND_UP(page_offset + max_len, PAGE_SIZE); |
| 146 | |
| 147 | seq_puts(m, "[anon:"); |
| 148 | |
| 149 | for (i = 0; i < num_pages; i++) { |
| 150 | int len; |
| 151 | int write_len; |
| 152 | const char *kaddr; |
| 153 | long pages_pinned; |
| 154 | struct page *page; |
| 155 | |
| 156 | pages_pinned = get_user_pages_remote(current, mm, |
| 157 | page_start_vaddr, 1, 0, &page, NULL); |
| 158 | if (pages_pinned < 1) { |
| 159 | seq_puts(m, "<fault>]"); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | kaddr = (const char *)kmap(page); |
| 164 | len = min(max_len, PAGE_SIZE - page_offset); |
| 165 | write_len = strnlen(kaddr + page_offset, len); |
| 166 | seq_write(m, kaddr + page_offset, write_len); |
| 167 | kunmap(page); |
| 168 | put_page(page); |
| 169 | |
| 170 | /* if strnlen hit a null terminator then we're done */ |
| 171 | if (write_len != len) |
| 172 | break; |
| 173 | |
| 174 | max_len -= len; |
| 175 | page_offset = 0; |
| 176 | page_start_vaddr += PAGE_SIZE; |
| 177 | } |
| 178 | |
| 179 | seq_putc(m, ']'); |
| 180 | } |
| 181 | |
Oleg Nesterov | 59b4bf1 | 2014-10-09 15:25:28 -0700 | [diff] [blame] | 182 | static void vma_stop(struct proc_maps_private *priv) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 183 | { |
Oleg Nesterov | 59b4bf1 | 2014-10-09 15:25:28 -0700 | [diff] [blame] | 184 | struct mm_struct *mm = priv->mm; |
| 185 | |
| 186 | release_task_mempolicy(priv); |
| 187 | up_read(&mm->mmap_sem); |
| 188 | mmput(mm); |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Oleg Nesterov | ad2a00e | 2014-10-09 15:25:39 -0700 | [diff] [blame] | 191 | static struct vm_area_struct * |
| 192 | m_next_vma(struct proc_maps_private *priv, struct vm_area_struct *vma) |
| 193 | { |
| 194 | if (vma == priv->tail_vma) |
| 195 | return NULL; |
| 196 | return vma->vm_next ?: priv->tail_vma; |
| 197 | } |
| 198 | |
Oleg Nesterov | b8c20a9 | 2014-10-09 15:25:41 -0700 | [diff] [blame] | 199 | static void m_cache_vma(struct seq_file *m, struct vm_area_struct *vma) |
| 200 | { |
| 201 | if (m->count < m->size) /* vma is copied successfully */ |
Robert Ho | 855af07 | 2016-10-07 17:02:36 -0700 | [diff] [blame] | 202 | m->version = m_next_vma(m->private, vma) ? vma->vm_end : -1UL; |
Oleg Nesterov | b8c20a9 | 2014-10-09 15:25:41 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Oleg Nesterov | 0c25532 | 2014-10-09 15:25:36 -0700 | [diff] [blame] | 205 | static void *m_start(struct seq_file *m, loff_t *ppos) |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 206 | { |
| 207 | struct proc_maps_private *priv = m->private; |
Oleg Nesterov | b8c20a9 | 2014-10-09 15:25:41 -0700 | [diff] [blame] | 208 | unsigned long last_addr = m->version; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 209 | struct mm_struct *mm; |
Oleg Nesterov | 0c25532 | 2014-10-09 15:25:36 -0700 | [diff] [blame] | 210 | struct vm_area_struct *vma; |
| 211 | unsigned int pos = *ppos; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 212 | |
Oleg Nesterov | b8c20a9 | 2014-10-09 15:25:41 -0700 | [diff] [blame] | 213 | /* See m_cache_vma(). Zero at the start or after lseek. */ |
| 214 | if (last_addr == -1UL) |
| 215 | return NULL; |
| 216 | |
Oleg Nesterov | 2c03376 | 2014-10-09 15:25:51 -0700 | [diff] [blame] | 217 | priv->task = get_proc_task(priv->inode); |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 218 | if (!priv->task) |
Al Viro | ec6fd8a | 2011-02-15 22:22:54 -0500 | [diff] [blame] | 219 | return ERR_PTR(-ESRCH); |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 220 | |
Oleg Nesterov | 29a40ac | 2014-10-09 15:25:26 -0700 | [diff] [blame] | 221 | mm = priv->mm; |
| 222 | if (!mm || !atomic_inc_not_zero(&mm->mm_users)) |
| 223 | return NULL; |
Oleg Nesterov | 0c25532 | 2014-10-09 15:25:36 -0700 | [diff] [blame] | 224 | |
Oleg Nesterov | 00f89d2 | 2009-07-10 03:27:38 +0200 | [diff] [blame] | 225 | down_read(&mm->mmap_sem); |
KAMEZAWA Hiroyuki | 9e78144 | 2012-10-19 17:00:55 +0900 | [diff] [blame] | 226 | hold_task_mempolicy(priv); |
Oleg Nesterov | 0c25532 | 2014-10-09 15:25:36 -0700 | [diff] [blame] | 227 | priv->tail_vma = get_gate_vma(mm); |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 228 | |
Oleg Nesterov | b8c20a9 | 2014-10-09 15:25:41 -0700 | [diff] [blame] | 229 | if (last_addr) { |
Robert Ho | 855af07 | 2016-10-07 17:02:36 -0700 | [diff] [blame] | 230 | vma = find_vma(mm, last_addr - 1); |
| 231 | if (vma && vma->vm_start <= last_addr) |
| 232 | vma = m_next_vma(priv, vma); |
| 233 | if (vma) |
Oleg Nesterov | b8c20a9 | 2014-10-09 15:25:41 -0700 | [diff] [blame] | 234 | return vma; |
| 235 | } |
| 236 | |
| 237 | m->version = 0; |
Oleg Nesterov | 0c25532 | 2014-10-09 15:25:36 -0700 | [diff] [blame] | 238 | if (pos < mm->map_count) { |
Oleg Nesterov | 557c2d8 | 2014-10-09 15:25:43 -0700 | [diff] [blame] | 239 | for (vma = mm->mmap; pos; pos--) { |
| 240 | m->version = vma->vm_start; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 241 | vma = vma->vm_next; |
Oleg Nesterov | 557c2d8 | 2014-10-09 15:25:43 -0700 | [diff] [blame] | 242 | } |
Oleg Nesterov | 0c25532 | 2014-10-09 15:25:36 -0700 | [diff] [blame] | 243 | return vma; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Oleg Nesterov | 557c2d8 | 2014-10-09 15:25:43 -0700 | [diff] [blame] | 246 | /* we do not bother to update m->version in this case */ |
Oleg Nesterov | 0c25532 | 2014-10-09 15:25:36 -0700 | [diff] [blame] | 247 | if (pos == mm->map_count && priv->tail_vma) |
| 248 | return priv->tail_vma; |
Oleg Nesterov | 59b4bf1 | 2014-10-09 15:25:28 -0700 | [diff] [blame] | 249 | |
| 250 | vma_stop(priv); |
| 251 | return NULL; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static void *m_next(struct seq_file *m, void *v, loff_t *pos) |
| 255 | { |
| 256 | struct proc_maps_private *priv = m->private; |
Oleg Nesterov | ad2a00e | 2014-10-09 15:25:39 -0700 | [diff] [blame] | 257 | struct vm_area_struct *next; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 258 | |
| 259 | (*pos)++; |
Oleg Nesterov | ad2a00e | 2014-10-09 15:25:39 -0700 | [diff] [blame] | 260 | next = m_next_vma(priv, v); |
Oleg Nesterov | 59b4bf1 | 2014-10-09 15:25:28 -0700 | [diff] [blame] | 261 | if (!next) |
| 262 | vma_stop(priv); |
| 263 | return next; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static void m_stop(struct seq_file *m, void *v) |
| 267 | { |
| 268 | struct proc_maps_private *priv = m->private; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 269 | |
Oleg Nesterov | 59b4bf1 | 2014-10-09 15:25:28 -0700 | [diff] [blame] | 270 | if (!IS_ERR_OR_NULL(v)) |
| 271 | vma_stop(priv); |
Oleg Nesterov | 0d5f5f4 | 2014-10-09 15:25:32 -0700 | [diff] [blame] | 272 | if (priv->task) { |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 273 | put_task_struct(priv->task); |
Oleg Nesterov | 0d5f5f4 | 2014-10-09 15:25:32 -0700 | [diff] [blame] | 274 | priv->task = NULL; |
| 275 | } |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Oleg Nesterov | 4db7d0e | 2014-10-09 15:25:21 -0700 | [diff] [blame] | 278 | static int proc_maps_open(struct inode *inode, struct file *file, |
| 279 | const struct seq_operations *ops, int psize) |
| 280 | { |
| 281 | struct proc_maps_private *priv = __seq_open_private(file, ops, psize); |
| 282 | |
| 283 | if (!priv) |
| 284 | return -ENOMEM; |
| 285 | |
Oleg Nesterov | 2c03376 | 2014-10-09 15:25:51 -0700 | [diff] [blame] | 286 | priv->inode = inode; |
Oleg Nesterov | 29a40ac | 2014-10-09 15:25:26 -0700 | [diff] [blame] | 287 | priv->mm = proc_mem_open(inode, PTRACE_MODE_READ); |
| 288 | if (IS_ERR(priv->mm)) { |
| 289 | int err = PTR_ERR(priv->mm); |
| 290 | |
| 291 | seq_release_private(inode, file); |
| 292 | return err; |
| 293 | } |
| 294 | |
Oleg Nesterov | 4db7d0e | 2014-10-09 15:25:21 -0700 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
Oleg Nesterov | 29a40ac | 2014-10-09 15:25:26 -0700 | [diff] [blame] | 298 | static int proc_map_release(struct inode *inode, struct file *file) |
| 299 | { |
| 300 | struct seq_file *seq = file->private_data; |
| 301 | struct proc_maps_private *priv = seq->private; |
| 302 | |
| 303 | if (priv->mm) |
| 304 | mmdrop(priv->mm); |
| 305 | |
| 306 | return seq_release_private(inode, file); |
| 307 | } |
| 308 | |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 309 | static int do_maps_open(struct inode *inode, struct file *file, |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 310 | const struct seq_operations *ops) |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 311 | { |
Oleg Nesterov | 4db7d0e | 2014-10-09 15:25:21 -0700 | [diff] [blame] | 312 | return proc_maps_open(inode, file, ops, |
| 313 | sizeof(struct proc_maps_private)); |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 314 | } |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 315 | |
Johannes Weiner | 65376df | 2016-02-02 16:57:29 -0800 | [diff] [blame] | 316 | /* |
| 317 | * Indicate if the VMA is a stack for the given task; for |
| 318 | * /proc/PID/maps that is the stack of the main task. |
| 319 | */ |
| 320 | static int is_stack(struct proc_maps_private *priv, |
Andy Lutomirski | b18cb64 | 2016-09-30 10:58:57 -0700 | [diff] [blame] | 321 | struct vm_area_struct *vma) |
Oleg Nesterov | 58cb654 | 2014-10-09 15:25:54 -0700 | [diff] [blame] | 322 | { |
Andy Lutomirski | b18cb64 | 2016-09-30 10:58:57 -0700 | [diff] [blame] | 323 | /* |
| 324 | * We make no effort to guess what a given thread considers to be |
| 325 | * its "stack". It's not even well-defined for programs written |
| 326 | * languages like Go. |
| 327 | */ |
| 328 | return vma->vm_start <= vma->vm_mm->start_stack && |
| 329 | vma->vm_end >= vma->vm_mm->start_stack; |
Oleg Nesterov | 58cb654 | 2014-10-09 15:25:54 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 332 | static void |
| 333 | 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] | 334 | { |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 335 | struct mm_struct *mm = vma->vm_mm; |
| 336 | struct file *file = vma->vm_file; |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 337 | struct proc_maps_private *priv = m->private; |
KOSAKI Motohiro | ca16d14 | 2011-05-26 19:16:19 +0900 | [diff] [blame] | 338 | vm_flags_t flags = vma->vm_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | unsigned long ino = 0; |
KAMEZAWA Hiroyuki | 6260a4b | 2009-04-06 19:00:30 -0700 | [diff] [blame] | 340 | unsigned long long pgoff = 0; |
Mikulas Patocka | a09a79f | 2011-05-09 13:01:09 +0200 | [diff] [blame] | 341 | unsigned long start, end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | dev_t dev = 0; |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 343 | const char *name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | if (file) { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 346 | struct inode *inode = file_inode(vma->vm_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | dev = inode->i_sb->s_dev; |
| 348 | ino = inode->i_ino; |
KAMEZAWA Hiroyuki | 6260a4b | 2009-04-06 19:00:30 -0700 | [diff] [blame] | 349 | pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Linus Torvalds | d782437 | 2010-08-15 11:35:52 -0700 | [diff] [blame] | 352 | /* We don't show the stack guard page in /proc/maps */ |
| 353 | start = vma->vm_start; |
Mikulas Patocka | a09a79f | 2011-05-09 13:01:09 +0200 | [diff] [blame] | 354 | end = vma->vm_end; |
Linus Torvalds | d782437 | 2010-08-15 11:35:52 -0700 | [diff] [blame] | 355 | |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 356 | seq_setwidth(m, 25 + sizeof(void *) * 6 - 1); |
| 357 | 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] | 358 | start, |
Mikulas Patocka | a09a79f | 2011-05-09 13:01:09 +0200 | [diff] [blame] | 359 | end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | flags & VM_READ ? 'r' : '-', |
| 361 | flags & VM_WRITE ? 'w' : '-', |
| 362 | flags & VM_EXEC ? 'x' : '-', |
| 363 | flags & VM_MAYSHARE ? 's' : 'p', |
KAMEZAWA Hiroyuki | 6260a4b | 2009-04-06 19:00:30 -0700 | [diff] [blame] | 364 | pgoff, |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 365 | MAJOR(dev), MINOR(dev), ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
| 367 | /* |
| 368 | * Print the dentry name for named mappings, and a |
| 369 | * special [heap] marker for the heap: |
| 370 | */ |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 371 | if (file) { |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 372 | seq_pad(m, ' '); |
Miklos Szeredi | 2726d56 | 2015-06-19 10:30:28 +0200 | [diff] [blame] | 373 | seq_file_path(m, file, "\n"); |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 374 | goto done; |
| 375 | } |
| 376 | |
Andy Lutomirski | 78d683e | 2014-05-19 15:58:32 -0700 | [diff] [blame] | 377 | if (vma->vm_ops && vma->vm_ops->name) { |
| 378 | name = vma->vm_ops->name(vma); |
| 379 | if (name) |
| 380 | goto done; |
| 381 | } |
| 382 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 383 | name = arch_vma_name(vma); |
| 384 | if (!name) { |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 385 | if (!mm) { |
| 386 | name = "[vdso]"; |
| 387 | goto done; |
| 388 | } |
| 389 | |
| 390 | if (vma->vm_start <= mm->brk && |
| 391 | vma->vm_end >= mm->start_brk) { |
| 392 | name = "[heap]"; |
| 393 | goto done; |
| 394 | } |
| 395 | |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 396 | if (is_stack(priv, vma)) { |
Johannes Weiner | 65376df | 2016-02-02 16:57:29 -0800 | [diff] [blame] | 397 | name = "[stack]"; |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 398 | goto done; |
| 399 | } |
| 400 | |
| 401 | if (vma_get_anon_name(vma)) { |
| 402 | seq_pad(m, ' '); |
| 403 | seq_print_vma_name(m, vma); |
| 404 | } |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | done: |
| 408 | if (name) { |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 409 | seq_pad(m, ' '); |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 410 | seq_puts(m, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | seq_putc(m, '\n'); |
Joe Korty | 7c88db0 | 2008-10-16 15:27:09 +0400 | [diff] [blame] | 413 | } |
| 414 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 415 | 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] | 416 | { |
Oleg Nesterov | ebb6cdd | 2014-10-09 15:25:34 -0700 | [diff] [blame] | 417 | show_map_vma(m, v, is_pid); |
Oleg Nesterov | b8c20a9 | 2014-10-09 15:25:41 -0700 | [diff] [blame] | 418 | m_cache_vma(m, v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 422 | static int show_pid_map(struct seq_file *m, void *v) |
| 423 | { |
| 424 | return show_map(m, v, 1); |
| 425 | } |
| 426 | |
| 427 | static int show_tid_map(struct seq_file *m, void *v) |
| 428 | { |
| 429 | return show_map(m, v, 0); |
| 430 | } |
| 431 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 432 | static const struct seq_operations proc_pid_maps_op = { |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 433 | .start = m_start, |
| 434 | .next = m_next, |
| 435 | .stop = m_stop, |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 436 | .show = show_pid_map |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 437 | }; |
| 438 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 439 | static const struct seq_operations proc_tid_maps_op = { |
| 440 | .start = m_start, |
| 441 | .next = m_next, |
| 442 | .stop = m_stop, |
| 443 | .show = show_tid_map |
| 444 | }; |
| 445 | |
| 446 | static int pid_maps_open(struct inode *inode, struct file *file) |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 447 | { |
| 448 | return do_maps_open(inode, file, &proc_pid_maps_op); |
| 449 | } |
| 450 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 451 | static int tid_maps_open(struct inode *inode, struct file *file) |
| 452 | { |
| 453 | return do_maps_open(inode, file, &proc_tid_maps_op); |
| 454 | } |
| 455 | |
| 456 | const struct file_operations proc_pid_maps_operations = { |
| 457 | .open = pid_maps_open, |
| 458 | .read = seq_read, |
| 459 | .llseek = seq_lseek, |
Oleg Nesterov | 29a40ac | 2014-10-09 15:25:26 -0700 | [diff] [blame] | 460 | .release = proc_map_release, |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | const struct file_operations proc_tid_maps_operations = { |
| 464 | .open = tid_maps_open, |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 465 | .read = seq_read, |
| 466 | .llseek = seq_lseek, |
Oleg Nesterov | 29a40ac | 2014-10-09 15:25:26 -0700 | [diff] [blame] | 467 | .release = proc_map_release, |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 468 | }; |
| 469 | |
| 470 | /* |
| 471 | * Proportional Set Size(PSS): my share of RSS. |
| 472 | * |
| 473 | * PSS of a process is the count of pages it has in memory, where each |
| 474 | * page is divided by the number of processes sharing it. So if a |
| 475 | * process has 1000 pages all to itself, and 1000 shared with one other |
| 476 | * process, its PSS will be 1500. |
| 477 | * |
| 478 | * To keep (accumulated) division errors low, we adopt a 64bit |
| 479 | * fixed-point pss counter to minimize division errors. So (pss >> |
| 480 | * PSS_SHIFT) would be the real byte count. |
| 481 | * |
| 482 | * A shift of 12 before division means (assuming 4K page size): |
| 483 | * - 1M 3-user-pages add up to 8KB errors; |
| 484 | * - supports mapcount up to 2^24, or 16M; |
| 485 | * - supports PSS up to 2^52 bytes, or 4PB. |
| 486 | */ |
| 487 | #define PSS_SHIFT 12 |
| 488 | |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 489 | #ifdef CONFIG_PROC_PAGE_MONITOR |
Peter Zijlstra | 214e471 | 2008-04-28 02:12:55 -0700 | [diff] [blame] | 490 | struct mem_size_stats { |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 491 | unsigned long resident; |
| 492 | unsigned long shared_clean; |
| 493 | unsigned long shared_dirty; |
| 494 | unsigned long private_clean; |
| 495 | unsigned long private_dirty; |
| 496 | unsigned long referenced; |
Nikanth Karthikesan | b40d4f8 | 2010-10-27 15:34:10 -0700 | [diff] [blame] | 497 | unsigned long anonymous; |
Dave Hansen | 4031a21 | 2011-03-22 16:33:01 -0700 | [diff] [blame] | 498 | unsigned long anonymous_thp; |
Kirill A. Shutemov | 65c4537 | 2016-07-26 15:26:10 -0700 | [diff] [blame] | 499 | unsigned long shmem_thp; |
Peter Zijlstra | 214e471 | 2008-04-28 02:12:55 -0700 | [diff] [blame] | 500 | unsigned long swap; |
Naoya Horiguchi | 25ee01a | 2015-11-05 18:47:11 -0800 | [diff] [blame] | 501 | unsigned long shared_hugetlb; |
| 502 | unsigned long private_hugetlb; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 503 | u64 pss; |
Minchan Kim | 8334b96 | 2015-09-08 15:00:24 -0700 | [diff] [blame] | 504 | u64 swap_pss; |
Vlastimil Babka | c261e7d | 2016-01-14 15:19:17 -0800 | [diff] [blame] | 505 | bool check_shmem_swap; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 506 | }; |
| 507 | |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 508 | static void smaps_account(struct mem_size_stats *mss, struct page *page, |
Kirill A. Shutemov | afd9883 | 2016-01-15 16:52:13 -0800 | [diff] [blame] | 509 | bool compound, bool young, bool dirty) |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 510 | { |
Kirill A. Shutemov | f4be615 | 2016-01-20 14:58:12 -0800 | [diff] [blame] | 511 | int i, nr = compound ? 1 << compound_order(page) : 1; |
Kirill A. Shutemov | afd9883 | 2016-01-15 16:52:13 -0800 | [diff] [blame] | 512 | unsigned long size = nr * PAGE_SIZE; |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 513 | |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 514 | if (PageAnon(page)) |
| 515 | mss->anonymous += size; |
| 516 | |
| 517 | mss->resident += size; |
| 518 | /* Accumulate the size in pages that have been accessed. */ |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 519 | if (young || page_is_young(page) || PageReferenced(page)) |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 520 | mss->referenced += size; |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 521 | |
Kirill A. Shutemov | afd9883 | 2016-01-15 16:52:13 -0800 | [diff] [blame] | 522 | /* |
| 523 | * page_count(page) == 1 guarantees the page is mapped exactly once. |
| 524 | * If any subpage of the compound page mapped with PTE it would elevate |
| 525 | * page_count(). |
| 526 | */ |
| 527 | if (page_count(page) == 1) { |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 528 | if (dirty || PageDirty(page)) |
| 529 | mss->private_dirty += size; |
| 530 | else |
| 531 | mss->private_clean += size; |
| 532 | mss->pss += (u64)size << PSS_SHIFT; |
Kirill A. Shutemov | afd9883 | 2016-01-15 16:52:13 -0800 | [diff] [blame] | 533 | return; |
| 534 | } |
| 535 | |
| 536 | for (i = 0; i < nr; i++, page++) { |
| 537 | int mapcount = page_mapcount(page); |
| 538 | |
| 539 | if (mapcount >= 2) { |
| 540 | if (dirty || PageDirty(page)) |
| 541 | mss->shared_dirty += PAGE_SIZE; |
| 542 | else |
| 543 | mss->shared_clean += PAGE_SIZE; |
| 544 | mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount; |
| 545 | } else { |
| 546 | if (dirty || PageDirty(page)) |
| 547 | mss->private_dirty += PAGE_SIZE; |
| 548 | else |
| 549 | mss->private_clean += PAGE_SIZE; |
| 550 | mss->pss += PAGE_SIZE << PSS_SHIFT; |
| 551 | } |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
Vlastimil Babka | c261e7d | 2016-01-14 15:19:17 -0800 | [diff] [blame] | 555 | #ifdef CONFIG_SHMEM |
Vlastimil Babka | c261e7d | 2016-01-14 15:19:17 -0800 | [diff] [blame] | 556 | static int smaps_pte_hole(unsigned long addr, unsigned long end, |
| 557 | struct mm_walk *walk) |
| 558 | { |
| 559 | struct mem_size_stats *mss = walk->private; |
| 560 | |
Vlastimil Babka | 48131e0 | 2016-01-14 15:19:23 -0800 | [diff] [blame] | 561 | mss->swap += shmem_partial_swap_usage( |
| 562 | walk->vma->vm_file->f_mapping, addr, end); |
Vlastimil Babka | c261e7d | 2016-01-14 15:19:17 -0800 | [diff] [blame] | 563 | |
| 564 | return 0; |
| 565 | } |
Vlastimil Babka | c261e7d | 2016-01-14 15:19:17 -0800 | [diff] [blame] | 566 | #endif |
| 567 | |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 568 | static void smaps_pte_entry(pte_t *pte, unsigned long addr, |
| 569 | struct mm_walk *walk) |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 570 | { |
| 571 | struct mem_size_stats *mss = walk->private; |
Naoya Horiguchi | 14eb6fd | 2015-02-11 15:27:43 -0800 | [diff] [blame] | 572 | struct vm_area_struct *vma = walk->vma; |
Konstantin Khlebnikov | b1d4d9e | 2012-05-31 16:26:20 -0700 | [diff] [blame] | 573 | struct page *page = NULL; |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 574 | |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 575 | if (pte_present(*pte)) { |
| 576 | page = vm_normal_page(vma, addr, *pte); |
| 577 | } else if (is_swap_pte(*pte)) { |
| 578 | swp_entry_t swpent = pte_to_swp_entry(*pte); |
Konstantin Khlebnikov | b1d4d9e | 2012-05-31 16:26:20 -0700 | [diff] [blame] | 579 | |
Minchan Kim | 8334b96 | 2015-09-08 15:00:24 -0700 | [diff] [blame] | 580 | if (!non_swap_entry(swpent)) { |
| 581 | int mapcount; |
| 582 | |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 583 | mss->swap += PAGE_SIZE; |
Minchan Kim | 8334b96 | 2015-09-08 15:00:24 -0700 | [diff] [blame] | 584 | mapcount = swp_swapcount(swpent); |
| 585 | if (mapcount >= 2) { |
| 586 | u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT; |
| 587 | |
| 588 | do_div(pss_delta, mapcount); |
| 589 | mss->swap_pss += pss_delta; |
| 590 | } else { |
| 591 | mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT; |
| 592 | } |
| 593 | } else if (is_migration_entry(swpent)) |
Konstantin Khlebnikov | b1d4d9e | 2012-05-31 16:26:20 -0700 | [diff] [blame] | 594 | page = migration_entry_to_page(swpent); |
Vlastimil Babka | c261e7d | 2016-01-14 15:19:17 -0800 | [diff] [blame] | 595 | } else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap |
| 596 | && pte_none(*pte))) { |
Vlastimil Babka | 48131e0 | 2016-01-14 15:19:23 -0800 | [diff] [blame] | 597 | page = find_get_entry(vma->vm_file->f_mapping, |
| 598 | linear_page_index(vma, addr)); |
| 599 | if (!page) |
| 600 | return; |
| 601 | |
| 602 | if (radix_tree_exceptional_entry(page)) |
| 603 | mss->swap += PAGE_SIZE; |
| 604 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 605 | put_page(page); |
Vlastimil Babka | 48131e0 | 2016-01-14 15:19:23 -0800 | [diff] [blame] | 606 | |
| 607 | return; |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 610 | if (!page) |
| 611 | return; |
Kirill A. Shutemov | afd9883 | 2016-01-15 16:52:13 -0800 | [diff] [blame] | 612 | |
| 613 | smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte)); |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 616 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 617 | static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, |
| 618 | struct mm_walk *walk) |
| 619 | { |
| 620 | struct mem_size_stats *mss = walk->private; |
Naoya Horiguchi | 14eb6fd | 2015-02-11 15:27:43 -0800 | [diff] [blame] | 621 | struct vm_area_struct *vma = walk->vma; |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 622 | struct page *page; |
| 623 | |
| 624 | /* FOLL_DUMP will return -EFAULT on huge zero page */ |
| 625 | page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP); |
| 626 | if (IS_ERR_OR_NULL(page)) |
| 627 | return; |
Kirill A. Shutemov | 65c4537 | 2016-07-26 15:26:10 -0700 | [diff] [blame] | 628 | if (PageAnon(page)) |
| 629 | mss->anonymous_thp += HPAGE_PMD_SIZE; |
| 630 | else if (PageSwapBacked(page)) |
| 631 | mss->shmem_thp += HPAGE_PMD_SIZE; |
Dan Williams | ca120cf | 2016-09-03 10:38:03 -0700 | [diff] [blame] | 632 | else if (is_zone_device_page(page)) |
| 633 | /* pass */; |
Kirill A. Shutemov | 65c4537 | 2016-07-26 15:26:10 -0700 | [diff] [blame] | 634 | else |
| 635 | VM_BUG_ON_PAGE(1, page); |
Kirill A. Shutemov | afd9883 | 2016-01-15 16:52:13 -0800 | [diff] [blame] | 636 | smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd)); |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 637 | } |
| 638 | #else |
| 639 | static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, |
| 640 | struct mm_walk *walk) |
| 641 | { |
| 642 | } |
| 643 | #endif |
| 644 | |
Matt Mackall | b3ae5ac | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 645 | 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] | 646 | struct mm_walk *walk) |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 647 | { |
Naoya Horiguchi | 14eb6fd | 2015-02-11 15:27:43 -0800 | [diff] [blame] | 648 | struct vm_area_struct *vma = walk->vma; |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 649 | pte_t *pte; |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 650 | spinlock_t *ptl; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 651 | |
Kirill A. Shutemov | b6ec57f | 2016-01-21 16:40:25 -0800 | [diff] [blame] | 652 | ptl = pmd_trans_huge_lock(pmd, vma); |
| 653 | if (ptl) { |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 654 | smaps_pmd_entry(pmd, addr, walk); |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 655 | spin_unlock(ptl); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 656 | return 0; |
Dave Hansen | 22e057c | 2011-03-22 16:33:00 -0700 | [diff] [blame] | 657 | } |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 658 | |
| 659 | if (pmd_trans_unstable(pmd)) |
| 660 | return 0; |
Dave Hansen | 22e057c | 2011-03-22 16:33:00 -0700 | [diff] [blame] | 661 | /* |
| 662 | * The mmap_sem held all the way back in m_start() is what |
| 663 | * keeps khugepaged out of here and from collapsing things |
| 664 | * in here. |
| 665 | */ |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 666 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
Dave Hansen | ae11c4d | 2011-03-22 16:32:58 -0700 | [diff] [blame] | 667 | for (; addr != end; pte++, addr += PAGE_SIZE) |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 668 | smaps_pte_entry(pte, addr, walk); |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 669 | pte_unmap_unlock(pte - 1, ptl); |
| 670 | cond_resched(); |
Matt Mackall | b3ae5ac | 2008-02-04 22:29:01 -0800 | [diff] [blame] | 671 | return 0; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Cyrill Gorcunov | 834f82e | 2012-12-17 16:03:13 -0800 | [diff] [blame] | 674 | static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) |
| 675 | { |
| 676 | /* |
| 677 | * Don't forget to update Documentation/ on changes. |
| 678 | */ |
| 679 | static const char mnemonics[BITS_PER_LONG][2] = { |
| 680 | /* |
| 681 | * In case if we meet a flag we don't know about. |
| 682 | */ |
| 683 | [0 ... (BITS_PER_LONG-1)] = "??", |
| 684 | |
| 685 | [ilog2(VM_READ)] = "rd", |
| 686 | [ilog2(VM_WRITE)] = "wr", |
| 687 | [ilog2(VM_EXEC)] = "ex", |
| 688 | [ilog2(VM_SHARED)] = "sh", |
| 689 | [ilog2(VM_MAYREAD)] = "mr", |
| 690 | [ilog2(VM_MAYWRITE)] = "mw", |
| 691 | [ilog2(VM_MAYEXEC)] = "me", |
| 692 | [ilog2(VM_MAYSHARE)] = "ms", |
| 693 | [ilog2(VM_GROWSDOWN)] = "gd", |
| 694 | [ilog2(VM_PFNMAP)] = "pf", |
| 695 | [ilog2(VM_DENYWRITE)] = "dw", |
Qiaowei Ren | 4aae7e4 | 2014-11-14 07:18:25 -0800 | [diff] [blame] | 696 | #ifdef CONFIG_X86_INTEL_MPX |
| 697 | [ilog2(VM_MPX)] = "mp", |
| 698 | #endif |
Cyrill Gorcunov | 834f82e | 2012-12-17 16:03:13 -0800 | [diff] [blame] | 699 | [ilog2(VM_LOCKED)] = "lo", |
| 700 | [ilog2(VM_IO)] = "io", |
| 701 | [ilog2(VM_SEQ_READ)] = "sr", |
| 702 | [ilog2(VM_RAND_READ)] = "rr", |
| 703 | [ilog2(VM_DONTCOPY)] = "dc", |
| 704 | [ilog2(VM_DONTEXPAND)] = "de", |
| 705 | [ilog2(VM_ACCOUNT)] = "ac", |
| 706 | [ilog2(VM_NORESERVE)] = "nr", |
| 707 | [ilog2(VM_HUGETLB)] = "ht", |
Cyrill Gorcunov | 834f82e | 2012-12-17 16:03:13 -0800 | [diff] [blame] | 708 | [ilog2(VM_ARCH_1)] = "ar", |
| 709 | [ilog2(VM_DONTDUMP)] = "dd", |
Naoya Horiguchi | ec8e41a | 2013-11-12 15:07:49 -0800 | [diff] [blame] | 710 | #ifdef CONFIG_MEM_SOFT_DIRTY |
| 711 | [ilog2(VM_SOFTDIRTY)] = "sd", |
| 712 | #endif |
Cyrill Gorcunov | 834f82e | 2012-12-17 16:03:13 -0800 | [diff] [blame] | 713 | [ilog2(VM_MIXEDMAP)] = "mm", |
| 714 | [ilog2(VM_HUGEPAGE)] = "hg", |
| 715 | [ilog2(VM_NOHUGEPAGE)] = "nh", |
| 716 | [ilog2(VM_MERGEABLE)] = "mg", |
Andrea Arcangeli | 16ba6f8 | 2015-09-04 15:46:17 -0700 | [diff] [blame] | 717 | [ilog2(VM_UFFD_MISSING)]= "um", |
| 718 | [ilog2(VM_UFFD_WP)] = "uw", |
Dave Hansen | c1192f8 | 2016-02-12 13:02:27 -0800 | [diff] [blame] | 719 | #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS |
| 720 | /* These come out via ProtectionKey: */ |
| 721 | [ilog2(VM_PKEY_BIT0)] = "", |
| 722 | [ilog2(VM_PKEY_BIT1)] = "", |
| 723 | [ilog2(VM_PKEY_BIT2)] = "", |
| 724 | [ilog2(VM_PKEY_BIT3)] = "", |
| 725 | #endif |
Cyrill Gorcunov | 834f82e | 2012-12-17 16:03:13 -0800 | [diff] [blame] | 726 | }; |
| 727 | size_t i; |
| 728 | |
| 729 | seq_puts(m, "VmFlags: "); |
| 730 | for (i = 0; i < BITS_PER_LONG; i++) { |
Dave Hansen | c1192f8 | 2016-02-12 13:02:27 -0800 | [diff] [blame] | 731 | if (!mnemonics[i][0]) |
| 732 | continue; |
Cyrill Gorcunov | 834f82e | 2012-12-17 16:03:13 -0800 | [diff] [blame] | 733 | if (vma->vm_flags & (1UL << i)) { |
| 734 | seq_printf(m, "%c%c ", |
| 735 | mnemonics[i][0], mnemonics[i][1]); |
| 736 | } |
| 737 | } |
| 738 | seq_putc(m, '\n'); |
| 739 | } |
| 740 | |
Naoya Horiguchi | 25ee01a | 2015-11-05 18:47:11 -0800 | [diff] [blame] | 741 | #ifdef CONFIG_HUGETLB_PAGE |
| 742 | static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask, |
| 743 | unsigned long addr, unsigned long end, |
| 744 | struct mm_walk *walk) |
| 745 | { |
| 746 | struct mem_size_stats *mss = walk->private; |
| 747 | struct vm_area_struct *vma = walk->vma; |
| 748 | struct page *page = NULL; |
| 749 | |
| 750 | if (pte_present(*pte)) { |
| 751 | page = vm_normal_page(vma, addr, *pte); |
| 752 | } else if (is_swap_pte(*pte)) { |
| 753 | swp_entry_t swpent = pte_to_swp_entry(*pte); |
| 754 | |
| 755 | if (is_migration_entry(swpent)) |
| 756 | page = migration_entry_to_page(swpent); |
| 757 | } |
| 758 | if (page) { |
| 759 | int mapcount = page_mapcount(page); |
| 760 | |
| 761 | if (mapcount >= 2) |
| 762 | mss->shared_hugetlb += huge_page_size(hstate_vma(vma)); |
| 763 | else |
| 764 | mss->private_hugetlb += huge_page_size(hstate_vma(vma)); |
| 765 | } |
| 766 | return 0; |
| 767 | } |
| 768 | #endif /* HUGETLB_PAGE */ |
| 769 | |
Dave Hansen | c1192f8 | 2016-02-12 13:02:27 -0800 | [diff] [blame] | 770 | void __weak arch_show_smap(struct seq_file *m, struct vm_area_struct *vma) |
| 771 | { |
| 772 | } |
| 773 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 774 | 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] | 775 | { |
| 776 | struct vm_area_struct *vma = v; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 777 | struct mem_size_stats mss; |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 778 | struct mm_walk smaps_walk = { |
| 779 | .pmd_entry = smaps_pte_range, |
Naoya Horiguchi | 25ee01a | 2015-11-05 18:47:11 -0800 | [diff] [blame] | 780 | #ifdef CONFIG_HUGETLB_PAGE |
| 781 | .hugetlb_entry = smaps_hugetlb_range, |
| 782 | #endif |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 783 | .mm = vma->vm_mm, |
| 784 | .private = &mss, |
| 785 | }; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 786 | |
| 787 | memset(&mss, 0, sizeof mss); |
Vlastimil Babka | c261e7d | 2016-01-14 15:19:17 -0800 | [diff] [blame] | 788 | |
| 789 | #ifdef CONFIG_SHMEM |
| 790 | if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) { |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 791 | /* |
| 792 | * For shared or readonly shmem mappings we know that all |
| 793 | * swapped out pages belong to the shmem object, and we can |
| 794 | * obtain the swap value much more efficiently. For private |
| 795 | * writable mappings, we might have COW pages that are |
| 796 | * not affected by the parent swapped out pages of the shmem |
| 797 | * object, so we have to distinguish them during the page walk. |
| 798 | * Unless we know that the shmem object (or the part mapped by |
| 799 | * our VMA) has no swapped out pages at all. |
| 800 | */ |
| 801 | unsigned long shmem_swapped = shmem_swap_usage(vma); |
| 802 | |
| 803 | if (!shmem_swapped || (vma->vm_flags & VM_SHARED) || |
| 804 | !(vma->vm_flags & VM_WRITE)) { |
| 805 | mss.swap = shmem_swapped; |
| 806 | } else { |
| 807 | mss.check_shmem_swap = true; |
| 808 | smaps_walk.pte_hole = smaps_pte_hole; |
| 809 | } |
Vlastimil Babka | c261e7d | 2016-01-14 15:19:17 -0800 | [diff] [blame] | 810 | } |
| 811 | #endif |
| 812 | |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 813 | /* mmap_sem is held in m_start */ |
Naoya Horiguchi | 14eb6fd | 2015-02-11 15:27:43 -0800 | [diff] [blame] | 814 | walk_page_vma(vma, &smaps_walk); |
Matt Mackall | 4752c36 | 2008-02-04 22:29:02 -0800 | [diff] [blame] | 815 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 816 | show_map_vma(m, vma, is_pid); |
Matt Mackall | 4752c36 | 2008-02-04 22:29:02 -0800 | [diff] [blame] | 817 | |
Colin Cross | 3e4578f | 2015-10-27 16:42:08 -0700 | [diff] [blame] | 818 | if (vma_get_anon_name(vma)) { |
| 819 | seq_puts(m, "Name: "); |
| 820 | seq_print_vma_name(m, vma); |
| 821 | seq_putc(m, '\n'); |
| 822 | } |
| 823 | |
Matt Mackall | 4752c36 | 2008-02-04 22:29:02 -0800 | [diff] [blame] | 824 | seq_printf(m, |
| 825 | "Size: %8lu kB\n" |
| 826 | "Rss: %8lu kB\n" |
| 827 | "Pss: %8lu kB\n" |
| 828 | "Shared_Clean: %8lu kB\n" |
| 829 | "Shared_Dirty: %8lu kB\n" |
| 830 | "Private_Clean: %8lu kB\n" |
| 831 | "Private_Dirty: %8lu kB\n" |
Peter Zijlstra | 214e471 | 2008-04-28 02:12:55 -0700 | [diff] [blame] | 832 | "Referenced: %8lu kB\n" |
Nikanth Karthikesan | b40d4f8 | 2010-10-27 15:34:10 -0700 | [diff] [blame] | 833 | "Anonymous: %8lu kB\n" |
Dave Hansen | 4031a21 | 2011-03-22 16:33:01 -0700 | [diff] [blame] | 834 | "AnonHugePages: %8lu kB\n" |
Kirill A. Shutemov | 65c4537 | 2016-07-26 15:26:10 -0700 | [diff] [blame] | 835 | "ShmemPmdMapped: %8lu kB\n" |
Naoya Horiguchi | 25ee01a | 2015-11-05 18:47:11 -0800 | [diff] [blame] | 836 | "Shared_Hugetlb: %8lu kB\n" |
| 837 | "Private_Hugetlb: %7lu kB\n" |
Mel Gorman | 08fba69 | 2009-01-06 14:38:53 -0800 | [diff] [blame] | 838 | "Swap: %8lu kB\n" |
Minchan Kim | 8334b96 | 2015-09-08 15:00:24 -0700 | [diff] [blame] | 839 | "SwapPss: %8lu kB\n" |
Mel Gorman | 3340289 | 2009-01-06 14:38:54 -0800 | [diff] [blame] | 840 | "KernelPageSize: %8lu kB\n" |
Nikanth Karthikesan | 2d90508 | 2011-01-13 15:45:53 -0800 | [diff] [blame] | 841 | "MMUPageSize: %8lu kB\n" |
| 842 | "Locked: %8lu kB\n", |
Matt Mackall | 4752c36 | 2008-02-04 22:29:02 -0800 | [diff] [blame] | 843 | (vma->vm_end - vma->vm_start) >> 10, |
| 844 | mss.resident >> 10, |
| 845 | (unsigned long)(mss.pss >> (10 + PSS_SHIFT)), |
| 846 | mss.shared_clean >> 10, |
| 847 | mss.shared_dirty >> 10, |
| 848 | mss.private_clean >> 10, |
| 849 | mss.private_dirty >> 10, |
Peter Zijlstra | 214e471 | 2008-04-28 02:12:55 -0700 | [diff] [blame] | 850 | mss.referenced >> 10, |
Nikanth Karthikesan | b40d4f8 | 2010-10-27 15:34:10 -0700 | [diff] [blame] | 851 | mss.anonymous >> 10, |
Dave Hansen | 4031a21 | 2011-03-22 16:33:01 -0700 | [diff] [blame] | 852 | mss.anonymous_thp >> 10, |
Kirill A. Shutemov | 65c4537 | 2016-07-26 15:26:10 -0700 | [diff] [blame] | 853 | mss.shmem_thp >> 10, |
Naoya Horiguchi | 25ee01a | 2015-11-05 18:47:11 -0800 | [diff] [blame] | 854 | mss.shared_hugetlb >> 10, |
| 855 | mss.private_hugetlb >> 10, |
Mel Gorman | 08fba69 | 2009-01-06 14:38:53 -0800 | [diff] [blame] | 856 | mss.swap >> 10, |
Minchan Kim | 8334b96 | 2015-09-08 15:00:24 -0700 | [diff] [blame] | 857 | (unsigned long)(mss.swap_pss >> (10 + PSS_SHIFT)), |
Mel Gorman | 3340289 | 2009-01-06 14:38:54 -0800 | [diff] [blame] | 858 | vma_kernel_pagesize(vma) >> 10, |
Nikanth Karthikesan | 2d90508 | 2011-01-13 15:45:53 -0800 | [diff] [blame] | 859 | vma_mmu_pagesize(vma) >> 10, |
| 860 | (vma->vm_flags & VM_LOCKED) ? |
| 861 | (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0); |
Matt Mackall | 4752c36 | 2008-02-04 22:29:02 -0800 | [diff] [blame] | 862 | |
Dave Hansen | c1192f8 | 2016-02-12 13:02:27 -0800 | [diff] [blame] | 863 | arch_show_smap(m, vma); |
Cyrill Gorcunov | 834f82e | 2012-12-17 16:03:13 -0800 | [diff] [blame] | 864 | show_smap_vma_flags(m, vma); |
Oleg Nesterov | b8c20a9 | 2014-10-09 15:25:41 -0700 | [diff] [blame] | 865 | m_cache_vma(m, vma); |
Joe Korty | 7c88db0 | 2008-10-16 15:27:09 +0400 | [diff] [blame] | 866 | return 0; |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 869 | static int show_pid_smap(struct seq_file *m, void *v) |
| 870 | { |
| 871 | return show_smap(m, v, 1); |
| 872 | } |
| 873 | |
| 874 | static int show_tid_smap(struct seq_file *m, void *v) |
| 875 | { |
| 876 | return show_smap(m, v, 0); |
| 877 | } |
| 878 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 879 | static const struct seq_operations proc_pid_smaps_op = { |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 880 | .start = m_start, |
| 881 | .next = m_next, |
| 882 | .stop = m_stop, |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 883 | .show = show_pid_smap |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 884 | }; |
| 885 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 886 | static const struct seq_operations proc_tid_smaps_op = { |
| 887 | .start = m_start, |
| 888 | .next = m_next, |
| 889 | .stop = m_stop, |
| 890 | .show = show_tid_smap |
| 891 | }; |
| 892 | |
| 893 | static int pid_smaps_open(struct inode *inode, struct file *file) |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 894 | { |
| 895 | return do_maps_open(inode, file, &proc_pid_smaps_op); |
| 896 | } |
| 897 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 898 | static int tid_smaps_open(struct inode *inode, struct file *file) |
| 899 | { |
| 900 | return do_maps_open(inode, file, &proc_tid_smaps_op); |
| 901 | } |
| 902 | |
| 903 | const struct file_operations proc_pid_smaps_operations = { |
| 904 | .open = pid_smaps_open, |
| 905 | .read = seq_read, |
| 906 | .llseek = seq_lseek, |
Oleg Nesterov | 29a40ac | 2014-10-09 15:25:26 -0700 | [diff] [blame] | 907 | .release = proc_map_release, |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 908 | }; |
| 909 | |
| 910 | const struct file_operations proc_tid_smaps_operations = { |
| 911 | .open = tid_smaps_open, |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 912 | .read = seq_read, |
| 913 | .llseek = seq_lseek, |
Oleg Nesterov | 29a40ac | 2014-10-09 15:25:26 -0700 | [diff] [blame] | 914 | .release = proc_map_release, |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 915 | }; |
| 916 | |
Pavel Emelyanov | 040fa02 | 2013-07-03 15:01:16 -0700 | [diff] [blame] | 917 | enum clear_refs_types { |
| 918 | CLEAR_REFS_ALL = 1, |
| 919 | CLEAR_REFS_ANON, |
| 920 | CLEAR_REFS_MAPPED, |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 921 | CLEAR_REFS_SOFT_DIRTY, |
Petr Cermak | 695f055 | 2015-02-12 15:01:00 -0800 | [diff] [blame] | 922 | CLEAR_REFS_MM_HIWATER_RSS, |
Pavel Emelyanov | 040fa02 | 2013-07-03 15:01:16 -0700 | [diff] [blame] | 923 | CLEAR_REFS_LAST, |
| 924 | }; |
| 925 | |
Pavel Emelyanov | af9de7e | 2013-07-03 15:01:18 -0700 | [diff] [blame] | 926 | struct clear_refs_private { |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 927 | enum clear_refs_types type; |
Pavel Emelyanov | af9de7e | 2013-07-03 15:01:18 -0700 | [diff] [blame] | 928 | }; |
| 929 | |
Kirill A. Shutemov | 7d5b3bf | 2015-02-11 15:28:08 -0800 | [diff] [blame] | 930 | #ifdef CONFIG_MEM_SOFT_DIRTY |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 931 | static inline void clear_soft_dirty(struct vm_area_struct *vma, |
| 932 | unsigned long addr, pte_t *pte) |
| 933 | { |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 934 | /* |
| 935 | * The soft-dirty tracker uses #PF-s to catch writes |
| 936 | * to pages, so write-protect the pte as well. See the |
| 937 | * Documentation/vm/soft-dirty.txt for full description |
| 938 | * of how soft-dirty works. |
| 939 | */ |
| 940 | pte_t ptent = *pte; |
Cyrill Gorcunov | 179ef71 | 2013-08-13 16:00:49 -0700 | [diff] [blame] | 941 | |
| 942 | if (pte_present(ptent)) { |
Laurent Dufour | 326c259 | 2015-11-05 18:49:21 -0800 | [diff] [blame] | 943 | ptent = ptep_modify_prot_start(vma->vm_mm, addr, pte); |
Cyrill Gorcunov | 179ef71 | 2013-08-13 16:00:49 -0700 | [diff] [blame] | 944 | ptent = pte_wrprotect(ptent); |
Martin Schwidefsky | a7b7617 | 2015-04-22 14:20:47 +0200 | [diff] [blame] | 945 | ptent = pte_clear_soft_dirty(ptent); |
Laurent Dufour | 326c259 | 2015-11-05 18:49:21 -0800 | [diff] [blame] | 946 | ptep_modify_prot_commit(vma->vm_mm, addr, pte, ptent); |
Cyrill Gorcunov | 179ef71 | 2013-08-13 16:00:49 -0700 | [diff] [blame] | 947 | } else if (is_swap_pte(ptent)) { |
| 948 | ptent = pte_swp_clear_soft_dirty(ptent); |
Laurent Dufour | 326c259 | 2015-11-05 18:49:21 -0800 | [diff] [blame] | 949 | set_pte_at(vma->vm_mm, addr, pte, ptent); |
Cyrill Gorcunov | 179ef71 | 2013-08-13 16:00:49 -0700 | [diff] [blame] | 950 | } |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 951 | } |
Laurent Dufour | 5d3875a | 2015-11-05 18:49:24 -0800 | [diff] [blame] | 952 | #else |
| 953 | static inline void clear_soft_dirty(struct vm_area_struct *vma, |
| 954 | unsigned long addr, pte_t *pte) |
| 955 | { |
| 956 | } |
| 957 | #endif |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 958 | |
Laurent Dufour | 5d3875a | 2015-11-05 18:49:24 -0800 | [diff] [blame] | 959 | #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE) |
Kirill A. Shutemov | 7d5b3bf | 2015-02-11 15:28:08 -0800 | [diff] [blame] | 960 | static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma, |
| 961 | unsigned long addr, pmd_t *pmdp) |
| 962 | { |
Kirill A. Shutemov | 5c9d083 | 2017-04-13 14:56:28 -0700 | [diff] [blame] | 963 | pmd_t pmd = *pmdp; |
| 964 | |
| 965 | /* See comment in change_huge_pmd() */ |
| 966 | pmdp_invalidate(vma, addr, pmdp); |
| 967 | if (pmd_dirty(*pmdp)) |
| 968 | pmd = pmd_mkdirty(pmd); |
| 969 | if (pmd_young(*pmdp)) |
| 970 | pmd = pmd_mkyoung(pmd); |
Kirill A. Shutemov | 7d5b3bf | 2015-02-11 15:28:08 -0800 | [diff] [blame] | 971 | |
| 972 | pmd = pmd_wrprotect(pmd); |
Martin Schwidefsky | a7b7617 | 2015-04-22 14:20:47 +0200 | [diff] [blame] | 973 | pmd = pmd_clear_soft_dirty(pmd); |
Kirill A. Shutemov | 7d5b3bf | 2015-02-11 15:28:08 -0800 | [diff] [blame] | 974 | |
Kirill A. Shutemov | 7d5b3bf | 2015-02-11 15:28:08 -0800 | [diff] [blame] | 975 | set_pmd_at(vma->vm_mm, addr, pmdp, pmd); |
| 976 | } |
Kirill A. Shutemov | 7d5b3bf | 2015-02-11 15:28:08 -0800 | [diff] [blame] | 977 | #else |
Kirill A. Shutemov | 7d5b3bf | 2015-02-11 15:28:08 -0800 | [diff] [blame] | 978 | static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma, |
| 979 | unsigned long addr, pmd_t *pmdp) |
| 980 | { |
| 981 | } |
| 982 | #endif |
| 983 | |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 984 | static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr, |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 985 | unsigned long end, struct mm_walk *walk) |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 986 | { |
Pavel Emelyanov | af9de7e | 2013-07-03 15:01:18 -0700 | [diff] [blame] | 987 | struct clear_refs_private *cp = walk->private; |
Naoya Horiguchi | 5c64f52 | 2015-02-11 15:27:46 -0800 | [diff] [blame] | 988 | struct vm_area_struct *vma = walk->vma; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 989 | pte_t *pte, ptent; |
| 990 | spinlock_t *ptl; |
| 991 | struct page *page; |
| 992 | |
Kirill A. Shutemov | b6ec57f | 2016-01-21 16:40:25 -0800 | [diff] [blame] | 993 | ptl = pmd_trans_huge_lock(pmd, vma); |
| 994 | if (ptl) { |
Kirill A. Shutemov | 7d5b3bf | 2015-02-11 15:28:08 -0800 | [diff] [blame] | 995 | if (cp->type == CLEAR_REFS_SOFT_DIRTY) { |
| 996 | clear_soft_dirty_pmd(vma, addr, pmd); |
| 997 | goto out; |
| 998 | } |
| 999 | |
| 1000 | page = pmd_page(*pmd); |
| 1001 | |
| 1002 | /* Clear accessed and referenced bits. */ |
| 1003 | pmdp_test_and_clear_young(vma, addr, pmd); |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 1004 | test_and_clear_page_young(page); |
Kirill A. Shutemov | 7d5b3bf | 2015-02-11 15:28:08 -0800 | [diff] [blame] | 1005 | ClearPageReferenced(page); |
| 1006 | out: |
| 1007 | spin_unlock(ptl); |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 1011 | if (pmd_trans_unstable(pmd)) |
| 1012 | return 0; |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 1013 | |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1014 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 1015 | for (; addr != end; pte++, addr += PAGE_SIZE) { |
| 1016 | ptent = *pte; |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1017 | |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 1018 | if (cp->type == CLEAR_REFS_SOFT_DIRTY) { |
| 1019 | clear_soft_dirty(vma, addr, pte); |
| 1020 | continue; |
| 1021 | } |
| 1022 | |
Cyrill Gorcunov | 179ef71 | 2013-08-13 16:00:49 -0700 | [diff] [blame] | 1023 | if (!pte_present(ptent)) |
| 1024 | continue; |
| 1025 | |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1026 | page = vm_normal_page(vma, addr, ptent); |
| 1027 | if (!page) |
| 1028 | continue; |
| 1029 | |
| 1030 | /* Clear accessed and referenced bits. */ |
| 1031 | ptep_test_and_clear_young(vma, addr, pte); |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 1032 | test_and_clear_page_young(page); |
Matt Mackall | a619879 | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1033 | ClearPageReferenced(page); |
| 1034 | } |
| 1035 | pte_unmap_unlock(pte - 1, ptl); |
| 1036 | cond_resched(); |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
Naoya Horiguchi | 5c64f52 | 2015-02-11 15:27:46 -0800 | [diff] [blame] | 1040 | static int clear_refs_test_walk(unsigned long start, unsigned long end, |
| 1041 | struct mm_walk *walk) |
| 1042 | { |
| 1043 | struct clear_refs_private *cp = walk->private; |
| 1044 | struct vm_area_struct *vma = walk->vma; |
| 1045 | |
Naoya Horiguchi | 48684a6 | 2015-02-11 15:28:06 -0800 | [diff] [blame] | 1046 | if (vma->vm_flags & VM_PFNMAP) |
| 1047 | return 1; |
| 1048 | |
Naoya Horiguchi | 5c64f52 | 2015-02-11 15:27:46 -0800 | [diff] [blame] | 1049 | /* |
| 1050 | * Writing 1 to /proc/pid/clear_refs affects all pages. |
| 1051 | * Writing 2 to /proc/pid/clear_refs only affects anonymous pages. |
| 1052 | * Writing 3 to /proc/pid/clear_refs only affects file mapped pages. |
| 1053 | * Writing 4 to /proc/pid/clear_refs affects all pages. |
| 1054 | */ |
| 1055 | if (cp->type == CLEAR_REFS_ANON && vma->vm_file) |
| 1056 | return 1; |
| 1057 | if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file) |
| 1058 | return 1; |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1062 | static ssize_t clear_refs_write(struct file *file, const char __user *buf, |
| 1063 | size_t count, loff_t *ppos) |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 1064 | { |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1065 | struct task_struct *task; |
Vincent Li | fb92a4b | 2009-09-22 16:45:36 -0700 | [diff] [blame] | 1066 | char buffer[PROC_NUMBUF]; |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1067 | struct mm_struct *mm; |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 1068 | struct vm_area_struct *vma; |
Pavel Emelyanov | 040fa02 | 2013-07-03 15:01:16 -0700 | [diff] [blame] | 1069 | enum clear_refs_types type; |
| 1070 | int itype; |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 1071 | int rv; |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 1072 | |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1073 | memset(buffer, 0, sizeof(buffer)); |
| 1074 | if (count > sizeof(buffer) - 1) |
| 1075 | count = sizeof(buffer) - 1; |
| 1076 | if (copy_from_user(buffer, buf, count)) |
| 1077 | return -EFAULT; |
Pavel Emelyanov | 040fa02 | 2013-07-03 15:01:16 -0700 | [diff] [blame] | 1078 | rv = kstrtoint(strstrip(buffer), 10, &itype); |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 1079 | if (rv < 0) |
| 1080 | return rv; |
Pavel Emelyanov | 040fa02 | 2013-07-03 15:01:16 -0700 | [diff] [blame] | 1081 | type = (enum clear_refs_types)itype; |
| 1082 | if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST) |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1083 | return -EINVAL; |
Pavel Emelyanov | 541c237 | 2013-07-03 15:01:22 -0700 | [diff] [blame] | 1084 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1085 | task = get_proc_task(file_inode(file)); |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1086 | if (!task) |
| 1087 | return -ESRCH; |
| 1088 | mm = get_task_mm(task); |
| 1089 | if (mm) { |
Pavel Emelyanov | af9de7e | 2013-07-03 15:01:18 -0700 | [diff] [blame] | 1090 | struct clear_refs_private cp = { |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 1091 | .type = type, |
Pavel Emelyanov | af9de7e | 2013-07-03 15:01:18 -0700 | [diff] [blame] | 1092 | }; |
Andrew Morton | 20cbc97 | 2008-07-05 12:29:05 -0700 | [diff] [blame] | 1093 | struct mm_walk clear_refs_walk = { |
| 1094 | .pmd_entry = clear_refs_pte_range, |
Naoya Horiguchi | 5c64f52 | 2015-02-11 15:27:46 -0800 | [diff] [blame] | 1095 | .test_walk = clear_refs_test_walk, |
Andrew Morton | 20cbc97 | 2008-07-05 12:29:05 -0700 | [diff] [blame] | 1096 | .mm = mm, |
Pavel Emelyanov | af9de7e | 2013-07-03 15:01:18 -0700 | [diff] [blame] | 1097 | .private = &cp, |
Andrew Morton | 20cbc97 | 2008-07-05 12:29:05 -0700 | [diff] [blame] | 1098 | }; |
Petr Cermak | 695f055 | 2015-02-12 15:01:00 -0800 | [diff] [blame] | 1099 | |
| 1100 | if (type == CLEAR_REFS_MM_HIWATER_RSS) { |
Michal Hocko | 4e80153 | 2016-05-23 16:25:45 -0700 | [diff] [blame] | 1101 | if (down_write_killable(&mm->mmap_sem)) { |
| 1102 | count = -EINTR; |
| 1103 | goto out_mm; |
| 1104 | } |
| 1105 | |
Petr Cermak | 695f055 | 2015-02-12 15:01:00 -0800 | [diff] [blame] | 1106 | /* |
| 1107 | * Writing 5 to /proc/pid/clear_refs resets the peak |
| 1108 | * resident set size to this mm's current rss value. |
| 1109 | */ |
Petr Cermak | 695f055 | 2015-02-12 15:01:00 -0800 | [diff] [blame] | 1110 | reset_mm_hiwater_rss(mm); |
| 1111 | up_write(&mm->mmap_sem); |
| 1112 | goto out_mm; |
| 1113 | } |
| 1114 | |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1115 | down_read(&mm->mmap_sem); |
Peter Feiner | 64e4550 | 2014-10-13 15:55:46 -0700 | [diff] [blame] | 1116 | if (type == CLEAR_REFS_SOFT_DIRTY) { |
| 1117 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 1118 | if (!(vma->vm_flags & VM_SOFTDIRTY)) |
| 1119 | continue; |
| 1120 | up_read(&mm->mmap_sem); |
Michal Hocko | 4e80153 | 2016-05-23 16:25:45 -0700 | [diff] [blame] | 1121 | if (down_write_killable(&mm->mmap_sem)) { |
| 1122 | count = -EINTR; |
| 1123 | goto out_mm; |
| 1124 | } |
Peter Feiner | 64e4550 | 2014-10-13 15:55:46 -0700 | [diff] [blame] | 1125 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 1126 | vma->vm_flags &= ~VM_SOFTDIRTY; |
| 1127 | vma_set_page_prot(vma); |
| 1128 | } |
| 1129 | downgrade_write(&mm->mmap_sem); |
| 1130 | break; |
| 1131 | } |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 1132 | mmu_notifier_invalidate_range_start(mm, 0, -1); |
Peter Feiner | 64e4550 | 2014-10-13 15:55:46 -0700 | [diff] [blame] | 1133 | } |
James Morse | 0f30206 | 2016-10-07 17:00:06 -0700 | [diff] [blame] | 1134 | walk_page_range(0, mm->highest_vm_end, &clear_refs_walk); |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 1135 | if (type == CLEAR_REFS_SOFT_DIRTY) |
| 1136 | mmu_notifier_invalidate_range_end(mm, 0, -1); |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1137 | flush_tlb_mm(mm); |
| 1138 | up_read(&mm->mmap_sem); |
Petr Cermak | 695f055 | 2015-02-12 15:01:00 -0800 | [diff] [blame] | 1139 | out_mm: |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1140 | mmput(mm); |
| 1141 | } |
| 1142 | put_task_struct(task); |
Vincent Li | fb92a4b | 2009-09-22 16:45:36 -0700 | [diff] [blame] | 1143 | |
| 1144 | return count; |
David Rientjes | b813e93 | 2007-05-06 14:49:24 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1147 | const struct file_operations proc_clear_refs_operations = { |
| 1148 | .write = clear_refs_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1149 | .llseek = noop_llseek, |
Matt Mackall | f248dcb | 2008-02-04 22:29:03 -0800 | [diff] [blame] | 1150 | }; |
| 1151 | |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 1152 | typedef struct { |
| 1153 | u64 pme; |
| 1154 | } pagemap_entry_t; |
| 1155 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1156 | struct pagemapread { |
yonghua zheng | 8c82962 | 2013-08-13 16:01:03 -0700 | [diff] [blame] | 1157 | int pos, len; /* units: PM_ENTRY_BYTES, not bytes */ |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 1158 | pagemap_entry_t *buffer; |
Konstantin Khlebnikov | 1c90308 | 2015-09-08 15:00:07 -0700 | [diff] [blame] | 1159 | bool show_pfn; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1160 | }; |
| 1161 | |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1162 | #define PAGEMAP_WALK_SIZE (PMD_SIZE) |
| 1163 | #define PAGEMAP_WALK_MASK (PMD_MASK) |
| 1164 | |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1165 | #define PM_ENTRY_BYTES sizeof(pagemap_entry_t) |
| 1166 | #define PM_PFRAME_BITS 55 |
| 1167 | #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0) |
| 1168 | #define PM_SOFT_DIRTY BIT_ULL(55) |
Konstantin Khlebnikov | 77bb499 | 2015-09-08 15:00:10 -0700 | [diff] [blame] | 1169 | #define PM_MMAP_EXCLUSIVE BIT_ULL(56) |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1170 | #define PM_FILE BIT_ULL(61) |
| 1171 | #define PM_SWAP BIT_ULL(62) |
| 1172 | #define PM_PRESENT BIT_ULL(63) |
Hans Rosenfeld | f16278c | 2008-03-21 18:46:59 -0500 | [diff] [blame] | 1173 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1174 | #define PM_END_OF_BUFFER 1 |
| 1175 | |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1176 | static inline pagemap_entry_t make_pme(u64 frame, u64 flags) |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 1177 | { |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1178 | return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags }; |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme, |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1182 | struct pagemapread *pm) |
| 1183 | { |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 1184 | pm->buffer[pm->pos++] = *pme; |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1185 | if (pm->pos >= pm->len) |
Thomas Tuttle | aae8679 | 2008-06-05 22:46:31 -0700 | [diff] [blame] | 1186 | return PM_END_OF_BUFFER; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | static int pagemap_pte_hole(unsigned long start, unsigned long end, |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 1191 | struct mm_walk *walk) |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1192 | { |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 1193 | struct pagemapread *pm = walk->private; |
Peter Feiner | 68b5a65 | 2014-08-06 16:08:09 -0700 | [diff] [blame] | 1194 | unsigned long addr = start; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1195 | int err = 0; |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 1196 | |
Peter Feiner | 68b5a65 | 2014-08-06 16:08:09 -0700 | [diff] [blame] | 1197 | while (addr < end) { |
| 1198 | struct vm_area_struct *vma = find_vma(walk->mm, addr); |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1199 | pagemap_entry_t pme = make_pme(0, 0); |
Peter Feiner | 87e6d49 | 2014-09-25 16:05:18 -0700 | [diff] [blame] | 1200 | /* End of address space hole, which we mark as non-present. */ |
| 1201 | unsigned long hole_end; |
Peter Feiner | 68b5a65 | 2014-08-06 16:08:09 -0700 | [diff] [blame] | 1202 | |
Peter Feiner | 87e6d49 | 2014-09-25 16:05:18 -0700 | [diff] [blame] | 1203 | if (vma) |
| 1204 | hole_end = min(end, vma->vm_start); |
| 1205 | else |
| 1206 | hole_end = end; |
| 1207 | |
| 1208 | for (; addr < hole_end; addr += PAGE_SIZE) { |
| 1209 | err = add_to_pagemap(addr, &pme, pm); |
| 1210 | if (err) |
| 1211 | goto out; |
Peter Feiner | 68b5a65 | 2014-08-06 16:08:09 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Peter Feiner | 87e6d49 | 2014-09-25 16:05:18 -0700 | [diff] [blame] | 1214 | if (!vma) |
| 1215 | break; |
| 1216 | |
| 1217 | /* Addresses in the VMA. */ |
| 1218 | if (vma->vm_flags & VM_SOFTDIRTY) |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1219 | pme = make_pme(0, PM_SOFT_DIRTY); |
Peter Feiner | 87e6d49 | 2014-09-25 16:05:18 -0700 | [diff] [blame] | 1220 | for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) { |
Peter Feiner | 68b5a65 | 2014-08-06 16:08:09 -0700 | [diff] [blame] | 1221 | err = add_to_pagemap(addr, &pme, pm); |
| 1222 | if (err) |
| 1223 | goto out; |
| 1224 | } |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1225 | } |
Peter Feiner | 68b5a65 | 2014-08-06 16:08:09 -0700 | [diff] [blame] | 1226 | out: |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1227 | return err; |
| 1228 | } |
| 1229 | |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1230 | static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm, |
Konstantin Khlebnikov | 052fb0d | 2012-05-31 16:26:19 -0700 | [diff] [blame] | 1231 | struct vm_area_struct *vma, unsigned long addr, pte_t pte) |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1232 | { |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1233 | u64 frame = 0, flags = 0; |
Konstantin Khlebnikov | 052fb0d | 2012-05-31 16:26:19 -0700 | [diff] [blame] | 1234 | struct page *page = NULL; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1235 | |
Konstantin Khlebnikov | 052fb0d | 2012-05-31 16:26:19 -0700 | [diff] [blame] | 1236 | if (pte_present(pte)) { |
Konstantin Khlebnikov | 1c90308 | 2015-09-08 15:00:07 -0700 | [diff] [blame] | 1237 | if (pm->show_pfn) |
| 1238 | frame = pte_pfn(pte); |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1239 | flags |= PM_PRESENT; |
Konstantin Khlebnikov | 052fb0d | 2012-05-31 16:26:19 -0700 | [diff] [blame] | 1240 | page = vm_normal_page(vma, addr, pte); |
Cyrill Gorcunov | e9cdd6e | 2013-10-16 13:46:53 -0700 | [diff] [blame] | 1241 | if (pte_soft_dirty(pte)) |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1242 | flags |= PM_SOFT_DIRTY; |
Konstantin Khlebnikov | 052fb0d | 2012-05-31 16:26:19 -0700 | [diff] [blame] | 1243 | } else if (is_swap_pte(pte)) { |
Cyrill Gorcunov | 179ef71 | 2013-08-13 16:00:49 -0700 | [diff] [blame] | 1244 | swp_entry_t entry; |
| 1245 | if (pte_swp_soft_dirty(pte)) |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1246 | flags |= PM_SOFT_DIRTY; |
Cyrill Gorcunov | 179ef71 | 2013-08-13 16:00:49 -0700 | [diff] [blame] | 1247 | entry = pte_to_swp_entry(pte); |
Konstantin Khlebnikov | 052fb0d | 2012-05-31 16:26:19 -0700 | [diff] [blame] | 1248 | frame = swp_type(entry) | |
| 1249 | (swp_offset(entry) << MAX_SWAPFILES_SHIFT); |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1250 | flags |= PM_SWAP; |
Konstantin Khlebnikov | 052fb0d | 2012-05-31 16:26:19 -0700 | [diff] [blame] | 1251 | if (is_migration_entry(entry)) |
| 1252 | page = migration_entry_to_page(entry); |
Konstantin Khlebnikov | 052fb0d | 2012-05-31 16:26:19 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | if (page && !PageAnon(page)) |
| 1256 | flags |= PM_FILE; |
Konstantin Khlebnikov | 77bb499 | 2015-09-08 15:00:10 -0700 | [diff] [blame] | 1257 | if (page && page_mapcount(page) == 1) |
| 1258 | flags |= PM_MMAP_EXCLUSIVE; |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1259 | if (vma->vm_flags & VM_SOFTDIRTY) |
| 1260 | flags |= PM_SOFT_DIRTY; |
Konstantin Khlebnikov | 052fb0d | 2012-05-31 16:26:19 -0700 | [diff] [blame] | 1261 | |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1262 | return make_pme(frame, flags); |
Dave Hansen | bcf8039 | 2008-06-12 15:21:48 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1265 | static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end, |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 1266 | struct mm_walk *walk) |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1267 | { |
Naoya Horiguchi | f995ece | 2015-02-11 15:27:48 -0800 | [diff] [blame] | 1268 | struct vm_area_struct *vma = walk->vma; |
Dave Hansen | 2165009 | 2008-06-12 15:21:47 -0700 | [diff] [blame] | 1269 | struct pagemapread *pm = walk->private; |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1270 | spinlock_t *ptl; |
Konstantin Khlebnikov | 05fbf35 | 2015-02-11 15:27:31 -0800 | [diff] [blame] | 1271 | pte_t *pte, *orig_pte; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1272 | int err = 0; |
| 1273 | |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1274 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Kirill A. Shutemov | b6ec57f | 2016-01-21 16:40:25 -0800 | [diff] [blame] | 1275 | ptl = pmd_trans_huge_lock(pmdp, vma); |
| 1276 | if (ptl) { |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1277 | u64 flags = 0, frame = 0; |
| 1278 | pmd_t pmd = *pmdp; |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 1279 | |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1280 | if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(pmd)) |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1281 | flags |= PM_SOFT_DIRTY; |
Cyrill Gorcunov | d9104d1 | 2013-09-11 14:22:24 -0700 | [diff] [blame] | 1282 | |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1283 | /* |
| 1284 | * Currently pmd for thp is always present because thp |
| 1285 | * can not be swapped-out, migrated, or HWPOISONed |
| 1286 | * (split in such cases instead.) |
| 1287 | * This if-check is just to prepare for future implementation. |
| 1288 | */ |
| 1289 | if (pmd_present(pmd)) { |
Konstantin Khlebnikov | 77bb499 | 2015-09-08 15:00:10 -0700 | [diff] [blame] | 1290 | struct page *page = pmd_page(pmd); |
| 1291 | |
| 1292 | if (page_mapcount(page) == 1) |
| 1293 | flags |= PM_MMAP_EXCLUSIVE; |
| 1294 | |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1295 | flags |= PM_PRESENT; |
Konstantin Khlebnikov | 1c90308 | 2015-09-08 15:00:07 -0700 | [diff] [blame] | 1296 | if (pm->show_pfn) |
| 1297 | frame = pmd_pfn(pmd) + |
| 1298 | ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1299 | } |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1300 | |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1301 | for (; addr != end; addr += PAGE_SIZE) { |
| 1302 | pagemap_entry_t pme = make_pme(frame, flags); |
| 1303 | |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 1304 | err = add_to_pagemap(addr, &pme, pm); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1305 | if (err) |
| 1306 | break; |
Konstantin Khlebnikov | 1c90308 | 2015-09-08 15:00:07 -0700 | [diff] [blame] | 1307 | if (pm->show_pfn && (flags & PM_PRESENT)) |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1308 | frame++; |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1309 | } |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1310 | spin_unlock(ptl); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1311 | return err; |
Naoya Horiguchi | 5aaabe8 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1314 | if (pmd_trans_unstable(pmdp)) |
Andrea Arcangeli | 45f83ce | 2012-03-28 14:42:40 -0700 | [diff] [blame] | 1315 | return 0; |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1316 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
Dave Hansen | bcf8039 | 2008-06-12 15:21:48 -0700 | [diff] [blame] | 1317 | |
Naoya Horiguchi | f995ece | 2015-02-11 15:27:48 -0800 | [diff] [blame] | 1318 | /* |
| 1319 | * We can assume that @vma always points to a valid one and @end never |
| 1320 | * goes beyond vma->vm_end. |
| 1321 | */ |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1322 | orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl); |
Naoya Horiguchi | f995ece | 2015-02-11 15:27:48 -0800 | [diff] [blame] | 1323 | for (; addr < end; pte++, addr += PAGE_SIZE) { |
| 1324 | pagemap_entry_t pme; |
Peter Feiner | 81d0fa6 | 2014-10-09 15:28:32 -0700 | [diff] [blame] | 1325 | |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1326 | pme = pte_to_pagemap_entry(pm, vma, addr, *pte); |
Naoya Horiguchi | f995ece | 2015-02-11 15:27:48 -0800 | [diff] [blame] | 1327 | err = add_to_pagemap(addr, &pme, pm); |
Konstantin Khlebnikov | 05fbf35 | 2015-02-11 15:27:31 -0800 | [diff] [blame] | 1328 | if (err) |
Peter Feiner | 81d0fa6 | 2014-10-09 15:28:32 -0700 | [diff] [blame] | 1329 | break; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1330 | } |
Naoya Horiguchi | f995ece | 2015-02-11 15:27:48 -0800 | [diff] [blame] | 1331 | pte_unmap_unlock(orig_pte, ptl); |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1332 | |
| 1333 | cond_resched(); |
| 1334 | |
| 1335 | return err; |
| 1336 | } |
| 1337 | |
Naoya Horiguchi | 1a5cb81 | 2010-05-24 14:32:12 -0700 | [diff] [blame] | 1338 | #ifdef CONFIG_HUGETLB_PAGE |
Naoya Horiguchi | 116354d | 2010-04-06 14:35:04 -0700 | [diff] [blame] | 1339 | /* This function walks within one hugetlb entry in the single call */ |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1340 | static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask, |
Naoya Horiguchi | 116354d | 2010-04-06 14:35:04 -0700 | [diff] [blame] | 1341 | unsigned long addr, unsigned long end, |
| 1342 | struct mm_walk *walk) |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 1343 | { |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 1344 | struct pagemapread *pm = walk->private; |
Naoya Horiguchi | f995ece | 2015-02-11 15:27:48 -0800 | [diff] [blame] | 1345 | struct vm_area_struct *vma = walk->vma; |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1346 | u64 flags = 0, frame = 0; |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 1347 | int err = 0; |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1348 | pte_t pte; |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 1349 | |
Naoya Horiguchi | f995ece | 2015-02-11 15:27:48 -0800 | [diff] [blame] | 1350 | if (vma->vm_flags & VM_SOFTDIRTY) |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1351 | flags |= PM_SOFT_DIRTY; |
Cyrill Gorcunov | d9104d1 | 2013-09-11 14:22:24 -0700 | [diff] [blame] | 1352 | |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1353 | pte = huge_ptep_get(ptep); |
| 1354 | if (pte_present(pte)) { |
| 1355 | struct page *page = pte_page(pte); |
| 1356 | |
| 1357 | if (!PageAnon(page)) |
| 1358 | flags |= PM_FILE; |
| 1359 | |
Konstantin Khlebnikov | 77bb499 | 2015-09-08 15:00:10 -0700 | [diff] [blame] | 1360 | if (page_mapcount(page) == 1) |
| 1361 | flags |= PM_MMAP_EXCLUSIVE; |
| 1362 | |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1363 | flags |= PM_PRESENT; |
Konstantin Khlebnikov | 1c90308 | 2015-09-08 15:00:07 -0700 | [diff] [blame] | 1364 | if (pm->show_pfn) |
| 1365 | frame = pte_pfn(pte) + |
| 1366 | ((addr & ~hmask) >> PAGE_SHIFT); |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 1369 | for (; addr != end; addr += PAGE_SIZE) { |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1370 | pagemap_entry_t pme = make_pme(frame, flags); |
| 1371 | |
Naoya Horiguchi | 092b50b | 2012-03-21 16:33:59 -0700 | [diff] [blame] | 1372 | err = add_to_pagemap(addr, &pme, pm); |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 1373 | if (err) |
| 1374 | return err; |
Konstantin Khlebnikov | 1c90308 | 2015-09-08 15:00:07 -0700 | [diff] [blame] | 1375 | if (pm->show_pfn && (flags & PM_PRESENT)) |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1376 | frame++; |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | cond_resched(); |
| 1380 | |
| 1381 | return err; |
| 1382 | } |
Naoya Horiguchi | 1a5cb81 | 2010-05-24 14:32:12 -0700 | [diff] [blame] | 1383 | #endif /* HUGETLB_PAGE */ |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 1384 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1385 | /* |
| 1386 | * /proc/pid/pagemap - an array mapping virtual pages to pfns |
| 1387 | * |
Hans Rosenfeld | f16278c | 2008-03-21 18:46:59 -0500 | [diff] [blame] | 1388 | * For each page in the address space, this file contains one 64-bit entry |
| 1389 | * consisting of the following: |
| 1390 | * |
Konstantin Khlebnikov | 052fb0d | 2012-05-31 16:26:19 -0700 | [diff] [blame] | 1391 | * Bits 0-54 page frame number (PFN) if present |
Hans Rosenfeld | f16278c | 2008-03-21 18:46:59 -0500 | [diff] [blame] | 1392 | * Bits 0-4 swap type if swapped |
Konstantin Khlebnikov | 052fb0d | 2012-05-31 16:26:19 -0700 | [diff] [blame] | 1393 | * Bits 5-54 swap offset if swapped |
Konstantin Khlebnikov | deb9454 | 2015-09-08 15:00:02 -0700 | [diff] [blame] | 1394 | * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt) |
Konstantin Khlebnikov | 77bb499 | 2015-09-08 15:00:10 -0700 | [diff] [blame] | 1395 | * Bit 56 page exclusively mapped |
| 1396 | * Bits 57-60 zero |
Konstantin Khlebnikov | 052fb0d | 2012-05-31 16:26:19 -0700 | [diff] [blame] | 1397 | * Bit 61 page is file-page or shared-anon |
Hans Rosenfeld | f16278c | 2008-03-21 18:46:59 -0500 | [diff] [blame] | 1398 | * Bit 62 page swapped |
| 1399 | * Bit 63 page present |
| 1400 | * |
| 1401 | * If the page is not present but in swap, then the PFN contains an |
| 1402 | * encoding of the swap file number and the page's offset into the |
| 1403 | * swap. Unmapped pages return a null PFN. This allows determining |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1404 | * precisely which pages are mapped (or in swap) and comparing mapped |
| 1405 | * pages between processes. |
| 1406 | * |
| 1407 | * Efficient users of this interface will use /proc/pid/maps to |
| 1408 | * determine which areas of memory are actually mapped and llseek to |
| 1409 | * skip over unmapped regions. |
| 1410 | */ |
| 1411 | static ssize_t pagemap_read(struct file *file, char __user *buf, |
| 1412 | size_t count, loff_t *ppos) |
| 1413 | { |
Konstantin Khlebnikov | a06db75 | 2015-09-08 14:59:59 -0700 | [diff] [blame] | 1414 | struct mm_struct *mm = file->private_data; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1415 | struct pagemapread pm; |
Alexey Dobriyan | ee1e6ab | 2008-07-21 14:21:36 -0700 | [diff] [blame] | 1416 | struct mm_walk pagemap_walk = {}; |
Andrew Morton | 5d7e0d2 | 2008-07-05 01:02:01 -0700 | [diff] [blame] | 1417 | unsigned long src; |
| 1418 | unsigned long svpfn; |
| 1419 | unsigned long start_vaddr; |
| 1420 | unsigned long end_vaddr; |
Konstantin Khlebnikov | a06db75 | 2015-09-08 14:59:59 -0700 | [diff] [blame] | 1421 | int ret = 0, copied = 0; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1422 | |
Konstantin Khlebnikov | a06db75 | 2015-09-08 14:59:59 -0700 | [diff] [blame] | 1423 | if (!mm || !atomic_inc_not_zero(&mm->mm_users)) |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1424 | goto out; |
| 1425 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1426 | ret = -EINVAL; |
| 1427 | /* file position must be aligned */ |
Thomas Tuttle | aae8679 | 2008-06-05 22:46:31 -0700 | [diff] [blame] | 1428 | if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES)) |
Konstantin Khlebnikov | a06db75 | 2015-09-08 14:59:59 -0700 | [diff] [blame] | 1429 | goto out_mm; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1430 | |
| 1431 | ret = 0; |
Vitaly Mayatskikh | 0816178 | 2009-04-30 15:08:18 -0700 | [diff] [blame] | 1432 | if (!count) |
Konstantin Khlebnikov | a06db75 | 2015-09-08 14:59:59 -0700 | [diff] [blame] | 1433 | goto out_mm; |
Vitaly Mayatskikh | 0816178 | 2009-04-30 15:08:18 -0700 | [diff] [blame] | 1434 | |
Konstantin Khlebnikov | 1c90308 | 2015-09-08 15:00:07 -0700 | [diff] [blame] | 1435 | /* do not disclose physical addresses: attack vector */ |
| 1436 | pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN); |
| 1437 | |
yonghua zheng | 8c82962 | 2013-08-13 16:01:03 -0700 | [diff] [blame] | 1438 | pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT); |
| 1439 | pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY); |
Andrew Morton | 5d7e0d2 | 2008-07-05 01:02:01 -0700 | [diff] [blame] | 1440 | ret = -ENOMEM; |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1441 | if (!pm.buffer) |
Konstantin Khlebnikov | a06db75 | 2015-09-08 14:59:59 -0700 | [diff] [blame] | 1442 | goto out_mm; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1443 | |
Konstantin Khlebnikov | 356515e | 2015-09-08 15:00:04 -0700 | [diff] [blame] | 1444 | pagemap_walk.pmd_entry = pagemap_pmd_range; |
Andrew Morton | 5d7e0d2 | 2008-07-05 01:02:01 -0700 | [diff] [blame] | 1445 | pagemap_walk.pte_hole = pagemap_pte_hole; |
Naoya Horiguchi | 1a5cb81 | 2010-05-24 14:32:12 -0700 | [diff] [blame] | 1446 | #ifdef CONFIG_HUGETLB_PAGE |
Naoya Horiguchi | 5dc3764 | 2009-12-14 18:00:01 -0800 | [diff] [blame] | 1447 | pagemap_walk.hugetlb_entry = pagemap_hugetlb_range; |
Naoya Horiguchi | 1a5cb81 | 2010-05-24 14:32:12 -0700 | [diff] [blame] | 1448 | #endif |
Andrew Morton | 5d7e0d2 | 2008-07-05 01:02:01 -0700 | [diff] [blame] | 1449 | pagemap_walk.mm = mm; |
| 1450 | pagemap_walk.private = ± |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1451 | |
Andrew Morton | 5d7e0d2 | 2008-07-05 01:02:01 -0700 | [diff] [blame] | 1452 | src = *ppos; |
| 1453 | svpfn = src / PM_ENTRY_BYTES; |
| 1454 | start_vaddr = svpfn << PAGE_SHIFT; |
Konstantin Khlebnikov | a06db75 | 2015-09-08 14:59:59 -0700 | [diff] [blame] | 1455 | end_vaddr = mm->task_size; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1456 | |
Andrew Morton | 5d7e0d2 | 2008-07-05 01:02:01 -0700 | [diff] [blame] | 1457 | /* watch out for wraparound */ |
Konstantin Khlebnikov | a06db75 | 2015-09-08 14:59:59 -0700 | [diff] [blame] | 1458 | if (svpfn > mm->task_size >> PAGE_SHIFT) |
Andrew Morton | 5d7e0d2 | 2008-07-05 01:02:01 -0700 | [diff] [blame] | 1459 | start_vaddr = end_vaddr; |
| 1460 | |
| 1461 | /* |
| 1462 | * The odds are that this will stop walking way |
| 1463 | * before end_vaddr, because the length of the |
| 1464 | * user buffer is tracked in "pm", and the walk |
| 1465 | * will stop when we hit the end of the buffer. |
| 1466 | */ |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1467 | ret = 0; |
| 1468 | while (count && (start_vaddr < end_vaddr)) { |
| 1469 | int len; |
| 1470 | unsigned long end; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1471 | |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1472 | pm.pos = 0; |
Naoya Horiguchi | ea251c1 | 2010-11-24 12:57:13 -0800 | [diff] [blame] | 1473 | end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK; |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1474 | /* overflow ? */ |
| 1475 | if (end < start_vaddr || end > end_vaddr) |
| 1476 | end = end_vaddr; |
| 1477 | down_read(&mm->mmap_sem); |
| 1478 | ret = walk_page_range(start_vaddr, end, &pagemap_walk); |
| 1479 | up_read(&mm->mmap_sem); |
| 1480 | start_vaddr = end; |
| 1481 | |
| 1482 | len = min(count, PM_ENTRY_BYTES * pm.pos); |
Dan Carpenter | 309361e0 | 2010-04-06 13:45:39 +0300 | [diff] [blame] | 1483 | if (copy_to_user(buf, pm.buffer, len)) { |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1484 | ret = -EFAULT; |
Konstantin Khlebnikov | a06db75 | 2015-09-08 14:59:59 -0700 | [diff] [blame] | 1485 | goto out_free; |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1486 | } |
| 1487 | copied += len; |
| 1488 | buf += len; |
| 1489 | count -= len; |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1490 | } |
KAMEZAWA Hiroyuki | d82ef02 | 2010-04-02 09:11:29 +0900 | [diff] [blame] | 1491 | *ppos += copied; |
| 1492 | if (!ret || ret == PM_END_OF_BUFFER) |
| 1493 | ret = copied; |
| 1494 | |
KOSAKI Motohiro | 98bc93e | 2011-05-26 16:25:53 -0700 | [diff] [blame] | 1495 | out_free: |
| 1496 | kfree(pm.buffer); |
Konstantin Khlebnikov | a06db75 | 2015-09-08 14:59:59 -0700 | [diff] [blame] | 1497 | out_mm: |
| 1498 | mmput(mm); |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1499 | out: |
| 1500 | return ret; |
| 1501 | } |
| 1502 | |
Pavel Emelyanov | 541c237 | 2013-07-03 15:01:22 -0700 | [diff] [blame] | 1503 | static int pagemap_open(struct inode *inode, struct file *file) |
| 1504 | { |
Konstantin Khlebnikov | a06db75 | 2015-09-08 14:59:59 -0700 | [diff] [blame] | 1505 | struct mm_struct *mm; |
| 1506 | |
Konstantin Khlebnikov | a06db75 | 2015-09-08 14:59:59 -0700 | [diff] [blame] | 1507 | mm = proc_mem_open(inode, PTRACE_MODE_READ); |
| 1508 | if (IS_ERR(mm)) |
| 1509 | return PTR_ERR(mm); |
| 1510 | file->private_data = mm; |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
| 1514 | static int pagemap_release(struct inode *inode, struct file *file) |
| 1515 | { |
| 1516 | struct mm_struct *mm = file->private_data; |
| 1517 | |
| 1518 | if (mm) |
| 1519 | mmdrop(mm); |
Pavel Emelyanov | 541c237 | 2013-07-03 15:01:22 -0700 | [diff] [blame] | 1520 | return 0; |
| 1521 | } |
| 1522 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1523 | const struct file_operations proc_pagemap_operations = { |
| 1524 | .llseek = mem_lseek, /* borrow this */ |
| 1525 | .read = pagemap_read, |
Pavel Emelyanov | 541c237 | 2013-07-03 15:01:22 -0700 | [diff] [blame] | 1526 | .open = pagemap_open, |
Konstantin Khlebnikov | a06db75 | 2015-09-08 14:59:59 -0700 | [diff] [blame] | 1527 | .release = pagemap_release, |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1528 | }; |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 1529 | #endif /* CONFIG_PROC_PAGE_MONITOR */ |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 1530 | |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1531 | #ifdef CONFIG_PROCESS_RECLAIM |
| 1532 | static int reclaim_pte_range(pmd_t *pmd, unsigned long addr, |
| 1533 | unsigned long end, struct mm_walk *walk) |
| 1534 | { |
Vinayak Menon | 1bddbcf | 2015-03-20 14:15:39 +0530 | [diff] [blame] | 1535 | struct reclaim_param *rp = walk->private; |
| 1536 | struct vm_area_struct *vma = rp->vma; |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1537 | pte_t *pte, ptent; |
| 1538 | spinlock_t *ptl; |
| 1539 | struct page *page; |
| 1540 | LIST_HEAD(page_list); |
| 1541 | int isolated; |
Vinayak Menon | 1bddbcf | 2015-03-20 14:15:39 +0530 | [diff] [blame] | 1542 | int reclaimed; |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1543 | |
| 1544 | split_huge_pmd(vma, addr, pmd); |
Vinayak Menon | 1bddbcf | 2015-03-20 14:15:39 +0530 | [diff] [blame] | 1545 | if (pmd_trans_unstable(pmd) || !rp->nr_to_reclaim) |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1546 | return 0; |
| 1547 | cont: |
| 1548 | isolated = 0; |
| 1549 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 1550 | for (; addr != end; pte++, addr += PAGE_SIZE) { |
| 1551 | ptent = *pte; |
| 1552 | if (!pte_present(ptent)) |
| 1553 | continue; |
| 1554 | |
| 1555 | page = vm_normal_page(vma, addr, ptent); |
| 1556 | if (!page) |
| 1557 | continue; |
| 1558 | |
| 1559 | if (isolate_lru_page(page)) |
| 1560 | continue; |
| 1561 | |
| 1562 | list_add(&page->lru, &page_list); |
| 1563 | inc_node_page_state(page, NR_ISOLATED_ANON + |
| 1564 | page_is_file_cache(page)); |
| 1565 | isolated++; |
Vinayak Menon | 1bddbcf | 2015-03-20 14:15:39 +0530 | [diff] [blame] | 1566 | rp->nr_scanned++; |
| 1567 | if ((isolated >= SWAP_CLUSTER_MAX) || !rp->nr_to_reclaim) |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1568 | break; |
| 1569 | } |
| 1570 | pte_unmap_unlock(pte - 1, ptl); |
Vinayak Menon | 1bddbcf | 2015-03-20 14:15:39 +0530 | [diff] [blame] | 1571 | reclaimed = reclaim_pages_from_list(&page_list, vma); |
| 1572 | rp->nr_reclaimed += reclaimed; |
| 1573 | rp->nr_to_reclaim -= reclaimed; |
| 1574 | if (rp->nr_to_reclaim < 0) |
| 1575 | rp->nr_to_reclaim = 0; |
| 1576 | |
| 1577 | if (rp->nr_to_reclaim && (addr != end)) |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1578 | goto cont; |
| 1579 | |
| 1580 | cond_resched(); |
| 1581 | return 0; |
| 1582 | } |
| 1583 | |
| 1584 | enum reclaim_type { |
| 1585 | RECLAIM_FILE, |
| 1586 | RECLAIM_ANON, |
| 1587 | RECLAIM_ALL, |
| 1588 | RECLAIM_RANGE, |
| 1589 | }; |
| 1590 | |
Vinayak Menon | 1bddbcf | 2015-03-20 14:15:39 +0530 | [diff] [blame] | 1591 | struct reclaim_param reclaim_task_anon(struct task_struct *task, |
| 1592 | int nr_to_reclaim) |
| 1593 | { |
| 1594 | struct mm_struct *mm; |
| 1595 | struct vm_area_struct *vma; |
| 1596 | struct mm_walk reclaim_walk = {}; |
| 1597 | struct reclaim_param rp; |
| 1598 | |
| 1599 | rp.nr_reclaimed = 0; |
| 1600 | rp.nr_scanned = 0; |
| 1601 | get_task_struct(task); |
| 1602 | mm = get_task_mm(task); |
| 1603 | if (!mm) |
| 1604 | goto out; |
| 1605 | |
| 1606 | reclaim_walk.mm = mm; |
| 1607 | reclaim_walk.pmd_entry = reclaim_pte_range; |
| 1608 | |
| 1609 | rp.nr_to_reclaim = nr_to_reclaim; |
| 1610 | reclaim_walk.private = &rp; |
| 1611 | |
| 1612 | down_read(&mm->mmap_sem); |
| 1613 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 1614 | if (is_vm_hugetlb_page(vma)) |
| 1615 | continue; |
| 1616 | |
| 1617 | if (vma->vm_file) |
| 1618 | continue; |
| 1619 | |
Vinayak Menon | e65af6b | 2017-05-26 16:29:55 +0530 | [diff] [blame] | 1620 | if (vma->vm_flags & VM_LOCKED) |
| 1621 | continue; |
| 1622 | |
Vinayak Menon | 1bddbcf | 2015-03-20 14:15:39 +0530 | [diff] [blame] | 1623 | if (!rp.nr_to_reclaim) |
| 1624 | break; |
| 1625 | |
| 1626 | rp.vma = vma; |
| 1627 | walk_page_range(vma->vm_start, vma->vm_end, |
| 1628 | &reclaim_walk); |
| 1629 | } |
| 1630 | |
| 1631 | flush_tlb_mm(mm); |
| 1632 | up_read(&mm->mmap_sem); |
| 1633 | mmput(mm); |
| 1634 | out: |
| 1635 | put_task_struct(task); |
| 1636 | return rp; |
| 1637 | } |
| 1638 | |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1639 | static ssize_t reclaim_write(struct file *file, const char __user *buf, |
| 1640 | size_t count, loff_t *ppos) |
| 1641 | { |
| 1642 | struct task_struct *task; |
Minchan Kim | 01fdd71 | 2013-05-09 16:21:28 +0900 | [diff] [blame] | 1643 | char buffer[200]; |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1644 | struct mm_struct *mm; |
| 1645 | struct vm_area_struct *vma; |
| 1646 | enum reclaim_type type; |
| 1647 | char *type_buf; |
Minchan Kim | 01fdd71 | 2013-05-09 16:21:28 +0900 | [diff] [blame] | 1648 | struct mm_walk reclaim_walk = {}; |
| 1649 | unsigned long start = 0; |
| 1650 | unsigned long end = 0; |
Vinayak Menon | 1bddbcf | 2015-03-20 14:15:39 +0530 | [diff] [blame] | 1651 | struct reclaim_param rp; |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1652 | |
| 1653 | memset(buffer, 0, sizeof(buffer)); |
| 1654 | if (count > sizeof(buffer) - 1) |
| 1655 | count = sizeof(buffer) - 1; |
| 1656 | |
| 1657 | if (copy_from_user(buffer, buf, count)) |
| 1658 | return -EFAULT; |
| 1659 | |
| 1660 | type_buf = strstrip(buffer); |
| 1661 | if (!strcmp(type_buf, "file")) |
| 1662 | type = RECLAIM_FILE; |
| 1663 | else if (!strcmp(type_buf, "anon")) |
| 1664 | type = RECLAIM_ANON; |
| 1665 | else if (!strcmp(type_buf, "all")) |
| 1666 | type = RECLAIM_ALL; |
Minchan Kim | 01fdd71 | 2013-05-09 16:21:28 +0900 | [diff] [blame] | 1667 | else if (isdigit(*type_buf)) |
| 1668 | type = RECLAIM_RANGE; |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1669 | else |
Minchan Kim | 01fdd71 | 2013-05-09 16:21:28 +0900 | [diff] [blame] | 1670 | goto out_err; |
| 1671 | |
| 1672 | if (type == RECLAIM_RANGE) { |
| 1673 | char *token; |
| 1674 | unsigned long long len, len_in, tmp; |
| 1675 | token = strsep(&type_buf, " "); |
| 1676 | if (!token) |
| 1677 | goto out_err; |
| 1678 | tmp = memparse(token, &token); |
| 1679 | if (tmp & ~PAGE_MASK || tmp > ULONG_MAX) |
| 1680 | goto out_err; |
| 1681 | start = tmp; |
| 1682 | |
| 1683 | token = strsep(&type_buf, " "); |
| 1684 | if (!token) |
| 1685 | goto out_err; |
| 1686 | len_in = memparse(token, &token); |
| 1687 | len = (len_in + ~PAGE_MASK) & PAGE_MASK; |
| 1688 | if (len > ULONG_MAX) |
| 1689 | goto out_err; |
| 1690 | /* |
| 1691 | * Check to see whether len was rounded up from small -ve |
| 1692 | * to zero. |
| 1693 | */ |
| 1694 | if (len_in && !len) |
| 1695 | goto out_err; |
| 1696 | |
| 1697 | end = start + len; |
| 1698 | if (end < start) |
| 1699 | goto out_err; |
| 1700 | } |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1701 | |
| 1702 | task = get_proc_task(file->f_path.dentry->d_inode); |
| 1703 | if (!task) |
| 1704 | return -ESRCH; |
| 1705 | |
| 1706 | mm = get_task_mm(task); |
Minchan Kim | 01fdd71 | 2013-05-09 16:21:28 +0900 | [diff] [blame] | 1707 | if (!mm) |
| 1708 | goto out; |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1709 | |
Minchan Kim | 01fdd71 | 2013-05-09 16:21:28 +0900 | [diff] [blame] | 1710 | reclaim_walk.mm = mm; |
| 1711 | reclaim_walk.pmd_entry = reclaim_pte_range; |
| 1712 | |
Vinayak Menon | 1bddbcf | 2015-03-20 14:15:39 +0530 | [diff] [blame] | 1713 | rp.nr_to_reclaim = INT_MAX; |
| 1714 | rp.nr_reclaimed = 0; |
| 1715 | reclaim_walk.private = &rp; |
| 1716 | |
Minchan Kim | 01fdd71 | 2013-05-09 16:21:28 +0900 | [diff] [blame] | 1717 | down_read(&mm->mmap_sem); |
| 1718 | if (type == RECLAIM_RANGE) { |
| 1719 | vma = find_vma(mm, start); |
| 1720 | while (vma) { |
| 1721 | if (vma->vm_start > end) |
| 1722 | break; |
| 1723 | if (is_vm_hugetlb_page(vma)) |
| 1724 | continue; |
| 1725 | |
Vinayak Menon | 1bddbcf | 2015-03-20 14:15:39 +0530 | [diff] [blame] | 1726 | rp.vma = vma; |
Minchan Kim | 01fdd71 | 2013-05-09 16:21:28 +0900 | [diff] [blame] | 1727 | walk_page_range(max(vma->vm_start, start), |
| 1728 | min(vma->vm_end, end), |
| 1729 | &reclaim_walk); |
| 1730 | vma = vma->vm_next; |
| 1731 | } |
| 1732 | } else { |
| 1733 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1734 | if (is_vm_hugetlb_page(vma)) |
| 1735 | continue; |
| 1736 | |
| 1737 | if (type == RECLAIM_ANON && vma->vm_file) |
| 1738 | continue; |
Minchan Kim | 01fdd71 | 2013-05-09 16:21:28 +0900 | [diff] [blame] | 1739 | |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1740 | if (type == RECLAIM_FILE && !vma->vm_file) |
| 1741 | continue; |
| 1742 | |
Vinayak Menon | 1bddbcf | 2015-03-20 14:15:39 +0530 | [diff] [blame] | 1743 | rp.vma = vma; |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1744 | walk_page_range(vma->vm_start, vma->vm_end, |
Minchan Kim | 01fdd71 | 2013-05-09 16:21:28 +0900 | [diff] [blame] | 1745 | &reclaim_walk); |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1746 | } |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1747 | } |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1748 | |
Minchan Kim | 01fdd71 | 2013-05-09 16:21:28 +0900 | [diff] [blame] | 1749 | flush_tlb_mm(mm); |
| 1750 | up_read(&mm->mmap_sem); |
| 1751 | mmput(mm); |
| 1752 | out: |
| 1753 | put_task_struct(task); |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1754 | return count; |
Minchan Kim | 01fdd71 | 2013-05-09 16:21:28 +0900 | [diff] [blame] | 1755 | |
| 1756 | out_err: |
| 1757 | return -EINVAL; |
Minchan Kim | ff92689 | 2013-05-09 16:21:24 +0900 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | const struct file_operations proc_reclaim_operations = { |
| 1761 | .write = reclaim_write, |
| 1762 | .llseek = noop_llseek, |
| 1763 | }; |
| 1764 | #endif |
| 1765 | |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 1766 | #ifdef CONFIG_NUMA |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 1767 | |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1768 | struct numa_maps { |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1769 | unsigned long pages; |
| 1770 | unsigned long anon; |
| 1771 | unsigned long active; |
| 1772 | unsigned long writeback; |
| 1773 | unsigned long mapcount_max; |
| 1774 | unsigned long dirty; |
| 1775 | unsigned long swapcache; |
| 1776 | unsigned long node[MAX_NUMNODES]; |
| 1777 | }; |
| 1778 | |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 1779 | struct numa_maps_private { |
| 1780 | struct proc_maps_private proc_maps; |
| 1781 | struct numa_maps md; |
| 1782 | }; |
| 1783 | |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1784 | static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty, |
| 1785 | unsigned long nr_pages) |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1786 | { |
| 1787 | int count = page_mapcount(page); |
| 1788 | |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1789 | md->pages += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1790 | if (pte_dirty || PageDirty(page)) |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1791 | md->dirty += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1792 | |
| 1793 | if (PageSwapCache(page)) |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1794 | md->swapcache += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1795 | |
| 1796 | if (PageActive(page) || PageUnevictable(page)) |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1797 | md->active += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1798 | |
| 1799 | if (PageWriteback(page)) |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1800 | md->writeback += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1801 | |
| 1802 | if (PageAnon(page)) |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1803 | md->anon += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1804 | |
| 1805 | if (count > md->mapcount_max) |
| 1806 | md->mapcount_max = count; |
| 1807 | |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1808 | md->node[page_to_nid(page)] += nr_pages; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1809 | } |
| 1810 | |
Dave Hansen | 3200a8a | 2011-09-20 15:19:39 -0700 | [diff] [blame] | 1811 | static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma, |
| 1812 | unsigned long addr) |
| 1813 | { |
| 1814 | struct page *page; |
| 1815 | int nid; |
| 1816 | |
| 1817 | if (!pte_present(pte)) |
| 1818 | return NULL; |
| 1819 | |
| 1820 | page = vm_normal_page(vma, addr, pte); |
| 1821 | if (!page) |
| 1822 | return NULL; |
| 1823 | |
| 1824 | if (PageReserved(page)) |
| 1825 | return NULL; |
| 1826 | |
| 1827 | nid = page_to_nid(page); |
Lai Jiangshan | 4ff1b2c | 2012-12-12 13:51:25 -0800 | [diff] [blame] | 1828 | if (!node_isset(nid, node_states[N_MEMORY])) |
Dave Hansen | 3200a8a | 2011-09-20 15:19:39 -0700 | [diff] [blame] | 1829 | return NULL; |
| 1830 | |
| 1831 | return page; |
| 1832 | } |
| 1833 | |
Gerald Schaefer | 28093f9 | 2016-04-28 16:18:35 -0700 | [diff] [blame] | 1834 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 1835 | static struct page *can_gather_numa_stats_pmd(pmd_t pmd, |
| 1836 | struct vm_area_struct *vma, |
| 1837 | unsigned long addr) |
| 1838 | { |
| 1839 | struct page *page; |
| 1840 | int nid; |
| 1841 | |
| 1842 | if (!pmd_present(pmd)) |
| 1843 | return NULL; |
| 1844 | |
| 1845 | page = vm_normal_page_pmd(vma, addr, pmd); |
| 1846 | if (!page) |
| 1847 | return NULL; |
| 1848 | |
| 1849 | if (PageReserved(page)) |
| 1850 | return NULL; |
| 1851 | |
| 1852 | nid = page_to_nid(page); |
| 1853 | if (!node_isset(nid, node_states[N_MEMORY])) |
| 1854 | return NULL; |
| 1855 | |
| 1856 | return page; |
| 1857 | } |
| 1858 | #endif |
| 1859 | |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1860 | static int gather_pte_stats(pmd_t *pmd, unsigned long addr, |
| 1861 | unsigned long end, struct mm_walk *walk) |
| 1862 | { |
Naoya Horiguchi | d85f4d6 | 2015-02-11 15:27:54 -0800 | [diff] [blame] | 1863 | struct numa_maps *md = walk->private; |
| 1864 | struct vm_area_struct *vma = walk->vma; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1865 | spinlock_t *ptl; |
| 1866 | pte_t *orig_pte; |
| 1867 | pte_t *pte; |
| 1868 | |
Gerald Schaefer | 28093f9 | 2016-04-28 16:18:35 -0700 | [diff] [blame] | 1869 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Kirill A. Shutemov | b6ec57f | 2016-01-21 16:40:25 -0800 | [diff] [blame] | 1870 | ptl = pmd_trans_huge_lock(pmd, vma); |
| 1871 | if (ptl) { |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1872 | struct page *page; |
| 1873 | |
Gerald Schaefer | 28093f9 | 2016-04-28 16:18:35 -0700 | [diff] [blame] | 1874 | page = can_gather_numa_stats_pmd(*pmd, vma, addr); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1875 | if (page) |
Gerald Schaefer | 28093f9 | 2016-04-28 16:18:35 -0700 | [diff] [blame] | 1876 | gather_stats(page, md, pmd_dirty(*pmd), |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1877 | HPAGE_PMD_SIZE/PAGE_SIZE); |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 1878 | spin_unlock(ptl); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1879 | return 0; |
Dave Hansen | 32ef438 | 2011-09-20 15:19:41 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 1882 | if (pmd_trans_unstable(pmd)) |
| 1883 | return 0; |
Gerald Schaefer | 28093f9 | 2016-04-28 16:18:35 -0700 | [diff] [blame] | 1884 | #endif |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1885 | orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); |
| 1886 | do { |
Naoya Horiguchi | d85f4d6 | 2015-02-11 15:27:54 -0800 | [diff] [blame] | 1887 | struct page *page = can_gather_numa_stats(*pte, vma, addr); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1888 | if (!page) |
| 1889 | continue; |
Dave Hansen | eb4866d | 2011-09-20 15:19:38 -0700 | [diff] [blame] | 1890 | gather_stats(page, md, pte_dirty(*pte), 1); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1891 | |
| 1892 | } while (pte++, addr += PAGE_SIZE, addr != end); |
| 1893 | pte_unmap_unlock(orig_pte, ptl); |
| 1894 | return 0; |
| 1895 | } |
| 1896 | #ifdef CONFIG_HUGETLB_PAGE |
Naoya Horiguchi | 632fd60 | 2015-02-11 15:27:51 -0800 | [diff] [blame] | 1897 | static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask, |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1898 | unsigned long addr, unsigned long end, struct mm_walk *walk) |
| 1899 | { |
Michael Holzheu | 5c2ff95 | 2016-02-02 16:57:26 -0800 | [diff] [blame] | 1900 | pte_t huge_pte = huge_ptep_get(pte); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1901 | struct numa_maps *md; |
| 1902 | struct page *page; |
| 1903 | |
Michael Holzheu | 5c2ff95 | 2016-02-02 16:57:26 -0800 | [diff] [blame] | 1904 | if (!pte_present(huge_pte)) |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1905 | return 0; |
| 1906 | |
Michael Holzheu | 5c2ff95 | 2016-02-02 16:57:26 -0800 | [diff] [blame] | 1907 | page = pte_page(huge_pte); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1908 | if (!page) |
| 1909 | return 0; |
| 1910 | |
| 1911 | md = walk->private; |
Michael Holzheu | 5c2ff95 | 2016-02-02 16:57:26 -0800 | [diff] [blame] | 1912 | gather_stats(page, md, pte_dirty(huge_pte), 1); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1913 | return 0; |
| 1914 | } |
| 1915 | |
| 1916 | #else |
Naoya Horiguchi | 632fd60 | 2015-02-11 15:27:51 -0800 | [diff] [blame] | 1917 | static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask, |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1918 | unsigned long addr, unsigned long end, struct mm_walk *walk) |
| 1919 | { |
| 1920 | return 0; |
| 1921 | } |
| 1922 | #endif |
| 1923 | |
| 1924 | /* |
| 1925 | * Display pages allocated per node and memory policy via /proc. |
| 1926 | */ |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 1927 | 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] | 1928 | { |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 1929 | struct numa_maps_private *numa_priv = m->private; |
| 1930 | struct proc_maps_private *proc_priv = &numa_priv->proc_maps; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1931 | struct vm_area_struct *vma = v; |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 1932 | struct numa_maps *md = &numa_priv->md; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1933 | struct file *file = vma->vm_file; |
| 1934 | struct mm_struct *mm = vma->vm_mm; |
Naoya Horiguchi | d85f4d6 | 2015-02-11 15:27:54 -0800 | [diff] [blame] | 1935 | struct mm_walk walk = { |
| 1936 | .hugetlb_entry = gather_hugetlb_stats, |
| 1937 | .pmd_entry = gather_pte_stats, |
| 1938 | .private = md, |
| 1939 | .mm = mm, |
| 1940 | }; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1941 | struct mempolicy *pol; |
David Rientjes | 948927e | 2013-11-12 15:07:28 -0800 | [diff] [blame] | 1942 | char buffer[64]; |
| 1943 | int nid; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1944 | |
| 1945 | if (!mm) |
| 1946 | return 0; |
| 1947 | |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 1948 | /* Ensure we start with an empty set of numa_maps statistics. */ |
| 1949 | memset(md, 0, sizeof(*md)); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1950 | |
Oleg Nesterov | 498f237 | 2014-10-09 15:27:52 -0700 | [diff] [blame] | 1951 | pol = __get_vma_policy(vma, vma->vm_start); |
| 1952 | if (pol) { |
| 1953 | mpol_to_str(buffer, sizeof(buffer), pol); |
| 1954 | mpol_cond_put(pol); |
| 1955 | } else { |
| 1956 | mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy); |
| 1957 | } |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1958 | |
| 1959 | seq_printf(m, "%08lx %s", vma->vm_start, buffer); |
| 1960 | |
| 1961 | if (file) { |
Fabian Frederick | 17c2b4e | 2014-06-06 14:37:03 -0700 | [diff] [blame] | 1962 | seq_puts(m, " file="); |
Miklos Szeredi | 2726d56 | 2015-06-19 10:30:28 +0200 | [diff] [blame] | 1963 | seq_file_path(m, file, "\n\t= "); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1964 | } 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] | 1965 | seq_puts(m, " heap"); |
Andy Lutomirski | b18cb64 | 2016-09-30 10:58:57 -0700 | [diff] [blame] | 1966 | } else if (is_stack(proc_priv, vma)) { |
Johannes Weiner | 65376df | 2016-02-02 16:57:29 -0800 | [diff] [blame] | 1967 | seq_puts(m, " stack"); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1968 | } |
| 1969 | |
Andrew Morton | fc360bd | 2011-10-31 17:06:32 -0700 | [diff] [blame] | 1970 | if (is_vm_hugetlb_page(vma)) |
Fabian Frederick | 17c2b4e | 2014-06-06 14:37:03 -0700 | [diff] [blame] | 1971 | seq_puts(m, " huge"); |
Andrew Morton | fc360bd | 2011-10-31 17:06:32 -0700 | [diff] [blame] | 1972 | |
Naoya Horiguchi | d85f4d6 | 2015-02-11 15:27:54 -0800 | [diff] [blame] | 1973 | /* mmap_sem is held by m_start */ |
| 1974 | walk_page_vma(vma, &walk); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 1975 | |
| 1976 | if (!md->pages) |
| 1977 | goto out; |
| 1978 | |
| 1979 | if (md->anon) |
| 1980 | seq_printf(m, " anon=%lu", md->anon); |
| 1981 | |
| 1982 | if (md->dirty) |
| 1983 | seq_printf(m, " dirty=%lu", md->dirty); |
| 1984 | |
| 1985 | if (md->pages != md->anon && md->pages != md->dirty) |
| 1986 | seq_printf(m, " mapped=%lu", md->pages); |
| 1987 | |
| 1988 | if (md->mapcount_max > 1) |
| 1989 | seq_printf(m, " mapmax=%lu", md->mapcount_max); |
| 1990 | |
| 1991 | if (md->swapcache) |
| 1992 | seq_printf(m, " swapcache=%lu", md->swapcache); |
| 1993 | |
| 1994 | if (md->active < md->pages && !is_vm_hugetlb_page(vma)) |
| 1995 | seq_printf(m, " active=%lu", md->active); |
| 1996 | |
| 1997 | if (md->writeback) |
| 1998 | seq_printf(m, " writeback=%lu", md->writeback); |
| 1999 | |
David Rientjes | 948927e | 2013-11-12 15:07:28 -0800 | [diff] [blame] | 2000 | for_each_node_state(nid, N_MEMORY) |
| 2001 | if (md->node[nid]) |
| 2002 | seq_printf(m, " N%d=%lu", nid, md->node[nid]); |
Rafael Aquini | 198d159 | 2015-02-12 15:01:08 -0800 | [diff] [blame] | 2003 | |
| 2004 | seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 2005 | out: |
| 2006 | seq_putc(m, '\n'); |
Oleg Nesterov | b8c20a9 | 2014-10-09 15:25:41 -0700 | [diff] [blame] | 2007 | m_cache_vma(m, vma); |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 2008 | return 0; |
| 2009 | } |
Stephen Wilson | 5b52fc8 | 2011-05-24 17:12:49 -0700 | [diff] [blame] | 2010 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2011 | static int show_pid_numa_map(struct seq_file *m, void *v) |
| 2012 | { |
| 2013 | return show_numa_map(m, v, 1); |
| 2014 | } |
| 2015 | |
| 2016 | static int show_tid_numa_map(struct seq_file *m, void *v) |
| 2017 | { |
| 2018 | return show_numa_map(m, v, 0); |
| 2019 | } |
| 2020 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 2021 | static const struct seq_operations proc_pid_numa_maps_op = { |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2022 | .start = m_start, |
| 2023 | .next = m_next, |
| 2024 | .stop = m_stop, |
| 2025 | .show = show_pid_numa_map, |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 2026 | }; |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2027 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2028 | static const struct seq_operations proc_tid_numa_maps_op = { |
| 2029 | .start = m_start, |
| 2030 | .next = m_next, |
| 2031 | .stop = m_stop, |
| 2032 | .show = show_tid_numa_map, |
| 2033 | }; |
| 2034 | |
| 2035 | static int numa_maps_open(struct inode *inode, struct file *file, |
| 2036 | const struct seq_operations *ops) |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2037 | { |
Oleg Nesterov | 4db7d0e | 2014-10-09 15:25:21 -0700 | [diff] [blame] | 2038 | return proc_maps_open(inode, file, ops, |
| 2039 | sizeof(struct numa_maps_private)); |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2040 | } |
| 2041 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2042 | static int pid_numa_maps_open(struct inode *inode, struct file *file) |
| 2043 | { |
| 2044 | return numa_maps_open(inode, file, &proc_pid_numa_maps_op); |
| 2045 | } |
| 2046 | |
| 2047 | static int tid_numa_maps_open(struct inode *inode, struct file *file) |
| 2048 | { |
| 2049 | return numa_maps_open(inode, file, &proc_tid_numa_maps_op); |
| 2050 | } |
| 2051 | |
| 2052 | const struct file_operations proc_pid_numa_maps_operations = { |
| 2053 | .open = pid_numa_maps_open, |
| 2054 | .read = seq_read, |
| 2055 | .llseek = seq_lseek, |
Oleg Nesterov | 29a40ac | 2014-10-09 15:25:26 -0700 | [diff] [blame] | 2056 | .release = proc_map_release, |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2057 | }; |
| 2058 | |
| 2059 | const struct file_operations proc_tid_numa_maps_operations = { |
| 2060 | .open = tid_numa_maps_open, |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2061 | .read = seq_read, |
| 2062 | .llseek = seq_lseek, |
Oleg Nesterov | 29a40ac | 2014-10-09 15:25:26 -0700 | [diff] [blame] | 2063 | .release = proc_map_release, |
Eric W. Biederman | 662795d | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2064 | }; |
Stephen Wilson | f69ff94 | 2011-05-24 17:12:47 -0700 | [diff] [blame] | 2065 | #endif /* CONFIG_NUMA */ |