blob: 280282b05bc7b6f7d73d7be19ec3d8127a7f54c7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/mm.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>
Mauricio Line070ad42005-09-03 15:55:10 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/elf.h>
Minchan Kimb3a81d02017-08-10 15:24:15 -070023#include <asm/tlb.h>
Mauricio Line070ad42005-09-03 15:55:10 -070024#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "internal.h"
26
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080027void task_mem(struct seq_file *m, struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Konstantin Khlebnikov84638332016-01-14 15:22:07 -080029 unsigned long text, lib, swap, ptes, pmds, anon, file, shmem;
Hugh Dickins365e9c872005-10-29 18:16:18 -070030 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
31
Jerome Marchand8cee8522016-01-14 15:19:29 -080032 anon = get_mm_counter(mm, MM_ANONPAGES);
33 file = get_mm_counter(mm, MM_FILEPAGES);
34 shmem = get_mm_counter(mm, MM_SHMEMPAGES);
35
Hugh Dickins365e9c872005-10-29 18:16:18 -070036 /*
37 * Note: to minimize their overhead, mm maintains hiwater_vm and
38 * hiwater_rss only when about to *lower* total_vm or rss. Any
39 * collector of these hiwater stats must therefore get total_vm
40 * and rss too, which will usually be the higher. Barriers? not
41 * worth the effort, such snapshots can always be inconsistent.
42 */
43 hiwater_vm = total_vm = mm->total_vm;
44 if (hiwater_vm < mm->hiwater_vm)
45 hiwater_vm = mm->hiwater_vm;
Jerome Marchand8cee8522016-01-14 15:19:29 -080046 hiwater_rss = total_rss = anon + file + shmem;
Hugh Dickins365e9c872005-10-29 18:16:18 -070047 if (hiwater_rss < mm->hiwater_rss)
48 hiwater_rss = mm->hiwater_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
51 lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080052 swap = get_mm_counter(mm, MM_SWAPENTS);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -080053 ptes = PTRS_PER_PTE * sizeof(pte_t) * atomic_long_read(&mm->nr_ptes);
54 pmds = PTRS_PER_PMD * sizeof(pmd_t) * mm_nr_pmds(mm);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080055 seq_printf(m,
Hugh Dickins365e9c872005-10-29 18:16:18 -070056 "VmPeak:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 "VmSize:\t%8lu kB\n"
58 "VmLck:\t%8lu kB\n"
Christoph Lameterbc3e53f2011-10-31 17:07:30 -070059 "VmPin:\t%8lu kB\n"
Hugh Dickins365e9c872005-10-29 18:16:18 -070060 "VmHWM:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 "VmRSS:\t%8lu kB\n"
Jerome Marchand8cee8522016-01-14 15:19:29 -080062 "RssAnon:\t%8lu kB\n"
63 "RssFile:\t%8lu kB\n"
64 "RssShmem:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 "VmData:\t%8lu kB\n"
66 "VmStk:\t%8lu kB\n"
67 "VmExe:\t%8lu kB\n"
68 "VmLib:\t%8lu kB\n"
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080069 "VmPTE:\t%8lu kB\n"
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -080070 "VmPMD:\t%8lu kB\n"
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080071 "VmSwap:\t%8lu kB\n",
Hugh Dickins365e9c872005-10-29 18:16:18 -070072 hiwater_vm << (PAGE_SHIFT-10),
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -070073 total_vm << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 mm->locked_vm << (PAGE_SHIFT-10),
Christoph Lameterbc3e53f2011-10-31 17:07:30 -070075 mm->pinned_vm << (PAGE_SHIFT-10),
Hugh Dickins365e9c872005-10-29 18:16:18 -070076 hiwater_rss << (PAGE_SHIFT-10),
77 total_rss << (PAGE_SHIFT-10),
Jerome Marchand8cee8522016-01-14 15:19:29 -080078 anon << (PAGE_SHIFT-10),
79 file << (PAGE_SHIFT-10),
80 shmem << (PAGE_SHIFT-10),
Konstantin Khlebnikov84638332016-01-14 15:22:07 -080081 mm->data_vm << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -080083 ptes >> 10,
84 pmds >> 10,
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080085 swap << (PAGE_SHIFT-10));
Naoya Horiguchi5d317b22015-11-05 18:47:14 -080086 hugetlb_report_usage(m, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89unsigned long task_vsize(struct mm_struct *mm)
90{
91 return PAGE_SIZE * mm->total_vm;
92}
93
Alexey Dobriyana2ade7b2011-01-12 17:00:32 -080094unsigned long task_statm(struct mm_struct *mm,
95 unsigned long *shared, unsigned long *text,
96 unsigned long *data, unsigned long *resident)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Jerome Marchandeca56ff2016-01-14 15:19:26 -080098 *shared = get_mm_counter(mm, MM_FILEPAGES) +
99 get_mm_counter(mm, MM_SHMEMPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
101 >> PAGE_SHIFT;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -0800102 *data = mm->data_vm + mm->stack_vm;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800103 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return mm->total_vm;
105}
106
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900107#ifdef CONFIG_NUMA
108/*
Oleg Nesterov498f2372014-10-09 15:27:52 -0700109 * Save get_task_policy() for show_numa_map().
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900110 */
111static void hold_task_mempolicy(struct proc_maps_private *priv)
112{
113 struct task_struct *task = priv->task;
114
115 task_lock(task);
Oleg Nesterov498f2372014-10-09 15:27:52 -0700116 priv->task_mempolicy = get_task_policy(task);
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900117 mpol_get(priv->task_mempolicy);
118 task_unlock(task);
119}
120static void release_task_mempolicy(struct proc_maps_private *priv)
121{
122 mpol_put(priv->task_mempolicy);
123}
124#else
125static void hold_task_mempolicy(struct proc_maps_private *priv)
126{
127}
128static void release_task_mempolicy(struct proc_maps_private *priv)
129{
130}
131#endif
132
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700133static void vma_stop(struct proc_maps_private *priv)
Mauricio Line070ad42005-09-03 15:55:10 -0700134{
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700135 struct mm_struct *mm = priv->mm;
136
137 release_task_mempolicy(priv);
138 up_read(&mm->mmap_sem);
139 mmput(mm);
Matt Mackalla6198792008-02-04 22:29:03 -0800140}
141
Oleg Nesterovad2a00e2014-10-09 15:25:39 -0700142static struct vm_area_struct *
143m_next_vma(struct proc_maps_private *priv, struct vm_area_struct *vma)
144{
145 if (vma == priv->tail_vma)
146 return NULL;
147 return vma->vm_next ?: priv->tail_vma;
148}
149
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700150static void m_cache_vma(struct seq_file *m, struct vm_area_struct *vma)
151{
152 if (m->count < m->size) /* vma is copied successfully */
Robert Ho855af072016-10-07 17:02:36 -0700153 m->version = m_next_vma(m->private, vma) ? vma->vm_end : -1UL;
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700154}
155
Oleg Nesterov0c255322014-10-09 15:25:36 -0700156static void *m_start(struct seq_file *m, loff_t *ppos)
Matt Mackalla6198792008-02-04 22:29:03 -0800157{
158 struct proc_maps_private *priv = m->private;
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700159 unsigned long last_addr = m->version;
Matt Mackalla6198792008-02-04 22:29:03 -0800160 struct mm_struct *mm;
Oleg Nesterov0c255322014-10-09 15:25:36 -0700161 struct vm_area_struct *vma;
162 unsigned int pos = *ppos;
Matt Mackalla6198792008-02-04 22:29:03 -0800163
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700164 /* See m_cache_vma(). Zero at the start or after lseek. */
165 if (last_addr == -1UL)
166 return NULL;
167
Oleg Nesterov2c033762014-10-09 15:25:51 -0700168 priv->task = get_proc_task(priv->inode);
Matt Mackalla6198792008-02-04 22:29:03 -0800169 if (!priv->task)
Al Viroec6fd8a2011-02-15 22:22:54 -0500170 return ERR_PTR(-ESRCH);
Matt Mackalla6198792008-02-04 22:29:03 -0800171
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700172 mm = priv->mm;
Vegard Nossum388f7932017-02-27 14:30:13 -0800173 if (!mm || !mmget_not_zero(mm))
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700174 return NULL;
Oleg Nesterov0c255322014-10-09 15:25:36 -0700175
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200176 down_read(&mm->mmap_sem);
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900177 hold_task_mempolicy(priv);
Oleg Nesterov0c255322014-10-09 15:25:36 -0700178 priv->tail_vma = get_gate_vma(mm);
Matt Mackalla6198792008-02-04 22:29:03 -0800179
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700180 if (last_addr) {
Robert Ho855af072016-10-07 17:02:36 -0700181 vma = find_vma(mm, last_addr - 1);
182 if (vma && vma->vm_start <= last_addr)
183 vma = m_next_vma(priv, vma);
184 if (vma)
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700185 return vma;
186 }
187
188 m->version = 0;
Oleg Nesterov0c255322014-10-09 15:25:36 -0700189 if (pos < mm->map_count) {
Oleg Nesterov557c2d82014-10-09 15:25:43 -0700190 for (vma = mm->mmap; pos; pos--) {
191 m->version = vma->vm_start;
Matt Mackalla6198792008-02-04 22:29:03 -0800192 vma = vma->vm_next;
Oleg Nesterov557c2d82014-10-09 15:25:43 -0700193 }
Oleg Nesterov0c255322014-10-09 15:25:36 -0700194 return vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800195 }
196
Oleg Nesterov557c2d82014-10-09 15:25:43 -0700197 /* we do not bother to update m->version in this case */
Oleg Nesterov0c255322014-10-09 15:25:36 -0700198 if (pos == mm->map_count && priv->tail_vma)
199 return priv->tail_vma;
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700200
201 vma_stop(priv);
202 return NULL;
Matt Mackalla6198792008-02-04 22:29:03 -0800203}
204
205static void *m_next(struct seq_file *m, void *v, loff_t *pos)
206{
207 struct proc_maps_private *priv = m->private;
Oleg Nesterovad2a00e2014-10-09 15:25:39 -0700208 struct vm_area_struct *next;
Matt Mackalla6198792008-02-04 22:29:03 -0800209
210 (*pos)++;
Oleg Nesterovad2a00e2014-10-09 15:25:39 -0700211 next = m_next_vma(priv, v);
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700212 if (!next)
213 vma_stop(priv);
214 return next;
Matt Mackalla6198792008-02-04 22:29:03 -0800215}
216
217static void m_stop(struct seq_file *m, void *v)
218{
219 struct proc_maps_private *priv = m->private;
Matt Mackalla6198792008-02-04 22:29:03 -0800220
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700221 if (!IS_ERR_OR_NULL(v))
222 vma_stop(priv);
Oleg Nesterov0d5f5f42014-10-09 15:25:32 -0700223 if (priv->task) {
Matt Mackalla6198792008-02-04 22:29:03 -0800224 put_task_struct(priv->task);
Oleg Nesterov0d5f5f42014-10-09 15:25:32 -0700225 priv->task = NULL;
226 }
Matt Mackalla6198792008-02-04 22:29:03 -0800227}
228
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700229static int proc_maps_open(struct inode *inode, struct file *file,
230 const struct seq_operations *ops, int psize)
231{
232 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
233
234 if (!priv)
235 return -ENOMEM;
236
Oleg Nesterov2c033762014-10-09 15:25:51 -0700237 priv->inode = inode;
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700238 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
239 if (IS_ERR(priv->mm)) {
240 int err = PTR_ERR(priv->mm);
241
242 seq_release_private(inode, file);
243 return err;
244 }
245
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700246 return 0;
247}
248
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700249static int proc_map_release(struct inode *inode, struct file *file)
250{
251 struct seq_file *seq = file->private_data;
252 struct proc_maps_private *priv = seq->private;
253
254 if (priv->mm)
255 mmdrop(priv->mm);
256
Daniel Colascione493b0e92017-09-06 16:25:08 -0700257 kfree(priv->rollup);
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700258 return seq_release_private(inode, file);
259}
260
Matt Mackalla6198792008-02-04 22:29:03 -0800261static int do_maps_open(struct inode *inode, struct file *file,
Jan Engelhardt03a44822008-02-08 04:21:19 -0800262 const struct seq_operations *ops)
Matt Mackalla6198792008-02-04 22:29:03 -0800263{
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700264 return proc_maps_open(inode, file, ops,
265 sizeof(struct proc_maps_private));
Matt Mackalla6198792008-02-04 22:29:03 -0800266}
Mauricio Line070ad42005-09-03 15:55:10 -0700267
Johannes Weiner65376df2016-02-02 16:57:29 -0800268/*
269 * Indicate if the VMA is a stack for the given task; for
270 * /proc/PID/maps that is the stack of the main task.
271 */
Michal Hocko1240ea02017-09-08 16:13:35 -0700272static int is_stack(struct vm_area_struct *vma)
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700273{
Andy Lutomirskib18cb642016-09-30 10:58:57 -0700274 /*
275 * We make no effort to guess what a given thread considers to be
276 * its "stack". It's not even well-defined for programs written
277 * languages like Go.
278 */
279 return vma->vm_start <= vma->vm_mm->start_stack &&
280 vma->vm_end >= vma->vm_mm->start_stack;
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700281}
282
Daniel Colascione493b0e92017-09-06 16:25:08 -0700283static void show_vma_header_prefix(struct seq_file *m,
284 unsigned long start, unsigned long end,
285 vm_flags_t flags, unsigned long long pgoff,
286 dev_t dev, unsigned long ino)
287{
288 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
289 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
290 start,
291 end,
292 flags & VM_READ ? 'r' : '-',
293 flags & VM_WRITE ? 'w' : '-',
294 flags & VM_EXEC ? 'x' : '-',
295 flags & VM_MAYSHARE ? 's' : 'p',
296 pgoff,
297 MAJOR(dev), MINOR(dev), ino);
298}
299
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700300static void
301show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Mauricio Line070ad42005-09-03 15:55:10 -0700303 struct mm_struct *mm = vma->vm_mm;
304 struct file *file = vma->vm_file;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900305 vm_flags_t flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 unsigned long ino = 0;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700307 unsigned long long pgoff = 0;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200308 unsigned long start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 dev_t dev = 0;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700310 const char *name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 if (file) {
Al Viro496ad9a2013-01-23 17:07:38 -0500313 struct inode *inode = file_inode(vma->vm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 dev = inode->i_sb->s_dev;
315 ino = inode->i_ino;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700316 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
Linus Torvaldsd7824372010-08-15 11:35:52 -0700319 start = vma->vm_start;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200320 end = vma->vm_end;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700321 show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /*
324 * Print the dentry name for named mappings, and a
325 * special [heap] marker for the heap:
326 */
Mauricio Line070ad42005-09-03 15:55:10 -0700327 if (file) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800328 seq_pad(m, ' ');
Miklos Szeredi2726d562015-06-19 10:30:28 +0200329 seq_file_path(m, file, "\n");
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700330 goto done;
331 }
332
Andy Lutomirski78d683e2014-05-19 15:58:32 -0700333 if (vma->vm_ops && vma->vm_ops->name) {
334 name = vma->vm_ops->name(vma);
335 if (name)
336 goto done;
337 }
338
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700339 name = arch_vma_name(vma);
340 if (!name) {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700341 if (!mm) {
342 name = "[vdso]";
343 goto done;
344 }
345
346 if (vma->vm_start <= mm->brk &&
347 vma->vm_end >= mm->start_brk) {
348 name = "[heap]";
349 goto done;
350 }
351
Michal Hocko1240ea02017-09-08 16:13:35 -0700352 if (is_stack(vma))
Johannes Weiner65376df2016-02-02 16:57:29 -0800353 name = "[stack]";
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700354 }
355
356done:
357 if (name) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800358 seq_pad(m, ' ');
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700359 seq_puts(m, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 seq_putc(m, '\n');
Joe Korty7c88db02008-10-16 15:27:09 +0400362}
363
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700364static int show_map(struct seq_file *m, void *v, int is_pid)
Joe Korty7c88db02008-10-16 15:27:09 +0400365{
Oleg Nesterovebb6cdd2014-10-09 15:25:34 -0700366 show_map_vma(m, v, is_pid);
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700367 m_cache_vma(m, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369}
370
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700371static int show_pid_map(struct seq_file *m, void *v)
372{
373 return show_map(m, v, 1);
374}
375
376static int show_tid_map(struct seq_file *m, void *v)
377{
378 return show_map(m, v, 0);
379}
380
Jan Engelhardt03a44822008-02-08 04:21:19 -0800381static const struct seq_operations proc_pid_maps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800382 .start = m_start,
383 .next = m_next,
384 .stop = m_stop,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700385 .show = show_pid_map
Matt Mackalla6198792008-02-04 22:29:03 -0800386};
387
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700388static const struct seq_operations proc_tid_maps_op = {
389 .start = m_start,
390 .next = m_next,
391 .stop = m_stop,
392 .show = show_tid_map
393};
394
395static int pid_maps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800396{
397 return do_maps_open(inode, file, &proc_pid_maps_op);
398}
399
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700400static int tid_maps_open(struct inode *inode, struct file *file)
401{
402 return do_maps_open(inode, file, &proc_tid_maps_op);
403}
404
405const struct file_operations proc_pid_maps_operations = {
406 .open = pid_maps_open,
407 .read = seq_read,
408 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700409 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700410};
411
412const struct file_operations proc_tid_maps_operations = {
413 .open = tid_maps_open,
Matt Mackalla6198792008-02-04 22:29:03 -0800414 .read = seq_read,
415 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700416 .release = proc_map_release,
Matt Mackalla6198792008-02-04 22:29:03 -0800417};
418
419/*
420 * Proportional Set Size(PSS): my share of RSS.
421 *
422 * PSS of a process is the count of pages it has in memory, where each
423 * page is divided by the number of processes sharing it. So if a
424 * process has 1000 pages all to itself, and 1000 shared with one other
425 * process, its PSS will be 1500.
426 *
427 * To keep (accumulated) division errors low, we adopt a 64bit
428 * fixed-point pss counter to minimize division errors. So (pss >>
429 * PSS_SHIFT) would be the real byte count.
430 *
431 * A shift of 12 before division means (assuming 4K page size):
432 * - 1M 3-user-pages add up to 8KB errors;
433 * - supports mapcount up to 2^24, or 16M;
434 * - supports PSS up to 2^52 bytes, or 4PB.
435 */
436#define PSS_SHIFT 12
437
Matt Mackall1e883282008-02-04 22:29:07 -0800438#ifdef CONFIG_PROC_PAGE_MONITOR
Peter Zijlstra214e4712008-04-28 02:12:55 -0700439struct mem_size_stats {
Daniel Colascione493b0e92017-09-06 16:25:08 -0700440 bool first;
Matt Mackalla6198792008-02-04 22:29:03 -0800441 unsigned long resident;
442 unsigned long shared_clean;
443 unsigned long shared_dirty;
444 unsigned long private_clean;
445 unsigned long private_dirty;
446 unsigned long referenced;
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700447 unsigned long anonymous;
Shaohua Licf8496e2017-05-03 14:52:42 -0700448 unsigned long lazyfree;
Dave Hansen4031a212011-03-22 16:33:01 -0700449 unsigned long anonymous_thp;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700450 unsigned long shmem_thp;
Peter Zijlstra214e4712008-04-28 02:12:55 -0700451 unsigned long swap;
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800452 unsigned long shared_hugetlb;
453 unsigned long private_hugetlb;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700454 unsigned long first_vma_start;
Matt Mackalla6198792008-02-04 22:29:03 -0800455 u64 pss;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700456 u64 pss_locked;
Minchan Kim8334b962015-09-08 15:00:24 -0700457 u64 swap_pss;
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800458 bool check_shmem_swap;
Matt Mackalla6198792008-02-04 22:29:03 -0800459};
460
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800461static void smaps_account(struct mem_size_stats *mss, struct page *page,
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800462 bool compound, bool young, bool dirty)
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800463{
Kirill A. Shutemovf4be6152016-01-20 14:58:12 -0800464 int i, nr = compound ? 1 << compound_order(page) : 1;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800465 unsigned long size = nr * PAGE_SIZE;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700466
Shaohua Licf8496e2017-05-03 14:52:42 -0700467 if (PageAnon(page)) {
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800468 mss->anonymous += size;
Shaohua Licf8496e2017-05-03 14:52:42 -0700469 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
470 mss->lazyfree += size;
471 }
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800472
473 mss->resident += size;
474 /* Accumulate the size in pages that have been accessed. */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700475 if (young || page_is_young(page) || PageReferenced(page))
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800476 mss->referenced += size;
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800477
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800478 /*
479 * page_count(page) == 1 guarantees the page is mapped exactly once.
480 * If any subpage of the compound page mapped with PTE it would elevate
481 * page_count().
482 */
483 if (page_count(page) == 1) {
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800484 if (dirty || PageDirty(page))
485 mss->private_dirty += size;
486 else
487 mss->private_clean += size;
488 mss->pss += (u64)size << PSS_SHIFT;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800489 return;
490 }
491
492 for (i = 0; i < nr; i++, page++) {
493 int mapcount = page_mapcount(page);
494
495 if (mapcount >= 2) {
496 if (dirty || PageDirty(page))
497 mss->shared_dirty += PAGE_SIZE;
498 else
499 mss->shared_clean += PAGE_SIZE;
500 mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount;
501 } else {
502 if (dirty || PageDirty(page))
503 mss->private_dirty += PAGE_SIZE;
504 else
505 mss->private_clean += PAGE_SIZE;
506 mss->pss += PAGE_SIZE << PSS_SHIFT;
507 }
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800508 }
509}
510
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800511#ifdef CONFIG_SHMEM
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800512static int smaps_pte_hole(unsigned long addr, unsigned long end,
513 struct mm_walk *walk)
514{
515 struct mem_size_stats *mss = walk->private;
516
Vlastimil Babka48131e02016-01-14 15:19:23 -0800517 mss->swap += shmem_partial_swap_usage(
518 walk->vma->vm_file->f_mapping, addr, end);
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800519
520 return 0;
521}
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800522#endif
523
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800524static void smaps_pte_entry(pte_t *pte, unsigned long addr,
525 struct mm_walk *walk)
Dave Hansenae11c4d2011-03-22 16:32:58 -0700526{
527 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800528 struct vm_area_struct *vma = walk->vma;
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700529 struct page *page = NULL;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700530
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800531 if (pte_present(*pte)) {
532 page = vm_normal_page(vma, addr, *pte);
533 } else if (is_swap_pte(*pte)) {
534 swp_entry_t swpent = pte_to_swp_entry(*pte);
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700535
Minchan Kim8334b962015-09-08 15:00:24 -0700536 if (!non_swap_entry(swpent)) {
537 int mapcount;
538
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800539 mss->swap += PAGE_SIZE;
Minchan Kim8334b962015-09-08 15:00:24 -0700540 mapcount = swp_swapcount(swpent);
541 if (mapcount >= 2) {
542 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
543
544 do_div(pss_delta, mapcount);
545 mss->swap_pss += pss_delta;
546 } else {
547 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
548 }
549 } else if (is_migration_entry(swpent))
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700550 page = migration_entry_to_page(swpent);
Jérôme Glisse5042db42017-09-08 16:11:43 -0700551 else if (is_device_private_entry(swpent))
552 page = device_private_entry_to_page(swpent);
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800553 } else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
554 && pte_none(*pte))) {
Vlastimil Babka48131e02016-01-14 15:19:23 -0800555 page = find_get_entry(vma->vm_file->f_mapping,
556 linear_page_index(vma, addr));
557 if (!page)
558 return;
559
560 if (radix_tree_exceptional_entry(page))
561 mss->swap += PAGE_SIZE;
562 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300563 put_page(page);
Vlastimil Babka48131e02016-01-14 15:19:23 -0800564
565 return;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700566 }
567
Dave Hansenae11c4d2011-03-22 16:32:58 -0700568 if (!page)
569 return;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800570
571 smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte));
Dave Hansenae11c4d2011-03-22 16:32:58 -0700572}
573
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800574#ifdef CONFIG_TRANSPARENT_HUGEPAGE
575static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
576 struct mm_walk *walk)
577{
578 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800579 struct vm_area_struct *vma = walk->vma;
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800580 struct page *page;
581
582 /* FOLL_DUMP will return -EFAULT on huge zero page */
583 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
584 if (IS_ERR_OR_NULL(page))
585 return;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700586 if (PageAnon(page))
587 mss->anonymous_thp += HPAGE_PMD_SIZE;
588 else if (PageSwapBacked(page))
589 mss->shmem_thp += HPAGE_PMD_SIZE;
Dan Williamsca120cf2016-09-03 10:38:03 -0700590 else if (is_zone_device_page(page))
591 /* pass */;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700592 else
593 VM_BUG_ON_PAGE(1, page);
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800594 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd));
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800595}
596#else
597static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
598 struct mm_walk *walk)
599{
600}
601#endif
602
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800603static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700604 struct mm_walk *walk)
Mauricio Line070ad42005-09-03 15:55:10 -0700605{
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800606 struct vm_area_struct *vma = walk->vma;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700607 pte_t *pte;
Hugh Dickins705e87c2005-10-29 18:16:27 -0700608 spinlock_t *ptl;
Mauricio Line070ad42005-09-03 15:55:10 -0700609
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -0800610 ptl = pmd_trans_huge_lock(pmd, vma);
611 if (ptl) {
Zi Yan84c3fc42017-09-08 16:11:01 -0700612 if (pmd_present(*pmd))
613 smaps_pmd_entry(pmd, addr, walk);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -0800614 spin_unlock(ptl);
David Rientjes14038302017-09-08 16:13:41 -0700615 goto out;
Dave Hansen22e057c2011-03-22 16:33:00 -0700616 }
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700617
618 if (pmd_trans_unstable(pmd))
David Rientjes14038302017-09-08 16:13:41 -0700619 goto out;
Dave Hansen22e057c2011-03-22 16:33:00 -0700620 /*
621 * The mmap_sem held all the way back in m_start() is what
622 * keeps khugepaged out of here and from collapsing things
623 * in here.
624 */
Hugh Dickins705e87c2005-10-29 18:16:27 -0700625 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700626 for (; addr != end; pte++, addr += PAGE_SIZE)
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800627 smaps_pte_entry(pte, addr, walk);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700628 pte_unmap_unlock(pte - 1, ptl);
David Rientjes14038302017-09-08 16:13:41 -0700629out:
Hugh Dickins705e87c2005-10-29 18:16:27 -0700630 cond_resched();
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800631 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700632}
633
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800634static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
635{
636 /*
637 * Don't forget to update Documentation/ on changes.
638 */
639 static const char mnemonics[BITS_PER_LONG][2] = {
640 /*
641 * In case if we meet a flag we don't know about.
642 */
643 [0 ... (BITS_PER_LONG-1)] = "??",
644
645 [ilog2(VM_READ)] = "rd",
646 [ilog2(VM_WRITE)] = "wr",
647 [ilog2(VM_EXEC)] = "ex",
648 [ilog2(VM_SHARED)] = "sh",
649 [ilog2(VM_MAYREAD)] = "mr",
650 [ilog2(VM_MAYWRITE)] = "mw",
651 [ilog2(VM_MAYEXEC)] = "me",
652 [ilog2(VM_MAYSHARE)] = "ms",
653 [ilog2(VM_GROWSDOWN)] = "gd",
654 [ilog2(VM_PFNMAP)] = "pf",
655 [ilog2(VM_DENYWRITE)] = "dw",
Qiaowei Ren4aae7e42014-11-14 07:18:25 -0800656#ifdef CONFIG_X86_INTEL_MPX
657 [ilog2(VM_MPX)] = "mp",
658#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800659 [ilog2(VM_LOCKED)] = "lo",
660 [ilog2(VM_IO)] = "io",
661 [ilog2(VM_SEQ_READ)] = "sr",
662 [ilog2(VM_RAND_READ)] = "rr",
663 [ilog2(VM_DONTCOPY)] = "dc",
664 [ilog2(VM_DONTEXPAND)] = "de",
665 [ilog2(VM_ACCOUNT)] = "ac",
666 [ilog2(VM_NORESERVE)] = "nr",
667 [ilog2(VM_HUGETLB)] = "ht",
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800668 [ilog2(VM_ARCH_1)] = "ar",
Rik van Rield2cd9ed2017-09-06 16:25:15 -0700669 [ilog2(VM_WIPEONFORK)] = "wf",
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800670 [ilog2(VM_DONTDUMP)] = "dd",
Naoya Horiguchiec8e41a2013-11-12 15:07:49 -0800671#ifdef CONFIG_MEM_SOFT_DIRTY
672 [ilog2(VM_SOFTDIRTY)] = "sd",
673#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800674 [ilog2(VM_MIXEDMAP)] = "mm",
675 [ilog2(VM_HUGEPAGE)] = "hg",
676 [ilog2(VM_NOHUGEPAGE)] = "nh",
677 [ilog2(VM_MERGEABLE)] = "mg",
Andrea Arcangeli16ba6f82015-09-04 15:46:17 -0700678 [ilog2(VM_UFFD_MISSING)]= "um",
679 [ilog2(VM_UFFD_WP)] = "uw",
Dave Hansenc1192f82016-02-12 13:02:27 -0800680#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
681 /* These come out via ProtectionKey: */
682 [ilog2(VM_PKEY_BIT0)] = "",
683 [ilog2(VM_PKEY_BIT1)] = "",
684 [ilog2(VM_PKEY_BIT2)] = "",
685 [ilog2(VM_PKEY_BIT3)] = "",
686#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800687 };
688 size_t i;
689
690 seq_puts(m, "VmFlags: ");
691 for (i = 0; i < BITS_PER_LONG; i++) {
Dave Hansenc1192f82016-02-12 13:02:27 -0800692 if (!mnemonics[i][0])
693 continue;
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800694 if (vma->vm_flags & (1UL << i)) {
695 seq_printf(m, "%c%c ",
696 mnemonics[i][0], mnemonics[i][1]);
697 }
698 }
699 seq_putc(m, '\n');
700}
701
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800702#ifdef CONFIG_HUGETLB_PAGE
703static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
704 unsigned long addr, unsigned long end,
705 struct mm_walk *walk)
706{
707 struct mem_size_stats *mss = walk->private;
708 struct vm_area_struct *vma = walk->vma;
709 struct page *page = NULL;
710
711 if (pte_present(*pte)) {
712 page = vm_normal_page(vma, addr, *pte);
713 } else if (is_swap_pte(*pte)) {
714 swp_entry_t swpent = pte_to_swp_entry(*pte);
715
716 if (is_migration_entry(swpent))
717 page = migration_entry_to_page(swpent);
Jérôme Glisse5042db42017-09-08 16:11:43 -0700718 else if (is_device_private_entry(swpent))
719 page = device_private_entry_to_page(swpent);
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800720 }
721 if (page) {
722 int mapcount = page_mapcount(page);
723
724 if (mapcount >= 2)
725 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
726 else
727 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
728 }
729 return 0;
730}
731#endif /* HUGETLB_PAGE */
732
Dave Hansenc1192f82016-02-12 13:02:27 -0800733void __weak arch_show_smap(struct seq_file *m, struct vm_area_struct *vma)
734{
735}
736
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700737static int show_smap(struct seq_file *m, void *v, int is_pid)
Mauricio Line070ad42005-09-03 15:55:10 -0700738{
Daniel Colascione493b0e92017-09-06 16:25:08 -0700739 struct proc_maps_private *priv = m->private;
Mauricio Line070ad42005-09-03 15:55:10 -0700740 struct vm_area_struct *vma = v;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700741 struct mem_size_stats mss_stack;
742 struct mem_size_stats *mss;
Dave Hansen21650092008-06-12 15:21:47 -0700743 struct mm_walk smaps_walk = {
744 .pmd_entry = smaps_pte_range,
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800745#ifdef CONFIG_HUGETLB_PAGE
746 .hugetlb_entry = smaps_hugetlb_range,
747#endif
Dave Hansen21650092008-06-12 15:21:47 -0700748 .mm = vma->vm_mm,
Dave Hansen21650092008-06-12 15:21:47 -0700749 };
Daniel Colascione493b0e92017-09-06 16:25:08 -0700750 int ret = 0;
751 bool rollup_mode;
752 bool last_vma;
Mauricio Line070ad42005-09-03 15:55:10 -0700753
Daniel Colascione493b0e92017-09-06 16:25:08 -0700754 if (priv->rollup) {
755 rollup_mode = true;
756 mss = priv->rollup;
757 if (mss->first) {
758 mss->first_vma_start = vma->vm_start;
759 mss->first = false;
760 }
761 last_vma = !m_next_vma(priv, vma);
762 } else {
763 rollup_mode = false;
764 memset(&mss_stack, 0, sizeof(mss_stack));
765 mss = &mss_stack;
766 }
767
768 smaps_walk.private = mss;
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800769
770#ifdef CONFIG_SHMEM
771 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800772 /*
773 * For shared or readonly shmem mappings we know that all
774 * swapped out pages belong to the shmem object, and we can
775 * obtain the swap value much more efficiently. For private
776 * writable mappings, we might have COW pages that are
777 * not affected by the parent swapped out pages of the shmem
778 * object, so we have to distinguish them during the page walk.
779 * Unless we know that the shmem object (or the part mapped by
780 * our VMA) has no swapped out pages at all.
781 */
782 unsigned long shmem_swapped = shmem_swap_usage(vma);
783
784 if (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
785 !(vma->vm_flags & VM_WRITE)) {
Daniel Colascione493b0e92017-09-06 16:25:08 -0700786 mss->swap = shmem_swapped;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800787 } else {
Daniel Colascione493b0e92017-09-06 16:25:08 -0700788 mss->check_shmem_swap = true;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800789 smaps_walk.pte_hole = smaps_pte_hole;
790 }
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800791 }
792#endif
793
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +0900794 /* mmap_sem is held in m_start */
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800795 walk_page_vma(vma, &smaps_walk);
Daniel Colascione493b0e92017-09-06 16:25:08 -0700796 if (vma->vm_flags & VM_LOCKED)
797 mss->pss_locked += mss->pss;
Matt Mackall4752c362008-02-04 22:29:02 -0800798
Daniel Colascione493b0e92017-09-06 16:25:08 -0700799 if (!rollup_mode) {
800 show_map_vma(m, vma, is_pid);
801 } else if (last_vma) {
802 show_vma_header_prefix(
803 m, mss->first_vma_start, vma->vm_end, 0, 0, 0, 0);
804 seq_pad(m, ' ');
805 seq_puts(m, "[rollup]\n");
806 } else {
807 ret = SEQ_SKIP;
808 }
Matt Mackall4752c362008-02-04 22:29:02 -0800809
Daniel Colascione493b0e92017-09-06 16:25:08 -0700810 if (!rollup_mode)
811 seq_printf(m,
812 "Size: %8lu kB\n"
813 "KernelPageSize: %8lu kB\n"
814 "MMUPageSize: %8lu kB\n",
815 (vma->vm_end - vma->vm_start) >> 10,
816 vma_kernel_pagesize(vma) >> 10,
817 vma_mmu_pagesize(vma) >> 10);
Matt Mackall4752c362008-02-04 22:29:02 -0800818
Daniel Colascione493b0e92017-09-06 16:25:08 -0700819
820 if (!rollup_mode || last_vma)
821 seq_printf(m,
822 "Rss: %8lu kB\n"
823 "Pss: %8lu kB\n"
824 "Shared_Clean: %8lu kB\n"
825 "Shared_Dirty: %8lu kB\n"
826 "Private_Clean: %8lu kB\n"
827 "Private_Dirty: %8lu kB\n"
828 "Referenced: %8lu kB\n"
829 "Anonymous: %8lu kB\n"
830 "LazyFree: %8lu kB\n"
831 "AnonHugePages: %8lu kB\n"
832 "ShmemPmdMapped: %8lu kB\n"
833 "Shared_Hugetlb: %8lu kB\n"
834 "Private_Hugetlb: %7lu kB\n"
835 "Swap: %8lu kB\n"
836 "SwapPss: %8lu kB\n"
837 "Locked: %8lu kB\n",
838 mss->resident >> 10,
839 (unsigned long)(mss->pss >> (10 + PSS_SHIFT)),
840 mss->shared_clean >> 10,
841 mss->shared_dirty >> 10,
842 mss->private_clean >> 10,
843 mss->private_dirty >> 10,
844 mss->referenced >> 10,
845 mss->anonymous >> 10,
846 mss->lazyfree >> 10,
847 mss->anonymous_thp >> 10,
848 mss->shmem_thp >> 10,
849 mss->shared_hugetlb >> 10,
850 mss->private_hugetlb >> 10,
851 mss->swap >> 10,
852 (unsigned long)(mss->swap_pss >> (10 + PSS_SHIFT)),
853 (unsigned long)(mss->pss >> (10 + PSS_SHIFT)));
854
855 if (!rollup_mode) {
856 arch_show_smap(m, vma);
857 show_smap_vma_flags(m, vma);
858 }
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700859 m_cache_vma(m, vma);
Daniel Colascione493b0e92017-09-06 16:25:08 -0700860 return ret;
Mauricio Line070ad42005-09-03 15:55:10 -0700861}
862
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700863static int show_pid_smap(struct seq_file *m, void *v)
864{
865 return show_smap(m, v, 1);
866}
867
868static int show_tid_smap(struct seq_file *m, void *v)
869{
870 return show_smap(m, v, 0);
871}
872
Jan Engelhardt03a44822008-02-08 04:21:19 -0800873static const struct seq_operations proc_pid_smaps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800874 .start = m_start,
875 .next = m_next,
876 .stop = m_stop,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700877 .show = show_pid_smap
Matt Mackalla6198792008-02-04 22:29:03 -0800878};
879
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700880static const struct seq_operations proc_tid_smaps_op = {
881 .start = m_start,
882 .next = m_next,
883 .stop = m_stop,
884 .show = show_tid_smap
885};
886
887static int pid_smaps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800888{
889 return do_maps_open(inode, file, &proc_pid_smaps_op);
890}
891
Daniel Colascione493b0e92017-09-06 16:25:08 -0700892static int pid_smaps_rollup_open(struct inode *inode, struct file *file)
893{
894 struct seq_file *seq;
895 struct proc_maps_private *priv;
896 int ret = do_maps_open(inode, file, &proc_pid_smaps_op);
897
898 if (ret < 0)
899 return ret;
900 seq = file->private_data;
901 priv = seq->private;
902 priv->rollup = kzalloc(sizeof(*priv->rollup), GFP_KERNEL);
903 if (!priv->rollup) {
904 proc_map_release(inode, file);
905 return -ENOMEM;
906 }
907 priv->rollup->first = true;
908 return 0;
909}
910
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700911static int tid_smaps_open(struct inode *inode, struct file *file)
912{
913 return do_maps_open(inode, file, &proc_tid_smaps_op);
914}
915
916const struct file_operations proc_pid_smaps_operations = {
917 .open = pid_smaps_open,
918 .read = seq_read,
919 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700920 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700921};
922
Daniel Colascione493b0e92017-09-06 16:25:08 -0700923const struct file_operations proc_pid_smaps_rollup_operations = {
924 .open = pid_smaps_rollup_open,
925 .read = seq_read,
926 .llseek = seq_lseek,
927 .release = proc_map_release,
928};
929
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700930const struct file_operations proc_tid_smaps_operations = {
931 .open = tid_smaps_open,
Matt Mackalla6198792008-02-04 22:29:03 -0800932 .read = seq_read,
933 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700934 .release = proc_map_release,
Matt Mackalla6198792008-02-04 22:29:03 -0800935};
936
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700937enum clear_refs_types {
938 CLEAR_REFS_ALL = 1,
939 CLEAR_REFS_ANON,
940 CLEAR_REFS_MAPPED,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700941 CLEAR_REFS_SOFT_DIRTY,
Petr Cermak695f0552015-02-12 15:01:00 -0800942 CLEAR_REFS_MM_HIWATER_RSS,
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700943 CLEAR_REFS_LAST,
944};
945
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700946struct clear_refs_private {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700947 enum clear_refs_types type;
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700948};
949
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800950#ifdef CONFIG_MEM_SOFT_DIRTY
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700951static inline void clear_soft_dirty(struct vm_area_struct *vma,
952 unsigned long addr, pte_t *pte)
953{
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700954 /*
955 * The soft-dirty tracker uses #PF-s to catch writes
956 * to pages, so write-protect the pte as well. See the
957 * Documentation/vm/soft-dirty.txt for full description
958 * of how soft-dirty works.
959 */
960 pte_t ptent = *pte;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700961
962 if (pte_present(ptent)) {
Laurent Dufour326c2592015-11-05 18:49:21 -0800963 ptent = ptep_modify_prot_start(vma->vm_mm, addr, pte);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700964 ptent = pte_wrprotect(ptent);
Martin Schwidefskya7b76172015-04-22 14:20:47 +0200965 ptent = pte_clear_soft_dirty(ptent);
Laurent Dufour326c2592015-11-05 18:49:21 -0800966 ptep_modify_prot_commit(vma->vm_mm, addr, pte, ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700967 } else if (is_swap_pte(ptent)) {
968 ptent = pte_swp_clear_soft_dirty(ptent);
Laurent Dufour326c2592015-11-05 18:49:21 -0800969 set_pte_at(vma->vm_mm, addr, pte, ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700970 }
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700971}
Laurent Dufour5d3875a2015-11-05 18:49:24 -0800972#else
973static inline void clear_soft_dirty(struct vm_area_struct *vma,
974 unsigned long addr, pte_t *pte)
975{
976}
977#endif
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700978
Laurent Dufour5d3875a2015-11-05 18:49:24 -0800979#if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800980static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
981 unsigned long addr, pmd_t *pmdp)
982{
Kirill A. Shutemov5b7abea2017-04-13 14:56:28 -0700983 pmd_t pmd = *pmdp;
984
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -0700985 if (pmd_present(pmd)) {
986 /* See comment in change_huge_pmd() */
987 pmdp_invalidate(vma, addr, pmdp);
988 if (pmd_dirty(*pmdp))
989 pmd = pmd_mkdirty(pmd);
990 if (pmd_young(*pmdp))
991 pmd = pmd_mkyoung(pmd);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800992
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -0700993 pmd = pmd_wrprotect(pmd);
994 pmd = pmd_clear_soft_dirty(pmd);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800995
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -0700996 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
997 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) {
998 pmd = pmd_swp_clear_soft_dirty(pmd);
999 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1000 }
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001001}
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001002#else
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001003static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1004 unsigned long addr, pmd_t *pmdp)
1005{
1006}
1007#endif
1008
Matt Mackalla6198792008-02-04 22:29:03 -08001009static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
Dave Hansen21650092008-06-12 15:21:47 -07001010 unsigned long end, struct mm_walk *walk)
Matt Mackalla6198792008-02-04 22:29:03 -08001011{
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001012 struct clear_refs_private *cp = walk->private;
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001013 struct vm_area_struct *vma = walk->vma;
Matt Mackalla6198792008-02-04 22:29:03 -08001014 pte_t *pte, ptent;
1015 spinlock_t *ptl;
1016 struct page *page;
1017
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001018 ptl = pmd_trans_huge_lock(pmd, vma);
1019 if (ptl) {
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001020 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1021 clear_soft_dirty_pmd(vma, addr, pmd);
1022 goto out;
1023 }
1024
Zi Yan84c3fc42017-09-08 16:11:01 -07001025 if (!pmd_present(*pmd))
1026 goto out;
1027
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001028 page = pmd_page(*pmd);
1029
1030 /* Clear accessed and referenced bits. */
1031 pmdp_test_and_clear_young(vma, addr, pmd);
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07001032 test_and_clear_page_young(page);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001033 ClearPageReferenced(page);
1034out:
1035 spin_unlock(ptl);
1036 return 0;
1037 }
1038
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001039 if (pmd_trans_unstable(pmd))
1040 return 0;
Dave Hansen03319322011-03-22 16:32:56 -07001041
Matt Mackalla6198792008-02-04 22:29:03 -08001042 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1043 for (; addr != end; pte++, addr += PAGE_SIZE) {
1044 ptent = *pte;
Matt Mackalla6198792008-02-04 22:29:03 -08001045
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001046 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1047 clear_soft_dirty(vma, addr, pte);
1048 continue;
1049 }
1050
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001051 if (!pte_present(ptent))
1052 continue;
1053
Matt Mackalla6198792008-02-04 22:29:03 -08001054 page = vm_normal_page(vma, addr, ptent);
1055 if (!page)
1056 continue;
1057
1058 /* Clear accessed and referenced bits. */
1059 ptep_test_and_clear_young(vma, addr, pte);
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07001060 test_and_clear_page_young(page);
Matt Mackalla6198792008-02-04 22:29:03 -08001061 ClearPageReferenced(page);
1062 }
1063 pte_unmap_unlock(pte - 1, ptl);
1064 cond_resched();
1065 return 0;
1066}
1067
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001068static int clear_refs_test_walk(unsigned long start, unsigned long end,
1069 struct mm_walk *walk)
1070{
1071 struct clear_refs_private *cp = walk->private;
1072 struct vm_area_struct *vma = walk->vma;
1073
Naoya Horiguchi48684a62015-02-11 15:28:06 -08001074 if (vma->vm_flags & VM_PFNMAP)
1075 return 1;
1076
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001077 /*
1078 * Writing 1 to /proc/pid/clear_refs affects all pages.
1079 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
1080 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
1081 * Writing 4 to /proc/pid/clear_refs affects all pages.
1082 */
1083 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
1084 return 1;
1085 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1086 return 1;
1087 return 0;
1088}
1089
Matt Mackallf248dcb2008-02-04 22:29:03 -08001090static ssize_t clear_refs_write(struct file *file, const char __user *buf,
1091 size_t count, loff_t *ppos)
David Rientjesb813e932007-05-06 14:49:24 -07001092{
Matt Mackallf248dcb2008-02-04 22:29:03 -08001093 struct task_struct *task;
Vincent Lifb92a4b2009-09-22 16:45:36 -07001094 char buffer[PROC_NUMBUF];
Matt Mackallf248dcb2008-02-04 22:29:03 -08001095 struct mm_struct *mm;
David Rientjesb813e932007-05-06 14:49:24 -07001096 struct vm_area_struct *vma;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001097 enum clear_refs_types type;
Minchan Kimb3a81d02017-08-10 15:24:15 -07001098 struct mmu_gather tlb;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001099 int itype;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001100 int rv;
David Rientjesb813e932007-05-06 14:49:24 -07001101
Matt Mackallf248dcb2008-02-04 22:29:03 -08001102 memset(buffer, 0, sizeof(buffer));
1103 if (count > sizeof(buffer) - 1)
1104 count = sizeof(buffer) - 1;
1105 if (copy_from_user(buffer, buf, count))
1106 return -EFAULT;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001107 rv = kstrtoint(strstrip(buffer), 10, &itype);
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001108 if (rv < 0)
1109 return rv;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001110 type = (enum clear_refs_types)itype;
1111 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
Matt Mackallf248dcb2008-02-04 22:29:03 -08001112 return -EINVAL;
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001113
Al Viro496ad9a2013-01-23 17:07:38 -05001114 task = get_proc_task(file_inode(file));
Matt Mackallf248dcb2008-02-04 22:29:03 -08001115 if (!task)
1116 return -ESRCH;
1117 mm = get_task_mm(task);
1118 if (mm) {
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001119 struct clear_refs_private cp = {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001120 .type = type,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001121 };
Andrew Morton20cbc972008-07-05 12:29:05 -07001122 struct mm_walk clear_refs_walk = {
1123 .pmd_entry = clear_refs_pte_range,
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001124 .test_walk = clear_refs_test_walk,
Andrew Morton20cbc972008-07-05 12:29:05 -07001125 .mm = mm,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001126 .private = &cp,
Andrew Morton20cbc972008-07-05 12:29:05 -07001127 };
Petr Cermak695f0552015-02-12 15:01:00 -08001128
1129 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
Michal Hocko4e801532016-05-23 16:25:45 -07001130 if (down_write_killable(&mm->mmap_sem)) {
1131 count = -EINTR;
1132 goto out_mm;
1133 }
1134
Petr Cermak695f0552015-02-12 15:01:00 -08001135 /*
1136 * Writing 5 to /proc/pid/clear_refs resets the peak
1137 * resident set size to this mm's current rss value.
1138 */
Petr Cermak695f0552015-02-12 15:01:00 -08001139 reset_mm_hiwater_rss(mm);
1140 up_write(&mm->mmap_sem);
1141 goto out_mm;
1142 }
1143
Matt Mackallf248dcb2008-02-04 22:29:03 -08001144 down_read(&mm->mmap_sem);
Minchan Kimb3a81d02017-08-10 15:24:15 -07001145 tlb_gather_mmu(&tlb, mm, 0, -1);
Peter Feiner64e455072014-10-13 15:55:46 -07001146 if (type == CLEAR_REFS_SOFT_DIRTY) {
1147 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1148 if (!(vma->vm_flags & VM_SOFTDIRTY))
1149 continue;
1150 up_read(&mm->mmap_sem);
Michal Hocko4e801532016-05-23 16:25:45 -07001151 if (down_write_killable(&mm->mmap_sem)) {
1152 count = -EINTR;
1153 goto out_mm;
1154 }
Peter Feiner64e455072014-10-13 15:55:46 -07001155 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1156 vma->vm_flags &= ~VM_SOFTDIRTY;
1157 vma_set_page_prot(vma);
1158 }
1159 downgrade_write(&mm->mmap_sem);
1160 break;
1161 }
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001162 mmu_notifier_invalidate_range_start(mm, 0, -1);
Peter Feiner64e455072014-10-13 15:55:46 -07001163 }
James Morse0f302062016-10-07 17:00:06 -07001164 walk_page_range(0, mm->highest_vm_end, &clear_refs_walk);
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001165 if (type == CLEAR_REFS_SOFT_DIRTY)
1166 mmu_notifier_invalidate_range_end(mm, 0, -1);
Minchan Kimb3a81d02017-08-10 15:24:15 -07001167 tlb_finish_mmu(&tlb, 0, -1);
Matt Mackallf248dcb2008-02-04 22:29:03 -08001168 up_read(&mm->mmap_sem);
Petr Cermak695f0552015-02-12 15:01:00 -08001169out_mm:
Matt Mackallf248dcb2008-02-04 22:29:03 -08001170 mmput(mm);
1171 }
1172 put_task_struct(task);
Vincent Lifb92a4b2009-09-22 16:45:36 -07001173
1174 return count;
David Rientjesb813e932007-05-06 14:49:24 -07001175}
1176
Matt Mackallf248dcb2008-02-04 22:29:03 -08001177const struct file_operations proc_clear_refs_operations = {
1178 .write = clear_refs_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001179 .llseek = noop_llseek,
Matt Mackallf248dcb2008-02-04 22:29:03 -08001180};
1181
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001182typedef struct {
1183 u64 pme;
1184} pagemap_entry_t;
1185
Matt Mackall85863e42008-02-04 22:29:04 -08001186struct pagemapread {
yonghua zheng8c829622013-08-13 16:01:03 -07001187 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001188 pagemap_entry_t *buffer;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001189 bool show_pfn;
Matt Mackall85863e42008-02-04 22:29:04 -08001190};
1191
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001192#define PAGEMAP_WALK_SIZE (PMD_SIZE)
1193#define PAGEMAP_WALK_MASK (PMD_MASK)
1194
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001195#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1196#define PM_PFRAME_BITS 55
1197#define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1198#define PM_SOFT_DIRTY BIT_ULL(55)
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001199#define PM_MMAP_EXCLUSIVE BIT_ULL(56)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001200#define PM_FILE BIT_ULL(61)
1201#define PM_SWAP BIT_ULL(62)
1202#define PM_PRESENT BIT_ULL(63)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001203
Matt Mackall85863e42008-02-04 22:29:04 -08001204#define PM_END_OF_BUFFER 1
1205
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001206static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001207{
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001208 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001209}
1210
1211static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
Matt Mackall85863e42008-02-04 22:29:04 -08001212 struct pagemapread *pm)
1213{
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001214 pm->buffer[pm->pos++] = *pme;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001215 if (pm->pos >= pm->len)
Thomas Tuttleaae86792008-06-05 22:46:31 -07001216 return PM_END_OF_BUFFER;
Matt Mackall85863e42008-02-04 22:29:04 -08001217 return 0;
1218}
1219
1220static int pagemap_pte_hole(unsigned long start, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -07001221 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001222{
Dave Hansen21650092008-06-12 15:21:47 -07001223 struct pagemapread *pm = walk->private;
Peter Feiner68b5a652014-08-06 16:08:09 -07001224 unsigned long addr = start;
Matt Mackall85863e42008-02-04 22:29:04 -08001225 int err = 0;
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001226
Peter Feiner68b5a652014-08-06 16:08:09 -07001227 while (addr < end) {
1228 struct vm_area_struct *vma = find_vma(walk->mm, addr);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001229 pagemap_entry_t pme = make_pme(0, 0);
Peter Feiner87e6d492014-09-25 16:05:18 -07001230 /* End of address space hole, which we mark as non-present. */
1231 unsigned long hole_end;
Peter Feiner68b5a652014-08-06 16:08:09 -07001232
Peter Feiner87e6d492014-09-25 16:05:18 -07001233 if (vma)
1234 hole_end = min(end, vma->vm_start);
1235 else
1236 hole_end = end;
1237
1238 for (; addr < hole_end; addr += PAGE_SIZE) {
1239 err = add_to_pagemap(addr, &pme, pm);
1240 if (err)
1241 goto out;
Peter Feiner68b5a652014-08-06 16:08:09 -07001242 }
1243
Peter Feiner87e6d492014-09-25 16:05:18 -07001244 if (!vma)
1245 break;
1246
1247 /* Addresses in the VMA. */
1248 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001249 pme = make_pme(0, PM_SOFT_DIRTY);
Peter Feiner87e6d492014-09-25 16:05:18 -07001250 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
Peter Feiner68b5a652014-08-06 16:08:09 -07001251 err = add_to_pagemap(addr, &pme, pm);
1252 if (err)
1253 goto out;
1254 }
Matt Mackall85863e42008-02-04 22:29:04 -08001255 }
Peter Feiner68b5a652014-08-06 16:08:09 -07001256out:
Matt Mackall85863e42008-02-04 22:29:04 -08001257 return err;
1258}
1259
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001260static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001261 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
Matt Mackall85863e42008-02-04 22:29:04 -08001262{
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001263 u64 frame = 0, flags = 0;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001264 struct page *page = NULL;
Matt Mackall85863e42008-02-04 22:29:04 -08001265
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001266 if (pte_present(pte)) {
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001267 if (pm->show_pfn)
1268 frame = pte_pfn(pte);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001269 flags |= PM_PRESENT;
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001270 page = _vm_normal_page(vma, addr, pte, true);
Cyrill Gorcunove9cdd6e2013-10-16 13:46:53 -07001271 if (pte_soft_dirty(pte))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001272 flags |= PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001273 } else if (is_swap_pte(pte)) {
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001274 swp_entry_t entry;
1275 if (pte_swp_soft_dirty(pte))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001276 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001277 entry = pte_to_swp_entry(pte);
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001278 frame = swp_type(entry) |
1279 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001280 flags |= PM_SWAP;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001281 if (is_migration_entry(entry))
1282 page = migration_entry_to_page(entry);
Jérôme Glisse5042db42017-09-08 16:11:43 -07001283
1284 if (is_device_private_entry(entry))
1285 page = device_private_entry_to_page(entry);
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001286 }
1287
1288 if (page && !PageAnon(page))
1289 flags |= PM_FILE;
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001290 if (page && page_mapcount(page) == 1)
1291 flags |= PM_MMAP_EXCLUSIVE;
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001292 if (vma->vm_flags & VM_SOFTDIRTY)
1293 flags |= PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001294
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001295 return make_pme(frame, flags);
Dave Hansenbcf80392008-06-12 15:21:48 -07001296}
1297
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001298static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -07001299 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001300{
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001301 struct vm_area_struct *vma = walk->vma;
Dave Hansen21650092008-06-12 15:21:47 -07001302 struct pagemapread *pm = walk->private;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001303 spinlock_t *ptl;
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001304 pte_t *pte, *orig_pte;
Matt Mackall85863e42008-02-04 22:29:04 -08001305 int err = 0;
1306
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001307#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001308 ptl = pmd_trans_huge_lock(pmdp, vma);
1309 if (ptl) {
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001310 u64 flags = 0, frame = 0;
1311 pmd_t pmd = *pmdp;
Zi Yan84c3fc42017-09-08 16:11:01 -07001312 struct page *page = NULL;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001313
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001314 if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(pmd))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001315 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001316
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001317 if (pmd_present(pmd)) {
Zi Yan84c3fc42017-09-08 16:11:01 -07001318 page = pmd_page(pmd);
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001319
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001320 flags |= PM_PRESENT;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001321 if (pm->show_pfn)
1322 frame = pmd_pfn(pmd) +
1323 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001324 }
Zi Yan84c3fc42017-09-08 16:11:01 -07001325#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1326 else if (is_swap_pmd(pmd)) {
1327 swp_entry_t entry = pmd_to_swp_entry(pmd);
1328
1329 frame = swp_type(entry) |
1330 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
1331 flags |= PM_SWAP;
1332 VM_BUG_ON(!is_pmd_migration_entry(pmd));
1333 page = migration_entry_to_page(entry);
1334 }
1335#endif
1336
1337 if (page && page_mapcount(page) == 1)
1338 flags |= PM_MMAP_EXCLUSIVE;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001339
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001340 for (; addr != end; addr += PAGE_SIZE) {
1341 pagemap_entry_t pme = make_pme(frame, flags);
1342
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001343 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001344 if (err)
1345 break;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001346 if (pm->show_pfn && (flags & PM_PRESENT))
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001347 frame++;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001348 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001349 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001350 return err;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001351 }
1352
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001353 if (pmd_trans_unstable(pmdp))
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07001354 return 0;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001355#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Dave Hansenbcf80392008-06-12 15:21:48 -07001356
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001357 /*
1358 * We can assume that @vma always points to a valid one and @end never
1359 * goes beyond vma->vm_end.
1360 */
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001361 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001362 for (; addr < end; pte++, addr += PAGE_SIZE) {
1363 pagemap_entry_t pme;
Peter Feiner81d0fa62014-10-09 15:28:32 -07001364
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001365 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001366 err = add_to_pagemap(addr, &pme, pm);
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001367 if (err)
Peter Feiner81d0fa62014-10-09 15:28:32 -07001368 break;
Matt Mackall85863e42008-02-04 22:29:04 -08001369 }
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001370 pte_unmap_unlock(orig_pte, ptl);
Matt Mackall85863e42008-02-04 22:29:04 -08001371
1372 cond_resched();
1373
1374 return err;
1375}
1376
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001377#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001378/* This function walks within one hugetlb entry in the single call */
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001379static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001380 unsigned long addr, unsigned long end,
1381 struct mm_walk *walk)
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001382{
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001383 struct pagemapread *pm = walk->private;
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001384 struct vm_area_struct *vma = walk->vma;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001385 u64 flags = 0, frame = 0;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001386 int err = 0;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001387 pte_t pte;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001388
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001389 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001390 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001391
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001392 pte = huge_ptep_get(ptep);
1393 if (pte_present(pte)) {
1394 struct page *page = pte_page(pte);
1395
1396 if (!PageAnon(page))
1397 flags |= PM_FILE;
1398
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001399 if (page_mapcount(page) == 1)
1400 flags |= PM_MMAP_EXCLUSIVE;
1401
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001402 flags |= PM_PRESENT;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001403 if (pm->show_pfn)
1404 frame = pte_pfn(pte) +
1405 ((addr & ~hmask) >> PAGE_SHIFT);
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001406 }
1407
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001408 for (; addr != end; addr += PAGE_SIZE) {
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001409 pagemap_entry_t pme = make_pme(frame, flags);
1410
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001411 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001412 if (err)
1413 return err;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001414 if (pm->show_pfn && (flags & PM_PRESENT))
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001415 frame++;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001416 }
1417
1418 cond_resched();
1419
1420 return err;
1421}
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001422#endif /* HUGETLB_PAGE */
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001423
Matt Mackall85863e42008-02-04 22:29:04 -08001424/*
1425 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1426 *
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001427 * For each page in the address space, this file contains one 64-bit entry
1428 * consisting of the following:
1429 *
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001430 * Bits 0-54 page frame number (PFN) if present
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001431 * Bits 0-4 swap type if swapped
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001432 * Bits 5-54 swap offset if swapped
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001433 * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001434 * Bit 56 page exclusively mapped
1435 * Bits 57-60 zero
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001436 * Bit 61 page is file-page or shared-anon
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001437 * Bit 62 page swapped
1438 * Bit 63 page present
1439 *
1440 * If the page is not present but in swap, then the PFN contains an
1441 * encoding of the swap file number and the page's offset into the
1442 * swap. Unmapped pages return a null PFN. This allows determining
Matt Mackall85863e42008-02-04 22:29:04 -08001443 * precisely which pages are mapped (or in swap) and comparing mapped
1444 * pages between processes.
1445 *
1446 * Efficient users of this interface will use /proc/pid/maps to
1447 * determine which areas of memory are actually mapped and llseek to
1448 * skip over unmapped regions.
1449 */
1450static ssize_t pagemap_read(struct file *file, char __user *buf,
1451 size_t count, loff_t *ppos)
1452{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001453 struct mm_struct *mm = file->private_data;
Matt Mackall85863e42008-02-04 22:29:04 -08001454 struct pagemapread pm;
Alexey Dobriyanee1e6ab2008-07-21 14:21:36 -07001455 struct mm_walk pagemap_walk = {};
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001456 unsigned long src;
1457 unsigned long svpfn;
1458 unsigned long start_vaddr;
1459 unsigned long end_vaddr;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001460 int ret = 0, copied = 0;
Matt Mackall85863e42008-02-04 22:29:04 -08001461
Vegard Nossum388f7932017-02-27 14:30:13 -08001462 if (!mm || !mmget_not_zero(mm))
Matt Mackall85863e42008-02-04 22:29:04 -08001463 goto out;
1464
Matt Mackall85863e42008-02-04 22:29:04 -08001465 ret = -EINVAL;
1466 /* file position must be aligned */
Thomas Tuttleaae86792008-06-05 22:46:31 -07001467 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001468 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001469
1470 ret = 0;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001471 if (!count)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001472 goto out_mm;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001473
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001474 /* do not disclose physical addresses: attack vector */
1475 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1476
yonghua zheng8c829622013-08-13 16:01:03 -07001477 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
Michal Hocko0ee931c2017-09-13 16:28:29 -07001478 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_KERNEL);
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001479 ret = -ENOMEM;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001480 if (!pm.buffer)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001481 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001482
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001483 pagemap_walk.pmd_entry = pagemap_pmd_range;
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001484 pagemap_walk.pte_hole = pagemap_pte_hole;
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001485#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001486 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001487#endif
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001488 pagemap_walk.mm = mm;
1489 pagemap_walk.private = &pm;
Matt Mackall85863e42008-02-04 22:29:04 -08001490
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001491 src = *ppos;
1492 svpfn = src / PM_ENTRY_BYTES;
1493 start_vaddr = svpfn << PAGE_SHIFT;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001494 end_vaddr = mm->task_size;
Matt Mackall85863e42008-02-04 22:29:04 -08001495
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001496 /* watch out for wraparound */
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001497 if (svpfn > mm->task_size >> PAGE_SHIFT)
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001498 start_vaddr = end_vaddr;
1499
1500 /*
1501 * The odds are that this will stop walking way
1502 * before end_vaddr, because the length of the
1503 * user buffer is tracked in "pm", and the walk
1504 * will stop when we hit the end of the buffer.
1505 */
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001506 ret = 0;
1507 while (count && (start_vaddr < end_vaddr)) {
1508 int len;
1509 unsigned long end;
Matt Mackall85863e42008-02-04 22:29:04 -08001510
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001511 pm.pos = 0;
Naoya Horiguchiea251c12010-11-24 12:57:13 -08001512 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001513 /* overflow ? */
1514 if (end < start_vaddr || end > end_vaddr)
1515 end = end_vaddr;
1516 down_read(&mm->mmap_sem);
1517 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1518 up_read(&mm->mmap_sem);
1519 start_vaddr = end;
1520
1521 len = min(count, PM_ENTRY_BYTES * pm.pos);
Dan Carpenter309361e02010-04-06 13:45:39 +03001522 if (copy_to_user(buf, pm.buffer, len)) {
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001523 ret = -EFAULT;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001524 goto out_free;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001525 }
1526 copied += len;
1527 buf += len;
1528 count -= len;
Matt Mackall85863e42008-02-04 22:29:04 -08001529 }
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001530 *ppos += copied;
1531 if (!ret || ret == PM_END_OF_BUFFER)
1532 ret = copied;
1533
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001534out_free:
1535 kfree(pm.buffer);
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001536out_mm:
1537 mmput(mm);
Matt Mackall85863e42008-02-04 22:29:04 -08001538out:
1539 return ret;
1540}
1541
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001542static int pagemap_open(struct inode *inode, struct file *file)
1543{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001544 struct mm_struct *mm;
1545
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001546 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1547 if (IS_ERR(mm))
1548 return PTR_ERR(mm);
1549 file->private_data = mm;
1550 return 0;
1551}
1552
1553static int pagemap_release(struct inode *inode, struct file *file)
1554{
1555 struct mm_struct *mm = file->private_data;
1556
1557 if (mm)
1558 mmdrop(mm);
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001559 return 0;
1560}
1561
Matt Mackall85863e42008-02-04 22:29:04 -08001562const struct file_operations proc_pagemap_operations = {
1563 .llseek = mem_lseek, /* borrow this */
1564 .read = pagemap_read,
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001565 .open = pagemap_open,
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001566 .release = pagemap_release,
Matt Mackall85863e42008-02-04 22:29:04 -08001567};
Matt Mackall1e883282008-02-04 22:29:07 -08001568#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall85863e42008-02-04 22:29:04 -08001569
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001570#ifdef CONFIG_NUMA
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001571
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001572struct numa_maps {
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001573 unsigned long pages;
1574 unsigned long anon;
1575 unsigned long active;
1576 unsigned long writeback;
1577 unsigned long mapcount_max;
1578 unsigned long dirty;
1579 unsigned long swapcache;
1580 unsigned long node[MAX_NUMNODES];
1581};
1582
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001583struct numa_maps_private {
1584 struct proc_maps_private proc_maps;
1585 struct numa_maps md;
1586};
1587
Dave Hanseneb4866d2011-09-20 15:19:38 -07001588static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1589 unsigned long nr_pages)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001590{
1591 int count = page_mapcount(page);
1592
Dave Hanseneb4866d2011-09-20 15:19:38 -07001593 md->pages += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001594 if (pte_dirty || PageDirty(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001595 md->dirty += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001596
1597 if (PageSwapCache(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001598 md->swapcache += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001599
1600 if (PageActive(page) || PageUnevictable(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001601 md->active += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001602
1603 if (PageWriteback(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001604 md->writeback += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001605
1606 if (PageAnon(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001607 md->anon += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001608
1609 if (count > md->mapcount_max)
1610 md->mapcount_max = count;
1611
Dave Hanseneb4866d2011-09-20 15:19:38 -07001612 md->node[page_to_nid(page)] += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001613}
1614
Dave Hansen3200a8a2011-09-20 15:19:39 -07001615static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1616 unsigned long addr)
1617{
1618 struct page *page;
1619 int nid;
1620
1621 if (!pte_present(pte))
1622 return NULL;
1623
1624 page = vm_normal_page(vma, addr, pte);
1625 if (!page)
1626 return NULL;
1627
1628 if (PageReserved(page))
1629 return NULL;
1630
1631 nid = page_to_nid(page);
Lai Jiangshan4ff1b2c2012-12-12 13:51:25 -08001632 if (!node_isset(nid, node_states[N_MEMORY]))
Dave Hansen3200a8a2011-09-20 15:19:39 -07001633 return NULL;
1634
1635 return page;
1636}
1637
Gerald Schaefer28093f92016-04-28 16:18:35 -07001638#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1639static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1640 struct vm_area_struct *vma,
1641 unsigned long addr)
1642{
1643 struct page *page;
1644 int nid;
1645
1646 if (!pmd_present(pmd))
1647 return NULL;
1648
1649 page = vm_normal_page_pmd(vma, addr, pmd);
1650 if (!page)
1651 return NULL;
1652
1653 if (PageReserved(page))
1654 return NULL;
1655
1656 nid = page_to_nid(page);
1657 if (!node_isset(nid, node_states[N_MEMORY]))
1658 return NULL;
1659
1660 return page;
1661}
1662#endif
1663
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001664static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1665 unsigned long end, struct mm_walk *walk)
1666{
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001667 struct numa_maps *md = walk->private;
1668 struct vm_area_struct *vma = walk->vma;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001669 spinlock_t *ptl;
1670 pte_t *orig_pte;
1671 pte_t *pte;
1672
Gerald Schaefer28093f92016-04-28 16:18:35 -07001673#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001674 ptl = pmd_trans_huge_lock(pmd, vma);
1675 if (ptl) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001676 struct page *page;
1677
Gerald Schaefer28093f92016-04-28 16:18:35 -07001678 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001679 if (page)
Gerald Schaefer28093f92016-04-28 16:18:35 -07001680 gather_stats(page, md, pmd_dirty(*pmd),
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001681 HPAGE_PMD_SIZE/PAGE_SIZE);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001682 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001683 return 0;
Dave Hansen32ef4382011-09-20 15:19:41 -07001684 }
1685
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001686 if (pmd_trans_unstable(pmd))
1687 return 0;
Gerald Schaefer28093f92016-04-28 16:18:35 -07001688#endif
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001689 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1690 do {
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001691 struct page *page = can_gather_numa_stats(*pte, vma, addr);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001692 if (!page)
1693 continue;
Dave Hanseneb4866d2011-09-20 15:19:38 -07001694 gather_stats(page, md, pte_dirty(*pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001695
1696 } while (pte++, addr += PAGE_SIZE, addr != end);
1697 pte_unmap_unlock(orig_pte, ptl);
Hugh Dickinsa66c04102016-12-12 16:44:47 -08001698 cond_resched();
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001699 return 0;
1700}
1701#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001702static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001703 unsigned long addr, unsigned long end, struct mm_walk *walk)
1704{
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001705 pte_t huge_pte = huge_ptep_get(pte);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001706 struct numa_maps *md;
1707 struct page *page;
1708
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001709 if (!pte_present(huge_pte))
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001710 return 0;
1711
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001712 page = pte_page(huge_pte);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001713 if (!page)
1714 return 0;
1715
1716 md = walk->private;
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001717 gather_stats(page, md, pte_dirty(huge_pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001718 return 0;
1719}
1720
1721#else
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001722static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001723 unsigned long addr, unsigned long end, struct mm_walk *walk)
1724{
1725 return 0;
1726}
1727#endif
1728
1729/*
1730 * Display pages allocated per node and memory policy via /proc.
1731 */
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001732static int show_numa_map(struct seq_file *m, void *v, int is_pid)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001733{
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001734 struct numa_maps_private *numa_priv = m->private;
1735 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001736 struct vm_area_struct *vma = v;
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001737 struct numa_maps *md = &numa_priv->md;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001738 struct file *file = vma->vm_file;
1739 struct mm_struct *mm = vma->vm_mm;
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001740 struct mm_walk walk = {
1741 .hugetlb_entry = gather_hugetlb_stats,
1742 .pmd_entry = gather_pte_stats,
1743 .private = md,
1744 .mm = mm,
1745 };
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001746 struct mempolicy *pol;
David Rientjes948927e2013-11-12 15:07:28 -08001747 char buffer[64];
1748 int nid;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001749
1750 if (!mm)
1751 return 0;
1752
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001753 /* Ensure we start with an empty set of numa_maps statistics. */
1754 memset(md, 0, sizeof(*md));
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001755
Oleg Nesterov498f2372014-10-09 15:27:52 -07001756 pol = __get_vma_policy(vma, vma->vm_start);
1757 if (pol) {
1758 mpol_to_str(buffer, sizeof(buffer), pol);
1759 mpol_cond_put(pol);
1760 } else {
1761 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1762 }
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001763
1764 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1765
1766 if (file) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001767 seq_puts(m, " file=");
Miklos Szeredi2726d562015-06-19 10:30:28 +02001768 seq_file_path(m, file, "\n\t= ");
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001769 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001770 seq_puts(m, " heap");
Michal Hocko1240ea02017-09-08 16:13:35 -07001771 } else if (is_stack(vma)) {
Johannes Weiner65376df2016-02-02 16:57:29 -08001772 seq_puts(m, " stack");
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001773 }
1774
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001775 if (is_vm_hugetlb_page(vma))
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001776 seq_puts(m, " huge");
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001777
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001778 /* mmap_sem is held by m_start */
1779 walk_page_vma(vma, &walk);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001780
1781 if (!md->pages)
1782 goto out;
1783
1784 if (md->anon)
1785 seq_printf(m, " anon=%lu", md->anon);
1786
1787 if (md->dirty)
1788 seq_printf(m, " dirty=%lu", md->dirty);
1789
1790 if (md->pages != md->anon && md->pages != md->dirty)
1791 seq_printf(m, " mapped=%lu", md->pages);
1792
1793 if (md->mapcount_max > 1)
1794 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1795
1796 if (md->swapcache)
1797 seq_printf(m, " swapcache=%lu", md->swapcache);
1798
1799 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1800 seq_printf(m, " active=%lu", md->active);
1801
1802 if (md->writeback)
1803 seq_printf(m, " writeback=%lu", md->writeback);
1804
David Rientjes948927e2013-11-12 15:07:28 -08001805 for_each_node_state(nid, N_MEMORY)
1806 if (md->node[nid])
1807 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
Rafael Aquini198d1592015-02-12 15:01:08 -08001808
1809 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001810out:
1811 seq_putc(m, '\n');
Oleg Nesterovb8c20a92014-10-09 15:25:41 -07001812 m_cache_vma(m, vma);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001813 return 0;
1814}
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001815
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001816static int show_pid_numa_map(struct seq_file *m, void *v)
1817{
1818 return show_numa_map(m, v, 1);
1819}
1820
1821static int show_tid_numa_map(struct seq_file *m, void *v)
1822{
1823 return show_numa_map(m, v, 0);
1824}
1825
Jan Engelhardt03a44822008-02-08 04:21:19 -08001826static const struct seq_operations proc_pid_numa_maps_op = {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001827 .start = m_start,
1828 .next = m_next,
1829 .stop = m_stop,
1830 .show = show_pid_numa_map,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001831};
Eric W. Biederman662795d2006-06-26 00:25:48 -07001832
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001833static const struct seq_operations proc_tid_numa_maps_op = {
1834 .start = m_start,
1835 .next = m_next,
1836 .stop = m_stop,
1837 .show = show_tid_numa_map,
1838};
1839
1840static int numa_maps_open(struct inode *inode, struct file *file,
1841 const struct seq_operations *ops)
Eric W. Biederman662795d2006-06-26 00:25:48 -07001842{
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -07001843 return proc_maps_open(inode, file, ops,
1844 sizeof(struct numa_maps_private));
Eric W. Biederman662795d2006-06-26 00:25:48 -07001845}
1846
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001847static int pid_numa_maps_open(struct inode *inode, struct file *file)
1848{
1849 return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1850}
1851
1852static int tid_numa_maps_open(struct inode *inode, struct file *file)
1853{
1854 return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1855}
1856
1857const struct file_operations proc_pid_numa_maps_operations = {
1858 .open = pid_numa_maps_open,
1859 .read = seq_read,
1860 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -07001861 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001862};
1863
1864const struct file_operations proc_tid_numa_maps_operations = {
1865 .open = tid_numa_maps_open,
Eric W. Biederman662795d2006-06-26 00:25:48 -07001866 .read = seq_read,
1867 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -07001868 .release = proc_map_release,
Eric W. Biederman662795d2006-06-26 00:25:48 -07001869};
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001870#endif /* CONFIG_NUMA */