blob: 981085b3270800c77528c06f2d567b4aaaae6a2f [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;
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700137 unsigned long last_addr = m->version;
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
173static void *m_next(struct seq_file *m, void *v, loff_t *pos)
174{
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
178 (*pos)++;
Oleg Nesterovad2a00e2014-10-09 15:25:39 -0700179 next = m_next_vma(priv, v);
Matthew Wilcox (Oracle)c2e88d22020-04-06 20:09:11 -0700180 m->version = next ? next->vm_start : -1UL;
181
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700182 return next;
Matt Mackalla6198792008-02-04 22:29:03 -0800183}
184
185static void m_stop(struct seq_file *m, void *v)
186{
187 struct proc_maps_private *priv = m->private;
Matthew Wilcox (Oracle)d07ded62020-04-06 20:09:08 -0700188 struct mm_struct *mm = priv->mm;
Matt Mackalla6198792008-02-04 22:29:03 -0800189
Matthew Wilcox (Oracle)d07ded62020-04-06 20:09:08 -0700190 if (!priv->task)
191 return;
192
193 release_task_mempolicy(priv);
194 up_read(&mm->mmap_sem);
195 mmput(mm);
196 put_task_struct(priv->task);
197 priv->task = NULL;
Matt Mackalla6198792008-02-04 22:29:03 -0800198}
199
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700200static int proc_maps_open(struct inode *inode, struct file *file,
201 const struct seq_operations *ops, int psize)
202{
203 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
204
205 if (!priv)
206 return -ENOMEM;
207
Oleg Nesterov2c033762014-10-09 15:25:51 -0700208 priv->inode = inode;
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700209 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
210 if (IS_ERR(priv->mm)) {
211 int err = PTR_ERR(priv->mm);
212
213 seq_release_private(inode, file);
214 return err;
215 }
216
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700217 return 0;
218}
219
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700220static int proc_map_release(struct inode *inode, struct file *file)
221{
222 struct seq_file *seq = file->private_data;
223 struct proc_maps_private *priv = seq->private;
224
225 if (priv->mm)
226 mmdrop(priv->mm);
227
228 return seq_release_private(inode, file);
229}
230
Matt Mackalla6198792008-02-04 22:29:03 -0800231static int do_maps_open(struct inode *inode, struct file *file,
Jan Engelhardt03a44822008-02-08 04:21:19 -0800232 const struct seq_operations *ops)
Matt Mackalla6198792008-02-04 22:29:03 -0800233{
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700234 return proc_maps_open(inode, file, ops,
235 sizeof(struct proc_maps_private));
Matt Mackalla6198792008-02-04 22:29:03 -0800236}
Mauricio Line070ad42005-09-03 15:55:10 -0700237
Johannes Weiner65376df2016-02-02 16:57:29 -0800238/*
239 * Indicate if the VMA is a stack for the given task; for
240 * /proc/PID/maps that is the stack of the main task.
241 */
Michal Hocko1240ea02017-09-08 16:13:35 -0700242static int is_stack(struct vm_area_struct *vma)
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700243{
Andy Lutomirskib18cb642016-09-30 10:58:57 -0700244 /*
245 * We make no effort to guess what a given thread considers to be
246 * its "stack". It's not even well-defined for programs written
247 * languages like Go.
248 */
249 return vma->vm_start <= vma->vm_mm->start_stack &&
250 vma->vm_end >= vma->vm_mm->start_stack;
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700251}
252
Daniel Colascione493b0e92017-09-06 16:25:08 -0700253static void show_vma_header_prefix(struct seq_file *m,
254 unsigned long start, unsigned long end,
255 vm_flags_t flags, unsigned long long pgoff,
256 dev_t dev, unsigned long ino)
257{
258 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
Andrei Vagin0e3dc012018-04-10 16:30:44 -0700259 seq_put_hex_ll(m, NULL, start, 8);
260 seq_put_hex_ll(m, "-", end, 8);
261 seq_putc(m, ' ');
262 seq_putc(m, flags & VM_READ ? 'r' : '-');
263 seq_putc(m, flags & VM_WRITE ? 'w' : '-');
264 seq_putc(m, flags & VM_EXEC ? 'x' : '-');
265 seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p');
266 seq_put_hex_ll(m, " ", pgoff, 8);
267 seq_put_hex_ll(m, " ", MAJOR(dev), 2);
268 seq_put_hex_ll(m, ":", MINOR(dev), 2);
269 seq_put_decimal_ull(m, " ", ino);
270 seq_putc(m, ' ');
Daniel Colascione493b0e92017-09-06 16:25:08 -0700271}
272
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700273static void
Vlastimil Babka871305b2018-08-21 21:52:48 -0700274show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Mauricio Line070ad42005-09-03 15:55:10 -0700276 struct mm_struct *mm = vma->vm_mm;
277 struct file *file = vma->vm_file;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900278 vm_flags_t flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 unsigned long ino = 0;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700280 unsigned long long pgoff = 0;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200281 unsigned long start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 dev_t dev = 0;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700283 const char *name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (file) {
Al Viro496ad9a2013-01-23 17:07:38 -0500286 struct inode *inode = file_inode(vma->vm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 dev = inode->i_sb->s_dev;
288 ino = inode->i_ino;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700289 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
Linus Torvaldsd7824372010-08-15 11:35:52 -0700292 start = vma->vm_start;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200293 end = vma->vm_end;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700294 show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /*
297 * Print the dentry name for named mappings, and a
298 * special [heap] marker for the heap:
299 */
Mauricio Line070ad42005-09-03 15:55:10 -0700300 if (file) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800301 seq_pad(m, ' ');
Miklos Szeredi2726d562015-06-19 10:30:28 +0200302 seq_file_path(m, file, "\n");
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700303 goto done;
304 }
305
Andy Lutomirski78d683e2014-05-19 15:58:32 -0700306 if (vma->vm_ops && vma->vm_ops->name) {
307 name = vma->vm_ops->name(vma);
308 if (name)
309 goto done;
310 }
311
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700312 name = arch_vma_name(vma);
313 if (!name) {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700314 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
Michal Hocko1240ea02017-09-08 16:13:35 -0700325 if (is_stack(vma))
Johannes Weiner65376df2016-02-02 16:57:29 -0800326 name = "[stack]";
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700327 }
328
329done:
330 if (name) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800331 seq_pad(m, ' ');
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700332 seq_puts(m, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 seq_putc(m, '\n');
Joe Korty7c88db02008-10-16 15:27:09 +0400335}
336
Vlastimil Babka871305b2018-08-21 21:52:48 -0700337static int show_map(struct seq_file *m, void *v)
Joe Korty7c88db02008-10-16 15:27:09 +0400338{
Vlastimil Babka871305b2018-08-21 21:52:48 -0700339 show_map_vma(m, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
341}
342
Jan Engelhardt03a44822008-02-08 04:21:19 -0800343static const struct seq_operations proc_pid_maps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800344 .start = m_start,
345 .next = m_next,
346 .stop = m_stop,
Vlastimil Babka871305b2018-08-21 21:52:48 -0700347 .show = show_map
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700348};
349
350static int pid_maps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800351{
352 return do_maps_open(inode, file, &proc_pid_maps_op);
353}
354
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700355const struct file_operations proc_pid_maps_operations = {
356 .open = pid_maps_open,
357 .read = seq_read,
358 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700359 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700360};
361
Matt Mackalla6198792008-02-04 22:29:03 -0800362/*
363 * Proportional Set Size(PSS): my share of RSS.
364 *
365 * PSS of a process is the count of pages it has in memory, where each
366 * page is divided by the number of processes sharing it. So if a
367 * process has 1000 pages all to itself, and 1000 shared with one other
368 * process, its PSS will be 1500.
369 *
370 * To keep (accumulated) division errors low, we adopt a 64bit
371 * fixed-point pss counter to minimize division errors. So (pss >>
372 * PSS_SHIFT) would be the real byte count.
373 *
374 * A shift of 12 before division means (assuming 4K page size):
375 * - 1M 3-user-pages add up to 8KB errors;
376 * - supports mapcount up to 2^24, or 16M;
377 * - supports PSS up to 2^52 bytes, or 4PB.
378 */
379#define PSS_SHIFT 12
380
Matt Mackall1e883282008-02-04 22:29:07 -0800381#ifdef CONFIG_PROC_PAGE_MONITOR
Peter Zijlstra214e4712008-04-28 02:12:55 -0700382struct mem_size_stats {
Matt Mackalla6198792008-02-04 22:29:03 -0800383 unsigned long resident;
384 unsigned long shared_clean;
385 unsigned long shared_dirty;
386 unsigned long private_clean;
387 unsigned long private_dirty;
388 unsigned long referenced;
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700389 unsigned long anonymous;
Shaohua Licf8496e2017-05-03 14:52:42 -0700390 unsigned long lazyfree;
Dave Hansen4031a212011-03-22 16:33:01 -0700391 unsigned long anonymous_thp;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700392 unsigned long shmem_thp;
Song Liu60fbf0a2019-09-23 15:37:54 -0700393 unsigned long file_thp;
Peter Zijlstra214e4712008-04-28 02:12:55 -0700394 unsigned long swap;
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800395 unsigned long shared_hugetlb;
396 unsigned long private_hugetlb;
Matt Mackalla6198792008-02-04 22:29:03 -0800397 u64 pss;
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700398 u64 pss_anon;
399 u64 pss_file;
400 u64 pss_shmem;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700401 u64 pss_locked;
Minchan Kim8334b962015-09-08 15:00:24 -0700402 u64 swap_pss;
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800403 bool check_shmem_swap;
Matt Mackalla6198792008-02-04 22:29:03 -0800404};
405
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700406static void smaps_page_accumulate(struct mem_size_stats *mss,
407 struct page *page, unsigned long size, unsigned long pss,
408 bool dirty, bool locked, bool private)
409{
410 mss->pss += pss;
411
412 if (PageAnon(page))
413 mss->pss_anon += pss;
414 else if (PageSwapBacked(page))
415 mss->pss_shmem += pss;
416 else
417 mss->pss_file += pss;
418
419 if (locked)
420 mss->pss_locked += pss;
421
422 if (dirty || PageDirty(page)) {
423 if (private)
424 mss->private_dirty += size;
425 else
426 mss->shared_dirty += size;
427 } else {
428 if (private)
429 mss->private_clean += size;
430 else
431 mss->shared_clean += size;
432 }
433}
434
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800435static void smaps_account(struct mem_size_stats *mss, struct page *page,
Sandeep Patil27dd7682019-02-12 15:36:11 -0800436 bool compound, bool young, bool dirty, bool locked)
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800437{
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700438 int i, nr = compound ? compound_nr(page) : 1;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800439 unsigned long size = nr * PAGE_SIZE;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700440
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700441 /*
442 * First accumulate quantities that depend only on |size| and the type
443 * of the compound page.
444 */
Shaohua Licf8496e2017-05-03 14:52:42 -0700445 if (PageAnon(page)) {
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800446 mss->anonymous += size;
Shaohua Licf8496e2017-05-03 14:52:42 -0700447 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
448 mss->lazyfree += size;
449 }
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800450
451 mss->resident += size;
452 /* Accumulate the size in pages that have been accessed. */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700453 if (young || page_is_young(page) || PageReferenced(page))
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800454 mss->referenced += size;
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800455
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800456 /*
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700457 * Then accumulate quantities that may depend on sharing, or that may
458 * differ page-by-page.
459 *
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800460 * page_count(page) == 1 guarantees the page is mapped exactly once.
461 * If any subpage of the compound page mapped with PTE it would elevate
462 * page_count().
463 */
464 if (page_count(page) == 1) {
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700465 smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty,
466 locked, true);
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800467 return;
468 }
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800469 for (i = 0; i < nr; i++, page++) {
470 int mapcount = page_mapcount(page);
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700471 unsigned long pss = PAGE_SIZE << PSS_SHIFT;
472 if (mapcount >= 2)
473 pss /= mapcount;
474 smaps_page_accumulate(mss, page, PAGE_SIZE, pss, dirty, locked,
475 mapcount < 2);
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800476 }
477}
478
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800479#ifdef CONFIG_SHMEM
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800480static int smaps_pte_hole(unsigned long addr, unsigned long end,
Steven Priceb7a16c72020-02-03 17:36:03 -0800481 __always_unused int depth, struct mm_walk *walk)
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800482{
483 struct mem_size_stats *mss = walk->private;
484
Vlastimil Babka48131e02016-01-14 15:19:23 -0800485 mss->swap += shmem_partial_swap_usage(
486 walk->vma->vm_file->f_mapping, addr, end);
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800487
488 return 0;
489}
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200490#else
491#define smaps_pte_hole NULL
492#endif /* CONFIG_SHMEM */
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800493
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800494static void smaps_pte_entry(pte_t *pte, unsigned long addr,
495 struct mm_walk *walk)
Dave Hansenae11c4d2011-03-22 16:32:58 -0700496{
497 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800498 struct vm_area_struct *vma = walk->vma;
Sandeep Patil27dd7682019-02-12 15:36:11 -0800499 bool locked = !!(vma->vm_flags & VM_LOCKED);
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700500 struct page *page = NULL;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700501
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800502 if (pte_present(*pte)) {
503 page = vm_normal_page(vma, addr, *pte);
504 } else if (is_swap_pte(*pte)) {
505 swp_entry_t swpent = pte_to_swp_entry(*pte);
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700506
Minchan Kim8334b962015-09-08 15:00:24 -0700507 if (!non_swap_entry(swpent)) {
508 int mapcount;
509
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800510 mss->swap += PAGE_SIZE;
Minchan Kim8334b962015-09-08 15:00:24 -0700511 mapcount = swp_swapcount(swpent);
512 if (mapcount >= 2) {
513 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
514
515 do_div(pss_delta, mapcount);
516 mss->swap_pss += pss_delta;
517 } else {
518 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
519 }
520 } else if (is_migration_entry(swpent))
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700521 page = migration_entry_to_page(swpent);
Jérôme Glisse5042db42017-09-08 16:11:43 -0700522 else if (is_device_private_entry(swpent))
523 page = device_private_entry_to_page(swpent);
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800524 } else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
525 && pte_none(*pte))) {
Vlastimil Babka48131e02016-01-14 15:19:23 -0800526 page = find_get_entry(vma->vm_file->f_mapping,
527 linear_page_index(vma, addr));
528 if (!page)
529 return;
530
Matthew Wilcox3159f942017-11-03 13:30:42 -0400531 if (xa_is_value(page))
Vlastimil Babka48131e02016-01-14 15:19:23 -0800532 mss->swap += PAGE_SIZE;
533 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300534 put_page(page);
Vlastimil Babka48131e02016-01-14 15:19:23 -0800535
536 return;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700537 }
538
Dave Hansenae11c4d2011-03-22 16:32:58 -0700539 if (!page)
540 return;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800541
Sandeep Patil27dd7682019-02-12 15:36:11 -0800542 smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte), locked);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700543}
544
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800545#ifdef CONFIG_TRANSPARENT_HUGEPAGE
546static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
547 struct mm_walk *walk)
548{
549 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800550 struct vm_area_struct *vma = walk->vma;
Sandeep Patil27dd7682019-02-12 15:36:11 -0800551 bool locked = !!(vma->vm_flags & VM_LOCKED);
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800552 struct page *page;
553
554 /* FOLL_DUMP will return -EFAULT on huge zero page */
555 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
556 if (IS_ERR_OR_NULL(page))
557 return;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700558 if (PageAnon(page))
559 mss->anonymous_thp += HPAGE_PMD_SIZE;
560 else if (PageSwapBacked(page))
561 mss->shmem_thp += HPAGE_PMD_SIZE;
Dan Williamsca120cf2016-09-03 10:38:03 -0700562 else if (is_zone_device_page(page))
563 /* pass */;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700564 else
Song Liu60fbf0a2019-09-23 15:37:54 -0700565 mss->file_thp += HPAGE_PMD_SIZE;
Sandeep Patil27dd7682019-02-12 15:36:11 -0800566 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), locked);
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800567}
568#else
569static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
570 struct mm_walk *walk)
571{
572}
573#endif
574
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800575static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700576 struct mm_walk *walk)
Mauricio Line070ad42005-09-03 15:55:10 -0700577{
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800578 struct vm_area_struct *vma = walk->vma;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700579 pte_t *pte;
Hugh Dickins705e87c2005-10-29 18:16:27 -0700580 spinlock_t *ptl;
Mauricio Line070ad42005-09-03 15:55:10 -0700581
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -0800582 ptl = pmd_trans_huge_lock(pmd, vma);
583 if (ptl) {
Zi Yan84c3fc42017-09-08 16:11:01 -0700584 if (pmd_present(*pmd))
585 smaps_pmd_entry(pmd, addr, walk);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -0800586 spin_unlock(ptl);
David Rientjes14038302017-09-08 16:13:41 -0700587 goto out;
Dave Hansen22e057c2011-03-22 16:33:00 -0700588 }
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700589
590 if (pmd_trans_unstable(pmd))
David Rientjes14038302017-09-08 16:13:41 -0700591 goto out;
Dave Hansen22e057c2011-03-22 16:33:00 -0700592 /*
593 * The mmap_sem held all the way back in m_start() is what
594 * keeps khugepaged out of here and from collapsing things
595 * in here.
596 */
Hugh Dickins705e87c2005-10-29 18:16:27 -0700597 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700598 for (; addr != end; pte++, addr += PAGE_SIZE)
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800599 smaps_pte_entry(pte, addr, walk);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700600 pte_unmap_unlock(pte - 1, ptl);
David Rientjes14038302017-09-08 16:13:41 -0700601out:
Hugh Dickins705e87c2005-10-29 18:16:27 -0700602 cond_resched();
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800603 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700604}
605
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800606static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
607{
608 /*
609 * Don't forget to update Documentation/ on changes.
610 */
611 static const char mnemonics[BITS_PER_LONG][2] = {
612 /*
613 * In case if we meet a flag we don't know about.
614 */
615 [0 ... (BITS_PER_LONG-1)] = "??",
616
617 [ilog2(VM_READ)] = "rd",
618 [ilog2(VM_WRITE)] = "wr",
619 [ilog2(VM_EXEC)] = "ex",
620 [ilog2(VM_SHARED)] = "sh",
621 [ilog2(VM_MAYREAD)] = "mr",
622 [ilog2(VM_MAYWRITE)] = "mw",
623 [ilog2(VM_MAYEXEC)] = "me",
624 [ilog2(VM_MAYSHARE)] = "ms",
625 [ilog2(VM_GROWSDOWN)] = "gd",
626 [ilog2(VM_PFNMAP)] = "pf",
627 [ilog2(VM_DENYWRITE)] = "dw",
Qiaowei Ren4aae7e42014-11-14 07:18:25 -0800628#ifdef CONFIG_X86_INTEL_MPX
629 [ilog2(VM_MPX)] = "mp",
630#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800631 [ilog2(VM_LOCKED)] = "lo",
632 [ilog2(VM_IO)] = "io",
633 [ilog2(VM_SEQ_READ)] = "sr",
634 [ilog2(VM_RAND_READ)] = "rr",
635 [ilog2(VM_DONTCOPY)] = "dc",
636 [ilog2(VM_DONTEXPAND)] = "de",
637 [ilog2(VM_ACCOUNT)] = "ac",
638 [ilog2(VM_NORESERVE)] = "nr",
639 [ilog2(VM_HUGETLB)] = "ht",
Jan Karab6fb2932017-11-01 16:36:41 +0100640 [ilog2(VM_SYNC)] = "sf",
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800641 [ilog2(VM_ARCH_1)] = "ar",
Rik van Rield2cd9ed2017-09-06 16:25:15 -0700642 [ilog2(VM_WIPEONFORK)] = "wf",
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800643 [ilog2(VM_DONTDUMP)] = "dd",
Naoya Horiguchiec8e41a2013-11-12 15:07:49 -0800644#ifdef CONFIG_MEM_SOFT_DIRTY
645 [ilog2(VM_SOFTDIRTY)] = "sd",
646#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800647 [ilog2(VM_MIXEDMAP)] = "mm",
648 [ilog2(VM_HUGEPAGE)] = "hg",
649 [ilog2(VM_NOHUGEPAGE)] = "nh",
650 [ilog2(VM_MERGEABLE)] = "mg",
Andrea Arcangeli16ba6f82015-09-04 15:46:17 -0700651 [ilog2(VM_UFFD_MISSING)]= "um",
652 [ilog2(VM_UFFD_WP)] = "uw",
Ram Pai52122132018-03-27 02:09:26 -0700653#ifdef CONFIG_ARCH_HAS_PKEYS
Dave Hansenc1192f82016-02-12 13:02:27 -0800654 /* These come out via ProtectionKey: */
655 [ilog2(VM_PKEY_BIT0)] = "",
656 [ilog2(VM_PKEY_BIT1)] = "",
657 [ilog2(VM_PKEY_BIT2)] = "",
658 [ilog2(VM_PKEY_BIT3)] = "",
Ram Pai2c9e0a62018-03-27 02:09:27 -0700659#if VM_PKEY_BIT4
660 [ilog2(VM_PKEY_BIT4)] = "",
Dave Hansenc1192f82016-02-12 13:02:27 -0800661#endif
Ram Pai52122132018-03-27 02:09:26 -0700662#endif /* CONFIG_ARCH_HAS_PKEYS */
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800663 };
664 size_t i;
665
666 seq_puts(m, "VmFlags: ");
667 for (i = 0; i < BITS_PER_LONG; i++) {
Dave Hansenc1192f82016-02-12 13:02:27 -0800668 if (!mnemonics[i][0])
669 continue;
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800670 if (vma->vm_flags & (1UL << i)) {
Andrei Vaginf6640662018-04-10 16:31:19 -0700671 seq_putc(m, mnemonics[i][0]);
672 seq_putc(m, mnemonics[i][1]);
673 seq_putc(m, ' ');
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800674 }
675 }
676 seq_putc(m, '\n');
677}
678
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800679#ifdef CONFIG_HUGETLB_PAGE
680static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
681 unsigned long addr, unsigned long end,
682 struct mm_walk *walk)
683{
684 struct mem_size_stats *mss = walk->private;
685 struct vm_area_struct *vma = walk->vma;
686 struct page *page = NULL;
687
688 if (pte_present(*pte)) {
689 page = vm_normal_page(vma, addr, *pte);
690 } else if (is_swap_pte(*pte)) {
691 swp_entry_t swpent = pte_to_swp_entry(*pte);
692
693 if (is_migration_entry(swpent))
694 page = migration_entry_to_page(swpent);
Jérôme Glisse5042db42017-09-08 16:11:43 -0700695 else if (is_device_private_entry(swpent))
696 page = device_private_entry_to_page(swpent);
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800697 }
698 if (page) {
699 int mapcount = page_mapcount(page);
700
701 if (mapcount >= 2)
702 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
703 else
704 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
705 }
706 return 0;
707}
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200708#else
709#define smaps_hugetlb_range NULL
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800710#endif /* HUGETLB_PAGE */
711
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200712static const struct mm_walk_ops smaps_walk_ops = {
713 .pmd_entry = smaps_pte_range,
714 .hugetlb_entry = smaps_hugetlb_range,
715};
716
717static const struct mm_walk_ops smaps_shmem_walk_ops = {
718 .pmd_entry = smaps_pte_range,
719 .hugetlb_entry = smaps_hugetlb_range,
720 .pte_hole = smaps_pte_hole,
721};
722
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700723static void smap_gather_stats(struct vm_area_struct *vma,
724 struct mem_size_stats *mss)
Mauricio Line070ad42005-09-03 15:55:10 -0700725{
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800726#ifdef CONFIG_SHMEM
Vlastimil Babkafa76da42018-10-26 15:02:16 -0700727 /* In case of smaps_rollup, reset the value from previous vma */
728 mss->check_shmem_swap = false;
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800729 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800730 /*
731 * For shared or readonly shmem mappings we know that all
732 * swapped out pages belong to the shmem object, and we can
733 * obtain the swap value much more efficiently. For private
734 * writable mappings, we might have COW pages that are
735 * not affected by the parent swapped out pages of the shmem
736 * object, so we have to distinguish them during the page walk.
737 * Unless we know that the shmem object (or the part mapped by
738 * our VMA) has no swapped out pages at all.
739 */
740 unsigned long shmem_swapped = shmem_swap_usage(vma);
741
742 if (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
743 !(vma->vm_flags & VM_WRITE)) {
Vlastimil Babkafa76da42018-10-26 15:02:16 -0700744 mss->swap += shmem_swapped;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800745 } else {
Daniel Colascione493b0e92017-09-06 16:25:08 -0700746 mss->check_shmem_swap = true;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200747 walk_page_vma(vma, &smaps_shmem_walk_ops, mss);
748 return;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800749 }
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800750 }
751#endif
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +0900752 /* mmap_sem is held in m_start */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200753 walk_page_vma(vma, &smaps_walk_ops, mss);
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700754}
755
756#define SEQ_PUT_DEC(str, val) \
757 seq_put_decimal_ull_width(m, str, (val) >> 10, 8)
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700758
759/* Show the contents common for smaps and smaps_rollup */
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700760static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss,
761 bool rollup_mode)
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700762{
763 SEQ_PUT_DEC("Rss: ", mss->resident);
764 SEQ_PUT_DEC(" kB\nPss: ", mss->pss >> PSS_SHIFT);
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700765 if (rollup_mode) {
766 /*
767 * These are meaningful only for smaps_rollup, otherwise two of
768 * them are zero, and the other one is the same as Pss.
769 */
770 SEQ_PUT_DEC(" kB\nPss_Anon: ",
771 mss->pss_anon >> PSS_SHIFT);
772 SEQ_PUT_DEC(" kB\nPss_File: ",
773 mss->pss_file >> PSS_SHIFT);
774 SEQ_PUT_DEC(" kB\nPss_Shmem: ",
775 mss->pss_shmem >> PSS_SHIFT);
776 }
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700777 SEQ_PUT_DEC(" kB\nShared_Clean: ", mss->shared_clean);
778 SEQ_PUT_DEC(" kB\nShared_Dirty: ", mss->shared_dirty);
779 SEQ_PUT_DEC(" kB\nPrivate_Clean: ", mss->private_clean);
780 SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty);
781 SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced);
782 SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous);
783 SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree);
784 SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp);
785 SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp);
Song Liu60fbf0a2019-09-23 15:37:54 -0700786 SEQ_PUT_DEC(" kB\nFilePmdMapped: ", mss->file_thp);
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700787 SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb);
788 seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ",
789 mss->private_hugetlb >> 10, 7);
790 SEQ_PUT_DEC(" kB\nSwap: ", mss->swap);
791 SEQ_PUT_DEC(" kB\nSwapPss: ",
792 mss->swap_pss >> PSS_SHIFT);
793 SEQ_PUT_DEC(" kB\nLocked: ",
794 mss->pss_locked >> PSS_SHIFT);
795 seq_puts(m, " kB\n");
796}
797
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700798static int show_smap(struct seq_file *m, void *v)
799{
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700800 struct vm_area_struct *vma = v;
Vlastimil Babka258f6692018-08-21 21:52:59 -0700801 struct mem_size_stats mss;
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700802
Vlastimil Babka258f6692018-08-21 21:52:59 -0700803 memset(&mss, 0, sizeof(mss));
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700804
Vlastimil Babka258f6692018-08-21 21:52:59 -0700805 smap_gather_stats(vma, &mss);
Matt Mackall4752c362008-02-04 22:29:02 -0800806
Vlastimil Babka258f6692018-08-21 21:52:59 -0700807 show_map_vma(m, vma);
Matt Mackall4752c362008-02-04 22:29:02 -0800808
Vlastimil Babka258f6692018-08-21 21:52:59 -0700809 SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start);
810 SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma));
811 SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma));
812 seq_puts(m, " kB\n");
Matt Mackall4752c362008-02-04 22:29:02 -0800813
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700814 __show_smap(m, &mss, false);
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700815
Yang Shic0630662019-07-18 15:57:27 -0700816 seq_printf(m, "THPeligible: %d\n",
817 transparent_hugepage_enabled(vma));
Michal Hocko7635d9c2018-12-28 00:38:21 -0800818
Vlastimil Babka258f6692018-08-21 21:52:59 -0700819 if (arch_pkeys_enabled())
820 seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma));
821 show_smap_vma_flags(m, vma);
822
Vlastimil Babka258f6692018-08-21 21:52:59 -0700823 return 0;
824}
825
826static int show_smaps_rollup(struct seq_file *m, void *v)
827{
828 struct proc_maps_private *priv = m->private;
829 struct mem_size_stats mss;
830 struct mm_struct *mm;
831 struct vm_area_struct *vma;
832 unsigned long last_vma_end = 0;
833 int ret = 0;
834
835 priv->task = get_proc_task(priv->inode);
836 if (!priv->task)
837 return -ESRCH;
838
839 mm = priv->mm;
840 if (!mm || !mmget_not_zero(mm)) {
841 ret = -ESRCH;
842 goto out_put_task;
843 }
844
845 memset(&mss, 0, sizeof(mss));
846
Konstantin Khlebnikova26a9782019-07-11 20:59:53 -0700847 ret = down_read_killable(&mm->mmap_sem);
848 if (ret)
849 goto out_put_mm;
850
Vlastimil Babka258f6692018-08-21 21:52:59 -0700851 hold_task_mempolicy(priv);
852
853 for (vma = priv->mm->mmap; vma; vma = vma->vm_next) {
854 smap_gather_stats(vma, &mss);
855 last_vma_end = vma->vm_end;
856 }
857
858 show_vma_header_prefix(m, priv->mm->mmap->vm_start,
859 last_vma_end, 0, 0, 0, 0);
860 seq_pad(m, ' ');
861 seq_puts(m, "[rollup]\n");
862
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700863 __show_smap(m, &mss, true);
Vlastimil Babka258f6692018-08-21 21:52:59 -0700864
865 release_task_mempolicy(priv);
866 up_read(&mm->mmap_sem);
Vlastimil Babka258f6692018-08-21 21:52:59 -0700867
Konstantin Khlebnikova26a9782019-07-11 20:59:53 -0700868out_put_mm:
869 mmput(mm);
Vlastimil Babka258f6692018-08-21 21:52:59 -0700870out_put_task:
871 put_task_struct(priv->task);
872 priv->task = NULL;
873
Daniel Colascione493b0e92017-09-06 16:25:08 -0700874 return ret;
Mauricio Line070ad42005-09-03 15:55:10 -0700875}
Andrei Vagind1be35c2018-04-10 16:31:16 -0700876#undef SEQ_PUT_DEC
Mauricio Line070ad42005-09-03 15:55:10 -0700877
Jan Engelhardt03a44822008-02-08 04:21:19 -0800878static const struct seq_operations proc_pid_smaps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800879 .start = m_start,
880 .next = m_next,
881 .stop = m_stop,
Vlastimil Babka871305b2018-08-21 21:52:48 -0700882 .show = show_smap
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700883};
884
885static int pid_smaps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800886{
887 return do_maps_open(inode, file, &proc_pid_smaps_op);
888}
889
Vlastimil Babka258f6692018-08-21 21:52:59 -0700890static int smaps_rollup_open(struct inode *inode, struct file *file)
Daniel Colascione493b0e92017-09-06 16:25:08 -0700891{
Vlastimil Babka258f6692018-08-21 21:52:59 -0700892 int ret;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700893 struct proc_maps_private *priv;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700894
Vlastimil Babka258f6692018-08-21 21:52:59 -0700895 priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT);
896 if (!priv)
Daniel Colascione493b0e92017-09-06 16:25:08 -0700897 return -ENOMEM;
Vlastimil Babka258f6692018-08-21 21:52:59 -0700898
899 ret = single_open(file, show_smaps_rollup, priv);
900 if (ret)
901 goto out_free;
902
903 priv->inode = inode;
904 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
905 if (IS_ERR(priv->mm)) {
906 ret = PTR_ERR(priv->mm);
907
908 single_release(inode, file);
909 goto out_free;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700910 }
Vlastimil Babka258f6692018-08-21 21:52:59 -0700911
Daniel Colascione493b0e92017-09-06 16:25:08 -0700912 return 0;
Vlastimil Babka258f6692018-08-21 21:52:59 -0700913
914out_free:
915 kfree(priv);
916 return ret;
917}
918
919static int smaps_rollup_release(struct inode *inode, struct file *file)
920{
921 struct seq_file *seq = file->private_data;
922 struct proc_maps_private *priv = seq->private;
923
924 if (priv->mm)
925 mmdrop(priv->mm);
926
927 kfree(priv);
928 return single_release(inode, file);
Daniel Colascione493b0e92017-09-06 16:25:08 -0700929}
930
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700931const struct file_operations proc_pid_smaps_operations = {
932 .open = pid_smaps_open,
933 .read = seq_read,
934 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700935 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700936};
937
Daniel Colascione493b0e92017-09-06 16:25:08 -0700938const struct file_operations proc_pid_smaps_rollup_operations = {
Vlastimil Babka258f6692018-08-21 21:52:59 -0700939 .open = smaps_rollup_open,
Daniel Colascione493b0e92017-09-06 16:25:08 -0700940 .read = seq_read,
941 .llseek = seq_lseek,
Vlastimil Babka258f6692018-08-21 21:52:59 -0700942 .release = smaps_rollup_release,
Daniel Colascione493b0e92017-09-06 16:25:08 -0700943};
944
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700945enum clear_refs_types {
946 CLEAR_REFS_ALL = 1,
947 CLEAR_REFS_ANON,
948 CLEAR_REFS_MAPPED,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700949 CLEAR_REFS_SOFT_DIRTY,
Petr Cermak695f0552015-02-12 15:01:00 -0800950 CLEAR_REFS_MM_HIWATER_RSS,
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700951 CLEAR_REFS_LAST,
952};
953
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700954struct clear_refs_private {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700955 enum clear_refs_types type;
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700956};
957
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800958#ifdef CONFIG_MEM_SOFT_DIRTY
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700959static inline void clear_soft_dirty(struct vm_area_struct *vma,
960 unsigned long addr, pte_t *pte)
961{
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700962 /*
963 * The soft-dirty tracker uses #PF-s to catch writes
964 * to pages, so write-protect the pte as well. See the
Mike Rapoport1ad13352018-04-18 11:07:49 +0300965 * Documentation/admin-guide/mm/soft-dirty.rst for full description
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700966 * of how soft-dirty works.
967 */
968 pte_t ptent = *pte;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700969
970 if (pte_present(ptent)) {
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -0800971 pte_t old_pte;
972
973 old_pte = ptep_modify_prot_start(vma, addr, pte);
974 ptent = pte_wrprotect(old_pte);
Martin Schwidefskya7b76172015-04-22 14:20:47 +0200975 ptent = pte_clear_soft_dirty(ptent);
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -0800976 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700977 } else if (is_swap_pte(ptent)) {
978 ptent = pte_swp_clear_soft_dirty(ptent);
Laurent Dufour326c2592015-11-05 18:49:21 -0800979 set_pte_at(vma->vm_mm, addr, pte, ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700980 }
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700981}
Laurent Dufour5d3875a2015-11-05 18:49:24 -0800982#else
983static inline void clear_soft_dirty(struct vm_area_struct *vma,
984 unsigned long addr, pte_t *pte)
985{
986}
987#endif
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700988
Laurent Dufour5d3875a2015-11-05 18:49:24 -0800989#if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800990static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
991 unsigned long addr, pmd_t *pmdp)
992{
Kirill A. Shutemova3cf9882018-01-31 16:18:20 -0800993 pmd_t old, pmd = *pmdp;
Kirill A. Shutemov5b7abea2017-04-13 14:56:28 -0700994
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -0700995 if (pmd_present(pmd)) {
996 /* See comment in change_huge_pmd() */
Kirill A. Shutemova3cf9882018-01-31 16:18:20 -0800997 old = pmdp_invalidate(vma, addr, pmdp);
998 if (pmd_dirty(old))
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -0700999 pmd = pmd_mkdirty(pmd);
Kirill A. Shutemova3cf9882018-01-31 16:18:20 -08001000 if (pmd_young(old))
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001001 pmd = pmd_mkyoung(pmd);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001002
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001003 pmd = pmd_wrprotect(pmd);
1004 pmd = pmd_clear_soft_dirty(pmd);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001005
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001006 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1007 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) {
1008 pmd = pmd_swp_clear_soft_dirty(pmd);
1009 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1010 }
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001011}
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001012#else
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001013static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1014 unsigned long addr, pmd_t *pmdp)
1015{
1016}
1017#endif
1018
Matt Mackalla6198792008-02-04 22:29:03 -08001019static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
Dave Hansen21650092008-06-12 15:21:47 -07001020 unsigned long end, struct mm_walk *walk)
Matt Mackalla6198792008-02-04 22:29:03 -08001021{
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001022 struct clear_refs_private *cp = walk->private;
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001023 struct vm_area_struct *vma = walk->vma;
Matt Mackalla6198792008-02-04 22:29:03 -08001024 pte_t *pte, ptent;
1025 spinlock_t *ptl;
1026 struct page *page;
1027
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001028 ptl = pmd_trans_huge_lock(pmd, vma);
1029 if (ptl) {
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001030 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1031 clear_soft_dirty_pmd(vma, addr, pmd);
1032 goto out;
1033 }
1034
Zi Yan84c3fc42017-09-08 16:11:01 -07001035 if (!pmd_present(*pmd))
1036 goto out;
1037
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001038 page = pmd_page(*pmd);
1039
1040 /* Clear accessed and referenced bits. */
1041 pmdp_test_and_clear_young(vma, addr, pmd);
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07001042 test_and_clear_page_young(page);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001043 ClearPageReferenced(page);
1044out:
1045 spin_unlock(ptl);
1046 return 0;
1047 }
1048
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001049 if (pmd_trans_unstable(pmd))
1050 return 0;
Dave Hansen03319322011-03-22 16:32:56 -07001051
Matt Mackalla6198792008-02-04 22:29:03 -08001052 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1053 for (; addr != end; pte++, addr += PAGE_SIZE) {
1054 ptent = *pte;
Matt Mackalla6198792008-02-04 22:29:03 -08001055
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001056 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1057 clear_soft_dirty(vma, addr, pte);
1058 continue;
1059 }
1060
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001061 if (!pte_present(ptent))
1062 continue;
1063
Matt Mackalla6198792008-02-04 22:29:03 -08001064 page = vm_normal_page(vma, addr, ptent);
1065 if (!page)
1066 continue;
1067
1068 /* Clear accessed and referenced bits. */
1069 ptep_test_and_clear_young(vma, addr, pte);
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07001070 test_and_clear_page_young(page);
Matt Mackalla6198792008-02-04 22:29:03 -08001071 ClearPageReferenced(page);
1072 }
1073 pte_unmap_unlock(pte - 1, ptl);
1074 cond_resched();
1075 return 0;
1076}
1077
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001078static int clear_refs_test_walk(unsigned long start, unsigned long end,
1079 struct mm_walk *walk)
1080{
1081 struct clear_refs_private *cp = walk->private;
1082 struct vm_area_struct *vma = walk->vma;
1083
Naoya Horiguchi48684a62015-02-11 15:28:06 -08001084 if (vma->vm_flags & VM_PFNMAP)
1085 return 1;
1086
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001087 /*
1088 * Writing 1 to /proc/pid/clear_refs affects all pages.
1089 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
1090 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
1091 * Writing 4 to /proc/pid/clear_refs affects all pages.
1092 */
1093 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
1094 return 1;
1095 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1096 return 1;
1097 return 0;
1098}
1099
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001100static const struct mm_walk_ops clear_refs_walk_ops = {
1101 .pmd_entry = clear_refs_pte_range,
1102 .test_walk = clear_refs_test_walk,
1103};
1104
Matt Mackallf248dcb2008-02-04 22:29:03 -08001105static ssize_t clear_refs_write(struct file *file, const char __user *buf,
1106 size_t count, loff_t *ppos)
David Rientjesb813e932007-05-06 14:49:24 -07001107{
Matt Mackallf248dcb2008-02-04 22:29:03 -08001108 struct task_struct *task;
Vincent Lifb92a4b2009-09-22 16:45:36 -07001109 char buffer[PROC_NUMBUF];
Matt Mackallf248dcb2008-02-04 22:29:03 -08001110 struct mm_struct *mm;
David Rientjesb813e932007-05-06 14:49:24 -07001111 struct vm_area_struct *vma;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001112 enum clear_refs_types type;
Minchan Kimb3a81d02017-08-10 15:24:15 -07001113 struct mmu_gather tlb;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001114 int itype;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001115 int rv;
David Rientjesb813e932007-05-06 14:49:24 -07001116
Matt Mackallf248dcb2008-02-04 22:29:03 -08001117 memset(buffer, 0, sizeof(buffer));
1118 if (count > sizeof(buffer) - 1)
1119 count = sizeof(buffer) - 1;
1120 if (copy_from_user(buffer, buf, count))
1121 return -EFAULT;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001122 rv = kstrtoint(strstrip(buffer), 10, &itype);
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001123 if (rv < 0)
1124 return rv;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001125 type = (enum clear_refs_types)itype;
1126 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
Matt Mackallf248dcb2008-02-04 22:29:03 -08001127 return -EINVAL;
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001128
Al Viro496ad9a2013-01-23 17:07:38 -05001129 task = get_proc_task(file_inode(file));
Matt Mackallf248dcb2008-02-04 22:29:03 -08001130 if (!task)
1131 return -ESRCH;
1132 mm = get_task_mm(task);
1133 if (mm) {
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001134 struct mmu_notifier_range range;
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001135 struct clear_refs_private cp = {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001136 .type = type,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001137 };
Petr Cermak695f0552015-02-12 15:01:00 -08001138
1139 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
Michal Hocko4e801532016-05-23 16:25:45 -07001140 if (down_write_killable(&mm->mmap_sem)) {
1141 count = -EINTR;
1142 goto out_mm;
1143 }
1144
Petr Cermak695f0552015-02-12 15:01:00 -08001145 /*
1146 * Writing 5 to /proc/pid/clear_refs resets the peak
1147 * resident set size to this mm's current rss value.
1148 */
Petr Cermak695f0552015-02-12 15:01:00 -08001149 reset_mm_hiwater_rss(mm);
1150 up_write(&mm->mmap_sem);
1151 goto out_mm;
1152 }
1153
Konstantin Khlebnikovc4603802019-07-11 21:00:00 -07001154 if (down_read_killable(&mm->mmap_sem)) {
1155 count = -EINTR;
1156 goto out_mm;
1157 }
Minchan Kimb3a81d02017-08-10 15:24:15 -07001158 tlb_gather_mmu(&tlb, mm, 0, -1);
Peter Feiner64e455072014-10-13 15:55:46 -07001159 if (type == CLEAR_REFS_SOFT_DIRTY) {
1160 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1161 if (!(vma->vm_flags & VM_SOFTDIRTY))
1162 continue;
1163 up_read(&mm->mmap_sem);
Michal Hocko4e801532016-05-23 16:25:45 -07001164 if (down_write_killable(&mm->mmap_sem)) {
1165 count = -EINTR;
1166 goto out_mm;
1167 }
Andrea Arcangeli04f58662019-04-18 17:50:52 -07001168 /*
1169 * Avoid to modify vma->vm_flags
1170 * without locked ops while the
1171 * coredump reads the vm_flags.
1172 */
1173 if (!mmget_still_valid(mm)) {
1174 /*
1175 * Silently return "count"
1176 * like if get_task_mm()
1177 * failed. FIXME: should this
1178 * function have returned
1179 * -ESRCH if get_task_mm()
1180 * failed like if
1181 * get_proc_task() fails?
1182 */
1183 up_write(&mm->mmap_sem);
1184 goto out_mm;
1185 }
Peter Feiner64e455072014-10-13 15:55:46 -07001186 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1187 vma->vm_flags &= ~VM_SOFTDIRTY;
1188 vma_set_page_prot(vma);
1189 }
1190 downgrade_write(&mm->mmap_sem);
1191 break;
1192 }
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001193
Jérôme Glisse7269f992019-05-13 17:20:53 -07001194 mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY,
1195 0, NULL, mm, 0, -1UL);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001196 mmu_notifier_invalidate_range_start(&range);
Peter Feiner64e455072014-10-13 15:55:46 -07001197 }
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001198 walk_page_range(mm, 0, mm->highest_vm_end, &clear_refs_walk_ops,
1199 &cp);
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001200 if (type == CLEAR_REFS_SOFT_DIRTY)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001201 mmu_notifier_invalidate_range_end(&range);
Minchan Kimb3a81d02017-08-10 15:24:15 -07001202 tlb_finish_mmu(&tlb, 0, -1);
Matt Mackallf248dcb2008-02-04 22:29:03 -08001203 up_read(&mm->mmap_sem);
Petr Cermak695f0552015-02-12 15:01:00 -08001204out_mm:
Matt Mackallf248dcb2008-02-04 22:29:03 -08001205 mmput(mm);
1206 }
1207 put_task_struct(task);
Vincent Lifb92a4b2009-09-22 16:45:36 -07001208
1209 return count;
David Rientjesb813e932007-05-06 14:49:24 -07001210}
1211
Matt Mackallf248dcb2008-02-04 22:29:03 -08001212const struct file_operations proc_clear_refs_operations = {
1213 .write = clear_refs_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001214 .llseek = noop_llseek,
Matt Mackallf248dcb2008-02-04 22:29:03 -08001215};
1216
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001217typedef struct {
1218 u64 pme;
1219} pagemap_entry_t;
1220
Matt Mackall85863e42008-02-04 22:29:04 -08001221struct pagemapread {
yonghua zheng8c829622013-08-13 16:01:03 -07001222 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001223 pagemap_entry_t *buffer;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001224 bool show_pfn;
Matt Mackall85863e42008-02-04 22:29:04 -08001225};
1226
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001227#define PAGEMAP_WALK_SIZE (PMD_SIZE)
1228#define PAGEMAP_WALK_MASK (PMD_MASK)
1229
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001230#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1231#define PM_PFRAME_BITS 55
1232#define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1233#define PM_SOFT_DIRTY BIT_ULL(55)
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001234#define PM_MMAP_EXCLUSIVE BIT_ULL(56)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001235#define PM_FILE BIT_ULL(61)
1236#define PM_SWAP BIT_ULL(62)
1237#define PM_PRESENT BIT_ULL(63)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001238
Matt Mackall85863e42008-02-04 22:29:04 -08001239#define PM_END_OF_BUFFER 1
1240
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001241static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001242{
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001243 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001244}
1245
1246static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
Matt Mackall85863e42008-02-04 22:29:04 -08001247 struct pagemapread *pm)
1248{
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001249 pm->buffer[pm->pos++] = *pme;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001250 if (pm->pos >= pm->len)
Thomas Tuttleaae86792008-06-05 22:46:31 -07001251 return PM_END_OF_BUFFER;
Matt Mackall85863e42008-02-04 22:29:04 -08001252 return 0;
1253}
1254
1255static int pagemap_pte_hole(unsigned long start, unsigned long end,
Steven Priceb7a16c72020-02-03 17:36:03 -08001256 __always_unused int depth, struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001257{
Dave Hansen21650092008-06-12 15:21:47 -07001258 struct pagemapread *pm = walk->private;
Peter Feiner68b5a652014-08-06 16:08:09 -07001259 unsigned long addr = start;
Matt Mackall85863e42008-02-04 22:29:04 -08001260 int err = 0;
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001261
Peter Feiner68b5a652014-08-06 16:08:09 -07001262 while (addr < end) {
1263 struct vm_area_struct *vma = find_vma(walk->mm, addr);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001264 pagemap_entry_t pme = make_pme(0, 0);
Peter Feiner87e6d492014-09-25 16:05:18 -07001265 /* End of address space hole, which we mark as non-present. */
1266 unsigned long hole_end;
Peter Feiner68b5a652014-08-06 16:08:09 -07001267
Peter Feiner87e6d492014-09-25 16:05:18 -07001268 if (vma)
1269 hole_end = min(end, vma->vm_start);
1270 else
1271 hole_end = end;
1272
1273 for (; addr < hole_end; addr += PAGE_SIZE) {
1274 err = add_to_pagemap(addr, &pme, pm);
1275 if (err)
1276 goto out;
Peter Feiner68b5a652014-08-06 16:08:09 -07001277 }
1278
Peter Feiner87e6d492014-09-25 16:05:18 -07001279 if (!vma)
1280 break;
1281
1282 /* Addresses in the VMA. */
1283 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001284 pme = make_pme(0, PM_SOFT_DIRTY);
Peter Feiner87e6d492014-09-25 16:05:18 -07001285 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
Peter Feiner68b5a652014-08-06 16:08:09 -07001286 err = add_to_pagemap(addr, &pme, pm);
1287 if (err)
1288 goto out;
1289 }
Matt Mackall85863e42008-02-04 22:29:04 -08001290 }
Peter Feiner68b5a652014-08-06 16:08:09 -07001291out:
Matt Mackall85863e42008-02-04 22:29:04 -08001292 return err;
1293}
1294
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001295static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001296 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
Matt Mackall85863e42008-02-04 22:29:04 -08001297{
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001298 u64 frame = 0, flags = 0;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001299 struct page *page = NULL;
Matt Mackall85863e42008-02-04 22:29:04 -08001300
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001301 if (pte_present(pte)) {
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001302 if (pm->show_pfn)
1303 frame = pte_pfn(pte);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001304 flags |= PM_PRESENT;
Christoph Hellwig25b29952019-06-13 22:50:49 +02001305 page = vm_normal_page(vma, addr, pte);
Cyrill Gorcunove9cdd6e2013-10-16 13:46:53 -07001306 if (pte_soft_dirty(pte))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001307 flags |= PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001308 } else if (is_swap_pte(pte)) {
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001309 swp_entry_t entry;
1310 if (pte_swp_soft_dirty(pte))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001311 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001312 entry = pte_to_swp_entry(pte);
Huang Yingab6ecf22018-06-07 17:07:39 -07001313 if (pm->show_pfn)
1314 frame = swp_type(entry) |
1315 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001316 flags |= PM_SWAP;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001317 if (is_migration_entry(entry))
1318 page = migration_entry_to_page(entry);
Jérôme Glisse5042db42017-09-08 16:11:43 -07001319
1320 if (is_device_private_entry(entry))
1321 page = device_private_entry_to_page(entry);
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001322 }
1323
1324 if (page && !PageAnon(page))
1325 flags |= PM_FILE;
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001326 if (page && page_mapcount(page) == 1)
1327 flags |= PM_MMAP_EXCLUSIVE;
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001328 if (vma->vm_flags & VM_SOFTDIRTY)
1329 flags |= PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001330
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001331 return make_pme(frame, flags);
Dave Hansenbcf80392008-06-12 15:21:48 -07001332}
1333
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001334static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -07001335 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001336{
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001337 struct vm_area_struct *vma = walk->vma;
Dave Hansen21650092008-06-12 15:21:47 -07001338 struct pagemapread *pm = walk->private;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001339 spinlock_t *ptl;
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001340 pte_t *pte, *orig_pte;
Matt Mackall85863e42008-02-04 22:29:04 -08001341 int err = 0;
1342
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001343#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001344 ptl = pmd_trans_huge_lock(pmdp, vma);
1345 if (ptl) {
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001346 u64 flags = 0, frame = 0;
1347 pmd_t pmd = *pmdp;
Zi Yan84c3fc42017-09-08 16:11:01 -07001348 struct page *page = NULL;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001349
Huang Yingb83d7e42017-11-02 15:59:34 -07001350 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001351 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001352
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001353 if (pmd_present(pmd)) {
Zi Yan84c3fc42017-09-08 16:11:01 -07001354 page = pmd_page(pmd);
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001355
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001356 flags |= PM_PRESENT;
Huang Yingb83d7e42017-11-02 15:59:34 -07001357 if (pmd_soft_dirty(pmd))
1358 flags |= PM_SOFT_DIRTY;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001359 if (pm->show_pfn)
1360 frame = pmd_pfn(pmd) +
1361 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001362 }
Zi Yan84c3fc42017-09-08 16:11:01 -07001363#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1364 else if (is_swap_pmd(pmd)) {
1365 swp_entry_t entry = pmd_to_swp_entry(pmd);
Huang Yingab6ecf22018-06-07 17:07:39 -07001366 unsigned long offset;
Zi Yan84c3fc42017-09-08 16:11:01 -07001367
Huang Yingab6ecf22018-06-07 17:07:39 -07001368 if (pm->show_pfn) {
1369 offset = swp_offset(entry) +
1370 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1371 frame = swp_type(entry) |
1372 (offset << MAX_SWAPFILES_SHIFT);
1373 }
Zi Yan84c3fc42017-09-08 16:11:01 -07001374 flags |= PM_SWAP;
Huang Yingb83d7e42017-11-02 15:59:34 -07001375 if (pmd_swp_soft_dirty(pmd))
1376 flags |= PM_SOFT_DIRTY;
Zi Yan84c3fc42017-09-08 16:11:01 -07001377 VM_BUG_ON(!is_pmd_migration_entry(pmd));
1378 page = migration_entry_to_page(entry);
1379 }
1380#endif
1381
1382 if (page && page_mapcount(page) == 1)
1383 flags |= PM_MMAP_EXCLUSIVE;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001384
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001385 for (; addr != end; addr += PAGE_SIZE) {
1386 pagemap_entry_t pme = make_pme(frame, flags);
1387
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001388 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001389 if (err)
1390 break;
Huang Yingab6ecf22018-06-07 17:07:39 -07001391 if (pm->show_pfn) {
1392 if (flags & PM_PRESENT)
1393 frame++;
1394 else if (flags & PM_SWAP)
1395 frame += (1 << MAX_SWAPFILES_SHIFT);
1396 }
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001397 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001398 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001399 return err;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001400 }
1401
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001402 if (pmd_trans_unstable(pmdp))
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07001403 return 0;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001404#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Dave Hansenbcf80392008-06-12 15:21:48 -07001405
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001406 /*
1407 * We can assume that @vma always points to a valid one and @end never
1408 * goes beyond vma->vm_end.
1409 */
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001410 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001411 for (; addr < end; pte++, addr += PAGE_SIZE) {
1412 pagemap_entry_t pme;
Peter Feiner81d0fa62014-10-09 15:28:32 -07001413
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001414 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001415 err = add_to_pagemap(addr, &pme, pm);
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001416 if (err)
Peter Feiner81d0fa62014-10-09 15:28:32 -07001417 break;
Matt Mackall85863e42008-02-04 22:29:04 -08001418 }
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001419 pte_unmap_unlock(orig_pte, ptl);
Matt Mackall85863e42008-02-04 22:29:04 -08001420
1421 cond_resched();
1422
1423 return err;
1424}
1425
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001426#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001427/* This function walks within one hugetlb entry in the single call */
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001428static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001429 unsigned long addr, unsigned long end,
1430 struct mm_walk *walk)
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001431{
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001432 struct pagemapread *pm = walk->private;
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001433 struct vm_area_struct *vma = walk->vma;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001434 u64 flags = 0, frame = 0;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001435 int err = 0;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001436 pte_t pte;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001437
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001438 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001439 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001440
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001441 pte = huge_ptep_get(ptep);
1442 if (pte_present(pte)) {
1443 struct page *page = pte_page(pte);
1444
1445 if (!PageAnon(page))
1446 flags |= PM_FILE;
1447
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001448 if (page_mapcount(page) == 1)
1449 flags |= PM_MMAP_EXCLUSIVE;
1450
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001451 flags |= PM_PRESENT;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001452 if (pm->show_pfn)
1453 frame = pte_pfn(pte) +
1454 ((addr & ~hmask) >> PAGE_SHIFT);
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001455 }
1456
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001457 for (; addr != end; addr += PAGE_SIZE) {
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001458 pagemap_entry_t pme = make_pme(frame, flags);
1459
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001460 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001461 if (err)
1462 return err;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001463 if (pm->show_pfn && (flags & PM_PRESENT))
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001464 frame++;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001465 }
1466
1467 cond_resched();
1468
1469 return err;
1470}
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001471#else
1472#define pagemap_hugetlb_range NULL
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001473#endif /* HUGETLB_PAGE */
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001474
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001475static const struct mm_walk_ops pagemap_ops = {
1476 .pmd_entry = pagemap_pmd_range,
1477 .pte_hole = pagemap_pte_hole,
1478 .hugetlb_entry = pagemap_hugetlb_range,
1479};
1480
Matt Mackall85863e42008-02-04 22:29:04 -08001481/*
1482 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1483 *
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001484 * For each page in the address space, this file contains one 64-bit entry
1485 * consisting of the following:
1486 *
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001487 * Bits 0-54 page frame number (PFN) if present
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001488 * Bits 0-4 swap type if swapped
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001489 * Bits 5-54 swap offset if swapped
Mike Rapoport1ad13352018-04-18 11:07:49 +03001490 * Bit 55 pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst)
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001491 * Bit 56 page exclusively mapped
1492 * Bits 57-60 zero
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001493 * Bit 61 page is file-page or shared-anon
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001494 * Bit 62 page swapped
1495 * Bit 63 page present
1496 *
1497 * If the page is not present but in swap, then the PFN contains an
1498 * encoding of the swap file number and the page's offset into the
1499 * swap. Unmapped pages return a null PFN. This allows determining
Matt Mackall85863e42008-02-04 22:29:04 -08001500 * precisely which pages are mapped (or in swap) and comparing mapped
1501 * pages between processes.
1502 *
1503 * Efficient users of this interface will use /proc/pid/maps to
1504 * determine which areas of memory are actually mapped and llseek to
1505 * skip over unmapped regions.
1506 */
1507static ssize_t pagemap_read(struct file *file, char __user *buf,
1508 size_t count, loff_t *ppos)
1509{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001510 struct mm_struct *mm = file->private_data;
Matt Mackall85863e42008-02-04 22:29:04 -08001511 struct pagemapread pm;
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001512 unsigned long src;
1513 unsigned long svpfn;
1514 unsigned long start_vaddr;
1515 unsigned long end_vaddr;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001516 int ret = 0, copied = 0;
Matt Mackall85863e42008-02-04 22:29:04 -08001517
Vegard Nossum388f7932017-02-27 14:30:13 -08001518 if (!mm || !mmget_not_zero(mm))
Matt Mackall85863e42008-02-04 22:29:04 -08001519 goto out;
1520
Matt Mackall85863e42008-02-04 22:29:04 -08001521 ret = -EINVAL;
1522 /* file position must be aligned */
Thomas Tuttleaae86792008-06-05 22:46:31 -07001523 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001524 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001525
1526 ret = 0;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001527 if (!count)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001528 goto out_mm;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001529
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001530 /* do not disclose physical addresses: attack vector */
1531 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1532
yonghua zheng8c829622013-08-13 16:01:03 -07001533 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
Kees Cook6da2ec52018-06-12 13:55:00 -07001534 pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL);
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001535 ret = -ENOMEM;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001536 if (!pm.buffer)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001537 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001538
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001539 src = *ppos;
1540 svpfn = src / PM_ENTRY_BYTES;
1541 start_vaddr = svpfn << PAGE_SHIFT;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001542 end_vaddr = mm->task_size;
Matt Mackall85863e42008-02-04 22:29:04 -08001543
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001544 /* watch out for wraparound */
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001545 if (svpfn > mm->task_size >> PAGE_SHIFT)
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001546 start_vaddr = end_vaddr;
1547
1548 /*
1549 * The odds are that this will stop walking way
1550 * before end_vaddr, because the length of the
1551 * user buffer is tracked in "pm", and the walk
1552 * will stop when we hit the end of the buffer.
1553 */
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001554 ret = 0;
1555 while (count && (start_vaddr < end_vaddr)) {
1556 int len;
1557 unsigned long end;
Matt Mackall85863e42008-02-04 22:29:04 -08001558
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001559 pm.pos = 0;
Naoya Horiguchiea251c12010-11-24 12:57:13 -08001560 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001561 /* overflow ? */
1562 if (end < start_vaddr || end > end_vaddr)
1563 end = end_vaddr;
Konstantin Khlebnikovad80b9322019-07-11 20:59:56 -07001564 ret = down_read_killable(&mm->mmap_sem);
1565 if (ret)
1566 goto out_free;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001567 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm);
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001568 up_read(&mm->mmap_sem);
1569 start_vaddr = end;
1570
1571 len = min(count, PM_ENTRY_BYTES * pm.pos);
Dan Carpenter309361e02010-04-06 13:45:39 +03001572 if (copy_to_user(buf, pm.buffer, len)) {
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001573 ret = -EFAULT;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001574 goto out_free;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001575 }
1576 copied += len;
1577 buf += len;
1578 count -= len;
Matt Mackall85863e42008-02-04 22:29:04 -08001579 }
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001580 *ppos += copied;
1581 if (!ret || ret == PM_END_OF_BUFFER)
1582 ret = copied;
1583
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001584out_free:
1585 kfree(pm.buffer);
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001586out_mm:
1587 mmput(mm);
Matt Mackall85863e42008-02-04 22:29:04 -08001588out:
1589 return ret;
1590}
1591
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001592static int pagemap_open(struct inode *inode, struct file *file)
1593{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001594 struct mm_struct *mm;
1595
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001596 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1597 if (IS_ERR(mm))
1598 return PTR_ERR(mm);
1599 file->private_data = mm;
1600 return 0;
1601}
1602
1603static int pagemap_release(struct inode *inode, struct file *file)
1604{
1605 struct mm_struct *mm = file->private_data;
1606
1607 if (mm)
1608 mmdrop(mm);
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001609 return 0;
1610}
1611
Matt Mackall85863e42008-02-04 22:29:04 -08001612const struct file_operations proc_pagemap_operations = {
1613 .llseek = mem_lseek, /* borrow this */
1614 .read = pagemap_read,
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001615 .open = pagemap_open,
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001616 .release = pagemap_release,
Matt Mackall85863e42008-02-04 22:29:04 -08001617};
Matt Mackall1e883282008-02-04 22:29:07 -08001618#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall85863e42008-02-04 22:29:04 -08001619
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001620#ifdef CONFIG_NUMA
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001621
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001622struct numa_maps {
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001623 unsigned long pages;
1624 unsigned long anon;
1625 unsigned long active;
1626 unsigned long writeback;
1627 unsigned long mapcount_max;
1628 unsigned long dirty;
1629 unsigned long swapcache;
1630 unsigned long node[MAX_NUMNODES];
1631};
1632
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001633struct numa_maps_private {
1634 struct proc_maps_private proc_maps;
1635 struct numa_maps md;
1636};
1637
Dave Hanseneb4866d2011-09-20 15:19:38 -07001638static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1639 unsigned long nr_pages)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001640{
1641 int count = page_mapcount(page);
1642
Dave Hanseneb4866d2011-09-20 15:19:38 -07001643 md->pages += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001644 if (pte_dirty || PageDirty(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001645 md->dirty += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001646
1647 if (PageSwapCache(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001648 md->swapcache += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001649
1650 if (PageActive(page) || PageUnevictable(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001651 md->active += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001652
1653 if (PageWriteback(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001654 md->writeback += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001655
1656 if (PageAnon(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001657 md->anon += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001658
1659 if (count > md->mapcount_max)
1660 md->mapcount_max = count;
1661
Dave Hanseneb4866d2011-09-20 15:19:38 -07001662 md->node[page_to_nid(page)] += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001663}
1664
Dave Hansen3200a8a2011-09-20 15:19:39 -07001665static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1666 unsigned long addr)
1667{
1668 struct page *page;
1669 int nid;
1670
1671 if (!pte_present(pte))
1672 return NULL;
1673
1674 page = vm_normal_page(vma, addr, pte);
1675 if (!page)
1676 return NULL;
1677
1678 if (PageReserved(page))
1679 return NULL;
1680
1681 nid = page_to_nid(page);
Lai Jiangshan4ff1b2c2012-12-12 13:51:25 -08001682 if (!node_isset(nid, node_states[N_MEMORY]))
Dave Hansen3200a8a2011-09-20 15:19:39 -07001683 return NULL;
1684
1685 return page;
1686}
1687
Gerald Schaefer28093f92016-04-28 16:18:35 -07001688#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1689static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1690 struct vm_area_struct *vma,
1691 unsigned long addr)
1692{
1693 struct page *page;
1694 int nid;
1695
1696 if (!pmd_present(pmd))
1697 return NULL;
1698
1699 page = vm_normal_page_pmd(vma, addr, pmd);
1700 if (!page)
1701 return NULL;
1702
1703 if (PageReserved(page))
1704 return NULL;
1705
1706 nid = page_to_nid(page);
1707 if (!node_isset(nid, node_states[N_MEMORY]))
1708 return NULL;
1709
1710 return page;
1711}
1712#endif
1713
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001714static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1715 unsigned long end, struct mm_walk *walk)
1716{
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001717 struct numa_maps *md = walk->private;
1718 struct vm_area_struct *vma = walk->vma;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001719 spinlock_t *ptl;
1720 pte_t *orig_pte;
1721 pte_t *pte;
1722
Gerald Schaefer28093f92016-04-28 16:18:35 -07001723#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001724 ptl = pmd_trans_huge_lock(pmd, vma);
1725 if (ptl) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001726 struct page *page;
1727
Gerald Schaefer28093f92016-04-28 16:18:35 -07001728 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001729 if (page)
Gerald Schaefer28093f92016-04-28 16:18:35 -07001730 gather_stats(page, md, pmd_dirty(*pmd),
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001731 HPAGE_PMD_SIZE/PAGE_SIZE);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001732 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001733 return 0;
Dave Hansen32ef4382011-09-20 15:19:41 -07001734 }
1735
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001736 if (pmd_trans_unstable(pmd))
1737 return 0;
Gerald Schaefer28093f92016-04-28 16:18:35 -07001738#endif
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001739 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1740 do {
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001741 struct page *page = can_gather_numa_stats(*pte, vma, addr);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001742 if (!page)
1743 continue;
Dave Hanseneb4866d2011-09-20 15:19:38 -07001744 gather_stats(page, md, pte_dirty(*pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001745
1746 } while (pte++, addr += PAGE_SIZE, addr != end);
1747 pte_unmap_unlock(orig_pte, ptl);
Hugh Dickinsa66c04102016-12-12 16:44:47 -08001748 cond_resched();
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001749 return 0;
1750}
1751#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001752static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001753 unsigned long addr, unsigned long end, struct mm_walk *walk)
1754{
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001755 pte_t huge_pte = huge_ptep_get(pte);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001756 struct numa_maps *md;
1757 struct page *page;
1758
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001759 if (!pte_present(huge_pte))
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001760 return 0;
1761
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001762 page = pte_page(huge_pte);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001763 if (!page)
1764 return 0;
1765
1766 md = walk->private;
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001767 gather_stats(page, md, pte_dirty(huge_pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001768 return 0;
1769}
1770
1771#else
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001772static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001773 unsigned long addr, unsigned long end, struct mm_walk *walk)
1774{
1775 return 0;
1776}
1777#endif
1778
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001779static const struct mm_walk_ops show_numa_ops = {
1780 .hugetlb_entry = gather_hugetlb_stats,
1781 .pmd_entry = gather_pte_stats,
1782};
1783
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001784/*
1785 * Display pages allocated per node and memory policy via /proc.
1786 */
Vlastimil Babka871305b2018-08-21 21:52:48 -07001787static int show_numa_map(struct seq_file *m, void *v)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001788{
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001789 struct numa_maps_private *numa_priv = m->private;
1790 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001791 struct vm_area_struct *vma = v;
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001792 struct numa_maps *md = &numa_priv->md;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001793 struct file *file = vma->vm_file;
1794 struct mm_struct *mm = vma->vm_mm;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001795 struct mempolicy *pol;
David Rientjes948927e2013-11-12 15:07:28 -08001796 char buffer[64];
1797 int nid;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001798
1799 if (!mm)
1800 return 0;
1801
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001802 /* Ensure we start with an empty set of numa_maps statistics. */
1803 memset(md, 0, sizeof(*md));
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001804
Oleg Nesterov498f2372014-10-09 15:27:52 -07001805 pol = __get_vma_policy(vma, vma->vm_start);
1806 if (pol) {
1807 mpol_to_str(buffer, sizeof(buffer), pol);
1808 mpol_cond_put(pol);
1809 } else {
1810 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1811 }
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001812
1813 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1814
1815 if (file) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001816 seq_puts(m, " file=");
Miklos Szeredi2726d562015-06-19 10:30:28 +02001817 seq_file_path(m, file, "\n\t= ");
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001818 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001819 seq_puts(m, " heap");
Michal Hocko1240ea02017-09-08 16:13:35 -07001820 } else if (is_stack(vma)) {
Johannes Weiner65376df2016-02-02 16:57:29 -08001821 seq_puts(m, " stack");
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001822 }
1823
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001824 if (is_vm_hugetlb_page(vma))
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001825 seq_puts(m, " huge");
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001826
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001827 /* mmap_sem is held by m_start */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001828 walk_page_vma(vma, &show_numa_ops, md);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001829
1830 if (!md->pages)
1831 goto out;
1832
1833 if (md->anon)
1834 seq_printf(m, " anon=%lu", md->anon);
1835
1836 if (md->dirty)
1837 seq_printf(m, " dirty=%lu", md->dirty);
1838
1839 if (md->pages != md->anon && md->pages != md->dirty)
1840 seq_printf(m, " mapped=%lu", md->pages);
1841
1842 if (md->mapcount_max > 1)
1843 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1844
1845 if (md->swapcache)
1846 seq_printf(m, " swapcache=%lu", md->swapcache);
1847
1848 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1849 seq_printf(m, " active=%lu", md->active);
1850
1851 if (md->writeback)
1852 seq_printf(m, " writeback=%lu", md->writeback);
1853
David Rientjes948927e2013-11-12 15:07:28 -08001854 for_each_node_state(nid, N_MEMORY)
1855 if (md->node[nid])
1856 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
Rafael Aquini198d1592015-02-12 15:01:08 -08001857
1858 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001859out:
1860 seq_putc(m, '\n');
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001861 return 0;
1862}
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001863
Jan Engelhardt03a44822008-02-08 04:21:19 -08001864static const struct seq_operations proc_pid_numa_maps_op = {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001865 .start = m_start,
1866 .next = m_next,
1867 .stop = m_stop,
Vlastimil Babka871305b2018-08-21 21:52:48 -07001868 .show = show_numa_map,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001869};
Eric W. Biederman662795d2006-06-26 00:25:48 -07001870
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001871static int pid_numa_maps_open(struct inode *inode, struct file *file)
1872{
Vlastimil Babka871305b2018-08-21 21:52:48 -07001873 return proc_maps_open(inode, file, &proc_pid_numa_maps_op,
1874 sizeof(struct numa_maps_private));
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001875}
1876
1877const struct file_operations proc_pid_numa_maps_operations = {
1878 .open = pid_numa_maps_open,
1879 .read = seq_read,
1880 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -07001881 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001882};
1883
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001884#endif /* CONFIG_NUMA */