blob: 9419c6972fcdcbe702fd1479cd1918a058711ec2 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwiga5201102019-08-28 16:19:53 +02002#include <linux/pagewalk.h>
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07003#include <linux/vmacache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/hugetlb.h>
Dave Hansen22e057c2011-03-22 16:33:00 -07005#include <linux/huge_mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/mount.h>
7#include <linux/seq_file.h>
Mauricio Line070ad42005-09-03 15:55:10 -07008#include <linux/highmem.h>
Kees Cook5096add2007-05-08 00:26:04 -07009#include <linux/ptrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Christoph Lameter6e21c8f2005-09-03 15:54:45 -070011#include <linux/pagemap.h>
12#include <linux/mempolicy.h>
Dave Hansen22e057c2011-03-22 16:33:00 -070013#include <linux/rmap.h>
Matt Mackall85863e42008-02-04 22:29:04 -080014#include <linux/swap.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010015#include <linux/sched/mm.h>
Matt Mackall85863e42008-02-04 22:29:04 -080016#include <linux/swapops.h>
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -070017#include <linux/mmu_notifier.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070018#include <linux/page_idle.h>
Vlastimil Babka6a15a372016-01-14 15:19:20 -080019#include <linux/shmem_fs.h>
Minchan Kimb3a81d02017-08-10 15:24:15 -070020#include <linux/uaccess.h>
Ram Pai27cca862018-04-13 23:55:07 +100021#include <linux/pkeys.h>
Mauricio Line070ad42005-09-03 15:55:10 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/elf.h>
Minchan Kimb3a81d02017-08-10 15:24:15 -070024#include <asm/tlb.h>
Mauricio Line070ad42005-09-03 15:55:10 -070025#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "internal.h"
27
Andrei Vagind1be35c2018-04-10 16:31:16 -070028#define SEQ_PUT_DEC(str, val) \
29 seq_put_decimal_ull_width(m, str, (val) << (PAGE_SHIFT-10), 8)
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080030void task_mem(struct seq_file *m, struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -080032 unsigned long text, lib, swap, anon, file, shmem;
Hugh Dickins365e9c872005-10-29 18:16:18 -070033 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
34
Jerome Marchand8cee8522016-01-14 15:19:29 -080035 anon = get_mm_counter(mm, MM_ANONPAGES);
36 file = get_mm_counter(mm, MM_FILEPAGES);
37 shmem = get_mm_counter(mm, MM_SHMEMPAGES);
38
Hugh Dickins365e9c872005-10-29 18:16:18 -070039 /*
40 * Note: to minimize their overhead, mm maintains hiwater_vm and
41 * hiwater_rss only when about to *lower* total_vm or rss. Any
42 * collector of these hiwater stats must therefore get total_vm
43 * and rss too, which will usually be the higher. Barriers? not
44 * worth the effort, such snapshots can always be inconsistent.
45 */
46 hiwater_vm = total_vm = mm->total_vm;
47 if (hiwater_vm < mm->hiwater_vm)
48 hiwater_vm = mm->hiwater_vm;
Jerome Marchand8cee8522016-01-14 15:19:29 -080049 hiwater_rss = total_rss = anon + file + shmem;
Hugh Dickins365e9c872005-10-29 18:16:18 -070050 if (hiwater_rss < mm->hiwater_rss)
51 hiwater_rss = mm->hiwater_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Konstantin Khlebnikov8526d842018-01-31 16:17:22 -080053 /* split executable areas between text and lib */
54 text = PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK);
55 text = min(text, mm->exec_vm << PAGE_SHIFT);
56 lib = (mm->exec_vm << PAGE_SHIFT) - text;
57
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080058 swap = get_mm_counter(mm, MM_SWAPENTS);
Andrei Vagind1be35c2018-04-10 16:31:16 -070059 SEQ_PUT_DEC("VmPeak:\t", hiwater_vm);
60 SEQ_PUT_DEC(" kB\nVmSize:\t", total_vm);
61 SEQ_PUT_DEC(" kB\nVmLck:\t", mm->locked_vm);
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -080062 SEQ_PUT_DEC(" kB\nVmPin:\t", atomic64_read(&mm->pinned_vm));
Andrei Vagind1be35c2018-04-10 16:31:16 -070063 SEQ_PUT_DEC(" kB\nVmHWM:\t", hiwater_rss);
64 SEQ_PUT_DEC(" kB\nVmRSS:\t", total_rss);
65 SEQ_PUT_DEC(" kB\nRssAnon:\t", anon);
66 SEQ_PUT_DEC(" kB\nRssFile:\t", file);
67 SEQ_PUT_DEC(" kB\nRssShmem:\t", shmem);
68 SEQ_PUT_DEC(" kB\nVmData:\t", mm->data_vm);
69 SEQ_PUT_DEC(" kB\nVmStk:\t", mm->stack_vm);
70 seq_put_decimal_ull_width(m,
71 " kB\nVmExe:\t", text >> 10, 8);
72 seq_put_decimal_ull_width(m,
73 " kB\nVmLib:\t", lib >> 10, 8);
74 seq_put_decimal_ull_width(m,
75 " kB\nVmPTE:\t", mm_pgtables_bytes(mm) >> 10, 8);
76 SEQ_PUT_DEC(" kB\nVmSwap:\t", swap);
77 seq_puts(m, " kB\n");
Naoya Horiguchi5d317b22015-11-05 18:47:14 -080078 hugetlb_report_usage(m, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
Andrei Vagind1be35c2018-04-10 16:31:16 -070080#undef SEQ_PUT_DEC
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82unsigned long task_vsize(struct mm_struct *mm)
83{
84 return PAGE_SIZE * mm->total_vm;
85}
86
Alexey Dobriyana2ade7b2011-01-12 17:00:32 -080087unsigned long task_statm(struct mm_struct *mm,
88 unsigned long *shared, unsigned long *text,
89 unsigned long *data, unsigned long *resident)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Jerome Marchandeca56ff2016-01-14 15:19:26 -080091 *shared = get_mm_counter(mm, MM_FILEPAGES) +
92 get_mm_counter(mm, MM_SHMEMPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
94 >> PAGE_SHIFT;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -080095 *data = mm->data_vm + mm->stack_vm;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -080096 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return mm->total_vm;
98}
99
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900100#ifdef CONFIG_NUMA
101/*
Oleg Nesterov498f2372014-10-09 15:27:52 -0700102 * Save get_task_policy() for show_numa_map().
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900103 */
104static void hold_task_mempolicy(struct proc_maps_private *priv)
105{
106 struct task_struct *task = priv->task;
107
108 task_lock(task);
Oleg Nesterov498f2372014-10-09 15:27:52 -0700109 priv->task_mempolicy = get_task_policy(task);
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900110 mpol_get(priv->task_mempolicy);
111 task_unlock(task);
112}
113static void release_task_mempolicy(struct proc_maps_private *priv)
114{
115 mpol_put(priv->task_mempolicy);
116}
117#else
118static void hold_task_mempolicy(struct proc_maps_private *priv)
119{
120}
121static void release_task_mempolicy(struct proc_maps_private *priv)
122{
123}
124#endif
125
Oleg Nesterovad2a00e2014-10-09 15:25:39 -0700126static struct vm_area_struct *
127m_next_vma(struct proc_maps_private *priv, struct vm_area_struct *vma)
128{
129 if (vma == priv->tail_vma)
130 return NULL;
131 return vma->vm_next ?: priv->tail_vma;
132}
133
Oleg Nesterov0c255322014-10-09 15:25:36 -0700134static void *m_start(struct seq_file *m, loff_t *ppos)
Matt Mackalla6198792008-02-04 22:29:03 -0800135{
136 struct proc_maps_private *priv = m->private;
Matthew Wilcox (Oracle)4781f2c2020-04-06 20:09:14 -0700137 unsigned long last_addr = *ppos;
Matt Mackalla6198792008-02-04 22:29:03 -0800138 struct mm_struct *mm;
Oleg Nesterov0c255322014-10-09 15:25:36 -0700139 struct vm_area_struct *vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800140
Matthew Wilcox (Oracle)c2e88d22020-04-06 20:09:11 -0700141 /* See m_next(). Zero at the start or after lseek. */
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700142 if (last_addr == -1UL)
143 return NULL;
144
Oleg Nesterov2c033762014-10-09 15:25:51 -0700145 priv->task = get_proc_task(priv->inode);
Matt Mackalla6198792008-02-04 22:29:03 -0800146 if (!priv->task)
Al Viroec6fd8a2011-02-15 22:22:54 -0500147 return ERR_PTR(-ESRCH);
Matt Mackalla6198792008-02-04 22:29:03 -0800148
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700149 mm = priv->mm;
Matthew Wilcox (Oracle)d07ded62020-04-06 20:09:08 -0700150 if (!mm || !mmget_not_zero(mm)) {
151 put_task_struct(priv->task);
152 priv->task = NULL;
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700153 return NULL;
Matthew Wilcox (Oracle)d07ded62020-04-06 20:09:08 -0700154 }
Oleg Nesterov0c255322014-10-09 15:25:36 -0700155
Konstantin Khlebnikov8a713e72019-07-11 20:59:50 -0700156 if (down_read_killable(&mm->mmap_sem)) {
157 mmput(mm);
Matthew Wilcox (Oracle)d07ded62020-04-06 20:09:08 -0700158 put_task_struct(priv->task);
159 priv->task = NULL;
Konstantin Khlebnikov8a713e72019-07-11 20:59:50 -0700160 return ERR_PTR(-EINTR);
161 }
162
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900163 hold_task_mempolicy(priv);
Oleg Nesterov0c255322014-10-09 15:25:36 -0700164 priv->tail_vma = get_gate_vma(mm);
Matt Mackalla6198792008-02-04 22:29:03 -0800165
Matthew Wilcox (Oracle)c2e88d22020-04-06 20:09:11 -0700166 vma = find_vma(mm, last_addr);
167 if (vma)
Oleg Nesterov0c255322014-10-09 15:25:36 -0700168 return vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800169
Matthew Wilcox (Oracle)c2e88d22020-04-06 20:09:11 -0700170 return priv->tail_vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800171}
172
Matthew Wilcox (Oracle)4781f2c2020-04-06 20:09:14 -0700173static void *m_next(struct seq_file *m, void *v, loff_t *ppos)
Matt Mackalla6198792008-02-04 22:29:03 -0800174{
175 struct proc_maps_private *priv = m->private;
Oleg Nesterovad2a00e2014-10-09 15:25:39 -0700176 struct vm_area_struct *next;
Matt Mackalla6198792008-02-04 22:29:03 -0800177
Oleg Nesterovad2a00e2014-10-09 15:25:39 -0700178 next = m_next_vma(priv, v);
Matthew Wilcox (Oracle)4781f2c2020-04-06 20:09:14 -0700179 *ppos = next ? next->vm_start : -1UL;
Matthew Wilcox (Oracle)c2e88d22020-04-06 20:09:11 -0700180
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700181 return next;
Matt Mackalla6198792008-02-04 22:29:03 -0800182}
183
184static void m_stop(struct seq_file *m, void *v)
185{
186 struct proc_maps_private *priv = m->private;
Matthew Wilcox (Oracle)d07ded62020-04-06 20:09:08 -0700187 struct mm_struct *mm = priv->mm;
Matt Mackalla6198792008-02-04 22:29:03 -0800188
Matthew Wilcox (Oracle)d07ded62020-04-06 20:09:08 -0700189 if (!priv->task)
190 return;
191
192 release_task_mempolicy(priv);
193 up_read(&mm->mmap_sem);
194 mmput(mm);
195 put_task_struct(priv->task);
196 priv->task = NULL;
Matt Mackalla6198792008-02-04 22:29:03 -0800197}
198
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700199static int proc_maps_open(struct inode *inode, struct file *file,
200 const struct seq_operations *ops, int psize)
201{
202 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
203
204 if (!priv)
205 return -ENOMEM;
206
Oleg Nesterov2c033762014-10-09 15:25:51 -0700207 priv->inode = inode;
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700208 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
209 if (IS_ERR(priv->mm)) {
210 int err = PTR_ERR(priv->mm);
211
212 seq_release_private(inode, file);
213 return err;
214 }
215
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700216 return 0;
217}
218
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700219static int proc_map_release(struct inode *inode, struct file *file)
220{
221 struct seq_file *seq = file->private_data;
222 struct proc_maps_private *priv = seq->private;
223
224 if (priv->mm)
225 mmdrop(priv->mm);
226
227 return seq_release_private(inode, file);
228}
229
Matt Mackalla6198792008-02-04 22:29:03 -0800230static int do_maps_open(struct inode *inode, struct file *file,
Jan Engelhardt03a44822008-02-08 04:21:19 -0800231 const struct seq_operations *ops)
Matt Mackalla6198792008-02-04 22:29:03 -0800232{
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700233 return proc_maps_open(inode, file, ops,
234 sizeof(struct proc_maps_private));
Matt Mackalla6198792008-02-04 22:29:03 -0800235}
Mauricio Line070ad42005-09-03 15:55:10 -0700236
Johannes Weiner65376df2016-02-02 16:57:29 -0800237/*
238 * Indicate if the VMA is a stack for the given task; for
239 * /proc/PID/maps that is the stack of the main task.
240 */
Michal Hocko1240ea02017-09-08 16:13:35 -0700241static int is_stack(struct vm_area_struct *vma)
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700242{
Andy Lutomirskib18cb642016-09-30 10:58:57 -0700243 /*
244 * We make no effort to guess what a given thread considers to be
245 * its "stack". It's not even well-defined for programs written
246 * languages like Go.
247 */
248 return vma->vm_start <= vma->vm_mm->start_stack &&
249 vma->vm_end >= vma->vm_mm->start_stack;
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700250}
251
Daniel Colascione493b0e92017-09-06 16:25:08 -0700252static void show_vma_header_prefix(struct seq_file *m,
253 unsigned long start, unsigned long end,
254 vm_flags_t flags, unsigned long long pgoff,
255 dev_t dev, unsigned long ino)
256{
257 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
Andrei Vagin0e3dc012018-04-10 16:30:44 -0700258 seq_put_hex_ll(m, NULL, start, 8);
259 seq_put_hex_ll(m, "-", end, 8);
260 seq_putc(m, ' ');
261 seq_putc(m, flags & VM_READ ? 'r' : '-');
262 seq_putc(m, flags & VM_WRITE ? 'w' : '-');
263 seq_putc(m, flags & VM_EXEC ? 'x' : '-');
264 seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p');
265 seq_put_hex_ll(m, " ", pgoff, 8);
266 seq_put_hex_ll(m, " ", MAJOR(dev), 2);
267 seq_put_hex_ll(m, ":", MINOR(dev), 2);
268 seq_put_decimal_ull(m, " ", ino);
269 seq_putc(m, ' ');
Daniel Colascione493b0e92017-09-06 16:25:08 -0700270}
271
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700272static void
Vlastimil Babka871305b2018-08-21 21:52:48 -0700273show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Mauricio Line070ad42005-09-03 15:55:10 -0700275 struct mm_struct *mm = vma->vm_mm;
276 struct file *file = vma->vm_file;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900277 vm_flags_t flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 unsigned long ino = 0;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700279 unsigned long long pgoff = 0;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200280 unsigned long start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 dev_t dev = 0;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700282 const char *name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (file) {
Al Viro496ad9a2013-01-23 17:07:38 -0500285 struct inode *inode = file_inode(vma->vm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 dev = inode->i_sb->s_dev;
287 ino = inode->i_ino;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700288 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
Linus Torvaldsd7824372010-08-15 11:35:52 -0700291 start = vma->vm_start;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200292 end = vma->vm_end;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700293 show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /*
296 * Print the dentry name for named mappings, and a
297 * special [heap] marker for the heap:
298 */
Mauricio Line070ad42005-09-03 15:55:10 -0700299 if (file) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800300 seq_pad(m, ' ');
Miklos Szeredi2726d562015-06-19 10:30:28 +0200301 seq_file_path(m, file, "\n");
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700302 goto done;
303 }
304
Andy Lutomirski78d683e2014-05-19 15:58:32 -0700305 if (vma->vm_ops && vma->vm_ops->name) {
306 name = vma->vm_ops->name(vma);
307 if (name)
308 goto done;
309 }
310
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700311 name = arch_vma_name(vma);
312 if (!name) {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700313 if (!mm) {
314 name = "[vdso]";
315 goto done;
316 }
317
318 if (vma->vm_start <= mm->brk &&
319 vma->vm_end >= mm->start_brk) {
320 name = "[heap]";
321 goto done;
322 }
323
Michal Hocko1240ea02017-09-08 16:13:35 -0700324 if (is_stack(vma))
Johannes Weiner65376df2016-02-02 16:57:29 -0800325 name = "[stack]";
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700326 }
327
328done:
329 if (name) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800330 seq_pad(m, ' ');
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700331 seq_puts(m, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333 seq_putc(m, '\n');
Joe Korty7c88db02008-10-16 15:27:09 +0400334}
335
Vlastimil Babka871305b2018-08-21 21:52:48 -0700336static int show_map(struct seq_file *m, void *v)
Joe Korty7c88db02008-10-16 15:27:09 +0400337{
Vlastimil Babka871305b2018-08-21 21:52:48 -0700338 show_map_vma(m, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return 0;
340}
341
Jan Engelhardt03a44822008-02-08 04:21:19 -0800342static const struct seq_operations proc_pid_maps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800343 .start = m_start,
344 .next = m_next,
345 .stop = m_stop,
Vlastimil Babka871305b2018-08-21 21:52:48 -0700346 .show = show_map
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700347};
348
349static int pid_maps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800350{
351 return do_maps_open(inode, file, &proc_pid_maps_op);
352}
353
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700354const struct file_operations proc_pid_maps_operations = {
355 .open = pid_maps_open,
356 .read = seq_read,
357 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700358 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700359};
360
Matt Mackalla6198792008-02-04 22:29:03 -0800361/*
362 * Proportional Set Size(PSS): my share of RSS.
363 *
364 * PSS of a process is the count of pages it has in memory, where each
365 * page is divided by the number of processes sharing it. So if a
366 * process has 1000 pages all to itself, and 1000 shared with one other
367 * process, its PSS will be 1500.
368 *
369 * To keep (accumulated) division errors low, we adopt a 64bit
370 * fixed-point pss counter to minimize division errors. So (pss >>
371 * PSS_SHIFT) would be the real byte count.
372 *
373 * A shift of 12 before division means (assuming 4K page size):
374 * - 1M 3-user-pages add up to 8KB errors;
375 * - supports mapcount up to 2^24, or 16M;
376 * - supports PSS up to 2^52 bytes, or 4PB.
377 */
378#define PSS_SHIFT 12
379
Matt Mackall1e883282008-02-04 22:29:07 -0800380#ifdef CONFIG_PROC_PAGE_MONITOR
Peter Zijlstra214e4712008-04-28 02:12:55 -0700381struct mem_size_stats {
Matt Mackalla6198792008-02-04 22:29:03 -0800382 unsigned long resident;
383 unsigned long shared_clean;
384 unsigned long shared_dirty;
385 unsigned long private_clean;
386 unsigned long private_dirty;
387 unsigned long referenced;
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700388 unsigned long anonymous;
Shaohua Licf8496e2017-05-03 14:52:42 -0700389 unsigned long lazyfree;
Dave Hansen4031a212011-03-22 16:33:01 -0700390 unsigned long anonymous_thp;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700391 unsigned long shmem_thp;
Song Liu60fbf0a2019-09-23 15:37:54 -0700392 unsigned long file_thp;
Peter Zijlstra214e4712008-04-28 02:12:55 -0700393 unsigned long swap;
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800394 unsigned long shared_hugetlb;
395 unsigned long private_hugetlb;
Matt Mackalla6198792008-02-04 22:29:03 -0800396 u64 pss;
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700397 u64 pss_anon;
398 u64 pss_file;
399 u64 pss_shmem;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700400 u64 pss_locked;
Minchan Kim8334b962015-09-08 15:00:24 -0700401 u64 swap_pss;
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800402 bool check_shmem_swap;
Matt Mackalla6198792008-02-04 22:29:03 -0800403};
404
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700405static void smaps_page_accumulate(struct mem_size_stats *mss,
406 struct page *page, unsigned long size, unsigned long pss,
407 bool dirty, bool locked, bool private)
408{
409 mss->pss += pss;
410
411 if (PageAnon(page))
412 mss->pss_anon += pss;
413 else if (PageSwapBacked(page))
414 mss->pss_shmem += pss;
415 else
416 mss->pss_file += pss;
417
418 if (locked)
419 mss->pss_locked += pss;
420
421 if (dirty || PageDirty(page)) {
422 if (private)
423 mss->private_dirty += size;
424 else
425 mss->shared_dirty += size;
426 } else {
427 if (private)
428 mss->private_clean += size;
429 else
430 mss->shared_clean += size;
431 }
432}
433
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800434static void smaps_account(struct mem_size_stats *mss, struct page *page,
Sandeep Patil27dd7682019-02-12 15:36:11 -0800435 bool compound, bool young, bool dirty, bool locked)
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800436{
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700437 int i, nr = compound ? compound_nr(page) : 1;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800438 unsigned long size = nr * PAGE_SIZE;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700439
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700440 /*
441 * First accumulate quantities that depend only on |size| and the type
442 * of the compound page.
443 */
Shaohua Licf8496e2017-05-03 14:52:42 -0700444 if (PageAnon(page)) {
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800445 mss->anonymous += size;
Shaohua Licf8496e2017-05-03 14:52:42 -0700446 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
447 mss->lazyfree += size;
448 }
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800449
450 mss->resident += size;
451 /* Accumulate the size in pages that have been accessed. */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700452 if (young || page_is_young(page) || PageReferenced(page))
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800453 mss->referenced += size;
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800454
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800455 /*
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700456 * Then accumulate quantities that may depend on sharing, or that may
457 * differ page-by-page.
458 *
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800459 * page_count(page) == 1 guarantees the page is mapped exactly once.
460 * If any subpage of the compound page mapped with PTE it would elevate
461 * page_count().
462 */
463 if (page_count(page) == 1) {
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700464 smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty,
465 locked, true);
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800466 return;
467 }
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800468 for (i = 0; i < nr; i++, page++) {
469 int mapcount = page_mapcount(page);
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700470 unsigned long pss = PAGE_SIZE << PSS_SHIFT;
471 if (mapcount >= 2)
472 pss /= mapcount;
473 smaps_page_accumulate(mss, page, PAGE_SIZE, pss, dirty, locked,
474 mapcount < 2);
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800475 }
476}
477
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800478#ifdef CONFIG_SHMEM
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800479static int smaps_pte_hole(unsigned long addr, unsigned long end,
Steven Priceb7a16c72020-02-03 17:36:03 -0800480 __always_unused int depth, struct mm_walk *walk)
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800481{
482 struct mem_size_stats *mss = walk->private;
483
Vlastimil Babka48131e02016-01-14 15:19:23 -0800484 mss->swap += shmem_partial_swap_usage(
485 walk->vma->vm_file->f_mapping, addr, end);
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800486
487 return 0;
488}
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200489#else
490#define smaps_pte_hole NULL
491#endif /* CONFIG_SHMEM */
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800492
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800493static void smaps_pte_entry(pte_t *pte, unsigned long addr,
494 struct mm_walk *walk)
Dave Hansenae11c4d2011-03-22 16:32:58 -0700495{
496 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800497 struct vm_area_struct *vma = walk->vma;
Sandeep Patil27dd7682019-02-12 15:36:11 -0800498 bool locked = !!(vma->vm_flags & VM_LOCKED);
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700499 struct page *page = NULL;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700500
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800501 if (pte_present(*pte)) {
502 page = vm_normal_page(vma, addr, *pte);
503 } else if (is_swap_pte(*pte)) {
504 swp_entry_t swpent = pte_to_swp_entry(*pte);
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700505
Minchan Kim8334b962015-09-08 15:00:24 -0700506 if (!non_swap_entry(swpent)) {
507 int mapcount;
508
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800509 mss->swap += PAGE_SIZE;
Minchan Kim8334b962015-09-08 15:00:24 -0700510 mapcount = swp_swapcount(swpent);
511 if (mapcount >= 2) {
512 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
513
514 do_div(pss_delta, mapcount);
515 mss->swap_pss += pss_delta;
516 } else {
517 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
518 }
519 } else if (is_migration_entry(swpent))
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700520 page = migration_entry_to_page(swpent);
Jérôme Glisse5042db42017-09-08 16:11:43 -0700521 else if (is_device_private_entry(swpent))
522 page = device_private_entry_to_page(swpent);
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800523 } else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
524 && pte_none(*pte))) {
Vlastimil Babka48131e02016-01-14 15:19:23 -0800525 page = find_get_entry(vma->vm_file->f_mapping,
526 linear_page_index(vma, addr));
527 if (!page)
528 return;
529
Matthew Wilcox3159f942017-11-03 13:30:42 -0400530 if (xa_is_value(page))
Vlastimil Babka48131e02016-01-14 15:19:23 -0800531 mss->swap += PAGE_SIZE;
532 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300533 put_page(page);
Vlastimil Babka48131e02016-01-14 15:19:23 -0800534
535 return;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700536 }
537
Dave Hansenae11c4d2011-03-22 16:32:58 -0700538 if (!page)
539 return;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800540
Sandeep Patil27dd7682019-02-12 15:36:11 -0800541 smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte), locked);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700542}
543
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800544#ifdef CONFIG_TRANSPARENT_HUGEPAGE
545static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
546 struct mm_walk *walk)
547{
548 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800549 struct vm_area_struct *vma = walk->vma;
Sandeep Patil27dd7682019-02-12 15:36:11 -0800550 bool locked = !!(vma->vm_flags & VM_LOCKED);
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800551 struct page *page;
552
553 /* FOLL_DUMP will return -EFAULT on huge zero page */
554 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
555 if (IS_ERR_OR_NULL(page))
556 return;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700557 if (PageAnon(page))
558 mss->anonymous_thp += HPAGE_PMD_SIZE;
559 else if (PageSwapBacked(page))
560 mss->shmem_thp += HPAGE_PMD_SIZE;
Dan Williamsca120cf2016-09-03 10:38:03 -0700561 else if (is_zone_device_page(page))
562 /* pass */;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700563 else
Song Liu60fbf0a2019-09-23 15:37:54 -0700564 mss->file_thp += HPAGE_PMD_SIZE;
Sandeep Patil27dd7682019-02-12 15:36:11 -0800565 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), locked);
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800566}
567#else
568static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
569 struct mm_walk *walk)
570{
571}
572#endif
573
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800574static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700575 struct mm_walk *walk)
Mauricio Line070ad42005-09-03 15:55:10 -0700576{
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800577 struct vm_area_struct *vma = walk->vma;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700578 pte_t *pte;
Hugh Dickins705e87c2005-10-29 18:16:27 -0700579 spinlock_t *ptl;
Mauricio Line070ad42005-09-03 15:55:10 -0700580
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -0800581 ptl = pmd_trans_huge_lock(pmd, vma);
582 if (ptl) {
Zi Yan84c3fc42017-09-08 16:11:01 -0700583 if (pmd_present(*pmd))
584 smaps_pmd_entry(pmd, addr, walk);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -0800585 spin_unlock(ptl);
David Rientjes14038302017-09-08 16:13:41 -0700586 goto out;
Dave Hansen22e057c2011-03-22 16:33:00 -0700587 }
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700588
589 if (pmd_trans_unstable(pmd))
David Rientjes14038302017-09-08 16:13:41 -0700590 goto out;
Dave Hansen22e057c2011-03-22 16:33:00 -0700591 /*
592 * The mmap_sem held all the way back in m_start() is what
593 * keeps khugepaged out of here and from collapsing things
594 * in here.
595 */
Hugh Dickins705e87c2005-10-29 18:16:27 -0700596 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700597 for (; addr != end; pte++, addr += PAGE_SIZE)
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800598 smaps_pte_entry(pte, addr, walk);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700599 pte_unmap_unlock(pte - 1, ptl);
David Rientjes14038302017-09-08 16:13:41 -0700600out:
Hugh Dickins705e87c2005-10-29 18:16:27 -0700601 cond_resched();
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800602 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700603}
604
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800605static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
606{
607 /*
608 * Don't forget to update Documentation/ on changes.
609 */
610 static const char mnemonics[BITS_PER_LONG][2] = {
611 /*
612 * In case if we meet a flag we don't know about.
613 */
614 [0 ... (BITS_PER_LONG-1)] = "??",
615
616 [ilog2(VM_READ)] = "rd",
617 [ilog2(VM_WRITE)] = "wr",
618 [ilog2(VM_EXEC)] = "ex",
619 [ilog2(VM_SHARED)] = "sh",
620 [ilog2(VM_MAYREAD)] = "mr",
621 [ilog2(VM_MAYWRITE)] = "mw",
622 [ilog2(VM_MAYEXEC)] = "me",
623 [ilog2(VM_MAYSHARE)] = "ms",
624 [ilog2(VM_GROWSDOWN)] = "gd",
625 [ilog2(VM_PFNMAP)] = "pf",
626 [ilog2(VM_DENYWRITE)] = "dw",
Qiaowei Ren4aae7e42014-11-14 07:18:25 -0800627#ifdef CONFIG_X86_INTEL_MPX
628 [ilog2(VM_MPX)] = "mp",
629#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800630 [ilog2(VM_LOCKED)] = "lo",
631 [ilog2(VM_IO)] = "io",
632 [ilog2(VM_SEQ_READ)] = "sr",
633 [ilog2(VM_RAND_READ)] = "rr",
634 [ilog2(VM_DONTCOPY)] = "dc",
635 [ilog2(VM_DONTEXPAND)] = "de",
636 [ilog2(VM_ACCOUNT)] = "ac",
637 [ilog2(VM_NORESERVE)] = "nr",
638 [ilog2(VM_HUGETLB)] = "ht",
Jan Karab6fb2932017-11-01 16:36:41 +0100639 [ilog2(VM_SYNC)] = "sf",
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800640 [ilog2(VM_ARCH_1)] = "ar",
Rik van Rield2cd9ed2017-09-06 16:25:15 -0700641 [ilog2(VM_WIPEONFORK)] = "wf",
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800642 [ilog2(VM_DONTDUMP)] = "dd",
Naoya Horiguchiec8e41a2013-11-12 15:07:49 -0800643#ifdef CONFIG_MEM_SOFT_DIRTY
644 [ilog2(VM_SOFTDIRTY)] = "sd",
645#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800646 [ilog2(VM_MIXEDMAP)] = "mm",
647 [ilog2(VM_HUGEPAGE)] = "hg",
648 [ilog2(VM_NOHUGEPAGE)] = "nh",
649 [ilog2(VM_MERGEABLE)] = "mg",
Andrea Arcangeli16ba6f82015-09-04 15:46:17 -0700650 [ilog2(VM_UFFD_MISSING)]= "um",
651 [ilog2(VM_UFFD_WP)] = "uw",
Ram Pai52122132018-03-27 02:09:26 -0700652#ifdef CONFIG_ARCH_HAS_PKEYS
Dave Hansenc1192f82016-02-12 13:02:27 -0800653 /* These come out via ProtectionKey: */
654 [ilog2(VM_PKEY_BIT0)] = "",
655 [ilog2(VM_PKEY_BIT1)] = "",
656 [ilog2(VM_PKEY_BIT2)] = "",
657 [ilog2(VM_PKEY_BIT3)] = "",
Ram Pai2c9e0a62018-03-27 02:09:27 -0700658#if VM_PKEY_BIT4
659 [ilog2(VM_PKEY_BIT4)] = "",
Dave Hansenc1192f82016-02-12 13:02:27 -0800660#endif
Ram Pai52122132018-03-27 02:09:26 -0700661#endif /* CONFIG_ARCH_HAS_PKEYS */
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800662 };
663 size_t i;
664
665 seq_puts(m, "VmFlags: ");
666 for (i = 0; i < BITS_PER_LONG; i++) {
Dave Hansenc1192f82016-02-12 13:02:27 -0800667 if (!mnemonics[i][0])
668 continue;
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800669 if (vma->vm_flags & (1UL << i)) {
Andrei Vaginf6640662018-04-10 16:31:19 -0700670 seq_putc(m, mnemonics[i][0]);
671 seq_putc(m, mnemonics[i][1]);
672 seq_putc(m, ' ');
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800673 }
674 }
675 seq_putc(m, '\n');
676}
677
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800678#ifdef CONFIG_HUGETLB_PAGE
679static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
680 unsigned long addr, unsigned long end,
681 struct mm_walk *walk)
682{
683 struct mem_size_stats *mss = walk->private;
684 struct vm_area_struct *vma = walk->vma;
685 struct page *page = NULL;
686
687 if (pte_present(*pte)) {
688 page = vm_normal_page(vma, addr, *pte);
689 } else if (is_swap_pte(*pte)) {
690 swp_entry_t swpent = pte_to_swp_entry(*pte);
691
692 if (is_migration_entry(swpent))
693 page = migration_entry_to_page(swpent);
Jérôme Glisse5042db42017-09-08 16:11:43 -0700694 else if (is_device_private_entry(swpent))
695 page = device_private_entry_to_page(swpent);
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800696 }
697 if (page) {
698 int mapcount = page_mapcount(page);
699
700 if (mapcount >= 2)
701 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
702 else
703 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
704 }
705 return 0;
706}
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200707#else
708#define smaps_hugetlb_range NULL
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800709#endif /* HUGETLB_PAGE */
710
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200711static const struct mm_walk_ops smaps_walk_ops = {
712 .pmd_entry = smaps_pte_range,
713 .hugetlb_entry = smaps_hugetlb_range,
714};
715
716static const struct mm_walk_ops smaps_shmem_walk_ops = {
717 .pmd_entry = smaps_pte_range,
718 .hugetlb_entry = smaps_hugetlb_range,
719 .pte_hole = smaps_pte_hole,
720};
721
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700722static void smap_gather_stats(struct vm_area_struct *vma,
723 struct mem_size_stats *mss)
Mauricio Line070ad42005-09-03 15:55:10 -0700724{
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800725#ifdef CONFIG_SHMEM
Vlastimil Babkafa76da42018-10-26 15:02:16 -0700726 /* In case of smaps_rollup, reset the value from previous vma */
727 mss->check_shmem_swap = false;
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800728 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800729 /*
730 * For shared or readonly shmem mappings we know that all
731 * swapped out pages belong to the shmem object, and we can
732 * obtain the swap value much more efficiently. For private
733 * writable mappings, we might have COW pages that are
734 * not affected by the parent swapped out pages of the shmem
735 * object, so we have to distinguish them during the page walk.
736 * Unless we know that the shmem object (or the part mapped by
737 * our VMA) has no swapped out pages at all.
738 */
739 unsigned long shmem_swapped = shmem_swap_usage(vma);
740
741 if (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
742 !(vma->vm_flags & VM_WRITE)) {
Vlastimil Babkafa76da42018-10-26 15:02:16 -0700743 mss->swap += shmem_swapped;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800744 } else {
Daniel Colascione493b0e92017-09-06 16:25:08 -0700745 mss->check_shmem_swap = true;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200746 walk_page_vma(vma, &smaps_shmem_walk_ops, mss);
747 return;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800748 }
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800749 }
750#endif
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +0900751 /* mmap_sem is held in m_start */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200752 walk_page_vma(vma, &smaps_walk_ops, mss);
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700753}
754
755#define SEQ_PUT_DEC(str, val) \
756 seq_put_decimal_ull_width(m, str, (val) >> 10, 8)
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700757
758/* Show the contents common for smaps and smaps_rollup */
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700759static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss,
760 bool rollup_mode)
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700761{
762 SEQ_PUT_DEC("Rss: ", mss->resident);
763 SEQ_PUT_DEC(" kB\nPss: ", mss->pss >> PSS_SHIFT);
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700764 if (rollup_mode) {
765 /*
766 * These are meaningful only for smaps_rollup, otherwise two of
767 * them are zero, and the other one is the same as Pss.
768 */
769 SEQ_PUT_DEC(" kB\nPss_Anon: ",
770 mss->pss_anon >> PSS_SHIFT);
771 SEQ_PUT_DEC(" kB\nPss_File: ",
772 mss->pss_file >> PSS_SHIFT);
773 SEQ_PUT_DEC(" kB\nPss_Shmem: ",
774 mss->pss_shmem >> PSS_SHIFT);
775 }
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700776 SEQ_PUT_DEC(" kB\nShared_Clean: ", mss->shared_clean);
777 SEQ_PUT_DEC(" kB\nShared_Dirty: ", mss->shared_dirty);
778 SEQ_PUT_DEC(" kB\nPrivate_Clean: ", mss->private_clean);
779 SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty);
780 SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced);
781 SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous);
782 SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree);
783 SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp);
784 SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp);
Song Liu60fbf0a2019-09-23 15:37:54 -0700785 SEQ_PUT_DEC(" kB\nFilePmdMapped: ", mss->file_thp);
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700786 SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb);
787 seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ",
788 mss->private_hugetlb >> 10, 7);
789 SEQ_PUT_DEC(" kB\nSwap: ", mss->swap);
790 SEQ_PUT_DEC(" kB\nSwapPss: ",
791 mss->swap_pss >> PSS_SHIFT);
792 SEQ_PUT_DEC(" kB\nLocked: ",
793 mss->pss_locked >> PSS_SHIFT);
794 seq_puts(m, " kB\n");
795}
796
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700797static int show_smap(struct seq_file *m, void *v)
798{
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700799 struct vm_area_struct *vma = v;
Vlastimil Babka258f6692018-08-21 21:52:59 -0700800 struct mem_size_stats mss;
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700801
Vlastimil Babka258f6692018-08-21 21:52:59 -0700802 memset(&mss, 0, sizeof(mss));
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700803
Vlastimil Babka258f6692018-08-21 21:52:59 -0700804 smap_gather_stats(vma, &mss);
Matt Mackall4752c362008-02-04 22:29:02 -0800805
Vlastimil Babka258f6692018-08-21 21:52:59 -0700806 show_map_vma(m, vma);
Matt Mackall4752c362008-02-04 22:29:02 -0800807
Vlastimil Babka258f6692018-08-21 21:52:59 -0700808 SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start);
809 SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma));
810 SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma));
811 seq_puts(m, " kB\n");
Matt Mackall4752c362008-02-04 22:29:02 -0800812
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700813 __show_smap(m, &mss, false);
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700814
Yang Shic0630662019-07-18 15:57:27 -0700815 seq_printf(m, "THPeligible: %d\n",
816 transparent_hugepage_enabled(vma));
Michal Hocko7635d9c2018-12-28 00:38:21 -0800817
Vlastimil Babka258f6692018-08-21 21:52:59 -0700818 if (arch_pkeys_enabled())
819 seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma));
820 show_smap_vma_flags(m, vma);
821
Vlastimil Babka258f6692018-08-21 21:52:59 -0700822 return 0;
823}
824
825static int show_smaps_rollup(struct seq_file *m, void *v)
826{
827 struct proc_maps_private *priv = m->private;
828 struct mem_size_stats mss;
829 struct mm_struct *mm;
830 struct vm_area_struct *vma;
831 unsigned long last_vma_end = 0;
832 int ret = 0;
833
834 priv->task = get_proc_task(priv->inode);
835 if (!priv->task)
836 return -ESRCH;
837
838 mm = priv->mm;
839 if (!mm || !mmget_not_zero(mm)) {
840 ret = -ESRCH;
841 goto out_put_task;
842 }
843
844 memset(&mss, 0, sizeof(mss));
845
Konstantin Khlebnikova26a9782019-07-11 20:59:53 -0700846 ret = down_read_killable(&mm->mmap_sem);
847 if (ret)
848 goto out_put_mm;
849
Vlastimil Babka258f6692018-08-21 21:52:59 -0700850 hold_task_mempolicy(priv);
851
852 for (vma = priv->mm->mmap; vma; vma = vma->vm_next) {
853 smap_gather_stats(vma, &mss);
854 last_vma_end = vma->vm_end;
855 }
856
857 show_vma_header_prefix(m, priv->mm->mmap->vm_start,
858 last_vma_end, 0, 0, 0, 0);
859 seq_pad(m, ' ');
860 seq_puts(m, "[rollup]\n");
861
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700862 __show_smap(m, &mss, true);
Vlastimil Babka258f6692018-08-21 21:52:59 -0700863
864 release_task_mempolicy(priv);
865 up_read(&mm->mmap_sem);
Vlastimil Babka258f6692018-08-21 21:52:59 -0700866
Konstantin Khlebnikova26a9782019-07-11 20:59:53 -0700867out_put_mm:
868 mmput(mm);
Vlastimil Babka258f6692018-08-21 21:52:59 -0700869out_put_task:
870 put_task_struct(priv->task);
871 priv->task = NULL;
872
Daniel Colascione493b0e92017-09-06 16:25:08 -0700873 return ret;
Mauricio Line070ad42005-09-03 15:55:10 -0700874}
Andrei Vagind1be35c2018-04-10 16:31:16 -0700875#undef SEQ_PUT_DEC
Mauricio Line070ad42005-09-03 15:55:10 -0700876
Jan Engelhardt03a44822008-02-08 04:21:19 -0800877static const struct seq_operations proc_pid_smaps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800878 .start = m_start,
879 .next = m_next,
880 .stop = m_stop,
Vlastimil Babka871305b2018-08-21 21:52:48 -0700881 .show = show_smap
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700882};
883
884static int pid_smaps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800885{
886 return do_maps_open(inode, file, &proc_pid_smaps_op);
887}
888
Vlastimil Babka258f6692018-08-21 21:52:59 -0700889static int smaps_rollup_open(struct inode *inode, struct file *file)
Daniel Colascione493b0e92017-09-06 16:25:08 -0700890{
Vlastimil Babka258f6692018-08-21 21:52:59 -0700891 int ret;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700892 struct proc_maps_private *priv;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700893
Vlastimil Babka258f6692018-08-21 21:52:59 -0700894 priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT);
895 if (!priv)
Daniel Colascione493b0e92017-09-06 16:25:08 -0700896 return -ENOMEM;
Vlastimil Babka258f6692018-08-21 21:52:59 -0700897
898 ret = single_open(file, show_smaps_rollup, priv);
899 if (ret)
900 goto out_free;
901
902 priv->inode = inode;
903 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
904 if (IS_ERR(priv->mm)) {
905 ret = PTR_ERR(priv->mm);
906
907 single_release(inode, file);
908 goto out_free;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700909 }
Vlastimil Babka258f6692018-08-21 21:52:59 -0700910
Daniel Colascione493b0e92017-09-06 16:25:08 -0700911 return 0;
Vlastimil Babka258f6692018-08-21 21:52:59 -0700912
913out_free:
914 kfree(priv);
915 return ret;
916}
917
918static int smaps_rollup_release(struct inode *inode, struct file *file)
919{
920 struct seq_file *seq = file->private_data;
921 struct proc_maps_private *priv = seq->private;
922
923 if (priv->mm)
924 mmdrop(priv->mm);
925
926 kfree(priv);
927 return single_release(inode, file);
Daniel Colascione493b0e92017-09-06 16:25:08 -0700928}
929
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700930const struct file_operations proc_pid_smaps_operations = {
931 .open = pid_smaps_open,
932 .read = seq_read,
933 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700934 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700935};
936
Daniel Colascione493b0e92017-09-06 16:25:08 -0700937const struct file_operations proc_pid_smaps_rollup_operations = {
Vlastimil Babka258f6692018-08-21 21:52:59 -0700938 .open = smaps_rollup_open,
Daniel Colascione493b0e92017-09-06 16:25:08 -0700939 .read = seq_read,
940 .llseek = seq_lseek,
Vlastimil Babka258f6692018-08-21 21:52:59 -0700941 .release = smaps_rollup_release,
Daniel Colascione493b0e92017-09-06 16:25:08 -0700942};
943
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700944enum clear_refs_types {
945 CLEAR_REFS_ALL = 1,
946 CLEAR_REFS_ANON,
947 CLEAR_REFS_MAPPED,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700948 CLEAR_REFS_SOFT_DIRTY,
Petr Cermak695f0552015-02-12 15:01:00 -0800949 CLEAR_REFS_MM_HIWATER_RSS,
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700950 CLEAR_REFS_LAST,
951};
952
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700953struct clear_refs_private {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700954 enum clear_refs_types type;
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700955};
956
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800957#ifdef CONFIG_MEM_SOFT_DIRTY
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700958static inline void clear_soft_dirty(struct vm_area_struct *vma,
959 unsigned long addr, pte_t *pte)
960{
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700961 /*
962 * The soft-dirty tracker uses #PF-s to catch writes
963 * to pages, so write-protect the pte as well. See the
Mike Rapoport1ad13352018-04-18 11:07:49 +0300964 * Documentation/admin-guide/mm/soft-dirty.rst for full description
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700965 * of how soft-dirty works.
966 */
967 pte_t ptent = *pte;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700968
969 if (pte_present(ptent)) {
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -0800970 pte_t old_pte;
971
972 old_pte = ptep_modify_prot_start(vma, addr, pte);
973 ptent = pte_wrprotect(old_pte);
Martin Schwidefskya7b76172015-04-22 14:20:47 +0200974 ptent = pte_clear_soft_dirty(ptent);
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -0800975 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700976 } else if (is_swap_pte(ptent)) {
977 ptent = pte_swp_clear_soft_dirty(ptent);
Laurent Dufour326c2592015-11-05 18:49:21 -0800978 set_pte_at(vma->vm_mm, addr, pte, ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700979 }
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700980}
Laurent Dufour5d3875a2015-11-05 18:49:24 -0800981#else
982static inline void clear_soft_dirty(struct vm_area_struct *vma,
983 unsigned long addr, pte_t *pte)
984{
985}
986#endif
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700987
Laurent Dufour5d3875a2015-11-05 18:49:24 -0800988#if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800989static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
990 unsigned long addr, pmd_t *pmdp)
991{
Kirill A. Shutemova3cf9882018-01-31 16:18:20 -0800992 pmd_t old, pmd = *pmdp;
Kirill A. Shutemov5b7abea2017-04-13 14:56:28 -0700993
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -0700994 if (pmd_present(pmd)) {
995 /* See comment in change_huge_pmd() */
Kirill A. Shutemova3cf9882018-01-31 16:18:20 -0800996 old = pmdp_invalidate(vma, addr, pmdp);
997 if (pmd_dirty(old))
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -0700998 pmd = pmd_mkdirty(pmd);
Kirill A. Shutemova3cf9882018-01-31 16:18:20 -0800999 if (pmd_young(old))
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001000 pmd = pmd_mkyoung(pmd);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001001
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001002 pmd = pmd_wrprotect(pmd);
1003 pmd = pmd_clear_soft_dirty(pmd);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001004
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001005 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1006 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) {
1007 pmd = pmd_swp_clear_soft_dirty(pmd);
1008 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1009 }
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001010}
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001011#else
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001012static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1013 unsigned long addr, pmd_t *pmdp)
1014{
1015}
1016#endif
1017
Matt Mackalla6198792008-02-04 22:29:03 -08001018static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
Dave Hansen21650092008-06-12 15:21:47 -07001019 unsigned long end, struct mm_walk *walk)
Matt Mackalla6198792008-02-04 22:29:03 -08001020{
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001021 struct clear_refs_private *cp = walk->private;
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001022 struct vm_area_struct *vma = walk->vma;
Matt Mackalla6198792008-02-04 22:29:03 -08001023 pte_t *pte, ptent;
1024 spinlock_t *ptl;
1025 struct page *page;
1026
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001027 ptl = pmd_trans_huge_lock(pmd, vma);
1028 if (ptl) {
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001029 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1030 clear_soft_dirty_pmd(vma, addr, pmd);
1031 goto out;
1032 }
1033
Zi Yan84c3fc42017-09-08 16:11:01 -07001034 if (!pmd_present(*pmd))
1035 goto out;
1036
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001037 page = pmd_page(*pmd);
1038
1039 /* Clear accessed and referenced bits. */
1040 pmdp_test_and_clear_young(vma, addr, pmd);
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07001041 test_and_clear_page_young(page);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001042 ClearPageReferenced(page);
1043out:
1044 spin_unlock(ptl);
1045 return 0;
1046 }
1047
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001048 if (pmd_trans_unstable(pmd))
1049 return 0;
Dave Hansen03319322011-03-22 16:32:56 -07001050
Matt Mackalla6198792008-02-04 22:29:03 -08001051 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1052 for (; addr != end; pte++, addr += PAGE_SIZE) {
1053 ptent = *pte;
Matt Mackalla6198792008-02-04 22:29:03 -08001054
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001055 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1056 clear_soft_dirty(vma, addr, pte);
1057 continue;
1058 }
1059
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001060 if (!pte_present(ptent))
1061 continue;
1062
Matt Mackalla6198792008-02-04 22:29:03 -08001063 page = vm_normal_page(vma, addr, ptent);
1064 if (!page)
1065 continue;
1066
1067 /* Clear accessed and referenced bits. */
1068 ptep_test_and_clear_young(vma, addr, pte);
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07001069 test_and_clear_page_young(page);
Matt Mackalla6198792008-02-04 22:29:03 -08001070 ClearPageReferenced(page);
1071 }
1072 pte_unmap_unlock(pte - 1, ptl);
1073 cond_resched();
1074 return 0;
1075}
1076
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001077static int clear_refs_test_walk(unsigned long start, unsigned long end,
1078 struct mm_walk *walk)
1079{
1080 struct clear_refs_private *cp = walk->private;
1081 struct vm_area_struct *vma = walk->vma;
1082
Naoya Horiguchi48684a62015-02-11 15:28:06 -08001083 if (vma->vm_flags & VM_PFNMAP)
1084 return 1;
1085
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001086 /*
1087 * Writing 1 to /proc/pid/clear_refs affects all pages.
1088 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
1089 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
1090 * Writing 4 to /proc/pid/clear_refs affects all pages.
1091 */
1092 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
1093 return 1;
1094 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1095 return 1;
1096 return 0;
1097}
1098
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001099static const struct mm_walk_ops clear_refs_walk_ops = {
1100 .pmd_entry = clear_refs_pte_range,
1101 .test_walk = clear_refs_test_walk,
1102};
1103
Matt Mackallf248dcb2008-02-04 22:29:03 -08001104static ssize_t clear_refs_write(struct file *file, const char __user *buf,
1105 size_t count, loff_t *ppos)
David Rientjesb813e932007-05-06 14:49:24 -07001106{
Matt Mackallf248dcb2008-02-04 22:29:03 -08001107 struct task_struct *task;
Vincent Lifb92a4b2009-09-22 16:45:36 -07001108 char buffer[PROC_NUMBUF];
Matt Mackallf248dcb2008-02-04 22:29:03 -08001109 struct mm_struct *mm;
David Rientjesb813e932007-05-06 14:49:24 -07001110 struct vm_area_struct *vma;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001111 enum clear_refs_types type;
Minchan Kimb3a81d02017-08-10 15:24:15 -07001112 struct mmu_gather tlb;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001113 int itype;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001114 int rv;
David Rientjesb813e932007-05-06 14:49:24 -07001115
Matt Mackallf248dcb2008-02-04 22:29:03 -08001116 memset(buffer, 0, sizeof(buffer));
1117 if (count > sizeof(buffer) - 1)
1118 count = sizeof(buffer) - 1;
1119 if (copy_from_user(buffer, buf, count))
1120 return -EFAULT;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001121 rv = kstrtoint(strstrip(buffer), 10, &itype);
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001122 if (rv < 0)
1123 return rv;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001124 type = (enum clear_refs_types)itype;
1125 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
Matt Mackallf248dcb2008-02-04 22:29:03 -08001126 return -EINVAL;
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001127
Al Viro496ad9a2013-01-23 17:07:38 -05001128 task = get_proc_task(file_inode(file));
Matt Mackallf248dcb2008-02-04 22:29:03 -08001129 if (!task)
1130 return -ESRCH;
1131 mm = get_task_mm(task);
1132 if (mm) {
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001133 struct mmu_notifier_range range;
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001134 struct clear_refs_private cp = {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001135 .type = type,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001136 };
Petr Cermak695f0552015-02-12 15:01:00 -08001137
1138 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
Michal Hocko4e801532016-05-23 16:25:45 -07001139 if (down_write_killable(&mm->mmap_sem)) {
1140 count = -EINTR;
1141 goto out_mm;
1142 }
1143
Petr Cermak695f0552015-02-12 15:01:00 -08001144 /*
1145 * Writing 5 to /proc/pid/clear_refs resets the peak
1146 * resident set size to this mm's current rss value.
1147 */
Petr Cermak695f0552015-02-12 15:01:00 -08001148 reset_mm_hiwater_rss(mm);
1149 up_write(&mm->mmap_sem);
1150 goto out_mm;
1151 }
1152
Konstantin Khlebnikovc4603802019-07-11 21:00:00 -07001153 if (down_read_killable(&mm->mmap_sem)) {
1154 count = -EINTR;
1155 goto out_mm;
1156 }
Minchan Kimb3a81d02017-08-10 15:24:15 -07001157 tlb_gather_mmu(&tlb, mm, 0, -1);
Peter Feiner64e455072014-10-13 15:55:46 -07001158 if (type == CLEAR_REFS_SOFT_DIRTY) {
1159 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1160 if (!(vma->vm_flags & VM_SOFTDIRTY))
1161 continue;
1162 up_read(&mm->mmap_sem);
Michal Hocko4e801532016-05-23 16:25:45 -07001163 if (down_write_killable(&mm->mmap_sem)) {
1164 count = -EINTR;
1165 goto out_mm;
1166 }
Andrea Arcangeli04f58662019-04-18 17:50:52 -07001167 /*
1168 * Avoid to modify vma->vm_flags
1169 * without locked ops while the
1170 * coredump reads the vm_flags.
1171 */
1172 if (!mmget_still_valid(mm)) {
1173 /*
1174 * Silently return "count"
1175 * like if get_task_mm()
1176 * failed. FIXME: should this
1177 * function have returned
1178 * -ESRCH if get_task_mm()
1179 * failed like if
1180 * get_proc_task() fails?
1181 */
1182 up_write(&mm->mmap_sem);
1183 goto out_mm;
1184 }
Peter Feiner64e455072014-10-13 15:55:46 -07001185 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1186 vma->vm_flags &= ~VM_SOFTDIRTY;
1187 vma_set_page_prot(vma);
1188 }
1189 downgrade_write(&mm->mmap_sem);
1190 break;
1191 }
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001192
Jérôme Glisse7269f992019-05-13 17:20:53 -07001193 mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY,
1194 0, NULL, mm, 0, -1UL);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001195 mmu_notifier_invalidate_range_start(&range);
Peter Feiner64e455072014-10-13 15:55:46 -07001196 }
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001197 walk_page_range(mm, 0, mm->highest_vm_end, &clear_refs_walk_ops,
1198 &cp);
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001199 if (type == CLEAR_REFS_SOFT_DIRTY)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001200 mmu_notifier_invalidate_range_end(&range);
Minchan Kimb3a81d02017-08-10 15:24:15 -07001201 tlb_finish_mmu(&tlb, 0, -1);
Matt Mackallf248dcb2008-02-04 22:29:03 -08001202 up_read(&mm->mmap_sem);
Petr Cermak695f0552015-02-12 15:01:00 -08001203out_mm:
Matt Mackallf248dcb2008-02-04 22:29:03 -08001204 mmput(mm);
1205 }
1206 put_task_struct(task);
Vincent Lifb92a4b2009-09-22 16:45:36 -07001207
1208 return count;
David Rientjesb813e932007-05-06 14:49:24 -07001209}
1210
Matt Mackallf248dcb2008-02-04 22:29:03 -08001211const struct file_operations proc_clear_refs_operations = {
1212 .write = clear_refs_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001213 .llseek = noop_llseek,
Matt Mackallf248dcb2008-02-04 22:29:03 -08001214};
1215
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001216typedef struct {
1217 u64 pme;
1218} pagemap_entry_t;
1219
Matt Mackall85863e42008-02-04 22:29:04 -08001220struct pagemapread {
yonghua zheng8c829622013-08-13 16:01:03 -07001221 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001222 pagemap_entry_t *buffer;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001223 bool show_pfn;
Matt Mackall85863e42008-02-04 22:29:04 -08001224};
1225
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001226#define PAGEMAP_WALK_SIZE (PMD_SIZE)
1227#define PAGEMAP_WALK_MASK (PMD_MASK)
1228
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001229#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1230#define PM_PFRAME_BITS 55
1231#define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1232#define PM_SOFT_DIRTY BIT_ULL(55)
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001233#define PM_MMAP_EXCLUSIVE BIT_ULL(56)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001234#define PM_FILE BIT_ULL(61)
1235#define PM_SWAP BIT_ULL(62)
1236#define PM_PRESENT BIT_ULL(63)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001237
Matt Mackall85863e42008-02-04 22:29:04 -08001238#define PM_END_OF_BUFFER 1
1239
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001240static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001241{
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001242 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001243}
1244
1245static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
Matt Mackall85863e42008-02-04 22:29:04 -08001246 struct pagemapread *pm)
1247{
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001248 pm->buffer[pm->pos++] = *pme;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001249 if (pm->pos >= pm->len)
Thomas Tuttleaae86792008-06-05 22:46:31 -07001250 return PM_END_OF_BUFFER;
Matt Mackall85863e42008-02-04 22:29:04 -08001251 return 0;
1252}
1253
1254static int pagemap_pte_hole(unsigned long start, unsigned long end,
Steven Priceb7a16c72020-02-03 17:36:03 -08001255 __always_unused int depth, struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001256{
Dave Hansen21650092008-06-12 15:21:47 -07001257 struct pagemapread *pm = walk->private;
Peter Feiner68b5a652014-08-06 16:08:09 -07001258 unsigned long addr = start;
Matt Mackall85863e42008-02-04 22:29:04 -08001259 int err = 0;
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001260
Peter Feiner68b5a652014-08-06 16:08:09 -07001261 while (addr < end) {
1262 struct vm_area_struct *vma = find_vma(walk->mm, addr);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001263 pagemap_entry_t pme = make_pme(0, 0);
Peter Feiner87e6d492014-09-25 16:05:18 -07001264 /* End of address space hole, which we mark as non-present. */
1265 unsigned long hole_end;
Peter Feiner68b5a652014-08-06 16:08:09 -07001266
Peter Feiner87e6d492014-09-25 16:05:18 -07001267 if (vma)
1268 hole_end = min(end, vma->vm_start);
1269 else
1270 hole_end = end;
1271
1272 for (; addr < hole_end; addr += PAGE_SIZE) {
1273 err = add_to_pagemap(addr, &pme, pm);
1274 if (err)
1275 goto out;
Peter Feiner68b5a652014-08-06 16:08:09 -07001276 }
1277
Peter Feiner87e6d492014-09-25 16:05:18 -07001278 if (!vma)
1279 break;
1280
1281 /* Addresses in the VMA. */
1282 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001283 pme = make_pme(0, PM_SOFT_DIRTY);
Peter Feiner87e6d492014-09-25 16:05:18 -07001284 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
Peter Feiner68b5a652014-08-06 16:08:09 -07001285 err = add_to_pagemap(addr, &pme, pm);
1286 if (err)
1287 goto out;
1288 }
Matt Mackall85863e42008-02-04 22:29:04 -08001289 }
Peter Feiner68b5a652014-08-06 16:08:09 -07001290out:
Matt Mackall85863e42008-02-04 22:29:04 -08001291 return err;
1292}
1293
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001294static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001295 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
Matt Mackall85863e42008-02-04 22:29:04 -08001296{
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001297 u64 frame = 0, flags = 0;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001298 struct page *page = NULL;
Matt Mackall85863e42008-02-04 22:29:04 -08001299
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001300 if (pte_present(pte)) {
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001301 if (pm->show_pfn)
1302 frame = pte_pfn(pte);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001303 flags |= PM_PRESENT;
Christoph Hellwig25b29952019-06-13 22:50:49 +02001304 page = vm_normal_page(vma, addr, pte);
Cyrill Gorcunove9cdd6e2013-10-16 13:46:53 -07001305 if (pte_soft_dirty(pte))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001306 flags |= PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001307 } else if (is_swap_pte(pte)) {
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001308 swp_entry_t entry;
1309 if (pte_swp_soft_dirty(pte))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001310 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001311 entry = pte_to_swp_entry(pte);
Huang Yingab6ecf22018-06-07 17:07:39 -07001312 if (pm->show_pfn)
1313 frame = swp_type(entry) |
1314 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001315 flags |= PM_SWAP;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001316 if (is_migration_entry(entry))
1317 page = migration_entry_to_page(entry);
Jérôme Glisse5042db42017-09-08 16:11:43 -07001318
1319 if (is_device_private_entry(entry))
1320 page = device_private_entry_to_page(entry);
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001321 }
1322
1323 if (page && !PageAnon(page))
1324 flags |= PM_FILE;
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001325 if (page && page_mapcount(page) == 1)
1326 flags |= PM_MMAP_EXCLUSIVE;
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001327 if (vma->vm_flags & VM_SOFTDIRTY)
1328 flags |= PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001329
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001330 return make_pme(frame, flags);
Dave Hansenbcf80392008-06-12 15:21:48 -07001331}
1332
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001333static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -07001334 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001335{
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001336 struct vm_area_struct *vma = walk->vma;
Dave Hansen21650092008-06-12 15:21:47 -07001337 struct pagemapread *pm = walk->private;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001338 spinlock_t *ptl;
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001339 pte_t *pte, *orig_pte;
Matt Mackall85863e42008-02-04 22:29:04 -08001340 int err = 0;
1341
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001342#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001343 ptl = pmd_trans_huge_lock(pmdp, vma);
1344 if (ptl) {
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001345 u64 flags = 0, frame = 0;
1346 pmd_t pmd = *pmdp;
Zi Yan84c3fc42017-09-08 16:11:01 -07001347 struct page *page = NULL;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001348
Huang Yingb83d7e42017-11-02 15:59:34 -07001349 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001350 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001351
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001352 if (pmd_present(pmd)) {
Zi Yan84c3fc42017-09-08 16:11:01 -07001353 page = pmd_page(pmd);
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001354
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001355 flags |= PM_PRESENT;
Huang Yingb83d7e42017-11-02 15:59:34 -07001356 if (pmd_soft_dirty(pmd))
1357 flags |= PM_SOFT_DIRTY;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001358 if (pm->show_pfn)
1359 frame = pmd_pfn(pmd) +
1360 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001361 }
Zi Yan84c3fc42017-09-08 16:11:01 -07001362#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1363 else if (is_swap_pmd(pmd)) {
1364 swp_entry_t entry = pmd_to_swp_entry(pmd);
Huang Yingab6ecf22018-06-07 17:07:39 -07001365 unsigned long offset;
Zi Yan84c3fc42017-09-08 16:11:01 -07001366
Huang Yingab6ecf22018-06-07 17:07:39 -07001367 if (pm->show_pfn) {
1368 offset = swp_offset(entry) +
1369 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1370 frame = swp_type(entry) |
1371 (offset << MAX_SWAPFILES_SHIFT);
1372 }
Zi Yan84c3fc42017-09-08 16:11:01 -07001373 flags |= PM_SWAP;
Huang Yingb83d7e42017-11-02 15:59:34 -07001374 if (pmd_swp_soft_dirty(pmd))
1375 flags |= PM_SOFT_DIRTY;
Zi Yan84c3fc42017-09-08 16:11:01 -07001376 VM_BUG_ON(!is_pmd_migration_entry(pmd));
1377 page = migration_entry_to_page(entry);
1378 }
1379#endif
1380
1381 if (page && page_mapcount(page) == 1)
1382 flags |= PM_MMAP_EXCLUSIVE;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001383
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001384 for (; addr != end; addr += PAGE_SIZE) {
1385 pagemap_entry_t pme = make_pme(frame, flags);
1386
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001387 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001388 if (err)
1389 break;
Huang Yingab6ecf22018-06-07 17:07:39 -07001390 if (pm->show_pfn) {
1391 if (flags & PM_PRESENT)
1392 frame++;
1393 else if (flags & PM_SWAP)
1394 frame += (1 << MAX_SWAPFILES_SHIFT);
1395 }
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001396 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001397 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001398 return err;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001399 }
1400
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001401 if (pmd_trans_unstable(pmdp))
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07001402 return 0;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001403#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Dave Hansenbcf80392008-06-12 15:21:48 -07001404
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001405 /*
1406 * We can assume that @vma always points to a valid one and @end never
1407 * goes beyond vma->vm_end.
1408 */
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001409 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001410 for (; addr < end; pte++, addr += PAGE_SIZE) {
1411 pagemap_entry_t pme;
Peter Feiner81d0fa62014-10-09 15:28:32 -07001412
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001413 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001414 err = add_to_pagemap(addr, &pme, pm);
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001415 if (err)
Peter Feiner81d0fa62014-10-09 15:28:32 -07001416 break;
Matt Mackall85863e42008-02-04 22:29:04 -08001417 }
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001418 pte_unmap_unlock(orig_pte, ptl);
Matt Mackall85863e42008-02-04 22:29:04 -08001419
1420 cond_resched();
1421
1422 return err;
1423}
1424
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001425#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001426/* This function walks within one hugetlb entry in the single call */
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001427static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001428 unsigned long addr, unsigned long end,
1429 struct mm_walk *walk)
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001430{
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001431 struct pagemapread *pm = walk->private;
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001432 struct vm_area_struct *vma = walk->vma;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001433 u64 flags = 0, frame = 0;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001434 int err = 0;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001435 pte_t pte;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001436
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001437 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001438 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001439
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001440 pte = huge_ptep_get(ptep);
1441 if (pte_present(pte)) {
1442 struct page *page = pte_page(pte);
1443
1444 if (!PageAnon(page))
1445 flags |= PM_FILE;
1446
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001447 if (page_mapcount(page) == 1)
1448 flags |= PM_MMAP_EXCLUSIVE;
1449
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001450 flags |= PM_PRESENT;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001451 if (pm->show_pfn)
1452 frame = pte_pfn(pte) +
1453 ((addr & ~hmask) >> PAGE_SHIFT);
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001454 }
1455
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001456 for (; addr != end; addr += PAGE_SIZE) {
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001457 pagemap_entry_t pme = make_pme(frame, flags);
1458
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001459 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001460 if (err)
1461 return err;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001462 if (pm->show_pfn && (flags & PM_PRESENT))
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001463 frame++;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001464 }
1465
1466 cond_resched();
1467
1468 return err;
1469}
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001470#else
1471#define pagemap_hugetlb_range NULL
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001472#endif /* HUGETLB_PAGE */
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001473
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001474static const struct mm_walk_ops pagemap_ops = {
1475 .pmd_entry = pagemap_pmd_range,
1476 .pte_hole = pagemap_pte_hole,
1477 .hugetlb_entry = pagemap_hugetlb_range,
1478};
1479
Matt Mackall85863e42008-02-04 22:29:04 -08001480/*
1481 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1482 *
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001483 * For each page in the address space, this file contains one 64-bit entry
1484 * consisting of the following:
1485 *
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001486 * Bits 0-54 page frame number (PFN) if present
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001487 * Bits 0-4 swap type if swapped
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001488 * Bits 5-54 swap offset if swapped
Mike Rapoport1ad13352018-04-18 11:07:49 +03001489 * Bit 55 pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst)
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001490 * Bit 56 page exclusively mapped
1491 * Bits 57-60 zero
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001492 * Bit 61 page is file-page or shared-anon
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001493 * Bit 62 page swapped
1494 * Bit 63 page present
1495 *
1496 * If the page is not present but in swap, then the PFN contains an
1497 * encoding of the swap file number and the page's offset into the
1498 * swap. Unmapped pages return a null PFN. This allows determining
Matt Mackall85863e42008-02-04 22:29:04 -08001499 * precisely which pages are mapped (or in swap) and comparing mapped
1500 * pages between processes.
1501 *
1502 * Efficient users of this interface will use /proc/pid/maps to
1503 * determine which areas of memory are actually mapped and llseek to
1504 * skip over unmapped regions.
1505 */
1506static ssize_t pagemap_read(struct file *file, char __user *buf,
1507 size_t count, loff_t *ppos)
1508{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001509 struct mm_struct *mm = file->private_data;
Matt Mackall85863e42008-02-04 22:29:04 -08001510 struct pagemapread pm;
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001511 unsigned long src;
1512 unsigned long svpfn;
1513 unsigned long start_vaddr;
1514 unsigned long end_vaddr;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001515 int ret = 0, copied = 0;
Matt Mackall85863e42008-02-04 22:29:04 -08001516
Vegard Nossum388f7932017-02-27 14:30:13 -08001517 if (!mm || !mmget_not_zero(mm))
Matt Mackall85863e42008-02-04 22:29:04 -08001518 goto out;
1519
Matt Mackall85863e42008-02-04 22:29:04 -08001520 ret = -EINVAL;
1521 /* file position must be aligned */
Thomas Tuttleaae86792008-06-05 22:46:31 -07001522 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001523 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001524
1525 ret = 0;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001526 if (!count)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001527 goto out_mm;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001528
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001529 /* do not disclose physical addresses: attack vector */
1530 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1531
yonghua zheng8c829622013-08-13 16:01:03 -07001532 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
Kees Cook6da2ec52018-06-12 13:55:00 -07001533 pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL);
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001534 ret = -ENOMEM;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001535 if (!pm.buffer)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001536 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001537
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001538 src = *ppos;
1539 svpfn = src / PM_ENTRY_BYTES;
1540 start_vaddr = svpfn << PAGE_SHIFT;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001541 end_vaddr = mm->task_size;
Matt Mackall85863e42008-02-04 22:29:04 -08001542
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001543 /* watch out for wraparound */
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001544 if (svpfn > mm->task_size >> PAGE_SHIFT)
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001545 start_vaddr = end_vaddr;
1546
1547 /*
1548 * The odds are that this will stop walking way
1549 * before end_vaddr, because the length of the
1550 * user buffer is tracked in "pm", and the walk
1551 * will stop when we hit the end of the buffer.
1552 */
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001553 ret = 0;
1554 while (count && (start_vaddr < end_vaddr)) {
1555 int len;
1556 unsigned long end;
Matt Mackall85863e42008-02-04 22:29:04 -08001557
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001558 pm.pos = 0;
Naoya Horiguchiea251c12010-11-24 12:57:13 -08001559 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001560 /* overflow ? */
1561 if (end < start_vaddr || end > end_vaddr)
1562 end = end_vaddr;
Konstantin Khlebnikovad80b9322019-07-11 20:59:56 -07001563 ret = down_read_killable(&mm->mmap_sem);
1564 if (ret)
1565 goto out_free;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001566 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm);
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001567 up_read(&mm->mmap_sem);
1568 start_vaddr = end;
1569
1570 len = min(count, PM_ENTRY_BYTES * pm.pos);
Dan Carpenter309361e02010-04-06 13:45:39 +03001571 if (copy_to_user(buf, pm.buffer, len)) {
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001572 ret = -EFAULT;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001573 goto out_free;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001574 }
1575 copied += len;
1576 buf += len;
1577 count -= len;
Matt Mackall85863e42008-02-04 22:29:04 -08001578 }
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001579 *ppos += copied;
1580 if (!ret || ret == PM_END_OF_BUFFER)
1581 ret = copied;
1582
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001583out_free:
1584 kfree(pm.buffer);
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001585out_mm:
1586 mmput(mm);
Matt Mackall85863e42008-02-04 22:29:04 -08001587out:
1588 return ret;
1589}
1590
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001591static int pagemap_open(struct inode *inode, struct file *file)
1592{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001593 struct mm_struct *mm;
1594
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001595 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1596 if (IS_ERR(mm))
1597 return PTR_ERR(mm);
1598 file->private_data = mm;
1599 return 0;
1600}
1601
1602static int pagemap_release(struct inode *inode, struct file *file)
1603{
1604 struct mm_struct *mm = file->private_data;
1605
1606 if (mm)
1607 mmdrop(mm);
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001608 return 0;
1609}
1610
Matt Mackall85863e42008-02-04 22:29:04 -08001611const struct file_operations proc_pagemap_operations = {
1612 .llseek = mem_lseek, /* borrow this */
1613 .read = pagemap_read,
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001614 .open = pagemap_open,
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001615 .release = pagemap_release,
Matt Mackall85863e42008-02-04 22:29:04 -08001616};
Matt Mackall1e883282008-02-04 22:29:07 -08001617#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall85863e42008-02-04 22:29:04 -08001618
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001619#ifdef CONFIG_NUMA
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001620
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001621struct numa_maps {
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001622 unsigned long pages;
1623 unsigned long anon;
1624 unsigned long active;
1625 unsigned long writeback;
1626 unsigned long mapcount_max;
1627 unsigned long dirty;
1628 unsigned long swapcache;
1629 unsigned long node[MAX_NUMNODES];
1630};
1631
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001632struct numa_maps_private {
1633 struct proc_maps_private proc_maps;
1634 struct numa_maps md;
1635};
1636
Dave Hanseneb4866d2011-09-20 15:19:38 -07001637static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1638 unsigned long nr_pages)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001639{
1640 int count = page_mapcount(page);
1641
Dave Hanseneb4866d2011-09-20 15:19:38 -07001642 md->pages += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001643 if (pte_dirty || PageDirty(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001644 md->dirty += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001645
1646 if (PageSwapCache(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001647 md->swapcache += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001648
1649 if (PageActive(page) || PageUnevictable(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001650 md->active += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001651
1652 if (PageWriteback(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001653 md->writeback += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001654
1655 if (PageAnon(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001656 md->anon += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001657
1658 if (count > md->mapcount_max)
1659 md->mapcount_max = count;
1660
Dave Hanseneb4866d2011-09-20 15:19:38 -07001661 md->node[page_to_nid(page)] += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001662}
1663
Dave Hansen3200a8a2011-09-20 15:19:39 -07001664static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1665 unsigned long addr)
1666{
1667 struct page *page;
1668 int nid;
1669
1670 if (!pte_present(pte))
1671 return NULL;
1672
1673 page = vm_normal_page(vma, addr, pte);
1674 if (!page)
1675 return NULL;
1676
1677 if (PageReserved(page))
1678 return NULL;
1679
1680 nid = page_to_nid(page);
Lai Jiangshan4ff1b2c2012-12-12 13:51:25 -08001681 if (!node_isset(nid, node_states[N_MEMORY]))
Dave Hansen3200a8a2011-09-20 15:19:39 -07001682 return NULL;
1683
1684 return page;
1685}
1686
Gerald Schaefer28093f92016-04-28 16:18:35 -07001687#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1688static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1689 struct vm_area_struct *vma,
1690 unsigned long addr)
1691{
1692 struct page *page;
1693 int nid;
1694
1695 if (!pmd_present(pmd))
1696 return NULL;
1697
1698 page = vm_normal_page_pmd(vma, addr, pmd);
1699 if (!page)
1700 return NULL;
1701
1702 if (PageReserved(page))
1703 return NULL;
1704
1705 nid = page_to_nid(page);
1706 if (!node_isset(nid, node_states[N_MEMORY]))
1707 return NULL;
1708
1709 return page;
1710}
1711#endif
1712
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001713static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1714 unsigned long end, struct mm_walk *walk)
1715{
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001716 struct numa_maps *md = walk->private;
1717 struct vm_area_struct *vma = walk->vma;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001718 spinlock_t *ptl;
1719 pte_t *orig_pte;
1720 pte_t *pte;
1721
Gerald Schaefer28093f92016-04-28 16:18:35 -07001722#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001723 ptl = pmd_trans_huge_lock(pmd, vma);
1724 if (ptl) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001725 struct page *page;
1726
Gerald Schaefer28093f92016-04-28 16:18:35 -07001727 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001728 if (page)
Gerald Schaefer28093f92016-04-28 16:18:35 -07001729 gather_stats(page, md, pmd_dirty(*pmd),
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001730 HPAGE_PMD_SIZE/PAGE_SIZE);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001731 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001732 return 0;
Dave Hansen32ef4382011-09-20 15:19:41 -07001733 }
1734
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001735 if (pmd_trans_unstable(pmd))
1736 return 0;
Gerald Schaefer28093f92016-04-28 16:18:35 -07001737#endif
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001738 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1739 do {
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001740 struct page *page = can_gather_numa_stats(*pte, vma, addr);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001741 if (!page)
1742 continue;
Dave Hanseneb4866d2011-09-20 15:19:38 -07001743 gather_stats(page, md, pte_dirty(*pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001744
1745 } while (pte++, addr += PAGE_SIZE, addr != end);
1746 pte_unmap_unlock(orig_pte, ptl);
Hugh Dickinsa66c04102016-12-12 16:44:47 -08001747 cond_resched();
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001748 return 0;
1749}
1750#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001751static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001752 unsigned long addr, unsigned long end, struct mm_walk *walk)
1753{
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001754 pte_t huge_pte = huge_ptep_get(pte);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001755 struct numa_maps *md;
1756 struct page *page;
1757
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001758 if (!pte_present(huge_pte))
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001759 return 0;
1760
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001761 page = pte_page(huge_pte);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001762 if (!page)
1763 return 0;
1764
1765 md = walk->private;
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001766 gather_stats(page, md, pte_dirty(huge_pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001767 return 0;
1768}
1769
1770#else
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001771static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001772 unsigned long addr, unsigned long end, struct mm_walk *walk)
1773{
1774 return 0;
1775}
1776#endif
1777
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001778static const struct mm_walk_ops show_numa_ops = {
1779 .hugetlb_entry = gather_hugetlb_stats,
1780 .pmd_entry = gather_pte_stats,
1781};
1782
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001783/*
1784 * Display pages allocated per node and memory policy via /proc.
1785 */
Vlastimil Babka871305b2018-08-21 21:52:48 -07001786static int show_numa_map(struct seq_file *m, void *v)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001787{
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001788 struct numa_maps_private *numa_priv = m->private;
1789 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001790 struct vm_area_struct *vma = v;
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001791 struct numa_maps *md = &numa_priv->md;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001792 struct file *file = vma->vm_file;
1793 struct mm_struct *mm = vma->vm_mm;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001794 struct mempolicy *pol;
David Rientjes948927e2013-11-12 15:07:28 -08001795 char buffer[64];
1796 int nid;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001797
1798 if (!mm)
1799 return 0;
1800
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001801 /* Ensure we start with an empty set of numa_maps statistics. */
1802 memset(md, 0, sizeof(*md));
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001803
Oleg Nesterov498f2372014-10-09 15:27:52 -07001804 pol = __get_vma_policy(vma, vma->vm_start);
1805 if (pol) {
1806 mpol_to_str(buffer, sizeof(buffer), pol);
1807 mpol_cond_put(pol);
1808 } else {
1809 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1810 }
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001811
1812 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1813
1814 if (file) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001815 seq_puts(m, " file=");
Miklos Szeredi2726d562015-06-19 10:30:28 +02001816 seq_file_path(m, file, "\n\t= ");
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001817 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001818 seq_puts(m, " heap");
Michal Hocko1240ea02017-09-08 16:13:35 -07001819 } else if (is_stack(vma)) {
Johannes Weiner65376df2016-02-02 16:57:29 -08001820 seq_puts(m, " stack");
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001821 }
1822
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001823 if (is_vm_hugetlb_page(vma))
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001824 seq_puts(m, " huge");
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001825
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001826 /* mmap_sem is held by m_start */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001827 walk_page_vma(vma, &show_numa_ops, md);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001828
1829 if (!md->pages)
1830 goto out;
1831
1832 if (md->anon)
1833 seq_printf(m, " anon=%lu", md->anon);
1834
1835 if (md->dirty)
1836 seq_printf(m, " dirty=%lu", md->dirty);
1837
1838 if (md->pages != md->anon && md->pages != md->dirty)
1839 seq_printf(m, " mapped=%lu", md->pages);
1840
1841 if (md->mapcount_max > 1)
1842 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1843
1844 if (md->swapcache)
1845 seq_printf(m, " swapcache=%lu", md->swapcache);
1846
1847 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1848 seq_printf(m, " active=%lu", md->active);
1849
1850 if (md->writeback)
1851 seq_printf(m, " writeback=%lu", md->writeback);
1852
David Rientjes948927e2013-11-12 15:07:28 -08001853 for_each_node_state(nid, N_MEMORY)
1854 if (md->node[nid])
1855 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
Rafael Aquini198d1592015-02-12 15:01:08 -08001856
1857 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001858out:
1859 seq_putc(m, '\n');
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001860 return 0;
1861}
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001862
Jan Engelhardt03a44822008-02-08 04:21:19 -08001863static const struct seq_operations proc_pid_numa_maps_op = {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001864 .start = m_start,
1865 .next = m_next,
1866 .stop = m_stop,
Vlastimil Babka871305b2018-08-21 21:52:48 -07001867 .show = show_numa_map,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001868};
Eric W. Biederman662795d2006-06-26 00:25:48 -07001869
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001870static int pid_numa_maps_open(struct inode *inode, struct file *file)
1871{
Vlastimil Babka871305b2018-08-21 21:52:48 -07001872 return proc_maps_open(inode, file, &proc_pid_numa_maps_op,
1873 sizeof(struct numa_maps_private));
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001874}
1875
1876const struct file_operations proc_pid_numa_maps_operations = {
1877 .open = pid_numa_maps_open,
1878 .read = seq_read,
1879 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -07001880 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001881};
1882
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001883#endif /* CONFIG_NUMA */