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