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