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