blob: fb52b548080da50cf2cece633c2585601485fc09 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/mm.h>
2#include <linux/hugetlb.h>
Dave Hansen22e057c2011-03-22 16:33:00 -07003#include <linux/huge_mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/mount.h>
5#include <linux/seq_file.h>
Mauricio Line070ad42005-09-03 15:55:10 -07006#include <linux/highmem.h>
Kees Cook5096add2007-05-08 00:26:04 -07007#include <linux/ptrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07009#include <linux/pagemap.h>
10#include <linux/mempolicy.h>
Dave Hansen22e057c2011-03-22 16:33:00 -070011#include <linux/rmap.h>
Matt Mackall85863e42008-02-04 22:29:04 -080012#include <linux/swap.h>
13#include <linux/swapops.h>
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -070014#include <linux/mmu_notifier.h>
Mauricio Line070ad42005-09-03 15:55:10 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/elf.h>
17#include <asm/uaccess.h>
Mauricio Line070ad42005-09-03 15:55:10 -070018#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "internal.h"
20
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080021void task_mem(struct seq_file *m, struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080023 unsigned long data, text, lib, swap;
Hugh Dickins365e9c872005-10-29 18:16:18 -070024 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
25
26 /*
27 * Note: to minimize their overhead, mm maintains hiwater_vm and
28 * hiwater_rss only when about to *lower* total_vm or rss. Any
29 * collector of these hiwater stats must therefore get total_vm
30 * and rss too, which will usually be the higher. Barriers? not
31 * worth the effort, such snapshots can always be inconsistent.
32 */
33 hiwater_vm = total_vm = mm->total_vm;
34 if (hiwater_vm < mm->hiwater_vm)
35 hiwater_vm = mm->hiwater_vm;
36 hiwater_rss = total_rss = get_mm_rss(mm);
37 if (hiwater_rss < mm->hiwater_rss)
38 hiwater_rss = mm->hiwater_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 data = mm->total_vm - mm->shared_vm - mm->stack_vm;
41 text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
42 lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080043 swap = get_mm_counter(mm, MM_SWAPENTS);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080044 seq_printf(m,
Hugh Dickins365e9c872005-10-29 18:16:18 -070045 "VmPeak:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 "VmSize:\t%8lu kB\n"
47 "VmLck:\t%8lu kB\n"
Christoph Lameterbc3e53f2011-10-31 17:07:30 -070048 "VmPin:\t%8lu kB\n"
Hugh Dickins365e9c872005-10-29 18:16:18 -070049 "VmHWM:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 "VmRSS:\t%8lu kB\n"
51 "VmData:\t%8lu kB\n"
52 "VmStk:\t%8lu kB\n"
53 "VmExe:\t%8lu kB\n"
54 "VmLib:\t%8lu kB\n"
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080055 "VmPTE:\t%8lu kB\n"
56 "VmSwap:\t%8lu kB\n",
Hugh Dickins365e9c872005-10-29 18:16:18 -070057 hiwater_vm << (PAGE_SHIFT-10),
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -070058 total_vm << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 mm->locked_vm << (PAGE_SHIFT-10),
Christoph Lameterbc3e53f2011-10-31 17:07:30 -070060 mm->pinned_vm << (PAGE_SHIFT-10),
Hugh Dickins365e9c872005-10-29 18:16:18 -070061 hiwater_rss << (PAGE_SHIFT-10),
62 total_rss << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 data << (PAGE_SHIFT-10),
64 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -080065 (PTRS_PER_PTE * sizeof(pte_t) *
66 atomic_long_read(&mm->nr_ptes)) >> 10,
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080067 swap << (PAGE_SHIFT-10));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70unsigned long task_vsize(struct mm_struct *mm)
71{
72 return PAGE_SIZE * mm->total_vm;
73}
74
Alexey Dobriyana2ade7b2011-01-12 17:00:32 -080075unsigned long task_statm(struct mm_struct *mm,
76 unsigned long *shared, unsigned long *text,
77 unsigned long *data, unsigned long *resident)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -080079 *shared = get_mm_counter(mm, MM_FILEPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
81 >> PAGE_SHIFT;
82 *data = mm->total_vm - mm->shared_vm;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -080083 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return mm->total_vm;
85}
86
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +090087#ifdef CONFIG_NUMA
88/*
89 * These functions are for numa_maps but called in generic **maps seq_file
90 * ->start(), ->stop() ops.
91 *
92 * numa_maps scans all vmas under mmap_sem and checks their mempolicy.
93 * Each mempolicy object is controlled by reference counting. The problem here
94 * is how to avoid accessing dead mempolicy object.
95 *
96 * Because we're holding mmap_sem while reading seq_file, it's safe to access
97 * each vma's mempolicy, no vma objects will never drop refs to mempolicy.
98 *
99 * A task's mempolicy (task->mempolicy) has different behavior. task->mempolicy
100 * is set and replaced under mmap_sem but unrefed and cleared under task_lock().
101 * So, without task_lock(), we cannot trust get_vma_policy() because we cannot
102 * gurantee the task never exits under us. But taking task_lock() around
103 * get_vma_plicy() causes lock order problem.
104 *
105 * To access task->mempolicy without lock, we hold a reference count of an
106 * object pointed by task->mempolicy and remember it. This will guarantee
107 * that task->mempolicy points to an alive object or NULL in numa_maps accesses.
108 */
109static void hold_task_mempolicy(struct proc_maps_private *priv)
110{
111 struct task_struct *task = priv->task;
112
113 task_lock(task);
114 priv->task_mempolicy = task->mempolicy;
115 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
Matt Mackalla6198792008-02-04 22:29:03 -0800131static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
Mauricio Line070ad42005-09-03 15:55:10 -0700132{
Matt Mackalla6198792008-02-04 22:29:03 -0800133 if (vma && vma != priv->tail_vma) {
134 struct mm_struct *mm = vma->vm_mm;
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900135 release_task_mempolicy(priv);
Matt Mackalla6198792008-02-04 22:29:03 -0800136 up_read(&mm->mmap_sem);
137 mmput(mm);
138 }
139}
140
141static void *m_start(struct seq_file *m, loff_t *pos)
142{
143 struct proc_maps_private *priv = m->private;
144 unsigned long last_addr = m->version;
145 struct mm_struct *mm;
146 struct vm_area_struct *vma, *tail_vma = NULL;
147 loff_t l = *pos;
148
149 /* Clear the per syscall fields in priv */
150 priv->task = NULL;
151 priv->tail_vma = NULL;
152
153 /*
154 * We remember last_addr rather than next_addr to hit with
155 * mmap_cache most of the time. We have zero last_addr at
156 * the beginning and also after lseek. We will have -1 last_addr
157 * after the end of the vmas.
158 */
159
160 if (last_addr == -1UL)
161 return NULL;
162
163 priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
164 if (!priv->task)
Al Viroec6fd8a2011-02-15 22:22:54 -0500165 return ERR_PTR(-ESRCH);
Matt Mackalla6198792008-02-04 22:29:03 -0800166
Cong Wange7dcd992012-05-31 16:26:17 -0700167 mm = mm_access(priv->task, PTRACE_MODE_READ);
Al Viroec6fd8a2011-02-15 22:22:54 -0500168 if (!mm || IS_ERR(mm))
169 return mm;
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200170 down_read(&mm->mmap_sem);
Matt Mackalla6198792008-02-04 22:29:03 -0800171
Stephen Wilson31db58b2011-03-13 15:49:15 -0400172 tail_vma = get_gate_vma(priv->task->mm);
Matt Mackalla6198792008-02-04 22:29:03 -0800173 priv->tail_vma = tail_vma;
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900174 hold_task_mempolicy(priv);
Matt Mackalla6198792008-02-04 22:29:03 -0800175 /* Start with last addr hint */
176 vma = find_vma(mm, last_addr);
177 if (last_addr && vma) {
178 vma = vma->vm_next;
179 goto out;
180 }
181
182 /*
183 * Check the vma index is within the range and do
184 * sequential scan until m_index.
185 */
186 vma = NULL;
187 if ((unsigned long)l < mm->map_count) {
188 vma = mm->mmap;
189 while (l-- && vma)
190 vma = vma->vm_next;
191 goto out;
192 }
193
194 if (l != mm->map_count)
195 tail_vma = NULL; /* After gate vma */
196
197out:
198 if (vma)
199 return vma;
200
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900201 release_task_mempolicy(priv);
Matt Mackalla6198792008-02-04 22:29:03 -0800202 /* End of vmas has been reached */
203 m->version = (tail_vma != NULL)? 0: -1UL;
204 up_read(&mm->mmap_sem);
205 mmput(mm);
206 return tail_vma;
207}
208
209static void *m_next(struct seq_file *m, void *v, loff_t *pos)
210{
211 struct proc_maps_private *priv = m->private;
212 struct vm_area_struct *vma = v;
213 struct vm_area_struct *tail_vma = priv->tail_vma;
214
215 (*pos)++;
216 if (vma && (vma != tail_vma) && vma->vm_next)
217 return vma->vm_next;
218 vma_stop(priv, vma);
219 return (vma != tail_vma)? tail_vma: NULL;
220}
221
222static void m_stop(struct seq_file *m, void *v)
223{
224 struct proc_maps_private *priv = m->private;
225 struct vm_area_struct *vma = v;
226
Linus Torvalds76597cd2011-03-27 19:09:29 -0700227 if (!IS_ERR(vma))
228 vma_stop(priv, vma);
Matt Mackalla6198792008-02-04 22:29:03 -0800229 if (priv->task)
230 put_task_struct(priv->task);
231}
232
233static int do_maps_open(struct inode *inode, struct file *file,
Jan Engelhardt03a44822008-02-08 04:21:19 -0800234 const struct seq_operations *ops)
Matt Mackalla6198792008-02-04 22:29:03 -0800235{
236 struct proc_maps_private *priv;
237 int ret = -ENOMEM;
238 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
239 if (priv) {
240 priv->pid = proc_pid(inode);
241 ret = seq_open(file, ops);
242 if (!ret) {
243 struct seq_file *m = file->private_data;
244 m->private = priv;
245 } else {
246 kfree(priv);
247 }
248 }
249 return ret;
250}
Mauricio Line070ad42005-09-03 15:55:10 -0700251
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700252static void
253show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Mauricio Line070ad42005-09-03 15:55:10 -0700255 struct mm_struct *mm = vma->vm_mm;
256 struct file *file = vma->vm_file;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700257 struct proc_maps_private *priv = m->private;
258 struct task_struct *task = priv->task;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900259 vm_flags_t flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 unsigned long ino = 0;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700261 unsigned long long pgoff = 0;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200262 unsigned long start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 dev_t dev = 0;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700264 const char *name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if (file) {
Al Viro496ad9a2013-01-23 17:07:38 -0500267 struct inode *inode = file_inode(vma->vm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 dev = inode->i_sb->s_dev;
269 ino = inode->i_ino;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700270 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
Linus Torvaldsd7824372010-08-15 11:35:52 -0700273 /* We don't show the stack guard page in /proc/maps */
274 start = vma->vm_start;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200275 if (stack_guard_page_start(vma, start))
276 start += PAGE_SIZE;
277 end = vma->vm_end;
278 if (stack_guard_page_end(vma, end))
279 end -= PAGE_SIZE;
Linus Torvaldsd7824372010-08-15 11:35:52 -0700280
Tetsuo Handa652586d2013-11-14 14:31:57 -0800281 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
282 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
Linus Torvaldsd7824372010-08-15 11:35:52 -0700283 start,
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200284 end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 flags & VM_READ ? 'r' : '-',
286 flags & VM_WRITE ? 'w' : '-',
287 flags & VM_EXEC ? 'x' : '-',
288 flags & VM_MAYSHARE ? 's' : 'p',
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700289 pgoff,
Tetsuo Handa652586d2013-11-14 14:31:57 -0800290 MAJOR(dev), MINOR(dev), ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /*
293 * Print the dentry name for named mappings, and a
294 * special [heap] marker for the heap:
295 */
Mauricio Line070ad42005-09-03 15:55:10 -0700296 if (file) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800297 seq_pad(m, ' ');
Jan Blunckc32c2f62008-02-14 19:38:43 -0800298 seq_path(m, &file->f_path, "\n");
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700299 goto done;
300 }
301
302 name = arch_vma_name(vma);
303 if (!name) {
304 pid_t tid;
305
306 if (!mm) {
307 name = "[vdso]";
308 goto done;
309 }
310
311 if (vma->vm_start <= mm->brk &&
312 vma->vm_end >= mm->start_brk) {
313 name = "[heap]";
314 goto done;
315 }
316
317 tid = vm_is_stack(task, vma, is_pid);
318
319 if (tid != 0) {
320 /*
321 * Thread stack in /proc/PID/task/TID/maps or
322 * the main process stack.
323 */
324 if (!is_pid || (vma->vm_start <= mm->start_stack &&
325 vma->vm_end >= mm->start_stack)) {
326 name = "[stack]";
Ingo Molnare6e54942006-06-27 02:53:50 -0700327 } else {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700328 /* Thread stack in /proc/PID/maps */
Tetsuo Handa652586d2013-11-14 14:31:57 -0800329 seq_pad(m, ' ');
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700330 seq_printf(m, "[stack:%d]", tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Ingo Molnare6e54942006-06-27 02:53:50 -0700332 }
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700333 }
334
335done:
336 if (name) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800337 seq_pad(m, ' ');
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700338 seq_puts(m, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 seq_putc(m, '\n');
Joe Korty7c88db02008-10-16 15:27:09 +0400341}
342
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700343static int show_map(struct seq_file *m, void *v, int is_pid)
Joe Korty7c88db02008-10-16 15:27:09 +0400344{
345 struct vm_area_struct *vma = v;
346 struct proc_maps_private *priv = m->private;
347 struct task_struct *task = priv->task;
348
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700349 show_map_vma(m, vma, is_pid);
Mauricio Line070ad42005-09-03 15:55:10 -0700350
Mauricio Line070ad42005-09-03 15:55:10 -0700351 if (m->count < m->size) /* vma is copied successfully */
Stephen Wilson31db58b2011-03-13 15:49:15 -0400352 m->version = (vma != get_gate_vma(task->mm))
353 ? vma->vm_start : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355}
356
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700357static int show_pid_map(struct seq_file *m, void *v)
358{
359 return show_map(m, v, 1);
360}
361
362static int show_tid_map(struct seq_file *m, void *v)
363{
364 return show_map(m, v, 0);
365}
366
Jan Engelhardt03a44822008-02-08 04:21:19 -0800367static const struct seq_operations proc_pid_maps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800368 .start = m_start,
369 .next = m_next,
370 .stop = m_stop,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700371 .show = show_pid_map
Matt Mackalla6198792008-02-04 22:29:03 -0800372};
373
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700374static const struct seq_operations proc_tid_maps_op = {
375 .start = m_start,
376 .next = m_next,
377 .stop = m_stop,
378 .show = show_tid_map
379};
380
381static int pid_maps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800382{
383 return do_maps_open(inode, file, &proc_pid_maps_op);
384}
385
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700386static int tid_maps_open(struct inode *inode, struct file *file)
387{
388 return do_maps_open(inode, file, &proc_tid_maps_op);
389}
390
391const struct file_operations proc_pid_maps_operations = {
392 .open = pid_maps_open,
393 .read = seq_read,
394 .llseek = seq_lseek,
395 .release = seq_release_private,
396};
397
398const struct file_operations proc_tid_maps_operations = {
399 .open = tid_maps_open,
Matt Mackalla6198792008-02-04 22:29:03 -0800400 .read = seq_read,
401 .llseek = seq_lseek,
402 .release = seq_release_private,
403};
404
405/*
406 * Proportional Set Size(PSS): my share of RSS.
407 *
408 * PSS of a process is the count of pages it has in memory, where each
409 * page is divided by the number of processes sharing it. So if a
410 * process has 1000 pages all to itself, and 1000 shared with one other
411 * process, its PSS will be 1500.
412 *
413 * To keep (accumulated) division errors low, we adopt a 64bit
414 * fixed-point pss counter to minimize division errors. So (pss >>
415 * PSS_SHIFT) would be the real byte count.
416 *
417 * A shift of 12 before division means (assuming 4K page size):
418 * - 1M 3-user-pages add up to 8KB errors;
419 * - supports mapcount up to 2^24, or 16M;
420 * - supports PSS up to 2^52 bytes, or 4PB.
421 */
422#define PSS_SHIFT 12
423
Matt Mackall1e883282008-02-04 22:29:07 -0800424#ifdef CONFIG_PROC_PAGE_MONITOR
Peter Zijlstra214e4712008-04-28 02:12:55 -0700425struct mem_size_stats {
Matt Mackalla6198792008-02-04 22:29:03 -0800426 struct vm_area_struct *vma;
427 unsigned long resident;
428 unsigned long shared_clean;
429 unsigned long shared_dirty;
430 unsigned long private_clean;
431 unsigned long private_dirty;
432 unsigned long referenced;
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700433 unsigned long anonymous;
Dave Hansen4031a212011-03-22 16:33:01 -0700434 unsigned long anonymous_thp;
Peter Zijlstra214e4712008-04-28 02:12:55 -0700435 unsigned long swap;
Konstantin Khlebnikovbca15542012-05-31 16:26:20 -0700436 unsigned long nonlinear;
Matt Mackalla6198792008-02-04 22:29:03 -0800437 u64 pss;
438};
439
Dave Hansenae11c4d2011-03-22 16:32:58 -0700440
441static void smaps_pte_entry(pte_t ptent, unsigned long addr,
Dave Hansen3c9acc72011-03-22 16:32:59 -0700442 unsigned long ptent_size, struct mm_walk *walk)
Dave Hansenae11c4d2011-03-22 16:32:58 -0700443{
444 struct mem_size_stats *mss = walk->private;
445 struct vm_area_struct *vma = mss->vma;
Konstantin Khlebnikovbca15542012-05-31 16:26:20 -0700446 pgoff_t pgoff = linear_page_index(vma, addr);
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700447 struct page *page = NULL;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700448 int mapcount;
449
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700450 if (pte_present(ptent)) {
451 page = vm_normal_page(vma, addr, ptent);
452 } else if (is_swap_pte(ptent)) {
453 swp_entry_t swpent = pte_to_swp_entry(ptent);
454
455 if (!non_swap_entry(swpent))
456 mss->swap += ptent_size;
457 else if (is_migration_entry(swpent))
458 page = migration_entry_to_page(swpent);
Konstantin Khlebnikovbca15542012-05-31 16:26:20 -0700459 } else if (pte_file(ptent)) {
460 if (pte_to_pgoff(ptent) != pgoff)
461 mss->nonlinear += ptent_size;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700462 }
463
Dave Hansenae11c4d2011-03-22 16:32:58 -0700464 if (!page)
465 return;
466
467 if (PageAnon(page))
Dave Hansen3c9acc72011-03-22 16:32:59 -0700468 mss->anonymous += ptent_size;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700469
Konstantin Khlebnikovbca15542012-05-31 16:26:20 -0700470 if (page->index != pgoff)
471 mss->nonlinear += ptent_size;
472
Dave Hansen3c9acc72011-03-22 16:32:59 -0700473 mss->resident += ptent_size;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700474 /* Accumulate the size in pages that have been accessed. */
475 if (pte_young(ptent) || PageReferenced(page))
Dave Hansen3c9acc72011-03-22 16:32:59 -0700476 mss->referenced += ptent_size;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700477 mapcount = page_mapcount(page);
478 if (mapcount >= 2) {
479 if (pte_dirty(ptent) || PageDirty(page))
Dave Hansen3c9acc72011-03-22 16:32:59 -0700480 mss->shared_dirty += ptent_size;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700481 else
Dave Hansen3c9acc72011-03-22 16:32:59 -0700482 mss->shared_clean += ptent_size;
483 mss->pss += (ptent_size << PSS_SHIFT) / mapcount;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700484 } else {
485 if (pte_dirty(ptent) || PageDirty(page))
Dave Hansen3c9acc72011-03-22 16:32:59 -0700486 mss->private_dirty += ptent_size;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700487 else
Dave Hansen3c9acc72011-03-22 16:32:59 -0700488 mss->private_clean += ptent_size;
489 mss->pss += (ptent_size << PSS_SHIFT);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700490 }
491}
492
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800493static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700494 struct mm_walk *walk)
Mauricio Line070ad42005-09-03 15:55:10 -0700495{
Dave Hansen21650092008-06-12 15:21:47 -0700496 struct mem_size_stats *mss = walk->private;
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800497 struct vm_area_struct *vma = mss->vma;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700498 pte_t *pte;
Hugh Dickins705e87c2005-10-29 18:16:27 -0700499 spinlock_t *ptl;
Mauricio Line070ad42005-09-03 15:55:10 -0700500
Kirill A. Shutemovbf929152013-11-14 14:30:54 -0800501 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -0700502 smaps_pte_entry(*(pte_t *)pmd, addr, HPAGE_PMD_SIZE, walk);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -0800503 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -0700504 mss->anonymous_thp += HPAGE_PMD_SIZE;
505 return 0;
Dave Hansen22e057c2011-03-22 16:33:00 -0700506 }
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700507
508 if (pmd_trans_unstable(pmd))
509 return 0;
Dave Hansen22e057c2011-03-22 16:33:00 -0700510 /*
511 * The mmap_sem held all the way back in m_start() is what
512 * keeps khugepaged out of here and from collapsing things
513 * in here.
514 */
Hugh Dickins705e87c2005-10-29 18:16:27 -0700515 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700516 for (; addr != end; pte++, addr += PAGE_SIZE)
Dave Hansen3c9acc72011-03-22 16:32:59 -0700517 smaps_pte_entry(*pte, addr, PAGE_SIZE, walk);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700518 pte_unmap_unlock(pte - 1, ptl);
519 cond_resched();
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800520 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700521}
522
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800523static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
524{
525 /*
526 * Don't forget to update Documentation/ on changes.
527 */
528 static const char mnemonics[BITS_PER_LONG][2] = {
529 /*
530 * In case if we meet a flag we don't know about.
531 */
532 [0 ... (BITS_PER_LONG-1)] = "??",
533
534 [ilog2(VM_READ)] = "rd",
535 [ilog2(VM_WRITE)] = "wr",
536 [ilog2(VM_EXEC)] = "ex",
537 [ilog2(VM_SHARED)] = "sh",
538 [ilog2(VM_MAYREAD)] = "mr",
539 [ilog2(VM_MAYWRITE)] = "mw",
540 [ilog2(VM_MAYEXEC)] = "me",
541 [ilog2(VM_MAYSHARE)] = "ms",
542 [ilog2(VM_GROWSDOWN)] = "gd",
543 [ilog2(VM_PFNMAP)] = "pf",
544 [ilog2(VM_DENYWRITE)] = "dw",
545 [ilog2(VM_LOCKED)] = "lo",
546 [ilog2(VM_IO)] = "io",
547 [ilog2(VM_SEQ_READ)] = "sr",
548 [ilog2(VM_RAND_READ)] = "rr",
549 [ilog2(VM_DONTCOPY)] = "dc",
550 [ilog2(VM_DONTEXPAND)] = "de",
551 [ilog2(VM_ACCOUNT)] = "ac",
552 [ilog2(VM_NORESERVE)] = "nr",
553 [ilog2(VM_HUGETLB)] = "ht",
554 [ilog2(VM_NONLINEAR)] = "nl",
555 [ilog2(VM_ARCH_1)] = "ar",
556 [ilog2(VM_DONTDUMP)] = "dd",
Naoya Horiguchiec8e41a2013-11-12 15:07:49 -0800557#ifdef CONFIG_MEM_SOFT_DIRTY
558 [ilog2(VM_SOFTDIRTY)] = "sd",
559#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800560 [ilog2(VM_MIXEDMAP)] = "mm",
561 [ilog2(VM_HUGEPAGE)] = "hg",
562 [ilog2(VM_NOHUGEPAGE)] = "nh",
563 [ilog2(VM_MERGEABLE)] = "mg",
564 };
565 size_t i;
566
567 seq_puts(m, "VmFlags: ");
568 for (i = 0; i < BITS_PER_LONG; i++) {
569 if (vma->vm_flags & (1UL << i)) {
570 seq_printf(m, "%c%c ",
571 mnemonics[i][0], mnemonics[i][1]);
572 }
573 }
574 seq_putc(m, '\n');
575}
576
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700577static int show_smap(struct seq_file *m, void *v, int is_pid)
Mauricio Line070ad42005-09-03 15:55:10 -0700578{
Joe Korty7c88db02008-10-16 15:27:09 +0400579 struct proc_maps_private *priv = m->private;
580 struct task_struct *task = priv->task;
Mauricio Line070ad42005-09-03 15:55:10 -0700581 struct vm_area_struct *vma = v;
Mauricio Line070ad42005-09-03 15:55:10 -0700582 struct mem_size_stats mss;
Dave Hansen21650092008-06-12 15:21:47 -0700583 struct mm_walk smaps_walk = {
584 .pmd_entry = smaps_pte_range,
585 .mm = vma->vm_mm,
586 .private = &mss,
587 };
Mauricio Line070ad42005-09-03 15:55:10 -0700588
589 memset(&mss, 0, sizeof mss);
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800590 mss.vma = vma;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +0900591 /* mmap_sem is held in m_start */
Nick Piggin5ddfae12006-03-06 15:42:57 -0800592 if (vma->vm_mm && !is_vm_hugetlb_page(vma))
Dave Hansen21650092008-06-12 15:21:47 -0700593 walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
Matt Mackall4752c362008-02-04 22:29:02 -0800594
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700595 show_map_vma(m, vma, is_pid);
Matt Mackall4752c362008-02-04 22:29:02 -0800596
597 seq_printf(m,
598 "Size: %8lu kB\n"
599 "Rss: %8lu kB\n"
600 "Pss: %8lu kB\n"
601 "Shared_Clean: %8lu kB\n"
602 "Shared_Dirty: %8lu kB\n"
603 "Private_Clean: %8lu kB\n"
604 "Private_Dirty: %8lu kB\n"
Peter Zijlstra214e4712008-04-28 02:12:55 -0700605 "Referenced: %8lu kB\n"
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700606 "Anonymous: %8lu kB\n"
Dave Hansen4031a212011-03-22 16:33:01 -0700607 "AnonHugePages: %8lu kB\n"
Mel Gorman08fba692009-01-06 14:38:53 -0800608 "Swap: %8lu kB\n"
Mel Gorman33402892009-01-06 14:38:54 -0800609 "KernelPageSize: %8lu kB\n"
Nikanth Karthikesan2d905082011-01-13 15:45:53 -0800610 "MMUPageSize: %8lu kB\n"
611 "Locked: %8lu kB\n",
Matt Mackall4752c362008-02-04 22:29:02 -0800612 (vma->vm_end - vma->vm_start) >> 10,
613 mss.resident >> 10,
614 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
615 mss.shared_clean >> 10,
616 mss.shared_dirty >> 10,
617 mss.private_clean >> 10,
618 mss.private_dirty >> 10,
Peter Zijlstra214e4712008-04-28 02:12:55 -0700619 mss.referenced >> 10,
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700620 mss.anonymous >> 10,
Dave Hansen4031a212011-03-22 16:33:01 -0700621 mss.anonymous_thp >> 10,
Mel Gorman08fba692009-01-06 14:38:53 -0800622 mss.swap >> 10,
Mel Gorman33402892009-01-06 14:38:54 -0800623 vma_kernel_pagesize(vma) >> 10,
Nikanth Karthikesan2d905082011-01-13 15:45:53 -0800624 vma_mmu_pagesize(vma) >> 10,
625 (vma->vm_flags & VM_LOCKED) ?
626 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
Matt Mackall4752c362008-02-04 22:29:02 -0800627
Konstantin Khlebnikovbca15542012-05-31 16:26:20 -0700628 if (vma->vm_flags & VM_NONLINEAR)
629 seq_printf(m, "Nonlinear: %8lu kB\n",
630 mss.nonlinear >> 10);
631
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800632 show_smap_vma_flags(m, vma);
633
Joe Korty7c88db02008-10-16 15:27:09 +0400634 if (m->count < m->size) /* vma is copied successfully */
Stephen Wilson31db58b2011-03-13 15:49:15 -0400635 m->version = (vma != get_gate_vma(task->mm))
636 ? vma->vm_start : 0;
Joe Korty7c88db02008-10-16 15:27:09 +0400637 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700638}
639
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700640static int show_pid_smap(struct seq_file *m, void *v)
641{
642 return show_smap(m, v, 1);
643}
644
645static int show_tid_smap(struct seq_file *m, void *v)
646{
647 return show_smap(m, v, 0);
648}
649
Jan Engelhardt03a44822008-02-08 04:21:19 -0800650static const struct seq_operations proc_pid_smaps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800651 .start = m_start,
652 .next = m_next,
653 .stop = m_stop,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700654 .show = show_pid_smap
Matt Mackalla6198792008-02-04 22:29:03 -0800655};
656
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700657static const struct seq_operations proc_tid_smaps_op = {
658 .start = m_start,
659 .next = m_next,
660 .stop = m_stop,
661 .show = show_tid_smap
662};
663
664static int pid_smaps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800665{
666 return do_maps_open(inode, file, &proc_pid_smaps_op);
667}
668
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700669static int tid_smaps_open(struct inode *inode, struct file *file)
670{
671 return do_maps_open(inode, file, &proc_tid_smaps_op);
672}
673
674const struct file_operations proc_pid_smaps_operations = {
675 .open = pid_smaps_open,
676 .read = seq_read,
677 .llseek = seq_lseek,
678 .release = seq_release_private,
679};
680
681const struct file_operations proc_tid_smaps_operations = {
682 .open = tid_smaps_open,
Matt Mackalla6198792008-02-04 22:29:03 -0800683 .read = seq_read,
684 .llseek = seq_lseek,
685 .release = seq_release_private,
686};
687
Pavel Emelyanov541c2372013-07-03 15:01:22 -0700688/*
689 * We do not want to have constant page-shift bits sitting in
690 * pagemap entries and are about to reuse them some time soon.
691 *
692 * Here's the "migration strategy":
693 * 1. when the system boots these bits remain what they are,
694 * but a warning about future change is printed in log;
695 * 2. once anyone clears soft-dirty bits via clear_refs file,
696 * these flag is set to denote, that user is aware of the
697 * new API and those page-shift bits change their meaning.
698 * The respective warning is printed in dmesg;
699 * 3. In a couple of releases we will remove all the mentions
700 * of page-shift in pagemap entries.
701 */
702
703static bool soft_dirty_cleared __read_mostly;
704
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700705enum clear_refs_types {
706 CLEAR_REFS_ALL = 1,
707 CLEAR_REFS_ANON,
708 CLEAR_REFS_MAPPED,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700709 CLEAR_REFS_SOFT_DIRTY,
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700710 CLEAR_REFS_LAST,
711};
712
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700713struct clear_refs_private {
714 struct vm_area_struct *vma;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700715 enum clear_refs_types type;
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700716};
717
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700718static inline void clear_soft_dirty(struct vm_area_struct *vma,
719 unsigned long addr, pte_t *pte)
720{
721#ifdef CONFIG_MEM_SOFT_DIRTY
722 /*
723 * The soft-dirty tracker uses #PF-s to catch writes
724 * to pages, so write-protect the pte as well. See the
725 * Documentation/vm/soft-dirty.txt for full description
726 * of how soft-dirty works.
727 */
728 pte_t ptent = *pte;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700729
730 if (pte_present(ptent)) {
731 ptent = pte_wrprotect(ptent);
732 ptent = pte_clear_flags(ptent, _PAGE_SOFT_DIRTY);
733 } else if (is_swap_pte(ptent)) {
734 ptent = pte_swp_clear_soft_dirty(ptent);
Cyrill Gorcunov41bb3472013-08-13 16:00:51 -0700735 } else if (pte_file(ptent)) {
736 ptent = pte_file_clear_soft_dirty(ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700737 }
738
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -0700739 if (vma->vm_flags & VM_SOFTDIRTY)
740 vma->vm_flags &= ~VM_SOFTDIRTY;
741
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700742 set_pte_at(vma->vm_mm, addr, pte, ptent);
743#endif
744}
745
Matt Mackalla6198792008-02-04 22:29:03 -0800746static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
Dave Hansen21650092008-06-12 15:21:47 -0700747 unsigned long end, struct mm_walk *walk)
Matt Mackalla6198792008-02-04 22:29:03 -0800748{
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700749 struct clear_refs_private *cp = walk->private;
750 struct vm_area_struct *vma = cp->vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800751 pte_t *pte, ptent;
752 spinlock_t *ptl;
753 struct page *page;
754
Kirill A. Shutemove1803772012-12-12 13:50:59 -0800755 split_huge_page_pmd(vma, addr, pmd);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700756 if (pmd_trans_unstable(pmd))
757 return 0;
Dave Hansen03319322011-03-22 16:32:56 -0700758
Matt Mackalla6198792008-02-04 22:29:03 -0800759 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
760 for (; addr != end; pte++, addr += PAGE_SIZE) {
761 ptent = *pte;
Matt Mackalla6198792008-02-04 22:29:03 -0800762
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700763 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
764 clear_soft_dirty(vma, addr, pte);
765 continue;
766 }
767
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700768 if (!pte_present(ptent))
769 continue;
770
Matt Mackalla6198792008-02-04 22:29:03 -0800771 page = vm_normal_page(vma, addr, ptent);
772 if (!page)
773 continue;
774
775 /* Clear accessed and referenced bits. */
776 ptep_test_and_clear_young(vma, addr, pte);
777 ClearPageReferenced(page);
778 }
779 pte_unmap_unlock(pte - 1, ptl);
780 cond_resched();
781 return 0;
782}
783
Matt Mackallf248dcb2008-02-04 22:29:03 -0800784static ssize_t clear_refs_write(struct file *file, const char __user *buf,
785 size_t count, loff_t *ppos)
David Rientjesb813e932007-05-06 14:49:24 -0700786{
Matt Mackallf248dcb2008-02-04 22:29:03 -0800787 struct task_struct *task;
Vincent Lifb92a4b2009-09-22 16:45:36 -0700788 char buffer[PROC_NUMBUF];
Matt Mackallf248dcb2008-02-04 22:29:03 -0800789 struct mm_struct *mm;
David Rientjesb813e932007-05-06 14:49:24 -0700790 struct vm_area_struct *vma;
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700791 enum clear_refs_types type;
792 int itype;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700793 int rv;
David Rientjesb813e932007-05-06 14:49:24 -0700794
Matt Mackallf248dcb2008-02-04 22:29:03 -0800795 memset(buffer, 0, sizeof(buffer));
796 if (count > sizeof(buffer) - 1)
797 count = sizeof(buffer) - 1;
798 if (copy_from_user(buffer, buf, count))
799 return -EFAULT;
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700800 rv = kstrtoint(strstrip(buffer), 10, &itype);
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700801 if (rv < 0)
802 return rv;
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700803 type = (enum clear_refs_types)itype;
804 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
Matt Mackallf248dcb2008-02-04 22:29:03 -0800805 return -EINVAL;
Pavel Emelyanov541c2372013-07-03 15:01:22 -0700806
807 if (type == CLEAR_REFS_SOFT_DIRTY) {
808 soft_dirty_cleared = true;
809 pr_warn_once("The pagemap bits 55-60 has changed their meaning! "
810 "See the linux/Documentation/vm/pagemap.txt for details.\n");
811 }
812
Al Viro496ad9a2013-01-23 17:07:38 -0500813 task = get_proc_task(file_inode(file));
Matt Mackallf248dcb2008-02-04 22:29:03 -0800814 if (!task)
815 return -ESRCH;
816 mm = get_task_mm(task);
817 if (mm) {
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700818 struct clear_refs_private cp = {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700819 .type = type,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700820 };
Andrew Morton20cbc972008-07-05 12:29:05 -0700821 struct mm_walk clear_refs_walk = {
822 .pmd_entry = clear_refs_pte_range,
823 .mm = mm,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700824 .private = &cp,
Andrew Morton20cbc972008-07-05 12:29:05 -0700825 };
Matt Mackallf248dcb2008-02-04 22:29:03 -0800826 down_read(&mm->mmap_sem);
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700827 if (type == CLEAR_REFS_SOFT_DIRTY)
828 mmu_notifier_invalidate_range_start(mm, 0, -1);
Dave Hansen21650092008-06-12 15:21:47 -0700829 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700830 cp.vma = vma;
Moussa A. Ba398499d2009-09-21 17:02:29 -0700831 if (is_vm_hugetlb_page(vma))
832 continue;
833 /*
834 * Writing 1 to /proc/pid/clear_refs affects all pages.
835 *
836 * Writing 2 to /proc/pid/clear_refs only affects
837 * Anonymous pages.
838 *
839 * Writing 3 to /proc/pid/clear_refs only affects file
840 * mapped pages.
841 */
842 if (type == CLEAR_REFS_ANON && vma->vm_file)
843 continue;
844 if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
845 continue;
846 walk_page_range(vma->vm_start, vma->vm_end,
847 &clear_refs_walk);
Dave Hansen21650092008-06-12 15:21:47 -0700848 }
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700849 if (type == CLEAR_REFS_SOFT_DIRTY)
850 mmu_notifier_invalidate_range_end(mm, 0, -1);
Matt Mackallf248dcb2008-02-04 22:29:03 -0800851 flush_tlb_mm(mm);
852 up_read(&mm->mmap_sem);
853 mmput(mm);
854 }
855 put_task_struct(task);
Vincent Lifb92a4b2009-09-22 16:45:36 -0700856
857 return count;
David Rientjesb813e932007-05-06 14:49:24 -0700858}
859
Matt Mackallf248dcb2008-02-04 22:29:03 -0800860const struct file_operations proc_clear_refs_operations = {
861 .write = clear_refs_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200862 .llseek = noop_llseek,
Matt Mackallf248dcb2008-02-04 22:29:03 -0800863};
864
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700865typedef struct {
866 u64 pme;
867} pagemap_entry_t;
868
Matt Mackall85863e42008-02-04 22:29:04 -0800869struct pagemapread {
yonghua zheng8c829622013-08-13 16:01:03 -0700870 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700871 pagemap_entry_t *buffer;
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700872 bool v2;
Matt Mackall85863e42008-02-04 22:29:04 -0800873};
874
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700875#define PAGEMAP_WALK_SIZE (PMD_SIZE)
876#define PAGEMAP_WALK_MASK (PMD_MASK)
877
yonghua zheng8c829622013-08-13 16:01:03 -0700878#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -0500879#define PM_STATUS_BITS 3
880#define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
881#define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
882#define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
883#define PM_PSHIFT_BITS 6
884#define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
885#define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700886#define __PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -0500887#define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
888#define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700889/* in "new" pagemap pshift bits are occupied with more status bits */
890#define PM_STATUS2(v2, x) (__PM_PSHIFT(v2 ? x : PAGE_SHIFT))
Hans Rosenfeldf16278c2008-03-21 18:46:59 -0500891
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700892#define __PM_SOFT_DIRTY (1LL)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -0500893#define PM_PRESENT PM_STATUS(4LL)
894#define PM_SWAP PM_STATUS(2LL)
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700895#define PM_FILE PM_STATUS(1LL)
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700896#define PM_NOT_PRESENT(v2) PM_STATUS2(v2, 0)
Matt Mackall85863e42008-02-04 22:29:04 -0800897#define PM_END_OF_BUFFER 1
898
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700899static inline pagemap_entry_t make_pme(u64 val)
900{
901 return (pagemap_entry_t) { .pme = val };
902}
903
904static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
Matt Mackall85863e42008-02-04 22:29:04 -0800905 struct pagemapread *pm)
906{
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700907 pm->buffer[pm->pos++] = *pme;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +0900908 if (pm->pos >= pm->len)
Thomas Tuttleaae86792008-06-05 22:46:31 -0700909 return PM_END_OF_BUFFER;
Matt Mackall85863e42008-02-04 22:29:04 -0800910 return 0;
911}
912
913static int pagemap_pte_hole(unsigned long start, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700914 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -0800915{
Dave Hansen21650092008-06-12 15:21:47 -0700916 struct pagemapread *pm = walk->private;
Matt Mackall85863e42008-02-04 22:29:04 -0800917 unsigned long addr;
918 int err = 0;
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700919 pagemap_entry_t pme = make_pme(PM_NOT_PRESENT(pm->v2));
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700920
Matt Mackall85863e42008-02-04 22:29:04 -0800921 for (addr = start; addr < end; addr += PAGE_SIZE) {
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700922 err = add_to_pagemap(addr, &pme, pm);
Matt Mackall85863e42008-02-04 22:29:04 -0800923 if (err)
924 break;
925 }
926 return err;
927}
928
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700929static void pte_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700930 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
Matt Mackall85863e42008-02-04 22:29:04 -0800931{
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700932 u64 frame, flags;
933 struct page *page = NULL;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700934 int flags2 = 0;
Matt Mackall85863e42008-02-04 22:29:04 -0800935
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700936 if (pte_present(pte)) {
937 frame = pte_pfn(pte);
938 flags = PM_PRESENT;
939 page = vm_normal_page(vma, addr, pte);
Cyrill Gorcunove9cdd6e2013-10-16 13:46:53 -0700940 if (pte_soft_dirty(pte))
941 flags2 |= __PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700942 } else if (is_swap_pte(pte)) {
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700943 swp_entry_t entry;
944 if (pte_swp_soft_dirty(pte))
945 flags2 |= __PM_SOFT_DIRTY;
946 entry = pte_to_swp_entry(pte);
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700947 frame = swp_type(entry) |
948 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
949 flags = PM_SWAP;
950 if (is_migration_entry(entry))
951 page = migration_entry_to_page(entry);
952 } else {
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -0700953 if (vma->vm_flags & VM_SOFTDIRTY)
954 flags2 |= __PM_SOFT_DIRTY;
955 *pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, flags2));
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700956 return;
957 }
958
959 if (page && !PageAnon(page))
960 flags |= PM_FILE;
Cyrill Gorcunove9cdd6e2013-10-16 13:46:53 -0700961 if ((vma->vm_flags & VM_SOFTDIRTY))
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700962 flags2 |= __PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700963
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700964 *pme = make_pme(PM_PFRAME(frame) | PM_STATUS2(pm->v2, flags2) | flags);
Dave Hansenbcf80392008-06-12 15:21:48 -0700965}
966
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700967#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700968static void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700969 pmd_t pmd, int offset, int pmd_flags2)
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700970{
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700971 /*
972 * Currently pmd for thp is always present because thp can not be
973 * swapped-out, migrated, or HWPOISONed (split in such cases instead.)
974 * This if-check is just to prepare for future implementation.
975 */
976 if (pmd_present(pmd))
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700977 *pme = make_pme(PM_PFRAME(pmd_pfn(pmd) + offset)
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700978 | PM_STATUS2(pm->v2, pmd_flags2) | PM_PRESENT);
Konstantin Khlebnikov16fbdce2012-05-10 13:01:43 -0700979 else
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -0700980 *pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, pmd_flags2));
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700981}
982#else
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700983static inline void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700984 pmd_t pmd, int offset, int pmd_flags2)
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700985{
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700986}
987#endif
988
Matt Mackall85863e42008-02-04 22:29:04 -0800989static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700990 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -0800991{
Dave Hansenbcf80392008-06-12 15:21:48 -0700992 struct vm_area_struct *vma;
Dave Hansen21650092008-06-12 15:21:47 -0700993 struct pagemapread *pm = walk->private;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -0800994 spinlock_t *ptl;
Matt Mackall85863e42008-02-04 22:29:04 -0800995 pte_t *pte;
996 int err = 0;
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700997 pagemap_entry_t pme = make_pme(PM_NOT_PRESENT(pm->v2));
Matt Mackall85863e42008-02-04 22:29:04 -0800998
Dave Hansenbcf80392008-06-12 15:21:48 -0700999 /* find the first VMA at or above 'addr' */
1000 vma = find_vma(walk->mm, addr);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001001 if (vma && pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001002 int pmd_flags2;
1003
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001004 if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(*pmd))
1005 pmd_flags2 = __PM_SOFT_DIRTY;
1006 else
1007 pmd_flags2 = 0;
1008
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001009 for (; addr != end; addr += PAGE_SIZE) {
1010 unsigned long offset;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001011
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001012 offset = (addr & ~PAGEMAP_WALK_MASK) >>
1013 PAGE_SHIFT;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001014 thp_pmd_to_pagemap_entry(&pme, pm, *pmd, offset, pmd_flags2);
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001015 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001016 if (err)
1017 break;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001018 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001019 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001020 return err;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001021 }
1022
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07001023 if (pmd_trans_unstable(pmd))
1024 return 0;
Matt Mackall85863e42008-02-04 22:29:04 -08001025 for (; addr != end; addr += PAGE_SIZE) {
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001026 int flags2;
Dave Hansenbcf80392008-06-12 15:21:48 -07001027
1028 /* check to see if we've left 'vma' behind
1029 * and need a new, higher one */
Konstantin Khlebnikov16fbdce2012-05-10 13:01:43 -07001030 if (vma && (addr >= vma->vm_end)) {
Dave Hansenbcf80392008-06-12 15:21:48 -07001031 vma = find_vma(walk->mm, addr);
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001032 if (vma && (vma->vm_flags & VM_SOFTDIRTY))
1033 flags2 = __PM_SOFT_DIRTY;
1034 else
1035 flags2 = 0;
1036 pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, flags2));
Konstantin Khlebnikov16fbdce2012-05-10 13:01:43 -07001037 }
Dave Hansenbcf80392008-06-12 15:21:48 -07001038
1039 /* check that 'vma' actually covers this address,
1040 * and that it isn't a huge page vma */
1041 if (vma && (vma->vm_start <= addr) &&
1042 !is_vm_hugetlb_page(vma)) {
1043 pte = pte_offset_map(pmd, addr);
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -07001044 pte_to_pagemap_entry(&pme, pm, vma, addr, *pte);
Dave Hansenbcf80392008-06-12 15:21:48 -07001045 /* unmap before userspace copy */
1046 pte_unmap(pte);
1047 }
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001048 err = add_to_pagemap(addr, &pme, pm);
Matt Mackall85863e42008-02-04 22:29:04 -08001049 if (err)
1050 return err;
1051 }
1052
1053 cond_resched();
1054
1055 return err;
1056}
1057
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001058#ifdef CONFIG_HUGETLB_PAGE
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -07001059static void huge_pte_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001060 pte_t pte, int offset, int flags2)
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001061{
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001062 if (pte_present(pte))
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001063 *pme = make_pme(PM_PFRAME(pte_pfn(pte) + offset) |
1064 PM_STATUS2(pm->v2, flags2) |
1065 PM_PRESENT);
Konstantin Khlebnikov16fbdce2012-05-10 13:01:43 -07001066 else
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001067 *pme = make_pme(PM_NOT_PRESENT(pm->v2) |
1068 PM_STATUS2(pm->v2, flags2));
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001069}
1070
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001071/* This function walks within one hugetlb entry in the single call */
1072static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
1073 unsigned long addr, unsigned long end,
1074 struct mm_walk *walk)
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001075{
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001076 struct pagemapread *pm = walk->private;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001077 struct vm_area_struct *vma;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001078 int err = 0;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001079 int flags2;
Konstantin Khlebnikov16fbdce2012-05-10 13:01:43 -07001080 pagemap_entry_t pme;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001081
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001082 vma = find_vma(walk->mm, addr);
1083 WARN_ON_ONCE(!vma);
1084
1085 if (vma && (vma->vm_flags & VM_SOFTDIRTY))
1086 flags2 = __PM_SOFT_DIRTY;
1087 else
1088 flags2 = 0;
1089
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001090 for (; addr != end; addr += PAGE_SIZE) {
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001091 int offset = (addr & ~hmask) >> PAGE_SHIFT;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001092 huge_pte_to_pagemap_entry(&pme, pm, *pte, offset, flags2);
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001093 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001094 if (err)
1095 return err;
1096 }
1097
1098 cond_resched();
1099
1100 return err;
1101}
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001102#endif /* HUGETLB_PAGE */
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001103
Matt Mackall85863e42008-02-04 22:29:04 -08001104/*
1105 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1106 *
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001107 * For each page in the address space, this file contains one 64-bit entry
1108 * consisting of the following:
1109 *
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001110 * Bits 0-54 page frame number (PFN) if present
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001111 * Bits 0-4 swap type if swapped
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001112 * Bits 5-54 swap offset if swapped
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001113 * Bits 55-60 page shift (page size = 1<<page shift)
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001114 * Bit 61 page is file-page or shared-anon
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001115 * Bit 62 page swapped
1116 * Bit 63 page present
1117 *
1118 * If the page is not present but in swap, then the PFN contains an
1119 * encoding of the swap file number and the page's offset into the
1120 * swap. Unmapped pages return a null PFN. This allows determining
Matt Mackall85863e42008-02-04 22:29:04 -08001121 * precisely which pages are mapped (or in swap) and comparing mapped
1122 * pages between processes.
1123 *
1124 * Efficient users of this interface will use /proc/pid/maps to
1125 * determine which areas of memory are actually mapped and llseek to
1126 * skip over unmapped regions.
1127 */
1128static ssize_t pagemap_read(struct file *file, char __user *buf,
1129 size_t count, loff_t *ppos)
1130{
Al Viro496ad9a2013-01-23 17:07:38 -05001131 struct task_struct *task = get_proc_task(file_inode(file));
Matt Mackall85863e42008-02-04 22:29:04 -08001132 struct mm_struct *mm;
1133 struct pagemapread pm;
Matt Mackall85863e42008-02-04 22:29:04 -08001134 int ret = -ESRCH;
Alexey Dobriyanee1e6ab2008-07-21 14:21:36 -07001135 struct mm_walk pagemap_walk = {};
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001136 unsigned long src;
1137 unsigned long svpfn;
1138 unsigned long start_vaddr;
1139 unsigned long end_vaddr;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001140 int copied = 0;
Matt Mackall85863e42008-02-04 22:29:04 -08001141
1142 if (!task)
1143 goto out;
1144
Matt Mackall85863e42008-02-04 22:29:04 -08001145 ret = -EINVAL;
1146 /* file position must be aligned */
Thomas Tuttleaae86792008-06-05 22:46:31 -07001147 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
Marcelo Tosattifb393802008-03-13 12:32:35 -07001148 goto out_task;
Matt Mackall85863e42008-02-04 22:29:04 -08001149
1150 ret = 0;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001151 if (!count)
1152 goto out_task;
1153
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001154 pm.v2 = soft_dirty_cleared;
yonghua zheng8c829622013-08-13 16:01:03 -07001155 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1156 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001157 ret = -ENOMEM;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001158 if (!pm.buffer)
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001159 goto out_task;
1160
Cong Wange7dcd992012-05-31 16:26:17 -07001161 mm = mm_access(task, PTRACE_MODE_READ);
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001162 ret = PTR_ERR(mm);
1163 if (!mm || IS_ERR(mm))
1164 goto out_free;
Matt Mackall85863e42008-02-04 22:29:04 -08001165
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001166 pagemap_walk.pmd_entry = pagemap_pte_range;
1167 pagemap_walk.pte_hole = pagemap_pte_hole;
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001168#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001169 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001170#endif
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001171 pagemap_walk.mm = mm;
1172 pagemap_walk.private = &pm;
Matt Mackall85863e42008-02-04 22:29:04 -08001173
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001174 src = *ppos;
1175 svpfn = src / PM_ENTRY_BYTES;
1176 start_vaddr = svpfn << PAGE_SHIFT;
1177 end_vaddr = TASK_SIZE_OF(task);
Matt Mackall85863e42008-02-04 22:29:04 -08001178
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001179 /* watch out for wraparound */
1180 if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
1181 start_vaddr = end_vaddr;
1182
1183 /*
1184 * The odds are that this will stop walking way
1185 * before end_vaddr, because the length of the
1186 * user buffer is tracked in "pm", and the walk
1187 * will stop when we hit the end of the buffer.
1188 */
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001189 ret = 0;
1190 while (count && (start_vaddr < end_vaddr)) {
1191 int len;
1192 unsigned long end;
Matt Mackall85863e42008-02-04 22:29:04 -08001193
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001194 pm.pos = 0;
Naoya Horiguchiea251c12010-11-24 12:57:13 -08001195 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001196 /* overflow ? */
1197 if (end < start_vaddr || end > end_vaddr)
1198 end = end_vaddr;
1199 down_read(&mm->mmap_sem);
1200 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1201 up_read(&mm->mmap_sem);
1202 start_vaddr = end;
1203
1204 len = min(count, PM_ENTRY_BYTES * pm.pos);
Dan Carpenter309361e02010-04-06 13:45:39 +03001205 if (copy_to_user(buf, pm.buffer, len)) {
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001206 ret = -EFAULT;
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001207 goto out_mm;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001208 }
1209 copied += len;
1210 buf += len;
1211 count -= len;
Matt Mackall85863e42008-02-04 22:29:04 -08001212 }
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001213 *ppos += copied;
1214 if (!ret || ret == PM_END_OF_BUFFER)
1215 ret = copied;
1216
Marcelo Tosattifb393802008-03-13 12:32:35 -07001217out_mm:
1218 mmput(mm);
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001219out_free:
1220 kfree(pm.buffer);
Matt Mackall85863e42008-02-04 22:29:04 -08001221out_task:
1222 put_task_struct(task);
1223out:
1224 return ret;
1225}
1226
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001227static int pagemap_open(struct inode *inode, struct file *file)
1228{
1229 pr_warn_once("Bits 55-60 of /proc/PID/pagemap entries are about "
1230 "to stop being page-shift some time soon. See the "
1231 "linux/Documentation/vm/pagemap.txt for details.\n");
1232 return 0;
1233}
1234
Matt Mackall85863e42008-02-04 22:29:04 -08001235const struct file_operations proc_pagemap_operations = {
1236 .llseek = mem_lseek, /* borrow this */
1237 .read = pagemap_read,
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001238 .open = pagemap_open,
Matt Mackall85863e42008-02-04 22:29:04 -08001239};
Matt Mackall1e883282008-02-04 22:29:07 -08001240#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall85863e42008-02-04 22:29:04 -08001241
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001242#ifdef CONFIG_NUMA
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001243
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001244struct numa_maps {
1245 struct vm_area_struct *vma;
1246 unsigned long pages;
1247 unsigned long anon;
1248 unsigned long active;
1249 unsigned long writeback;
1250 unsigned long mapcount_max;
1251 unsigned long dirty;
1252 unsigned long swapcache;
1253 unsigned long node[MAX_NUMNODES];
1254};
1255
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001256struct numa_maps_private {
1257 struct proc_maps_private proc_maps;
1258 struct numa_maps md;
1259};
1260
Dave Hanseneb4866d2011-09-20 15:19:38 -07001261static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1262 unsigned long nr_pages)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001263{
1264 int count = page_mapcount(page);
1265
Dave Hanseneb4866d2011-09-20 15:19:38 -07001266 md->pages += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001267 if (pte_dirty || PageDirty(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001268 md->dirty += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001269
1270 if (PageSwapCache(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001271 md->swapcache += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001272
1273 if (PageActive(page) || PageUnevictable(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001274 md->active += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001275
1276 if (PageWriteback(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001277 md->writeback += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001278
1279 if (PageAnon(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001280 md->anon += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001281
1282 if (count > md->mapcount_max)
1283 md->mapcount_max = count;
1284
Dave Hanseneb4866d2011-09-20 15:19:38 -07001285 md->node[page_to_nid(page)] += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001286}
1287
Dave Hansen3200a8a2011-09-20 15:19:39 -07001288static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1289 unsigned long addr)
1290{
1291 struct page *page;
1292 int nid;
1293
1294 if (!pte_present(pte))
1295 return NULL;
1296
1297 page = vm_normal_page(vma, addr, pte);
1298 if (!page)
1299 return NULL;
1300
1301 if (PageReserved(page))
1302 return NULL;
1303
1304 nid = page_to_nid(page);
Lai Jiangshan4ff1b2c2012-12-12 13:51:25 -08001305 if (!node_isset(nid, node_states[N_MEMORY]))
Dave Hansen3200a8a2011-09-20 15:19:39 -07001306 return NULL;
1307
1308 return page;
1309}
1310
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001311static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1312 unsigned long end, struct mm_walk *walk)
1313{
1314 struct numa_maps *md;
1315 spinlock_t *ptl;
1316 pte_t *orig_pte;
1317 pte_t *pte;
1318
1319 md = walk->private;
Dave Hansen32ef4382011-09-20 15:19:41 -07001320
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001321 if (pmd_trans_huge_lock(pmd, md->vma, &ptl) == 1) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001322 pte_t huge_pte = *(pte_t *)pmd;
1323 struct page *page;
1324
1325 page = can_gather_numa_stats(huge_pte, md->vma, addr);
1326 if (page)
1327 gather_stats(page, md, pte_dirty(huge_pte),
1328 HPAGE_PMD_SIZE/PAGE_SIZE);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001329 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001330 return 0;
Dave Hansen32ef4382011-09-20 15:19:41 -07001331 }
1332
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001333 if (pmd_trans_unstable(pmd))
1334 return 0;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001335 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1336 do {
Dave Hansen3200a8a2011-09-20 15:19:39 -07001337 struct page *page = can_gather_numa_stats(*pte, md->vma, addr);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001338 if (!page)
1339 continue;
Dave Hanseneb4866d2011-09-20 15:19:38 -07001340 gather_stats(page, md, pte_dirty(*pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001341
1342 } while (pte++, addr += PAGE_SIZE, addr != end);
1343 pte_unmap_unlock(orig_pte, ptl);
1344 return 0;
1345}
1346#ifdef CONFIG_HUGETLB_PAGE
1347static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1348 unsigned long addr, unsigned long end, struct mm_walk *walk)
1349{
1350 struct numa_maps *md;
1351 struct page *page;
1352
1353 if (pte_none(*pte))
1354 return 0;
1355
1356 page = pte_page(*pte);
1357 if (!page)
1358 return 0;
1359
1360 md = walk->private;
Dave Hanseneb4866d2011-09-20 15:19:38 -07001361 gather_stats(page, md, pte_dirty(*pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001362 return 0;
1363}
1364
1365#else
1366static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1367 unsigned long addr, unsigned long end, struct mm_walk *walk)
1368{
1369 return 0;
1370}
1371#endif
1372
1373/*
1374 * Display pages allocated per node and memory policy via /proc.
1375 */
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001376static int show_numa_map(struct seq_file *m, void *v, int is_pid)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001377{
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001378 struct numa_maps_private *numa_priv = m->private;
1379 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001380 struct vm_area_struct *vma = v;
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001381 struct numa_maps *md = &numa_priv->md;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001382 struct file *file = vma->vm_file;
David Rientjes32f85162012-10-16 17:31:23 -07001383 struct task_struct *task = proc_priv->task;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001384 struct mm_struct *mm = vma->vm_mm;
1385 struct mm_walk walk = {};
1386 struct mempolicy *pol;
David Rientjes948927e2013-11-12 15:07:28 -08001387 char buffer[64];
1388 int nid;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001389
1390 if (!mm)
1391 return 0;
1392
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001393 /* Ensure we start with an empty set of numa_maps statistics. */
1394 memset(md, 0, sizeof(*md));
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001395
1396 md->vma = vma;
1397
1398 walk.hugetlb_entry = gather_hugetbl_stats;
1399 walk.pmd_entry = gather_pte_stats;
1400 walk.private = md;
1401 walk.mm = mm;
1402
David Rientjes32f85162012-10-16 17:31:23 -07001403 pol = get_vma_policy(task, vma, vma->vm_start);
David Rientjes948927e2013-11-12 15:07:28 -08001404 mpol_to_str(buffer, sizeof(buffer), pol);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001405 mpol_cond_put(pol);
1406
1407 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1408
1409 if (file) {
1410 seq_printf(m, " file=");
1411 seq_path(m, &file->f_path, "\n\t= ");
1412 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1413 seq_printf(m, " heap");
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001414 } else {
David Rientjes32f85162012-10-16 17:31:23 -07001415 pid_t tid = vm_is_stack(task, vma, is_pid);
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001416 if (tid != 0) {
1417 /*
1418 * Thread stack in /proc/PID/task/TID/maps or
1419 * the main process stack.
1420 */
1421 if (!is_pid || (vma->vm_start <= mm->start_stack &&
1422 vma->vm_end >= mm->start_stack))
1423 seq_printf(m, " stack");
1424 else
1425 seq_printf(m, " stack:%d", tid);
1426 }
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001427 }
1428
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001429 if (is_vm_hugetlb_page(vma))
1430 seq_printf(m, " huge");
1431
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001432 walk_page_range(vma->vm_start, vma->vm_end, &walk);
1433
1434 if (!md->pages)
1435 goto out;
1436
1437 if (md->anon)
1438 seq_printf(m, " anon=%lu", md->anon);
1439
1440 if (md->dirty)
1441 seq_printf(m, " dirty=%lu", md->dirty);
1442
1443 if (md->pages != md->anon && md->pages != md->dirty)
1444 seq_printf(m, " mapped=%lu", md->pages);
1445
1446 if (md->mapcount_max > 1)
1447 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1448
1449 if (md->swapcache)
1450 seq_printf(m, " swapcache=%lu", md->swapcache);
1451
1452 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1453 seq_printf(m, " active=%lu", md->active);
1454
1455 if (md->writeback)
1456 seq_printf(m, " writeback=%lu", md->writeback);
1457
David Rientjes948927e2013-11-12 15:07:28 -08001458 for_each_node_state(nid, N_MEMORY)
1459 if (md->node[nid])
1460 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001461out:
1462 seq_putc(m, '\n');
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001463
1464 if (m->count < m->size)
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001465 m->version = (vma != proc_priv->tail_vma) ? vma->vm_start : 0;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001466 return 0;
1467}
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001468
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001469static int show_pid_numa_map(struct seq_file *m, void *v)
1470{
1471 return show_numa_map(m, v, 1);
1472}
1473
1474static int show_tid_numa_map(struct seq_file *m, void *v)
1475{
1476 return show_numa_map(m, v, 0);
1477}
1478
Jan Engelhardt03a44822008-02-08 04:21:19 -08001479static const struct seq_operations proc_pid_numa_maps_op = {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001480 .start = m_start,
1481 .next = m_next,
1482 .stop = m_stop,
1483 .show = show_pid_numa_map,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001484};
Eric W. Biederman662795d2006-06-26 00:25:48 -07001485
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001486static const struct seq_operations proc_tid_numa_maps_op = {
1487 .start = m_start,
1488 .next = m_next,
1489 .stop = m_stop,
1490 .show = show_tid_numa_map,
1491};
1492
1493static int numa_maps_open(struct inode *inode, struct file *file,
1494 const struct seq_operations *ops)
Eric W. Biederman662795d2006-06-26 00:25:48 -07001495{
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001496 struct numa_maps_private *priv;
1497 int ret = -ENOMEM;
1498 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1499 if (priv) {
1500 priv->proc_maps.pid = proc_pid(inode);
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001501 ret = seq_open(file, ops);
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001502 if (!ret) {
1503 struct seq_file *m = file->private_data;
1504 m->private = priv;
1505 } else {
1506 kfree(priv);
1507 }
1508 }
1509 return ret;
Eric W. Biederman662795d2006-06-26 00:25:48 -07001510}
1511
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001512static int pid_numa_maps_open(struct inode *inode, struct file *file)
1513{
1514 return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1515}
1516
1517static int tid_numa_maps_open(struct inode *inode, struct file *file)
1518{
1519 return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1520}
1521
1522const struct file_operations proc_pid_numa_maps_operations = {
1523 .open = pid_numa_maps_open,
1524 .read = seq_read,
1525 .llseek = seq_lseek,
1526 .release = seq_release_private,
1527};
1528
1529const struct file_operations proc_tid_numa_maps_operations = {
1530 .open = tid_numa_maps_open,
Eric W. Biederman662795d2006-06-26 00:25:48 -07001531 .read = seq_read,
1532 .llseek = seq_lseek,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001533 .release = seq_release_private,
Eric W. Biederman662795d2006-06-26 00:25:48 -07001534};
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001535#endif /* CONFIG_NUMA */