blob: f08bd31c1081cc0536602db9d865f4bf491440c9 [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;
Dave Hansen4031a212011-03-22 16:33:01 -0700444 unsigned long anonymous_thp;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700445 unsigned long shmem_thp;
Peter Zijlstra214e4712008-04-28 02:12:55 -0700446 unsigned long swap;
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800447 unsigned long shared_hugetlb;
448 unsigned long private_hugetlb;
Matt Mackalla6198792008-02-04 22:29:03 -0800449 u64 pss;
Minchan Kim8334b962015-09-08 15:00:24 -0700450 u64 swap_pss;
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800451 bool check_shmem_swap;
Matt Mackalla6198792008-02-04 22:29:03 -0800452};
453
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800454static void smaps_account(struct mem_size_stats *mss, struct page *page,
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800455 bool compound, bool young, bool dirty)
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800456{
Kirill A. Shutemovf4be6152016-01-20 14:58:12 -0800457 int i, nr = compound ? 1 << compound_order(page) : 1;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800458 unsigned long size = nr * PAGE_SIZE;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700459
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800460 if (PageAnon(page))
461 mss->anonymous += size;
462
463 mss->resident += size;
464 /* Accumulate the size in pages that have been accessed. */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700465 if (young || page_is_young(page) || PageReferenced(page))
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800466 mss->referenced += size;
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800467
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800468 /*
469 * page_count(page) == 1 guarantees the page is mapped exactly once.
470 * If any subpage of the compound page mapped with PTE it would elevate
471 * page_count().
472 */
473 if (page_count(page) == 1) {
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800474 if (dirty || PageDirty(page))
475 mss->private_dirty += size;
476 else
477 mss->private_clean += size;
478 mss->pss += (u64)size << PSS_SHIFT;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800479 return;
480 }
481
482 for (i = 0; i < nr; i++, page++) {
483 int mapcount = page_mapcount(page);
484
485 if (mapcount >= 2) {
486 if (dirty || PageDirty(page))
487 mss->shared_dirty += PAGE_SIZE;
488 else
489 mss->shared_clean += PAGE_SIZE;
490 mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount;
491 } else {
492 if (dirty || PageDirty(page))
493 mss->private_dirty += PAGE_SIZE;
494 else
495 mss->private_clean += PAGE_SIZE;
496 mss->pss += PAGE_SIZE << PSS_SHIFT;
497 }
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800498 }
499}
500
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800501#ifdef CONFIG_SHMEM
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800502static int smaps_pte_hole(unsigned long addr, unsigned long end,
503 struct mm_walk *walk)
504{
505 struct mem_size_stats *mss = walk->private;
506
Vlastimil Babka48131e02016-01-14 15:19:23 -0800507 mss->swap += shmem_partial_swap_usage(
508 walk->vma->vm_file->f_mapping, addr, end);
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800509
510 return 0;
511}
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800512#endif
513
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800514static void smaps_pte_entry(pte_t *pte, unsigned long addr,
515 struct mm_walk *walk)
Dave Hansenae11c4d2011-03-22 16:32:58 -0700516{
517 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800518 struct vm_area_struct *vma = walk->vma;
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700519 struct page *page = NULL;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700520
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800521 if (pte_present(*pte)) {
522 page = vm_normal_page(vma, addr, *pte);
523 } else if (is_swap_pte(*pte)) {
524 swp_entry_t swpent = pte_to_swp_entry(*pte);
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700525
Minchan Kim8334b962015-09-08 15:00:24 -0700526 if (!non_swap_entry(swpent)) {
527 int mapcount;
528
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800529 mss->swap += PAGE_SIZE;
Minchan Kim8334b962015-09-08 15:00:24 -0700530 mapcount = swp_swapcount(swpent);
531 if (mapcount >= 2) {
532 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
533
534 do_div(pss_delta, mapcount);
535 mss->swap_pss += pss_delta;
536 } else {
537 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
538 }
539 } else if (is_migration_entry(swpent))
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700540 page = migration_entry_to_page(swpent);
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800541 } else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
542 && pte_none(*pte))) {
Vlastimil Babka48131e02016-01-14 15:19:23 -0800543 page = find_get_entry(vma->vm_file->f_mapping,
544 linear_page_index(vma, addr));
545 if (!page)
546 return;
547
548 if (radix_tree_exceptional_entry(page))
549 mss->swap += PAGE_SIZE;
550 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300551 put_page(page);
Vlastimil Babka48131e02016-01-14 15:19:23 -0800552
553 return;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700554 }
555
Dave Hansenae11c4d2011-03-22 16:32:58 -0700556 if (!page)
557 return;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800558
559 smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte));
Dave Hansenae11c4d2011-03-22 16:32:58 -0700560}
561
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800562#ifdef CONFIG_TRANSPARENT_HUGEPAGE
563static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
564 struct mm_walk *walk)
565{
566 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800567 struct vm_area_struct *vma = walk->vma;
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800568 struct page *page;
569
570 /* FOLL_DUMP will return -EFAULT on huge zero page */
571 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
572 if (IS_ERR_OR_NULL(page))
573 return;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700574 if (PageAnon(page))
575 mss->anonymous_thp += HPAGE_PMD_SIZE;
576 else if (PageSwapBacked(page))
577 mss->shmem_thp += HPAGE_PMD_SIZE;
Dan Williamsca120cf2016-09-03 10:38:03 -0700578 else if (is_zone_device_page(page))
579 /* pass */;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700580 else
581 VM_BUG_ON_PAGE(1, page);
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800582 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd));
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800583}
584#else
585static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
586 struct mm_walk *walk)
587{
588}
589#endif
590
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800591static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700592 struct mm_walk *walk)
Mauricio Line070ad42005-09-03 15:55:10 -0700593{
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800594 struct vm_area_struct *vma = walk->vma;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700595 pte_t *pte;
Hugh Dickins705e87c2005-10-29 18:16:27 -0700596 spinlock_t *ptl;
Mauricio Line070ad42005-09-03 15:55:10 -0700597
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -0800598 ptl = pmd_trans_huge_lock(pmd, vma);
599 if (ptl) {
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800600 smaps_pmd_entry(pmd, addr, walk);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -0800601 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -0700602 return 0;
Dave Hansen22e057c2011-03-22 16:33:00 -0700603 }
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700604
605 if (pmd_trans_unstable(pmd))
606 return 0;
Dave Hansen22e057c2011-03-22 16:33:00 -0700607 /*
608 * The mmap_sem held all the way back in m_start() is what
609 * keeps khugepaged out of here and from collapsing things
610 * in here.
611 */
Hugh Dickins705e87c2005-10-29 18:16:27 -0700612 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700613 for (; addr != end; pte++, addr += PAGE_SIZE)
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800614 smaps_pte_entry(pte, addr, walk);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700615 pte_unmap_unlock(pte - 1, ptl);
616 cond_resched();
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800617 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700618}
619
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800620static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
621{
622 /*
623 * Don't forget to update Documentation/ on changes.
624 */
625 static const char mnemonics[BITS_PER_LONG][2] = {
626 /*
627 * In case if we meet a flag we don't know about.
628 */
629 [0 ... (BITS_PER_LONG-1)] = "??",
630
631 [ilog2(VM_READ)] = "rd",
632 [ilog2(VM_WRITE)] = "wr",
633 [ilog2(VM_EXEC)] = "ex",
634 [ilog2(VM_SHARED)] = "sh",
635 [ilog2(VM_MAYREAD)] = "mr",
636 [ilog2(VM_MAYWRITE)] = "mw",
637 [ilog2(VM_MAYEXEC)] = "me",
638 [ilog2(VM_MAYSHARE)] = "ms",
639 [ilog2(VM_GROWSDOWN)] = "gd",
640 [ilog2(VM_PFNMAP)] = "pf",
641 [ilog2(VM_DENYWRITE)] = "dw",
Qiaowei Ren4aae7e42014-11-14 07:18:25 -0800642#ifdef CONFIG_X86_INTEL_MPX
643 [ilog2(VM_MPX)] = "mp",
644#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800645 [ilog2(VM_LOCKED)] = "lo",
646 [ilog2(VM_IO)] = "io",
647 [ilog2(VM_SEQ_READ)] = "sr",
648 [ilog2(VM_RAND_READ)] = "rr",
649 [ilog2(VM_DONTCOPY)] = "dc",
650 [ilog2(VM_DONTEXPAND)] = "de",
651 [ilog2(VM_ACCOUNT)] = "ac",
652 [ilog2(VM_NORESERVE)] = "nr",
653 [ilog2(VM_HUGETLB)] = "ht",
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800654 [ilog2(VM_ARCH_1)] = "ar",
655 [ilog2(VM_DONTDUMP)] = "dd",
Naoya Horiguchiec8e41a2013-11-12 15:07:49 -0800656#ifdef CONFIG_MEM_SOFT_DIRTY
657 [ilog2(VM_SOFTDIRTY)] = "sd",
658#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800659 [ilog2(VM_MIXEDMAP)] = "mm",
660 [ilog2(VM_HUGEPAGE)] = "hg",
661 [ilog2(VM_NOHUGEPAGE)] = "nh",
662 [ilog2(VM_MERGEABLE)] = "mg",
Andrea Arcangeli16ba6f82015-09-04 15:46:17 -0700663 [ilog2(VM_UFFD_MISSING)]= "um",
664 [ilog2(VM_UFFD_WP)] = "uw",
Dave Hansenc1192f82016-02-12 13:02:27 -0800665#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
666 /* These come out via ProtectionKey: */
667 [ilog2(VM_PKEY_BIT0)] = "",
668 [ilog2(VM_PKEY_BIT1)] = "",
669 [ilog2(VM_PKEY_BIT2)] = "",
670 [ilog2(VM_PKEY_BIT3)] = "",
671#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800672 };
673 size_t i;
674
675 seq_puts(m, "VmFlags: ");
676 for (i = 0; i < BITS_PER_LONG; i++) {
Dave Hansenc1192f82016-02-12 13:02:27 -0800677 if (!mnemonics[i][0])
678 continue;
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800679 if (vma->vm_flags & (1UL << i)) {
680 seq_printf(m, "%c%c ",
681 mnemonics[i][0], mnemonics[i][1]);
682 }
683 }
684 seq_putc(m, '\n');
685}
686
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800687#ifdef CONFIG_HUGETLB_PAGE
688static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
689 unsigned long addr, unsigned long end,
690 struct mm_walk *walk)
691{
692 struct mem_size_stats *mss = walk->private;
693 struct vm_area_struct *vma = walk->vma;
694 struct page *page = NULL;
695
696 if (pte_present(*pte)) {
697 page = vm_normal_page(vma, addr, *pte);
698 } else if (is_swap_pte(*pte)) {
699 swp_entry_t swpent = pte_to_swp_entry(*pte);
700
701 if (is_migration_entry(swpent))
702 page = migration_entry_to_page(swpent);
703 }
704 if (page) {
705 int mapcount = page_mapcount(page);
706
707 if (mapcount >= 2)
708 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
709 else
710 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
711 }
712 return 0;
713}
714#endif /* HUGETLB_PAGE */
715
Dave Hansenc1192f82016-02-12 13:02:27 -0800716void __weak arch_show_smap(struct seq_file *m, struct vm_area_struct *vma)
717{
718}
719
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700720static int show_smap(struct seq_file *m, void *v, int is_pid)
Mauricio Line070ad42005-09-03 15:55:10 -0700721{
722 struct vm_area_struct *vma = v;
Mauricio Line070ad42005-09-03 15:55:10 -0700723 struct mem_size_stats mss;
Dave Hansen21650092008-06-12 15:21:47 -0700724 struct mm_walk smaps_walk = {
725 .pmd_entry = smaps_pte_range,
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800726#ifdef CONFIG_HUGETLB_PAGE
727 .hugetlb_entry = smaps_hugetlb_range,
728#endif
Dave Hansen21650092008-06-12 15:21:47 -0700729 .mm = vma->vm_mm,
730 .private = &mss,
731 };
Mauricio Line070ad42005-09-03 15:55:10 -0700732
733 memset(&mss, 0, sizeof mss);
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800734
735#ifdef CONFIG_SHMEM
736 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800737 /*
738 * For shared or readonly shmem mappings we know that all
739 * swapped out pages belong to the shmem object, and we can
740 * obtain the swap value much more efficiently. For private
741 * writable mappings, we might have COW pages that are
742 * not affected by the parent swapped out pages of the shmem
743 * object, so we have to distinguish them during the page walk.
744 * Unless we know that the shmem object (or the part mapped by
745 * our VMA) has no swapped out pages at all.
746 */
747 unsigned long shmem_swapped = shmem_swap_usage(vma);
748
749 if (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
750 !(vma->vm_flags & VM_WRITE)) {
751 mss.swap = shmem_swapped;
752 } else {
753 mss.check_shmem_swap = true;
754 smaps_walk.pte_hole = smaps_pte_hole;
755 }
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800756 }
757#endif
758
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +0900759 /* mmap_sem is held in m_start */
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800760 walk_page_vma(vma, &smaps_walk);
Matt Mackall4752c362008-02-04 22:29:02 -0800761
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700762 show_map_vma(m, vma, is_pid);
Matt Mackall4752c362008-02-04 22:29:02 -0800763
764 seq_printf(m,
765 "Size: %8lu kB\n"
766 "Rss: %8lu kB\n"
767 "Pss: %8lu kB\n"
768 "Shared_Clean: %8lu kB\n"
769 "Shared_Dirty: %8lu kB\n"
770 "Private_Clean: %8lu kB\n"
771 "Private_Dirty: %8lu kB\n"
Peter Zijlstra214e4712008-04-28 02:12:55 -0700772 "Referenced: %8lu kB\n"
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700773 "Anonymous: %8lu kB\n"
Dave Hansen4031a212011-03-22 16:33:01 -0700774 "AnonHugePages: %8lu kB\n"
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700775 "ShmemPmdMapped: %8lu kB\n"
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800776 "Shared_Hugetlb: %8lu kB\n"
777 "Private_Hugetlb: %7lu kB\n"
Mel Gorman08fba692009-01-06 14:38:53 -0800778 "Swap: %8lu kB\n"
Minchan Kim8334b962015-09-08 15:00:24 -0700779 "SwapPss: %8lu kB\n"
Mel Gorman33402892009-01-06 14:38:54 -0800780 "KernelPageSize: %8lu kB\n"
Nikanth Karthikesan2d905082011-01-13 15:45:53 -0800781 "MMUPageSize: %8lu kB\n"
782 "Locked: %8lu kB\n",
Matt Mackall4752c362008-02-04 22:29:02 -0800783 (vma->vm_end - vma->vm_start) >> 10,
784 mss.resident >> 10,
785 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
786 mss.shared_clean >> 10,
787 mss.shared_dirty >> 10,
788 mss.private_clean >> 10,
789 mss.private_dirty >> 10,
Peter Zijlstra214e4712008-04-28 02:12:55 -0700790 mss.referenced >> 10,
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700791 mss.anonymous >> 10,
Dave Hansen4031a212011-03-22 16:33:01 -0700792 mss.anonymous_thp >> 10,
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700793 mss.shmem_thp >> 10,
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800794 mss.shared_hugetlb >> 10,
795 mss.private_hugetlb >> 10,
Mel Gorman08fba692009-01-06 14:38:53 -0800796 mss.swap >> 10,
Minchan Kim8334b962015-09-08 15:00:24 -0700797 (unsigned long)(mss.swap_pss >> (10 + PSS_SHIFT)),
Mel Gorman33402892009-01-06 14:38:54 -0800798 vma_kernel_pagesize(vma) >> 10,
Nikanth Karthikesan2d905082011-01-13 15:45:53 -0800799 vma_mmu_pagesize(vma) >> 10,
800 (vma->vm_flags & VM_LOCKED) ?
801 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
Matt Mackall4752c362008-02-04 22:29:02 -0800802
Dave Hansenc1192f82016-02-12 13:02:27 -0800803 arch_show_smap(m, vma);
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800804 show_smap_vma_flags(m, vma);
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700805 m_cache_vma(m, vma);
Joe Korty7c88db02008-10-16 15:27:09 +0400806 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700807}
808
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700809static int show_pid_smap(struct seq_file *m, void *v)
810{
811 return show_smap(m, v, 1);
812}
813
814static int show_tid_smap(struct seq_file *m, void *v)
815{
816 return show_smap(m, v, 0);
817}
818
Jan Engelhardt03a44822008-02-08 04:21:19 -0800819static const struct seq_operations proc_pid_smaps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800820 .start = m_start,
821 .next = m_next,
822 .stop = m_stop,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700823 .show = show_pid_smap
Matt Mackalla6198792008-02-04 22:29:03 -0800824};
825
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700826static const struct seq_operations proc_tid_smaps_op = {
827 .start = m_start,
828 .next = m_next,
829 .stop = m_stop,
830 .show = show_tid_smap
831};
832
833static int pid_smaps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800834{
835 return do_maps_open(inode, file, &proc_pid_smaps_op);
836}
837
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700838static int tid_smaps_open(struct inode *inode, struct file *file)
839{
840 return do_maps_open(inode, file, &proc_tid_smaps_op);
841}
842
843const struct file_operations proc_pid_smaps_operations = {
844 .open = pid_smaps_open,
845 .read = seq_read,
846 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700847 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700848};
849
850const struct file_operations proc_tid_smaps_operations = {
851 .open = tid_smaps_open,
Matt Mackalla6198792008-02-04 22:29:03 -0800852 .read = seq_read,
853 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700854 .release = proc_map_release,
Matt Mackalla6198792008-02-04 22:29:03 -0800855};
856
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700857enum clear_refs_types {
858 CLEAR_REFS_ALL = 1,
859 CLEAR_REFS_ANON,
860 CLEAR_REFS_MAPPED,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700861 CLEAR_REFS_SOFT_DIRTY,
Petr Cermak695f0552015-02-12 15:01:00 -0800862 CLEAR_REFS_MM_HIWATER_RSS,
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700863 CLEAR_REFS_LAST,
864};
865
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700866struct clear_refs_private {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700867 enum clear_refs_types type;
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700868};
869
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800870#ifdef CONFIG_MEM_SOFT_DIRTY
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700871static inline void clear_soft_dirty(struct vm_area_struct *vma,
872 unsigned long addr, pte_t *pte)
873{
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700874 /*
875 * The soft-dirty tracker uses #PF-s to catch writes
876 * to pages, so write-protect the pte as well. See the
877 * Documentation/vm/soft-dirty.txt for full description
878 * of how soft-dirty works.
879 */
880 pte_t ptent = *pte;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700881
882 if (pte_present(ptent)) {
Laurent Dufour326c2592015-11-05 18:49:21 -0800883 ptent = ptep_modify_prot_start(vma->vm_mm, addr, pte);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700884 ptent = pte_wrprotect(ptent);
Martin Schwidefskya7b76172015-04-22 14:20:47 +0200885 ptent = pte_clear_soft_dirty(ptent);
Laurent Dufour326c2592015-11-05 18:49:21 -0800886 ptep_modify_prot_commit(vma->vm_mm, addr, pte, ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700887 } else if (is_swap_pte(ptent)) {
888 ptent = pte_swp_clear_soft_dirty(ptent);
Laurent Dufour326c2592015-11-05 18:49:21 -0800889 set_pte_at(vma->vm_mm, addr, pte, ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700890 }
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700891}
Laurent Dufour5d3875a2015-11-05 18:49:24 -0800892#else
893static inline void clear_soft_dirty(struct vm_area_struct *vma,
894 unsigned long addr, pte_t *pte)
895{
896}
897#endif
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700898
Laurent Dufour5d3875a2015-11-05 18:49:24 -0800899#if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800900static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
901 unsigned long addr, pmd_t *pmdp)
902{
Laurent Dufour326c2592015-11-05 18:49:21 -0800903 pmd_t pmd = pmdp_huge_get_and_clear(vma->vm_mm, addr, pmdp);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800904
905 pmd = pmd_wrprotect(pmd);
Martin Schwidefskya7b76172015-04-22 14:20:47 +0200906 pmd = pmd_clear_soft_dirty(pmd);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800907
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800908 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
909}
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800910#else
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800911static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
912 unsigned long addr, pmd_t *pmdp)
913{
914}
915#endif
916
Matt Mackalla6198792008-02-04 22:29:03 -0800917static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
Dave Hansen21650092008-06-12 15:21:47 -0700918 unsigned long end, struct mm_walk *walk)
Matt Mackalla6198792008-02-04 22:29:03 -0800919{
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700920 struct clear_refs_private *cp = walk->private;
Naoya Horiguchi5c64f522015-02-11 15:27:46 -0800921 struct vm_area_struct *vma = walk->vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800922 pte_t *pte, ptent;
923 spinlock_t *ptl;
924 struct page *page;
925
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -0800926 ptl = pmd_trans_huge_lock(pmd, vma);
927 if (ptl) {
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800928 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
929 clear_soft_dirty_pmd(vma, addr, pmd);
930 goto out;
931 }
932
933 page = pmd_page(*pmd);
934
935 /* Clear accessed and referenced bits. */
936 pmdp_test_and_clear_young(vma, addr, pmd);
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700937 test_and_clear_page_young(page);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800938 ClearPageReferenced(page);
939out:
940 spin_unlock(ptl);
941 return 0;
942 }
943
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700944 if (pmd_trans_unstable(pmd))
945 return 0;
Dave Hansen03319322011-03-22 16:32:56 -0700946
Matt Mackalla6198792008-02-04 22:29:03 -0800947 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
948 for (; addr != end; pte++, addr += PAGE_SIZE) {
949 ptent = *pte;
Matt Mackalla6198792008-02-04 22:29:03 -0800950
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700951 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
952 clear_soft_dirty(vma, addr, pte);
953 continue;
954 }
955
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700956 if (!pte_present(ptent))
957 continue;
958
Matt Mackalla6198792008-02-04 22:29:03 -0800959 page = vm_normal_page(vma, addr, ptent);
960 if (!page)
961 continue;
962
963 /* Clear accessed and referenced bits. */
964 ptep_test_and_clear_young(vma, addr, pte);
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700965 test_and_clear_page_young(page);
Matt Mackalla6198792008-02-04 22:29:03 -0800966 ClearPageReferenced(page);
967 }
968 pte_unmap_unlock(pte - 1, ptl);
969 cond_resched();
970 return 0;
971}
972
Naoya Horiguchi5c64f522015-02-11 15:27:46 -0800973static int clear_refs_test_walk(unsigned long start, unsigned long end,
974 struct mm_walk *walk)
975{
976 struct clear_refs_private *cp = walk->private;
977 struct vm_area_struct *vma = walk->vma;
978
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800979 if (vma->vm_flags & VM_PFNMAP)
980 return 1;
981
Naoya Horiguchi5c64f522015-02-11 15:27:46 -0800982 /*
983 * Writing 1 to /proc/pid/clear_refs affects all pages.
984 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
985 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
986 * Writing 4 to /proc/pid/clear_refs affects all pages.
987 */
988 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
989 return 1;
990 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
991 return 1;
992 return 0;
993}
994
Matt Mackallf248dcb2008-02-04 22:29:03 -0800995static ssize_t clear_refs_write(struct file *file, const char __user *buf,
996 size_t count, loff_t *ppos)
David Rientjesb813e932007-05-06 14:49:24 -0700997{
Matt Mackallf248dcb2008-02-04 22:29:03 -0800998 struct task_struct *task;
Vincent Lifb92a4b2009-09-22 16:45:36 -0700999 char buffer[PROC_NUMBUF];
Matt Mackallf248dcb2008-02-04 22:29:03 -08001000 struct mm_struct *mm;
David Rientjesb813e932007-05-06 14:49:24 -07001001 struct vm_area_struct *vma;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001002 enum clear_refs_types type;
1003 int itype;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001004 int rv;
David Rientjesb813e932007-05-06 14:49:24 -07001005
Matt Mackallf248dcb2008-02-04 22:29:03 -08001006 memset(buffer, 0, sizeof(buffer));
1007 if (count > sizeof(buffer) - 1)
1008 count = sizeof(buffer) - 1;
1009 if (copy_from_user(buffer, buf, count))
1010 return -EFAULT;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001011 rv = kstrtoint(strstrip(buffer), 10, &itype);
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001012 if (rv < 0)
1013 return rv;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001014 type = (enum clear_refs_types)itype;
1015 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
Matt Mackallf248dcb2008-02-04 22:29:03 -08001016 return -EINVAL;
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001017
Al Viro496ad9a2013-01-23 17:07:38 -05001018 task = get_proc_task(file_inode(file));
Matt Mackallf248dcb2008-02-04 22:29:03 -08001019 if (!task)
1020 return -ESRCH;
1021 mm = get_task_mm(task);
1022 if (mm) {
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001023 struct clear_refs_private cp = {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001024 .type = type,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001025 };
Andrew Morton20cbc972008-07-05 12:29:05 -07001026 struct mm_walk clear_refs_walk = {
1027 .pmd_entry = clear_refs_pte_range,
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001028 .test_walk = clear_refs_test_walk,
Andrew Morton20cbc972008-07-05 12:29:05 -07001029 .mm = mm,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001030 .private = &cp,
Andrew Morton20cbc972008-07-05 12:29:05 -07001031 };
Petr Cermak695f0552015-02-12 15:01:00 -08001032
1033 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
Michal Hocko4e801532016-05-23 16:25:45 -07001034 if (down_write_killable(&mm->mmap_sem)) {
1035 count = -EINTR;
1036 goto out_mm;
1037 }
1038
Petr Cermak695f0552015-02-12 15:01:00 -08001039 /*
1040 * Writing 5 to /proc/pid/clear_refs resets the peak
1041 * resident set size to this mm's current rss value.
1042 */
Petr Cermak695f0552015-02-12 15:01:00 -08001043 reset_mm_hiwater_rss(mm);
1044 up_write(&mm->mmap_sem);
1045 goto out_mm;
1046 }
1047
Matt Mackallf248dcb2008-02-04 22:29:03 -08001048 down_read(&mm->mmap_sem);
Peter Feiner64e455072014-10-13 15:55:46 -07001049 if (type == CLEAR_REFS_SOFT_DIRTY) {
1050 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1051 if (!(vma->vm_flags & VM_SOFTDIRTY))
1052 continue;
1053 up_read(&mm->mmap_sem);
Michal Hocko4e801532016-05-23 16:25:45 -07001054 if (down_write_killable(&mm->mmap_sem)) {
1055 count = -EINTR;
1056 goto out_mm;
1057 }
Peter Feiner64e455072014-10-13 15:55:46 -07001058 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1059 vma->vm_flags &= ~VM_SOFTDIRTY;
1060 vma_set_page_prot(vma);
1061 }
1062 downgrade_write(&mm->mmap_sem);
1063 break;
1064 }
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001065 mmu_notifier_invalidate_range_start(mm, 0, -1);
Peter Feiner64e455072014-10-13 15:55:46 -07001066 }
James Morse0f302062016-10-07 17:00:06 -07001067 walk_page_range(0, mm->highest_vm_end, &clear_refs_walk);
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001068 if (type == CLEAR_REFS_SOFT_DIRTY)
1069 mmu_notifier_invalidate_range_end(mm, 0, -1);
Matt Mackallf248dcb2008-02-04 22:29:03 -08001070 flush_tlb_mm(mm);
1071 up_read(&mm->mmap_sem);
Petr Cermak695f0552015-02-12 15:01:00 -08001072out_mm:
Matt Mackallf248dcb2008-02-04 22:29:03 -08001073 mmput(mm);
1074 }
1075 put_task_struct(task);
Vincent Lifb92a4b2009-09-22 16:45:36 -07001076
1077 return count;
David Rientjesb813e932007-05-06 14:49:24 -07001078}
1079
Matt Mackallf248dcb2008-02-04 22:29:03 -08001080const struct file_operations proc_clear_refs_operations = {
1081 .write = clear_refs_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001082 .llseek = noop_llseek,
Matt Mackallf248dcb2008-02-04 22:29:03 -08001083};
1084
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001085typedef struct {
1086 u64 pme;
1087} pagemap_entry_t;
1088
Matt Mackall85863e42008-02-04 22:29:04 -08001089struct pagemapread {
yonghua zheng8c829622013-08-13 16:01:03 -07001090 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001091 pagemap_entry_t *buffer;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001092 bool show_pfn;
Matt Mackall85863e42008-02-04 22:29:04 -08001093};
1094
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001095#define PAGEMAP_WALK_SIZE (PMD_SIZE)
1096#define PAGEMAP_WALK_MASK (PMD_MASK)
1097
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001098#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1099#define PM_PFRAME_BITS 55
1100#define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1101#define PM_SOFT_DIRTY BIT_ULL(55)
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001102#define PM_MMAP_EXCLUSIVE BIT_ULL(56)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001103#define PM_FILE BIT_ULL(61)
1104#define PM_SWAP BIT_ULL(62)
1105#define PM_PRESENT BIT_ULL(63)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001106
Matt Mackall85863e42008-02-04 22:29:04 -08001107#define PM_END_OF_BUFFER 1
1108
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001109static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001110{
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001111 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001112}
1113
1114static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
Matt Mackall85863e42008-02-04 22:29:04 -08001115 struct pagemapread *pm)
1116{
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001117 pm->buffer[pm->pos++] = *pme;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001118 if (pm->pos >= pm->len)
Thomas Tuttleaae86792008-06-05 22:46:31 -07001119 return PM_END_OF_BUFFER;
Matt Mackall85863e42008-02-04 22:29:04 -08001120 return 0;
1121}
1122
1123static int pagemap_pte_hole(unsigned long start, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -07001124 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001125{
Dave Hansen21650092008-06-12 15:21:47 -07001126 struct pagemapread *pm = walk->private;
Peter Feiner68b5a652014-08-06 16:08:09 -07001127 unsigned long addr = start;
Matt Mackall85863e42008-02-04 22:29:04 -08001128 int err = 0;
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001129
Peter Feiner68b5a652014-08-06 16:08:09 -07001130 while (addr < end) {
1131 struct vm_area_struct *vma = find_vma(walk->mm, addr);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001132 pagemap_entry_t pme = make_pme(0, 0);
Peter Feiner87e6d492014-09-25 16:05:18 -07001133 /* End of address space hole, which we mark as non-present. */
1134 unsigned long hole_end;
Peter Feiner68b5a652014-08-06 16:08:09 -07001135
Peter Feiner87e6d492014-09-25 16:05:18 -07001136 if (vma)
1137 hole_end = min(end, vma->vm_start);
1138 else
1139 hole_end = end;
1140
1141 for (; addr < hole_end; addr += PAGE_SIZE) {
1142 err = add_to_pagemap(addr, &pme, pm);
1143 if (err)
1144 goto out;
Peter Feiner68b5a652014-08-06 16:08:09 -07001145 }
1146
Peter Feiner87e6d492014-09-25 16:05:18 -07001147 if (!vma)
1148 break;
1149
1150 /* Addresses in the VMA. */
1151 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001152 pme = make_pme(0, PM_SOFT_DIRTY);
Peter Feiner87e6d492014-09-25 16:05:18 -07001153 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
Peter Feiner68b5a652014-08-06 16:08:09 -07001154 err = add_to_pagemap(addr, &pme, pm);
1155 if (err)
1156 goto out;
1157 }
Matt Mackall85863e42008-02-04 22:29:04 -08001158 }
Peter Feiner68b5a652014-08-06 16:08:09 -07001159out:
Matt Mackall85863e42008-02-04 22:29:04 -08001160 return err;
1161}
1162
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001163static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001164 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
Matt Mackall85863e42008-02-04 22:29:04 -08001165{
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001166 u64 frame = 0, flags = 0;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001167 struct page *page = NULL;
Matt Mackall85863e42008-02-04 22:29:04 -08001168
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001169 if (pte_present(pte)) {
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001170 if (pm->show_pfn)
1171 frame = pte_pfn(pte);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001172 flags |= PM_PRESENT;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001173 page = vm_normal_page(vma, addr, pte);
Cyrill Gorcunove9cdd6e2013-10-16 13:46:53 -07001174 if (pte_soft_dirty(pte))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001175 flags |= PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001176 } else if (is_swap_pte(pte)) {
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001177 swp_entry_t entry;
1178 if (pte_swp_soft_dirty(pte))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001179 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001180 entry = pte_to_swp_entry(pte);
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001181 frame = swp_type(entry) |
1182 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001183 flags |= PM_SWAP;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001184 if (is_migration_entry(entry))
1185 page = migration_entry_to_page(entry);
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001186 }
1187
1188 if (page && !PageAnon(page))
1189 flags |= PM_FILE;
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001190 if (page && page_mapcount(page) == 1)
1191 flags |= PM_MMAP_EXCLUSIVE;
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001192 if (vma->vm_flags & VM_SOFTDIRTY)
1193 flags |= PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001194
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001195 return make_pme(frame, flags);
Dave Hansenbcf80392008-06-12 15:21:48 -07001196}
1197
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001198static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -07001199 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001200{
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001201 struct vm_area_struct *vma = walk->vma;
Dave Hansen21650092008-06-12 15:21:47 -07001202 struct pagemapread *pm = walk->private;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001203 spinlock_t *ptl;
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001204 pte_t *pte, *orig_pte;
Matt Mackall85863e42008-02-04 22:29:04 -08001205 int err = 0;
1206
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001207#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001208 ptl = pmd_trans_huge_lock(pmdp, vma);
1209 if (ptl) {
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001210 u64 flags = 0, frame = 0;
1211 pmd_t pmd = *pmdp;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001212
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001213 if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(pmd))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001214 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001215
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001216 /*
1217 * Currently pmd for thp is always present because thp
1218 * can not be swapped-out, migrated, or HWPOISONed
1219 * (split in such cases instead.)
1220 * This if-check is just to prepare for future implementation.
1221 */
1222 if (pmd_present(pmd)) {
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001223 struct page *page = pmd_page(pmd);
1224
1225 if (page_mapcount(page) == 1)
1226 flags |= PM_MMAP_EXCLUSIVE;
1227
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001228 flags |= PM_PRESENT;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001229 if (pm->show_pfn)
1230 frame = pmd_pfn(pmd) +
1231 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001232 }
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001233
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001234 for (; addr != end; addr += PAGE_SIZE) {
1235 pagemap_entry_t pme = make_pme(frame, flags);
1236
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001237 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001238 if (err)
1239 break;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001240 if (pm->show_pfn && (flags & PM_PRESENT))
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001241 frame++;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001242 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001243 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001244 return err;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001245 }
1246
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001247 if (pmd_trans_unstable(pmdp))
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07001248 return 0;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001249#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Dave Hansenbcf80392008-06-12 15:21:48 -07001250
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001251 /*
1252 * We can assume that @vma always points to a valid one and @end never
1253 * goes beyond vma->vm_end.
1254 */
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001255 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001256 for (; addr < end; pte++, addr += PAGE_SIZE) {
1257 pagemap_entry_t pme;
Peter Feiner81d0fa62014-10-09 15:28:32 -07001258
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001259 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001260 err = add_to_pagemap(addr, &pme, pm);
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001261 if (err)
Peter Feiner81d0fa62014-10-09 15:28:32 -07001262 break;
Matt Mackall85863e42008-02-04 22:29:04 -08001263 }
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001264 pte_unmap_unlock(orig_pte, ptl);
Matt Mackall85863e42008-02-04 22:29:04 -08001265
1266 cond_resched();
1267
1268 return err;
1269}
1270
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001271#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001272/* This function walks within one hugetlb entry in the single call */
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001273static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001274 unsigned long addr, unsigned long end,
1275 struct mm_walk *walk)
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001276{
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001277 struct pagemapread *pm = walk->private;
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001278 struct vm_area_struct *vma = walk->vma;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001279 u64 flags = 0, frame = 0;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001280 int err = 0;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001281 pte_t pte;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001282
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001283 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001284 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001285
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001286 pte = huge_ptep_get(ptep);
1287 if (pte_present(pte)) {
1288 struct page *page = pte_page(pte);
1289
1290 if (!PageAnon(page))
1291 flags |= PM_FILE;
1292
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001293 if (page_mapcount(page) == 1)
1294 flags |= PM_MMAP_EXCLUSIVE;
1295
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001296 flags |= PM_PRESENT;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001297 if (pm->show_pfn)
1298 frame = pte_pfn(pte) +
1299 ((addr & ~hmask) >> PAGE_SHIFT);
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001300 }
1301
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001302 for (; addr != end; addr += PAGE_SIZE) {
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001303 pagemap_entry_t pme = make_pme(frame, flags);
1304
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001305 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001306 if (err)
1307 return err;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001308 if (pm->show_pfn && (flags & PM_PRESENT))
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001309 frame++;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001310 }
1311
1312 cond_resched();
1313
1314 return err;
1315}
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001316#endif /* HUGETLB_PAGE */
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001317
Matt Mackall85863e42008-02-04 22:29:04 -08001318/*
1319 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1320 *
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001321 * For each page in the address space, this file contains one 64-bit entry
1322 * consisting of the following:
1323 *
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001324 * Bits 0-54 page frame number (PFN) if present
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001325 * Bits 0-4 swap type if swapped
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001326 * Bits 5-54 swap offset if swapped
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001327 * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001328 * Bit 56 page exclusively mapped
1329 * Bits 57-60 zero
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001330 * Bit 61 page is file-page or shared-anon
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001331 * Bit 62 page swapped
1332 * Bit 63 page present
1333 *
1334 * If the page is not present but in swap, then the PFN contains an
1335 * encoding of the swap file number and the page's offset into the
1336 * swap. Unmapped pages return a null PFN. This allows determining
Matt Mackall85863e42008-02-04 22:29:04 -08001337 * precisely which pages are mapped (or in swap) and comparing mapped
1338 * pages between processes.
1339 *
1340 * Efficient users of this interface will use /proc/pid/maps to
1341 * determine which areas of memory are actually mapped and llseek to
1342 * skip over unmapped regions.
1343 */
1344static ssize_t pagemap_read(struct file *file, char __user *buf,
1345 size_t count, loff_t *ppos)
1346{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001347 struct mm_struct *mm = file->private_data;
Matt Mackall85863e42008-02-04 22:29:04 -08001348 struct pagemapread pm;
Alexey Dobriyanee1e6ab2008-07-21 14:21:36 -07001349 struct mm_walk pagemap_walk = {};
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001350 unsigned long src;
1351 unsigned long svpfn;
1352 unsigned long start_vaddr;
1353 unsigned long end_vaddr;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001354 int ret = 0, copied = 0;
Matt Mackall85863e42008-02-04 22:29:04 -08001355
Vegard Nossum388f7932017-02-27 14:30:13 -08001356 if (!mm || !mmget_not_zero(mm))
Matt Mackall85863e42008-02-04 22:29:04 -08001357 goto out;
1358
Matt Mackall85863e42008-02-04 22:29:04 -08001359 ret = -EINVAL;
1360 /* file position must be aligned */
Thomas Tuttleaae86792008-06-05 22:46:31 -07001361 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001362 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001363
1364 ret = 0;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001365 if (!count)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001366 goto out_mm;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001367
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001368 /* do not disclose physical addresses: attack vector */
1369 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1370
yonghua zheng8c829622013-08-13 16:01:03 -07001371 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1372 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001373 ret = -ENOMEM;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001374 if (!pm.buffer)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001375 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001376
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001377 pagemap_walk.pmd_entry = pagemap_pmd_range;
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001378 pagemap_walk.pte_hole = pagemap_pte_hole;
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001379#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001380 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001381#endif
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001382 pagemap_walk.mm = mm;
1383 pagemap_walk.private = &pm;
Matt Mackall85863e42008-02-04 22:29:04 -08001384
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001385 src = *ppos;
1386 svpfn = src / PM_ENTRY_BYTES;
1387 start_vaddr = svpfn << PAGE_SHIFT;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001388 end_vaddr = mm->task_size;
Matt Mackall85863e42008-02-04 22:29:04 -08001389
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001390 /* watch out for wraparound */
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001391 if (svpfn > mm->task_size >> PAGE_SHIFT)
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001392 start_vaddr = end_vaddr;
1393
1394 /*
1395 * The odds are that this will stop walking way
1396 * before end_vaddr, because the length of the
1397 * user buffer is tracked in "pm", and the walk
1398 * will stop when we hit the end of the buffer.
1399 */
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001400 ret = 0;
1401 while (count && (start_vaddr < end_vaddr)) {
1402 int len;
1403 unsigned long end;
Matt Mackall85863e42008-02-04 22:29:04 -08001404
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001405 pm.pos = 0;
Naoya Horiguchiea251c12010-11-24 12:57:13 -08001406 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001407 /* overflow ? */
1408 if (end < start_vaddr || end > end_vaddr)
1409 end = end_vaddr;
1410 down_read(&mm->mmap_sem);
1411 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1412 up_read(&mm->mmap_sem);
1413 start_vaddr = end;
1414
1415 len = min(count, PM_ENTRY_BYTES * pm.pos);
Dan Carpenter309361e02010-04-06 13:45:39 +03001416 if (copy_to_user(buf, pm.buffer, len)) {
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001417 ret = -EFAULT;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001418 goto out_free;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001419 }
1420 copied += len;
1421 buf += len;
1422 count -= len;
Matt Mackall85863e42008-02-04 22:29:04 -08001423 }
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001424 *ppos += copied;
1425 if (!ret || ret == PM_END_OF_BUFFER)
1426 ret = copied;
1427
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001428out_free:
1429 kfree(pm.buffer);
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001430out_mm:
1431 mmput(mm);
Matt Mackall85863e42008-02-04 22:29:04 -08001432out:
1433 return ret;
1434}
1435
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001436static int pagemap_open(struct inode *inode, struct file *file)
1437{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001438 struct mm_struct *mm;
1439
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001440 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1441 if (IS_ERR(mm))
1442 return PTR_ERR(mm);
1443 file->private_data = mm;
1444 return 0;
1445}
1446
1447static int pagemap_release(struct inode *inode, struct file *file)
1448{
1449 struct mm_struct *mm = file->private_data;
1450
1451 if (mm)
1452 mmdrop(mm);
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001453 return 0;
1454}
1455
Matt Mackall85863e42008-02-04 22:29:04 -08001456const struct file_operations proc_pagemap_operations = {
1457 .llseek = mem_lseek, /* borrow this */
1458 .read = pagemap_read,
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001459 .open = pagemap_open,
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001460 .release = pagemap_release,
Matt Mackall85863e42008-02-04 22:29:04 -08001461};
Matt Mackall1e883282008-02-04 22:29:07 -08001462#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall85863e42008-02-04 22:29:04 -08001463
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001464#ifdef CONFIG_NUMA
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001465
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001466struct numa_maps {
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001467 unsigned long pages;
1468 unsigned long anon;
1469 unsigned long active;
1470 unsigned long writeback;
1471 unsigned long mapcount_max;
1472 unsigned long dirty;
1473 unsigned long swapcache;
1474 unsigned long node[MAX_NUMNODES];
1475};
1476
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001477struct numa_maps_private {
1478 struct proc_maps_private proc_maps;
1479 struct numa_maps md;
1480};
1481
Dave Hanseneb4866d2011-09-20 15:19:38 -07001482static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1483 unsigned long nr_pages)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001484{
1485 int count = page_mapcount(page);
1486
Dave Hanseneb4866d2011-09-20 15:19:38 -07001487 md->pages += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001488 if (pte_dirty || PageDirty(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001489 md->dirty += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001490
1491 if (PageSwapCache(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001492 md->swapcache += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001493
1494 if (PageActive(page) || PageUnevictable(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001495 md->active += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001496
1497 if (PageWriteback(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001498 md->writeback += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001499
1500 if (PageAnon(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001501 md->anon += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001502
1503 if (count > md->mapcount_max)
1504 md->mapcount_max = count;
1505
Dave Hanseneb4866d2011-09-20 15:19:38 -07001506 md->node[page_to_nid(page)] += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001507}
1508
Dave Hansen3200a8a2011-09-20 15:19:39 -07001509static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1510 unsigned long addr)
1511{
1512 struct page *page;
1513 int nid;
1514
1515 if (!pte_present(pte))
1516 return NULL;
1517
1518 page = vm_normal_page(vma, addr, pte);
1519 if (!page)
1520 return NULL;
1521
1522 if (PageReserved(page))
1523 return NULL;
1524
1525 nid = page_to_nid(page);
Lai Jiangshan4ff1b2c2012-12-12 13:51:25 -08001526 if (!node_isset(nid, node_states[N_MEMORY]))
Dave Hansen3200a8a2011-09-20 15:19:39 -07001527 return NULL;
1528
1529 return page;
1530}
1531
Gerald Schaefer28093f92016-04-28 16:18:35 -07001532#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1533static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1534 struct vm_area_struct *vma,
1535 unsigned long addr)
1536{
1537 struct page *page;
1538 int nid;
1539
1540 if (!pmd_present(pmd))
1541 return NULL;
1542
1543 page = vm_normal_page_pmd(vma, addr, pmd);
1544 if (!page)
1545 return NULL;
1546
1547 if (PageReserved(page))
1548 return NULL;
1549
1550 nid = page_to_nid(page);
1551 if (!node_isset(nid, node_states[N_MEMORY]))
1552 return NULL;
1553
1554 return page;
1555}
1556#endif
1557
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001558static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1559 unsigned long end, struct mm_walk *walk)
1560{
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001561 struct numa_maps *md = walk->private;
1562 struct vm_area_struct *vma = walk->vma;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001563 spinlock_t *ptl;
1564 pte_t *orig_pte;
1565 pte_t *pte;
1566
Gerald Schaefer28093f92016-04-28 16:18:35 -07001567#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001568 ptl = pmd_trans_huge_lock(pmd, vma);
1569 if (ptl) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001570 struct page *page;
1571
Gerald Schaefer28093f92016-04-28 16:18:35 -07001572 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001573 if (page)
Gerald Schaefer28093f92016-04-28 16:18:35 -07001574 gather_stats(page, md, pmd_dirty(*pmd),
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001575 HPAGE_PMD_SIZE/PAGE_SIZE);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001576 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001577 return 0;
Dave Hansen32ef4382011-09-20 15:19:41 -07001578 }
1579
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001580 if (pmd_trans_unstable(pmd))
1581 return 0;
Gerald Schaefer28093f92016-04-28 16:18:35 -07001582#endif
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001583 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1584 do {
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001585 struct page *page = can_gather_numa_stats(*pte, vma, addr);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001586 if (!page)
1587 continue;
Dave Hanseneb4866d2011-09-20 15:19:38 -07001588 gather_stats(page, md, pte_dirty(*pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001589
1590 } while (pte++, addr += PAGE_SIZE, addr != end);
1591 pte_unmap_unlock(orig_pte, ptl);
Hugh Dickinsa66c04102016-12-12 16:44:47 -08001592 cond_resched();
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001593 return 0;
1594}
1595#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001596static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001597 unsigned long addr, unsigned long end, struct mm_walk *walk)
1598{
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001599 pte_t huge_pte = huge_ptep_get(pte);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001600 struct numa_maps *md;
1601 struct page *page;
1602
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001603 if (!pte_present(huge_pte))
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001604 return 0;
1605
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001606 page = pte_page(huge_pte);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001607 if (!page)
1608 return 0;
1609
1610 md = walk->private;
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001611 gather_stats(page, md, pte_dirty(huge_pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001612 return 0;
1613}
1614
1615#else
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001616static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001617 unsigned long addr, unsigned long end, struct mm_walk *walk)
1618{
1619 return 0;
1620}
1621#endif
1622
1623/*
1624 * Display pages allocated per node and memory policy via /proc.
1625 */
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001626static int show_numa_map(struct seq_file *m, void *v, int is_pid)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001627{
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001628 struct numa_maps_private *numa_priv = m->private;
1629 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001630 struct vm_area_struct *vma = v;
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001631 struct numa_maps *md = &numa_priv->md;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001632 struct file *file = vma->vm_file;
1633 struct mm_struct *mm = vma->vm_mm;
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001634 struct mm_walk walk = {
1635 .hugetlb_entry = gather_hugetlb_stats,
1636 .pmd_entry = gather_pte_stats,
1637 .private = md,
1638 .mm = mm,
1639 };
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001640 struct mempolicy *pol;
David Rientjes948927e2013-11-12 15:07:28 -08001641 char buffer[64];
1642 int nid;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001643
1644 if (!mm)
1645 return 0;
1646
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001647 /* Ensure we start with an empty set of numa_maps statistics. */
1648 memset(md, 0, sizeof(*md));
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001649
Oleg Nesterov498f2372014-10-09 15:27:52 -07001650 pol = __get_vma_policy(vma, vma->vm_start);
1651 if (pol) {
1652 mpol_to_str(buffer, sizeof(buffer), pol);
1653 mpol_cond_put(pol);
1654 } else {
1655 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1656 }
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001657
1658 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1659
1660 if (file) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001661 seq_puts(m, " file=");
Miklos Szeredi2726d562015-06-19 10:30:28 +02001662 seq_file_path(m, file, "\n\t= ");
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001663 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001664 seq_puts(m, " heap");
Andy Lutomirskib18cb642016-09-30 10:58:57 -07001665 } else if (is_stack(proc_priv, vma)) {
Johannes Weiner65376df2016-02-02 16:57:29 -08001666 seq_puts(m, " stack");
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001667 }
1668
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001669 if (is_vm_hugetlb_page(vma))
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001670 seq_puts(m, " huge");
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001671
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001672 /* mmap_sem is held by m_start */
1673 walk_page_vma(vma, &walk);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001674
1675 if (!md->pages)
1676 goto out;
1677
1678 if (md->anon)
1679 seq_printf(m, " anon=%lu", md->anon);
1680
1681 if (md->dirty)
1682 seq_printf(m, " dirty=%lu", md->dirty);
1683
1684 if (md->pages != md->anon && md->pages != md->dirty)
1685 seq_printf(m, " mapped=%lu", md->pages);
1686
1687 if (md->mapcount_max > 1)
1688 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1689
1690 if (md->swapcache)
1691 seq_printf(m, " swapcache=%lu", md->swapcache);
1692
1693 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1694 seq_printf(m, " active=%lu", md->active);
1695
1696 if (md->writeback)
1697 seq_printf(m, " writeback=%lu", md->writeback);
1698
David Rientjes948927e2013-11-12 15:07:28 -08001699 for_each_node_state(nid, N_MEMORY)
1700 if (md->node[nid])
1701 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
Rafael Aquini198d1592015-02-12 15:01:08 -08001702
1703 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001704out:
1705 seq_putc(m, '\n');
Oleg Nesterovb8c20a92014-10-09 15:25:41 -07001706 m_cache_vma(m, vma);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001707 return 0;
1708}
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001709
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001710static int show_pid_numa_map(struct seq_file *m, void *v)
1711{
1712 return show_numa_map(m, v, 1);
1713}
1714
1715static int show_tid_numa_map(struct seq_file *m, void *v)
1716{
1717 return show_numa_map(m, v, 0);
1718}
1719
Jan Engelhardt03a44822008-02-08 04:21:19 -08001720static const struct seq_operations proc_pid_numa_maps_op = {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001721 .start = m_start,
1722 .next = m_next,
1723 .stop = m_stop,
1724 .show = show_pid_numa_map,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001725};
Eric W. Biederman662795d2006-06-26 00:25:48 -07001726
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001727static const struct seq_operations proc_tid_numa_maps_op = {
1728 .start = m_start,
1729 .next = m_next,
1730 .stop = m_stop,
1731 .show = show_tid_numa_map,
1732};
1733
1734static int numa_maps_open(struct inode *inode, struct file *file,
1735 const struct seq_operations *ops)
Eric W. Biederman662795d2006-06-26 00:25:48 -07001736{
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -07001737 return proc_maps_open(inode, file, ops,
1738 sizeof(struct numa_maps_private));
Eric W. Biederman662795d2006-06-26 00:25:48 -07001739}
1740
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001741static int pid_numa_maps_open(struct inode *inode, struct file *file)
1742{
1743 return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1744}
1745
1746static int tid_numa_maps_open(struct inode *inode, struct file *file)
1747{
1748 return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1749}
1750
1751const struct file_operations proc_pid_numa_maps_operations = {
1752 .open = pid_numa_maps_open,
1753 .read = seq_read,
1754 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -07001755 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001756};
1757
1758const struct file_operations proc_tid_numa_maps_operations = {
1759 .open = tid_numa_maps_open,
Eric W. Biederman662795d2006-06-26 00:25:48 -07001760 .read = seq_read,
1761 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -07001762 .release = proc_map_release,
Eric W. Biederman662795d2006-06-26 00:25:48 -07001763};
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001764#endif /* CONFIG_NUMA */