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