blob: 42b5cf5d03262cc082dc6fa86a79da580ef9ea46 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static void pad_len_spaces(struct seq_file *m, int len)
88{
89 len = 25 + sizeof(void*) * 6 - len;
90 if (len < 1)
91 len = 1;
92 seq_printf(m, "%*c", len, ' ');
93}
94
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +090095#ifdef CONFIG_NUMA
96/*
97 * These functions are for numa_maps but called in generic **maps seq_file
98 * ->start(), ->stop() ops.
99 *
100 * numa_maps scans all vmas under mmap_sem and checks their mempolicy.
101 * Each mempolicy object is controlled by reference counting. The problem here
102 * is how to avoid accessing dead mempolicy object.
103 *
104 * Because we're holding mmap_sem while reading seq_file, it's safe to access
105 * each vma's mempolicy, no vma objects will never drop refs to mempolicy.
106 *
107 * A task's mempolicy (task->mempolicy) has different behavior. task->mempolicy
108 * is set and replaced under mmap_sem but unrefed and cleared under task_lock().
109 * So, without task_lock(), we cannot trust get_vma_policy() because we cannot
110 * gurantee the task never exits under us. But taking task_lock() around
111 * get_vma_plicy() causes lock order problem.
112 *
113 * To access task->mempolicy without lock, we hold a reference count of an
114 * object pointed by task->mempolicy and remember it. This will guarantee
115 * that task->mempolicy points to an alive object or NULL in numa_maps accesses.
116 */
117static void hold_task_mempolicy(struct proc_maps_private *priv)
118{
119 struct task_struct *task = priv->task;
120
121 task_lock(task);
122 priv->task_mempolicy = task->mempolicy;
123 mpol_get(priv->task_mempolicy);
124 task_unlock(task);
125}
126static void release_task_mempolicy(struct proc_maps_private *priv)
127{
128 mpol_put(priv->task_mempolicy);
129}
130#else
131static void hold_task_mempolicy(struct proc_maps_private *priv)
132{
133}
134static void release_task_mempolicy(struct proc_maps_private *priv)
135{
136}
137#endif
138
Matt Mackalla6198792008-02-04 22:29:03 -0800139static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
Mauricio Line070ad42005-09-03 15:55:10 -0700140{
Matt Mackalla6198792008-02-04 22:29:03 -0800141 if (vma && vma != priv->tail_vma) {
142 struct mm_struct *mm = vma->vm_mm;
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900143 release_task_mempolicy(priv);
Matt Mackalla6198792008-02-04 22:29:03 -0800144 up_read(&mm->mmap_sem);
145 mmput(mm);
146 }
147}
148
149static void *m_start(struct seq_file *m, loff_t *pos)
150{
151 struct proc_maps_private *priv = m->private;
152 unsigned long last_addr = m->version;
153 struct mm_struct *mm;
154 struct vm_area_struct *vma, *tail_vma = NULL;
155 loff_t l = *pos;
156
157 /* Clear the per syscall fields in priv */
158 priv->task = NULL;
159 priv->tail_vma = NULL;
160
161 /*
162 * We remember last_addr rather than next_addr to hit with
163 * mmap_cache most of the time. We have zero last_addr at
164 * the beginning and also after lseek. We will have -1 last_addr
165 * after the end of the vmas.
166 */
167
168 if (last_addr == -1UL)
169 return NULL;
170
171 priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
172 if (!priv->task)
Al Viroec6fd8a2011-02-15 22:22:54 -0500173 return ERR_PTR(-ESRCH);
Matt Mackalla6198792008-02-04 22:29:03 -0800174
Cong Wange7dcd992012-05-31 16:26:17 -0700175 mm = mm_access(priv->task, PTRACE_MODE_READ);
Al Viroec6fd8a2011-02-15 22:22:54 -0500176 if (!mm || IS_ERR(mm))
177 return mm;
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200178 down_read(&mm->mmap_sem);
Matt Mackalla6198792008-02-04 22:29:03 -0800179
Stephen Wilson31db58b2011-03-13 15:49:15 -0400180 tail_vma = get_gate_vma(priv->task->mm);
Matt Mackalla6198792008-02-04 22:29:03 -0800181 priv->tail_vma = tail_vma;
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900182 hold_task_mempolicy(priv);
Matt Mackalla6198792008-02-04 22:29:03 -0800183 /* Start with last addr hint */
184 vma = find_vma(mm, last_addr);
185 if (last_addr && vma) {
186 vma = vma->vm_next;
187 goto out;
188 }
189
190 /*
191 * Check the vma index is within the range and do
192 * sequential scan until m_index.
193 */
194 vma = NULL;
195 if ((unsigned long)l < mm->map_count) {
196 vma = mm->mmap;
197 while (l-- && vma)
198 vma = vma->vm_next;
199 goto out;
200 }
201
202 if (l != mm->map_count)
203 tail_vma = NULL; /* After gate vma */
204
205out:
206 if (vma)
207 return vma;
208
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900209 release_task_mempolicy(priv);
Matt Mackalla6198792008-02-04 22:29:03 -0800210 /* End of vmas has been reached */
211 m->version = (tail_vma != NULL)? 0: -1UL;
212 up_read(&mm->mmap_sem);
213 mmput(mm);
214 return tail_vma;
215}
216
217static void *m_next(struct seq_file *m, void *v, loff_t *pos)
218{
219 struct proc_maps_private *priv = m->private;
220 struct vm_area_struct *vma = v;
221 struct vm_area_struct *tail_vma = priv->tail_vma;
222
223 (*pos)++;
224 if (vma && (vma != tail_vma) && vma->vm_next)
225 return vma->vm_next;
226 vma_stop(priv, vma);
227 return (vma != tail_vma)? tail_vma: NULL;
228}
229
230static void m_stop(struct seq_file *m, void *v)
231{
232 struct proc_maps_private *priv = m->private;
233 struct vm_area_struct *vma = v;
234
Linus Torvalds76597cd2011-03-27 19:09:29 -0700235 if (!IS_ERR(vma))
236 vma_stop(priv, vma);
Matt Mackalla6198792008-02-04 22:29:03 -0800237 if (priv->task)
238 put_task_struct(priv->task);
239}
240
241static int do_maps_open(struct inode *inode, struct file *file,
Jan Engelhardt03a44822008-02-08 04:21:19 -0800242 const struct seq_operations *ops)
Matt Mackalla6198792008-02-04 22:29:03 -0800243{
244 struct proc_maps_private *priv;
245 int ret = -ENOMEM;
246 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
247 if (priv) {
248 priv->pid = proc_pid(inode);
249 ret = seq_open(file, ops);
250 if (!ret) {
251 struct seq_file *m = file->private_data;
252 m->private = priv;
253 } else {
254 kfree(priv);
255 }
256 }
257 return ret;
258}
Mauricio Line070ad42005-09-03 15:55:10 -0700259
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700260static void
261show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Mauricio Line070ad42005-09-03 15:55:10 -0700263 struct mm_struct *mm = vma->vm_mm;
264 struct file *file = vma->vm_file;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700265 struct proc_maps_private *priv = m->private;
266 struct task_struct *task = priv->task;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900267 vm_flags_t flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 unsigned long ino = 0;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700269 unsigned long long pgoff = 0;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200270 unsigned long start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 dev_t dev = 0;
272 int len;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700273 const char *name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 if (file) {
Al Viro496ad9a2013-01-23 17:07:38 -0500276 struct inode *inode = file_inode(vma->vm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dev = inode->i_sb->s_dev;
278 ino = inode->i_ino;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700279 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281
Linus Torvaldsd7824372010-08-15 11:35:52 -0700282 /* We don't show the stack guard page in /proc/maps */
283 start = vma->vm_start;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200284 if (stack_guard_page_start(vma, start))
285 start += PAGE_SIZE;
286 end = vma->vm_end;
287 if (stack_guard_page_end(vma, end))
288 end -= PAGE_SIZE;
Linus Torvaldsd7824372010-08-15 11:35:52 -0700289
Clement Calmels1804dc62008-08-20 14:09:00 -0700290 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
Linus Torvaldsd7824372010-08-15 11:35:52 -0700291 start,
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200292 end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 flags & VM_READ ? 'r' : '-',
294 flags & VM_WRITE ? 'w' : '-',
295 flags & VM_EXEC ? 'x' : '-',
296 flags & VM_MAYSHARE ? 's' : 'p',
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700297 pgoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 MAJOR(dev), MINOR(dev), ino, &len);
299
300 /*
301 * Print the dentry name for named mappings, and a
302 * special [heap] marker for the heap:
303 */
Mauricio Line070ad42005-09-03 15:55:10 -0700304 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 pad_len_spaces(m, len);
Jan Blunckc32c2f62008-02-14 19:38:43 -0800306 seq_path(m, &file->f_path, "\n");
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700307 goto done;
308 }
309
310 name = arch_vma_name(vma);
311 if (!name) {
312 pid_t tid;
313
314 if (!mm) {
315 name = "[vdso]";
316 goto done;
317 }
318
319 if (vma->vm_start <= mm->brk &&
320 vma->vm_end >= mm->start_brk) {
321 name = "[heap]";
322 goto done;
323 }
324
325 tid = vm_is_stack(task, vma, is_pid);
326
327 if (tid != 0) {
328 /*
329 * Thread stack in /proc/PID/task/TID/maps or
330 * the main process stack.
331 */
332 if (!is_pid || (vma->vm_start <= mm->start_stack &&
333 vma->vm_end >= mm->start_stack)) {
334 name = "[stack]";
Ingo Molnare6e54942006-06-27 02:53:50 -0700335 } else {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700336 /* Thread stack in /proc/PID/maps */
337 pad_len_spaces(m, len);
338 seq_printf(m, "[stack:%d]", tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Ingo Molnare6e54942006-06-27 02:53:50 -0700340 }
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700341 }
342
343done:
344 if (name) {
345 pad_len_spaces(m, len);
346 seq_puts(m, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 seq_putc(m, '\n');
Joe Korty7c88db02008-10-16 15:27:09 +0400349}
350
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700351static int show_map(struct seq_file *m, void *v, int is_pid)
Joe Korty7c88db02008-10-16 15:27:09 +0400352{
353 struct vm_area_struct *vma = v;
354 struct proc_maps_private *priv = m->private;
355 struct task_struct *task = priv->task;
356
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700357 show_map_vma(m, vma, is_pid);
Mauricio Line070ad42005-09-03 15:55:10 -0700358
Mauricio Line070ad42005-09-03 15:55:10 -0700359 if (m->count < m->size) /* vma is copied successfully */
Stephen Wilson31db58b2011-03-13 15:49:15 -0400360 m->version = (vma != get_gate_vma(task->mm))
361 ? vma->vm_start : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 0;
363}
364
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700365static int show_pid_map(struct seq_file *m, void *v)
366{
367 return show_map(m, v, 1);
368}
369
370static int show_tid_map(struct seq_file *m, void *v)
371{
372 return show_map(m, v, 0);
373}
374
Jan Engelhardt03a44822008-02-08 04:21:19 -0800375static const struct seq_operations proc_pid_maps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800376 .start = m_start,
377 .next = m_next,
378 .stop = m_stop,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700379 .show = show_pid_map
Matt Mackalla6198792008-02-04 22:29:03 -0800380};
381
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700382static const struct seq_operations proc_tid_maps_op = {
383 .start = m_start,
384 .next = m_next,
385 .stop = m_stop,
386 .show = show_tid_map
387};
388
389static int pid_maps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800390{
391 return do_maps_open(inode, file, &proc_pid_maps_op);
392}
393
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700394static int tid_maps_open(struct inode *inode, struct file *file)
395{
396 return do_maps_open(inode, file, &proc_tid_maps_op);
397}
398
399const struct file_operations proc_pid_maps_operations = {
400 .open = pid_maps_open,
401 .read = seq_read,
402 .llseek = seq_lseek,
403 .release = seq_release_private,
404};
405
406const struct file_operations proc_tid_maps_operations = {
407 .open = tid_maps_open,
Matt Mackalla6198792008-02-04 22:29:03 -0800408 .read = seq_read,
409 .llseek = seq_lseek,
410 .release = seq_release_private,
411};
412
413/*
414 * Proportional Set Size(PSS): my share of RSS.
415 *
416 * PSS of a process is the count of pages it has in memory, where each
417 * page is divided by the number of processes sharing it. So if a
418 * process has 1000 pages all to itself, and 1000 shared with one other
419 * process, its PSS will be 1500.
420 *
421 * To keep (accumulated) division errors low, we adopt a 64bit
422 * fixed-point pss counter to minimize division errors. So (pss >>
423 * PSS_SHIFT) would be the real byte count.
424 *
425 * A shift of 12 before division means (assuming 4K page size):
426 * - 1M 3-user-pages add up to 8KB errors;
427 * - supports mapcount up to 2^24, or 16M;
428 * - supports PSS up to 2^52 bytes, or 4PB.
429 */
430#define PSS_SHIFT 12
431
Matt Mackall1e883282008-02-04 22:29:07 -0800432#ifdef CONFIG_PROC_PAGE_MONITOR
Peter Zijlstra214e4712008-04-28 02:12:55 -0700433struct mem_size_stats {
Matt Mackalla6198792008-02-04 22:29:03 -0800434 struct vm_area_struct *vma;
435 unsigned long resident;
436 unsigned long shared_clean;
437 unsigned long shared_dirty;
438 unsigned long private_clean;
439 unsigned long private_dirty;
440 unsigned long referenced;
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700441 unsigned long anonymous;
Dave Hansen4031a212011-03-22 16:33:01 -0700442 unsigned long anonymous_thp;
Peter Zijlstra214e4712008-04-28 02:12:55 -0700443 unsigned long swap;
Konstantin Khlebnikovbca15542012-05-31 16:26:20 -0700444 unsigned long nonlinear;
Matt Mackalla6198792008-02-04 22:29:03 -0800445 u64 pss;
446};
447
Dave Hansenae11c4d2011-03-22 16:32:58 -0700448
449static void smaps_pte_entry(pte_t ptent, unsigned long addr,
Dave Hansen3c9acc72011-03-22 16:32:59 -0700450 unsigned long ptent_size, struct mm_walk *walk)
Dave Hansenae11c4d2011-03-22 16:32:58 -0700451{
452 struct mem_size_stats *mss = walk->private;
453 struct vm_area_struct *vma = mss->vma;
Konstantin Khlebnikovbca15542012-05-31 16:26:20 -0700454 pgoff_t pgoff = linear_page_index(vma, addr);
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700455 struct page *page = NULL;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700456 int mapcount;
457
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700458 if (pte_present(ptent)) {
459 page = vm_normal_page(vma, addr, ptent);
460 } else if (is_swap_pte(ptent)) {
461 swp_entry_t swpent = pte_to_swp_entry(ptent);
462
463 if (!non_swap_entry(swpent))
464 mss->swap += ptent_size;
465 else if (is_migration_entry(swpent))
466 page = migration_entry_to_page(swpent);
Konstantin Khlebnikovbca15542012-05-31 16:26:20 -0700467 } else if (pte_file(ptent)) {
468 if (pte_to_pgoff(ptent) != pgoff)
469 mss->nonlinear += ptent_size;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700470 }
471
Dave Hansenae11c4d2011-03-22 16:32:58 -0700472 if (!page)
473 return;
474
475 if (PageAnon(page))
Dave Hansen3c9acc72011-03-22 16:32:59 -0700476 mss->anonymous += ptent_size;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700477
Konstantin Khlebnikovbca15542012-05-31 16:26:20 -0700478 if (page->index != pgoff)
479 mss->nonlinear += ptent_size;
480
Dave Hansen3c9acc72011-03-22 16:32:59 -0700481 mss->resident += ptent_size;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700482 /* Accumulate the size in pages that have been accessed. */
483 if (pte_young(ptent) || PageReferenced(page))
Dave Hansen3c9acc72011-03-22 16:32:59 -0700484 mss->referenced += ptent_size;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700485 mapcount = page_mapcount(page);
486 if (mapcount >= 2) {
487 if (pte_dirty(ptent) || PageDirty(page))
Dave Hansen3c9acc72011-03-22 16:32:59 -0700488 mss->shared_dirty += ptent_size;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700489 else
Dave Hansen3c9acc72011-03-22 16:32:59 -0700490 mss->shared_clean += ptent_size;
491 mss->pss += (ptent_size << PSS_SHIFT) / mapcount;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700492 } else {
493 if (pte_dirty(ptent) || PageDirty(page))
Dave Hansen3c9acc72011-03-22 16:32:59 -0700494 mss->private_dirty += ptent_size;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700495 else
Dave Hansen3c9acc72011-03-22 16:32:59 -0700496 mss->private_clean += ptent_size;
497 mss->pss += (ptent_size << PSS_SHIFT);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700498 }
499}
500
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800501static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700502 struct mm_walk *walk)
Mauricio Line070ad42005-09-03 15:55:10 -0700503{
Dave Hansen21650092008-06-12 15:21:47 -0700504 struct mem_size_stats *mss = walk->private;
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800505 struct vm_area_struct *vma = mss->vma;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700506 pte_t *pte;
Hugh Dickins705e87c2005-10-29 18:16:27 -0700507 spinlock_t *ptl;
Mauricio Line070ad42005-09-03 15:55:10 -0700508
Kirill A. Shutemovbf929152013-11-14 14:30:54 -0800509 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -0700510 smaps_pte_entry(*(pte_t *)pmd, addr, HPAGE_PMD_SIZE, walk);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -0800511 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -0700512 mss->anonymous_thp += HPAGE_PMD_SIZE;
513 return 0;
Dave Hansen22e057c2011-03-22 16:33:00 -0700514 }
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700515
516 if (pmd_trans_unstable(pmd))
517 return 0;
Dave Hansen22e057c2011-03-22 16:33:00 -0700518 /*
519 * The mmap_sem held all the way back in m_start() is what
520 * keeps khugepaged out of here and from collapsing things
521 * in here.
522 */
Hugh Dickins705e87c2005-10-29 18:16:27 -0700523 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700524 for (; addr != end; pte++, addr += PAGE_SIZE)
Dave Hansen3c9acc72011-03-22 16:32:59 -0700525 smaps_pte_entry(*pte, addr, PAGE_SIZE, walk);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700526 pte_unmap_unlock(pte - 1, ptl);
527 cond_resched();
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800528 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700529}
530
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800531static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
532{
533 /*
534 * Don't forget to update Documentation/ on changes.
535 */
536 static const char mnemonics[BITS_PER_LONG][2] = {
537 /*
538 * In case if we meet a flag we don't know about.
539 */
540 [0 ... (BITS_PER_LONG-1)] = "??",
541
542 [ilog2(VM_READ)] = "rd",
543 [ilog2(VM_WRITE)] = "wr",
544 [ilog2(VM_EXEC)] = "ex",
545 [ilog2(VM_SHARED)] = "sh",
546 [ilog2(VM_MAYREAD)] = "mr",
547 [ilog2(VM_MAYWRITE)] = "mw",
548 [ilog2(VM_MAYEXEC)] = "me",
549 [ilog2(VM_MAYSHARE)] = "ms",
550 [ilog2(VM_GROWSDOWN)] = "gd",
551 [ilog2(VM_PFNMAP)] = "pf",
552 [ilog2(VM_DENYWRITE)] = "dw",
553 [ilog2(VM_LOCKED)] = "lo",
554 [ilog2(VM_IO)] = "io",
555 [ilog2(VM_SEQ_READ)] = "sr",
556 [ilog2(VM_RAND_READ)] = "rr",
557 [ilog2(VM_DONTCOPY)] = "dc",
558 [ilog2(VM_DONTEXPAND)] = "de",
559 [ilog2(VM_ACCOUNT)] = "ac",
560 [ilog2(VM_NORESERVE)] = "nr",
561 [ilog2(VM_HUGETLB)] = "ht",
562 [ilog2(VM_NONLINEAR)] = "nl",
563 [ilog2(VM_ARCH_1)] = "ar",
564 [ilog2(VM_DONTDUMP)] = "dd",
Naoya Horiguchiec8e41a2013-11-12 15:07:49 -0800565#ifdef CONFIG_MEM_SOFT_DIRTY
566 [ilog2(VM_SOFTDIRTY)] = "sd",
567#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800568 [ilog2(VM_MIXEDMAP)] = "mm",
569 [ilog2(VM_HUGEPAGE)] = "hg",
570 [ilog2(VM_NOHUGEPAGE)] = "nh",
571 [ilog2(VM_MERGEABLE)] = "mg",
572 };
573 size_t i;
574
575 seq_puts(m, "VmFlags: ");
576 for (i = 0; i < BITS_PER_LONG; i++) {
577 if (vma->vm_flags & (1UL << i)) {
578 seq_printf(m, "%c%c ",
579 mnemonics[i][0], mnemonics[i][1]);
580 }
581 }
582 seq_putc(m, '\n');
583}
584
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700585static int show_smap(struct seq_file *m, void *v, int is_pid)
Mauricio Line070ad42005-09-03 15:55:10 -0700586{
Joe Korty7c88db02008-10-16 15:27:09 +0400587 struct proc_maps_private *priv = m->private;
588 struct task_struct *task = priv->task;
Mauricio Line070ad42005-09-03 15:55:10 -0700589 struct vm_area_struct *vma = v;
Mauricio Line070ad42005-09-03 15:55:10 -0700590 struct mem_size_stats mss;
Dave Hansen21650092008-06-12 15:21:47 -0700591 struct mm_walk smaps_walk = {
592 .pmd_entry = smaps_pte_range,
593 .mm = vma->vm_mm,
594 .private = &mss,
595 };
Mauricio Line070ad42005-09-03 15:55:10 -0700596
597 memset(&mss, 0, sizeof mss);
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800598 mss.vma = vma;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +0900599 /* mmap_sem is held in m_start */
Nick Piggin5ddfae12006-03-06 15:42:57 -0800600 if (vma->vm_mm && !is_vm_hugetlb_page(vma))
Dave Hansen21650092008-06-12 15:21:47 -0700601 walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
Matt Mackall4752c362008-02-04 22:29:02 -0800602
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700603 show_map_vma(m, vma, is_pid);
Matt Mackall4752c362008-02-04 22:29:02 -0800604
605 seq_printf(m,
606 "Size: %8lu kB\n"
607 "Rss: %8lu kB\n"
608 "Pss: %8lu kB\n"
609 "Shared_Clean: %8lu kB\n"
610 "Shared_Dirty: %8lu kB\n"
611 "Private_Clean: %8lu kB\n"
612 "Private_Dirty: %8lu kB\n"
Peter Zijlstra214e4712008-04-28 02:12:55 -0700613 "Referenced: %8lu kB\n"
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700614 "Anonymous: %8lu kB\n"
Dave Hansen4031a212011-03-22 16:33:01 -0700615 "AnonHugePages: %8lu kB\n"
Mel Gorman08fba692009-01-06 14:38:53 -0800616 "Swap: %8lu kB\n"
Mel Gorman33402892009-01-06 14:38:54 -0800617 "KernelPageSize: %8lu kB\n"
Nikanth Karthikesan2d905082011-01-13 15:45:53 -0800618 "MMUPageSize: %8lu kB\n"
619 "Locked: %8lu kB\n",
Matt Mackall4752c362008-02-04 22:29:02 -0800620 (vma->vm_end - vma->vm_start) >> 10,
621 mss.resident >> 10,
622 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
623 mss.shared_clean >> 10,
624 mss.shared_dirty >> 10,
625 mss.private_clean >> 10,
626 mss.private_dirty >> 10,
Peter Zijlstra214e4712008-04-28 02:12:55 -0700627 mss.referenced >> 10,
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700628 mss.anonymous >> 10,
Dave Hansen4031a212011-03-22 16:33:01 -0700629 mss.anonymous_thp >> 10,
Mel Gorman08fba692009-01-06 14:38:53 -0800630 mss.swap >> 10,
Mel Gorman33402892009-01-06 14:38:54 -0800631 vma_kernel_pagesize(vma) >> 10,
Nikanth Karthikesan2d905082011-01-13 15:45:53 -0800632 vma_mmu_pagesize(vma) >> 10,
633 (vma->vm_flags & VM_LOCKED) ?
634 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
Matt Mackall4752c362008-02-04 22:29:02 -0800635
Konstantin Khlebnikovbca15542012-05-31 16:26:20 -0700636 if (vma->vm_flags & VM_NONLINEAR)
637 seq_printf(m, "Nonlinear: %8lu kB\n",
638 mss.nonlinear >> 10);
639
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800640 show_smap_vma_flags(m, vma);
641
Joe Korty7c88db02008-10-16 15:27:09 +0400642 if (m->count < m->size) /* vma is copied successfully */
Stephen Wilson31db58b2011-03-13 15:49:15 -0400643 m->version = (vma != get_gate_vma(task->mm))
644 ? vma->vm_start : 0;
Joe Korty7c88db02008-10-16 15:27:09 +0400645 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700646}
647
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700648static int show_pid_smap(struct seq_file *m, void *v)
649{
650 return show_smap(m, v, 1);
651}
652
653static int show_tid_smap(struct seq_file *m, void *v)
654{
655 return show_smap(m, v, 0);
656}
657
Jan Engelhardt03a44822008-02-08 04:21:19 -0800658static const struct seq_operations proc_pid_smaps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800659 .start = m_start,
660 .next = m_next,
661 .stop = m_stop,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700662 .show = show_pid_smap
Matt Mackalla6198792008-02-04 22:29:03 -0800663};
664
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700665static const struct seq_operations proc_tid_smaps_op = {
666 .start = m_start,
667 .next = m_next,
668 .stop = m_stop,
669 .show = show_tid_smap
670};
671
672static int pid_smaps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800673{
674 return do_maps_open(inode, file, &proc_pid_smaps_op);
675}
676
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700677static int tid_smaps_open(struct inode *inode, struct file *file)
678{
679 return do_maps_open(inode, file, &proc_tid_smaps_op);
680}
681
682const struct file_operations proc_pid_smaps_operations = {
683 .open = pid_smaps_open,
684 .read = seq_read,
685 .llseek = seq_lseek,
686 .release = seq_release_private,
687};
688
689const struct file_operations proc_tid_smaps_operations = {
690 .open = tid_smaps_open,
Matt Mackalla6198792008-02-04 22:29:03 -0800691 .read = seq_read,
692 .llseek = seq_lseek,
693 .release = seq_release_private,
694};
695
Pavel Emelyanov541c2372013-07-03 15:01:22 -0700696/*
697 * We do not want to have constant page-shift bits sitting in
698 * pagemap entries and are about to reuse them some time soon.
699 *
700 * Here's the "migration strategy":
701 * 1. when the system boots these bits remain what they are,
702 * but a warning about future change is printed in log;
703 * 2. once anyone clears soft-dirty bits via clear_refs file,
704 * these flag is set to denote, that user is aware of the
705 * new API and those page-shift bits change their meaning.
706 * The respective warning is printed in dmesg;
707 * 3. In a couple of releases we will remove all the mentions
708 * of page-shift in pagemap entries.
709 */
710
711static bool soft_dirty_cleared __read_mostly;
712
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700713enum clear_refs_types {
714 CLEAR_REFS_ALL = 1,
715 CLEAR_REFS_ANON,
716 CLEAR_REFS_MAPPED,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700717 CLEAR_REFS_SOFT_DIRTY,
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700718 CLEAR_REFS_LAST,
719};
720
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700721struct clear_refs_private {
722 struct vm_area_struct *vma;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700723 enum clear_refs_types type;
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700724};
725
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700726static inline void clear_soft_dirty(struct vm_area_struct *vma,
727 unsigned long addr, pte_t *pte)
728{
729#ifdef CONFIG_MEM_SOFT_DIRTY
730 /*
731 * The soft-dirty tracker uses #PF-s to catch writes
732 * to pages, so write-protect the pte as well. See the
733 * Documentation/vm/soft-dirty.txt for full description
734 * of how soft-dirty works.
735 */
736 pte_t ptent = *pte;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700737
738 if (pte_present(ptent)) {
739 ptent = pte_wrprotect(ptent);
740 ptent = pte_clear_flags(ptent, _PAGE_SOFT_DIRTY);
741 } else if (is_swap_pte(ptent)) {
742 ptent = pte_swp_clear_soft_dirty(ptent);
Cyrill Gorcunov41bb3472013-08-13 16:00:51 -0700743 } else if (pte_file(ptent)) {
744 ptent = pte_file_clear_soft_dirty(ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700745 }
746
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -0700747 if (vma->vm_flags & VM_SOFTDIRTY)
748 vma->vm_flags &= ~VM_SOFTDIRTY;
749
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700750 set_pte_at(vma->vm_mm, addr, pte, ptent);
751#endif
752}
753
Matt Mackalla6198792008-02-04 22:29:03 -0800754static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
Dave Hansen21650092008-06-12 15:21:47 -0700755 unsigned long end, struct mm_walk *walk)
Matt Mackalla6198792008-02-04 22:29:03 -0800756{
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700757 struct clear_refs_private *cp = walk->private;
758 struct vm_area_struct *vma = cp->vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800759 pte_t *pte, ptent;
760 spinlock_t *ptl;
761 struct page *page;
762
Kirill A. Shutemove1803772012-12-12 13:50:59 -0800763 split_huge_page_pmd(vma, addr, pmd);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700764 if (pmd_trans_unstable(pmd))
765 return 0;
Dave Hansen03319322011-03-22 16:32:56 -0700766
Matt Mackalla6198792008-02-04 22:29:03 -0800767 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
768 for (; addr != end; pte++, addr += PAGE_SIZE) {
769 ptent = *pte;
Matt Mackalla6198792008-02-04 22:29:03 -0800770
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700771 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
772 clear_soft_dirty(vma, addr, pte);
773 continue;
774 }
775
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700776 if (!pte_present(ptent))
777 continue;
778
Matt Mackalla6198792008-02-04 22:29:03 -0800779 page = vm_normal_page(vma, addr, ptent);
780 if (!page)
781 continue;
782
783 /* Clear accessed and referenced bits. */
784 ptep_test_and_clear_young(vma, addr, pte);
785 ClearPageReferenced(page);
786 }
787 pte_unmap_unlock(pte - 1, ptl);
788 cond_resched();
789 return 0;
790}
791
Matt Mackallf248dcb2008-02-04 22:29:03 -0800792static ssize_t clear_refs_write(struct file *file, const char __user *buf,
793 size_t count, loff_t *ppos)
David Rientjesb813e932007-05-06 14:49:24 -0700794{
Matt Mackallf248dcb2008-02-04 22:29:03 -0800795 struct task_struct *task;
Vincent Lifb92a4b2009-09-22 16:45:36 -0700796 char buffer[PROC_NUMBUF];
Matt Mackallf248dcb2008-02-04 22:29:03 -0800797 struct mm_struct *mm;
David Rientjesb813e932007-05-06 14:49:24 -0700798 struct vm_area_struct *vma;
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700799 enum clear_refs_types type;
800 int itype;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700801 int rv;
David Rientjesb813e932007-05-06 14:49:24 -0700802
Matt Mackallf248dcb2008-02-04 22:29:03 -0800803 memset(buffer, 0, sizeof(buffer));
804 if (count > sizeof(buffer) - 1)
805 count = sizeof(buffer) - 1;
806 if (copy_from_user(buffer, buf, count))
807 return -EFAULT;
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700808 rv = kstrtoint(strstrip(buffer), 10, &itype);
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700809 if (rv < 0)
810 return rv;
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700811 type = (enum clear_refs_types)itype;
812 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
Matt Mackallf248dcb2008-02-04 22:29:03 -0800813 return -EINVAL;
Pavel Emelyanov541c2372013-07-03 15:01:22 -0700814
815 if (type == CLEAR_REFS_SOFT_DIRTY) {
816 soft_dirty_cleared = true;
817 pr_warn_once("The pagemap bits 55-60 has changed their meaning! "
818 "See the linux/Documentation/vm/pagemap.txt for details.\n");
819 }
820
Al Viro496ad9a2013-01-23 17:07:38 -0500821 task = get_proc_task(file_inode(file));
Matt Mackallf248dcb2008-02-04 22:29:03 -0800822 if (!task)
823 return -ESRCH;
824 mm = get_task_mm(task);
825 if (mm) {
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700826 struct clear_refs_private cp = {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700827 .type = type,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700828 };
Andrew Morton20cbc972008-07-05 12:29:05 -0700829 struct mm_walk clear_refs_walk = {
830 .pmd_entry = clear_refs_pte_range,
831 .mm = mm,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700832 .private = &cp,
Andrew Morton20cbc972008-07-05 12:29:05 -0700833 };
Matt Mackallf248dcb2008-02-04 22:29:03 -0800834 down_read(&mm->mmap_sem);
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700835 if (type == CLEAR_REFS_SOFT_DIRTY)
836 mmu_notifier_invalidate_range_start(mm, 0, -1);
Dave Hansen21650092008-06-12 15:21:47 -0700837 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700838 cp.vma = vma;
Moussa A. Ba398499d2009-09-21 17:02:29 -0700839 if (is_vm_hugetlb_page(vma))
840 continue;
841 /*
842 * Writing 1 to /proc/pid/clear_refs affects all pages.
843 *
844 * Writing 2 to /proc/pid/clear_refs only affects
845 * Anonymous pages.
846 *
847 * Writing 3 to /proc/pid/clear_refs only affects file
848 * mapped pages.
849 */
850 if (type == CLEAR_REFS_ANON && vma->vm_file)
851 continue;
852 if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
853 continue;
854 walk_page_range(vma->vm_start, vma->vm_end,
855 &clear_refs_walk);
Dave Hansen21650092008-06-12 15:21:47 -0700856 }
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700857 if (type == CLEAR_REFS_SOFT_DIRTY)
858 mmu_notifier_invalidate_range_end(mm, 0, -1);
Matt Mackallf248dcb2008-02-04 22:29:03 -0800859 flush_tlb_mm(mm);
860 up_read(&mm->mmap_sem);
861 mmput(mm);
862 }
863 put_task_struct(task);
Vincent Lifb92a4b2009-09-22 16:45:36 -0700864
865 return count;
David Rientjesb813e932007-05-06 14:49:24 -0700866}
867
Matt Mackallf248dcb2008-02-04 22:29:03 -0800868const struct file_operations proc_clear_refs_operations = {
869 .write = clear_refs_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200870 .llseek = noop_llseek,
Matt Mackallf248dcb2008-02-04 22:29:03 -0800871};
872
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700873typedef struct {
874 u64 pme;
875} pagemap_entry_t;
876
Matt Mackall85863e42008-02-04 22:29:04 -0800877struct pagemapread {
yonghua zheng8c829622013-08-13 16:01:03 -0700878 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700879 pagemap_entry_t *buffer;
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700880 bool v2;
Matt Mackall85863e42008-02-04 22:29:04 -0800881};
882
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700883#define PAGEMAP_WALK_SIZE (PMD_SIZE)
884#define PAGEMAP_WALK_MASK (PMD_MASK)
885
yonghua zheng8c829622013-08-13 16:01:03 -0700886#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -0500887#define PM_STATUS_BITS 3
888#define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
889#define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
890#define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
891#define PM_PSHIFT_BITS 6
892#define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
893#define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700894#define __PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -0500895#define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
896#define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700897/* in "new" pagemap pshift bits are occupied with more status bits */
898#define PM_STATUS2(v2, x) (__PM_PSHIFT(v2 ? x : PAGE_SHIFT))
Hans Rosenfeldf16278c2008-03-21 18:46:59 -0500899
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700900#define __PM_SOFT_DIRTY (1LL)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -0500901#define PM_PRESENT PM_STATUS(4LL)
902#define PM_SWAP PM_STATUS(2LL)
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700903#define PM_FILE PM_STATUS(1LL)
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700904#define PM_NOT_PRESENT(v2) PM_STATUS2(v2, 0)
Matt Mackall85863e42008-02-04 22:29:04 -0800905#define PM_END_OF_BUFFER 1
906
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700907static inline pagemap_entry_t make_pme(u64 val)
908{
909 return (pagemap_entry_t) { .pme = val };
910}
911
912static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
Matt Mackall85863e42008-02-04 22:29:04 -0800913 struct pagemapread *pm)
914{
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700915 pm->buffer[pm->pos++] = *pme;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +0900916 if (pm->pos >= pm->len)
Thomas Tuttleaae86792008-06-05 22:46:31 -0700917 return PM_END_OF_BUFFER;
Matt Mackall85863e42008-02-04 22:29:04 -0800918 return 0;
919}
920
921static int pagemap_pte_hole(unsigned long start, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700922 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -0800923{
Dave Hansen21650092008-06-12 15:21:47 -0700924 struct pagemapread *pm = walk->private;
Matt Mackall85863e42008-02-04 22:29:04 -0800925 unsigned long addr;
926 int err = 0;
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700927 pagemap_entry_t pme = make_pme(PM_NOT_PRESENT(pm->v2));
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700928
Matt Mackall85863e42008-02-04 22:29:04 -0800929 for (addr = start; addr < end; addr += PAGE_SIZE) {
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700930 err = add_to_pagemap(addr, &pme, pm);
Matt Mackall85863e42008-02-04 22:29:04 -0800931 if (err)
932 break;
933 }
934 return err;
935}
936
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700937static void pte_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700938 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
Matt Mackall85863e42008-02-04 22:29:04 -0800939{
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700940 u64 frame, flags;
941 struct page *page = NULL;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700942 int flags2 = 0;
Matt Mackall85863e42008-02-04 22:29:04 -0800943
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700944 if (pte_present(pte)) {
945 frame = pte_pfn(pte);
946 flags = PM_PRESENT;
947 page = vm_normal_page(vma, addr, pte);
Cyrill Gorcunove9cdd6e2013-10-16 13:46:53 -0700948 if (pte_soft_dirty(pte))
949 flags2 |= __PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700950 } else if (is_swap_pte(pte)) {
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700951 swp_entry_t entry;
952 if (pte_swp_soft_dirty(pte))
953 flags2 |= __PM_SOFT_DIRTY;
954 entry = pte_to_swp_entry(pte);
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700955 frame = swp_type(entry) |
956 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
957 flags = PM_SWAP;
958 if (is_migration_entry(entry))
959 page = migration_entry_to_page(entry);
960 } else {
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -0700961 if (vma->vm_flags & VM_SOFTDIRTY)
962 flags2 |= __PM_SOFT_DIRTY;
963 *pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, flags2));
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700964 return;
965 }
966
967 if (page && !PageAnon(page))
968 flags |= PM_FILE;
Cyrill Gorcunove9cdd6e2013-10-16 13:46:53 -0700969 if ((vma->vm_flags & VM_SOFTDIRTY))
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700970 flags2 |= __PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700971
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700972 *pme = make_pme(PM_PFRAME(frame) | PM_STATUS2(pm->v2, flags2) | flags);
Dave Hansenbcf80392008-06-12 15:21:48 -0700973}
974
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700975#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700976static void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700977 pmd_t pmd, int offset, int pmd_flags2)
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700978{
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700979 /*
980 * Currently pmd for thp is always present because thp can not be
981 * swapped-out, migrated, or HWPOISONed (split in such cases instead.)
982 * This if-check is just to prepare for future implementation.
983 */
984 if (pmd_present(pmd))
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700985 *pme = make_pme(PM_PFRAME(pmd_pfn(pmd) + offset)
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700986 | PM_STATUS2(pm->v2, pmd_flags2) | PM_PRESENT);
Konstantin Khlebnikov16fbdce2012-05-10 13:01:43 -0700987 else
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -0700988 *pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, pmd_flags2));
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700989}
990#else
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700991static inline void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700992 pmd_t pmd, int offset, int pmd_flags2)
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700993{
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700994}
995#endif
996
Matt Mackall85863e42008-02-04 22:29:04 -0800997static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700998 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -0800999{
Dave Hansenbcf80392008-06-12 15:21:48 -07001000 struct vm_area_struct *vma;
Dave Hansen21650092008-06-12 15:21:47 -07001001 struct pagemapread *pm = walk->private;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001002 spinlock_t *ptl;
Matt Mackall85863e42008-02-04 22:29:04 -08001003 pte_t *pte;
1004 int err = 0;
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -07001005 pagemap_entry_t pme = make_pme(PM_NOT_PRESENT(pm->v2));
Matt Mackall85863e42008-02-04 22:29:04 -08001006
Dave Hansenbcf80392008-06-12 15:21:48 -07001007 /* find the first VMA at or above 'addr' */
1008 vma = find_vma(walk->mm, addr);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001009 if (vma && pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001010 int pmd_flags2;
1011
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001012 if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(*pmd))
1013 pmd_flags2 = __PM_SOFT_DIRTY;
1014 else
1015 pmd_flags2 = 0;
1016
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001017 for (; addr != end; addr += PAGE_SIZE) {
1018 unsigned long offset;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001019
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001020 offset = (addr & ~PAGEMAP_WALK_MASK) >>
1021 PAGE_SHIFT;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001022 thp_pmd_to_pagemap_entry(&pme, pm, *pmd, offset, pmd_flags2);
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001023 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001024 if (err)
1025 break;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001026 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001027 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001028 return err;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001029 }
1030
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07001031 if (pmd_trans_unstable(pmd))
1032 return 0;
Matt Mackall85863e42008-02-04 22:29:04 -08001033 for (; addr != end; addr += PAGE_SIZE) {
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001034 int flags2;
Dave Hansenbcf80392008-06-12 15:21:48 -07001035
1036 /* check to see if we've left 'vma' behind
1037 * and need a new, higher one */
Konstantin Khlebnikov16fbdce2012-05-10 13:01:43 -07001038 if (vma && (addr >= vma->vm_end)) {
Dave Hansenbcf80392008-06-12 15:21:48 -07001039 vma = find_vma(walk->mm, addr);
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001040 if (vma && (vma->vm_flags & VM_SOFTDIRTY))
1041 flags2 = __PM_SOFT_DIRTY;
1042 else
1043 flags2 = 0;
1044 pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, flags2));
Konstantin Khlebnikov16fbdce2012-05-10 13:01:43 -07001045 }
Dave Hansenbcf80392008-06-12 15:21:48 -07001046
1047 /* check that 'vma' actually covers this address,
1048 * and that it isn't a huge page vma */
1049 if (vma && (vma->vm_start <= addr) &&
1050 !is_vm_hugetlb_page(vma)) {
1051 pte = pte_offset_map(pmd, addr);
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -07001052 pte_to_pagemap_entry(&pme, pm, vma, addr, *pte);
Dave Hansenbcf80392008-06-12 15:21:48 -07001053 /* unmap before userspace copy */
1054 pte_unmap(pte);
1055 }
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001056 err = add_to_pagemap(addr, &pme, pm);
Matt Mackall85863e42008-02-04 22:29:04 -08001057 if (err)
1058 return err;
1059 }
1060
1061 cond_resched();
1062
1063 return err;
1064}
1065
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001066#ifdef CONFIG_HUGETLB_PAGE
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -07001067static void huge_pte_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001068 pte_t pte, int offset, int flags2)
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001069{
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001070 if (pte_present(pte))
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001071 *pme = make_pme(PM_PFRAME(pte_pfn(pte) + offset) |
1072 PM_STATUS2(pm->v2, flags2) |
1073 PM_PRESENT);
Konstantin Khlebnikov16fbdce2012-05-10 13:01:43 -07001074 else
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001075 *pme = make_pme(PM_NOT_PRESENT(pm->v2) |
1076 PM_STATUS2(pm->v2, flags2));
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001077}
1078
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001079/* This function walks within one hugetlb entry in the single call */
1080static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
1081 unsigned long addr, unsigned long end,
1082 struct mm_walk *walk)
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001083{
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001084 struct pagemapread *pm = walk->private;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001085 struct vm_area_struct *vma;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001086 int err = 0;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001087 int flags2;
Konstantin Khlebnikov16fbdce2012-05-10 13:01:43 -07001088 pagemap_entry_t pme;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001089
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001090 vma = find_vma(walk->mm, addr);
1091 WARN_ON_ONCE(!vma);
1092
1093 if (vma && (vma->vm_flags & VM_SOFTDIRTY))
1094 flags2 = __PM_SOFT_DIRTY;
1095 else
1096 flags2 = 0;
1097
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001098 for (; addr != end; addr += PAGE_SIZE) {
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001099 int offset = (addr & ~hmask) >> PAGE_SHIFT;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001100 huge_pte_to_pagemap_entry(&pme, pm, *pte, offset, flags2);
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001101 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001102 if (err)
1103 return err;
1104 }
1105
1106 cond_resched();
1107
1108 return err;
1109}
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001110#endif /* HUGETLB_PAGE */
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001111
Matt Mackall85863e42008-02-04 22:29:04 -08001112/*
1113 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1114 *
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001115 * For each page in the address space, this file contains one 64-bit entry
1116 * consisting of the following:
1117 *
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001118 * Bits 0-54 page frame number (PFN) if present
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001119 * Bits 0-4 swap type if swapped
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001120 * Bits 5-54 swap offset if swapped
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001121 * Bits 55-60 page shift (page size = 1<<page shift)
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001122 * Bit 61 page is file-page or shared-anon
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001123 * Bit 62 page swapped
1124 * Bit 63 page present
1125 *
1126 * If the page is not present but in swap, then the PFN contains an
1127 * encoding of the swap file number and the page's offset into the
1128 * swap. Unmapped pages return a null PFN. This allows determining
Matt Mackall85863e42008-02-04 22:29:04 -08001129 * precisely which pages are mapped (or in swap) and comparing mapped
1130 * pages between processes.
1131 *
1132 * Efficient users of this interface will use /proc/pid/maps to
1133 * determine which areas of memory are actually mapped and llseek to
1134 * skip over unmapped regions.
1135 */
1136static ssize_t pagemap_read(struct file *file, char __user *buf,
1137 size_t count, loff_t *ppos)
1138{
Al Viro496ad9a2013-01-23 17:07:38 -05001139 struct task_struct *task = get_proc_task(file_inode(file));
Matt Mackall85863e42008-02-04 22:29:04 -08001140 struct mm_struct *mm;
1141 struct pagemapread pm;
Matt Mackall85863e42008-02-04 22:29:04 -08001142 int ret = -ESRCH;
Alexey Dobriyanee1e6ab2008-07-21 14:21:36 -07001143 struct mm_walk pagemap_walk = {};
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001144 unsigned long src;
1145 unsigned long svpfn;
1146 unsigned long start_vaddr;
1147 unsigned long end_vaddr;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001148 int copied = 0;
Matt Mackall85863e42008-02-04 22:29:04 -08001149
1150 if (!task)
1151 goto out;
1152
Matt Mackall85863e42008-02-04 22:29:04 -08001153 ret = -EINVAL;
1154 /* file position must be aligned */
Thomas Tuttleaae86792008-06-05 22:46:31 -07001155 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
Marcelo Tosattifb393802008-03-13 12:32:35 -07001156 goto out_task;
Matt Mackall85863e42008-02-04 22:29:04 -08001157
1158 ret = 0;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001159 if (!count)
1160 goto out_task;
1161
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001162 pm.v2 = soft_dirty_cleared;
yonghua zheng8c829622013-08-13 16:01:03 -07001163 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1164 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001165 ret = -ENOMEM;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001166 if (!pm.buffer)
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001167 goto out_task;
1168
Cong Wange7dcd992012-05-31 16:26:17 -07001169 mm = mm_access(task, PTRACE_MODE_READ);
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001170 ret = PTR_ERR(mm);
1171 if (!mm || IS_ERR(mm))
1172 goto out_free;
Matt Mackall85863e42008-02-04 22:29:04 -08001173
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001174 pagemap_walk.pmd_entry = pagemap_pte_range;
1175 pagemap_walk.pte_hole = pagemap_pte_hole;
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001176#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001177 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001178#endif
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001179 pagemap_walk.mm = mm;
1180 pagemap_walk.private = &pm;
Matt Mackall85863e42008-02-04 22:29:04 -08001181
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001182 src = *ppos;
1183 svpfn = src / PM_ENTRY_BYTES;
1184 start_vaddr = svpfn << PAGE_SHIFT;
1185 end_vaddr = TASK_SIZE_OF(task);
Matt Mackall85863e42008-02-04 22:29:04 -08001186
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001187 /* watch out for wraparound */
1188 if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
1189 start_vaddr = end_vaddr;
1190
1191 /*
1192 * The odds are that this will stop walking way
1193 * before end_vaddr, because the length of the
1194 * user buffer is tracked in "pm", and the walk
1195 * will stop when we hit the end of the buffer.
1196 */
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001197 ret = 0;
1198 while (count && (start_vaddr < end_vaddr)) {
1199 int len;
1200 unsigned long end;
Matt Mackall85863e42008-02-04 22:29:04 -08001201
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001202 pm.pos = 0;
Naoya Horiguchiea251c12010-11-24 12:57:13 -08001203 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001204 /* overflow ? */
1205 if (end < start_vaddr || end > end_vaddr)
1206 end = end_vaddr;
1207 down_read(&mm->mmap_sem);
1208 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1209 up_read(&mm->mmap_sem);
1210 start_vaddr = end;
1211
1212 len = min(count, PM_ENTRY_BYTES * pm.pos);
Dan Carpenter309361e02010-04-06 13:45:39 +03001213 if (copy_to_user(buf, pm.buffer, len)) {
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001214 ret = -EFAULT;
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001215 goto out_mm;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001216 }
1217 copied += len;
1218 buf += len;
1219 count -= len;
Matt Mackall85863e42008-02-04 22:29:04 -08001220 }
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001221 *ppos += copied;
1222 if (!ret || ret == PM_END_OF_BUFFER)
1223 ret = copied;
1224
Marcelo Tosattifb393802008-03-13 12:32:35 -07001225out_mm:
1226 mmput(mm);
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001227out_free:
1228 kfree(pm.buffer);
Matt Mackall85863e42008-02-04 22:29:04 -08001229out_task:
1230 put_task_struct(task);
1231out:
1232 return ret;
1233}
1234
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001235static int pagemap_open(struct inode *inode, struct file *file)
1236{
1237 pr_warn_once("Bits 55-60 of /proc/PID/pagemap entries are about "
1238 "to stop being page-shift some time soon. See the "
1239 "linux/Documentation/vm/pagemap.txt for details.\n");
1240 return 0;
1241}
1242
Matt Mackall85863e42008-02-04 22:29:04 -08001243const struct file_operations proc_pagemap_operations = {
1244 .llseek = mem_lseek, /* borrow this */
1245 .read = pagemap_read,
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001246 .open = pagemap_open,
Matt Mackall85863e42008-02-04 22:29:04 -08001247};
Matt Mackall1e883282008-02-04 22:29:07 -08001248#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall85863e42008-02-04 22:29:04 -08001249
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001250#ifdef CONFIG_NUMA
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001251
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001252struct numa_maps {
1253 struct vm_area_struct *vma;
1254 unsigned long pages;
1255 unsigned long anon;
1256 unsigned long active;
1257 unsigned long writeback;
1258 unsigned long mapcount_max;
1259 unsigned long dirty;
1260 unsigned long swapcache;
1261 unsigned long node[MAX_NUMNODES];
1262};
1263
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001264struct numa_maps_private {
1265 struct proc_maps_private proc_maps;
1266 struct numa_maps md;
1267};
1268
Dave Hanseneb4866d2011-09-20 15:19:38 -07001269static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1270 unsigned long nr_pages)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001271{
1272 int count = page_mapcount(page);
1273
Dave Hanseneb4866d2011-09-20 15:19:38 -07001274 md->pages += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001275 if (pte_dirty || PageDirty(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001276 md->dirty += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001277
1278 if (PageSwapCache(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001279 md->swapcache += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001280
1281 if (PageActive(page) || PageUnevictable(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001282 md->active += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001283
1284 if (PageWriteback(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001285 md->writeback += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001286
1287 if (PageAnon(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001288 md->anon += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001289
1290 if (count > md->mapcount_max)
1291 md->mapcount_max = count;
1292
Dave Hanseneb4866d2011-09-20 15:19:38 -07001293 md->node[page_to_nid(page)] += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001294}
1295
Dave Hansen3200a8a2011-09-20 15:19:39 -07001296static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1297 unsigned long addr)
1298{
1299 struct page *page;
1300 int nid;
1301
1302 if (!pte_present(pte))
1303 return NULL;
1304
1305 page = vm_normal_page(vma, addr, pte);
1306 if (!page)
1307 return NULL;
1308
1309 if (PageReserved(page))
1310 return NULL;
1311
1312 nid = page_to_nid(page);
Lai Jiangshan4ff1b2c2012-12-12 13:51:25 -08001313 if (!node_isset(nid, node_states[N_MEMORY]))
Dave Hansen3200a8a2011-09-20 15:19:39 -07001314 return NULL;
1315
1316 return page;
1317}
1318
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001319static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1320 unsigned long end, struct mm_walk *walk)
1321{
1322 struct numa_maps *md;
1323 spinlock_t *ptl;
1324 pte_t *orig_pte;
1325 pte_t *pte;
1326
1327 md = walk->private;
Dave Hansen32ef4382011-09-20 15:19:41 -07001328
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001329 if (pmd_trans_huge_lock(pmd, md->vma, &ptl) == 1) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001330 pte_t huge_pte = *(pte_t *)pmd;
1331 struct page *page;
1332
1333 page = can_gather_numa_stats(huge_pte, md->vma, addr);
1334 if (page)
1335 gather_stats(page, md, pte_dirty(huge_pte),
1336 HPAGE_PMD_SIZE/PAGE_SIZE);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001337 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001338 return 0;
Dave Hansen32ef4382011-09-20 15:19:41 -07001339 }
1340
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001341 if (pmd_trans_unstable(pmd))
1342 return 0;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001343 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1344 do {
Dave Hansen3200a8a2011-09-20 15:19:39 -07001345 struct page *page = can_gather_numa_stats(*pte, md->vma, addr);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001346 if (!page)
1347 continue;
Dave Hanseneb4866d2011-09-20 15:19:38 -07001348 gather_stats(page, md, pte_dirty(*pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001349
1350 } while (pte++, addr += PAGE_SIZE, addr != end);
1351 pte_unmap_unlock(orig_pte, ptl);
1352 return 0;
1353}
1354#ifdef CONFIG_HUGETLB_PAGE
1355static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1356 unsigned long addr, unsigned long end, struct mm_walk *walk)
1357{
1358 struct numa_maps *md;
1359 struct page *page;
1360
1361 if (pte_none(*pte))
1362 return 0;
1363
1364 page = pte_page(*pte);
1365 if (!page)
1366 return 0;
1367
1368 md = walk->private;
Dave Hanseneb4866d2011-09-20 15:19:38 -07001369 gather_stats(page, md, pte_dirty(*pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001370 return 0;
1371}
1372
1373#else
1374static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
1375 unsigned long addr, unsigned long end, struct mm_walk *walk)
1376{
1377 return 0;
1378}
1379#endif
1380
1381/*
1382 * Display pages allocated per node and memory policy via /proc.
1383 */
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001384static int show_numa_map(struct seq_file *m, void *v, int is_pid)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001385{
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001386 struct numa_maps_private *numa_priv = m->private;
1387 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001388 struct vm_area_struct *vma = v;
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001389 struct numa_maps *md = &numa_priv->md;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001390 struct file *file = vma->vm_file;
David Rientjes32f85162012-10-16 17:31:23 -07001391 struct task_struct *task = proc_priv->task;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001392 struct mm_struct *mm = vma->vm_mm;
1393 struct mm_walk walk = {};
1394 struct mempolicy *pol;
David Rientjes948927e2013-11-12 15:07:28 -08001395 char buffer[64];
1396 int nid;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001397
1398 if (!mm)
1399 return 0;
1400
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001401 /* Ensure we start with an empty set of numa_maps statistics. */
1402 memset(md, 0, sizeof(*md));
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001403
1404 md->vma = vma;
1405
1406 walk.hugetlb_entry = gather_hugetbl_stats;
1407 walk.pmd_entry = gather_pte_stats;
1408 walk.private = md;
1409 walk.mm = mm;
1410
David Rientjes32f85162012-10-16 17:31:23 -07001411 pol = get_vma_policy(task, vma, vma->vm_start);
David Rientjes948927e2013-11-12 15:07:28 -08001412 mpol_to_str(buffer, sizeof(buffer), pol);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001413 mpol_cond_put(pol);
1414
1415 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1416
1417 if (file) {
1418 seq_printf(m, " file=");
1419 seq_path(m, &file->f_path, "\n\t= ");
1420 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1421 seq_printf(m, " heap");
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001422 } else {
David Rientjes32f85162012-10-16 17:31:23 -07001423 pid_t tid = vm_is_stack(task, vma, is_pid);
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001424 if (tid != 0) {
1425 /*
1426 * Thread stack in /proc/PID/task/TID/maps or
1427 * the main process stack.
1428 */
1429 if (!is_pid || (vma->vm_start <= mm->start_stack &&
1430 vma->vm_end >= mm->start_stack))
1431 seq_printf(m, " stack");
1432 else
1433 seq_printf(m, " stack:%d", tid);
1434 }
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001435 }
1436
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001437 if (is_vm_hugetlb_page(vma))
1438 seq_printf(m, " huge");
1439
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001440 walk_page_range(vma->vm_start, vma->vm_end, &walk);
1441
1442 if (!md->pages)
1443 goto out;
1444
1445 if (md->anon)
1446 seq_printf(m, " anon=%lu", md->anon);
1447
1448 if (md->dirty)
1449 seq_printf(m, " dirty=%lu", md->dirty);
1450
1451 if (md->pages != md->anon && md->pages != md->dirty)
1452 seq_printf(m, " mapped=%lu", md->pages);
1453
1454 if (md->mapcount_max > 1)
1455 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1456
1457 if (md->swapcache)
1458 seq_printf(m, " swapcache=%lu", md->swapcache);
1459
1460 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1461 seq_printf(m, " active=%lu", md->active);
1462
1463 if (md->writeback)
1464 seq_printf(m, " writeback=%lu", md->writeback);
1465
David Rientjes948927e2013-11-12 15:07:28 -08001466 for_each_node_state(nid, N_MEMORY)
1467 if (md->node[nid])
1468 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001469out:
1470 seq_putc(m, '\n');
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001471
1472 if (m->count < m->size)
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001473 m->version = (vma != proc_priv->tail_vma) ? vma->vm_start : 0;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001474 return 0;
1475}
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001476
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001477static int show_pid_numa_map(struct seq_file *m, void *v)
1478{
1479 return show_numa_map(m, v, 1);
1480}
1481
1482static int show_tid_numa_map(struct seq_file *m, void *v)
1483{
1484 return show_numa_map(m, v, 0);
1485}
1486
Jan Engelhardt03a44822008-02-08 04:21:19 -08001487static const struct seq_operations proc_pid_numa_maps_op = {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001488 .start = m_start,
1489 .next = m_next,
1490 .stop = m_stop,
1491 .show = show_pid_numa_map,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001492};
Eric W. Biederman662795d2006-06-26 00:25:48 -07001493
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001494static const struct seq_operations proc_tid_numa_maps_op = {
1495 .start = m_start,
1496 .next = m_next,
1497 .stop = m_stop,
1498 .show = show_tid_numa_map,
1499};
1500
1501static int numa_maps_open(struct inode *inode, struct file *file,
1502 const struct seq_operations *ops)
Eric W. Biederman662795d2006-06-26 00:25:48 -07001503{
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001504 struct numa_maps_private *priv;
1505 int ret = -ENOMEM;
1506 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1507 if (priv) {
1508 priv->proc_maps.pid = proc_pid(inode);
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001509 ret = seq_open(file, ops);
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001510 if (!ret) {
1511 struct seq_file *m = file->private_data;
1512 m->private = priv;
1513 } else {
1514 kfree(priv);
1515 }
1516 }
1517 return ret;
Eric W. Biederman662795d2006-06-26 00:25:48 -07001518}
1519
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001520static int pid_numa_maps_open(struct inode *inode, struct file *file)
1521{
1522 return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1523}
1524
1525static int tid_numa_maps_open(struct inode *inode, struct file *file)
1526{
1527 return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1528}
1529
1530const struct file_operations proc_pid_numa_maps_operations = {
1531 .open = pid_numa_maps_open,
1532 .read = seq_read,
1533 .llseek = seq_lseek,
1534 .release = seq_release_private,
1535};
1536
1537const struct file_operations proc_tid_numa_maps_operations = {
1538 .open = tid_numa_maps_open,
Eric W. Biederman662795d2006-06-26 00:25:48 -07001539 .read = seq_read,
1540 .llseek = seq_lseek,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001541 .release = seq_release_private,
Eric W. Biederman662795d2006-06-26 00:25:48 -07001542};
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001543#endif /* CONFIG_NUMA */