blob: 07c86f51d225f4d34a41229f0238c974f57098b4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/mm.h>
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002#include <linux/vmacache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/hugetlb.h>
Dave Hansen22e057c2011-03-22 16:33:00 -07004#include <linux/huge_mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/mount.h>
6#include <linux/seq_file.h>
Mauricio Line070ad42005-09-03 15:55:10 -07007#include <linux/highmem.h>
Kees Cook5096add2007-05-08 00:26:04 -07008#include <linux/ptrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Christoph Lameter6e21c8f2005-09-03 15:54:45 -070010#include <linux/pagemap.h>
11#include <linux/mempolicy.h>
Dave Hansen22e057c2011-03-22 16:33:00 -070012#include <linux/rmap.h>
Matt Mackall85863e42008-02-04 22:29:04 -080013#include <linux/swap.h>
14#include <linux/swapops.h>
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -070015#include <linux/mmu_notifier.h>
Mauricio Line070ad42005-09-03 15:55:10 -070016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/elf.h>
18#include <asm/uaccess.h>
Mauricio Line070ad42005-09-03 15:55:10 -070019#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "internal.h"
21
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080022void task_mem(struct seq_file *m, struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -080024 unsigned long data, text, lib, swap, ptes, pmds;
Hugh Dickins365e9c872005-10-29 18:16:18 -070025 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
26
27 /*
28 * Note: to minimize their overhead, mm maintains hiwater_vm and
29 * hiwater_rss only when about to *lower* total_vm or rss. Any
30 * collector of these hiwater stats must therefore get total_vm
31 * and rss too, which will usually be the higher. Barriers? not
32 * worth the effort, such snapshots can always be inconsistent.
33 */
34 hiwater_vm = total_vm = mm->total_vm;
35 if (hiwater_vm < mm->hiwater_vm)
36 hiwater_vm = mm->hiwater_vm;
37 hiwater_rss = total_rss = get_mm_rss(mm);
38 if (hiwater_rss < mm->hiwater_rss)
39 hiwater_rss = mm->hiwater_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 data = mm->total_vm - mm->shared_vm - mm->stack_vm;
42 text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
43 lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080044 swap = get_mm_counter(mm, MM_SWAPENTS);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -080045 ptes = PTRS_PER_PTE * sizeof(pte_t) * atomic_long_read(&mm->nr_ptes);
46 pmds = PTRS_PER_PMD * sizeof(pmd_t) * mm_nr_pmds(mm);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080047 seq_printf(m,
Hugh Dickins365e9c872005-10-29 18:16:18 -070048 "VmPeak:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 "VmSize:\t%8lu kB\n"
50 "VmLck:\t%8lu kB\n"
Christoph Lameterbc3e53f2011-10-31 17:07:30 -070051 "VmPin:\t%8lu kB\n"
Hugh Dickins365e9c872005-10-29 18:16:18 -070052 "VmHWM:\t%8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 "VmRSS:\t%8lu kB\n"
54 "VmData:\t%8lu kB\n"
55 "VmStk:\t%8lu kB\n"
56 "VmExe:\t%8lu kB\n"
57 "VmLib:\t%8lu kB\n"
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080058 "VmPTE:\t%8lu kB\n"
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -080059 "VmPMD:\t%8lu kB\n"
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080060 "VmSwap:\t%8lu kB\n",
Hugh Dickins365e9c872005-10-29 18:16:18 -070061 hiwater_vm << (PAGE_SHIFT-10),
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -070062 total_vm << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 mm->locked_vm << (PAGE_SHIFT-10),
Christoph Lameterbc3e53f2011-10-31 17:07:30 -070064 mm->pinned_vm << (PAGE_SHIFT-10),
Hugh Dickins365e9c872005-10-29 18:16:18 -070065 hiwater_rss << (PAGE_SHIFT-10),
66 total_rss << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 data << (PAGE_SHIFT-10),
68 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -080069 ptes >> 10,
70 pmds >> 10,
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080071 swap << (PAGE_SHIFT-10));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74unsigned long task_vsize(struct mm_struct *mm)
75{
76 return PAGE_SIZE * mm->total_vm;
77}
78
Alexey Dobriyana2ade7b2011-01-12 17:00:32 -080079unsigned long task_statm(struct mm_struct *mm,
80 unsigned long *shared, unsigned long *text,
81 unsigned long *data, unsigned long *resident)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -080083 *shared = get_mm_counter(mm, MM_FILEPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
85 >> PAGE_SHIFT;
86 *data = mm->total_vm - mm->shared_vm;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -080087 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return mm->total_vm;
89}
90
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +090091#ifdef CONFIG_NUMA
92/*
Oleg Nesterov498f2372014-10-09 15:27:52 -070093 * Save get_task_policy() for show_numa_map().
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +090094 */
95static void hold_task_mempolicy(struct proc_maps_private *priv)
96{
97 struct task_struct *task = priv->task;
98
99 task_lock(task);
Oleg Nesterov498f2372014-10-09 15:27:52 -0700100 priv->task_mempolicy = get_task_policy(task);
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900101 mpol_get(priv->task_mempolicy);
102 task_unlock(task);
103}
104static void release_task_mempolicy(struct proc_maps_private *priv)
105{
106 mpol_put(priv->task_mempolicy);
107}
108#else
109static void hold_task_mempolicy(struct proc_maps_private *priv)
110{
111}
112static void release_task_mempolicy(struct proc_maps_private *priv)
113{
114}
115#endif
116
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700117static void vma_stop(struct proc_maps_private *priv)
Mauricio Line070ad42005-09-03 15:55:10 -0700118{
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700119 struct mm_struct *mm = priv->mm;
120
121 release_task_mempolicy(priv);
122 up_read(&mm->mmap_sem);
123 mmput(mm);
Matt Mackalla6198792008-02-04 22:29:03 -0800124}
125
Oleg Nesterovad2a00e2014-10-09 15:25:39 -0700126static struct vm_area_struct *
127m_next_vma(struct proc_maps_private *priv, struct vm_area_struct *vma)
128{
129 if (vma == priv->tail_vma)
130 return NULL;
131 return vma->vm_next ?: priv->tail_vma;
132}
133
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700134static void m_cache_vma(struct seq_file *m, struct vm_area_struct *vma)
135{
136 if (m->count < m->size) /* vma is copied successfully */
137 m->version = m_next_vma(m->private, vma) ? vma->vm_start : -1UL;
138}
139
Oleg Nesterov0c255322014-10-09 15:25:36 -0700140static void *m_start(struct seq_file *m, loff_t *ppos)
Matt Mackalla6198792008-02-04 22:29:03 -0800141{
142 struct proc_maps_private *priv = m->private;
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700143 unsigned long last_addr = m->version;
Matt Mackalla6198792008-02-04 22:29:03 -0800144 struct mm_struct *mm;
Oleg Nesterov0c255322014-10-09 15:25:36 -0700145 struct vm_area_struct *vma;
146 unsigned int pos = *ppos;
Matt Mackalla6198792008-02-04 22:29:03 -0800147
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700148 /* See m_cache_vma(). Zero at the start or after lseek. */
149 if (last_addr == -1UL)
150 return NULL;
151
Oleg Nesterov2c033762014-10-09 15:25:51 -0700152 priv->task = get_proc_task(priv->inode);
Matt Mackalla6198792008-02-04 22:29:03 -0800153 if (!priv->task)
Al Viroec6fd8a2011-02-15 22:22:54 -0500154 return ERR_PTR(-ESRCH);
Matt Mackalla6198792008-02-04 22:29:03 -0800155
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700156 mm = priv->mm;
157 if (!mm || !atomic_inc_not_zero(&mm->mm_users))
158 return NULL;
Oleg Nesterov0c255322014-10-09 15:25:36 -0700159
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200160 down_read(&mm->mmap_sem);
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900161 hold_task_mempolicy(priv);
Oleg Nesterov0c255322014-10-09 15:25:36 -0700162 priv->tail_vma = get_gate_vma(mm);
Matt Mackalla6198792008-02-04 22:29:03 -0800163
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700164 if (last_addr) {
165 vma = find_vma(mm, last_addr);
166 if (vma && (vma = m_next_vma(priv, vma)))
167 return vma;
168 }
169
170 m->version = 0;
Oleg Nesterov0c255322014-10-09 15:25:36 -0700171 if (pos < mm->map_count) {
Oleg Nesterov557c2d82014-10-09 15:25:43 -0700172 for (vma = mm->mmap; pos; pos--) {
173 m->version = vma->vm_start;
Matt Mackalla6198792008-02-04 22:29:03 -0800174 vma = vma->vm_next;
Oleg Nesterov557c2d82014-10-09 15:25:43 -0700175 }
Oleg Nesterov0c255322014-10-09 15:25:36 -0700176 return vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800177 }
178
Oleg Nesterov557c2d82014-10-09 15:25:43 -0700179 /* we do not bother to update m->version in this case */
Oleg Nesterov0c255322014-10-09 15:25:36 -0700180 if (pos == mm->map_count && priv->tail_vma)
181 return priv->tail_vma;
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700182
183 vma_stop(priv);
184 return NULL;
Matt Mackalla6198792008-02-04 22:29:03 -0800185}
186
187static void *m_next(struct seq_file *m, void *v, loff_t *pos)
188{
189 struct proc_maps_private *priv = m->private;
Oleg Nesterovad2a00e2014-10-09 15:25:39 -0700190 struct vm_area_struct *next;
Matt Mackalla6198792008-02-04 22:29:03 -0800191
192 (*pos)++;
Oleg Nesterovad2a00e2014-10-09 15:25:39 -0700193 next = m_next_vma(priv, v);
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700194 if (!next)
195 vma_stop(priv);
196 return next;
Matt Mackalla6198792008-02-04 22:29:03 -0800197}
198
199static void m_stop(struct seq_file *m, void *v)
200{
201 struct proc_maps_private *priv = m->private;
Matt Mackalla6198792008-02-04 22:29:03 -0800202
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700203 if (!IS_ERR_OR_NULL(v))
204 vma_stop(priv);
Oleg Nesterov0d5f5f42014-10-09 15:25:32 -0700205 if (priv->task) {
Matt Mackalla6198792008-02-04 22:29:03 -0800206 put_task_struct(priv->task);
Oleg Nesterov0d5f5f42014-10-09 15:25:32 -0700207 priv->task = NULL;
208 }
Matt Mackalla6198792008-02-04 22:29:03 -0800209}
210
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700211static int proc_maps_open(struct inode *inode, struct file *file,
212 const struct seq_operations *ops, int psize)
213{
214 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
215
216 if (!priv)
217 return -ENOMEM;
218
Oleg Nesterov2c033762014-10-09 15:25:51 -0700219 priv->inode = inode;
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700220 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
221 if (IS_ERR(priv->mm)) {
222 int err = PTR_ERR(priv->mm);
223
224 seq_release_private(inode, file);
225 return err;
226 }
227
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700228 return 0;
229}
230
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700231static int proc_map_release(struct inode *inode, struct file *file)
232{
233 struct seq_file *seq = file->private_data;
234 struct proc_maps_private *priv = seq->private;
235
236 if (priv->mm)
237 mmdrop(priv->mm);
238
239 return seq_release_private(inode, file);
240}
241
Matt Mackalla6198792008-02-04 22:29:03 -0800242static int do_maps_open(struct inode *inode, struct file *file,
Jan Engelhardt03a44822008-02-08 04:21:19 -0800243 const struct seq_operations *ops)
Matt Mackalla6198792008-02-04 22:29:03 -0800244{
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700245 return proc_maps_open(inode, file, ops,
246 sizeof(struct proc_maps_private));
Matt Mackalla6198792008-02-04 22:29:03 -0800247}
Mauricio Line070ad42005-09-03 15:55:10 -0700248
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700249static pid_t pid_of_stack(struct proc_maps_private *priv,
250 struct vm_area_struct *vma, bool is_pid)
251{
252 struct inode *inode = priv->inode;
253 struct task_struct *task;
254 pid_t ret = 0;
255
256 rcu_read_lock();
257 task = pid_task(proc_pid(inode), PIDTYPE_PID);
258 if (task) {
259 task = task_of_stack(task, vma, is_pid);
260 if (task)
261 ret = task_pid_nr_ns(task, inode->i_sb->s_fs_info);
262 }
263 rcu_read_unlock();
264
265 return ret;
266}
267
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700268static void
269show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Mauricio Line070ad42005-09-03 15:55:10 -0700271 struct mm_struct *mm = vma->vm_mm;
272 struct file *file = vma->vm_file;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700273 struct proc_maps_private *priv = m->private;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900274 vm_flags_t flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 unsigned long ino = 0;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700276 unsigned long long pgoff = 0;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200277 unsigned long start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 dev_t dev = 0;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700279 const char *name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (file) {
Al Viro496ad9a2013-01-23 17:07:38 -0500282 struct inode *inode = file_inode(vma->vm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 dev = inode->i_sb->s_dev;
284 ino = inode->i_ino;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700285 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287
Linus Torvaldsd7824372010-08-15 11:35:52 -0700288 /* We don't show the stack guard page in /proc/maps */
289 start = vma->vm_start;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200290 if (stack_guard_page_start(vma, start))
291 start += PAGE_SIZE;
292 end = vma->vm_end;
293 if (stack_guard_page_end(vma, end))
294 end -= PAGE_SIZE;
Linus Torvaldsd7824372010-08-15 11:35:52 -0700295
Tetsuo Handa652586d2013-11-14 14:31:57 -0800296 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
297 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
Linus Torvaldsd7824372010-08-15 11:35:52 -0700298 start,
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200299 end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 flags & VM_READ ? 'r' : '-',
301 flags & VM_WRITE ? 'w' : '-',
302 flags & VM_EXEC ? 'x' : '-',
303 flags & VM_MAYSHARE ? 's' : 'p',
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700304 pgoff,
Tetsuo Handa652586d2013-11-14 14:31:57 -0800305 MAJOR(dev), MINOR(dev), ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 /*
308 * Print the dentry name for named mappings, and a
309 * special [heap] marker for the heap:
310 */
Mauricio Line070ad42005-09-03 15:55:10 -0700311 if (file) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800312 seq_pad(m, ' ');
Miklos Szeredi2726d562015-06-19 10:30:28 +0200313 seq_file_path(m, file, "\n");
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700314 goto done;
315 }
316
Andy Lutomirski78d683e2014-05-19 15:58:32 -0700317 if (vma->vm_ops && vma->vm_ops->name) {
318 name = vma->vm_ops->name(vma);
319 if (name)
320 goto done;
321 }
322
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700323 name = arch_vma_name(vma);
324 if (!name) {
325 pid_t tid;
326
327 if (!mm) {
328 name = "[vdso]";
329 goto done;
330 }
331
332 if (vma->vm_start <= mm->brk &&
333 vma->vm_end >= mm->start_brk) {
334 name = "[heap]";
335 goto done;
336 }
337
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700338 tid = pid_of_stack(priv, vma, is_pid);
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700339 if (tid != 0) {
340 /*
341 * Thread stack in /proc/PID/task/TID/maps or
342 * the main process stack.
343 */
344 if (!is_pid || (vma->vm_start <= mm->start_stack &&
345 vma->vm_end >= mm->start_stack)) {
346 name = "[stack]";
Ingo Molnare6e54942006-06-27 02:53:50 -0700347 } else {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700348 /* Thread stack in /proc/PID/maps */
Tetsuo Handa652586d2013-11-14 14:31:57 -0800349 seq_pad(m, ' ');
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700350 seq_printf(m, "[stack:%d]", tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Ingo Molnare6e54942006-06-27 02:53:50 -0700352 }
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700353 }
354
355done:
356 if (name) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800357 seq_pad(m, ' ');
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700358 seq_puts(m, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 seq_putc(m, '\n');
Joe Korty7c88db02008-10-16 15:27:09 +0400361}
362
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700363static int show_map(struct seq_file *m, void *v, int is_pid)
Joe Korty7c88db02008-10-16 15:27:09 +0400364{
Oleg Nesterovebb6cdd2014-10-09 15:25:34 -0700365 show_map_vma(m, v, is_pid);
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700366 m_cache_vma(m, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368}
369
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700370static int show_pid_map(struct seq_file *m, void *v)
371{
372 return show_map(m, v, 1);
373}
374
375static int show_tid_map(struct seq_file *m, void *v)
376{
377 return show_map(m, v, 0);
378}
379
Jan Engelhardt03a44822008-02-08 04:21:19 -0800380static const struct seq_operations proc_pid_maps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800381 .start = m_start,
382 .next = m_next,
383 .stop = m_stop,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700384 .show = show_pid_map
Matt Mackalla6198792008-02-04 22:29:03 -0800385};
386
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700387static const struct seq_operations proc_tid_maps_op = {
388 .start = m_start,
389 .next = m_next,
390 .stop = m_stop,
391 .show = show_tid_map
392};
393
394static int pid_maps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800395{
396 return do_maps_open(inode, file, &proc_pid_maps_op);
397}
398
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700399static int tid_maps_open(struct inode *inode, struct file *file)
400{
401 return do_maps_open(inode, file, &proc_tid_maps_op);
402}
403
404const struct file_operations proc_pid_maps_operations = {
405 .open = pid_maps_open,
406 .read = seq_read,
407 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700408 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700409};
410
411const struct file_operations proc_tid_maps_operations = {
412 .open = tid_maps_open,
Matt Mackalla6198792008-02-04 22:29:03 -0800413 .read = seq_read,
414 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700415 .release = proc_map_release,
Matt Mackalla6198792008-02-04 22:29:03 -0800416};
417
418/*
419 * Proportional Set Size(PSS): my share of RSS.
420 *
421 * PSS of a process is the count of pages it has in memory, where each
422 * page is divided by the number of processes sharing it. So if a
423 * process has 1000 pages all to itself, and 1000 shared with one other
424 * process, its PSS will be 1500.
425 *
426 * To keep (accumulated) division errors low, we adopt a 64bit
427 * fixed-point pss counter to minimize division errors. So (pss >>
428 * PSS_SHIFT) would be the real byte count.
429 *
430 * A shift of 12 before division means (assuming 4K page size):
431 * - 1M 3-user-pages add up to 8KB errors;
432 * - supports mapcount up to 2^24, or 16M;
433 * - supports PSS up to 2^52 bytes, or 4PB.
434 */
435#define PSS_SHIFT 12
436
Matt Mackall1e883282008-02-04 22:29:07 -0800437#ifdef CONFIG_PROC_PAGE_MONITOR
Peter Zijlstra214e4712008-04-28 02:12:55 -0700438struct mem_size_stats {
Matt Mackalla6198792008-02-04 22:29:03 -0800439 unsigned long resident;
440 unsigned long shared_clean;
441 unsigned long shared_dirty;
442 unsigned long private_clean;
443 unsigned long private_dirty;
444 unsigned long referenced;
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700445 unsigned long anonymous;
Dave Hansen4031a212011-03-22 16:33:01 -0700446 unsigned long anonymous_thp;
Peter Zijlstra214e4712008-04-28 02:12:55 -0700447 unsigned long swap;
Matt Mackalla6198792008-02-04 22:29:03 -0800448 u64 pss;
449};
450
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800451static void smaps_account(struct mem_size_stats *mss, struct page *page,
452 unsigned long size, bool young, bool dirty)
453{
454 int mapcount;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700455
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800456 if (PageAnon(page))
457 mss->anonymous += size;
458
459 mss->resident += size;
460 /* Accumulate the size in pages that have been accessed. */
461 if (young || PageReferenced(page))
462 mss->referenced += size;
463 mapcount = page_mapcount(page);
464 if (mapcount >= 2) {
465 u64 pss_delta;
466
467 if (dirty || PageDirty(page))
468 mss->shared_dirty += size;
469 else
470 mss->shared_clean += size;
471 pss_delta = (u64)size << PSS_SHIFT;
472 do_div(pss_delta, mapcount);
473 mss->pss += pss_delta;
474 } else {
475 if (dirty || PageDirty(page))
476 mss->private_dirty += size;
477 else
478 mss->private_clean += size;
479 mss->pss += (u64)size << PSS_SHIFT;
480 }
481}
482
483static void smaps_pte_entry(pte_t *pte, unsigned long addr,
484 struct mm_walk *walk)
Dave Hansenae11c4d2011-03-22 16:32:58 -0700485{
486 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800487 struct vm_area_struct *vma = walk->vma;
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700488 struct page *page = NULL;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700489
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800490 if (pte_present(*pte)) {
491 page = vm_normal_page(vma, addr, *pte);
492 } else if (is_swap_pte(*pte)) {
493 swp_entry_t swpent = pte_to_swp_entry(*pte);
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700494
495 if (!non_swap_entry(swpent))
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800496 mss->swap += PAGE_SIZE;
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700497 else if (is_migration_entry(swpent))
498 page = migration_entry_to_page(swpent);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700499 }
500
Dave Hansenae11c4d2011-03-22 16:32:58 -0700501 if (!page)
502 return;
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800503 smaps_account(mss, page, PAGE_SIZE, pte_young(*pte), pte_dirty(*pte));
Dave Hansenae11c4d2011-03-22 16:32:58 -0700504}
505
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800506#ifdef CONFIG_TRANSPARENT_HUGEPAGE
507static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
508 struct mm_walk *walk)
509{
510 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800511 struct vm_area_struct *vma = walk->vma;
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800512 struct page *page;
513
514 /* FOLL_DUMP will return -EFAULT on huge zero page */
515 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
516 if (IS_ERR_OR_NULL(page))
517 return;
518 mss->anonymous_thp += HPAGE_PMD_SIZE;
519 smaps_account(mss, page, HPAGE_PMD_SIZE,
520 pmd_young(*pmd), pmd_dirty(*pmd));
521}
522#else
523static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
524 struct mm_walk *walk)
525{
526}
527#endif
528
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800529static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700530 struct mm_walk *walk)
Mauricio Line070ad42005-09-03 15:55:10 -0700531{
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800532 struct vm_area_struct *vma = walk->vma;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700533 pte_t *pte;
Hugh Dickins705e87c2005-10-29 18:16:27 -0700534 spinlock_t *ptl;
Mauricio Line070ad42005-09-03 15:55:10 -0700535
Kirill A. Shutemovbf929152013-11-14 14:30:54 -0800536 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800537 smaps_pmd_entry(pmd, addr, walk);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -0800538 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -0700539 return 0;
Dave Hansen22e057c2011-03-22 16:33:00 -0700540 }
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700541
542 if (pmd_trans_unstable(pmd))
543 return 0;
Dave Hansen22e057c2011-03-22 16:33:00 -0700544 /*
545 * The mmap_sem held all the way back in m_start() is what
546 * keeps khugepaged out of here and from collapsing things
547 * in here.
548 */
Hugh Dickins705e87c2005-10-29 18:16:27 -0700549 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700550 for (; addr != end; pte++, addr += PAGE_SIZE)
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800551 smaps_pte_entry(pte, addr, walk);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700552 pte_unmap_unlock(pte - 1, ptl);
553 cond_resched();
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800554 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700555}
556
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800557static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
558{
559 /*
560 * Don't forget to update Documentation/ on changes.
561 */
562 static const char mnemonics[BITS_PER_LONG][2] = {
563 /*
564 * In case if we meet a flag we don't know about.
565 */
566 [0 ... (BITS_PER_LONG-1)] = "??",
567
568 [ilog2(VM_READ)] = "rd",
569 [ilog2(VM_WRITE)] = "wr",
570 [ilog2(VM_EXEC)] = "ex",
571 [ilog2(VM_SHARED)] = "sh",
572 [ilog2(VM_MAYREAD)] = "mr",
573 [ilog2(VM_MAYWRITE)] = "mw",
574 [ilog2(VM_MAYEXEC)] = "me",
575 [ilog2(VM_MAYSHARE)] = "ms",
576 [ilog2(VM_GROWSDOWN)] = "gd",
577 [ilog2(VM_PFNMAP)] = "pf",
578 [ilog2(VM_DENYWRITE)] = "dw",
Qiaowei Ren4aae7e42014-11-14 07:18:25 -0800579#ifdef CONFIG_X86_INTEL_MPX
580 [ilog2(VM_MPX)] = "mp",
581#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800582 [ilog2(VM_LOCKED)] = "lo",
583 [ilog2(VM_IO)] = "io",
584 [ilog2(VM_SEQ_READ)] = "sr",
585 [ilog2(VM_RAND_READ)] = "rr",
586 [ilog2(VM_DONTCOPY)] = "dc",
587 [ilog2(VM_DONTEXPAND)] = "de",
588 [ilog2(VM_ACCOUNT)] = "ac",
589 [ilog2(VM_NORESERVE)] = "nr",
590 [ilog2(VM_HUGETLB)] = "ht",
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800591 [ilog2(VM_ARCH_1)] = "ar",
592 [ilog2(VM_DONTDUMP)] = "dd",
Naoya Horiguchiec8e41a2013-11-12 15:07:49 -0800593#ifdef CONFIG_MEM_SOFT_DIRTY
594 [ilog2(VM_SOFTDIRTY)] = "sd",
595#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800596 [ilog2(VM_MIXEDMAP)] = "mm",
597 [ilog2(VM_HUGEPAGE)] = "hg",
598 [ilog2(VM_NOHUGEPAGE)] = "nh",
599 [ilog2(VM_MERGEABLE)] = "mg",
Andrea Arcangeli16ba6f82015-09-04 15:46:17 -0700600 [ilog2(VM_UFFD_MISSING)]= "um",
601 [ilog2(VM_UFFD_WP)] = "uw",
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800602 };
603 size_t i;
604
605 seq_puts(m, "VmFlags: ");
606 for (i = 0; i < BITS_PER_LONG; i++) {
607 if (vma->vm_flags & (1UL << i)) {
608 seq_printf(m, "%c%c ",
609 mnemonics[i][0], mnemonics[i][1]);
610 }
611 }
612 seq_putc(m, '\n');
613}
614
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700615static int show_smap(struct seq_file *m, void *v, int is_pid)
Mauricio Line070ad42005-09-03 15:55:10 -0700616{
617 struct vm_area_struct *vma = v;
Mauricio Line070ad42005-09-03 15:55:10 -0700618 struct mem_size_stats mss;
Dave Hansen21650092008-06-12 15:21:47 -0700619 struct mm_walk smaps_walk = {
620 .pmd_entry = smaps_pte_range,
621 .mm = vma->vm_mm,
622 .private = &mss,
623 };
Mauricio Line070ad42005-09-03 15:55:10 -0700624
625 memset(&mss, 0, sizeof mss);
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +0900626 /* mmap_sem is held in m_start */
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800627 walk_page_vma(vma, &smaps_walk);
Matt Mackall4752c362008-02-04 22:29:02 -0800628
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700629 show_map_vma(m, vma, is_pid);
Matt Mackall4752c362008-02-04 22:29:02 -0800630
631 seq_printf(m,
632 "Size: %8lu kB\n"
633 "Rss: %8lu kB\n"
634 "Pss: %8lu kB\n"
635 "Shared_Clean: %8lu kB\n"
636 "Shared_Dirty: %8lu kB\n"
637 "Private_Clean: %8lu kB\n"
638 "Private_Dirty: %8lu kB\n"
Peter Zijlstra214e4712008-04-28 02:12:55 -0700639 "Referenced: %8lu kB\n"
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700640 "Anonymous: %8lu kB\n"
Dave Hansen4031a212011-03-22 16:33:01 -0700641 "AnonHugePages: %8lu kB\n"
Mel Gorman08fba692009-01-06 14:38:53 -0800642 "Swap: %8lu kB\n"
Mel Gorman33402892009-01-06 14:38:54 -0800643 "KernelPageSize: %8lu kB\n"
Nikanth Karthikesan2d905082011-01-13 15:45:53 -0800644 "MMUPageSize: %8lu kB\n"
645 "Locked: %8lu kB\n",
Matt Mackall4752c362008-02-04 22:29:02 -0800646 (vma->vm_end - vma->vm_start) >> 10,
647 mss.resident >> 10,
648 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
649 mss.shared_clean >> 10,
650 mss.shared_dirty >> 10,
651 mss.private_clean >> 10,
652 mss.private_dirty >> 10,
Peter Zijlstra214e4712008-04-28 02:12:55 -0700653 mss.referenced >> 10,
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700654 mss.anonymous >> 10,
Dave Hansen4031a212011-03-22 16:33:01 -0700655 mss.anonymous_thp >> 10,
Mel Gorman08fba692009-01-06 14:38:53 -0800656 mss.swap >> 10,
Mel Gorman33402892009-01-06 14:38:54 -0800657 vma_kernel_pagesize(vma) >> 10,
Nikanth Karthikesan2d905082011-01-13 15:45:53 -0800658 vma_mmu_pagesize(vma) >> 10,
659 (vma->vm_flags & VM_LOCKED) ?
660 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
Matt Mackall4752c362008-02-04 22:29:02 -0800661
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800662 show_smap_vma_flags(m, vma);
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700663 m_cache_vma(m, vma);
Joe Korty7c88db02008-10-16 15:27:09 +0400664 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700665}
666
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700667static int show_pid_smap(struct seq_file *m, void *v)
668{
669 return show_smap(m, v, 1);
670}
671
672static int show_tid_smap(struct seq_file *m, void *v)
673{
674 return show_smap(m, v, 0);
675}
676
Jan Engelhardt03a44822008-02-08 04:21:19 -0800677static const struct seq_operations proc_pid_smaps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800678 .start = m_start,
679 .next = m_next,
680 .stop = m_stop,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700681 .show = show_pid_smap
Matt Mackalla6198792008-02-04 22:29:03 -0800682};
683
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700684static const struct seq_operations proc_tid_smaps_op = {
685 .start = m_start,
686 .next = m_next,
687 .stop = m_stop,
688 .show = show_tid_smap
689};
690
691static int pid_smaps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800692{
693 return do_maps_open(inode, file, &proc_pid_smaps_op);
694}
695
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700696static int tid_smaps_open(struct inode *inode, struct file *file)
697{
698 return do_maps_open(inode, file, &proc_tid_smaps_op);
699}
700
701const struct file_operations proc_pid_smaps_operations = {
702 .open = pid_smaps_open,
703 .read = seq_read,
704 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700705 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700706};
707
708const struct file_operations proc_tid_smaps_operations = {
709 .open = tid_smaps_open,
Matt Mackalla6198792008-02-04 22:29:03 -0800710 .read = seq_read,
711 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700712 .release = proc_map_release,
Matt Mackalla6198792008-02-04 22:29:03 -0800713};
714
Pavel Emelyanov541c2372013-07-03 15:01:22 -0700715/*
716 * We do not want to have constant page-shift bits sitting in
717 * pagemap entries and are about to reuse them some time soon.
718 *
719 * Here's the "migration strategy":
720 * 1. when the system boots these bits remain what they are,
721 * but a warning about future change is printed in log;
722 * 2. once anyone clears soft-dirty bits via clear_refs file,
723 * these flag is set to denote, that user is aware of the
724 * new API and those page-shift bits change their meaning.
725 * The respective warning is printed in dmesg;
726 * 3. In a couple of releases we will remove all the mentions
727 * of page-shift in pagemap entries.
728 */
729
730static bool soft_dirty_cleared __read_mostly;
731
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700732enum clear_refs_types {
733 CLEAR_REFS_ALL = 1,
734 CLEAR_REFS_ANON,
735 CLEAR_REFS_MAPPED,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700736 CLEAR_REFS_SOFT_DIRTY,
Petr Cermak695f0552015-02-12 15:01:00 -0800737 CLEAR_REFS_MM_HIWATER_RSS,
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700738 CLEAR_REFS_LAST,
739};
740
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700741struct clear_refs_private {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700742 enum clear_refs_types type;
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700743};
744
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800745#ifdef CONFIG_MEM_SOFT_DIRTY
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700746static inline void clear_soft_dirty(struct vm_area_struct *vma,
747 unsigned long addr, pte_t *pte)
748{
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700749 /*
750 * The soft-dirty tracker uses #PF-s to catch writes
751 * to pages, so write-protect the pte as well. See the
752 * Documentation/vm/soft-dirty.txt for full description
753 * of how soft-dirty works.
754 */
755 pte_t ptent = *pte;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700756
757 if (pte_present(ptent)) {
758 ptent = pte_wrprotect(ptent);
759 ptent = pte_clear_flags(ptent, _PAGE_SOFT_DIRTY);
760 } else if (is_swap_pte(ptent)) {
761 ptent = pte_swp_clear_soft_dirty(ptent);
762 }
763
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700764 set_pte_at(vma->vm_mm, addr, pte, ptent);
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700765}
766
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800767static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
768 unsigned long addr, pmd_t *pmdp)
769{
770 pmd_t pmd = *pmdp;
771
772 pmd = pmd_wrprotect(pmd);
773 pmd = pmd_clear_flags(pmd, _PAGE_SOFT_DIRTY);
774
775 if (vma->vm_flags & VM_SOFTDIRTY)
776 vma->vm_flags &= ~VM_SOFTDIRTY;
777
778 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
779}
780
781#else
782
783static inline void clear_soft_dirty(struct vm_area_struct *vma,
784 unsigned long addr, pte_t *pte)
785{
786}
787
788static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
789 unsigned long addr, pmd_t *pmdp)
790{
791}
792#endif
793
Matt Mackalla6198792008-02-04 22:29:03 -0800794static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
Dave Hansen21650092008-06-12 15:21:47 -0700795 unsigned long end, struct mm_walk *walk)
Matt Mackalla6198792008-02-04 22:29:03 -0800796{
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700797 struct clear_refs_private *cp = walk->private;
Naoya Horiguchi5c64f522015-02-11 15:27:46 -0800798 struct vm_area_struct *vma = walk->vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800799 pte_t *pte, ptent;
800 spinlock_t *ptl;
801 struct page *page;
802
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -0800803 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
804 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
805 clear_soft_dirty_pmd(vma, addr, pmd);
806 goto out;
807 }
808
809 page = pmd_page(*pmd);
810
811 /* Clear accessed and referenced bits. */
812 pmdp_test_and_clear_young(vma, addr, pmd);
813 ClearPageReferenced(page);
814out:
815 spin_unlock(ptl);
816 return 0;
817 }
818
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700819 if (pmd_trans_unstable(pmd))
820 return 0;
Dave Hansen03319322011-03-22 16:32:56 -0700821
Matt Mackalla6198792008-02-04 22:29:03 -0800822 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
823 for (; addr != end; pte++, addr += PAGE_SIZE) {
824 ptent = *pte;
Matt Mackalla6198792008-02-04 22:29:03 -0800825
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700826 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
827 clear_soft_dirty(vma, addr, pte);
828 continue;
829 }
830
Cyrill Gorcunov179ef712013-08-13 16:00:49 -0700831 if (!pte_present(ptent))
832 continue;
833
Matt Mackalla6198792008-02-04 22:29:03 -0800834 page = vm_normal_page(vma, addr, ptent);
835 if (!page)
836 continue;
837
838 /* Clear accessed and referenced bits. */
839 ptep_test_and_clear_young(vma, addr, pte);
840 ClearPageReferenced(page);
841 }
842 pte_unmap_unlock(pte - 1, ptl);
843 cond_resched();
844 return 0;
845}
846
Naoya Horiguchi5c64f522015-02-11 15:27:46 -0800847static int clear_refs_test_walk(unsigned long start, unsigned long end,
848 struct mm_walk *walk)
849{
850 struct clear_refs_private *cp = walk->private;
851 struct vm_area_struct *vma = walk->vma;
852
Naoya Horiguchi48684a62015-02-11 15:28:06 -0800853 if (vma->vm_flags & VM_PFNMAP)
854 return 1;
855
Naoya Horiguchi5c64f522015-02-11 15:27:46 -0800856 /*
857 * Writing 1 to /proc/pid/clear_refs affects all pages.
858 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
859 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
860 * Writing 4 to /proc/pid/clear_refs affects all pages.
861 */
862 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
863 return 1;
864 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
865 return 1;
866 return 0;
867}
868
Matt Mackallf248dcb2008-02-04 22:29:03 -0800869static ssize_t clear_refs_write(struct file *file, const char __user *buf,
870 size_t count, loff_t *ppos)
David Rientjesb813e932007-05-06 14:49:24 -0700871{
Matt Mackallf248dcb2008-02-04 22:29:03 -0800872 struct task_struct *task;
Vincent Lifb92a4b2009-09-22 16:45:36 -0700873 char buffer[PROC_NUMBUF];
Matt Mackallf248dcb2008-02-04 22:29:03 -0800874 struct mm_struct *mm;
David Rientjesb813e932007-05-06 14:49:24 -0700875 struct vm_area_struct *vma;
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700876 enum clear_refs_types type;
877 int itype;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700878 int rv;
David Rientjesb813e932007-05-06 14:49:24 -0700879
Matt Mackallf248dcb2008-02-04 22:29:03 -0800880 memset(buffer, 0, sizeof(buffer));
881 if (count > sizeof(buffer) - 1)
882 count = sizeof(buffer) - 1;
883 if (copy_from_user(buffer, buf, count))
884 return -EFAULT;
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700885 rv = kstrtoint(strstrip(buffer), 10, &itype);
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700886 if (rv < 0)
887 return rv;
Pavel Emelyanov040fa022013-07-03 15:01:16 -0700888 type = (enum clear_refs_types)itype;
889 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
Matt Mackallf248dcb2008-02-04 22:29:03 -0800890 return -EINVAL;
Pavel Emelyanov541c2372013-07-03 15:01:22 -0700891
892 if (type == CLEAR_REFS_SOFT_DIRTY) {
893 soft_dirty_cleared = true;
Cyrill Gorcunovc86c97f2014-06-04 16:06:43 -0700894 pr_warn_once("The pagemap bits 55-60 has changed their meaning!"
895 " See the linux/Documentation/vm/pagemap.txt for "
896 "details.\n");
Pavel Emelyanov541c2372013-07-03 15:01:22 -0700897 }
898
Al Viro496ad9a2013-01-23 17:07:38 -0500899 task = get_proc_task(file_inode(file));
Matt Mackallf248dcb2008-02-04 22:29:03 -0800900 if (!task)
901 return -ESRCH;
902 mm = get_task_mm(task);
903 if (mm) {
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700904 struct clear_refs_private cp = {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700905 .type = type,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700906 };
Andrew Morton20cbc972008-07-05 12:29:05 -0700907 struct mm_walk clear_refs_walk = {
908 .pmd_entry = clear_refs_pte_range,
Naoya Horiguchi5c64f522015-02-11 15:27:46 -0800909 .test_walk = clear_refs_test_walk,
Andrew Morton20cbc972008-07-05 12:29:05 -0700910 .mm = mm,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -0700911 .private = &cp,
Andrew Morton20cbc972008-07-05 12:29:05 -0700912 };
Petr Cermak695f0552015-02-12 15:01:00 -0800913
914 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
915 /*
916 * Writing 5 to /proc/pid/clear_refs resets the peak
917 * resident set size to this mm's current rss value.
918 */
919 down_write(&mm->mmap_sem);
920 reset_mm_hiwater_rss(mm);
921 up_write(&mm->mmap_sem);
922 goto out_mm;
923 }
924
Matt Mackallf248dcb2008-02-04 22:29:03 -0800925 down_read(&mm->mmap_sem);
Peter Feiner64e455072014-10-13 15:55:46 -0700926 if (type == CLEAR_REFS_SOFT_DIRTY) {
927 for (vma = mm->mmap; vma; vma = vma->vm_next) {
928 if (!(vma->vm_flags & VM_SOFTDIRTY))
929 continue;
930 up_read(&mm->mmap_sem);
931 down_write(&mm->mmap_sem);
932 for (vma = mm->mmap; vma; vma = vma->vm_next) {
933 vma->vm_flags &= ~VM_SOFTDIRTY;
934 vma_set_page_prot(vma);
935 }
936 downgrade_write(&mm->mmap_sem);
937 break;
938 }
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700939 mmu_notifier_invalidate_range_start(mm, 0, -1);
Peter Feiner64e455072014-10-13 15:55:46 -0700940 }
Naoya Horiguchi5c64f522015-02-11 15:27:46 -0800941 walk_page_range(0, ~0UL, &clear_refs_walk);
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700942 if (type == CLEAR_REFS_SOFT_DIRTY)
943 mmu_notifier_invalidate_range_end(mm, 0, -1);
Matt Mackallf248dcb2008-02-04 22:29:03 -0800944 flush_tlb_mm(mm);
945 up_read(&mm->mmap_sem);
Petr Cermak695f0552015-02-12 15:01:00 -0800946out_mm:
Matt Mackallf248dcb2008-02-04 22:29:03 -0800947 mmput(mm);
948 }
949 put_task_struct(task);
Vincent Lifb92a4b2009-09-22 16:45:36 -0700950
951 return count;
David Rientjesb813e932007-05-06 14:49:24 -0700952}
953
Matt Mackallf248dcb2008-02-04 22:29:03 -0800954const struct file_operations proc_clear_refs_operations = {
955 .write = clear_refs_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200956 .llseek = noop_llseek,
Matt Mackallf248dcb2008-02-04 22:29:03 -0800957};
958
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700959typedef struct {
960 u64 pme;
961} pagemap_entry_t;
962
Matt Mackall85863e42008-02-04 22:29:04 -0800963struct pagemapread {
yonghua zheng8c829622013-08-13 16:01:03 -0700964 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700965 pagemap_entry_t *buffer;
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700966 bool v2;
Matt Mackall85863e42008-02-04 22:29:04 -0800967};
968
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -0700969#define PAGEMAP_WALK_SIZE (PMD_SIZE)
970#define PAGEMAP_WALK_MASK (PMD_MASK)
971
yonghua zheng8c829622013-08-13 16:01:03 -0700972#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -0500973#define PM_STATUS_BITS 3
974#define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
975#define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
976#define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
977#define PM_PSHIFT_BITS 6
978#define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
979#define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700980#define __PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -0500981#define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
982#define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700983/* in "new" pagemap pshift bits are occupied with more status bits */
984#define PM_STATUS2(v2, x) (__PM_PSHIFT(v2 ? x : PAGE_SHIFT))
Hans Rosenfeldf16278c2008-03-21 18:46:59 -0500985
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700986#define __PM_SOFT_DIRTY (1LL)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -0500987#define PM_PRESENT PM_STATUS(4LL)
988#define PM_SWAP PM_STATUS(2LL)
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -0700989#define PM_FILE PM_STATUS(1LL)
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -0700990#define PM_NOT_PRESENT(v2) PM_STATUS2(v2, 0)
Matt Mackall85863e42008-02-04 22:29:04 -0800991#define PM_END_OF_BUFFER 1
992
Naoya Horiguchi092b50b2012-03-21 16:33:59 -0700993static inline pagemap_entry_t make_pme(u64 val)
994{
995 return (pagemap_entry_t) { .pme = val };
996}
997
998static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
Matt Mackall85863e42008-02-04 22:29:04 -0800999 struct pagemapread *pm)
1000{
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001001 pm->buffer[pm->pos++] = *pme;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001002 if (pm->pos >= pm->len)
Thomas Tuttleaae86792008-06-05 22:46:31 -07001003 return PM_END_OF_BUFFER;
Matt Mackall85863e42008-02-04 22:29:04 -08001004 return 0;
1005}
1006
1007static int pagemap_pte_hole(unsigned long start, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -07001008 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001009{
Dave Hansen21650092008-06-12 15:21:47 -07001010 struct pagemapread *pm = walk->private;
Peter Feiner68b5a652014-08-06 16:08:09 -07001011 unsigned long addr = start;
Matt Mackall85863e42008-02-04 22:29:04 -08001012 int err = 0;
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001013
Peter Feiner68b5a652014-08-06 16:08:09 -07001014 while (addr < end) {
1015 struct vm_area_struct *vma = find_vma(walk->mm, addr);
1016 pagemap_entry_t pme = make_pme(PM_NOT_PRESENT(pm->v2));
Peter Feiner87e6d492014-09-25 16:05:18 -07001017 /* End of address space hole, which we mark as non-present. */
1018 unsigned long hole_end;
Peter Feiner68b5a652014-08-06 16:08:09 -07001019
Peter Feiner87e6d492014-09-25 16:05:18 -07001020 if (vma)
1021 hole_end = min(end, vma->vm_start);
1022 else
1023 hole_end = end;
1024
1025 for (; addr < hole_end; addr += PAGE_SIZE) {
1026 err = add_to_pagemap(addr, &pme, pm);
1027 if (err)
1028 goto out;
Peter Feiner68b5a652014-08-06 16:08:09 -07001029 }
1030
Peter Feiner87e6d492014-09-25 16:05:18 -07001031 if (!vma)
1032 break;
1033
1034 /* Addresses in the VMA. */
1035 if (vma->vm_flags & VM_SOFTDIRTY)
1036 pme.pme |= PM_STATUS2(pm->v2, __PM_SOFT_DIRTY);
1037 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
Peter Feiner68b5a652014-08-06 16:08:09 -07001038 err = add_to_pagemap(addr, &pme, pm);
1039 if (err)
1040 goto out;
1041 }
Matt Mackall85863e42008-02-04 22:29:04 -08001042 }
Peter Feiner68b5a652014-08-06 16:08:09 -07001043out:
Matt Mackall85863e42008-02-04 22:29:04 -08001044 return err;
1045}
1046
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -07001047static void pte_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001048 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
Matt Mackall85863e42008-02-04 22:29:04 -08001049{
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001050 u64 frame, flags;
1051 struct page *page = NULL;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001052 int flags2 = 0;
Matt Mackall85863e42008-02-04 22:29:04 -08001053
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001054 if (pte_present(pte)) {
1055 frame = pte_pfn(pte);
1056 flags = PM_PRESENT;
1057 page = vm_normal_page(vma, addr, pte);
Cyrill Gorcunove9cdd6e2013-10-16 13:46:53 -07001058 if (pte_soft_dirty(pte))
1059 flags2 |= __PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001060 } else if (is_swap_pte(pte)) {
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001061 swp_entry_t entry;
1062 if (pte_swp_soft_dirty(pte))
1063 flags2 |= __PM_SOFT_DIRTY;
1064 entry = pte_to_swp_entry(pte);
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001065 frame = swp_type(entry) |
1066 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
1067 flags = PM_SWAP;
1068 if (is_migration_entry(entry))
1069 page = migration_entry_to_page(entry);
1070 } else {
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001071 if (vma->vm_flags & VM_SOFTDIRTY)
1072 flags2 |= __PM_SOFT_DIRTY;
1073 *pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, flags2));
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001074 return;
1075 }
1076
1077 if (page && !PageAnon(page))
1078 flags |= PM_FILE;
Cyrill Gorcunove9cdd6e2013-10-16 13:46:53 -07001079 if ((vma->vm_flags & VM_SOFTDIRTY))
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001080 flags2 |= __PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001081
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001082 *pme = make_pme(PM_PFRAME(frame) | PM_STATUS2(pm->v2, flags2) | flags);
Dave Hansenbcf80392008-06-12 15:21:48 -07001083}
1084
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001085#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -07001086static void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001087 pmd_t pmd, int offset, int pmd_flags2)
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001088{
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001089 /*
1090 * Currently pmd for thp is always present because thp can not be
1091 * swapped-out, migrated, or HWPOISONed (split in such cases instead.)
1092 * This if-check is just to prepare for future implementation.
1093 */
1094 if (pmd_present(pmd))
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001095 *pme = make_pme(PM_PFRAME(pmd_pfn(pmd) + offset)
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001096 | PM_STATUS2(pm->v2, pmd_flags2) | PM_PRESENT);
Konstantin Khlebnikov16fbdce2012-05-10 13:01:43 -07001097 else
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001098 *pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, pmd_flags2));
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001099}
1100#else
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -07001101static inline void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001102 pmd_t pmd, int offset, int pmd_flags2)
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001103{
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001104}
1105#endif
1106
Matt Mackall85863e42008-02-04 22:29:04 -08001107static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -07001108 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001109{
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001110 struct vm_area_struct *vma = walk->vma;
Dave Hansen21650092008-06-12 15:21:47 -07001111 struct pagemapread *pm = walk->private;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001112 spinlock_t *ptl;
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001113 pte_t *pte, *orig_pte;
Matt Mackall85863e42008-02-04 22:29:04 -08001114 int err = 0;
1115
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001116 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001117 int pmd_flags2;
1118
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001119 if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(*pmd))
1120 pmd_flags2 = __PM_SOFT_DIRTY;
1121 else
1122 pmd_flags2 = 0;
1123
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001124 for (; addr != end; addr += PAGE_SIZE) {
1125 unsigned long offset;
Peter Feiner81d0fa62014-10-09 15:28:32 -07001126 pagemap_entry_t pme;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001127
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001128 offset = (addr & ~PAGEMAP_WALK_MASK) >>
1129 PAGE_SHIFT;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001130 thp_pmd_to_pagemap_entry(&pme, pm, *pmd, offset, pmd_flags2);
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001131 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001132 if (err)
1133 break;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001134 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001135 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001136 return err;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001137 }
1138
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07001139 if (pmd_trans_unstable(pmd))
1140 return 0;
Dave Hansenbcf80392008-06-12 15:21:48 -07001141
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001142 /*
1143 * We can assume that @vma always points to a valid one and @end never
1144 * goes beyond vma->vm_end.
1145 */
1146 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1147 for (; addr < end; pte++, addr += PAGE_SIZE) {
1148 pagemap_entry_t pme;
Peter Feiner81d0fa62014-10-09 15:28:32 -07001149
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001150 pte_to_pagemap_entry(&pme, pm, vma, addr, *pte);
1151 err = add_to_pagemap(addr, &pme, pm);
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001152 if (err)
Peter Feiner81d0fa62014-10-09 15:28:32 -07001153 break;
Matt Mackall85863e42008-02-04 22:29:04 -08001154 }
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001155 pte_unmap_unlock(orig_pte, ptl);
Matt Mackall85863e42008-02-04 22:29:04 -08001156
1157 cond_resched();
1158
1159 return err;
1160}
1161
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001162#ifdef CONFIG_HUGETLB_PAGE
Pavel Emelyanov2b0a9f02013-07-03 15:01:19 -07001163static void huge_pte_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001164 pte_t pte, int offset, int flags2)
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001165{
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001166 if (pte_present(pte))
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001167 *pme = make_pme(PM_PFRAME(pte_pfn(pte) + offset) |
1168 PM_STATUS2(pm->v2, flags2) |
1169 PM_PRESENT);
Konstantin Khlebnikov16fbdce2012-05-10 13:01:43 -07001170 else
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001171 *pme = make_pme(PM_NOT_PRESENT(pm->v2) |
1172 PM_STATUS2(pm->v2, flags2));
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001173}
1174
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001175/* This function walks within one hugetlb entry in the single call */
1176static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
1177 unsigned long addr, unsigned long end,
1178 struct mm_walk *walk)
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001179{
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001180 struct pagemapread *pm = walk->private;
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001181 struct vm_area_struct *vma = walk->vma;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001182 int err = 0;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001183 int flags2;
Konstantin Khlebnikov16fbdce2012-05-10 13:01:43 -07001184 pagemap_entry_t pme;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001185
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001186 if (vma->vm_flags & VM_SOFTDIRTY)
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001187 flags2 = __PM_SOFT_DIRTY;
1188 else
1189 flags2 = 0;
1190
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001191 for (; addr != end; addr += PAGE_SIZE) {
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001192 int offset = (addr & ~hmask) >> PAGE_SHIFT;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001193 huge_pte_to_pagemap_entry(&pme, pm, *pte, offset, flags2);
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001194 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001195 if (err)
1196 return err;
1197 }
1198
1199 cond_resched();
1200
1201 return err;
1202}
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001203#endif /* HUGETLB_PAGE */
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001204
Matt Mackall85863e42008-02-04 22:29:04 -08001205/*
1206 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1207 *
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001208 * For each page in the address space, this file contains one 64-bit entry
1209 * consisting of the following:
1210 *
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001211 * Bits 0-54 page frame number (PFN) if present
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001212 * Bits 0-4 swap type if swapped
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001213 * Bits 5-54 swap offset if swapped
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001214 * Bits 55-60 page shift (page size = 1<<page shift)
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001215 * Bit 61 page is file-page or shared-anon
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001216 * Bit 62 page swapped
1217 * Bit 63 page present
1218 *
1219 * If the page is not present but in swap, then the PFN contains an
1220 * encoding of the swap file number and the page's offset into the
1221 * swap. Unmapped pages return a null PFN. This allows determining
Matt Mackall85863e42008-02-04 22:29:04 -08001222 * precisely which pages are mapped (or in swap) and comparing mapped
1223 * pages between processes.
1224 *
1225 * Efficient users of this interface will use /proc/pid/maps to
1226 * determine which areas of memory are actually mapped and llseek to
1227 * skip over unmapped regions.
1228 */
1229static ssize_t pagemap_read(struct file *file, char __user *buf,
1230 size_t count, loff_t *ppos)
1231{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001232 struct mm_struct *mm = file->private_data;
Matt Mackall85863e42008-02-04 22:29:04 -08001233 struct pagemapread pm;
Alexey Dobriyanee1e6ab2008-07-21 14:21:36 -07001234 struct mm_walk pagemap_walk = {};
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001235 unsigned long src;
1236 unsigned long svpfn;
1237 unsigned long start_vaddr;
1238 unsigned long end_vaddr;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001239 int ret = 0, copied = 0;
Matt Mackall85863e42008-02-04 22:29:04 -08001240
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001241 if (!mm || !atomic_inc_not_zero(&mm->mm_users))
Matt Mackall85863e42008-02-04 22:29:04 -08001242 goto out;
1243
Matt Mackall85863e42008-02-04 22:29:04 -08001244 ret = -EINVAL;
1245 /* file position must be aligned */
Thomas Tuttleaae86792008-06-05 22:46:31 -07001246 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001247 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001248
1249 ret = 0;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001250 if (!count)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001251 goto out_mm;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001252
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001253 pm.v2 = soft_dirty_cleared;
yonghua zheng8c829622013-08-13 16:01:03 -07001254 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1255 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001256 ret = -ENOMEM;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001257 if (!pm.buffer)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001258 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001259
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001260 pagemap_walk.pmd_entry = pagemap_pte_range;
1261 pagemap_walk.pte_hole = pagemap_pte_hole;
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001262#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001263 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001264#endif
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001265 pagemap_walk.mm = mm;
1266 pagemap_walk.private = &pm;
Matt Mackall85863e42008-02-04 22:29:04 -08001267
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001268 src = *ppos;
1269 svpfn = src / PM_ENTRY_BYTES;
1270 start_vaddr = svpfn << PAGE_SHIFT;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001271 end_vaddr = mm->task_size;
Matt Mackall85863e42008-02-04 22:29:04 -08001272
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001273 /* watch out for wraparound */
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001274 if (svpfn > mm->task_size >> PAGE_SHIFT)
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001275 start_vaddr = end_vaddr;
1276
1277 /*
1278 * The odds are that this will stop walking way
1279 * before end_vaddr, because the length of the
1280 * user buffer is tracked in "pm", and the walk
1281 * will stop when we hit the end of the buffer.
1282 */
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001283 ret = 0;
1284 while (count && (start_vaddr < end_vaddr)) {
1285 int len;
1286 unsigned long end;
Matt Mackall85863e42008-02-04 22:29:04 -08001287
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001288 pm.pos = 0;
Naoya Horiguchiea251c12010-11-24 12:57:13 -08001289 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001290 /* overflow ? */
1291 if (end < start_vaddr || end > end_vaddr)
1292 end = end_vaddr;
1293 down_read(&mm->mmap_sem);
1294 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1295 up_read(&mm->mmap_sem);
1296 start_vaddr = end;
1297
1298 len = min(count, PM_ENTRY_BYTES * pm.pos);
Dan Carpenter309361e02010-04-06 13:45:39 +03001299 if (copy_to_user(buf, pm.buffer, len)) {
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001300 ret = -EFAULT;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001301 goto out_free;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001302 }
1303 copied += len;
1304 buf += len;
1305 count -= len;
Matt Mackall85863e42008-02-04 22:29:04 -08001306 }
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001307 *ppos += copied;
1308 if (!ret || ret == PM_END_OF_BUFFER)
1309 ret = copied;
1310
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001311out_free:
1312 kfree(pm.buffer);
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001313out_mm:
1314 mmput(mm);
Matt Mackall85863e42008-02-04 22:29:04 -08001315out:
1316 return ret;
1317}
1318
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001319static int pagemap_open(struct inode *inode, struct file *file)
1320{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001321 struct mm_struct *mm;
1322
Kirill A. Shutemovab676b72015-03-09 23:11:12 +02001323 /* do not disclose physical addresses: attack vector */
1324 if (!capable(CAP_SYS_ADMIN))
1325 return -EPERM;
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001326 pr_warn_once("Bits 55-60 of /proc/PID/pagemap entries are about "
1327 "to stop being page-shift some time soon. See the "
1328 "linux/Documentation/vm/pagemap.txt for details.\n");
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001329
1330 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1331 if (IS_ERR(mm))
1332 return PTR_ERR(mm);
1333 file->private_data = mm;
1334 return 0;
1335}
1336
1337static int pagemap_release(struct inode *inode, struct file *file)
1338{
1339 struct mm_struct *mm = file->private_data;
1340
1341 if (mm)
1342 mmdrop(mm);
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001343 return 0;
1344}
1345
Matt Mackall85863e42008-02-04 22:29:04 -08001346const struct file_operations proc_pagemap_operations = {
1347 .llseek = mem_lseek, /* borrow this */
1348 .read = pagemap_read,
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001349 .open = pagemap_open,
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001350 .release = pagemap_release,
Matt Mackall85863e42008-02-04 22:29:04 -08001351};
Matt Mackall1e883282008-02-04 22:29:07 -08001352#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall85863e42008-02-04 22:29:04 -08001353
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001354#ifdef CONFIG_NUMA
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001355
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001356struct numa_maps {
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001357 unsigned long pages;
1358 unsigned long anon;
1359 unsigned long active;
1360 unsigned long writeback;
1361 unsigned long mapcount_max;
1362 unsigned long dirty;
1363 unsigned long swapcache;
1364 unsigned long node[MAX_NUMNODES];
1365};
1366
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001367struct numa_maps_private {
1368 struct proc_maps_private proc_maps;
1369 struct numa_maps md;
1370};
1371
Dave Hanseneb4866d2011-09-20 15:19:38 -07001372static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1373 unsigned long nr_pages)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001374{
1375 int count = page_mapcount(page);
1376
Dave Hanseneb4866d2011-09-20 15:19:38 -07001377 md->pages += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001378 if (pte_dirty || PageDirty(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001379 md->dirty += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001380
1381 if (PageSwapCache(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001382 md->swapcache += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001383
1384 if (PageActive(page) || PageUnevictable(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001385 md->active += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001386
1387 if (PageWriteback(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001388 md->writeback += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001389
1390 if (PageAnon(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001391 md->anon += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001392
1393 if (count > md->mapcount_max)
1394 md->mapcount_max = count;
1395
Dave Hanseneb4866d2011-09-20 15:19:38 -07001396 md->node[page_to_nid(page)] += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001397}
1398
Dave Hansen3200a8a2011-09-20 15:19:39 -07001399static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1400 unsigned long addr)
1401{
1402 struct page *page;
1403 int nid;
1404
1405 if (!pte_present(pte))
1406 return NULL;
1407
1408 page = vm_normal_page(vma, addr, pte);
1409 if (!page)
1410 return NULL;
1411
1412 if (PageReserved(page))
1413 return NULL;
1414
1415 nid = page_to_nid(page);
Lai Jiangshan4ff1b2c2012-12-12 13:51:25 -08001416 if (!node_isset(nid, node_states[N_MEMORY]))
Dave Hansen3200a8a2011-09-20 15:19:39 -07001417 return NULL;
1418
1419 return page;
1420}
1421
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001422static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1423 unsigned long end, struct mm_walk *walk)
1424{
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001425 struct numa_maps *md = walk->private;
1426 struct vm_area_struct *vma = walk->vma;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001427 spinlock_t *ptl;
1428 pte_t *orig_pte;
1429 pte_t *pte;
1430
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001431 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001432 pte_t huge_pte = *(pte_t *)pmd;
1433 struct page *page;
1434
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001435 page = can_gather_numa_stats(huge_pte, vma, addr);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001436 if (page)
1437 gather_stats(page, md, pte_dirty(huge_pte),
1438 HPAGE_PMD_SIZE/PAGE_SIZE);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001439 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001440 return 0;
Dave Hansen32ef4382011-09-20 15:19:41 -07001441 }
1442
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001443 if (pmd_trans_unstable(pmd))
1444 return 0;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001445 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1446 do {
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001447 struct page *page = can_gather_numa_stats(*pte, vma, addr);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001448 if (!page)
1449 continue;
Dave Hanseneb4866d2011-09-20 15:19:38 -07001450 gather_stats(page, md, pte_dirty(*pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001451
1452 } while (pte++, addr += PAGE_SIZE, addr != end);
1453 pte_unmap_unlock(orig_pte, ptl);
1454 return 0;
1455}
1456#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001457static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001458 unsigned long addr, unsigned long end, struct mm_walk *walk)
1459{
1460 struct numa_maps *md;
1461 struct page *page;
1462
Naoya Horiguchid4c54912014-06-06 10:00:01 -04001463 if (!pte_present(*pte))
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001464 return 0;
1465
1466 page = pte_page(*pte);
1467 if (!page)
1468 return 0;
1469
1470 md = walk->private;
Dave Hanseneb4866d2011-09-20 15:19:38 -07001471 gather_stats(page, md, pte_dirty(*pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001472 return 0;
1473}
1474
1475#else
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001476static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001477 unsigned long addr, unsigned long end, struct mm_walk *walk)
1478{
1479 return 0;
1480}
1481#endif
1482
1483/*
1484 * Display pages allocated per node and memory policy via /proc.
1485 */
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001486static int show_numa_map(struct seq_file *m, void *v, int is_pid)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001487{
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001488 struct numa_maps_private *numa_priv = m->private;
1489 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001490 struct vm_area_struct *vma = v;
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001491 struct numa_maps *md = &numa_priv->md;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001492 struct file *file = vma->vm_file;
1493 struct mm_struct *mm = vma->vm_mm;
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001494 struct mm_walk walk = {
1495 .hugetlb_entry = gather_hugetlb_stats,
1496 .pmd_entry = gather_pte_stats,
1497 .private = md,
1498 .mm = mm,
1499 };
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001500 struct mempolicy *pol;
David Rientjes948927e2013-11-12 15:07:28 -08001501 char buffer[64];
1502 int nid;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001503
1504 if (!mm)
1505 return 0;
1506
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001507 /* Ensure we start with an empty set of numa_maps statistics. */
1508 memset(md, 0, sizeof(*md));
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001509
Oleg Nesterov498f2372014-10-09 15:27:52 -07001510 pol = __get_vma_policy(vma, vma->vm_start);
1511 if (pol) {
1512 mpol_to_str(buffer, sizeof(buffer), pol);
1513 mpol_cond_put(pol);
1514 } else {
1515 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1516 }
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001517
1518 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1519
1520 if (file) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001521 seq_puts(m, " file=");
Miklos Szeredi2726d562015-06-19 10:30:28 +02001522 seq_file_path(m, file, "\n\t= ");
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001523 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001524 seq_puts(m, " heap");
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001525 } else {
Oleg Nesterov58cb6542014-10-09 15:25:54 -07001526 pid_t tid = pid_of_stack(proc_priv, vma, is_pid);
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001527 if (tid != 0) {
1528 /*
1529 * Thread stack in /proc/PID/task/TID/maps or
1530 * the main process stack.
1531 */
1532 if (!is_pid || (vma->vm_start <= mm->start_stack &&
1533 vma->vm_end >= mm->start_stack))
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001534 seq_puts(m, " stack");
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001535 else
1536 seq_printf(m, " stack:%d", tid);
1537 }
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001538 }
1539
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001540 if (is_vm_hugetlb_page(vma))
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001541 seq_puts(m, " huge");
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001542
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001543 /* mmap_sem is held by m_start */
1544 walk_page_vma(vma, &walk);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001545
1546 if (!md->pages)
1547 goto out;
1548
1549 if (md->anon)
1550 seq_printf(m, " anon=%lu", md->anon);
1551
1552 if (md->dirty)
1553 seq_printf(m, " dirty=%lu", md->dirty);
1554
1555 if (md->pages != md->anon && md->pages != md->dirty)
1556 seq_printf(m, " mapped=%lu", md->pages);
1557
1558 if (md->mapcount_max > 1)
1559 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1560
1561 if (md->swapcache)
1562 seq_printf(m, " swapcache=%lu", md->swapcache);
1563
1564 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1565 seq_printf(m, " active=%lu", md->active);
1566
1567 if (md->writeback)
1568 seq_printf(m, " writeback=%lu", md->writeback);
1569
David Rientjes948927e2013-11-12 15:07:28 -08001570 for_each_node_state(nid, N_MEMORY)
1571 if (md->node[nid])
1572 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
Rafael Aquini198d1592015-02-12 15:01:08 -08001573
1574 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001575out:
1576 seq_putc(m, '\n');
Oleg Nesterovb8c20a92014-10-09 15:25:41 -07001577 m_cache_vma(m, vma);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001578 return 0;
1579}
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001580
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001581static int show_pid_numa_map(struct seq_file *m, void *v)
1582{
1583 return show_numa_map(m, v, 1);
1584}
1585
1586static int show_tid_numa_map(struct seq_file *m, void *v)
1587{
1588 return show_numa_map(m, v, 0);
1589}
1590
Jan Engelhardt03a44822008-02-08 04:21:19 -08001591static const struct seq_operations proc_pid_numa_maps_op = {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001592 .start = m_start,
1593 .next = m_next,
1594 .stop = m_stop,
1595 .show = show_pid_numa_map,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001596};
Eric W. Biederman662795d2006-06-26 00:25:48 -07001597
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001598static const struct seq_operations proc_tid_numa_maps_op = {
1599 .start = m_start,
1600 .next = m_next,
1601 .stop = m_stop,
1602 .show = show_tid_numa_map,
1603};
1604
1605static int numa_maps_open(struct inode *inode, struct file *file,
1606 const struct seq_operations *ops)
Eric W. Biederman662795d2006-06-26 00:25:48 -07001607{
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -07001608 return proc_maps_open(inode, file, ops,
1609 sizeof(struct numa_maps_private));
Eric W. Biederman662795d2006-06-26 00:25:48 -07001610}
1611
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001612static int pid_numa_maps_open(struct inode *inode, struct file *file)
1613{
1614 return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1615}
1616
1617static int tid_numa_maps_open(struct inode *inode, struct file *file)
1618{
1619 return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1620}
1621
1622const struct file_operations proc_pid_numa_maps_operations = {
1623 .open = pid_numa_maps_open,
1624 .read = seq_read,
1625 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -07001626 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001627};
1628
1629const struct file_operations proc_tid_numa_maps_operations = {
1630 .open = tid_numa_maps_open,
Eric W. Biederman662795d2006-06-26 00:25:48 -07001631 .read = seq_read,
1632 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -07001633 .release = proc_map_release,
Eric W. Biederman662795d2006-06-26 00:25:48 -07001634};
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001635#endif /* CONFIG_NUMA */