mm: pagewalk: fix misbehavior of walk_page_range for vma(VM_PFNMAP)
[deliverable/linux.git] / fs / proc / task_mmu.c
CommitLineData
1da177e4 1#include <linux/mm.h>
615d6e87 2#include <linux/vmacache.h>
1da177e4 3#include <linux/hugetlb.h>
22e057c5 4#include <linux/huge_mm.h>
1da177e4
LT
5#include <linux/mount.h>
6#include <linux/seq_file.h>
e070ad49 7#include <linux/highmem.h>
5096add8 8#include <linux/ptrace.h>
5a0e3ad6 9#include <linux/slab.h>
6e21c8f1
CL
10#include <linux/pagemap.h>
11#include <linux/mempolicy.h>
22e057c5 12#include <linux/rmap.h>
85863e47
MM
13#include <linux/swap.h>
14#include <linux/swapops.h>
0f8975ec 15#include <linux/mmu_notifier.h>
e070ad49 16
1da177e4
LT
17#include <asm/elf.h>
18#include <asm/uaccess.h>
e070ad49 19#include <asm/tlbflush.h>
1da177e4
LT
20#include "internal.h"
21
df5f8314 22void task_mem(struct seq_file *m, struct mm_struct *mm)
1da177e4 23{
dc6c9a35 24 unsigned long data, text, lib, swap, ptes, pmds;
365e9c87
HD
25 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;
1da177e4
LT
40
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;
b084d435 44 swap = get_mm_counter(mm, MM_SWAPENTS);
dc6c9a35
KS
45 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);
df5f8314 47 seq_printf(m,
365e9c87 48 "VmPeak:\t%8lu kB\n"
1da177e4
LT
49 "VmSize:\t%8lu kB\n"
50 "VmLck:\t%8lu kB\n"
bc3e53f6 51 "VmPin:\t%8lu kB\n"
365e9c87 52 "VmHWM:\t%8lu kB\n"
1da177e4
LT
53 "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"
b084d435 58 "VmPTE:\t%8lu kB\n"
dc6c9a35 59 "VmPMD:\t%8lu kB\n"
b084d435 60 "VmSwap:\t%8lu kB\n",
365e9c87 61 hiwater_vm << (PAGE_SHIFT-10),
314e51b9 62 total_vm << (PAGE_SHIFT-10),
1da177e4 63 mm->locked_vm << (PAGE_SHIFT-10),
bc3e53f6 64 mm->pinned_vm << (PAGE_SHIFT-10),
365e9c87
HD
65 hiwater_rss << (PAGE_SHIFT-10),
66 total_rss << (PAGE_SHIFT-10),
1da177e4
LT
67 data << (PAGE_SHIFT-10),
68 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
dc6c9a35
KS
69 ptes >> 10,
70 pmds >> 10,
b084d435 71 swap << (PAGE_SHIFT-10));
1da177e4
LT
72}
73
74unsigned long task_vsize(struct mm_struct *mm)
75{
76 return PAGE_SIZE * mm->total_vm;
77}
78
a2ade7b6
AD
79unsigned long task_statm(struct mm_struct *mm,
80 unsigned long *shared, unsigned long *text,
81 unsigned long *data, unsigned long *resident)
1da177e4 82{
d559db08 83 *shared = get_mm_counter(mm, MM_FILEPAGES);
1da177e4
LT
84 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
85 >> PAGE_SHIFT;
86 *data = mm->total_vm - mm->shared_vm;
d559db08 87 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
1da177e4
LT
88 return mm->total_vm;
89}
90
9e781440
KH
91#ifdef CONFIG_NUMA
92/*
498f2371 93 * Save get_task_policy() for show_numa_map().
9e781440
KH
94 */
95static void hold_task_mempolicy(struct proc_maps_private *priv)
96{
97 struct task_struct *task = priv->task;
98
99 task_lock(task);
498f2371 100 priv->task_mempolicy = get_task_policy(task);
9e781440
KH
101 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
59b4bf12 117static void vma_stop(struct proc_maps_private *priv)
a6198797 118{
59b4bf12
ON
119 struct mm_struct *mm = priv->mm;
120
121 release_task_mempolicy(priv);
122 up_read(&mm->mmap_sem);
123 mmput(mm);
a6198797 124}
ec4dd3eb 125
ad2a00e4
ON
126static 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
b8c20a9b
ON
134static 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
0c255321 140static void *m_start(struct seq_file *m, loff_t *ppos)
e070ad49 141{
a6198797 142 struct proc_maps_private *priv = m->private;
b8c20a9b 143 unsigned long last_addr = m->version;
a6198797 144 struct mm_struct *mm;
0c255321
ON
145 struct vm_area_struct *vma;
146 unsigned int pos = *ppos;
a6198797 147
b8c20a9b
ON
148 /* See m_cache_vma(). Zero at the start or after lseek. */
149 if (last_addr == -1UL)
150 return NULL;
151
2c03376d 152 priv->task = get_proc_task(priv->inode);
a6198797 153 if (!priv->task)
ec6fd8a4 154 return ERR_PTR(-ESRCH);
a6198797 155
29a40ace
ON
156 mm = priv->mm;
157 if (!mm || !atomic_inc_not_zero(&mm->mm_users))
158 return NULL;
a6198797 159
0c255321 160 down_read(&mm->mmap_sem);
9e781440 161 hold_task_mempolicy(priv);
0c255321 162 priv->tail_vma = get_gate_vma(mm);
a6198797 163
b8c20a9b
ON
164 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;
0c255321 171 if (pos < mm->map_count) {
557c2d8a
ON
172 for (vma = mm->mmap; pos; pos--) {
173 m->version = vma->vm_start;
a6198797 174 vma = vma->vm_next;
557c2d8a 175 }
a6198797 176 return vma;
0c255321 177 }
a6198797 178
557c2d8a 179 /* we do not bother to update m->version in this case */
0c255321
ON
180 if (pos == mm->map_count && priv->tail_vma)
181 return priv->tail_vma;
59b4bf12
ON
182
183 vma_stop(priv);
184 return NULL;
a6198797
MM
185}
186
187static void *m_next(struct seq_file *m, void *v, loff_t *pos)
188{
189 struct proc_maps_private *priv = m->private;
ad2a00e4 190 struct vm_area_struct *next;
a6198797
MM
191
192 (*pos)++;
ad2a00e4 193 next = m_next_vma(priv, v);
59b4bf12
ON
194 if (!next)
195 vma_stop(priv);
196 return next;
a6198797
MM
197}
198
199static void m_stop(struct seq_file *m, void *v)
200{
201 struct proc_maps_private *priv = m->private;
a6198797 202
59b4bf12
ON
203 if (!IS_ERR_OR_NULL(v))
204 vma_stop(priv);
0d5f5f45 205 if (priv->task) {
a6198797 206 put_task_struct(priv->task);
0d5f5f45
ON
207 priv->task = NULL;
208 }
a6198797
MM
209}
210
4db7d0ee
ON
211static 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
2c03376d 219 priv->inode = inode;
29a40ace
ON
220 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
4db7d0ee
ON
228 return 0;
229}
230
29a40ace
ON
231static 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
a6198797 242static int do_maps_open(struct inode *inode, struct file *file,
03a44825 243 const struct seq_operations *ops)
a6198797 244{
4db7d0ee
ON
245 return proc_maps_open(inode, file, ops,
246 sizeof(struct proc_maps_private));
a6198797 247}
e070ad49 248
58cb6548
ON
249static 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
b7643757
SP
268static void
269show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
1da177e4 270{
e070ad49
ML
271 struct mm_struct *mm = vma->vm_mm;
272 struct file *file = vma->vm_file;
b7643757 273 struct proc_maps_private *priv = m->private;
ca16d140 274 vm_flags_t flags = vma->vm_flags;
1da177e4 275 unsigned long ino = 0;
6260a4b0 276 unsigned long long pgoff = 0;
a09a79f6 277 unsigned long start, end;
1da177e4 278 dev_t dev = 0;
b7643757 279 const char *name = NULL;
1da177e4
LT
280
281 if (file) {
496ad9aa 282 struct inode *inode = file_inode(vma->vm_file);
1da177e4
LT
283 dev = inode->i_sb->s_dev;
284 ino = inode->i_ino;
6260a4b0 285 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
1da177e4
LT
286 }
287
d7824370
LT
288 /* We don't show the stack guard page in /proc/maps */
289 start = vma->vm_start;
a09a79f6
MP
290 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;
d7824370 295
652586df
TH
296 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
297 seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
d7824370 298 start,
a09a79f6 299 end,
1da177e4
LT
300 flags & VM_READ ? 'r' : '-',
301 flags & VM_WRITE ? 'w' : '-',
302 flags & VM_EXEC ? 'x' : '-',
303 flags & VM_MAYSHARE ? 's' : 'p',
6260a4b0 304 pgoff,
652586df 305 MAJOR(dev), MINOR(dev), ino);
1da177e4
LT
306
307 /*
308 * Print the dentry name for named mappings, and a
309 * special [heap] marker for the heap:
310 */
e070ad49 311 if (file) {
652586df 312 seq_pad(m, ' ');
c32c2f63 313 seq_path(m, &file->f_path, "\n");
b7643757
SP
314 goto done;
315 }
316
78d683e8
AL
317 if (vma->vm_ops && vma->vm_ops->name) {
318 name = vma->vm_ops->name(vma);
319 if (name)
320 goto done;
321 }
322
b7643757
SP
323 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
58cb6548 338 tid = pid_of_stack(priv, vma, is_pid);
b7643757
SP
339 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]";
e6e5494c 347 } else {
b7643757 348 /* Thread stack in /proc/PID/maps */
652586df 349 seq_pad(m, ' ');
b7643757 350 seq_printf(m, "[stack:%d]", tid);
1da177e4 351 }
e6e5494c 352 }
b7643757
SP
353 }
354
355done:
356 if (name) {
652586df 357 seq_pad(m, ' ');
b7643757 358 seq_puts(m, name);
1da177e4
LT
359 }
360 seq_putc(m, '\n');
7c88db0c
JK
361}
362
b7643757 363static int show_map(struct seq_file *m, void *v, int is_pid)
7c88db0c 364{
ebb6cdde 365 show_map_vma(m, v, is_pid);
b8c20a9b 366 m_cache_vma(m, v);
1da177e4
LT
367 return 0;
368}
369
b7643757
SP
370static 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
03a44825 380static const struct seq_operations proc_pid_maps_op = {
a6198797
MM
381 .start = m_start,
382 .next = m_next,
383 .stop = m_stop,
b7643757
SP
384 .show = show_pid_map
385};
386
387static 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
a6198797
MM
392};
393
b7643757 394static int pid_maps_open(struct inode *inode, struct file *file)
a6198797
MM
395{
396 return do_maps_open(inode, file, &proc_pid_maps_op);
397}
398
b7643757
SP
399static 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,
29a40ace 408 .release = proc_map_release,
b7643757
SP
409};
410
411const struct file_operations proc_tid_maps_operations = {
412 .open = tid_maps_open,
a6198797
MM
413 .read = seq_read,
414 .llseek = seq_lseek,
29a40ace 415 .release = proc_map_release,
a6198797
MM
416};
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
1e883281 437#ifdef CONFIG_PROC_PAGE_MONITOR
214e471f 438struct mem_size_stats {
a6198797
MM
439 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;
b40d4f84 445 unsigned long anonymous;
4031a219 446 unsigned long anonymous_thp;
214e471f 447 unsigned long swap;
a6198797
MM
448 u64 pss;
449};
450
c164e038
KS
451static void smaps_account(struct mem_size_stats *mss, struct page *page,
452 unsigned long size, bool young, bool dirty)
453{
454 int mapcount;
455
456 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}
ae11c4d9 482
c164e038
KS
483static void smaps_pte_entry(pte_t *pte, unsigned long addr,
484 struct mm_walk *walk)
ae11c4d9
DH
485{
486 struct mem_size_stats *mss = walk->private;
14eb6fdd 487 struct vm_area_struct *vma = walk->vma;
b1d4d9e0 488 struct page *page = NULL;
ae11c4d9 489
c164e038
KS
490 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);
ae11c4d9 494
b1d4d9e0 495 if (!non_swap_entry(swpent))
c164e038 496 mss->swap += PAGE_SIZE;
b1d4d9e0
KK
497 else if (is_migration_entry(swpent))
498 page = migration_entry_to_page(swpent);
499 }
ae11c4d9 500
ae11c4d9
DH
501 if (!page)
502 return;
c164e038 503 smaps_account(mss, page, PAGE_SIZE, pte_young(*pte), pte_dirty(*pte));
ae11c4d9
DH
504}
505
c164e038
KS
506#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;
14eb6fdd 511 struct vm_area_struct *vma = walk->vma;
c164e038
KS
512 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
b3ae5acb 529static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
2165009b 530 struct mm_walk *walk)
e070ad49 531{
14eb6fdd 532 struct vm_area_struct *vma = walk->vma;
ae11c4d9 533 pte_t *pte;
705e87c0 534 spinlock_t *ptl;
e070ad49 535
bf929152 536 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
c164e038 537 smaps_pmd_entry(pmd, addr, walk);
bf929152 538 spin_unlock(ptl);
025c5b24 539 return 0;
22e057c5 540 }
1a5a9906
AA
541
542 if (pmd_trans_unstable(pmd))
543 return 0;
22e057c5
DH
544 /*
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 */
705e87c0 549 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
ae11c4d9 550 for (; addr != end; pte++, addr += PAGE_SIZE)
c164e038 551 smaps_pte_entry(pte, addr, walk);
705e87c0
HD
552 pte_unmap_unlock(pte - 1, ptl);
553 cond_resched();
b3ae5acb 554 return 0;
e070ad49
ML
555}
556
834f82e2
CG
557static 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",
4aae7e43
QR
579#ifdef CONFIG_X86_INTEL_MPX
580 [ilog2(VM_MPX)] = "mp",
581#endif
834f82e2
CG
582 [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",
834f82e2
CG
591 [ilog2(VM_ARCH_1)] = "ar",
592 [ilog2(VM_DONTDUMP)] = "dd",
ec8e41ae
NH
593#ifdef CONFIG_MEM_SOFT_DIRTY
594 [ilog2(VM_SOFTDIRTY)] = "sd",
595#endif
834f82e2
CG
596 [ilog2(VM_MIXEDMAP)] = "mm",
597 [ilog2(VM_HUGEPAGE)] = "hg",
598 [ilog2(VM_NOHUGEPAGE)] = "nh",
599 [ilog2(VM_MERGEABLE)] = "mg",
600 };
601 size_t i;
602
603 seq_puts(m, "VmFlags: ");
604 for (i = 0; i < BITS_PER_LONG; i++) {
605 if (vma->vm_flags & (1UL << i)) {
606 seq_printf(m, "%c%c ",
607 mnemonics[i][0], mnemonics[i][1]);
608 }
609 }
610 seq_putc(m, '\n');
611}
612
b7643757 613static int show_smap(struct seq_file *m, void *v, int is_pid)
e070ad49
ML
614{
615 struct vm_area_struct *vma = v;
e070ad49 616 struct mem_size_stats mss;
2165009b
DH
617 struct mm_walk smaps_walk = {
618 .pmd_entry = smaps_pte_range,
619 .mm = vma->vm_mm,
620 .private = &mss,
621 };
e070ad49
ML
622
623 memset(&mss, 0, sizeof mss);
d82ef020 624 /* mmap_sem is held in m_start */
14eb6fdd 625 walk_page_vma(vma, &smaps_walk);
4752c369 626
b7643757 627 show_map_vma(m, vma, is_pid);
4752c369
MM
628
629 seq_printf(m,
630 "Size: %8lu kB\n"
631 "Rss: %8lu kB\n"
632 "Pss: %8lu kB\n"
633 "Shared_Clean: %8lu kB\n"
634 "Shared_Dirty: %8lu kB\n"
635 "Private_Clean: %8lu kB\n"
636 "Private_Dirty: %8lu kB\n"
214e471f 637 "Referenced: %8lu kB\n"
b40d4f84 638 "Anonymous: %8lu kB\n"
4031a219 639 "AnonHugePages: %8lu kB\n"
08fba699 640 "Swap: %8lu kB\n"
3340289d 641 "KernelPageSize: %8lu kB\n"
2d90508f
NK
642 "MMUPageSize: %8lu kB\n"
643 "Locked: %8lu kB\n",
4752c369
MM
644 (vma->vm_end - vma->vm_start) >> 10,
645 mss.resident >> 10,
646 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
647 mss.shared_clean >> 10,
648 mss.shared_dirty >> 10,
649 mss.private_clean >> 10,
650 mss.private_dirty >> 10,
214e471f 651 mss.referenced >> 10,
b40d4f84 652 mss.anonymous >> 10,
4031a219 653 mss.anonymous_thp >> 10,
08fba699 654 mss.swap >> 10,
3340289d 655 vma_kernel_pagesize(vma) >> 10,
2d90508f
NK
656 vma_mmu_pagesize(vma) >> 10,
657 (vma->vm_flags & VM_LOCKED) ?
658 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
4752c369 659
834f82e2 660 show_smap_vma_flags(m, vma);
b8c20a9b 661 m_cache_vma(m, vma);
7c88db0c 662 return 0;
e070ad49
ML
663}
664
b7643757
SP
665static int show_pid_smap(struct seq_file *m, void *v)
666{
667 return show_smap(m, v, 1);
668}
669
670static int show_tid_smap(struct seq_file *m, void *v)
671{
672 return show_smap(m, v, 0);
673}
674
03a44825 675static const struct seq_operations proc_pid_smaps_op = {
a6198797
MM
676 .start = m_start,
677 .next = m_next,
678 .stop = m_stop,
b7643757
SP
679 .show = show_pid_smap
680};
681
682static const struct seq_operations proc_tid_smaps_op = {
683 .start = m_start,
684 .next = m_next,
685 .stop = m_stop,
686 .show = show_tid_smap
a6198797
MM
687};
688
b7643757 689static int pid_smaps_open(struct inode *inode, struct file *file)
a6198797
MM
690{
691 return do_maps_open(inode, file, &proc_pid_smaps_op);
692}
693
b7643757
SP
694static int tid_smaps_open(struct inode *inode, struct file *file)
695{
696 return do_maps_open(inode, file, &proc_tid_smaps_op);
697}
698
699const struct file_operations proc_pid_smaps_operations = {
700 .open = pid_smaps_open,
701 .read = seq_read,
702 .llseek = seq_lseek,
29a40ace 703 .release = proc_map_release,
b7643757
SP
704};
705
706const struct file_operations proc_tid_smaps_operations = {
707 .open = tid_smaps_open,
a6198797
MM
708 .read = seq_read,
709 .llseek = seq_lseek,
29a40ace 710 .release = proc_map_release,
a6198797
MM
711};
712
541c237c
PE
713/*
714 * We do not want to have constant page-shift bits sitting in
715 * pagemap entries and are about to reuse them some time soon.
716 *
717 * Here's the "migration strategy":
718 * 1. when the system boots these bits remain what they are,
719 * but a warning about future change is printed in log;
720 * 2. once anyone clears soft-dirty bits via clear_refs file,
721 * these flag is set to denote, that user is aware of the
722 * new API and those page-shift bits change their meaning.
723 * The respective warning is printed in dmesg;
724 * 3. In a couple of releases we will remove all the mentions
725 * of page-shift in pagemap entries.
726 */
727
728static bool soft_dirty_cleared __read_mostly;
729
040fa020
PE
730enum clear_refs_types {
731 CLEAR_REFS_ALL = 1,
732 CLEAR_REFS_ANON,
733 CLEAR_REFS_MAPPED,
0f8975ec 734 CLEAR_REFS_SOFT_DIRTY,
040fa020
PE
735 CLEAR_REFS_LAST,
736};
737
af9de7eb 738struct clear_refs_private {
0f8975ec 739 enum clear_refs_types type;
af9de7eb
PE
740};
741
0f8975ec
PE
742static inline void clear_soft_dirty(struct vm_area_struct *vma,
743 unsigned long addr, pte_t *pte)
744{
745#ifdef CONFIG_MEM_SOFT_DIRTY
746 /*
747 * The soft-dirty tracker uses #PF-s to catch writes
748 * to pages, so write-protect the pte as well. See the
749 * Documentation/vm/soft-dirty.txt for full description
750 * of how soft-dirty works.
751 */
752 pte_t ptent = *pte;
179ef71c
CG
753
754 if (pte_present(ptent)) {
755 ptent = pte_wrprotect(ptent);
756 ptent = pte_clear_flags(ptent, _PAGE_SOFT_DIRTY);
757 } else if (is_swap_pte(ptent)) {
758 ptent = pte_swp_clear_soft_dirty(ptent);
759 }
760
0f8975ec
PE
761 set_pte_at(vma->vm_mm, addr, pte, ptent);
762#endif
763}
764
a6198797 765static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
2165009b 766 unsigned long end, struct mm_walk *walk)
a6198797 767{
af9de7eb 768 struct clear_refs_private *cp = walk->private;
5c64f52a 769 struct vm_area_struct *vma = walk->vma;
a6198797
MM
770 pte_t *pte, ptent;
771 spinlock_t *ptl;
772 struct page *page;
773
e180377f 774 split_huge_page_pmd(vma, addr, pmd);
1a5a9906
AA
775 if (pmd_trans_unstable(pmd))
776 return 0;
03319327 777
a6198797
MM
778 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
779 for (; addr != end; pte++, addr += PAGE_SIZE) {
780 ptent = *pte;
a6198797 781
0f8975ec
PE
782 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
783 clear_soft_dirty(vma, addr, pte);
784 continue;
785 }
786
179ef71c
CG
787 if (!pte_present(ptent))
788 continue;
789
a6198797
MM
790 page = vm_normal_page(vma, addr, ptent);
791 if (!page)
792 continue;
793
794 /* Clear accessed and referenced bits. */
795 ptep_test_and_clear_young(vma, addr, pte);
796 ClearPageReferenced(page);
797 }
798 pte_unmap_unlock(pte - 1, ptl);
799 cond_resched();
800 return 0;
801}
802
5c64f52a
NH
803static int clear_refs_test_walk(unsigned long start, unsigned long end,
804 struct mm_walk *walk)
805{
806 struct clear_refs_private *cp = walk->private;
807 struct vm_area_struct *vma = walk->vma;
808
48684a65
NH
809 if (vma->vm_flags & VM_PFNMAP)
810 return 1;
811
5c64f52a
NH
812 /*
813 * Writing 1 to /proc/pid/clear_refs affects all pages.
814 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
815 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
816 * Writing 4 to /proc/pid/clear_refs affects all pages.
817 */
818 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
819 return 1;
820 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
821 return 1;
822 return 0;
823}
824
f248dcb3
MM
825static ssize_t clear_refs_write(struct file *file, const char __user *buf,
826 size_t count, loff_t *ppos)
b813e931 827{
f248dcb3 828 struct task_struct *task;
fb92a4b0 829 char buffer[PROC_NUMBUF];
f248dcb3 830 struct mm_struct *mm;
b813e931 831 struct vm_area_struct *vma;
040fa020
PE
832 enum clear_refs_types type;
833 int itype;
0a8cb8e3 834 int rv;
b813e931 835
f248dcb3
MM
836 memset(buffer, 0, sizeof(buffer));
837 if (count > sizeof(buffer) - 1)
838 count = sizeof(buffer) - 1;
839 if (copy_from_user(buffer, buf, count))
840 return -EFAULT;
040fa020 841 rv = kstrtoint(strstrip(buffer), 10, &itype);
0a8cb8e3
AD
842 if (rv < 0)
843 return rv;
040fa020
PE
844 type = (enum clear_refs_types)itype;
845 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
f248dcb3 846 return -EINVAL;
541c237c
PE
847
848 if (type == CLEAR_REFS_SOFT_DIRTY) {
849 soft_dirty_cleared = true;
c86c97ff
CG
850 pr_warn_once("The pagemap bits 55-60 has changed their meaning!"
851 " See the linux/Documentation/vm/pagemap.txt for "
852 "details.\n");
541c237c
PE
853 }
854
496ad9aa 855 task = get_proc_task(file_inode(file));
f248dcb3
MM
856 if (!task)
857 return -ESRCH;
858 mm = get_task_mm(task);
859 if (mm) {
af9de7eb 860 struct clear_refs_private cp = {
0f8975ec 861 .type = type,
af9de7eb 862 };
20cbc972
AM
863 struct mm_walk clear_refs_walk = {
864 .pmd_entry = clear_refs_pte_range,
5c64f52a 865 .test_walk = clear_refs_test_walk,
20cbc972 866 .mm = mm,
af9de7eb 867 .private = &cp,
20cbc972 868 };
f248dcb3 869 down_read(&mm->mmap_sem);
64e45507
PF
870 if (type == CLEAR_REFS_SOFT_DIRTY) {
871 for (vma = mm->mmap; vma; vma = vma->vm_next) {
872 if (!(vma->vm_flags & VM_SOFTDIRTY))
873 continue;
874 up_read(&mm->mmap_sem);
875 down_write(&mm->mmap_sem);
876 for (vma = mm->mmap; vma; vma = vma->vm_next) {
877 vma->vm_flags &= ~VM_SOFTDIRTY;
878 vma_set_page_prot(vma);
879 }
880 downgrade_write(&mm->mmap_sem);
881 break;
882 }
0f8975ec 883 mmu_notifier_invalidate_range_start(mm, 0, -1);
64e45507 884 }
5c64f52a 885 walk_page_range(0, ~0UL, &clear_refs_walk);
0f8975ec
PE
886 if (type == CLEAR_REFS_SOFT_DIRTY)
887 mmu_notifier_invalidate_range_end(mm, 0, -1);
f248dcb3
MM
888 flush_tlb_mm(mm);
889 up_read(&mm->mmap_sem);
890 mmput(mm);
891 }
892 put_task_struct(task);
fb92a4b0
VL
893
894 return count;
b813e931
DR
895}
896
f248dcb3
MM
897const struct file_operations proc_clear_refs_operations = {
898 .write = clear_refs_write,
6038f373 899 .llseek = noop_llseek,
f248dcb3
MM
900};
901
092b50ba
NH
902typedef struct {
903 u64 pme;
904} pagemap_entry_t;
905
85863e47 906struct pagemapread {
8c829622 907 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
092b50ba 908 pagemap_entry_t *buffer;
2b0a9f01 909 bool v2;
85863e47
MM
910};
911
5aaabe83
NH
912#define PAGEMAP_WALK_SIZE (PMD_SIZE)
913#define PAGEMAP_WALK_MASK (PMD_MASK)
914
8c829622 915#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
f16278c6
HR
916#define PM_STATUS_BITS 3
917#define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
918#define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
919#define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
920#define PM_PSHIFT_BITS 6
921#define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
922#define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
2b0a9f01 923#define __PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
f16278c6
HR
924#define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
925#define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
2b0a9f01
PE
926/* in "new" pagemap pshift bits are occupied with more status bits */
927#define PM_STATUS2(v2, x) (__PM_PSHIFT(v2 ? x : PAGE_SHIFT))
f16278c6 928
0f8975ec 929#define __PM_SOFT_DIRTY (1LL)
f16278c6
HR
930#define PM_PRESENT PM_STATUS(4LL)
931#define PM_SWAP PM_STATUS(2LL)
052fb0d6 932#define PM_FILE PM_STATUS(1LL)
2b0a9f01 933#define PM_NOT_PRESENT(v2) PM_STATUS2(v2, 0)
85863e47
MM
934#define PM_END_OF_BUFFER 1
935
092b50ba
NH
936static inline pagemap_entry_t make_pme(u64 val)
937{
938 return (pagemap_entry_t) { .pme = val };
939}
940
941static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
85863e47
MM
942 struct pagemapread *pm)
943{
092b50ba 944 pm->buffer[pm->pos++] = *pme;
d82ef020 945 if (pm->pos >= pm->len)
aae8679b 946 return PM_END_OF_BUFFER;
85863e47
MM
947 return 0;
948}
949
950static int pagemap_pte_hole(unsigned long start, unsigned long end,
2165009b 951 struct mm_walk *walk)
85863e47 952{
2165009b 953 struct pagemapread *pm = walk->private;
68b5a652 954 unsigned long addr = start;
85863e47 955 int err = 0;
092b50ba 956
68b5a652
PF
957 while (addr < end) {
958 struct vm_area_struct *vma = find_vma(walk->mm, addr);
959 pagemap_entry_t pme = make_pme(PM_NOT_PRESENT(pm->v2));
87e6d49a
PF
960 /* End of address space hole, which we mark as non-present. */
961 unsigned long hole_end;
68b5a652 962
87e6d49a
PF
963 if (vma)
964 hole_end = min(end, vma->vm_start);
965 else
966 hole_end = end;
967
968 for (; addr < hole_end; addr += PAGE_SIZE) {
969 err = add_to_pagemap(addr, &pme, pm);
970 if (err)
971 goto out;
68b5a652
PF
972 }
973
87e6d49a
PF
974 if (!vma)
975 break;
976
977 /* Addresses in the VMA. */
978 if (vma->vm_flags & VM_SOFTDIRTY)
979 pme.pme |= PM_STATUS2(pm->v2, __PM_SOFT_DIRTY);
980 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
68b5a652
PF
981 err = add_to_pagemap(addr, &pme, pm);
982 if (err)
983 goto out;
984 }
85863e47 985 }
68b5a652 986out:
85863e47
MM
987 return err;
988}
989
2b0a9f01 990static void pte_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
052fb0d6 991 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
85863e47 992{
052fb0d6
KK
993 u64 frame, flags;
994 struct page *page = NULL;
0f8975ec 995 int flags2 = 0;
85863e47 996
052fb0d6
KK
997 if (pte_present(pte)) {
998 frame = pte_pfn(pte);
999 flags = PM_PRESENT;
1000 page = vm_normal_page(vma, addr, pte);
e9cdd6e7
CG
1001 if (pte_soft_dirty(pte))
1002 flags2 |= __PM_SOFT_DIRTY;
052fb0d6 1003 } else if (is_swap_pte(pte)) {
179ef71c
CG
1004 swp_entry_t entry;
1005 if (pte_swp_soft_dirty(pte))
1006 flags2 |= __PM_SOFT_DIRTY;
1007 entry = pte_to_swp_entry(pte);
052fb0d6
KK
1008 frame = swp_type(entry) |
1009 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
1010 flags = PM_SWAP;
1011 if (is_migration_entry(entry))
1012 page = migration_entry_to_page(entry);
1013 } else {
d9104d1c
CG
1014 if (vma->vm_flags & VM_SOFTDIRTY)
1015 flags2 |= __PM_SOFT_DIRTY;
1016 *pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, flags2));
052fb0d6
KK
1017 return;
1018 }
1019
1020 if (page && !PageAnon(page))
1021 flags |= PM_FILE;
e9cdd6e7 1022 if ((vma->vm_flags & VM_SOFTDIRTY))
0f8975ec 1023 flags2 |= __PM_SOFT_DIRTY;
052fb0d6 1024
0f8975ec 1025 *pme = make_pme(PM_PFRAME(frame) | PM_STATUS2(pm->v2, flags2) | flags);
bcf8039e
DH
1026}
1027
5aaabe83 1028#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2b0a9f01 1029static void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
0f8975ec 1030 pmd_t pmd, int offset, int pmd_flags2)
5aaabe83 1031{
5aaabe83
NH
1032 /*
1033 * Currently pmd for thp is always present because thp can not be
1034 * swapped-out, migrated, or HWPOISONed (split in such cases instead.)
1035 * This if-check is just to prepare for future implementation.
1036 */
1037 if (pmd_present(pmd))
092b50ba 1038 *pme = make_pme(PM_PFRAME(pmd_pfn(pmd) + offset)
0f8975ec 1039 | PM_STATUS2(pm->v2, pmd_flags2) | PM_PRESENT);
16fbdce6 1040 else
d9104d1c 1041 *pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, pmd_flags2));
5aaabe83
NH
1042}
1043#else
2b0a9f01 1044static inline void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
0f8975ec 1045 pmd_t pmd, int offset, int pmd_flags2)
5aaabe83 1046{
5aaabe83
NH
1047}
1048#endif
1049
85863e47 1050static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
2165009b 1051 struct mm_walk *walk)
85863e47 1052{
f995ece2 1053 struct vm_area_struct *vma = walk->vma;
2165009b 1054 struct pagemapread *pm = walk->private;
bf929152 1055 spinlock_t *ptl;
05fbf357 1056 pte_t *pte, *orig_pte;
85863e47
MM
1057 int err = 0;
1058
f995ece2 1059 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
0f8975ec
PE
1060 int pmd_flags2;
1061
d9104d1c
CG
1062 if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(*pmd))
1063 pmd_flags2 = __PM_SOFT_DIRTY;
1064 else
1065 pmd_flags2 = 0;
1066
025c5b24
NH
1067 for (; addr != end; addr += PAGE_SIZE) {
1068 unsigned long offset;
81d0fa62 1069 pagemap_entry_t pme;
025c5b24
NH
1070
1071 offset = (addr & ~PAGEMAP_WALK_MASK) >>
1072 PAGE_SHIFT;
0f8975ec 1073 thp_pmd_to_pagemap_entry(&pme, pm, *pmd, offset, pmd_flags2);
092b50ba 1074 err = add_to_pagemap(addr, &pme, pm);
025c5b24
NH
1075 if (err)
1076 break;
5aaabe83 1077 }
bf929152 1078 spin_unlock(ptl);
025c5b24 1079 return err;
5aaabe83
NH
1080 }
1081
45f83cef
AA
1082 if (pmd_trans_unstable(pmd))
1083 return 0;
81d0fa62 1084
f995ece2
NH
1085 /*
1086 * We can assume that @vma always points to a valid one and @end never
1087 * goes beyond vma->vm_end.
1088 */
1089 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1090 for (; addr < end; pte++, addr += PAGE_SIZE) {
1091 pagemap_entry_t pme;
05fbf357 1092
f995ece2
NH
1093 pte_to_pagemap_entry(&pme, pm, vma, addr, *pte);
1094 err = add_to_pagemap(addr, &pme, pm);
05fbf357 1095 if (err)
81d0fa62 1096 break;
85863e47 1097 }
f995ece2 1098 pte_unmap_unlock(orig_pte, ptl);
85863e47
MM
1099
1100 cond_resched();
1101
1102 return err;
1103}
1104
1a5cb814 1105#ifdef CONFIG_HUGETLB_PAGE
2b0a9f01 1106static void huge_pte_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
d9104d1c 1107 pte_t pte, int offset, int flags2)
5dc37642 1108{
5dc37642 1109 if (pte_present(pte))
d9104d1c
CG
1110 *pme = make_pme(PM_PFRAME(pte_pfn(pte) + offset) |
1111 PM_STATUS2(pm->v2, flags2) |
1112 PM_PRESENT);
16fbdce6 1113 else
d9104d1c
CG
1114 *pme = make_pme(PM_NOT_PRESENT(pm->v2) |
1115 PM_STATUS2(pm->v2, flags2));
5dc37642
NH
1116}
1117
116354d1
NH
1118/* This function walks within one hugetlb entry in the single call */
1119static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
1120 unsigned long addr, unsigned long end,
1121 struct mm_walk *walk)
5dc37642 1122{
5dc37642 1123 struct pagemapread *pm = walk->private;
f995ece2 1124 struct vm_area_struct *vma = walk->vma;
5dc37642 1125 int err = 0;
d9104d1c 1126 int flags2;
16fbdce6 1127 pagemap_entry_t pme;
5dc37642 1128
f995ece2 1129 if (vma->vm_flags & VM_SOFTDIRTY)
d9104d1c
CG
1130 flags2 = __PM_SOFT_DIRTY;
1131 else
1132 flags2 = 0;
1133
5dc37642 1134 for (; addr != end; addr += PAGE_SIZE) {
116354d1 1135 int offset = (addr & ~hmask) >> PAGE_SHIFT;
d9104d1c 1136 huge_pte_to_pagemap_entry(&pme, pm, *pte, offset, flags2);
092b50ba 1137 err = add_to_pagemap(addr, &pme, pm);
5dc37642
NH
1138 if (err)
1139 return err;
1140 }
1141
1142 cond_resched();
1143
1144 return err;
1145}
1a5cb814 1146#endif /* HUGETLB_PAGE */
5dc37642 1147
85863e47
MM
1148/*
1149 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1150 *
f16278c6
HR
1151 * For each page in the address space, this file contains one 64-bit entry
1152 * consisting of the following:
1153 *
052fb0d6 1154 * Bits 0-54 page frame number (PFN) if present
f16278c6 1155 * Bits 0-4 swap type if swapped
052fb0d6 1156 * Bits 5-54 swap offset if swapped
f16278c6 1157 * Bits 55-60 page shift (page size = 1<<page shift)
052fb0d6 1158 * Bit 61 page is file-page or shared-anon
f16278c6
HR
1159 * Bit 62 page swapped
1160 * Bit 63 page present
1161 *
1162 * If the page is not present but in swap, then the PFN contains an
1163 * encoding of the swap file number and the page's offset into the
1164 * swap. Unmapped pages return a null PFN. This allows determining
85863e47
MM
1165 * precisely which pages are mapped (or in swap) and comparing mapped
1166 * pages between processes.
1167 *
1168 * Efficient users of this interface will use /proc/pid/maps to
1169 * determine which areas of memory are actually mapped and llseek to
1170 * skip over unmapped regions.
1171 */
1172static ssize_t pagemap_read(struct file *file, char __user *buf,
1173 size_t count, loff_t *ppos)
1174{
496ad9aa 1175 struct task_struct *task = get_proc_task(file_inode(file));
85863e47
MM
1176 struct mm_struct *mm;
1177 struct pagemapread pm;
85863e47 1178 int ret = -ESRCH;
ee1e6ab6 1179 struct mm_walk pagemap_walk = {};
5d7e0d2b
AM
1180 unsigned long src;
1181 unsigned long svpfn;
1182 unsigned long start_vaddr;
1183 unsigned long end_vaddr;
d82ef020 1184 int copied = 0;
85863e47
MM
1185
1186 if (!task)
1187 goto out;
1188
85863e47
MM
1189 ret = -EINVAL;
1190 /* file position must be aligned */
aae8679b 1191 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
fb39380b 1192 goto out_task;
85863e47
MM
1193
1194 ret = 0;
08161786
VM
1195 if (!count)
1196 goto out_task;
1197
541c237c 1198 pm.v2 = soft_dirty_cleared;
8c829622 1199 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1200 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
5d7e0d2b 1201 ret = -ENOMEM;
d82ef020 1202 if (!pm.buffer)
98bc93e5
KM
1203 goto out_task;
1204
e7dcd999 1205 mm = mm_access(task, PTRACE_MODE_READ);
98bc93e5
KM
1206 ret = PTR_ERR(mm);
1207 if (!mm || IS_ERR(mm))
1208 goto out_free;
85863e47 1209
5d7e0d2b
AM
1210 pagemap_walk.pmd_entry = pagemap_pte_range;
1211 pagemap_walk.pte_hole = pagemap_pte_hole;
1a5cb814 1212#ifdef CONFIG_HUGETLB_PAGE
5dc37642 1213 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
1a5cb814 1214#endif
5d7e0d2b
AM
1215 pagemap_walk.mm = mm;
1216 pagemap_walk.private = &pm;
1217
1218 src = *ppos;
1219 svpfn = src / PM_ENTRY_BYTES;
1220 start_vaddr = svpfn << PAGE_SHIFT;
1221 end_vaddr = TASK_SIZE_OF(task);
1222
1223 /* watch out for wraparound */
1224 if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
1225 start_vaddr = end_vaddr;
1226
1227 /*
1228 * The odds are that this will stop walking way
1229 * before end_vaddr, because the length of the
1230 * user buffer is tracked in "pm", and the walk
1231 * will stop when we hit the end of the buffer.
1232 */
d82ef020
KH
1233 ret = 0;
1234 while (count && (start_vaddr < end_vaddr)) {
1235 int len;
1236 unsigned long end;
1237
1238 pm.pos = 0;
ea251c1d 1239 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
d82ef020
KH
1240 /* overflow ? */
1241 if (end < start_vaddr || end > end_vaddr)
1242 end = end_vaddr;
1243 down_read(&mm->mmap_sem);
1244 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1245 up_read(&mm->mmap_sem);
1246 start_vaddr = end;
1247
1248 len = min(count, PM_ENTRY_BYTES * pm.pos);
309361e0 1249 if (copy_to_user(buf, pm.buffer, len)) {
d82ef020 1250 ret = -EFAULT;
98bc93e5 1251 goto out_mm;
d82ef020
KH
1252 }
1253 copied += len;
1254 buf += len;
1255 count -= len;
85863e47 1256 }
d82ef020
KH
1257 *ppos += copied;
1258 if (!ret || ret == PM_END_OF_BUFFER)
1259 ret = copied;
1260
fb39380b
MT
1261out_mm:
1262 mmput(mm);
98bc93e5
KM
1263out_free:
1264 kfree(pm.buffer);
85863e47
MM
1265out_task:
1266 put_task_struct(task);
1267out:
1268 return ret;
1269}
1270
541c237c
PE
1271static int pagemap_open(struct inode *inode, struct file *file)
1272{
1273 pr_warn_once("Bits 55-60 of /proc/PID/pagemap entries are about "
1274 "to stop being page-shift some time soon. See the "
1275 "linux/Documentation/vm/pagemap.txt for details.\n");
1276 return 0;
1277}
1278
85863e47
MM
1279const struct file_operations proc_pagemap_operations = {
1280 .llseek = mem_lseek, /* borrow this */
1281 .read = pagemap_read,
541c237c 1282 .open = pagemap_open,
85863e47 1283};
1e883281 1284#endif /* CONFIG_PROC_PAGE_MONITOR */
85863e47 1285
6e21c8f1 1286#ifdef CONFIG_NUMA
6e21c8f1 1287
f69ff943 1288struct numa_maps {
f69ff943
SW
1289 unsigned long pages;
1290 unsigned long anon;
1291 unsigned long active;
1292 unsigned long writeback;
1293 unsigned long mapcount_max;
1294 unsigned long dirty;
1295 unsigned long swapcache;
1296 unsigned long node[MAX_NUMNODES];
1297};
1298
5b52fc89
SW
1299struct numa_maps_private {
1300 struct proc_maps_private proc_maps;
1301 struct numa_maps md;
1302};
1303
eb4866d0
DH
1304static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1305 unsigned long nr_pages)
f69ff943
SW
1306{
1307 int count = page_mapcount(page);
1308
eb4866d0 1309 md->pages += nr_pages;
f69ff943 1310 if (pte_dirty || PageDirty(page))
eb4866d0 1311 md->dirty += nr_pages;
f69ff943
SW
1312
1313 if (PageSwapCache(page))
eb4866d0 1314 md->swapcache += nr_pages;
f69ff943
SW
1315
1316 if (PageActive(page) || PageUnevictable(page))
eb4866d0 1317 md->active += nr_pages;
f69ff943
SW
1318
1319 if (PageWriteback(page))
eb4866d0 1320 md->writeback += nr_pages;
f69ff943
SW
1321
1322 if (PageAnon(page))
eb4866d0 1323 md->anon += nr_pages;
f69ff943
SW
1324
1325 if (count > md->mapcount_max)
1326 md->mapcount_max = count;
1327
eb4866d0 1328 md->node[page_to_nid(page)] += nr_pages;
f69ff943
SW
1329}
1330
3200a8aa
DH
1331static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1332 unsigned long addr)
1333{
1334 struct page *page;
1335 int nid;
1336
1337 if (!pte_present(pte))
1338 return NULL;
1339
1340 page = vm_normal_page(vma, addr, pte);
1341 if (!page)
1342 return NULL;
1343
1344 if (PageReserved(page))
1345 return NULL;
1346
1347 nid = page_to_nid(page);
4ff1b2c2 1348 if (!node_isset(nid, node_states[N_MEMORY]))
3200a8aa
DH
1349 return NULL;
1350
1351 return page;
1352}
1353
f69ff943
SW
1354static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1355 unsigned long end, struct mm_walk *walk)
1356{
d85f4d6d
NH
1357 struct numa_maps *md = walk->private;
1358 struct vm_area_struct *vma = walk->vma;
f69ff943
SW
1359 spinlock_t *ptl;
1360 pte_t *orig_pte;
1361 pte_t *pte;
1362
d85f4d6d 1363 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
025c5b24
NH
1364 pte_t huge_pte = *(pte_t *)pmd;
1365 struct page *page;
1366
d85f4d6d 1367 page = can_gather_numa_stats(huge_pte, vma, addr);
025c5b24
NH
1368 if (page)
1369 gather_stats(page, md, pte_dirty(huge_pte),
1370 HPAGE_PMD_SIZE/PAGE_SIZE);
bf929152 1371 spin_unlock(ptl);
025c5b24 1372 return 0;
32ef4384
DH
1373 }
1374
1a5a9906
AA
1375 if (pmd_trans_unstable(pmd))
1376 return 0;
f69ff943
SW
1377 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1378 do {
d85f4d6d 1379 struct page *page = can_gather_numa_stats(*pte, vma, addr);
f69ff943
SW
1380 if (!page)
1381 continue;
eb4866d0 1382 gather_stats(page, md, pte_dirty(*pte), 1);
f69ff943
SW
1383
1384 } while (pte++, addr += PAGE_SIZE, addr != end);
1385 pte_unmap_unlock(orig_pte, ptl);
1386 return 0;
1387}
1388#ifdef CONFIG_HUGETLB_PAGE
632fd60f 1389static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
f69ff943
SW
1390 unsigned long addr, unsigned long end, struct mm_walk *walk)
1391{
1392 struct numa_maps *md;
1393 struct page *page;
1394
d4c54919 1395 if (!pte_present(*pte))
f69ff943
SW
1396 return 0;
1397
1398 page = pte_page(*pte);
1399 if (!page)
1400 return 0;
1401
1402 md = walk->private;
eb4866d0 1403 gather_stats(page, md, pte_dirty(*pte), 1);
f69ff943
SW
1404 return 0;
1405}
1406
1407#else
632fd60f 1408static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
f69ff943
SW
1409 unsigned long addr, unsigned long end, struct mm_walk *walk)
1410{
1411 return 0;
1412}
1413#endif
1414
1415/*
1416 * Display pages allocated per node and memory policy via /proc.
1417 */
b7643757 1418static int show_numa_map(struct seq_file *m, void *v, int is_pid)
f69ff943 1419{
5b52fc89
SW
1420 struct numa_maps_private *numa_priv = m->private;
1421 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
f69ff943 1422 struct vm_area_struct *vma = v;
5b52fc89 1423 struct numa_maps *md = &numa_priv->md;
f69ff943
SW
1424 struct file *file = vma->vm_file;
1425 struct mm_struct *mm = vma->vm_mm;
d85f4d6d
NH
1426 struct mm_walk walk = {
1427 .hugetlb_entry = gather_hugetlb_stats,
1428 .pmd_entry = gather_pte_stats,
1429 .private = md,
1430 .mm = mm,
1431 };
f69ff943 1432 struct mempolicy *pol;
948927ee
DR
1433 char buffer[64];
1434 int nid;
f69ff943
SW
1435
1436 if (!mm)
1437 return 0;
1438
5b52fc89
SW
1439 /* Ensure we start with an empty set of numa_maps statistics. */
1440 memset(md, 0, sizeof(*md));
f69ff943 1441
498f2371
ON
1442 pol = __get_vma_policy(vma, vma->vm_start);
1443 if (pol) {
1444 mpol_to_str(buffer, sizeof(buffer), pol);
1445 mpol_cond_put(pol);
1446 } else {
1447 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1448 }
f69ff943
SW
1449
1450 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1451
1452 if (file) {
17c2b4ee 1453 seq_puts(m, " file=");
f69ff943
SW
1454 seq_path(m, &file->f_path, "\n\t= ");
1455 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
17c2b4ee 1456 seq_puts(m, " heap");
b7643757 1457 } else {
58cb6548 1458 pid_t tid = pid_of_stack(proc_priv, vma, is_pid);
b7643757
SP
1459 if (tid != 0) {
1460 /*
1461 * Thread stack in /proc/PID/task/TID/maps or
1462 * the main process stack.
1463 */
1464 if (!is_pid || (vma->vm_start <= mm->start_stack &&
1465 vma->vm_end >= mm->start_stack))
17c2b4ee 1466 seq_puts(m, " stack");
b7643757
SP
1467 else
1468 seq_printf(m, " stack:%d", tid);
1469 }
f69ff943
SW
1470 }
1471
fc360bd9 1472 if (is_vm_hugetlb_page(vma))
17c2b4ee 1473 seq_puts(m, " huge");
fc360bd9 1474
d85f4d6d
NH
1475 /* mmap_sem is held by m_start */
1476 walk_page_vma(vma, &walk);
f69ff943
SW
1477
1478 if (!md->pages)
1479 goto out;
1480
1481 if (md->anon)
1482 seq_printf(m, " anon=%lu", md->anon);
1483
1484 if (md->dirty)
1485 seq_printf(m, " dirty=%lu", md->dirty);
1486
1487 if (md->pages != md->anon && md->pages != md->dirty)
1488 seq_printf(m, " mapped=%lu", md->pages);
1489
1490 if (md->mapcount_max > 1)
1491 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1492
1493 if (md->swapcache)
1494 seq_printf(m, " swapcache=%lu", md->swapcache);
1495
1496 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1497 seq_printf(m, " active=%lu", md->active);
1498
1499 if (md->writeback)
1500 seq_printf(m, " writeback=%lu", md->writeback);
1501
948927ee
DR
1502 for_each_node_state(nid, N_MEMORY)
1503 if (md->node[nid])
1504 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
f69ff943
SW
1505out:
1506 seq_putc(m, '\n');
b8c20a9b 1507 m_cache_vma(m, vma);
f69ff943
SW
1508 return 0;
1509}
5b52fc89 1510
b7643757
SP
1511static int show_pid_numa_map(struct seq_file *m, void *v)
1512{
1513 return show_numa_map(m, v, 1);
1514}
1515
1516static int show_tid_numa_map(struct seq_file *m, void *v)
1517{
1518 return show_numa_map(m, v, 0);
1519}
1520
03a44825 1521static const struct seq_operations proc_pid_numa_maps_op = {
b7643757
SP
1522 .start = m_start,
1523 .next = m_next,
1524 .stop = m_stop,
1525 .show = show_pid_numa_map,
6e21c8f1 1526};
662795de 1527
b7643757
SP
1528static const struct seq_operations proc_tid_numa_maps_op = {
1529 .start = m_start,
1530 .next = m_next,
1531 .stop = m_stop,
1532 .show = show_tid_numa_map,
1533};
1534
1535static int numa_maps_open(struct inode *inode, struct file *file,
1536 const struct seq_operations *ops)
662795de 1537{
4db7d0ee
ON
1538 return proc_maps_open(inode, file, ops,
1539 sizeof(struct numa_maps_private));
662795de
EB
1540}
1541
b7643757
SP
1542static int pid_numa_maps_open(struct inode *inode, struct file *file)
1543{
1544 return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1545}
1546
1547static int tid_numa_maps_open(struct inode *inode, struct file *file)
1548{
1549 return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1550}
1551
1552const struct file_operations proc_pid_numa_maps_operations = {
1553 .open = pid_numa_maps_open,
1554 .read = seq_read,
1555 .llseek = seq_lseek,
29a40ace 1556 .release = proc_map_release,
b7643757
SP
1557};
1558
1559const struct file_operations proc_tid_numa_maps_operations = {
1560 .open = tid_numa_maps_open,
662795de
EB
1561 .read = seq_read,
1562 .llseek = seq_lseek,
29a40ace 1563 .release = proc_map_release,
662795de 1564};
f69ff943 1565#endif /* CONFIG_NUMA */
This page took 0.860814 seconds and 5 git commands to generate.