mmu-notifier: add clear_young callback
[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, ' ');
2726d566 313 seq_file_path(m, file, "\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 448 u64 pss;
8334b962 449 u64 swap_pss;
a6198797
MM
450};
451
c164e038
KS
452static void smaps_account(struct mem_size_stats *mss, struct page *page,
453 unsigned long size, bool young, bool dirty)
454{
455 int mapcount;
456
457 if (PageAnon(page))
458 mss->anonymous += size;
459
460 mss->resident += size;
461 /* Accumulate the size in pages that have been accessed. */
462 if (young || PageReferenced(page))
463 mss->referenced += size;
464 mapcount = page_mapcount(page);
465 if (mapcount >= 2) {
466 u64 pss_delta;
467
468 if (dirty || PageDirty(page))
469 mss->shared_dirty += size;
470 else
471 mss->shared_clean += size;
472 pss_delta = (u64)size << PSS_SHIFT;
473 do_div(pss_delta, mapcount);
474 mss->pss += pss_delta;
475 } else {
476 if (dirty || PageDirty(page))
477 mss->private_dirty += size;
478 else
479 mss->private_clean += size;
480 mss->pss += (u64)size << PSS_SHIFT;
481 }
482}
ae11c4d9 483
c164e038
KS
484static void smaps_pte_entry(pte_t *pte, unsigned long addr,
485 struct mm_walk *walk)
ae11c4d9
DH
486{
487 struct mem_size_stats *mss = walk->private;
14eb6fdd 488 struct vm_area_struct *vma = walk->vma;
b1d4d9e0 489 struct page *page = NULL;
ae11c4d9 490
c164e038
KS
491 if (pte_present(*pte)) {
492 page = vm_normal_page(vma, addr, *pte);
493 } else if (is_swap_pte(*pte)) {
494 swp_entry_t swpent = pte_to_swp_entry(*pte);
ae11c4d9 495
8334b962
MK
496 if (!non_swap_entry(swpent)) {
497 int mapcount;
498
c164e038 499 mss->swap += PAGE_SIZE;
8334b962
MK
500 mapcount = swp_swapcount(swpent);
501 if (mapcount >= 2) {
502 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
503
504 do_div(pss_delta, mapcount);
505 mss->swap_pss += pss_delta;
506 } else {
507 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
508 }
509 } else if (is_migration_entry(swpent))
b1d4d9e0
KK
510 page = migration_entry_to_page(swpent);
511 }
ae11c4d9 512
ae11c4d9
DH
513 if (!page)
514 return;
c164e038 515 smaps_account(mss, page, PAGE_SIZE, pte_young(*pte), pte_dirty(*pte));
ae11c4d9
DH
516}
517
c164e038
KS
518#ifdef CONFIG_TRANSPARENT_HUGEPAGE
519static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
520 struct mm_walk *walk)
521{
522 struct mem_size_stats *mss = walk->private;
14eb6fdd 523 struct vm_area_struct *vma = walk->vma;
c164e038
KS
524 struct page *page;
525
526 /* FOLL_DUMP will return -EFAULT on huge zero page */
527 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
528 if (IS_ERR_OR_NULL(page))
529 return;
530 mss->anonymous_thp += HPAGE_PMD_SIZE;
531 smaps_account(mss, page, HPAGE_PMD_SIZE,
532 pmd_young(*pmd), pmd_dirty(*pmd));
533}
534#else
535static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
536 struct mm_walk *walk)
537{
538}
539#endif
540
b3ae5acb 541static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
2165009b 542 struct mm_walk *walk)
e070ad49 543{
14eb6fdd 544 struct vm_area_struct *vma = walk->vma;
ae11c4d9 545 pte_t *pte;
705e87c0 546 spinlock_t *ptl;
e070ad49 547
bf929152 548 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
c164e038 549 smaps_pmd_entry(pmd, addr, walk);
bf929152 550 spin_unlock(ptl);
025c5b24 551 return 0;
22e057c5 552 }
1a5a9906
AA
553
554 if (pmd_trans_unstable(pmd))
555 return 0;
22e057c5
DH
556 /*
557 * The mmap_sem held all the way back in m_start() is what
558 * keeps khugepaged out of here and from collapsing things
559 * in here.
560 */
705e87c0 561 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
ae11c4d9 562 for (; addr != end; pte++, addr += PAGE_SIZE)
c164e038 563 smaps_pte_entry(pte, addr, walk);
705e87c0
HD
564 pte_unmap_unlock(pte - 1, ptl);
565 cond_resched();
b3ae5acb 566 return 0;
e070ad49
ML
567}
568
834f82e2
CG
569static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
570{
571 /*
572 * Don't forget to update Documentation/ on changes.
573 */
574 static const char mnemonics[BITS_PER_LONG][2] = {
575 /*
576 * In case if we meet a flag we don't know about.
577 */
578 [0 ... (BITS_PER_LONG-1)] = "??",
579
580 [ilog2(VM_READ)] = "rd",
581 [ilog2(VM_WRITE)] = "wr",
582 [ilog2(VM_EXEC)] = "ex",
583 [ilog2(VM_SHARED)] = "sh",
584 [ilog2(VM_MAYREAD)] = "mr",
585 [ilog2(VM_MAYWRITE)] = "mw",
586 [ilog2(VM_MAYEXEC)] = "me",
587 [ilog2(VM_MAYSHARE)] = "ms",
588 [ilog2(VM_GROWSDOWN)] = "gd",
589 [ilog2(VM_PFNMAP)] = "pf",
590 [ilog2(VM_DENYWRITE)] = "dw",
4aae7e43
QR
591#ifdef CONFIG_X86_INTEL_MPX
592 [ilog2(VM_MPX)] = "mp",
593#endif
834f82e2
CG
594 [ilog2(VM_LOCKED)] = "lo",
595 [ilog2(VM_IO)] = "io",
596 [ilog2(VM_SEQ_READ)] = "sr",
597 [ilog2(VM_RAND_READ)] = "rr",
598 [ilog2(VM_DONTCOPY)] = "dc",
599 [ilog2(VM_DONTEXPAND)] = "de",
600 [ilog2(VM_ACCOUNT)] = "ac",
601 [ilog2(VM_NORESERVE)] = "nr",
602 [ilog2(VM_HUGETLB)] = "ht",
834f82e2
CG
603 [ilog2(VM_ARCH_1)] = "ar",
604 [ilog2(VM_DONTDUMP)] = "dd",
ec8e41ae
NH
605#ifdef CONFIG_MEM_SOFT_DIRTY
606 [ilog2(VM_SOFTDIRTY)] = "sd",
607#endif
834f82e2
CG
608 [ilog2(VM_MIXEDMAP)] = "mm",
609 [ilog2(VM_HUGEPAGE)] = "hg",
610 [ilog2(VM_NOHUGEPAGE)] = "nh",
611 [ilog2(VM_MERGEABLE)] = "mg",
16ba6f81
AA
612 [ilog2(VM_UFFD_MISSING)]= "um",
613 [ilog2(VM_UFFD_WP)] = "uw",
834f82e2
CG
614 };
615 size_t i;
616
617 seq_puts(m, "VmFlags: ");
618 for (i = 0; i < BITS_PER_LONG; i++) {
619 if (vma->vm_flags & (1UL << i)) {
620 seq_printf(m, "%c%c ",
621 mnemonics[i][0], mnemonics[i][1]);
622 }
623 }
624 seq_putc(m, '\n');
625}
626
b7643757 627static int show_smap(struct seq_file *m, void *v, int is_pid)
e070ad49
ML
628{
629 struct vm_area_struct *vma = v;
e070ad49 630 struct mem_size_stats mss;
2165009b
DH
631 struct mm_walk smaps_walk = {
632 .pmd_entry = smaps_pte_range,
633 .mm = vma->vm_mm,
634 .private = &mss,
635 };
e070ad49
ML
636
637 memset(&mss, 0, sizeof mss);
d82ef020 638 /* mmap_sem is held in m_start */
14eb6fdd 639 walk_page_vma(vma, &smaps_walk);
4752c369 640
b7643757 641 show_map_vma(m, vma, is_pid);
4752c369
MM
642
643 seq_printf(m,
644 "Size: %8lu kB\n"
645 "Rss: %8lu kB\n"
646 "Pss: %8lu kB\n"
647 "Shared_Clean: %8lu kB\n"
648 "Shared_Dirty: %8lu kB\n"
649 "Private_Clean: %8lu kB\n"
650 "Private_Dirty: %8lu kB\n"
214e471f 651 "Referenced: %8lu kB\n"
b40d4f84 652 "Anonymous: %8lu kB\n"
4031a219 653 "AnonHugePages: %8lu kB\n"
08fba699 654 "Swap: %8lu kB\n"
8334b962 655 "SwapPss: %8lu kB\n"
3340289d 656 "KernelPageSize: %8lu kB\n"
2d90508f
NK
657 "MMUPageSize: %8lu kB\n"
658 "Locked: %8lu kB\n",
4752c369
MM
659 (vma->vm_end - vma->vm_start) >> 10,
660 mss.resident >> 10,
661 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
662 mss.shared_clean >> 10,
663 mss.shared_dirty >> 10,
664 mss.private_clean >> 10,
665 mss.private_dirty >> 10,
214e471f 666 mss.referenced >> 10,
b40d4f84 667 mss.anonymous >> 10,
4031a219 668 mss.anonymous_thp >> 10,
08fba699 669 mss.swap >> 10,
8334b962 670 (unsigned long)(mss.swap_pss >> (10 + PSS_SHIFT)),
3340289d 671 vma_kernel_pagesize(vma) >> 10,
2d90508f
NK
672 vma_mmu_pagesize(vma) >> 10,
673 (vma->vm_flags & VM_LOCKED) ?
674 (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
4752c369 675
834f82e2 676 show_smap_vma_flags(m, vma);
b8c20a9b 677 m_cache_vma(m, vma);
7c88db0c 678 return 0;
e070ad49
ML
679}
680
b7643757
SP
681static int show_pid_smap(struct seq_file *m, void *v)
682{
683 return show_smap(m, v, 1);
684}
685
686static int show_tid_smap(struct seq_file *m, void *v)
687{
688 return show_smap(m, v, 0);
689}
690
03a44825 691static const struct seq_operations proc_pid_smaps_op = {
a6198797
MM
692 .start = m_start,
693 .next = m_next,
694 .stop = m_stop,
b7643757
SP
695 .show = show_pid_smap
696};
697
698static const struct seq_operations proc_tid_smaps_op = {
699 .start = m_start,
700 .next = m_next,
701 .stop = m_stop,
702 .show = show_tid_smap
a6198797
MM
703};
704
b7643757 705static int pid_smaps_open(struct inode *inode, struct file *file)
a6198797
MM
706{
707 return do_maps_open(inode, file, &proc_pid_smaps_op);
708}
709
b7643757
SP
710static int tid_smaps_open(struct inode *inode, struct file *file)
711{
712 return do_maps_open(inode, file, &proc_tid_smaps_op);
713}
714
715const struct file_operations proc_pid_smaps_operations = {
716 .open = pid_smaps_open,
717 .read = seq_read,
718 .llseek = seq_lseek,
29a40ace 719 .release = proc_map_release,
b7643757
SP
720};
721
722const struct file_operations proc_tid_smaps_operations = {
723 .open = tid_smaps_open,
a6198797
MM
724 .read = seq_read,
725 .llseek = seq_lseek,
29a40ace 726 .release = proc_map_release,
a6198797
MM
727};
728
040fa020
PE
729enum clear_refs_types {
730 CLEAR_REFS_ALL = 1,
731 CLEAR_REFS_ANON,
732 CLEAR_REFS_MAPPED,
0f8975ec 733 CLEAR_REFS_SOFT_DIRTY,
695f0559 734 CLEAR_REFS_MM_HIWATER_RSS,
040fa020
PE
735 CLEAR_REFS_LAST,
736};
737
af9de7eb 738struct clear_refs_private {
0f8975ec 739 enum clear_refs_types type;
af9de7eb
PE
740};
741
7d5b3bfa 742#ifdef CONFIG_MEM_SOFT_DIRTY
0f8975ec
PE
743static inline void clear_soft_dirty(struct vm_area_struct *vma,
744 unsigned long addr, pte_t *pte)
745{
0f8975ec
PE
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 761 set_pte_at(vma->vm_mm, addr, pte, ptent);
0f8975ec
PE
762}
763
7d5b3bfa
KS
764static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
765 unsigned long addr, pmd_t *pmdp)
766{
767 pmd_t pmd = *pmdp;
768
769 pmd = pmd_wrprotect(pmd);
770 pmd = pmd_clear_flags(pmd, _PAGE_SOFT_DIRTY);
771
772 if (vma->vm_flags & VM_SOFTDIRTY)
773 vma->vm_flags &= ~VM_SOFTDIRTY;
774
775 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
776}
777
778#else
779
780static inline void clear_soft_dirty(struct vm_area_struct *vma,
781 unsigned long addr, pte_t *pte)
782{
783}
784
785static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
786 unsigned long addr, pmd_t *pmdp)
787{
788}
789#endif
790
a6198797 791static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
2165009b 792 unsigned long end, struct mm_walk *walk)
a6198797 793{
af9de7eb 794 struct clear_refs_private *cp = walk->private;
5c64f52a 795 struct vm_area_struct *vma = walk->vma;
a6198797
MM
796 pte_t *pte, ptent;
797 spinlock_t *ptl;
798 struct page *page;
799
7d5b3bfa
KS
800 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
801 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
802 clear_soft_dirty_pmd(vma, addr, pmd);
803 goto out;
804 }
805
806 page = pmd_page(*pmd);
807
808 /* Clear accessed and referenced bits. */
809 pmdp_test_and_clear_young(vma, addr, pmd);
810 ClearPageReferenced(page);
811out:
812 spin_unlock(ptl);
813 return 0;
814 }
815
1a5a9906
AA
816 if (pmd_trans_unstable(pmd))
817 return 0;
03319327 818
a6198797
MM
819 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
820 for (; addr != end; pte++, addr += PAGE_SIZE) {
821 ptent = *pte;
a6198797 822
0f8975ec
PE
823 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
824 clear_soft_dirty(vma, addr, pte);
825 continue;
826 }
827
179ef71c
CG
828 if (!pte_present(ptent))
829 continue;
830
a6198797
MM
831 page = vm_normal_page(vma, addr, ptent);
832 if (!page)
833 continue;
834
835 /* Clear accessed and referenced bits. */
836 ptep_test_and_clear_young(vma, addr, pte);
837 ClearPageReferenced(page);
838 }
839 pte_unmap_unlock(pte - 1, ptl);
840 cond_resched();
841 return 0;
842}
843
5c64f52a
NH
844static int clear_refs_test_walk(unsigned long start, unsigned long end,
845 struct mm_walk *walk)
846{
847 struct clear_refs_private *cp = walk->private;
848 struct vm_area_struct *vma = walk->vma;
849
48684a65
NH
850 if (vma->vm_flags & VM_PFNMAP)
851 return 1;
852
5c64f52a
NH
853 /*
854 * Writing 1 to /proc/pid/clear_refs affects all pages.
855 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
856 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
857 * Writing 4 to /proc/pid/clear_refs affects all pages.
858 */
859 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
860 return 1;
861 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
862 return 1;
863 return 0;
864}
865
f248dcb3
MM
866static ssize_t clear_refs_write(struct file *file, const char __user *buf,
867 size_t count, loff_t *ppos)
b813e931 868{
f248dcb3 869 struct task_struct *task;
fb92a4b0 870 char buffer[PROC_NUMBUF];
f248dcb3 871 struct mm_struct *mm;
b813e931 872 struct vm_area_struct *vma;
040fa020
PE
873 enum clear_refs_types type;
874 int itype;
0a8cb8e3 875 int rv;
b813e931 876
f248dcb3
MM
877 memset(buffer, 0, sizeof(buffer));
878 if (count > sizeof(buffer) - 1)
879 count = sizeof(buffer) - 1;
880 if (copy_from_user(buffer, buf, count))
881 return -EFAULT;
040fa020 882 rv = kstrtoint(strstrip(buffer), 10, &itype);
0a8cb8e3
AD
883 if (rv < 0)
884 return rv;
040fa020
PE
885 type = (enum clear_refs_types)itype;
886 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
f248dcb3 887 return -EINVAL;
541c237c 888
496ad9aa 889 task = get_proc_task(file_inode(file));
f248dcb3
MM
890 if (!task)
891 return -ESRCH;
892 mm = get_task_mm(task);
893 if (mm) {
af9de7eb 894 struct clear_refs_private cp = {
0f8975ec 895 .type = type,
af9de7eb 896 };
20cbc972
AM
897 struct mm_walk clear_refs_walk = {
898 .pmd_entry = clear_refs_pte_range,
5c64f52a 899 .test_walk = clear_refs_test_walk,
20cbc972 900 .mm = mm,
af9de7eb 901 .private = &cp,
20cbc972 902 };
695f0559
PC
903
904 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
905 /*
906 * Writing 5 to /proc/pid/clear_refs resets the peak
907 * resident set size to this mm's current rss value.
908 */
909 down_write(&mm->mmap_sem);
910 reset_mm_hiwater_rss(mm);
911 up_write(&mm->mmap_sem);
912 goto out_mm;
913 }
914
f248dcb3 915 down_read(&mm->mmap_sem);
64e45507
PF
916 if (type == CLEAR_REFS_SOFT_DIRTY) {
917 for (vma = mm->mmap; vma; vma = vma->vm_next) {
918 if (!(vma->vm_flags & VM_SOFTDIRTY))
919 continue;
920 up_read(&mm->mmap_sem);
921 down_write(&mm->mmap_sem);
922 for (vma = mm->mmap; vma; vma = vma->vm_next) {
923 vma->vm_flags &= ~VM_SOFTDIRTY;
924 vma_set_page_prot(vma);
925 }
926 downgrade_write(&mm->mmap_sem);
927 break;
928 }
0f8975ec 929 mmu_notifier_invalidate_range_start(mm, 0, -1);
64e45507 930 }
5c64f52a 931 walk_page_range(0, ~0UL, &clear_refs_walk);
0f8975ec
PE
932 if (type == CLEAR_REFS_SOFT_DIRTY)
933 mmu_notifier_invalidate_range_end(mm, 0, -1);
f248dcb3
MM
934 flush_tlb_mm(mm);
935 up_read(&mm->mmap_sem);
695f0559 936out_mm:
f248dcb3
MM
937 mmput(mm);
938 }
939 put_task_struct(task);
fb92a4b0
VL
940
941 return count;
b813e931
DR
942}
943
f248dcb3
MM
944const struct file_operations proc_clear_refs_operations = {
945 .write = clear_refs_write,
6038f373 946 .llseek = noop_llseek,
f248dcb3
MM
947};
948
092b50ba
NH
949typedef struct {
950 u64 pme;
951} pagemap_entry_t;
952
85863e47 953struct pagemapread {
8c829622 954 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
092b50ba 955 pagemap_entry_t *buffer;
1c90308e 956 bool show_pfn;
85863e47
MM
957};
958
5aaabe83
NH
959#define PAGEMAP_WALK_SIZE (PMD_SIZE)
960#define PAGEMAP_WALK_MASK (PMD_MASK)
961
deb94544
KK
962#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
963#define PM_PFRAME_BITS 55
964#define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
965#define PM_SOFT_DIRTY BIT_ULL(55)
77bb499b 966#define PM_MMAP_EXCLUSIVE BIT_ULL(56)
deb94544
KK
967#define PM_FILE BIT_ULL(61)
968#define PM_SWAP BIT_ULL(62)
969#define PM_PRESENT BIT_ULL(63)
970
85863e47
MM
971#define PM_END_OF_BUFFER 1
972
deb94544 973static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
092b50ba 974{
deb94544 975 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
092b50ba
NH
976}
977
978static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
85863e47
MM
979 struct pagemapread *pm)
980{
092b50ba 981 pm->buffer[pm->pos++] = *pme;
d82ef020 982 if (pm->pos >= pm->len)
aae8679b 983 return PM_END_OF_BUFFER;
85863e47
MM
984 return 0;
985}
986
987static int pagemap_pte_hole(unsigned long start, unsigned long end,
2165009b 988 struct mm_walk *walk)
85863e47 989{
2165009b 990 struct pagemapread *pm = walk->private;
68b5a652 991 unsigned long addr = start;
85863e47 992 int err = 0;
092b50ba 993
68b5a652
PF
994 while (addr < end) {
995 struct vm_area_struct *vma = find_vma(walk->mm, addr);
deb94544 996 pagemap_entry_t pme = make_pme(0, 0);
87e6d49a
PF
997 /* End of address space hole, which we mark as non-present. */
998 unsigned long hole_end;
68b5a652 999
87e6d49a
PF
1000 if (vma)
1001 hole_end = min(end, vma->vm_start);
1002 else
1003 hole_end = end;
1004
1005 for (; addr < hole_end; addr += PAGE_SIZE) {
1006 err = add_to_pagemap(addr, &pme, pm);
1007 if (err)
1008 goto out;
68b5a652
PF
1009 }
1010
87e6d49a
PF
1011 if (!vma)
1012 break;
1013
1014 /* Addresses in the VMA. */
1015 if (vma->vm_flags & VM_SOFTDIRTY)
deb94544 1016 pme = make_pme(0, PM_SOFT_DIRTY);
87e6d49a 1017 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
68b5a652
PF
1018 err = add_to_pagemap(addr, &pme, pm);
1019 if (err)
1020 goto out;
1021 }
85863e47 1022 }
68b5a652 1023out:
85863e47
MM
1024 return err;
1025}
1026
deb94544 1027static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
052fb0d6 1028 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
85863e47 1029{
deb94544 1030 u64 frame = 0, flags = 0;
052fb0d6 1031 struct page *page = NULL;
85863e47 1032
052fb0d6 1033 if (pte_present(pte)) {
1c90308e
KK
1034 if (pm->show_pfn)
1035 frame = pte_pfn(pte);
deb94544 1036 flags |= PM_PRESENT;
052fb0d6 1037 page = vm_normal_page(vma, addr, pte);
e9cdd6e7 1038 if (pte_soft_dirty(pte))
deb94544 1039 flags |= PM_SOFT_DIRTY;
052fb0d6 1040 } else if (is_swap_pte(pte)) {
179ef71c
CG
1041 swp_entry_t entry;
1042 if (pte_swp_soft_dirty(pte))
deb94544 1043 flags |= PM_SOFT_DIRTY;
179ef71c 1044 entry = pte_to_swp_entry(pte);
052fb0d6
KK
1045 frame = swp_type(entry) |
1046 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
deb94544 1047 flags |= PM_SWAP;
052fb0d6
KK
1048 if (is_migration_entry(entry))
1049 page = migration_entry_to_page(entry);
052fb0d6
KK
1050 }
1051
1052 if (page && !PageAnon(page))
1053 flags |= PM_FILE;
77bb499b
KK
1054 if (page && page_mapcount(page) == 1)
1055 flags |= PM_MMAP_EXCLUSIVE;
deb94544
KK
1056 if (vma->vm_flags & VM_SOFTDIRTY)
1057 flags |= PM_SOFT_DIRTY;
052fb0d6 1058
deb94544 1059 return make_pme(frame, flags);
bcf8039e
DH
1060}
1061
356515e7 1062static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
2165009b 1063 struct mm_walk *walk)
85863e47 1064{
f995ece2 1065 struct vm_area_struct *vma = walk->vma;
2165009b 1066 struct pagemapread *pm = walk->private;
bf929152 1067 spinlock_t *ptl;
05fbf357 1068 pte_t *pte, *orig_pte;
85863e47
MM
1069 int err = 0;
1070
356515e7
KK
1071#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1072 if (pmd_trans_huge_lock(pmdp, vma, &ptl) == 1) {
1073 u64 flags = 0, frame = 0;
1074 pmd_t pmd = *pmdp;
0f8975ec 1075
356515e7 1076 if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(pmd))
deb94544 1077 flags |= PM_SOFT_DIRTY;
d9104d1c 1078
356515e7
KK
1079 /*
1080 * Currently pmd for thp is always present because thp
1081 * can not be swapped-out, migrated, or HWPOISONed
1082 * (split in such cases instead.)
1083 * This if-check is just to prepare for future implementation.
1084 */
1085 if (pmd_present(pmd)) {
77bb499b
KK
1086 struct page *page = pmd_page(pmd);
1087
1088 if (page_mapcount(page) == 1)
1089 flags |= PM_MMAP_EXCLUSIVE;
1090
356515e7 1091 flags |= PM_PRESENT;
1c90308e
KK
1092 if (pm->show_pfn)
1093 frame = pmd_pfn(pmd) +
1094 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
356515e7
KK
1095 }
1096
025c5b24 1097 for (; addr != end; addr += PAGE_SIZE) {
356515e7 1098 pagemap_entry_t pme = make_pme(frame, flags);
025c5b24 1099
092b50ba 1100 err = add_to_pagemap(addr, &pme, pm);
025c5b24
NH
1101 if (err)
1102 break;
1c90308e 1103 if (pm->show_pfn && (flags & PM_PRESENT))
356515e7 1104 frame++;
5aaabe83 1105 }
bf929152 1106 spin_unlock(ptl);
025c5b24 1107 return err;
5aaabe83
NH
1108 }
1109
356515e7 1110 if (pmd_trans_unstable(pmdp))
45f83cef 1111 return 0;
356515e7 1112#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
81d0fa62 1113
f995ece2
NH
1114 /*
1115 * We can assume that @vma always points to a valid one and @end never
1116 * goes beyond vma->vm_end.
1117 */
356515e7 1118 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
f995ece2
NH
1119 for (; addr < end; pte++, addr += PAGE_SIZE) {
1120 pagemap_entry_t pme;
05fbf357 1121
deb94544 1122 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
f995ece2 1123 err = add_to_pagemap(addr, &pme, pm);
05fbf357 1124 if (err)
81d0fa62 1125 break;
85863e47 1126 }
f995ece2 1127 pte_unmap_unlock(orig_pte, ptl);
85863e47
MM
1128
1129 cond_resched();
1130
1131 return err;
1132}
1133
1a5cb814 1134#ifdef CONFIG_HUGETLB_PAGE
116354d1 1135/* This function walks within one hugetlb entry in the single call */
356515e7 1136static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
116354d1
NH
1137 unsigned long addr, unsigned long end,
1138 struct mm_walk *walk)
5dc37642 1139{
5dc37642 1140 struct pagemapread *pm = walk->private;
f995ece2 1141 struct vm_area_struct *vma = walk->vma;
356515e7 1142 u64 flags = 0, frame = 0;
5dc37642 1143 int err = 0;
356515e7 1144 pte_t pte;
5dc37642 1145
f995ece2 1146 if (vma->vm_flags & VM_SOFTDIRTY)
deb94544 1147 flags |= PM_SOFT_DIRTY;
d9104d1c 1148
356515e7
KK
1149 pte = huge_ptep_get(ptep);
1150 if (pte_present(pte)) {
1151 struct page *page = pte_page(pte);
1152
1153 if (!PageAnon(page))
1154 flags |= PM_FILE;
1155
77bb499b
KK
1156 if (page_mapcount(page) == 1)
1157 flags |= PM_MMAP_EXCLUSIVE;
1158
356515e7 1159 flags |= PM_PRESENT;
1c90308e
KK
1160 if (pm->show_pfn)
1161 frame = pte_pfn(pte) +
1162 ((addr & ~hmask) >> PAGE_SHIFT);
356515e7
KK
1163 }
1164
5dc37642 1165 for (; addr != end; addr += PAGE_SIZE) {
356515e7
KK
1166 pagemap_entry_t pme = make_pme(frame, flags);
1167
092b50ba 1168 err = add_to_pagemap(addr, &pme, pm);
5dc37642
NH
1169 if (err)
1170 return err;
1c90308e 1171 if (pm->show_pfn && (flags & PM_PRESENT))
356515e7 1172 frame++;
5dc37642
NH
1173 }
1174
1175 cond_resched();
1176
1177 return err;
1178}
1a5cb814 1179#endif /* HUGETLB_PAGE */
5dc37642 1180
85863e47
MM
1181/*
1182 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1183 *
f16278c6
HR
1184 * For each page in the address space, this file contains one 64-bit entry
1185 * consisting of the following:
1186 *
052fb0d6 1187 * Bits 0-54 page frame number (PFN) if present
f16278c6 1188 * Bits 0-4 swap type if swapped
052fb0d6 1189 * Bits 5-54 swap offset if swapped
deb94544 1190 * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
77bb499b
KK
1191 * Bit 56 page exclusively mapped
1192 * Bits 57-60 zero
052fb0d6 1193 * Bit 61 page is file-page or shared-anon
f16278c6
HR
1194 * Bit 62 page swapped
1195 * Bit 63 page present
1196 *
1197 * If the page is not present but in swap, then the PFN contains an
1198 * encoding of the swap file number and the page's offset into the
1199 * swap. Unmapped pages return a null PFN. This allows determining
85863e47
MM
1200 * precisely which pages are mapped (or in swap) and comparing mapped
1201 * pages between processes.
1202 *
1203 * Efficient users of this interface will use /proc/pid/maps to
1204 * determine which areas of memory are actually mapped and llseek to
1205 * skip over unmapped regions.
1206 */
1207static ssize_t pagemap_read(struct file *file, char __user *buf,
1208 size_t count, loff_t *ppos)
1209{
a06db751 1210 struct mm_struct *mm = file->private_data;
85863e47 1211 struct pagemapread pm;
ee1e6ab6 1212 struct mm_walk pagemap_walk = {};
5d7e0d2b
AM
1213 unsigned long src;
1214 unsigned long svpfn;
1215 unsigned long start_vaddr;
1216 unsigned long end_vaddr;
a06db751 1217 int ret = 0, copied = 0;
85863e47 1218
a06db751 1219 if (!mm || !atomic_inc_not_zero(&mm->mm_users))
85863e47
MM
1220 goto out;
1221
85863e47
MM
1222 ret = -EINVAL;
1223 /* file position must be aligned */
aae8679b 1224 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
a06db751 1225 goto out_mm;
85863e47
MM
1226
1227 ret = 0;
08161786 1228 if (!count)
a06db751 1229 goto out_mm;
08161786 1230
1c90308e
KK
1231 /* do not disclose physical addresses: attack vector */
1232 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1233
8c829622 1234 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1235 pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
5d7e0d2b 1236 ret = -ENOMEM;
d82ef020 1237 if (!pm.buffer)
a06db751 1238 goto out_mm;
85863e47 1239
356515e7 1240 pagemap_walk.pmd_entry = pagemap_pmd_range;
5d7e0d2b 1241 pagemap_walk.pte_hole = pagemap_pte_hole;
1a5cb814 1242#ifdef CONFIG_HUGETLB_PAGE
5dc37642 1243 pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
1a5cb814 1244#endif
5d7e0d2b
AM
1245 pagemap_walk.mm = mm;
1246 pagemap_walk.private = &pm;
1247
1248 src = *ppos;
1249 svpfn = src / PM_ENTRY_BYTES;
1250 start_vaddr = svpfn << PAGE_SHIFT;
a06db751 1251 end_vaddr = mm->task_size;
5d7e0d2b
AM
1252
1253 /* watch out for wraparound */
a06db751 1254 if (svpfn > mm->task_size >> PAGE_SHIFT)
5d7e0d2b
AM
1255 start_vaddr = end_vaddr;
1256
1257 /*
1258 * The odds are that this will stop walking way
1259 * before end_vaddr, because the length of the
1260 * user buffer is tracked in "pm", and the walk
1261 * will stop when we hit the end of the buffer.
1262 */
d82ef020
KH
1263 ret = 0;
1264 while (count && (start_vaddr < end_vaddr)) {
1265 int len;
1266 unsigned long end;
1267
1268 pm.pos = 0;
ea251c1d 1269 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
d82ef020
KH
1270 /* overflow ? */
1271 if (end < start_vaddr || end > end_vaddr)
1272 end = end_vaddr;
1273 down_read(&mm->mmap_sem);
1274 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
1275 up_read(&mm->mmap_sem);
1276 start_vaddr = end;
1277
1278 len = min(count, PM_ENTRY_BYTES * pm.pos);
309361e0 1279 if (copy_to_user(buf, pm.buffer, len)) {
d82ef020 1280 ret = -EFAULT;
a06db751 1281 goto out_free;
d82ef020
KH
1282 }
1283 copied += len;
1284 buf += len;
1285 count -= len;
85863e47 1286 }
d82ef020
KH
1287 *ppos += copied;
1288 if (!ret || ret == PM_END_OF_BUFFER)
1289 ret = copied;
1290
98bc93e5
KM
1291out_free:
1292 kfree(pm.buffer);
a06db751
KK
1293out_mm:
1294 mmput(mm);
85863e47
MM
1295out:
1296 return ret;
1297}
1298
541c237c
PE
1299static int pagemap_open(struct inode *inode, struct file *file)
1300{
a06db751
KK
1301 struct mm_struct *mm;
1302
a06db751
KK
1303 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1304 if (IS_ERR(mm))
1305 return PTR_ERR(mm);
1306 file->private_data = mm;
1307 return 0;
1308}
1309
1310static int pagemap_release(struct inode *inode, struct file *file)
1311{
1312 struct mm_struct *mm = file->private_data;
1313
1314 if (mm)
1315 mmdrop(mm);
541c237c
PE
1316 return 0;
1317}
1318
85863e47
MM
1319const struct file_operations proc_pagemap_operations = {
1320 .llseek = mem_lseek, /* borrow this */
1321 .read = pagemap_read,
541c237c 1322 .open = pagemap_open,
a06db751 1323 .release = pagemap_release,
85863e47 1324};
1e883281 1325#endif /* CONFIG_PROC_PAGE_MONITOR */
85863e47 1326
6e21c8f1 1327#ifdef CONFIG_NUMA
6e21c8f1 1328
f69ff943 1329struct numa_maps {
f69ff943
SW
1330 unsigned long pages;
1331 unsigned long anon;
1332 unsigned long active;
1333 unsigned long writeback;
1334 unsigned long mapcount_max;
1335 unsigned long dirty;
1336 unsigned long swapcache;
1337 unsigned long node[MAX_NUMNODES];
1338};
1339
5b52fc89
SW
1340struct numa_maps_private {
1341 struct proc_maps_private proc_maps;
1342 struct numa_maps md;
1343};
1344
eb4866d0
DH
1345static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1346 unsigned long nr_pages)
f69ff943
SW
1347{
1348 int count = page_mapcount(page);
1349
eb4866d0 1350 md->pages += nr_pages;
f69ff943 1351 if (pte_dirty || PageDirty(page))
eb4866d0 1352 md->dirty += nr_pages;
f69ff943
SW
1353
1354 if (PageSwapCache(page))
eb4866d0 1355 md->swapcache += nr_pages;
f69ff943
SW
1356
1357 if (PageActive(page) || PageUnevictable(page))
eb4866d0 1358 md->active += nr_pages;
f69ff943
SW
1359
1360 if (PageWriteback(page))
eb4866d0 1361 md->writeback += nr_pages;
f69ff943
SW
1362
1363 if (PageAnon(page))
eb4866d0 1364 md->anon += nr_pages;
f69ff943
SW
1365
1366 if (count > md->mapcount_max)
1367 md->mapcount_max = count;
1368
eb4866d0 1369 md->node[page_to_nid(page)] += nr_pages;
f69ff943
SW
1370}
1371
3200a8aa
DH
1372static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1373 unsigned long addr)
1374{
1375 struct page *page;
1376 int nid;
1377
1378 if (!pte_present(pte))
1379 return NULL;
1380
1381 page = vm_normal_page(vma, addr, pte);
1382 if (!page)
1383 return NULL;
1384
1385 if (PageReserved(page))
1386 return NULL;
1387
1388 nid = page_to_nid(page);
4ff1b2c2 1389 if (!node_isset(nid, node_states[N_MEMORY]))
3200a8aa
DH
1390 return NULL;
1391
1392 return page;
1393}
1394
f69ff943
SW
1395static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1396 unsigned long end, struct mm_walk *walk)
1397{
d85f4d6d
NH
1398 struct numa_maps *md = walk->private;
1399 struct vm_area_struct *vma = walk->vma;
f69ff943
SW
1400 spinlock_t *ptl;
1401 pte_t *orig_pte;
1402 pte_t *pte;
1403
d85f4d6d 1404 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
025c5b24
NH
1405 pte_t huge_pte = *(pte_t *)pmd;
1406 struct page *page;
1407
d85f4d6d 1408 page = can_gather_numa_stats(huge_pte, vma, addr);
025c5b24
NH
1409 if (page)
1410 gather_stats(page, md, pte_dirty(huge_pte),
1411 HPAGE_PMD_SIZE/PAGE_SIZE);
bf929152 1412 spin_unlock(ptl);
025c5b24 1413 return 0;
32ef4384
DH
1414 }
1415
1a5a9906
AA
1416 if (pmd_trans_unstable(pmd))
1417 return 0;
f69ff943
SW
1418 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1419 do {
d85f4d6d 1420 struct page *page = can_gather_numa_stats(*pte, vma, addr);
f69ff943
SW
1421 if (!page)
1422 continue;
eb4866d0 1423 gather_stats(page, md, pte_dirty(*pte), 1);
f69ff943
SW
1424
1425 } while (pte++, addr += PAGE_SIZE, addr != end);
1426 pte_unmap_unlock(orig_pte, ptl);
1427 return 0;
1428}
1429#ifdef CONFIG_HUGETLB_PAGE
632fd60f 1430static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
f69ff943
SW
1431 unsigned long addr, unsigned long end, struct mm_walk *walk)
1432{
1433 struct numa_maps *md;
1434 struct page *page;
1435
d4c54919 1436 if (!pte_present(*pte))
f69ff943
SW
1437 return 0;
1438
1439 page = pte_page(*pte);
1440 if (!page)
1441 return 0;
1442
1443 md = walk->private;
eb4866d0 1444 gather_stats(page, md, pte_dirty(*pte), 1);
f69ff943
SW
1445 return 0;
1446}
1447
1448#else
632fd60f 1449static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
f69ff943
SW
1450 unsigned long addr, unsigned long end, struct mm_walk *walk)
1451{
1452 return 0;
1453}
1454#endif
1455
1456/*
1457 * Display pages allocated per node and memory policy via /proc.
1458 */
b7643757 1459static int show_numa_map(struct seq_file *m, void *v, int is_pid)
f69ff943 1460{
5b52fc89
SW
1461 struct numa_maps_private *numa_priv = m->private;
1462 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
f69ff943 1463 struct vm_area_struct *vma = v;
5b52fc89 1464 struct numa_maps *md = &numa_priv->md;
f69ff943
SW
1465 struct file *file = vma->vm_file;
1466 struct mm_struct *mm = vma->vm_mm;
d85f4d6d
NH
1467 struct mm_walk walk = {
1468 .hugetlb_entry = gather_hugetlb_stats,
1469 .pmd_entry = gather_pte_stats,
1470 .private = md,
1471 .mm = mm,
1472 };
f69ff943 1473 struct mempolicy *pol;
948927ee
DR
1474 char buffer[64];
1475 int nid;
f69ff943
SW
1476
1477 if (!mm)
1478 return 0;
1479
5b52fc89
SW
1480 /* Ensure we start with an empty set of numa_maps statistics. */
1481 memset(md, 0, sizeof(*md));
f69ff943 1482
498f2371
ON
1483 pol = __get_vma_policy(vma, vma->vm_start);
1484 if (pol) {
1485 mpol_to_str(buffer, sizeof(buffer), pol);
1486 mpol_cond_put(pol);
1487 } else {
1488 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1489 }
f69ff943
SW
1490
1491 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1492
1493 if (file) {
17c2b4ee 1494 seq_puts(m, " file=");
2726d566 1495 seq_file_path(m, file, "\n\t= ");
f69ff943 1496 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
17c2b4ee 1497 seq_puts(m, " heap");
b7643757 1498 } else {
58cb6548 1499 pid_t tid = pid_of_stack(proc_priv, vma, is_pid);
b7643757
SP
1500 if (tid != 0) {
1501 /*
1502 * Thread stack in /proc/PID/task/TID/maps or
1503 * the main process stack.
1504 */
1505 if (!is_pid || (vma->vm_start <= mm->start_stack &&
1506 vma->vm_end >= mm->start_stack))
17c2b4ee 1507 seq_puts(m, " stack");
b7643757
SP
1508 else
1509 seq_printf(m, " stack:%d", tid);
1510 }
f69ff943
SW
1511 }
1512
fc360bd9 1513 if (is_vm_hugetlb_page(vma))
17c2b4ee 1514 seq_puts(m, " huge");
fc360bd9 1515
d85f4d6d
NH
1516 /* mmap_sem is held by m_start */
1517 walk_page_vma(vma, &walk);
f69ff943
SW
1518
1519 if (!md->pages)
1520 goto out;
1521
1522 if (md->anon)
1523 seq_printf(m, " anon=%lu", md->anon);
1524
1525 if (md->dirty)
1526 seq_printf(m, " dirty=%lu", md->dirty);
1527
1528 if (md->pages != md->anon && md->pages != md->dirty)
1529 seq_printf(m, " mapped=%lu", md->pages);
1530
1531 if (md->mapcount_max > 1)
1532 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1533
1534 if (md->swapcache)
1535 seq_printf(m, " swapcache=%lu", md->swapcache);
1536
1537 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1538 seq_printf(m, " active=%lu", md->active);
1539
1540 if (md->writeback)
1541 seq_printf(m, " writeback=%lu", md->writeback);
1542
948927ee
DR
1543 for_each_node_state(nid, N_MEMORY)
1544 if (md->node[nid])
1545 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
198d1597
RA
1546
1547 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
f69ff943
SW
1548out:
1549 seq_putc(m, '\n');
b8c20a9b 1550 m_cache_vma(m, vma);
f69ff943
SW
1551 return 0;
1552}
5b52fc89 1553
b7643757
SP
1554static int show_pid_numa_map(struct seq_file *m, void *v)
1555{
1556 return show_numa_map(m, v, 1);
1557}
1558
1559static int show_tid_numa_map(struct seq_file *m, void *v)
1560{
1561 return show_numa_map(m, v, 0);
1562}
1563
03a44825 1564static const struct seq_operations proc_pid_numa_maps_op = {
b7643757
SP
1565 .start = m_start,
1566 .next = m_next,
1567 .stop = m_stop,
1568 .show = show_pid_numa_map,
6e21c8f1 1569};
662795de 1570
b7643757
SP
1571static const struct seq_operations proc_tid_numa_maps_op = {
1572 .start = m_start,
1573 .next = m_next,
1574 .stop = m_stop,
1575 .show = show_tid_numa_map,
1576};
1577
1578static int numa_maps_open(struct inode *inode, struct file *file,
1579 const struct seq_operations *ops)
662795de 1580{
4db7d0ee
ON
1581 return proc_maps_open(inode, file, ops,
1582 sizeof(struct numa_maps_private));
662795de
EB
1583}
1584
b7643757
SP
1585static int pid_numa_maps_open(struct inode *inode, struct file *file)
1586{
1587 return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
1588}
1589
1590static int tid_numa_maps_open(struct inode *inode, struct file *file)
1591{
1592 return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
1593}
1594
1595const struct file_operations proc_pid_numa_maps_operations = {
1596 .open = pid_numa_maps_open,
1597 .read = seq_read,
1598 .llseek = seq_lseek,
29a40ace 1599 .release = proc_map_release,
b7643757
SP
1600};
1601
1602const struct file_operations proc_tid_numa_maps_operations = {
1603 .open = tid_numa_maps_open,
662795de
EB
1604 .read = seq_read,
1605 .llseek = seq_lseek,
29a40ace 1606 .release = proc_map_release,
662795de 1607};
f69ff943 1608#endif /* CONFIG_NUMA */
This page took 0.873004 seconds and 5 git commands to generate.