blob: f0c8b33d99b137e0c607448237216c2826df3fe8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/mm.h>
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002#include <linux/vmacache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/hugetlb.h>
Dave Hansen22e057c2011-03-22 16:33:00 -07004#include <linux/huge_mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/mount.h>
6#include <linux/seq_file.h>
Mauricio Line070ad42005-09-03 15:55:10 -07007#include <linux/highmem.h>
Kees Cook5096add2007-05-08 00:26:04 -07008#include <linux/ptrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Christoph Lameter6e21c8f2005-09-03 15:54:45 -070010#include <linux/pagemap.h>
11#include <linux/mempolicy.h>
Dave Hansen22e057c2011-03-22 16:33:00 -070012#include <linux/rmap.h>
Matt Mackall85863e42008-02-04 22:29:04 -080013#include <linux/swap.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010014#include <linux/sched/mm.h>
Matt Mackall85863e42008-02-04 22:29:04 -080015#include <linux/swapops.h>
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -070016#include <linux/mmu_notifier.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070017#include <linux/page_idle.h>
Vlastimil Babka6a15a372016-01-14 15:19:20 -080018#include <linux/shmem_fs.h>
Mauricio Line070ad42005-09-03 15:55:10 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/elf.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h>
Mauricio Line070ad42005-09-03 15:55:10 -070022#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "internal.h"
24
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080025void task_mem(struct seq_file *m, struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Konstantin Khlebnikov84638332016-01-14 15:22:07 -080027 unsigned long text, lib, swap, ptes, pmds, anon, file, shmem;
Hugh Dickins365e9c872005-10-29 18:16:18 -070028 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
29
Jerome Marchand8cee8522016-01-14 15:19:29 -080030 anon = get_mm_counter(mm, MM_ANONPAGES);
31 file = get_mm_counter(mm, MM_FILEPAGES);
32 shmem = get_mm_counter(mm, MM_SHMEMPAGES);
33
Hugh Dickins365e9c872005-10-29 18:16:18 -070034 /*
35 * Note: to minimize their overhead, mm maintains hiwater_vm and
36 * hiwater_rss only when about to *lower* total_vm or rss. Any
37 * collector of these hiwater stats must therefore get total_vm
38 * and rss too, which will usually be the higher. Barriers? not
39 * worth the effort, such snapshots can always be inconsistent.
40 */
41 hiwater_vm = total_vm = mm->total_vm;
42 if (hiwater_vm < mm->hiwater_vm)
43 hiwater_vm = mm->hiwater_vm;
Jerome Marchand8cee8522016-01-14 15:19:29 -080044 hiwater_rss = total_rss = anon + file + shmem;
Hugh Dickins365e9c872005-10-29 18:16:18 -070045 if (hiwater_rss < mm->hiwater_rss)
46 hiwater_rss = mm->hiwater_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
49 lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080050 swap = get_mm_counter(mm, MM_SWAPENTS);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -080051 ptes = PTRS_PER_PTE * sizeof(pte_t) * atomic_long_read(&mm->nr_ptes);
52 pmds = PTRS_PER_PMD * sizeof(pmd_t) * mm_nr_pmds(mm);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080053 seq_printf(m,
Hugh Dickins365e9c872005-10-29 18:16:18 -070054 "VmPeak:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 "VmSize:\t%8lu kB\n"
56 "VmLck:\t%8lu kB\n"
Christoph Lameterbc3e53f2011-10-31 17:07:30 -070057 "VmPin:\t%8lu kB\n"
Hugh Dickins365e9c872005-10-29 18:16:18 -070058 "VmHWM:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 "VmRSS:\t%8lu kB\n"
Jerome Marchand8cee8522016-01-14 15:19:29 -080060 "RssAnon:\t%8lu kB\n"
61 "RssFile:\t%8lu kB\n"
62 "RssShmem:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 "VmData:\t%8lu kB\n"
64 "VmStk:\t%8lu kB\n"
65 "VmExe:\t%8lu kB\n"
66 "VmLib:\t%8lu kB\n"
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080067 "VmPTE:\t%8lu kB\n"
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -080068 "VmPMD:\t%8lu kB\n"
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080069 "VmSwap:\t%8lu kB\n",
Hugh Dickins365e9c872005-10-29 18:16:18 -070070 hiwater_vm << (PAGE_SHIFT-10),
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -070071 total_vm << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 mm->locked_vm << (PAGE_SHIFT-10),
Christoph Lameterbc3e53f2011-10-31 17:07:30 -070073 mm->pinned_vm << (PAGE_SHIFT-10),
Hugh Dickins365e9c872005-10-29 18:16:18 -070074 hiwater_rss << (PAGE_SHIFT-10),
75 total_rss << (PAGE_SHIFT-10),
Jerome Marchand8cee8522016-01-14 15:19:29 -080076 anon << (PAGE_SHIFT-10),
77 file << (PAGE_SHIFT-10),
78 shmem << (PAGE_SHIFT-10),
Konstantin Khlebnikov84638332016-01-14 15:22:07 -080079 mm->data_vm << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -080081 ptes >> 10,
82 pmds >> 10,
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080083 swap << (PAGE_SHIFT-10));
Naoya Horiguchi5d317b22015-11-05 18:47:14 -080084 hugetlb_report_usage(m, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87unsigned long task_vsize(struct mm_struct *mm)
88{
89 return PAGE_SIZE * mm->total_vm;
90}
91
Alexey Dobriyana2ade7b2011-01-12 17:00:32 -080092unsigned long task_statm(struct mm_struct *mm,
93 unsigned long *shared, unsigned long *text,
94 unsigned long *data, unsigned long *resident)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Jerome Marchandeca56ff2016-01-14 15:19:26 -080096 *shared = get_mm_counter(mm, MM_FILEPAGES) +
97 get_mm_counter(mm, MM_SHMEMPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
99 >> PAGE_SHIFT;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -0800100 *data = mm->data_vm + mm->stack_vm;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800101 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return mm->total_vm;
103}
104
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900105#ifdef CONFIG_NUMA
106/*
Oleg Nesterov498f2372014-10-09 15:27:52 -0700107 * Save get_task_policy() for show_numa_map().
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900108 */
109static void hold_task_mempolicy(struct proc_maps_private *priv)
110{
111 struct task_struct *task = priv->task;
112
113 task_lock(task);
Oleg Nesterov498f2372014-10-09 15:27:52 -0700114 priv->task_mempolicy = get_task_policy(task);
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900115 mpol_get(priv->task_mempolicy);
116 task_unlock(task);
117}
118static void release_task_mempolicy(struct proc_maps_private *priv)
119{
120 mpol_put(priv->task_mempolicy);
121}
122#else
123static void hold_task_mempolicy(struct proc_maps_private *priv)
124{
125}
126static void release_task_mempolicy(struct proc_maps_private *priv)
127{
128}
129#endif
130
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700131static void vma_stop(struct proc_maps_private *priv)
Mauricio Line070ad42005-09-03 15:55:10 -0700132{
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700133 struct mm_struct *mm = priv->mm;
134
135 release_task_mempolicy(priv);
136 up_read(&mm->mmap_sem);
137 mmput(mm);
Matt Mackalla6198792008-02-04 22:29:03 -0800138}
139
Oleg Nesterovad2a00e2014-10-09 15:25:39 -0700140static struct vm_area_struct *
141m_next_vma(struct proc_maps_private *priv, struct vm_area_struct *vma)
142{
143 if (vma == priv->tail_vma)
144 return NULL;
145 return vma->vm_next ?: priv->tail_vma;
146}
147
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700148static void m_cache_vma(struct seq_file *m, struct vm_area_struct *vma)
149{
150 if (m->count < m->size) /* vma is copied successfully */
Robert Ho855af072016-10-07 17:02:36 -0700151 m->version = m_next_vma(m->private, vma) ? vma->vm_end : -1UL;
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700152}
153
Oleg Nesterov0c255322014-10-09 15:25:36 -0700154static void *m_start(struct seq_file *m, loff_t *ppos)
Matt Mackalla6198792008-02-04 22:29:03 -0800155{
156 struct proc_maps_private *priv = m->private;
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700157 unsigned long last_addr = m->version;
Matt Mackalla6198792008-02-04 22:29:03 -0800158 struct mm_struct *mm;
Oleg Nesterov0c255322014-10-09 15:25:36 -0700159 struct vm_area_struct *vma;
160 unsigned int pos = *ppos;
Matt Mackalla6198792008-02-04 22:29:03 -0800161
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700162 /* See m_cache_vma(). Zero at the start or after lseek. */
163 if (last_addr == -1UL)
164 return NULL;
165
Oleg Nesterov2c033762014-10-09 15:25:51 -0700166 priv->task = get_proc_task(priv->inode);
Matt Mackalla6198792008-02-04 22:29:03 -0800167 if (!priv->task)
Al Viroec6fd8a2011-02-15 22:22:54 -0500168 return ERR_PTR(-ESRCH);
Matt Mackalla6198792008-02-04 22:29:03 -0800169
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700170 mm = priv->mm;
Vegard Nossum388f7932017-02-27 14:30:13 -0800171 if (!mm || !mmget_not_zero(mm))
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700172 return NULL;
Oleg Nesterov0c255322014-10-09 15:25:36 -0700173
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200174 down_read(&mm->mmap_sem);
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900175 hold_task_mempolicy(priv);
Oleg Nesterov0c255322014-10-09 15:25:36 -0700176 priv->tail_vma = get_gate_vma(mm);
Matt Mackalla6198792008-02-04 22:29:03 -0800177
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700178 if (last_addr) {
Robert Ho855af072016-10-07 17:02:36 -0700179 vma = find_vma(mm, last_addr - 1);
180 if (vma && vma->vm_start <= last_addr)
181 vma = m_next_vma(priv, vma);
182 if (vma)
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700183 return vma;
184 }
185
186 m->version = 0;
Oleg Nesterov0c255322014-10-09 15:25:36 -0700187 if (pos < mm->map_count) {
Oleg Nesterov557c2d82014-10-09 15:25:43 -0700188 for (vma = mm->mmap; pos; pos--) {
189 m->version = vma->vm_start;
Matt Mackalla6198792008-02-04 22:29:03 -0800190 vma = vma->vm_next;
Oleg Nesterov557c2d82014-10-09 15:25:43 -0700191 }
Oleg Nesterov0c255322014-10-09 15:25:36 -0700192 return vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800193 }
194
Oleg Nesterov557c2d82014-10-09 15:25:43 -0700195 /* we do not bother to update m->version in this case */
Oleg Nesterov0c255322014-10-09 15:25:36 -0700196 if (pos == mm->map_count && priv->tail_vma)
197 return priv->tail_vma;
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700198
199 vma_stop(priv);
200 return NULL;
Matt Mackalla6198792008-02-04 22:29:03 -0800201}
202
203static void *m_next(struct seq_file *m, void *v, loff_t *pos)
204{
205 struct proc_maps_private *priv = m->private;
Oleg Nesterovad2a00e2014-10-09 15:25:39 -0700206 struct vm_area_struct *next;
Matt Mackalla6198792008-02-04 22:29:03 -0800207
208 (*pos)++;
Oleg Nesterovad2a00e2014-10-09 15:25:39 -0700209 next = m_next_vma(priv, v);
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700210 if (!next)
211 vma_stop(priv);
212 return next;
Matt Mackalla6198792008-02-04 22:29:03 -0800213}
214
215static void m_stop(struct seq_file *m, void *v)
216{
217 struct proc_maps_private *priv = m->private;
Matt Mackalla6198792008-02-04 22:29:03 -0800218
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700219 if (!IS_ERR_OR_NULL(v))
220 vma_stop(priv);
Oleg Nesterov0d5f5f42014-10-09 15:25:32 -0700221 if (priv->task) {
Matt Mackalla6198792008-02-04 22:29:03 -0800222 put_task_struct(priv->task);
Oleg Nesterov0d5f5f42014-10-09 15:25:32 -0700223 priv->task = NULL;
224 }
Matt Mackalla6198792008-02-04 22:29:03 -0800225}
226
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700227static int proc_maps_open(struct inode *inode, struct file *file,
228 const struct seq_operations *ops, int psize)
229{
230 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
231
232 if (!priv)
233 return -ENOMEM;
234
Oleg Nesterov2c033762014-10-09 15:25:51 -0700235 priv->inode = inode;
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700236 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
237 if (IS_ERR(priv->mm)) {
238 int err = PTR_ERR(priv->mm);
239
240 seq_release_private(inode, file);
241 return err;
242 }
243
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700244 return 0;
245}
246
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700247static int proc_map_release(struct inode *inode, struct file *file)
248{
249 struct seq_file *seq = file->private_data;
250 struct proc_maps_private *priv = seq->private;
251
252 if (priv->mm)
253 mmdrop(priv->mm);
254
255 return seq_release_private(inode, file);
256}
257
Matt Mackalla6198792008-02-04 22:29:03 -0800258static int do_maps_open(struct inode *inode, struct file *file,
Jan Engelhardt03a44822008-02-08 04:21:19 -0800259 const struct seq_operations *ops)
Matt Mackalla6198792008-02-04 22:29:03 -0800260{
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700261 return proc_maps_open(inode, file, ops,
262 sizeof(struct proc_maps_private));
Matt Mackalla6198792008-02-04 22:29:03 -0800263}
Mauricio Line070ad42005-09-03 15:55:10 -0700264
Johannes Weiner65376df2016-02-02 16:57:29 -0800265/*
266 * Indicate if the VMA is a stack for the given task; for
267 * /proc/PID/maps that is the stack of the main task.
268 */
269static int is_stack(struct proc_maps_private *priv,
Andy Lutomirskib18cb642016-09-30 10:58:57 -0700270 struct vm_area_struct *vma)
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700271{
Andy Lutomirskib18cb642016-09-30 10:58:57 -0700272 /*
273 * We make no effort to guess what a given thread considers to be
274 * its "stack". It's not even well-defined for programs written
275 * languages like Go.
276 */
277 return vma->vm_start <= vma->vm_mm->start_stack &&
278 vma->vm_end >= vma->vm_mm->start_stack;
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700279}
280
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700281static void
282show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Mauricio Line070ad42005-09-03 15:55:10 -0700284 struct mm_struct *mm = vma->vm_mm;
285 struct file *file = vma->vm_file;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700286 struct proc_maps_private *priv = m->private;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900287 vm_flags_t flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 unsigned long ino = 0;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700289 unsigned long long pgoff = 0;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200290 unsigned long start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 dev_t dev = 0;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700292 const char *name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 if (file) {
Al Viro496ad9a2013-01-23 17:07:38 -0500295 struct inode *inode = file_inode(vma->vm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 dev = inode->i_sb->s_dev;
297 ino = inode->i_ino;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700298 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
Linus Torvaldsd7824372010-08-15 11:35:52 -0700301 /* We don't show the stack guard page in /proc/maps */
302 start = vma->vm_start;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200303 if (stack_guard_page_start(vma, start))
304 start += PAGE_SIZE;
305 end = vma->vm_end;
306 if (stack_guard_page_end(vma, end))
307 end -= PAGE_SIZE;
Linus Torvaldsd7824372010-08-15 11:35:52 -0700308
Tetsuo Handa652586d2013-11-14 14:31:57 -0800309 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
310 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
Linus Torvaldsd7824372010-08-15 11:35:52 -0700311 start,
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200312 end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 flags & VM_READ ? 'r' : '-',
314 flags & VM_WRITE ? 'w' : '-',
315 flags & VM_EXEC ? 'x' : '-',
316 flags & VM_MAYSHARE ? 's' : 'p',
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700317 pgoff,
Tetsuo Handa652586d2013-11-14 14:31:57 -0800318 MAJOR(dev), MINOR(dev), ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /*
321 * Print the dentry name for named mappings, and a
322 * special [heap] marker for the heap:
323 */
Mauricio Line070ad42005-09-03 15:55:10 -0700324 if (file) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800325 seq_pad(m, ' ');
Miklos Szeredi2726d562015-06-19 10:30:28 +0200326 seq_file_path(m, file, "\n");
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700327 goto done;
328 }
329
Andy Lutomirski78d683e2014-05-19 15:58:32 -0700330 if (vma->vm_ops && vma->vm_ops->name) {
331 name = vma->vm_ops->name(vma);
332 if (name)
333 goto done;
334 }
335
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700336 name = arch_vma_name(vma);
337 if (!name) {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700338 if (!mm) {
339 name = "[vdso]";
340 goto done;
341 }
342
343 if (vma->vm_start <= mm->brk &&
344 vma->vm_end >= mm->start_brk) {
345 name = "[heap]";
346 goto done;
347 }
348
Andy Lutomirskib18cb642016-09-30 10:58:57 -0700349 if (is_stack(priv, vma))
Johannes Weiner65376df2016-02-02 16:57:29 -0800350 name = "[stack]";
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700351 }
352
353done:
354 if (name) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800355 seq_pad(m, ' ');
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700356 seq_puts(m, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358 seq_putc(m, '\n');
Joe Korty7c88db02008-10-16 15:27:09 +0400359}
360
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700361static int show_map(struct seq_file *m, void *v, int is_pid)
Joe Korty7c88db02008-10-16 15:27:09 +0400362{
Oleg Nesterovebb6cdd2014-10-09 15:25:34 -0700363 show_map_vma(m, v, is_pid);
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700364 m_cache_vma(m, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366}
367
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700368static int show_pid_map(struct seq_file *m, void *v)
369{
370 return show_map(m, v, 1);
371}
372
373static int show_tid_map(struct seq_file *m, void *v)
374{
375 return show_map(m, v, 0);
376}
377
Jan Engelhardt03a44822008-02-08 04:21:19 -0800378static const struct seq_operations proc_pid_maps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800379 .start = m_start,
380 .next = m_next,
381 .stop = m_stop,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700382 .show = show_pid_map
Matt Mackalla6198792008-02-04 22:29:03 -0800383};
384
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700385static const struct seq_operations proc_tid_maps_op = {
386 .start = m_start,
387 .next = m_next,
388 .stop = m_stop,
389 .show = show_tid_map
390};
391
392static int pid_maps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800393{
394 return do_maps_open(inode, file, &proc_pid_maps_op);
395}
396
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700397static int tid_maps_open(struct inode *inode, struct file *file)
398{
399 return do_maps_open(inode, file, &proc_tid_maps_op);
400}
401
402const struct file_operations proc_pid_maps_operations = {
403 .open = pid_maps_open,
404 .read = seq_read,
405 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700406 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700407};
408
409const struct file_operations proc_tid_maps_operations = {
410 .open = tid_maps_open,
Matt Mackalla6198792008-02-04 22:29:03 -0800411 .read = seq_read,
412 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700413 .release = proc_map_release,
Matt Mackalla6198792008-02-04 22:29:03 -0800414};
415
416/*
417 * Proportional Set Size(PSS): my share of RSS.
418 *
419 * PSS of a process is the count of pages it has in memory, where each
420 * page is divided by the number of processes sharing it. So if a
421 * process has 1000 pages all to itself, and 1000 shared with one other
422 * process, its PSS will be 1500.
423 *
424 * To keep (accumulated) division errors low, we adopt a 64bit
425 * fixed-point pss counter to minimize division errors. So (pss >>
426 * PSS_SHIFT) would be the real byte count.
427 *
428 * A shift of 12 before division means (assuming 4K page size):
429 * - 1M 3-user-pages add up to 8KB errors;
430 * - supports mapcount up to 2^24, or 16M;
431 * - supports PSS up to 2^52 bytes, or 4PB.
432 */
433#define PSS_SHIFT 12
434
Matt Mackall1e883282008-02-04 22:29:07 -0800435#ifdef CONFIG_PROC_PAGE_MONITOR
Peter Zijlstra214e4712008-04-28 02:12:55 -0700436struct mem_size_stats {
Matt Mackalla6198792008-02-04 22:29:03 -0800437 unsigned long resident;
438 unsigned long shared_clean;
439 unsigned long shared_dirty;
440 unsigned long private_clean;
441 unsigned long private_dirty;
442 unsigned long referenced;
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700443 unsigned long anonymous;
Shaohua Licf8496e2017-05-03 14:52:42 -0700444 unsigned long lazyfree;
Dave Hansen4031a212011-03-22 16:33:01 -0700445 unsigned long anonymous_thp;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700446 unsigned long shmem_thp;
Peter Zijlstra214e4712008-04-28 02:12:55 -0700447 unsigned long swap;
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800448 unsigned long shared_hugetlb;
449 unsigned long private_hugetlb;
Matt Mackalla6198792008-02-04 22:29:03 -0800450 u64 pss;
Minchan Kim8334b962015-09-08 15:00:24 -0700451 u64 swap_pss;
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800452 bool check_shmem_swap;
Matt Mackalla6198792008-02-04 22:29:03 -0800453};
454
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800455static void smaps_account(struct mem_size_stats *mss, struct page *page,
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800456 bool compound, bool young, bool dirty)
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800457{
Kirill A. Shutemovf4be6152016-01-20 14:58:12 -0800458 int i, nr = compound ? 1 << compound_order(page) : 1;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800459 unsigned long size = nr * PAGE_SIZE;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700460
Shaohua Licf8496e2017-05-03 14:52:42 -0700461 if (PageAnon(page)) {
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800462 mss->anonymous += size;
Shaohua Licf8496e2017-05-03 14:52:42 -0700463 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
464 mss->lazyfree += size;
465 }
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800466
467 mss->resident += size;
468 /* Accumulate the size in pages that have been accessed. */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700469 if (young || page_is_young(page) || PageReferenced(page))
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800470 mss->referenced += size;
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800471
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800472 /*
473 * page_count(page) == 1 guarantees the page is mapped exactly once.
474 * If any subpage of the compound page mapped with PTE it would elevate
475 * page_count().
476 */
477 if (page_count(page) == 1) {
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800478 if (dirty || PageDirty(page))
479 mss->private_dirty += size;
480 else
481 mss->private_clean += size;
482 mss->pss += (u64)size << PSS_SHIFT;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800483 return;
484 }
485
486 for (i = 0; i < nr; i++, page++) {
487 int mapcount = page_mapcount(page);
488
489 if (mapcount >= 2) {
490 if (dirty || PageDirty(page))
491 mss->shared_dirty += PAGE_SIZE;
492 else
493 mss->shared_clean += PAGE_SIZE;
494 mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount;
495 } else {
496 if (dirty || PageDirty(page))
497 mss->private_dirty += PAGE_SIZE;
498 else
499 mss->private_clean += PAGE_SIZE;
500 mss->pss += PAGE_SIZE << PSS_SHIFT;
501 }
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800502 }
503}
504
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800505#ifdef CONFIG_SHMEM
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800506static int smaps_pte_hole(unsigned long addr, unsigned long end,
507 struct mm_walk *walk)
508{
509 struct mem_size_stats *mss = walk->private;
510
Vlastimil Babka48131e02016-01-14 15:19:23 -0800511 mss->swap += shmem_partial_swap_usage(
512 walk->vma->vm_file->f_mapping, addr, end);
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800513
514 return 0;
515}
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800516#endif
517
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800518static void smaps_pte_entry(pte_t *pte, unsigned long addr,
519 struct mm_walk *walk)
Dave Hansenae11c4d2011-03-22 16:32:58 -0700520{
521 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800522 struct vm_area_struct *vma = walk->vma;
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700523 struct page *page = NULL;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700524
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800525 if (pte_present(*pte)) {
526 page = vm_normal_page(vma, addr, *pte);
527 } else if (is_swap_pte(*pte)) {
528 swp_entry_t swpent = pte_to_swp_entry(*pte);
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700529
Minchan Kim8334b962015-09-08 15:00:24 -0700530 if (!non_swap_entry(swpent)) {
531 int mapcount;
532
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800533 mss->swap += PAGE_SIZE;
Minchan Kim8334b962015-09-08 15:00:24 -0700534 mapcount = swp_swapcount(swpent);
535 if (mapcount >= 2) {
536 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
537
538 do_div(pss_delta, mapcount);
539 mss->swap_pss += pss_delta;
540 } else {
541 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
542 }
543 } else if (is_migration_entry(swpent))
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700544 page = migration_entry_to_page(swpent);
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800545 } else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
546 && pte_none(*pte))) {
Vlastimil Babka48131e02016-01-14 15:19:23 -0800547 page = find_get_entry(vma->vm_file->f_mapping,
548 linear_page_index(vma, addr));
549 if (!page)
550 return;
551
552 if (radix_tree_exceptional_entry(page))
553 mss->swap += PAGE_SIZE;
554 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300555 put_page(page);
Vlastimil Babka48131e02016-01-14 15:19:23 -0800556
557 return;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700558 }
559
Dave Hansenae11c4d2011-03-22 16:32:58 -0700560 if (!page)
561 return;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800562
563 smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte));
Dave Hansenae11c4d2011-03-22 16:32:58 -0700564}
565
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800566#ifdef CONFIG_TRANSPARENT_HUGEPAGE
567static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
568 struct mm_walk *walk)
569{
570 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800571 struct vm_area_struct *vma = walk->vma;
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800572 struct page *page;
573
574 /* FOLL_DUMP will return -EFAULT on huge zero page */
575 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
576 if (IS_ERR_OR_NULL(page))
577 return;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700578 if (PageAnon(page))
579 mss->anonymous_thp += HPAGE_PMD_SIZE;
580 else if (PageSwapBacked(page))
581 mss->shmem_thp += HPAGE_PMD_SIZE;
Dan Williamsca120cf2016-09-03 10:38:03 -0700582 else if (is_zone_device_page(page))
583 /* pass */;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700584 else
585 VM_BUG_ON_PAGE(1, page);
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800586 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd));
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800587}
588#else
589static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
590 struct mm_walk *walk)
591{
592}
593#endif
594
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800595static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700596 struct mm_walk *walk)
Mauricio Line070ad42005-09-03 15:55:10 -0700597{
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800598 struct vm_area_struct *vma = walk->vma;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700599 pte_t *pte;
Hugh Dickins705e87c2005-10-29 18:16:27 -0700600 spinlock_t *ptl;
Mauricio Line070ad42005-09-03 15:55:10 -0700601
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -0800602 ptl = pmd_trans_huge_lock(pmd, vma);
603 if (ptl) {
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800604 smaps_pmd_entry(pmd, addr, walk);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -0800605 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -0700606 return 0;
Dave Hansen22e057c2011-03-22 16:33:00 -0700607 }
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700608
609 if (pmd_trans_unstable(pmd))
610 return 0;
Dave Hansen22e057c2011-03-22 16:33:00 -0700611 /*
612 * The mmap_sem held all the way back in m_start() is what
613 * keeps khugepaged out of here and from collapsing things
614 * in here.
615 */
Hugh Dickins705e87c2005-10-29 18:16:27 -0700616 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700617 for (; addr != end; pte++, addr += PAGE_SIZE)
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800618 smaps_pte_entry(pte, addr, walk);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700619 pte_unmap_unlock(pte - 1, ptl);
620 cond_resched();
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800621 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700622}
623
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800624static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
625{
626 /*
627 * Don't forget to update Documentation/ on changes.
628 */
629 static const char mnemonics[BITS_PER_LONG][2] = {
630 /*
631 * In case if we meet a flag we don't know about.
632 */
633 [0 ... (BITS_PER_LONG-1)] = "??",
634
635 [ilog2(VM_READ)] = "rd",
636 [ilog2(VM_WRITE)] = "wr",
637 [ilog2(VM_EXEC)] = "ex",
638 [ilog2(VM_SHARED)] = "sh",
639 [ilog2(VM_MAYREAD)] = "mr",
640 [ilog2(VM_MAYWRITE)] = "mw",
641 [ilog2(VM_MAYEXEC)] = "me",
642 [ilog2(VM_MAYSHARE)] = "ms",
643 [ilog2(VM_GROWSDOWN)] = "gd",
644 [ilog2(VM_PFNMAP)] = "pf",
645 [ilog2(VM_DENYWRITE)] = "dw",
Qiaowei Ren4aae7e42014-11-14 07:18:25 -0800646#ifdef CONFIG_X86_INTEL_MPX
647 [ilog2(VM_MPX)] = "mp",
648#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800649 [ilog2(VM_LOCKED)] = "lo",
650 [ilog2(VM_IO)] = "io",
651 [ilog2(VM_SEQ_READ)] = "sr",
652 [ilog2(VM_RAND_READ)] = "rr",
653 [ilog2(VM_DONTCOPY)] = "dc",
654 [ilog2(VM_DONTEXPAND)] = "de",
655 [ilog2(VM_ACCOUNT)] = "ac",
656 [ilog2(VM_NORESERVE)] = "nr",
657 [ilog2(VM_HUGETLB)] = "ht",
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800658 [ilog2(VM_ARCH_1)] = "ar",
659 [ilog2(VM_DONTDUMP)] = "dd",
Naoya Horiguchiec8e41a2013-11-12 15:07:49 -0800660#ifdef CONFIG_MEM_SOFT_DIRTY
661 [ilog2(VM_SOFTDIRTY)] = "sd",
662#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800663 [ilog2(VM_MIXEDMAP)] = "mm",
664 [ilog2(VM_HUGEPAGE)] = "hg",
665 [ilog2(VM_NOHUGEPAGE)] = "nh",
666 [ilog2(VM_MERGEABLE)] = "mg",
Andrea Arcangeli16ba6f82015-09-04 15:46:17 -0700667 [ilog2(VM_UFFD_MISSING)]= "um",
668 [ilog2(VM_UFFD_WP)] = "uw",
Dave Hansenc1192f82016-02-12 13:02:27 -0800669#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
670 /* These come out via ProtectionKey: */
671 [ilog2(VM_PKEY_BIT0)] = "",
672 [ilog2(VM_PKEY_BIT1)] = "",
673 [ilog2(VM_PKEY_BIT2)] = "",
674 [ilog2(VM_PKEY_BIT3)] = "",
675#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800676 };
677 size_t i;
678
679 seq_puts(m, "VmFlags: ");
680 for (i = 0; i < BITS_PER_LONG; i++) {
Dave Hansenc1192f82016-02-12 13:02:27 -0800681 if (!mnemonics[i][0])
682 continue;
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800683 if (vma->vm_flags & (1UL << i)) {
684 seq_printf(m, "%c%c ",
685 mnemonics[i][0], mnemonics[i][1]);
686 }
687 }
688 seq_putc(m, '\n');
689}
690
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800691#ifdef CONFIG_HUGETLB_PAGE
692static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
693 unsigned long addr, unsigned long end,
694 struct mm_walk *walk)
695{
696 struct mem_size_stats *mss = walk->private;
697 struct vm_area_struct *vma = walk->vma;
698 struct page *page = NULL;
699
700 if (pte_present(*pte)) {
701 page = vm_normal_page(vma, addr, *pte);
702 } else if (is_swap_pte(*pte)) {
703 swp_entry_t swpent = pte_to_swp_entry(*pte);
704
705 if (is_migration_entry(swpent))
706 page = migration_entry_to_page(swpent);
707 }
708 if (page) {
709 int mapcount = page_mapcount(page);
710
711 if (mapcount >= 2)
712 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
713 else
714 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
715 }
716 return 0;
717}
718#endif /* HUGETLB_PAGE */
719
Dave Hansenc1192f82016-02-12 13:02:27 -0800720void __weak arch_show_smap(struct seq_file *m, struct vm_area_struct *vma)
721{
722}
723
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700724static int show_smap(struct seq_file *m, void *v, int is_pid)
Mauricio Line070ad42005-09-03 15:55:10 -0700725{
726 struct vm_area_struct *vma = v;
Mauricio Line070ad42005-09-03 15:55:10 -0700727 struct mem_size_stats mss;
Dave Hansen21650092008-06-12 15:21:47 -0700728 struct mm_walk smaps_walk = {
729 .pmd_entry = smaps_pte_range,
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800730#ifdef CONFIG_HUGETLB_PAGE
731 .hugetlb_entry = smaps_hugetlb_range,
732#endif
Dave Hansen21650092008-06-12 15:21:47 -0700733 .mm = vma->vm_mm,
734 .private = &mss,
735 };
Mauricio Line070ad42005-09-03 15:55:10 -0700736
737 memset(&mss, 0, sizeof mss);
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800738
739#ifdef CONFIG_SHMEM
740 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800741 /*
742 * For shared or readonly shmem mappings we know that all
743 * swapped out pages belong to the shmem object, and we can
744 * obtain the swap value much more efficiently. For private
745 * writable mappings, we might have COW pages that are
746 * not affected by the parent swapped out pages of the shmem
747 * object, so we have to distinguish them during the page walk.
748 * Unless we know that the shmem object (or the part mapped by
749 * our VMA) has no swapped out pages at all.
750 */
751 unsigned long shmem_swapped = shmem_swap_usage(vma);
752
753 if (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
754 !(vma->vm_flags & VM_WRITE)) {
755 mss.swap = shmem_swapped;
756 } else {
757 mss.check_shmem_swap = true;
758 smaps_walk.pte_hole = smaps_pte_hole;
759 }
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800760 }
761#endif
762
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +0900763 /* mmap_sem is held in m_start */
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800764 walk_page_vma(vma, &smaps_walk);
Matt Mackall4752c362008-02-04 22:29:02 -0800765
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700766 show_map_vma(m, vma, is_pid);
Matt Mackall4752c362008-02-04 22:29:02 -0800767
768 seq_printf(m,
769 "Size: %8lu kB\n"
770 "Rss: %8lu kB\n"
771 "Pss: %8lu kB\n"
772 "Shared_Clean: %8lu kB\n"
773 "Shared_Dirty: %8lu kB\n"
774 "Private_Clean: %8lu kB\n"
775 "Private_Dirty: %8lu kB\n"
Peter Zijlstra214e4712008-04-28 02:12:55 -0700776 "Referenced: %8lu kB\n"
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700777 "Anonymous: %8lu kB\n"
Shaohua Licf8496e2017-05-03 14:52:42 -0700778 "LazyFree: %8lu kB\n"
Dave Hansen4031a212011-03-22 16:33:01 -0700779 "AnonHugePages: %8lu kB\n"
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700780 "ShmemPmdMapped: %8lu kB\n"
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800781 "Shared_Hugetlb: %8lu kB\n"
782 "Private_Hugetlb: %7lu kB\n"
Mel Gorman08fba692009-01-06 14:38:53 -0800783 "Swap: %8lu kB\n"
Minchan Kim8334b962015-09-08 15:00:24 -0700784 "SwapPss: %8lu kB\n"
Mel Gorman33402892009-01-06 14:38:54 -0800785 "KernelPageSize: %8lu kB\n"
Nikanth Karthikesan2d905082011-01-13 15:45:53 -0800786 "MMUPageSize: %8lu kB\n"
787 "Locked: %8lu kB\n",
Matt Mackall4752c362008-02-04 22:29:02 -0800788 (vma->vm_end - vma->vm_start) >> 10,
789 mss.resident >> 10,
790 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
791 mss.shared_clean >> 10,
792 mss.shared_dirty >> 10,
793 mss.private_clean >> 10,
794 mss.private_dirty >> 10,
Peter Zijlstra214e4712008-04-28 02:12:55 -0700795 mss.referenced >> 10,
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700796 mss.anonymous >> 10,
Shaohua Licf8496e2017-05-03 14:52:42 -0700797 mss.lazyfree >> 10,
Dave Hansen4031a212011-03-22 16:33:01 -0700798 mss.anonymous_thp >> 10,
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700799 mss.shmem_thp >> 10,
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800800 mss.shared_hugetlb >> 10,
801 mss.private_hugetlb >> 10,
Mel Gorman08fba692009-01-06 14:38:53 -0800802 mss.swap >> 10,
Minchan Kim8334b962015-09-08 15:00:24 -0700803 (unsigned long)(mss.swap_pss >> (10 + PSS_SHIFT)),
Mel Gorman33402892009-01-06 14:38:54 -0800804 vma_kernel_pagesize(vma) >> 10,
Nikanth Karthikesan2d905082011-01-13 15:45:53 -0800805 vma_mmu_pagesize(vma) >> 10,
806 (vma->vm_flags & VM_LOCKED) ?
807 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
Matt Mackall4752c362008-02-04 22:29:02 -0800808
Dave Hansenc1192f82016-02-12 13:02:27 -0800809 arch_show_smap(m, vma);
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800810 show_smap_vma_flags(m, vma);
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700811 m_cache_vma(m, vma);
Joe Korty7c88db02008-10-16 15:27:09 +0400812 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700813}
814
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700815static int show_pid_smap(struct seq_file *m, void *v)
816{
817 return show_smap(m, v, 1);
818}
819
820static int show_tid_smap(struct seq_file *m, void *v)
821{
822 return show_smap(m, v, 0);
823}
824
Jan Engelhardt03a44822008-02-08 04:21:19 -0800825static const struct seq_operations proc_pid_smaps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800826 .start = m_start,
827 .next = m_next,
828 .stop = m_stop,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700829 .show = show_pid_smap
Matt Mackalla6198792008-02-04 22:29:03 -0800830};
831
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700832static const struct seq_operations proc_tid_smaps_op = {
833 .start = m_start,
834 .next = m_next,
835 .stop = m_stop,
836 .show = show_tid_smap
837};
838
839static int pid_smaps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800840{
841 return do_maps_open(inode, file, &proc_pid_smaps_op);
842}
843
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700844static int tid_smaps_open(struct inode *inode, struct file *file)
845{
846 return do_maps_open(inode, file, &proc_tid_smaps_op);
847}
848
849const struct file_operations proc_pid_smaps_operations = {
850 .open = pid_smaps_open,
851 .read = seq_read,
852 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700853 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700854};
855
856const struct file_operations proc_tid_smaps_operations = {
857 .open = tid_smaps_open,
Matt Mackalla6198792008-02-04 22:29:03 -0800858 .read = seq_read,
859 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700860 .release = proc_map_release,
Matt Mackalla6198792008-02-04 22:29:03 -0800861};
862
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700863enum clear_refs_types {
864 CLEAR_REFS_ALL = 1,
865 CLEAR_REFS_ANON,
866 CLEAR_REFS_MAPPED,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700867 CLEAR_REFS_SOFT_DIRTY,
Petr Cermak695f0552015-02-12 15:01:00 -0800868 CLEAR_REFS_MM_HIWATER_RSS,
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700869 CLEAR_REFS_LAST,
870};
871
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700872struct clear_refs_private {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700873 enum clear_refs_types type;
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700874};
875
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800876#ifdef CONFIG_MEM_SOFT_DIRTY
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700877static inline void clear_soft_dirty(struct vm_area_struct *vma,
878 unsigned long addr, pte_t *pte)
879{
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700880 /*
881 * The soft-dirty tracker uses #PF-s to catch writes
882 * to pages, so write-protect the pte as well. See the
883 * Documentation/vm/soft-dirty.txt for full description
884 * of how soft-dirty works.
885 */
886 pte_t ptent = *pte;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700887
888 if (pte_present(ptent)) {
Laurent Dufour326c2592015-11-05 18:49:21 -0800889 ptent = ptep_modify_prot_start(vma->vm_mm, addr, pte);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700890 ptent = pte_wrprotect(ptent);
Martin Schwidefskya7b76172015-04-22 14:20:47 +0200891 ptent = pte_clear_soft_dirty(ptent);
Laurent Dufour326c2592015-11-05 18:49:21 -0800892 ptep_modify_prot_commit(vma->vm_mm, addr, pte, ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700893 } else if (is_swap_pte(ptent)) {
894 ptent = pte_swp_clear_soft_dirty(ptent);
Laurent Dufour326c2592015-11-05 18:49:21 -0800895 set_pte_at(vma->vm_mm, addr, pte, ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700896 }
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700897}
Laurent Dufour5d3875a2015-11-05 18:49:24 -0800898#else
899static inline void clear_soft_dirty(struct vm_area_struct *vma,
900 unsigned long addr, pte_t *pte)
901{
902}
903#endif
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700904
Laurent Dufour5d3875a2015-11-05 18:49:24 -0800905#if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800906static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
907 unsigned long addr, pmd_t *pmdp)
908{
Kirill A. Shutemov5b7abea2017-04-13 14:56:28 -0700909 pmd_t pmd = *pmdp;
910
911 /* See comment in change_huge_pmd() */
912 pmdp_invalidate(vma, addr, pmdp);
913 if (pmd_dirty(*pmdp))
914 pmd = pmd_mkdirty(pmd);
915 if (pmd_young(*pmdp))
916 pmd = pmd_mkyoung(pmd);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800917
918 pmd = pmd_wrprotect(pmd);
Martin Schwidefskya7b76172015-04-22 14:20:47 +0200919 pmd = pmd_clear_soft_dirty(pmd);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800920
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800921 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
922}
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800923#else
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800924static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
925 unsigned long addr, pmd_t *pmdp)
926{
927}
928#endif
929
Matt Mackalla6198792008-02-04 22:29:03 -0800930static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
Dave Hansen21650092008-06-12 15:21:47 -0700931 unsigned long end, struct mm_walk *walk)
Matt Mackalla6198792008-02-04 22:29:03 -0800932{
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700933 struct clear_refs_private *cp = walk->private;
Naoya Horiguchi5c64f522015-02-11 15:27:46 -0800934 struct vm_area_struct *vma = walk->vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800935 pte_t *pte, ptent;
936 spinlock_t *ptl;
937 struct page *page;
938
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -0800939 ptl = pmd_trans_huge_lock(pmd, vma);
940 if (ptl) {
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800941 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
942 clear_soft_dirty_pmd(vma, addr, pmd);
943 goto out;
944 }
945
946 page = pmd_page(*pmd);
947
948 /* Clear accessed and referenced bits. */
949 pmdp_test_and_clear_young(vma, addr, pmd);
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700950 test_and_clear_page_young(page);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800951 ClearPageReferenced(page);
952out:
953 spin_unlock(ptl);
954 return 0;
955 }
956
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700957 if (pmd_trans_unstable(pmd))
958 return 0;
Dave Hansen03319322011-03-22 16:32:56 -0700959
Matt Mackalla6198792008-02-04 22:29:03 -0800960 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
961 for (; addr != end; pte++, addr += PAGE_SIZE) {
962 ptent = *pte;
Matt Mackalla6198792008-02-04 22:29:03 -0800963
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700964 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
965 clear_soft_dirty(vma, addr, pte);
966 continue;
967 }
968
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700969 if (!pte_present(ptent))
970 continue;
971
Matt Mackalla6198792008-02-04 22:29:03 -0800972 page = vm_normal_page(vma, addr, ptent);
973 if (!page)
974 continue;
975
976 /* Clear accessed and referenced bits. */
977 ptep_test_and_clear_young(vma, addr, pte);
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700978 test_and_clear_page_young(page);
Matt Mackalla6198792008-02-04 22:29:03 -0800979 ClearPageReferenced(page);
980 }
981 pte_unmap_unlock(pte - 1, ptl);
982 cond_resched();
983 return 0;
984}
985
Naoya Horiguchi5c64f522015-02-11 15:27:46 -0800986static int clear_refs_test_walk(unsigned long start, unsigned long end,
987 struct mm_walk *walk)
988{
989 struct clear_refs_private *cp = walk->private;
990 struct vm_area_struct *vma = walk->vma;
991
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800992 if (vma->vm_flags & VM_PFNMAP)
993 return 1;
994
Naoya Horiguchi5c64f522015-02-11 15:27:46 -0800995 /*
996 * Writing 1 to /proc/pid/clear_refs affects all pages.
997 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
998 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
999 * Writing 4 to /proc/pid/clear_refs affects all pages.
1000 */
1001 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
1002 return 1;
1003 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1004 return 1;
1005 return 0;
1006}
1007
Matt Mackallf248dcb2008-02-04 22:29:03 -08001008static ssize_t clear_refs_write(struct file *file, const char __user *buf,
1009 size_t count, loff_t *ppos)
David Rientjesb813e932007-05-06 14:49:24 -07001010{
Matt Mackallf248dcb2008-02-04 22:29:03 -08001011 struct task_struct *task;
Vincent Lifb92a4b2009-09-22 16:45:36 -07001012 char buffer[PROC_NUMBUF];
Matt Mackallf248dcb2008-02-04 22:29:03 -08001013 struct mm_struct *mm;
David Rientjesb813e932007-05-06 14:49:24 -07001014 struct vm_area_struct *vma;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001015 enum clear_refs_types type;
1016 int itype;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001017 int rv;
David Rientjesb813e932007-05-06 14:49:24 -07001018
Matt Mackallf248dcb2008-02-04 22:29:03 -08001019 memset(buffer, 0, sizeof(buffer));
1020 if (count > sizeof(buffer) - 1)
1021 count = sizeof(buffer) - 1;
1022 if (copy_from_user(buffer, buf, count))
1023 return -EFAULT;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001024 rv = kstrtoint(strstrip(buffer), 10, &itype);
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001025 if (rv < 0)
1026 return rv;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001027 type = (enum clear_refs_types)itype;
1028 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
Matt Mackallf248dcb2008-02-04 22:29:03 -08001029 return -EINVAL;
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001030
Al Viro496ad9a2013-01-23 17:07:38 -05001031 task = get_proc_task(file_inode(file));
Matt Mackallf248dcb2008-02-04 22:29:03 -08001032 if (!task)
1033 return -ESRCH;
1034 mm = get_task_mm(task);
1035 if (mm) {
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001036 struct clear_refs_private cp = {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001037 .type = type,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001038 };
Andrew Morton20cbc972008-07-05 12:29:05 -07001039 struct mm_walk clear_refs_walk = {
1040 .pmd_entry = clear_refs_pte_range,
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001041 .test_walk = clear_refs_test_walk,
Andrew Morton20cbc972008-07-05 12:29:05 -07001042 .mm = mm,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001043 .private = &cp,
Andrew Morton20cbc972008-07-05 12:29:05 -07001044 };
Petr Cermak695f0552015-02-12 15:01:00 -08001045
1046 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
Michal Hocko4e801532016-05-23 16:25:45 -07001047 if (down_write_killable(&mm->mmap_sem)) {
1048 count = -EINTR;
1049 goto out_mm;
1050 }
1051
Petr Cermak695f0552015-02-12 15:01:00 -08001052 /*
1053 * Writing 5 to /proc/pid/clear_refs resets the peak
1054 * resident set size to this mm's current rss value.
1055 */
Petr Cermak695f0552015-02-12 15:01:00 -08001056 reset_mm_hiwater_rss(mm);
1057 up_write(&mm->mmap_sem);
1058 goto out_mm;
1059 }
1060
Matt Mackallf248dcb2008-02-04 22:29:03 -08001061 down_read(&mm->mmap_sem);
Peter Feiner64e455072014-10-13 15:55:46 -07001062 if (type == CLEAR_REFS_SOFT_DIRTY) {
1063 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1064 if (!(vma->vm_flags & VM_SOFTDIRTY))
1065 continue;
1066 up_read(&mm->mmap_sem);
Michal Hocko4e801532016-05-23 16:25:45 -07001067 if (down_write_killable(&mm->mmap_sem)) {
1068 count = -EINTR;
1069 goto out_mm;
1070 }
Peter Feiner64e455072014-10-13 15:55:46 -07001071 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1072 vma->vm_flags &= ~VM_SOFTDIRTY;
1073 vma_set_page_prot(vma);
1074 }
1075 downgrade_write(&mm->mmap_sem);
1076 break;
1077 }
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001078 mmu_notifier_invalidate_range_start(mm, 0, -1);
Peter Feiner64e455072014-10-13 15:55:46 -07001079 }
James Morse0f302062016-10-07 17:00:06 -07001080 walk_page_range(0, mm->highest_vm_end, &clear_refs_walk);
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001081 if (type == CLEAR_REFS_SOFT_DIRTY)
1082 mmu_notifier_invalidate_range_end(mm, 0, -1);
Matt Mackallf248dcb2008-02-04 22:29:03 -08001083 flush_tlb_mm(mm);
1084 up_read(&mm->mmap_sem);
Petr Cermak695f0552015-02-12 15:01:00 -08001085out_mm:
Matt Mackallf248dcb2008-02-04 22:29:03 -08001086 mmput(mm);
1087 }
1088 put_task_struct(task);
Vincent Lifb92a4b2009-09-22 16:45:36 -07001089
1090 return count;
David Rientjesb813e932007-05-06 14:49:24 -07001091}
1092
Matt Mackallf248dcb2008-02-04 22:29:03 -08001093const struct file_operations proc_clear_refs_operations = {
1094 .write = clear_refs_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001095 .llseek = noop_llseek,
Matt Mackallf248dcb2008-02-04 22:29:03 -08001096};
1097
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001098typedef struct {
1099 u64 pme;
1100} pagemap_entry_t;
1101
Matt Mackall85863e42008-02-04 22:29:04 -08001102struct pagemapread {
yonghua zheng8c829622013-08-13 16:01:03 -07001103 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001104 pagemap_entry_t *buffer;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001105 bool show_pfn;
Matt Mackall85863e42008-02-04 22:29:04 -08001106};
1107
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001108#define PAGEMAP_WALK_SIZE (PMD_SIZE)
1109#define PAGEMAP_WALK_MASK (PMD_MASK)
1110
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001111#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1112#define PM_PFRAME_BITS 55
1113#define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1114#define PM_SOFT_DIRTY BIT_ULL(55)
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001115#define PM_MMAP_EXCLUSIVE BIT_ULL(56)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001116#define PM_FILE BIT_ULL(61)
1117#define PM_SWAP BIT_ULL(62)
1118#define PM_PRESENT BIT_ULL(63)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001119
Matt Mackall85863e42008-02-04 22:29:04 -08001120#define PM_END_OF_BUFFER 1
1121
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001122static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001123{
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001124 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001125}
1126
1127static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
Matt Mackall85863e42008-02-04 22:29:04 -08001128 struct pagemapread *pm)
1129{
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001130 pm->buffer[pm->pos++] = *pme;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001131 if (pm->pos >= pm->len)
Thomas Tuttleaae86792008-06-05 22:46:31 -07001132 return PM_END_OF_BUFFER;
Matt Mackall85863e42008-02-04 22:29:04 -08001133 return 0;
1134}
1135
1136static int pagemap_pte_hole(unsigned long start, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -07001137 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001138{
Dave Hansen21650092008-06-12 15:21:47 -07001139 struct pagemapread *pm = walk->private;
Peter Feiner68b5a652014-08-06 16:08:09 -07001140 unsigned long addr = start;
Matt Mackall85863e42008-02-04 22:29:04 -08001141 int err = 0;
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001142
Peter Feiner68b5a652014-08-06 16:08:09 -07001143 while (addr < end) {
1144 struct vm_area_struct *vma = find_vma(walk->mm, addr);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001145 pagemap_entry_t pme = make_pme(0, 0);
Peter Feiner87e6d492014-09-25 16:05:18 -07001146 /* End of address space hole, which we mark as non-present. */
1147 unsigned long hole_end;
Peter Feiner68b5a652014-08-06 16:08:09 -07001148
Peter Feiner87e6d492014-09-25 16:05:18 -07001149 if (vma)
1150 hole_end = min(end, vma->vm_start);
1151 else
1152 hole_end = end;
1153
1154 for (; addr < hole_end; addr += PAGE_SIZE) {
1155 err = add_to_pagemap(addr, &pme, pm);
1156 if (err)
1157 goto out;
Peter Feiner68b5a652014-08-06 16:08:09 -07001158 }
1159
Peter Feiner87e6d492014-09-25 16:05:18 -07001160 if (!vma)
1161 break;
1162
1163 /* Addresses in the VMA. */
1164 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001165 pme = make_pme(0, PM_SOFT_DIRTY);
Peter Feiner87e6d492014-09-25 16:05:18 -07001166 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
Peter Feiner68b5a652014-08-06 16:08:09 -07001167 err = add_to_pagemap(addr, &pme, pm);
1168 if (err)
1169 goto out;
1170 }
Matt Mackall85863e42008-02-04 22:29:04 -08001171 }
Peter Feiner68b5a652014-08-06 16:08:09 -07001172out:
Matt Mackall85863e42008-02-04 22:29:04 -08001173 return err;
1174}
1175
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001176static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001177 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
Matt Mackall85863e42008-02-04 22:29:04 -08001178{
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001179 u64 frame = 0, flags = 0;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001180 struct page *page = NULL;
Matt Mackall85863e42008-02-04 22:29:04 -08001181
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001182 if (pte_present(pte)) {
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001183 if (pm->show_pfn)
1184 frame = pte_pfn(pte);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001185 flags |= PM_PRESENT;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001186 page = vm_normal_page(vma, addr, pte);
Cyrill Gorcunove9cdd6e2013-10-16 13:46:53 -07001187 if (pte_soft_dirty(pte))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001188 flags |= PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001189 } else if (is_swap_pte(pte)) {
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001190 swp_entry_t entry;
1191 if (pte_swp_soft_dirty(pte))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001192 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001193 entry = pte_to_swp_entry(pte);
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001194 frame = swp_type(entry) |
1195 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001196 flags |= PM_SWAP;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001197 if (is_migration_entry(entry))
1198 page = migration_entry_to_page(entry);
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001199 }
1200
1201 if (page && !PageAnon(page))
1202 flags |= PM_FILE;
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001203 if (page && page_mapcount(page) == 1)
1204 flags |= PM_MMAP_EXCLUSIVE;
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001205 if (vma->vm_flags & VM_SOFTDIRTY)
1206 flags |= PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001207
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001208 return make_pme(frame, flags);
Dave Hansenbcf80392008-06-12 15:21:48 -07001209}
1210
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001211static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -07001212 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001213{
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001214 struct vm_area_struct *vma = walk->vma;
Dave Hansen21650092008-06-12 15:21:47 -07001215 struct pagemapread *pm = walk->private;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001216 spinlock_t *ptl;
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001217 pte_t *pte, *orig_pte;
Matt Mackall85863e42008-02-04 22:29:04 -08001218 int err = 0;
1219
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001220#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001221 ptl = pmd_trans_huge_lock(pmdp, vma);
1222 if (ptl) {
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001223 u64 flags = 0, frame = 0;
1224 pmd_t pmd = *pmdp;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001225
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001226 if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(pmd))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001227 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001228
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001229 /*
1230 * Currently pmd for thp is always present because thp
1231 * can not be swapped-out, migrated, or HWPOISONed
1232 * (split in such cases instead.)
1233 * This if-check is just to prepare for future implementation.
1234 */
1235 if (pmd_present(pmd)) {
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001236 struct page *page = pmd_page(pmd);
1237
1238 if (page_mapcount(page) == 1)
1239 flags |= PM_MMAP_EXCLUSIVE;
1240
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001241 flags |= PM_PRESENT;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001242 if (pm->show_pfn)
1243 frame = pmd_pfn(pmd) +
1244 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001245 }
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001246
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001247 for (; addr != end; addr += PAGE_SIZE) {
1248 pagemap_entry_t pme = make_pme(frame, flags);
1249
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001250 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001251 if (err)
1252 break;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001253 if (pm->show_pfn && (flags & PM_PRESENT))
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001254 frame++;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001255 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001256 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001257 return err;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001258 }
1259
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001260 if (pmd_trans_unstable(pmdp))
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07001261 return 0;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001262#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Dave Hansenbcf80392008-06-12 15:21:48 -07001263
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001264 /*
1265 * We can assume that @vma always points to a valid one and @end never
1266 * goes beyond vma->vm_end.
1267 */
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001268 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001269 for (; addr < end; pte++, addr += PAGE_SIZE) {
1270 pagemap_entry_t pme;
Peter Feiner81d0fa62014-10-09 15:28:32 -07001271
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001272 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001273 err = add_to_pagemap(addr, &pme, pm);
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001274 if (err)
Peter Feiner81d0fa62014-10-09 15:28:32 -07001275 break;
Matt Mackall85863e42008-02-04 22:29:04 -08001276 }
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001277 pte_unmap_unlock(orig_pte, ptl);
Matt Mackall85863e42008-02-04 22:29:04 -08001278
1279 cond_resched();
1280
1281 return err;
1282}
1283
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001284#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001285/* This function walks within one hugetlb entry in the single call */
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001286static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001287 unsigned long addr, unsigned long end,
1288 struct mm_walk *walk)
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001289{
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001290 struct pagemapread *pm = walk->private;
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001291 struct vm_area_struct *vma = walk->vma;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001292 u64 flags = 0, frame = 0;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001293 int err = 0;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001294 pte_t pte;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001295
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001296 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001297 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001298
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001299 pte = huge_ptep_get(ptep);
1300 if (pte_present(pte)) {
1301 struct page *page = pte_page(pte);
1302
1303 if (!PageAnon(page))
1304 flags |= PM_FILE;
1305
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001306 if (page_mapcount(page) == 1)
1307 flags |= PM_MMAP_EXCLUSIVE;
1308
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001309 flags |= PM_PRESENT;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001310 if (pm->show_pfn)
1311 frame = pte_pfn(pte) +
1312 ((addr & ~hmask) >> PAGE_SHIFT);
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001313 }
1314
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001315 for (; addr != end; addr += PAGE_SIZE) {
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001316 pagemap_entry_t pme = make_pme(frame, flags);
1317
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001318 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001319 if (err)
1320 return err;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001321 if (pm->show_pfn && (flags & PM_PRESENT))
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001322 frame++;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001323 }
1324
1325 cond_resched();
1326
1327 return err;
1328}
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001329#endif /* HUGETLB_PAGE */
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001330
Matt Mackall85863e42008-02-04 22:29:04 -08001331/*
1332 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1333 *
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001334 * For each page in the address space, this file contains one 64-bit entry
1335 * consisting of the following:
1336 *
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001337 * Bits 0-54 page frame number (PFN) if present
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001338 * Bits 0-4 swap type if swapped
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001339 * Bits 5-54 swap offset if swapped
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001340 * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001341 * Bit 56 page exclusively mapped
1342 * Bits 57-60 zero
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001343 * Bit 61 page is file-page or shared-anon
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001344 * Bit 62 page swapped
1345 * Bit 63 page present
1346 *
1347 * If the page is not present but in swap, then the PFN contains an
1348 * encoding of the swap file number and the page's offset into the
1349 * swap. Unmapped pages return a null PFN. This allows determining
Matt Mackall85863e42008-02-04 22:29:04 -08001350 * precisely which pages are mapped (or in swap) and comparing mapped
1351 * pages between processes.
1352 *
1353 * Efficient users of this interface will use /proc/pid/maps to
1354 * determine which areas of memory are actually mapped and llseek to
1355 * skip over unmapped regions.
1356 */
1357static ssize_t pagemap_read(struct file *file, char __user *buf,
1358 size_t count, loff_t *ppos)
1359{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001360 struct mm_struct *mm = file->private_data;
Matt Mackall85863e42008-02-04 22:29:04 -08001361 struct pagemapread pm;
Alexey Dobriyanee1e6ab2008-07-21 14:21:36 -07001362 struct mm_walk pagemap_walk = {};
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001363 unsigned long src;
1364 unsigned long svpfn;
1365 unsigned long start_vaddr;
1366 unsigned long end_vaddr;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001367 int ret = 0, copied = 0;
Matt Mackall85863e42008-02-04 22:29:04 -08001368
Vegard Nossum388f7932017-02-27 14:30:13 -08001369 if (!mm || !mmget_not_zero(mm))
Matt Mackall85863e42008-02-04 22:29:04 -08001370 goto out;
1371
Matt Mackall85863e42008-02-04 22:29:04 -08001372 ret = -EINVAL;
1373 /* file position must be aligned */
Thomas Tuttleaae86792008-06-05 22:46:31 -07001374 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001375 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001376
1377 ret = 0;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001378 if (!count)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001379 goto out_mm;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001380
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001381 /* do not disclose physical addresses: attack vector */
1382 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1383
yonghua zheng8c829622013-08-13 16:01:03 -07001384 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1385 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001386 ret = -ENOMEM;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001387 if (!pm.buffer)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001388 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001389
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001390 pagemap_walk.pmd_entry = pagemap_pmd_range;
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001391 pagemap_walk.pte_hole = pagemap_pte_hole;
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001392#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001393 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001394#endif
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001395 pagemap_walk.mm = mm;
1396 pagemap_walk.private = &pm;
Matt Mackall85863e42008-02-04 22:29:04 -08001397
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001398 src = *ppos;
1399 svpfn = src / PM_ENTRY_BYTES;
1400 start_vaddr = svpfn << PAGE_SHIFT;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001401 end_vaddr = mm->task_size;
Matt Mackall85863e42008-02-04 22:29:04 -08001402
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001403 /* watch out for wraparound */
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001404 if (svpfn > mm->task_size >> PAGE_SHIFT)
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001405 start_vaddr = end_vaddr;
1406
1407 /*
1408 * The odds are that this will stop walking way
1409 * before end_vaddr, because the length of the
1410 * user buffer is tracked in "pm", and the walk
1411 * will stop when we hit the end of the buffer.
1412 */
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001413 ret = 0;
1414 while (count && (start_vaddr < end_vaddr)) {
1415 int len;
1416 unsigned long end;
Matt Mackall85863e42008-02-04 22:29:04 -08001417
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001418 pm.pos = 0;
Naoya Horiguchiea251c12010-11-24 12:57:13 -08001419 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001420 /* overflow ? */
1421 if (end < start_vaddr || end > end_vaddr)
1422 end = end_vaddr;
1423 down_read(&mm->mmap_sem);
1424 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1425 up_read(&mm->mmap_sem);
1426 start_vaddr = end;
1427
1428 len = min(count, PM_ENTRY_BYTES * pm.pos);
Dan Carpenter309361e02010-04-06 13:45:39 +03001429 if (copy_to_user(buf, pm.buffer, len)) {
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001430 ret = -EFAULT;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001431 goto out_free;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001432 }
1433 copied += len;
1434 buf += len;
1435 count -= len;
Matt Mackall85863e42008-02-04 22:29:04 -08001436 }
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001437 *ppos += copied;
1438 if (!ret || ret == PM_END_OF_BUFFER)
1439 ret = copied;
1440
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001441out_free:
1442 kfree(pm.buffer);
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001443out_mm:
1444 mmput(mm);
Matt Mackall85863e42008-02-04 22:29:04 -08001445out:
1446 return ret;
1447}
1448
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001449static int pagemap_open(struct inode *inode, struct file *file)
1450{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001451 struct mm_struct *mm;
1452
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001453 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1454 if (IS_ERR(mm))
1455 return PTR_ERR(mm);
1456 file->private_data = mm;
1457 return 0;
1458}
1459
1460static int pagemap_release(struct inode *inode, struct file *file)
1461{
1462 struct mm_struct *mm = file->private_data;
1463
1464 if (mm)
1465 mmdrop(mm);
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001466 return 0;
1467}
1468
Matt Mackall85863e42008-02-04 22:29:04 -08001469const struct file_operations proc_pagemap_operations = {
1470 .llseek = mem_lseek, /* borrow this */
1471 .read = pagemap_read,
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001472 .open = pagemap_open,
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001473 .release = pagemap_release,
Matt Mackall85863e42008-02-04 22:29:04 -08001474};
Matt Mackall1e883282008-02-04 22:29:07 -08001475#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall85863e42008-02-04 22:29:04 -08001476
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001477#ifdef CONFIG_NUMA
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001478
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001479struct numa_maps {
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001480 unsigned long pages;
1481 unsigned long anon;
1482 unsigned long active;
1483 unsigned long writeback;
1484 unsigned long mapcount_max;
1485 unsigned long dirty;
1486 unsigned long swapcache;
1487 unsigned long node[MAX_NUMNODES];
1488};
1489
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001490struct numa_maps_private {
1491 struct proc_maps_private proc_maps;
1492 struct numa_maps md;
1493};
1494
Dave Hanseneb4866d2011-09-20 15:19:38 -07001495static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1496 unsigned long nr_pages)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001497{
1498 int count = page_mapcount(page);
1499
Dave Hanseneb4866d2011-09-20 15:19:38 -07001500 md->pages += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001501 if (pte_dirty || PageDirty(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001502 md->dirty += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001503
1504 if (PageSwapCache(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001505 md->swapcache += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001506
1507 if (PageActive(page) || PageUnevictable(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001508 md->active += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001509
1510 if (PageWriteback(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001511 md->writeback += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001512
1513 if (PageAnon(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001514 md->anon += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001515
1516 if (count > md->mapcount_max)
1517 md->mapcount_max = count;
1518
Dave Hanseneb4866d2011-09-20 15:19:38 -07001519 md->node[page_to_nid(page)] += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001520}
1521
Dave Hansen3200a8a2011-09-20 15:19:39 -07001522static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1523 unsigned long addr)
1524{
1525 struct page *page;
1526 int nid;
1527
1528 if (!pte_present(pte))
1529 return NULL;
1530
1531 page = vm_normal_page(vma, addr, pte);
1532 if (!page)
1533 return NULL;
1534
1535 if (PageReserved(page))
1536 return NULL;
1537
1538 nid = page_to_nid(page);
Lai Jiangshan4ff1b2c2012-12-12 13:51:25 -08001539 if (!node_isset(nid, node_states[N_MEMORY]))
Dave Hansen3200a8a2011-09-20 15:19:39 -07001540 return NULL;
1541
1542 return page;
1543}
1544
Gerald Schaefer28093f92016-04-28 16:18:35 -07001545#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1546static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1547 struct vm_area_struct *vma,
1548 unsigned long addr)
1549{
1550 struct page *page;
1551 int nid;
1552
1553 if (!pmd_present(pmd))
1554 return NULL;
1555
1556 page = vm_normal_page_pmd(vma, addr, pmd);
1557 if (!page)
1558 return NULL;
1559
1560 if (PageReserved(page))
1561 return NULL;
1562
1563 nid = page_to_nid(page);
1564 if (!node_isset(nid, node_states[N_MEMORY]))
1565 return NULL;
1566
1567 return page;
1568}
1569#endif
1570
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001571static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1572 unsigned long end, struct mm_walk *walk)
1573{
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001574 struct numa_maps *md = walk->private;
1575 struct vm_area_struct *vma = walk->vma;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001576 spinlock_t *ptl;
1577 pte_t *orig_pte;
1578 pte_t *pte;
1579
Gerald Schaefer28093f92016-04-28 16:18:35 -07001580#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001581 ptl = pmd_trans_huge_lock(pmd, vma);
1582 if (ptl) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001583 struct page *page;
1584
Gerald Schaefer28093f92016-04-28 16:18:35 -07001585 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001586 if (page)
Gerald Schaefer28093f92016-04-28 16:18:35 -07001587 gather_stats(page, md, pmd_dirty(*pmd),
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001588 HPAGE_PMD_SIZE/PAGE_SIZE);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001589 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001590 return 0;
Dave Hansen32ef4382011-09-20 15:19:41 -07001591 }
1592
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001593 if (pmd_trans_unstable(pmd))
1594 return 0;
Gerald Schaefer28093f92016-04-28 16:18:35 -07001595#endif
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001596 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1597 do {
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001598 struct page *page = can_gather_numa_stats(*pte, vma, addr);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001599 if (!page)
1600 continue;
Dave Hanseneb4866d2011-09-20 15:19:38 -07001601 gather_stats(page, md, pte_dirty(*pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001602
1603 } while (pte++, addr += PAGE_SIZE, addr != end);
1604 pte_unmap_unlock(orig_pte, ptl);
Hugh Dickinsa66c04102016-12-12 16:44:47 -08001605 cond_resched();
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001606 return 0;
1607}
1608#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001609static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001610 unsigned long addr, unsigned long end, struct mm_walk *walk)
1611{
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001612 pte_t huge_pte = huge_ptep_get(pte);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001613 struct numa_maps *md;
1614 struct page *page;
1615
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001616 if (!pte_present(huge_pte))
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001617 return 0;
1618
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001619 page = pte_page(huge_pte);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001620 if (!page)
1621 return 0;
1622
1623 md = walk->private;
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001624 gather_stats(page, md, pte_dirty(huge_pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001625 return 0;
1626}
1627
1628#else
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001629static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001630 unsigned long addr, unsigned long end, struct mm_walk *walk)
1631{
1632 return 0;
1633}
1634#endif
1635
1636/*
1637 * Display pages allocated per node and memory policy via /proc.
1638 */
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001639static int show_numa_map(struct seq_file *m, void *v, int is_pid)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001640{
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001641 struct numa_maps_private *numa_priv = m->private;
1642 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001643 struct vm_area_struct *vma = v;
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001644 struct numa_maps *md = &numa_priv->md;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001645 struct file *file = vma->vm_file;
1646 struct mm_struct *mm = vma->vm_mm;
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001647 struct mm_walk walk = {
1648 .hugetlb_entry = gather_hugetlb_stats,
1649 .pmd_entry = gather_pte_stats,
1650 .private = md,
1651 .mm = mm,
1652 };
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001653 struct mempolicy *pol;
David Rientjes948927e2013-11-12 15:07:28 -08001654 char buffer[64];
1655 int nid;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001656
1657 if (!mm)
1658 return 0;
1659
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001660 /* Ensure we start with an empty set of numa_maps statistics. */
1661 memset(md, 0, sizeof(*md));
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001662
Oleg Nesterov498f2372014-10-09 15:27:52 -07001663 pol = __get_vma_policy(vma, vma->vm_start);
1664 if (pol) {
1665 mpol_to_str(buffer, sizeof(buffer), pol);
1666 mpol_cond_put(pol);
1667 } else {
1668 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1669 }
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001670
1671 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1672
1673 if (file) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001674 seq_puts(m, " file=");
Miklos Szeredi2726d562015-06-19 10:30:28 +02001675 seq_file_path(m, file, "\n\t= ");
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001676 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001677 seq_puts(m, " heap");
Andy Lutomirskib18cb642016-09-30 10:58:57 -07001678 } else if (is_stack(proc_priv, vma)) {
Johannes Weiner65376df2016-02-02 16:57:29 -08001679 seq_puts(m, " stack");
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001680 }
1681
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001682 if (is_vm_hugetlb_page(vma))
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001683 seq_puts(m, " huge");
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001684
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001685 /* mmap_sem is held by m_start */
1686 walk_page_vma(vma, &walk);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001687
1688 if (!md->pages)
1689 goto out;
1690
1691 if (md->anon)
1692 seq_printf(m, " anon=%lu", md->anon);
1693
1694 if (md->dirty)
1695 seq_printf(m, " dirty=%lu", md->dirty);
1696
1697 if (md->pages != md->anon && md->pages != md->dirty)
1698 seq_printf(m, " mapped=%lu", md->pages);
1699
1700 if (md->mapcount_max > 1)
1701 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1702
1703 if (md->swapcache)
1704 seq_printf(m, " swapcache=%lu", md->swapcache);
1705
1706 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1707 seq_printf(m, " active=%lu", md->active);
1708
1709 if (md->writeback)
1710 seq_printf(m, " writeback=%lu", md->writeback);
1711
David Rientjes948927e2013-11-12 15:07:28 -08001712 for_each_node_state(nid, N_MEMORY)
1713 if (md->node[nid])
1714 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
Rafael Aquini198d1592015-02-12 15:01:08 -08001715
1716 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001717out:
1718 seq_putc(m, '\n');
Oleg Nesterovb8c20a92014-10-09 15:25:41 -07001719 m_cache_vma(m, vma);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001720 return 0;
1721}
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001722
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001723static int show_pid_numa_map(struct seq_file *m, void *v)
1724{
1725 return show_numa_map(m, v, 1);
1726}
1727
1728static int show_tid_numa_map(struct seq_file *m, void *v)
1729{
1730 return show_numa_map(m, v, 0);
1731}
1732
Jan Engelhardt03a44822008-02-08 04:21:19 -08001733static const struct seq_operations proc_pid_numa_maps_op = {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001734 .start = m_start,
1735 .next = m_next,
1736 .stop = m_stop,
1737 .show = show_pid_numa_map,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001738};
Eric W. Biederman662795d2006-06-26 00:25:48 -07001739
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001740static const struct seq_operations proc_tid_numa_maps_op = {
1741 .start = m_start,
1742 .next = m_next,
1743 .stop = m_stop,
1744 .show = show_tid_numa_map,
1745};
1746
1747static int numa_maps_open(struct inode *inode, struct file *file,
1748 const struct seq_operations *ops)
Eric W. Biederman662795d2006-06-26 00:25:48 -07001749{
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -07001750 return proc_maps_open(inode, file, ops,
1751 sizeof(struct numa_maps_private));
Eric W. Biederman662795d2006-06-26 00:25:48 -07001752}
1753
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001754static int pid_numa_maps_open(struct inode *inode, struct file *file)
1755{
1756 return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1757}
1758
1759static int tid_numa_maps_open(struct inode *inode, struct file *file)
1760{
1761 return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1762}
1763
1764const struct file_operations proc_pid_numa_maps_operations = {
1765 .open = pid_numa_maps_open,
1766 .read = seq_read,
1767 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -07001768 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001769};
1770
1771const struct file_operations proc_tid_numa_maps_operations = {
1772 .open = tid_numa_maps_open,
Eric W. Biederman662795d2006-06-26 00:25:48 -07001773 .read = seq_read,
1774 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -07001775 .release = proc_map_release,
Eric W. Biederman662795d2006-06-26 00:25:48 -07001776};
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001777#endif /* CONFIG_NUMA */