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