]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/proc/task_mmu.c
UBUNTU: SAUCE: AUFS
[mirror_ubuntu-jammy-kernel.git] / fs / proc / task_mmu.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
a520110e 2#include <linux/pagewalk.h>
615d6e87 3#include <linux/vmacache.h>
1da177e4 4#include <linux/hugetlb.h>
22e057c5 5#include <linux/huge_mm.h>
1da177e4
LT
6#include <linux/mount.h>
7#include <linux/seq_file.h>
e070ad49 8#include <linux/highmem.h>
5096add8 9#include <linux/ptrace.h>
5a0e3ad6 10#include <linux/slab.h>
6e21c8f1
CL
11#include <linux/pagemap.h>
12#include <linux/mempolicy.h>
22e057c5 13#include <linux/rmap.h>
85863e47 14#include <linux/swap.h>
6e84f315 15#include <linux/sched/mm.h>
85863e47 16#include <linux/swapops.h>
0f8975ec 17#include <linux/mmu_notifier.h>
33c3fc71 18#include <linux/page_idle.h>
6a15a370 19#include <linux/shmem_fs.h>
b3a81d08 20#include <linux/uaccess.h>
27cca866 21#include <linux/pkeys.h>
e070ad49 22
1da177e4 23#include <asm/elf.h>
b3a81d08 24#include <asm/tlb.h>
e070ad49 25#include <asm/tlbflush.h>
1da177e4
LT
26#include "internal.h"
27
d1be35cb
AV
28#define SEQ_PUT_DEC(str, val) \
29 seq_put_decimal_ull_width(m, str, (val) << (PAGE_SHIFT-10), 8)
df5f8314 30void task_mem(struct seq_file *m, struct mm_struct *mm)
1da177e4 31{
af5b0f6a 32 unsigned long text, lib, swap, anon, file, shmem;
365e9c87
HD
33 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
34
8cee852e
JM
35 anon = get_mm_counter(mm, MM_ANONPAGES);
36 file = get_mm_counter(mm, MM_FILEPAGES);
37 shmem = get_mm_counter(mm, MM_SHMEMPAGES);
38
365e9c87
HD
39 /*
40 * Note: to minimize their overhead, mm maintains hiwater_vm and
41 * hiwater_rss only when about to *lower* total_vm or rss. Any
42 * collector of these hiwater stats must therefore get total_vm
43 * and rss too, which will usually be the higher. Barriers? not
44 * worth the effort, such snapshots can always be inconsistent.
45 */
46 hiwater_vm = total_vm = mm->total_vm;
47 if (hiwater_vm < mm->hiwater_vm)
48 hiwater_vm = mm->hiwater_vm;
8cee852e 49 hiwater_rss = total_rss = anon + file + shmem;
365e9c87
HD
50 if (hiwater_rss < mm->hiwater_rss)
51 hiwater_rss = mm->hiwater_rss;
1da177e4 52
8526d84f
KK
53 /* split executable areas between text and lib */
54 text = PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK);
55 text = min(text, mm->exec_vm << PAGE_SHIFT);
56 lib = (mm->exec_vm << PAGE_SHIFT) - text;
57
b084d435 58 swap = get_mm_counter(mm, MM_SWAPENTS);
d1be35cb
AV
59 SEQ_PUT_DEC("VmPeak:\t", hiwater_vm);
60 SEQ_PUT_DEC(" kB\nVmSize:\t", total_vm);
61 SEQ_PUT_DEC(" kB\nVmLck:\t", mm->locked_vm);
70f8a3ca 62 SEQ_PUT_DEC(" kB\nVmPin:\t", atomic64_read(&mm->pinned_vm));
d1be35cb
AV
63 SEQ_PUT_DEC(" kB\nVmHWM:\t", hiwater_rss);
64 SEQ_PUT_DEC(" kB\nVmRSS:\t", total_rss);
65 SEQ_PUT_DEC(" kB\nRssAnon:\t", anon);
66 SEQ_PUT_DEC(" kB\nRssFile:\t", file);
67 SEQ_PUT_DEC(" kB\nRssShmem:\t", shmem);
68 SEQ_PUT_DEC(" kB\nVmData:\t", mm->data_vm);
69 SEQ_PUT_DEC(" kB\nVmStk:\t", mm->stack_vm);
70 seq_put_decimal_ull_width(m,
71 " kB\nVmExe:\t", text >> 10, 8);
72 seq_put_decimal_ull_width(m,
73 " kB\nVmLib:\t", lib >> 10, 8);
74 seq_put_decimal_ull_width(m,
75 " kB\nVmPTE:\t", mm_pgtables_bytes(mm) >> 10, 8);
76 SEQ_PUT_DEC(" kB\nVmSwap:\t", swap);
77 seq_puts(m, " kB\n");
5d317b2b 78 hugetlb_report_usage(m, mm);
1da177e4 79}
d1be35cb 80#undef SEQ_PUT_DEC
1da177e4
LT
81
82unsigned long task_vsize(struct mm_struct *mm)
83{
84 return PAGE_SIZE * mm->total_vm;
85}
86
a2ade7b6
AD
87unsigned long task_statm(struct mm_struct *mm,
88 unsigned long *shared, unsigned long *text,
89 unsigned long *data, unsigned long *resident)
1da177e4 90{
eca56ff9
JM
91 *shared = get_mm_counter(mm, MM_FILEPAGES) +
92 get_mm_counter(mm, MM_SHMEMPAGES);
1da177e4
LT
93 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
94 >> PAGE_SHIFT;
84638335 95 *data = mm->data_vm + mm->stack_vm;
d559db08 96 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
1da177e4
LT
97 return mm->total_vm;
98}
99
9e781440
KH
100#ifdef CONFIG_NUMA
101/*
498f2371 102 * Save get_task_policy() for show_numa_map().
9e781440
KH
103 */
104static void hold_task_mempolicy(struct proc_maps_private *priv)
105{
106 struct task_struct *task = priv->task;
107
108 task_lock(task);
498f2371 109 priv->task_mempolicy = get_task_policy(task);
9e781440
KH
110 mpol_get(priv->task_mempolicy);
111 task_unlock(task);
112}
113static void release_task_mempolicy(struct proc_maps_private *priv)
114{
115 mpol_put(priv->task_mempolicy);
116}
117#else
118static void hold_task_mempolicy(struct proc_maps_private *priv)
119{
120}
121static void release_task_mempolicy(struct proc_maps_private *priv)
122{
123}
124#endif
125
0c255321 126static void *m_start(struct seq_file *m, loff_t *ppos)
e070ad49 127{
a6198797 128 struct proc_maps_private *priv = m->private;
4781f2c3 129 unsigned long last_addr = *ppos;
a6198797 130 struct mm_struct *mm;
0c255321 131 struct vm_area_struct *vma;
a6198797 132
c2e88d22 133 /* See m_next(). Zero at the start or after lseek. */
b8c20a9b
ON
134 if (last_addr == -1UL)
135 return NULL;
136
2c03376d 137 priv->task = get_proc_task(priv->inode);
a6198797 138 if (!priv->task)
ec6fd8a4 139 return ERR_PTR(-ESRCH);
a6198797 140
29a40ace 141 mm = priv->mm;
d07ded61
MWO
142 if (!mm || !mmget_not_zero(mm)) {
143 put_task_struct(priv->task);
144 priv->task = NULL;
29a40ace 145 return NULL;
d07ded61 146 }
a6198797 147
d8ed45c5 148 if (mmap_read_lock_killable(mm)) {
8a713e7d 149 mmput(mm);
d07ded61
MWO
150 put_task_struct(priv->task);
151 priv->task = NULL;
8a713e7d
KK
152 return ERR_PTR(-EINTR);
153 }
154
9e781440 155 hold_task_mempolicy(priv);
0c255321 156 priv->tail_vma = get_gate_vma(mm);
a6198797 157
c2e88d22
MWO
158 vma = find_vma(mm, last_addr);
159 if (vma)
a6198797 160 return vma;
59b4bf12 161
c2e88d22 162 return priv->tail_vma;
a6198797
MM
163}
164
4781f2c3 165static void *m_next(struct seq_file *m, void *v, loff_t *ppos)
a6198797
MM
166{
167 struct proc_maps_private *priv = m->private;
fad95500
MWO
168 struct vm_area_struct *next, *vma = v;
169
170 if (vma == priv->tail_vma)
171 next = NULL;
172 else if (vma->vm_next)
173 next = vma->vm_next;
174 else
175 next = priv->tail_vma;
a6198797 176
4781f2c3 177 *ppos = next ? next->vm_start : -1UL;
c2e88d22 178
59b4bf12 179 return next;
a6198797
MM
180}
181
182static void m_stop(struct seq_file *m, void *v)
183{
184 struct proc_maps_private *priv = m->private;
d07ded61 185 struct mm_struct *mm = priv->mm;
a6198797 186
d07ded61
MWO
187 if (!priv->task)
188 return;
189
190 release_task_mempolicy(priv);
d8ed45c5 191 mmap_read_unlock(mm);
d07ded61
MWO
192 mmput(mm);
193 put_task_struct(priv->task);
194 priv->task = NULL;
a6198797
MM
195}
196
4db7d0ee
ON
197static int proc_maps_open(struct inode *inode, struct file *file,
198 const struct seq_operations *ops, int psize)
199{
200 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
201
202 if (!priv)
203 return -ENOMEM;
204
2c03376d 205 priv->inode = inode;
29a40ace
ON
206 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
207 if (IS_ERR(priv->mm)) {
208 int err = PTR_ERR(priv->mm);
209
210 seq_release_private(inode, file);
211 return err;
212 }
213
4db7d0ee
ON
214 return 0;
215}
216
29a40ace
ON
217static int proc_map_release(struct inode *inode, struct file *file)
218{
219 struct seq_file *seq = file->private_data;
220 struct proc_maps_private *priv = seq->private;
221
222 if (priv->mm)
223 mmdrop(priv->mm);
224
225 return seq_release_private(inode, file);
226}
227
a6198797 228static int do_maps_open(struct inode *inode, struct file *file,
03a44825 229 const struct seq_operations *ops)
a6198797 230{
4db7d0ee
ON
231 return proc_maps_open(inode, file, ops,
232 sizeof(struct proc_maps_private));
a6198797 233}
e070ad49 234
65376df5
JW
235/*
236 * Indicate if the VMA is a stack for the given task; for
237 * /proc/PID/maps that is the stack of the main task.
238 */
1240ea0d 239static int is_stack(struct vm_area_struct *vma)
58cb6548 240{
b18cb64e
AL
241 /*
242 * We make no effort to guess what a given thread considers to be
243 * its "stack". It's not even well-defined for programs written
244 * languages like Go.
245 */
246 return vma->vm_start <= vma->vm_mm->start_stack &&
247 vma->vm_end >= vma->vm_mm->start_stack;
58cb6548
ON
248}
249
493b0e9d
DC
250static void show_vma_header_prefix(struct seq_file *m,
251 unsigned long start, unsigned long end,
252 vm_flags_t flags, unsigned long long pgoff,
253 dev_t dev, unsigned long ino)
254{
255 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
0e3dc019
AV
256 seq_put_hex_ll(m, NULL, start, 8);
257 seq_put_hex_ll(m, "-", end, 8);
258 seq_putc(m, ' ');
259 seq_putc(m, flags & VM_READ ? 'r' : '-');
260 seq_putc(m, flags & VM_WRITE ? 'w' : '-');
261 seq_putc(m, flags & VM_EXEC ? 'x' : '-');
262 seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p');
263 seq_put_hex_ll(m, " ", pgoff, 8);
264 seq_put_hex_ll(m, " ", MAJOR(dev), 2);
265 seq_put_hex_ll(m, ":", MINOR(dev), 2);
266 seq_put_decimal_ull(m, " ", ino);
267 seq_putc(m, ' ');
493b0e9d
DC
268}
269
b7643757 270static void
871305bb 271show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
1da177e4 272{
e070ad49
ML
273 struct mm_struct *mm = vma->vm_mm;
274 struct file *file = vma->vm_file;
ca16d140 275 vm_flags_t flags = vma->vm_flags;
1da177e4 276 unsigned long ino = 0;
6260a4b0 277 unsigned long long pgoff = 0;
a09a79f6 278 unsigned long start, end;
1da177e4 279 dev_t dev = 0;
b7643757 280 const char *name = NULL;
1da177e4
LT
281
282 if (file) {
7fea21ae
AR
283 struct inode *inode;
284
285 file = vma_pr_or_file(vma);
286 inode = file_inode(file);
1da177e4
LT
287 dev = inode->i_sb->s_dev;
288 ino = inode->i_ino;
6260a4b0 289 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
1da177e4
LT
290 }
291
d7824370 292 start = vma->vm_start;
a09a79f6 293 end = vma->vm_end;
493b0e9d 294 show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
1da177e4
LT
295
296 /*
297 * Print the dentry name for named mappings, and a
298 * special [heap] marker for the heap:
299 */
e070ad49 300 if (file) {
652586df 301 seq_pad(m, ' ');
2726d566 302 seq_file_path(m, file, "\n");
b7643757
SP
303 goto done;
304 }
305
78d683e8
AL
306 if (vma->vm_ops && vma->vm_ops->name) {
307 name = vma->vm_ops->name(vma);
308 if (name)
309 goto done;
310 }
311
b7643757
SP
312 name = arch_vma_name(vma);
313 if (!name) {
b7643757
SP
314 if (!mm) {
315 name = "[vdso]";
316 goto done;
317 }
318
319 if (vma->vm_start <= mm->brk &&
320 vma->vm_end >= mm->start_brk) {
321 name = "[heap]";
322 goto done;
323 }
324
1240ea0d 325 if (is_stack(vma))
65376df5 326 name = "[stack]";
b7643757
SP
327 }
328
329done:
330 if (name) {
652586df 331 seq_pad(m, ' ');
b7643757 332 seq_puts(m, name);
1da177e4
LT
333 }
334 seq_putc(m, '\n');
7c88db0c
JK
335}
336
871305bb 337static int show_map(struct seq_file *m, void *v)
7c88db0c 338{
871305bb 339 show_map_vma(m, v);
1da177e4
LT
340 return 0;
341}
342
03a44825 343static const struct seq_operations proc_pid_maps_op = {
a6198797
MM
344 .start = m_start,
345 .next = m_next,
346 .stop = m_stop,
871305bb 347 .show = show_map
a6198797
MM
348};
349
b7643757 350static int pid_maps_open(struct inode *inode, struct file *file)
a6198797
MM
351{
352 return do_maps_open(inode, file, &proc_pid_maps_op);
353}
354
b7643757
SP
355const struct file_operations proc_pid_maps_operations = {
356 .open = pid_maps_open,
357 .read = seq_read,
358 .llseek = seq_lseek,
29a40ace 359 .release = proc_map_release,
b7643757
SP
360};
361
a6198797
MM
362/*
363 * Proportional Set Size(PSS): my share of RSS.
364 *
365 * PSS of a process is the count of pages it has in memory, where each
366 * page is divided by the number of processes sharing it. So if a
367 * process has 1000 pages all to itself, and 1000 shared with one other
368 * process, its PSS will be 1500.
369 *
370 * To keep (accumulated) division errors low, we adopt a 64bit
371 * fixed-point pss counter to minimize division errors. So (pss >>
372 * PSS_SHIFT) would be the real byte count.
373 *
374 * A shift of 12 before division means (assuming 4K page size):
375 * - 1M 3-user-pages add up to 8KB errors;
376 * - supports mapcount up to 2^24, or 16M;
377 * - supports PSS up to 2^52 bytes, or 4PB.
378 */
379#define PSS_SHIFT 12
380
1e883281 381#ifdef CONFIG_PROC_PAGE_MONITOR
214e471f 382struct mem_size_stats {
a6198797
MM
383 unsigned long resident;
384 unsigned long shared_clean;
385 unsigned long shared_dirty;
386 unsigned long private_clean;
387 unsigned long private_dirty;
388 unsigned long referenced;
b40d4f84 389 unsigned long anonymous;
cf8496ea 390 unsigned long lazyfree;
4031a219 391 unsigned long anonymous_thp;
65c45377 392 unsigned long shmem_thp;
60fbf0ab 393 unsigned long file_thp;
214e471f 394 unsigned long swap;
25ee01a2
NH
395 unsigned long shared_hugetlb;
396 unsigned long private_hugetlb;
a6198797 397 u64 pss;
ee2ad71b
LS
398 u64 pss_anon;
399 u64 pss_file;
400 u64 pss_shmem;
493b0e9d 401 u64 pss_locked;
8334b962 402 u64 swap_pss;
c261e7d9 403 bool check_shmem_swap;
a6198797
MM
404};
405
ee2ad71b
LS
406static void smaps_page_accumulate(struct mem_size_stats *mss,
407 struct page *page, unsigned long size, unsigned long pss,
408 bool dirty, bool locked, bool private)
409{
410 mss->pss += pss;
411
412 if (PageAnon(page))
413 mss->pss_anon += pss;
414 else if (PageSwapBacked(page))
415 mss->pss_shmem += pss;
416 else
417 mss->pss_file += pss;
418
419 if (locked)
420 mss->pss_locked += pss;
421
422 if (dirty || PageDirty(page)) {
423 if (private)
424 mss->private_dirty += size;
425 else
426 mss->shared_dirty += size;
427 } else {
428 if (private)
429 mss->private_clean += size;
430 else
431 mss->shared_clean += size;
432 }
433}
434
c164e038 435static void smaps_account(struct mem_size_stats *mss, struct page *page,
27dd768e 436 bool compound, bool young, bool dirty, bool locked)
c164e038 437{
d8c6546b 438 int i, nr = compound ? compound_nr(page) : 1;
afd9883f 439 unsigned long size = nr * PAGE_SIZE;
c164e038 440
ee2ad71b
LS
441 /*
442 * First accumulate quantities that depend only on |size| and the type
443 * of the compound page.
444 */
cf8496ea 445 if (PageAnon(page)) {
c164e038 446 mss->anonymous += size;
cf8496ea
SL
447 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
448 mss->lazyfree += size;
449 }
c164e038
KS
450
451 mss->resident += size;
452 /* Accumulate the size in pages that have been accessed. */
33c3fc71 453 if (young || page_is_young(page) || PageReferenced(page))
c164e038 454 mss->referenced += size;
c164e038 455
afd9883f 456 /*
ee2ad71b
LS
457 * Then accumulate quantities that may depend on sharing, or that may
458 * differ page-by-page.
459 *
afd9883f
KS
460 * page_count(page) == 1 guarantees the page is mapped exactly once.
461 * If any subpage of the compound page mapped with PTE it would elevate
462 * page_count().
463 */
464 if (page_count(page) == 1) {
ee2ad71b
LS
465 smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty,
466 locked, true);
afd9883f
KS
467 return;
468 }
afd9883f
KS
469 for (i = 0; i < nr; i++, page++) {
470 int mapcount = page_mapcount(page);
ee2ad71b
LS
471 unsigned long pss = PAGE_SIZE << PSS_SHIFT;
472 if (mapcount >= 2)
473 pss /= mapcount;
474 smaps_page_accumulate(mss, page, PAGE_SIZE, pss, dirty, locked,
475 mapcount < 2);
c164e038
KS
476 }
477}
ae11c4d9 478
c261e7d9 479#ifdef CONFIG_SHMEM
c261e7d9 480static int smaps_pte_hole(unsigned long addr, unsigned long end,
b7a16c7a 481 __always_unused int depth, struct mm_walk *walk)
c261e7d9
VB
482{
483 struct mem_size_stats *mss = walk->private;
484
48131e03
VB
485 mss->swap += shmem_partial_swap_usage(
486 walk->vma->vm_file->f_mapping, addr, end);
c261e7d9
VB
487
488 return 0;
489}
7b86ac33
CH
490#else
491#define smaps_pte_hole NULL
492#endif /* CONFIG_SHMEM */
c261e7d9 493
c164e038
KS
494static void smaps_pte_entry(pte_t *pte, unsigned long addr,
495 struct mm_walk *walk)
ae11c4d9
DH
496{
497 struct mem_size_stats *mss = walk->private;
14eb6fdd 498 struct vm_area_struct *vma = walk->vma;
27dd768e 499 bool locked = !!(vma->vm_flags & VM_LOCKED);
b1d4d9e0 500 struct page *page = NULL;
ae11c4d9 501
c164e038
KS
502 if (pte_present(*pte)) {
503 page = vm_normal_page(vma, addr, *pte);
504 } else if (is_swap_pte(*pte)) {
505 swp_entry_t swpent = pte_to_swp_entry(*pte);
ae11c4d9 506
8334b962
MK
507 if (!non_swap_entry(swpent)) {
508 int mapcount;
509
c164e038 510 mss->swap += PAGE_SIZE;
8334b962
MK
511 mapcount = swp_swapcount(swpent);
512 if (mapcount >= 2) {
513 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
514
515 do_div(pss_delta, mapcount);
516 mss->swap_pss += pss_delta;
517 } else {
518 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
519 }
af5cdaf8
AP
520 } else if (is_pfn_swap_entry(swpent))
521 page = pfn_swap_entry_to_page(swpent);
c261e7d9
VB
522 } else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
523 && pte_none(*pte))) {
8cf88646 524 page = xa_load(&vma->vm_file->f_mapping->i_pages,
48131e03 525 linear_page_index(vma, addr));
3159f943 526 if (xa_is_value(page))
48131e03 527 mss->swap += PAGE_SIZE;
48131e03 528 return;
b1d4d9e0 529 }
ae11c4d9 530
ae11c4d9
DH
531 if (!page)
532 return;
afd9883f 533
27dd768e 534 smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte), locked);
ae11c4d9
DH
535}
536
c164e038
KS
537#ifdef CONFIG_TRANSPARENT_HUGEPAGE
538static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
539 struct mm_walk *walk)
540{
541 struct mem_size_stats *mss = walk->private;
14eb6fdd 542 struct vm_area_struct *vma = walk->vma;
27dd768e 543 bool locked = !!(vma->vm_flags & VM_LOCKED);
c94b6923
HY
544 struct page *page = NULL;
545
546 if (pmd_present(*pmd)) {
547 /* FOLL_DUMP will return -EFAULT on huge zero page */
548 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
549 } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) {
550 swp_entry_t entry = pmd_to_swp_entry(*pmd);
c164e038 551
c94b6923 552 if (is_migration_entry(entry))
af5cdaf8 553 page = pfn_swap_entry_to_page(entry);
c94b6923 554 }
c164e038
KS
555 if (IS_ERR_OR_NULL(page))
556 return;
65c45377
KS
557 if (PageAnon(page))
558 mss->anonymous_thp += HPAGE_PMD_SIZE;
559 else if (PageSwapBacked(page))
560 mss->shmem_thp += HPAGE_PMD_SIZE;
ca120cf6
DW
561 else if (is_zone_device_page(page))
562 /* pass */;
65c45377 563 else
60fbf0ab 564 mss->file_thp += HPAGE_PMD_SIZE;
27dd768e 565 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), locked);
c164e038
KS
566}
567#else
568static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
569 struct mm_walk *walk)
570{
571}
572#endif
573
b3ae5acb 574static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
2165009b 575 struct mm_walk *walk)
e070ad49 576{
14eb6fdd 577 struct vm_area_struct *vma = walk->vma;
ae11c4d9 578 pte_t *pte;
705e87c0 579 spinlock_t *ptl;
e070ad49 580
b6ec57f4
KS
581 ptl = pmd_trans_huge_lock(pmd, vma);
582 if (ptl) {
c94b6923 583 smaps_pmd_entry(pmd, addr, walk);
bf929152 584 spin_unlock(ptl);
14038302 585 goto out;
22e057c5 586 }
1a5a9906
AA
587
588 if (pmd_trans_unstable(pmd))
14038302 589 goto out;
22e057c5 590 /*
c1e8d7c6 591 * The mmap_lock held all the way back in m_start() is what
22e057c5
DH
592 * keeps khugepaged out of here and from collapsing things
593 * in here.
594 */
705e87c0 595 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
ae11c4d9 596 for (; addr != end; pte++, addr += PAGE_SIZE)
c164e038 597 smaps_pte_entry(pte, addr, walk);
705e87c0 598 pte_unmap_unlock(pte - 1, ptl);
14038302 599out:
705e87c0 600 cond_resched();
b3ae5acb 601 return 0;
e070ad49
ML
602}
603
834f82e2
CG
604static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
605{
606 /*
607 * Don't forget to update Documentation/ on changes.
608 */
609 static const char mnemonics[BITS_PER_LONG][2] = {
610 /*
611 * In case if we meet a flag we don't know about.
612 */
613 [0 ... (BITS_PER_LONG-1)] = "??",
614
615 [ilog2(VM_READ)] = "rd",
616 [ilog2(VM_WRITE)] = "wr",
617 [ilog2(VM_EXEC)] = "ex",
618 [ilog2(VM_SHARED)] = "sh",
619 [ilog2(VM_MAYREAD)] = "mr",
620 [ilog2(VM_MAYWRITE)] = "mw",
621 [ilog2(VM_MAYEXEC)] = "me",
622 [ilog2(VM_MAYSHARE)] = "ms",
623 [ilog2(VM_GROWSDOWN)] = "gd",
624 [ilog2(VM_PFNMAP)] = "pf",
834f82e2
CG
625 [ilog2(VM_LOCKED)] = "lo",
626 [ilog2(VM_IO)] = "io",
627 [ilog2(VM_SEQ_READ)] = "sr",
628 [ilog2(VM_RAND_READ)] = "rr",
629 [ilog2(VM_DONTCOPY)] = "dc",
630 [ilog2(VM_DONTEXPAND)] = "de",
631 [ilog2(VM_ACCOUNT)] = "ac",
632 [ilog2(VM_NORESERVE)] = "nr",
633 [ilog2(VM_HUGETLB)] = "ht",
b6fb293f 634 [ilog2(VM_SYNC)] = "sf",
834f82e2 635 [ilog2(VM_ARCH_1)] = "ar",
d2cd9ede 636 [ilog2(VM_WIPEONFORK)] = "wf",
834f82e2 637 [ilog2(VM_DONTDUMP)] = "dd",
424037b7
DK
638#ifdef CONFIG_ARM64_BTI
639 [ilog2(VM_ARM64_BTI)] = "bt",
640#endif
ec8e41ae
NH
641#ifdef CONFIG_MEM_SOFT_DIRTY
642 [ilog2(VM_SOFTDIRTY)] = "sd",
643#endif
834f82e2
CG
644 [ilog2(VM_MIXEDMAP)] = "mm",
645 [ilog2(VM_HUGEPAGE)] = "hg",
646 [ilog2(VM_NOHUGEPAGE)] = "nh",
647 [ilog2(VM_MERGEABLE)] = "mg",
16ba6f81
AA
648 [ilog2(VM_UFFD_MISSING)]= "um",
649 [ilog2(VM_UFFD_WP)] = "uw",
9f341931
CM
650#ifdef CONFIG_ARM64_MTE
651 [ilog2(VM_MTE)] = "mt",
652 [ilog2(VM_MTE_ALLOWED)] = "",
653#endif
5212213a 654#ifdef CONFIG_ARCH_HAS_PKEYS
c1192f84
DH
655 /* These come out via ProtectionKey: */
656 [ilog2(VM_PKEY_BIT0)] = "",
657 [ilog2(VM_PKEY_BIT1)] = "",
658 [ilog2(VM_PKEY_BIT2)] = "",
659 [ilog2(VM_PKEY_BIT3)] = "",
2c9e0a6f
RP
660#if VM_PKEY_BIT4
661 [ilog2(VM_PKEY_BIT4)] = "",
c1192f84 662#endif
5212213a 663#endif /* CONFIG_ARCH_HAS_PKEYS */
7677f7fd
AR
664#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
665 [ilog2(VM_UFFD_MINOR)] = "ui",
666#endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
834f82e2
CG
667 };
668 size_t i;
669
670 seq_puts(m, "VmFlags: ");
671 for (i = 0; i < BITS_PER_LONG; i++) {
c1192f84
DH
672 if (!mnemonics[i][0])
673 continue;
834f82e2 674 if (vma->vm_flags & (1UL << i)) {
f6640663
AV
675 seq_putc(m, mnemonics[i][0]);
676 seq_putc(m, mnemonics[i][1]);
677 seq_putc(m, ' ');
834f82e2
CG
678 }
679 }
680 seq_putc(m, '\n');
681}
682
25ee01a2
NH
683#ifdef CONFIG_HUGETLB_PAGE
684static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
685 unsigned long addr, unsigned long end,
686 struct mm_walk *walk)
687{
688 struct mem_size_stats *mss = walk->private;
689 struct vm_area_struct *vma = walk->vma;
690 struct page *page = NULL;
691
692 if (pte_present(*pte)) {
693 page = vm_normal_page(vma, addr, *pte);
694 } else if (is_swap_pte(*pte)) {
695 swp_entry_t swpent = pte_to_swp_entry(*pte);
696
af5cdaf8
AP
697 if (is_pfn_swap_entry(swpent))
698 page = pfn_swap_entry_to_page(swpent);
25ee01a2
NH
699 }
700 if (page) {
701 int mapcount = page_mapcount(page);
702
703 if (mapcount >= 2)
704 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
705 else
706 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
707 }
708 return 0;
709}
7b86ac33
CH
710#else
711#define smaps_hugetlb_range NULL
25ee01a2
NH
712#endif /* HUGETLB_PAGE */
713
7b86ac33
CH
714static const struct mm_walk_ops smaps_walk_ops = {
715 .pmd_entry = smaps_pte_range,
716 .hugetlb_entry = smaps_hugetlb_range,
717};
718
719static const struct mm_walk_ops smaps_shmem_walk_ops = {
720 .pmd_entry = smaps_pte_range,
721 .hugetlb_entry = smaps_hugetlb_range,
722 .pte_hole = smaps_pte_hole,
723};
724
03b4b114
CC
725/*
726 * Gather mem stats from @vma with the indicated beginning
727 * address @start, and keep them in @mss.
728 *
729 * Use vm_start of @vma as the beginning address if @start is 0.
730 */
8e68d689 731static void smap_gather_stats(struct vm_area_struct *vma,
03b4b114 732 struct mem_size_stats *mss, unsigned long start)
e070ad49 733{
03b4b114
CC
734 const struct mm_walk_ops *ops = &smaps_walk_ops;
735
736 /* Invalid start */
737 if (start >= vma->vm_end)
738 return;
739
c261e7d9 740#ifdef CONFIG_SHMEM
fa76da46
VB
741 /* In case of smaps_rollup, reset the value from previous vma */
742 mss->check_shmem_swap = false;
c261e7d9 743 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
6a15a370
VB
744 /*
745 * For shared or readonly shmem mappings we know that all
746 * swapped out pages belong to the shmem object, and we can
747 * obtain the swap value much more efficiently. For private
748 * writable mappings, we might have COW pages that are
749 * not affected by the parent swapped out pages of the shmem
750 * object, so we have to distinguish them during the page walk.
751 * Unless we know that the shmem object (or the part mapped by
752 * our VMA) has no swapped out pages at all.
753 */
754 unsigned long shmem_swapped = shmem_swap_usage(vma);
755
03b4b114
CC
756 if (!start && (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
757 !(vma->vm_flags & VM_WRITE))) {
fa76da46 758 mss->swap += shmem_swapped;
6a15a370 759 } else {
493b0e9d 760 mss->check_shmem_swap = true;
03b4b114 761 ops = &smaps_shmem_walk_ops;
6a15a370 762 }
c261e7d9
VB
763 }
764#endif
c1e8d7c6 765 /* mmap_lock is held in m_start */
03b4b114
CC
766 if (!start)
767 walk_page_vma(vma, ops, mss);
768 else
769 walk_page_range(vma->vm_mm, start, vma->vm_end, ops, mss);
8e68d689
VB
770}
771
772#define SEQ_PUT_DEC(str, val) \
773 seq_put_decimal_ull_width(m, str, (val) >> 10, 8)
f1547959
VB
774
775/* Show the contents common for smaps and smaps_rollup */
ee2ad71b
LS
776static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss,
777 bool rollup_mode)
f1547959
VB
778{
779 SEQ_PUT_DEC("Rss: ", mss->resident);
780 SEQ_PUT_DEC(" kB\nPss: ", mss->pss >> PSS_SHIFT);
ee2ad71b
LS
781 if (rollup_mode) {
782 /*
783 * These are meaningful only for smaps_rollup, otherwise two of
784 * them are zero, and the other one is the same as Pss.
785 */
786 SEQ_PUT_DEC(" kB\nPss_Anon: ",
787 mss->pss_anon >> PSS_SHIFT);
788 SEQ_PUT_DEC(" kB\nPss_File: ",
789 mss->pss_file >> PSS_SHIFT);
790 SEQ_PUT_DEC(" kB\nPss_Shmem: ",
791 mss->pss_shmem >> PSS_SHIFT);
792 }
f1547959
VB
793 SEQ_PUT_DEC(" kB\nShared_Clean: ", mss->shared_clean);
794 SEQ_PUT_DEC(" kB\nShared_Dirty: ", mss->shared_dirty);
795 SEQ_PUT_DEC(" kB\nPrivate_Clean: ", mss->private_clean);
796 SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty);
797 SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced);
798 SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous);
799 SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree);
800 SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp);
801 SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp);
471e78cc 802 SEQ_PUT_DEC(" kB\nFilePmdMapped: ", mss->file_thp);
f1547959
VB
803 SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb);
804 seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ",
805 mss->private_hugetlb >> 10, 7);
806 SEQ_PUT_DEC(" kB\nSwap: ", mss->swap);
807 SEQ_PUT_DEC(" kB\nSwapPss: ",
808 mss->swap_pss >> PSS_SHIFT);
809 SEQ_PUT_DEC(" kB\nLocked: ",
810 mss->pss_locked >> PSS_SHIFT);
811 seq_puts(m, " kB\n");
812}
813
8e68d689
VB
814static int show_smap(struct seq_file *m, void *v)
815{
8e68d689 816 struct vm_area_struct *vma = v;
258f669e
VB
817 struct mem_size_stats mss;
818
819 memset(&mss, 0, sizeof(mss));
820
03b4b114 821 smap_gather_stats(vma, &mss, 0);
258f669e
VB
822
823 show_map_vma(m, vma);
824
825 SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start);
826 SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma));
827 SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma));
828 seq_puts(m, " kB\n");
829
ee2ad71b 830 __show_smap(m, &mss, false);
258f669e 831
471e78cc 832 seq_printf(m, "THPeligible: %d\n",
e6be37b2 833 transparent_hugepage_active(vma));
7635d9cb 834
258f669e
VB
835 if (arch_pkeys_enabled())
836 seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma));
837 show_smap_vma_flags(m, vma);
838
258f669e
VB
839 return 0;
840}
841
842static int show_smaps_rollup(struct seq_file *m, void *v)
843{
844 struct proc_maps_private *priv = m->private;
845 struct mem_size_stats mss;
846 struct mm_struct *mm;
847 struct vm_area_struct *vma;
848 unsigned long last_vma_end = 0;
8e68d689 849 int ret = 0;
8e68d689 850
258f669e
VB
851 priv->task = get_proc_task(priv->inode);
852 if (!priv->task)
853 return -ESRCH;
493b0e9d 854
258f669e
VB
855 mm = priv->mm;
856 if (!mm || !mmget_not_zero(mm)) {
857 ret = -ESRCH;
858 goto out_put_task;
493b0e9d 859 }
4752c369 860
258f669e 861 memset(&mss, 0, sizeof(mss));
493b0e9d 862
d8ed45c5 863 ret = mmap_read_lock_killable(mm);
a26a9781
KK
864 if (ret)
865 goto out_put_mm;
866
258f669e 867 hold_task_mempolicy(priv);
f1547959 868
ff9f47f6 869 for (vma = priv->mm->mmap; vma;) {
03b4b114 870 smap_gather_stats(vma, &mss, 0);
258f669e 871 last_vma_end = vma->vm_end;
ff9f47f6
CC
872
873 /*
874 * Release mmap_lock temporarily if someone wants to
875 * access it for write request.
876 */
877 if (mmap_lock_is_contended(mm)) {
878 mmap_read_unlock(mm);
879 ret = mmap_read_lock_killable(mm);
880 if (ret) {
881 release_task_mempolicy(priv);
882 goto out_put_mm;
883 }
884
885 /*
886 * After dropping the lock, there are four cases to
887 * consider. See the following example for explanation.
888 *
889 * +------+------+-----------+
890 * | VMA1 | VMA2 | VMA3 |
891 * +------+------+-----------+
892 * | | | |
893 * 4k 8k 16k 400k
894 *
895 * Suppose we drop the lock after reading VMA2 due to
896 * contention, then we get:
897 *
898 * last_vma_end = 16k
899 *
900 * 1) VMA2 is freed, but VMA3 exists:
901 *
902 * find_vma(mm, 16k - 1) will return VMA3.
903 * In this case, just continue from VMA3.
904 *
905 * 2) VMA2 still exists:
906 *
907 * find_vma(mm, 16k - 1) will return VMA2.
908 * Iterate the loop like the original one.
909 *
910 * 3) No more VMAs can be found:
911 *
912 * find_vma(mm, 16k - 1) will return NULL.
913 * No more things to do, just break.
914 *
915 * 4) (last_vma_end - 1) is the middle of a vma (VMA'):
916 *
917 * find_vma(mm, 16k - 1) will return VMA' whose range
918 * contains last_vma_end.
919 * Iterate VMA' from last_vma_end.
920 */
921 vma = find_vma(mm, last_vma_end - 1);
922 /* Case 3 above */
923 if (!vma)
924 break;
925
926 /* Case 1 above */
927 if (vma->vm_start >= last_vma_end)
928 continue;
929
930 /* Case 4 above */
931 if (vma->vm_end > last_vma_end)
932 smap_gather_stats(vma, &mss, last_vma_end);
933 }
934 /* Case 2 above */
935 vma = vma->vm_next;
493b0e9d 936 }
258f669e
VB
937
938 show_vma_header_prefix(m, priv->mm->mmap->vm_start,
939 last_vma_end, 0, 0, 0, 0);
940 seq_pad(m, ' ');
941 seq_puts(m, "[rollup]\n");
942
ee2ad71b 943 __show_smap(m, &mss, true);
258f669e
VB
944
945 release_task_mempolicy(priv);
d8ed45c5 946 mmap_read_unlock(mm);
258f669e 947
a26a9781
KK
948out_put_mm:
949 mmput(mm);
258f669e
VB
950out_put_task:
951 put_task_struct(priv->task);
952 priv->task = NULL;
953
493b0e9d 954 return ret;
e070ad49 955}
d1be35cb 956#undef SEQ_PUT_DEC
e070ad49 957
03a44825 958static const struct seq_operations proc_pid_smaps_op = {
a6198797
MM
959 .start = m_start,
960 .next = m_next,
961 .stop = m_stop,
871305bb 962 .show = show_smap
a6198797
MM
963};
964
b7643757 965static int pid_smaps_open(struct inode *inode, struct file *file)
a6198797
MM
966{
967 return do_maps_open(inode, file, &proc_pid_smaps_op);
968}
969
258f669e 970static int smaps_rollup_open(struct inode *inode, struct file *file)
493b0e9d 971{
258f669e 972 int ret;
493b0e9d 973 struct proc_maps_private *priv;
258f669e
VB
974
975 priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT);
976 if (!priv)
493b0e9d 977 return -ENOMEM;
258f669e
VB
978
979 ret = single_open(file, show_smaps_rollup, priv);
980 if (ret)
981 goto out_free;
982
983 priv->inode = inode;
984 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
985 if (IS_ERR(priv->mm)) {
986 ret = PTR_ERR(priv->mm);
987
988 single_release(inode, file);
989 goto out_free;
493b0e9d 990 }
258f669e 991
493b0e9d 992 return 0;
258f669e
VB
993
994out_free:
995 kfree(priv);
996 return ret;
997}
998
999static int smaps_rollup_release(struct inode *inode, struct file *file)
1000{
1001 struct seq_file *seq = file->private_data;
1002 struct proc_maps_private *priv = seq->private;
1003
1004 if (priv->mm)
1005 mmdrop(priv->mm);
1006
1007 kfree(priv);
1008 return single_release(inode, file);
493b0e9d
DC
1009}
1010
b7643757
SP
1011const struct file_operations proc_pid_smaps_operations = {
1012 .open = pid_smaps_open,
1013 .read = seq_read,
1014 .llseek = seq_lseek,
29a40ace 1015 .release = proc_map_release,
b7643757
SP
1016};
1017
493b0e9d 1018const struct file_operations proc_pid_smaps_rollup_operations = {
258f669e 1019 .open = smaps_rollup_open,
493b0e9d
DC
1020 .read = seq_read,
1021 .llseek = seq_lseek,
258f669e 1022 .release = smaps_rollup_release,
493b0e9d
DC
1023};
1024
040fa020
PE
1025enum clear_refs_types {
1026 CLEAR_REFS_ALL = 1,
1027 CLEAR_REFS_ANON,
1028 CLEAR_REFS_MAPPED,
0f8975ec 1029 CLEAR_REFS_SOFT_DIRTY,
695f0559 1030 CLEAR_REFS_MM_HIWATER_RSS,
040fa020
PE
1031 CLEAR_REFS_LAST,
1032};
1033
af9de7eb 1034struct clear_refs_private {
0f8975ec 1035 enum clear_refs_types type;
af9de7eb
PE
1036};
1037
7d5b3bfa 1038#ifdef CONFIG_MEM_SOFT_DIRTY
9348b73c 1039
9348b73c
LT
1040static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1041{
1042 struct page *page;
1043
1044 if (!pte_write(pte))
1045 return false;
1046 if (!is_cow_mapping(vma->vm_flags))
1047 return false;
a458b76a 1048 if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags)))
9348b73c
LT
1049 return false;
1050 page = vm_normal_page(vma, addr, pte);
1051 if (!page)
1052 return false;
1053 return page_maybe_dma_pinned(page);
1054}
1055
0f8975ec
PE
1056static inline void clear_soft_dirty(struct vm_area_struct *vma,
1057 unsigned long addr, pte_t *pte)
1058{
0f8975ec
PE
1059 /*
1060 * The soft-dirty tracker uses #PF-s to catch writes
1061 * to pages, so write-protect the pte as well. See the
1ad1335d 1062 * Documentation/admin-guide/mm/soft-dirty.rst for full description
0f8975ec
PE
1063 * of how soft-dirty works.
1064 */
1065 pte_t ptent = *pte;
179ef71c
CG
1066
1067 if (pte_present(ptent)) {
04a86453
AK
1068 pte_t old_pte;
1069
9348b73c
LT
1070 if (pte_is_pinned(vma, addr, ptent))
1071 return;
04a86453
AK
1072 old_pte = ptep_modify_prot_start(vma, addr, pte);
1073 ptent = pte_wrprotect(old_pte);
a7b76174 1074 ptent = pte_clear_soft_dirty(ptent);
04a86453 1075 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
179ef71c
CG
1076 } else if (is_swap_pte(ptent)) {
1077 ptent = pte_swp_clear_soft_dirty(ptent);
326c2597 1078 set_pte_at(vma->vm_mm, addr, pte, ptent);
179ef71c 1079 }
0f8975ec 1080}
5d3875a0
LD
1081#else
1082static inline void clear_soft_dirty(struct vm_area_struct *vma,
1083 unsigned long addr, pte_t *pte)
1084{
1085}
1086#endif
0f8975ec 1087
5d3875a0 1088#if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
7d5b3bfa
KS
1089static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1090 unsigned long addr, pmd_t *pmdp)
1091{
a3cf988f 1092 pmd_t old, pmd = *pmdp;
5b7abeae 1093
ab6e3d09
NH
1094 if (pmd_present(pmd)) {
1095 /* See comment in change_huge_pmd() */
a3cf988f
KS
1096 old = pmdp_invalidate(vma, addr, pmdp);
1097 if (pmd_dirty(old))
ab6e3d09 1098 pmd = pmd_mkdirty(pmd);
a3cf988f 1099 if (pmd_young(old))
ab6e3d09
NH
1100 pmd = pmd_mkyoung(pmd);
1101
1102 pmd = pmd_wrprotect(pmd);
1103 pmd = pmd_clear_soft_dirty(pmd);
1104
1105 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1106 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) {
1107 pmd = pmd_swp_clear_soft_dirty(pmd);
1108 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1109 }
7d5b3bfa 1110}
7d5b3bfa 1111#else
7d5b3bfa
KS
1112static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1113 unsigned long addr, pmd_t *pmdp)
1114{
1115}
1116#endif
1117
a6198797 1118static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
2165009b 1119 unsigned long end, struct mm_walk *walk)
a6198797 1120{
af9de7eb 1121 struct clear_refs_private *cp = walk->private;
5c64f52a 1122 struct vm_area_struct *vma = walk->vma;
a6198797
MM
1123 pte_t *pte, ptent;
1124 spinlock_t *ptl;
1125 struct page *page;
1126
b6ec57f4
KS
1127 ptl = pmd_trans_huge_lock(pmd, vma);
1128 if (ptl) {
7d5b3bfa
KS
1129 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1130 clear_soft_dirty_pmd(vma, addr, pmd);
1131 goto out;
1132 }
1133
84c3fc4e
ZY
1134 if (!pmd_present(*pmd))
1135 goto out;
1136
7d5b3bfa
KS
1137 page = pmd_page(*pmd);
1138
1139 /* Clear accessed and referenced bits. */
1140 pmdp_test_and_clear_young(vma, addr, pmd);
33c3fc71 1141 test_and_clear_page_young(page);
7d5b3bfa
KS
1142 ClearPageReferenced(page);
1143out:
1144 spin_unlock(ptl);
1145 return 0;
1146 }
1147
1a5a9906
AA
1148 if (pmd_trans_unstable(pmd))
1149 return 0;
03319327 1150
a6198797
MM
1151 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1152 for (; addr != end; pte++, addr += PAGE_SIZE) {
1153 ptent = *pte;
a6198797 1154
0f8975ec
PE
1155 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1156 clear_soft_dirty(vma, addr, pte);
1157 continue;
1158 }
1159
179ef71c
CG
1160 if (!pte_present(ptent))
1161 continue;
1162
a6198797
MM
1163 page = vm_normal_page(vma, addr, ptent);
1164 if (!page)
1165 continue;
1166
1167 /* Clear accessed and referenced bits. */
1168 ptep_test_and_clear_young(vma, addr, pte);
33c3fc71 1169 test_and_clear_page_young(page);
a6198797
MM
1170 ClearPageReferenced(page);
1171 }
1172 pte_unmap_unlock(pte - 1, ptl);
1173 cond_resched();
1174 return 0;
1175}
1176
5c64f52a
NH
1177static int clear_refs_test_walk(unsigned long start, unsigned long end,
1178 struct mm_walk *walk)
1179{
1180 struct clear_refs_private *cp = walk->private;
1181 struct vm_area_struct *vma = walk->vma;
1182
48684a65
NH
1183 if (vma->vm_flags & VM_PFNMAP)
1184 return 1;
1185
5c64f52a
NH
1186 /*
1187 * Writing 1 to /proc/pid/clear_refs affects all pages.
1188 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
1189 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
1190 * Writing 4 to /proc/pid/clear_refs affects all pages.
1191 */
1192 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
1193 return 1;
1194 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1195 return 1;
1196 return 0;
1197}
1198
7b86ac33
CH
1199static const struct mm_walk_ops clear_refs_walk_ops = {
1200 .pmd_entry = clear_refs_pte_range,
1201 .test_walk = clear_refs_test_walk,
1202};
1203
f248dcb3
MM
1204static ssize_t clear_refs_write(struct file *file, const char __user *buf,
1205 size_t count, loff_t *ppos)
b813e931 1206{
f248dcb3 1207 struct task_struct *task;
fb92a4b0 1208 char buffer[PROC_NUMBUF];
f248dcb3 1209 struct mm_struct *mm;
b813e931 1210 struct vm_area_struct *vma;
040fa020
PE
1211 enum clear_refs_types type;
1212 int itype;
0a8cb8e3 1213 int rv;
b813e931 1214
f248dcb3
MM
1215 memset(buffer, 0, sizeof(buffer));
1216 if (count > sizeof(buffer) - 1)
1217 count = sizeof(buffer) - 1;
1218 if (copy_from_user(buffer, buf, count))
1219 return -EFAULT;
040fa020 1220 rv = kstrtoint(strstrip(buffer), 10, &itype);
0a8cb8e3
AD
1221 if (rv < 0)
1222 return rv;
040fa020
PE
1223 type = (enum clear_refs_types)itype;
1224 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
f248dcb3 1225 return -EINVAL;
541c237c 1226
496ad9aa 1227 task = get_proc_task(file_inode(file));
f248dcb3
MM
1228 if (!task)
1229 return -ESRCH;
1230 mm = get_task_mm(task);
1231 if (mm) {
ac46d4f3 1232 struct mmu_notifier_range range;
af9de7eb 1233 struct clear_refs_private cp = {
0f8975ec 1234 .type = type,
af9de7eb 1235 };
695f0559 1236
29a951df
LT
1237 if (mmap_write_lock_killable(mm)) {
1238 count = -EINTR;
1239 goto out_mm;
1240 }
695f0559
PC
1241 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
1242 /*
1243 * Writing 5 to /proc/pid/clear_refs resets the peak
1244 * resident set size to this mm's current rss value.
1245 */
695f0559 1246 reset_mm_hiwater_rss(mm);
29a951df 1247 goto out_unlock;
695f0559
PC
1248 }
1249
64e45507
PF
1250 if (type == CLEAR_REFS_SOFT_DIRTY) {
1251 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1252 if (!(vma->vm_flags & VM_SOFTDIRTY))
1253 continue;
29a951df
LT
1254 vma->vm_flags &= ~VM_SOFTDIRTY;
1255 vma_set_page_prot(vma);
64e45507 1256 }
ac46d4f3 1257
912efa17 1258 inc_tlb_flush_pending(mm);
7269f999
JG
1259 mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY,
1260 0, NULL, mm, 0, -1UL);
ac46d4f3 1261 mmu_notifier_invalidate_range_start(&range);
64e45507 1262 }
7b86ac33
CH
1263 walk_page_range(mm, 0, mm->highest_vm_end, &clear_refs_walk_ops,
1264 &cp);
912efa17 1265 if (type == CLEAR_REFS_SOFT_DIRTY) {
ac46d4f3 1266 mmu_notifier_invalidate_range_end(&range);
912efa17
WD
1267 flush_tlb_mm(mm);
1268 dec_tlb_flush_pending(mm);
1269 }
29a951df
LT
1270out_unlock:
1271 mmap_write_unlock(mm);
695f0559 1272out_mm:
f248dcb3
MM
1273 mmput(mm);
1274 }
1275 put_task_struct(task);
fb92a4b0
VL
1276
1277 return count;
b813e931
DR
1278}
1279
f248dcb3
MM
1280const struct file_operations proc_clear_refs_operations = {
1281 .write = clear_refs_write,
6038f373 1282 .llseek = noop_llseek,
f248dcb3
MM
1283};
1284
092b50ba
NH
1285typedef struct {
1286 u64 pme;
1287} pagemap_entry_t;
1288
85863e47 1289struct pagemapread {
8c829622 1290 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
092b50ba 1291 pagemap_entry_t *buffer;
1c90308e 1292 bool show_pfn;
85863e47
MM
1293};
1294
5aaabe83
NH
1295#define PAGEMAP_WALK_SIZE (PMD_SIZE)
1296#define PAGEMAP_WALK_MASK (PMD_MASK)
1297
deb94544
KK
1298#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1299#define PM_PFRAME_BITS 55
1300#define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1301#define PM_SOFT_DIRTY BIT_ULL(55)
77bb499b 1302#define PM_MMAP_EXCLUSIVE BIT_ULL(56)
fb8e37f3 1303#define PM_UFFD_WP BIT_ULL(57)
deb94544
KK
1304#define PM_FILE BIT_ULL(61)
1305#define PM_SWAP BIT_ULL(62)
1306#define PM_PRESENT BIT_ULL(63)
1307
85863e47
MM
1308#define PM_END_OF_BUFFER 1
1309
deb94544 1310static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
092b50ba 1311{
deb94544 1312 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
092b50ba
NH
1313}
1314
1315static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
85863e47
MM
1316 struct pagemapread *pm)
1317{
092b50ba 1318 pm->buffer[pm->pos++] = *pme;
d82ef020 1319 if (pm->pos >= pm->len)
aae8679b 1320 return PM_END_OF_BUFFER;
85863e47
MM
1321 return 0;
1322}
1323
1324static int pagemap_pte_hole(unsigned long start, unsigned long end,
b7a16c7a 1325 __always_unused int depth, struct mm_walk *walk)
85863e47 1326{
2165009b 1327 struct pagemapread *pm = walk->private;
68b5a652 1328 unsigned long addr = start;
85863e47 1329 int err = 0;
092b50ba 1330
68b5a652
PF
1331 while (addr < end) {
1332 struct vm_area_struct *vma = find_vma(walk->mm, addr);
deb94544 1333 pagemap_entry_t pme = make_pme(0, 0);
87e6d49a
PF
1334 /* End of address space hole, which we mark as non-present. */
1335 unsigned long hole_end;
68b5a652 1336
87e6d49a
PF
1337 if (vma)
1338 hole_end = min(end, vma->vm_start);
1339 else
1340 hole_end = end;
1341
1342 for (; addr < hole_end; addr += PAGE_SIZE) {
1343 err = add_to_pagemap(addr, &pme, pm);
1344 if (err)
1345 goto out;
68b5a652
PF
1346 }
1347
87e6d49a
PF
1348 if (!vma)
1349 break;
1350
1351 /* Addresses in the VMA. */
1352 if (vma->vm_flags & VM_SOFTDIRTY)
deb94544 1353 pme = make_pme(0, PM_SOFT_DIRTY);
87e6d49a 1354 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
68b5a652
PF
1355 err = add_to_pagemap(addr, &pme, pm);
1356 if (err)
1357 goto out;
1358 }
85863e47 1359 }
68b5a652 1360out:
85863e47
MM
1361 return err;
1362}
1363
deb94544 1364static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
052fb0d6 1365 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
85863e47 1366{
deb94544 1367 u64 frame = 0, flags = 0;
052fb0d6 1368 struct page *page = NULL;
85863e47 1369
052fb0d6 1370 if (pte_present(pte)) {
1c90308e
KK
1371 if (pm->show_pfn)
1372 frame = pte_pfn(pte);
deb94544 1373 flags |= PM_PRESENT;
25b2995a 1374 page = vm_normal_page(vma, addr, pte);
e9cdd6e7 1375 if (pte_soft_dirty(pte))
deb94544 1376 flags |= PM_SOFT_DIRTY;
fb8e37f3
PX
1377 if (pte_uffd_wp(pte))
1378 flags |= PM_UFFD_WP;
052fb0d6 1379 } else if (is_swap_pte(pte)) {
179ef71c
CG
1380 swp_entry_t entry;
1381 if (pte_swp_soft_dirty(pte))
deb94544 1382 flags |= PM_SOFT_DIRTY;
fb8e37f3
PX
1383 if (pte_swp_uffd_wp(pte))
1384 flags |= PM_UFFD_WP;
179ef71c 1385 entry = pte_to_swp_entry(pte);
ab6ecf24
HY
1386 if (pm->show_pfn)
1387 frame = swp_type(entry) |
1388 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
deb94544 1389 flags |= PM_SWAP;
af5cdaf8
AP
1390 if (is_pfn_swap_entry(entry))
1391 page = pfn_swap_entry_to_page(entry);
052fb0d6
KK
1392 }
1393
1394 if (page && !PageAnon(page))
1395 flags |= PM_FILE;
77bb499b
KK
1396 if (page && page_mapcount(page) == 1)
1397 flags |= PM_MMAP_EXCLUSIVE;
deb94544
KK
1398 if (vma->vm_flags & VM_SOFTDIRTY)
1399 flags |= PM_SOFT_DIRTY;
052fb0d6 1400
deb94544 1401 return make_pme(frame, flags);
bcf8039e
DH
1402}
1403
356515e7 1404static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
2165009b 1405 struct mm_walk *walk)
85863e47 1406{
f995ece2 1407 struct vm_area_struct *vma = walk->vma;
2165009b 1408 struct pagemapread *pm = walk->private;
bf929152 1409 spinlock_t *ptl;
05fbf357 1410 pte_t *pte, *orig_pte;
85863e47
MM
1411 int err = 0;
1412
356515e7 1413#ifdef CONFIG_TRANSPARENT_HUGEPAGE
b6ec57f4
KS
1414 ptl = pmd_trans_huge_lock(pmdp, vma);
1415 if (ptl) {
356515e7
KK
1416 u64 flags = 0, frame = 0;
1417 pmd_t pmd = *pmdp;
84c3fc4e 1418 struct page *page = NULL;
0f8975ec 1419
b83d7e43 1420 if (vma->vm_flags & VM_SOFTDIRTY)
deb94544 1421 flags |= PM_SOFT_DIRTY;
d9104d1c 1422
356515e7 1423 if (pmd_present(pmd)) {
84c3fc4e 1424 page = pmd_page(pmd);
77bb499b 1425
356515e7 1426 flags |= PM_PRESENT;
b83d7e43
HY
1427 if (pmd_soft_dirty(pmd))
1428 flags |= PM_SOFT_DIRTY;
fb8e37f3
PX
1429 if (pmd_uffd_wp(pmd))
1430 flags |= PM_UFFD_WP;
1c90308e
KK
1431 if (pm->show_pfn)
1432 frame = pmd_pfn(pmd) +
1433 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
356515e7 1434 }
84c3fc4e
ZY
1435#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1436 else if (is_swap_pmd(pmd)) {
1437 swp_entry_t entry = pmd_to_swp_entry(pmd);
ab6ecf24 1438 unsigned long offset;
84c3fc4e 1439
ab6ecf24
HY
1440 if (pm->show_pfn) {
1441 offset = swp_offset(entry) +
1442 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1443 frame = swp_type(entry) |
1444 (offset << MAX_SWAPFILES_SHIFT);
1445 }
84c3fc4e 1446 flags |= PM_SWAP;
b83d7e43
HY
1447 if (pmd_swp_soft_dirty(pmd))
1448 flags |= PM_SOFT_DIRTY;
fb8e37f3
PX
1449 if (pmd_swp_uffd_wp(pmd))
1450 flags |= PM_UFFD_WP;
84c3fc4e 1451 VM_BUG_ON(!is_pmd_migration_entry(pmd));
af5cdaf8 1452 page = pfn_swap_entry_to_page(entry);
84c3fc4e
ZY
1453 }
1454#endif
1455
1456 if (page && page_mapcount(page) == 1)
1457 flags |= PM_MMAP_EXCLUSIVE;
356515e7 1458
025c5b24 1459 for (; addr != end; addr += PAGE_SIZE) {
356515e7 1460 pagemap_entry_t pme = make_pme(frame, flags);
025c5b24 1461
092b50ba 1462 err = add_to_pagemap(addr, &pme, pm);
025c5b24
NH
1463 if (err)
1464 break;
ab6ecf24
HY
1465 if (pm->show_pfn) {
1466 if (flags & PM_PRESENT)
1467 frame++;
1468 else if (flags & PM_SWAP)
1469 frame += (1 << MAX_SWAPFILES_SHIFT);
1470 }
5aaabe83 1471 }
bf929152 1472 spin_unlock(ptl);
025c5b24 1473 return err;
5aaabe83
NH
1474 }
1475
356515e7 1476 if (pmd_trans_unstable(pmdp))
45f83cef 1477 return 0;
356515e7 1478#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
81d0fa62 1479
f995ece2
NH
1480 /*
1481 * We can assume that @vma always points to a valid one and @end never
1482 * goes beyond vma->vm_end.
1483 */
356515e7 1484 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
f995ece2
NH
1485 for (; addr < end; pte++, addr += PAGE_SIZE) {
1486 pagemap_entry_t pme;
05fbf357 1487
deb94544 1488 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
f995ece2 1489 err = add_to_pagemap(addr, &pme, pm);
05fbf357 1490 if (err)
81d0fa62 1491 break;
85863e47 1492 }
f995ece2 1493 pte_unmap_unlock(orig_pte, ptl);
85863e47
MM
1494
1495 cond_resched();
1496
1497 return err;
1498}
1499
1a5cb814 1500#ifdef CONFIG_HUGETLB_PAGE
116354d1 1501/* This function walks within one hugetlb entry in the single call */
356515e7 1502static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
116354d1
NH
1503 unsigned long addr, unsigned long end,
1504 struct mm_walk *walk)
5dc37642 1505{
5dc37642 1506 struct pagemapread *pm = walk->private;
f995ece2 1507 struct vm_area_struct *vma = walk->vma;
356515e7 1508 u64 flags = 0, frame = 0;
5dc37642 1509 int err = 0;
356515e7 1510 pte_t pte;
5dc37642 1511
f995ece2 1512 if (vma->vm_flags & VM_SOFTDIRTY)
deb94544 1513 flags |= PM_SOFT_DIRTY;
d9104d1c 1514
356515e7
KK
1515 pte = huge_ptep_get(ptep);
1516 if (pte_present(pte)) {
1517 struct page *page = pte_page(pte);
1518
1519 if (!PageAnon(page))
1520 flags |= PM_FILE;
1521
77bb499b
KK
1522 if (page_mapcount(page) == 1)
1523 flags |= PM_MMAP_EXCLUSIVE;
1524
356515e7 1525 flags |= PM_PRESENT;
1c90308e
KK
1526 if (pm->show_pfn)
1527 frame = pte_pfn(pte) +
1528 ((addr & ~hmask) >> PAGE_SHIFT);
356515e7
KK
1529 }
1530
5dc37642 1531 for (; addr != end; addr += PAGE_SIZE) {
356515e7
KK
1532 pagemap_entry_t pme = make_pme(frame, flags);
1533
092b50ba 1534 err = add_to_pagemap(addr, &pme, pm);
5dc37642
NH
1535 if (err)
1536 return err;
1c90308e 1537 if (pm->show_pfn && (flags & PM_PRESENT))
356515e7 1538 frame++;
5dc37642
NH
1539 }
1540
1541 cond_resched();
1542
1543 return err;
1544}
7b86ac33
CH
1545#else
1546#define pagemap_hugetlb_range NULL
1a5cb814 1547#endif /* HUGETLB_PAGE */
5dc37642 1548
7b86ac33
CH
1549static const struct mm_walk_ops pagemap_ops = {
1550 .pmd_entry = pagemap_pmd_range,
1551 .pte_hole = pagemap_pte_hole,
1552 .hugetlb_entry = pagemap_hugetlb_range,
1553};
1554
85863e47
MM
1555/*
1556 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1557 *
f16278c6
HR
1558 * For each page in the address space, this file contains one 64-bit entry
1559 * consisting of the following:
1560 *
052fb0d6 1561 * Bits 0-54 page frame number (PFN) if present
f16278c6 1562 * Bits 0-4 swap type if swapped
052fb0d6 1563 * Bits 5-54 swap offset if swapped
1ad1335d 1564 * Bit 55 pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst)
77bb499b
KK
1565 * Bit 56 page exclusively mapped
1566 * Bits 57-60 zero
052fb0d6 1567 * Bit 61 page is file-page or shared-anon
f16278c6
HR
1568 * Bit 62 page swapped
1569 * Bit 63 page present
1570 *
1571 * If the page is not present but in swap, then the PFN contains an
1572 * encoding of the swap file number and the page's offset into the
1573 * swap. Unmapped pages return a null PFN. This allows determining
85863e47
MM
1574 * precisely which pages are mapped (or in swap) and comparing mapped
1575 * pages between processes.
1576 *
1577 * Efficient users of this interface will use /proc/pid/maps to
1578 * determine which areas of memory are actually mapped and llseek to
1579 * skip over unmapped regions.
1580 */
1581static ssize_t pagemap_read(struct file *file, char __user *buf,
1582 size_t count, loff_t *ppos)
1583{
a06db751 1584 struct mm_struct *mm = file->private_data;
85863e47 1585 struct pagemapread pm;
5d7e0d2b
AM
1586 unsigned long src;
1587 unsigned long svpfn;
1588 unsigned long start_vaddr;
1589 unsigned long end_vaddr;
a06db751 1590 int ret = 0, copied = 0;
85863e47 1591
388f7934 1592 if (!mm || !mmget_not_zero(mm))
85863e47
MM
1593 goto out;
1594
85863e47
MM
1595 ret = -EINVAL;
1596 /* file position must be aligned */
aae8679b 1597 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
a06db751 1598 goto out_mm;
85863e47
MM
1599
1600 ret = 0;
08161786 1601 if (!count)
a06db751 1602 goto out_mm;
08161786 1603
1c90308e
KK
1604 /* do not disclose physical addresses: attack vector */
1605 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1606
8c829622 1607 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
6da2ec56 1608 pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL);
5d7e0d2b 1609 ret = -ENOMEM;
d82ef020 1610 if (!pm.buffer)
a06db751 1611 goto out_mm;
85863e47 1612
5d7e0d2b
AM
1613 src = *ppos;
1614 svpfn = src / PM_ENTRY_BYTES;
a06db751 1615 end_vaddr = mm->task_size;
5d7e0d2b
AM
1616
1617 /* watch out for wraparound */
40d6366e
MC
1618 start_vaddr = end_vaddr;
1619 if (svpfn <= (ULONG_MAX >> PAGE_SHIFT))
1620 start_vaddr = untagged_addr(svpfn << PAGE_SHIFT);
1621
1622 /* Ensure the address is inside the task */
1623 if (start_vaddr > mm->task_size)
5d7e0d2b
AM
1624 start_vaddr = end_vaddr;
1625
1626 /*
1627 * The odds are that this will stop walking way
1628 * before end_vaddr, because the length of the
1629 * user buffer is tracked in "pm", and the walk
1630 * will stop when we hit the end of the buffer.
1631 */
d82ef020
KH
1632 ret = 0;
1633 while (count && (start_vaddr < end_vaddr)) {
1634 int len;
1635 unsigned long end;
1636
1637 pm.pos = 0;
ea251c1d 1638 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
d82ef020
KH
1639 /* overflow ? */
1640 if (end < start_vaddr || end > end_vaddr)
1641 end = end_vaddr;
d8ed45c5 1642 ret = mmap_read_lock_killable(mm);
ad80b932
KK
1643 if (ret)
1644 goto out_free;
7b86ac33 1645 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm);
d8ed45c5 1646 mmap_read_unlock(mm);
d82ef020
KH
1647 start_vaddr = end;
1648
1649 len = min(count, PM_ENTRY_BYTES * pm.pos);
309361e0 1650 if (copy_to_user(buf, pm.buffer, len)) {
d82ef020 1651 ret = -EFAULT;
a06db751 1652 goto out_free;
d82ef020
KH
1653 }
1654 copied += len;
1655 buf += len;
1656 count -= len;
85863e47 1657 }
d82ef020
KH
1658 *ppos += copied;
1659 if (!ret || ret == PM_END_OF_BUFFER)
1660 ret = copied;
1661
98bc93e5
KM
1662out_free:
1663 kfree(pm.buffer);
a06db751
KK
1664out_mm:
1665 mmput(mm);
85863e47
MM
1666out:
1667 return ret;
1668}
1669
541c237c
PE
1670static int pagemap_open(struct inode *inode, struct file *file)
1671{
a06db751
KK
1672 struct mm_struct *mm;
1673
a06db751
KK
1674 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1675 if (IS_ERR(mm))
1676 return PTR_ERR(mm);
1677 file->private_data = mm;
1678 return 0;
1679}
1680
1681static int pagemap_release(struct inode *inode, struct file *file)
1682{
1683 struct mm_struct *mm = file->private_data;
1684
1685 if (mm)
1686 mmdrop(mm);
541c237c
PE
1687 return 0;
1688}
1689
85863e47
MM
1690const struct file_operations proc_pagemap_operations = {
1691 .llseek = mem_lseek, /* borrow this */
1692 .read = pagemap_read,
541c237c 1693 .open = pagemap_open,
a06db751 1694 .release = pagemap_release,
85863e47 1695};
1e883281 1696#endif /* CONFIG_PROC_PAGE_MONITOR */
85863e47 1697
6e21c8f1 1698#ifdef CONFIG_NUMA
6e21c8f1 1699
f69ff943 1700struct numa_maps {
f69ff943
SW
1701 unsigned long pages;
1702 unsigned long anon;
1703 unsigned long active;
1704 unsigned long writeback;
1705 unsigned long mapcount_max;
1706 unsigned long dirty;
1707 unsigned long swapcache;
1708 unsigned long node[MAX_NUMNODES];
1709};
1710
5b52fc89
SW
1711struct numa_maps_private {
1712 struct proc_maps_private proc_maps;
1713 struct numa_maps md;
1714};
1715
eb4866d0
DH
1716static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1717 unsigned long nr_pages)
f69ff943
SW
1718{
1719 int count = page_mapcount(page);
1720
eb4866d0 1721 md->pages += nr_pages;
f69ff943 1722 if (pte_dirty || PageDirty(page))
eb4866d0 1723 md->dirty += nr_pages;
f69ff943
SW
1724
1725 if (PageSwapCache(page))
eb4866d0 1726 md->swapcache += nr_pages;
f69ff943
SW
1727
1728 if (PageActive(page) || PageUnevictable(page))
eb4866d0 1729 md->active += nr_pages;
f69ff943
SW
1730
1731 if (PageWriteback(page))
eb4866d0 1732 md->writeback += nr_pages;
f69ff943
SW
1733
1734 if (PageAnon(page))
eb4866d0 1735 md->anon += nr_pages;
f69ff943
SW
1736
1737 if (count > md->mapcount_max)
1738 md->mapcount_max = count;
1739
eb4866d0 1740 md->node[page_to_nid(page)] += nr_pages;
f69ff943
SW
1741}
1742
3200a8aa
DH
1743static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1744 unsigned long addr)
1745{
1746 struct page *page;
1747 int nid;
1748
1749 if (!pte_present(pte))
1750 return NULL;
1751
1752 page = vm_normal_page(vma, addr, pte);
1753 if (!page)
1754 return NULL;
1755
1756 if (PageReserved(page))
1757 return NULL;
1758
1759 nid = page_to_nid(page);
4ff1b2c2 1760 if (!node_isset(nid, node_states[N_MEMORY]))
3200a8aa
DH
1761 return NULL;
1762
1763 return page;
1764}
1765
28093f9f
GS
1766#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1767static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1768 struct vm_area_struct *vma,
1769 unsigned long addr)
1770{
1771 struct page *page;
1772 int nid;
1773
1774 if (!pmd_present(pmd))
1775 return NULL;
1776
1777 page = vm_normal_page_pmd(vma, addr, pmd);
1778 if (!page)
1779 return NULL;
1780
1781 if (PageReserved(page))
1782 return NULL;
1783
1784 nid = page_to_nid(page);
1785 if (!node_isset(nid, node_states[N_MEMORY]))
1786 return NULL;
1787
1788 return page;
1789}
1790#endif
1791
f69ff943
SW
1792static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1793 unsigned long end, struct mm_walk *walk)
1794{
d85f4d6d
NH
1795 struct numa_maps *md = walk->private;
1796 struct vm_area_struct *vma = walk->vma;
f69ff943
SW
1797 spinlock_t *ptl;
1798 pte_t *orig_pte;
1799 pte_t *pte;
1800
28093f9f 1801#ifdef CONFIG_TRANSPARENT_HUGEPAGE
b6ec57f4
KS
1802 ptl = pmd_trans_huge_lock(pmd, vma);
1803 if (ptl) {
025c5b24
NH
1804 struct page *page;
1805
28093f9f 1806 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
025c5b24 1807 if (page)
28093f9f 1808 gather_stats(page, md, pmd_dirty(*pmd),
025c5b24 1809 HPAGE_PMD_SIZE/PAGE_SIZE);
bf929152 1810 spin_unlock(ptl);
025c5b24 1811 return 0;
32ef4384
DH
1812 }
1813
1a5a9906
AA
1814 if (pmd_trans_unstable(pmd))
1815 return 0;
28093f9f 1816#endif
f69ff943
SW
1817 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1818 do {
d85f4d6d 1819 struct page *page = can_gather_numa_stats(*pte, vma, addr);
f69ff943
SW
1820 if (!page)
1821 continue;
eb4866d0 1822 gather_stats(page, md, pte_dirty(*pte), 1);
f69ff943
SW
1823
1824 } while (pte++, addr += PAGE_SIZE, addr != end);
1825 pte_unmap_unlock(orig_pte, ptl);
a66c0410 1826 cond_resched();
f69ff943
SW
1827 return 0;
1828}
1829#ifdef CONFIG_HUGETLB_PAGE
632fd60f 1830static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
f69ff943
SW
1831 unsigned long addr, unsigned long end, struct mm_walk *walk)
1832{
5c2ff95e 1833 pte_t huge_pte = huge_ptep_get(pte);
f69ff943
SW
1834 struct numa_maps *md;
1835 struct page *page;
1836
5c2ff95e 1837 if (!pte_present(huge_pte))
f69ff943
SW
1838 return 0;
1839
5c2ff95e 1840 page = pte_page(huge_pte);
f69ff943
SW
1841 if (!page)
1842 return 0;
1843
1844 md = walk->private;
5c2ff95e 1845 gather_stats(page, md, pte_dirty(huge_pte), 1);
f69ff943
SW
1846 return 0;
1847}
1848
1849#else
632fd60f 1850static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
f69ff943
SW
1851 unsigned long addr, unsigned long end, struct mm_walk *walk)
1852{
1853 return 0;
1854}
1855#endif
1856
7b86ac33
CH
1857static const struct mm_walk_ops show_numa_ops = {
1858 .hugetlb_entry = gather_hugetlb_stats,
1859 .pmd_entry = gather_pte_stats,
1860};
1861
f69ff943
SW
1862/*
1863 * Display pages allocated per node and memory policy via /proc.
1864 */
871305bb 1865static int show_numa_map(struct seq_file *m, void *v)
f69ff943 1866{
5b52fc89
SW
1867 struct numa_maps_private *numa_priv = m->private;
1868 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
f69ff943 1869 struct vm_area_struct *vma = v;
5b52fc89 1870 struct numa_maps *md = &numa_priv->md;
7fea21ae 1871 struct file *file = vma_pr_or_file(vma);
f69ff943 1872 struct mm_struct *mm = vma->vm_mm;
f69ff943 1873 struct mempolicy *pol;
948927ee
DR
1874 char buffer[64];
1875 int nid;
f69ff943
SW
1876
1877 if (!mm)
1878 return 0;
1879
5b52fc89
SW
1880 /* Ensure we start with an empty set of numa_maps statistics. */
1881 memset(md, 0, sizeof(*md));
f69ff943 1882
498f2371
ON
1883 pol = __get_vma_policy(vma, vma->vm_start);
1884 if (pol) {
1885 mpol_to_str(buffer, sizeof(buffer), pol);
1886 mpol_cond_put(pol);
1887 } else {
1888 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1889 }
f69ff943
SW
1890
1891 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1892
1893 if (file) {
17c2b4ee 1894 seq_puts(m, " file=");
2726d566 1895 seq_file_path(m, file, "\n\t= ");
f69ff943 1896 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
17c2b4ee 1897 seq_puts(m, " heap");
1240ea0d 1898 } else if (is_stack(vma)) {
65376df5 1899 seq_puts(m, " stack");
f69ff943
SW
1900 }
1901
fc360bd9 1902 if (is_vm_hugetlb_page(vma))
17c2b4ee 1903 seq_puts(m, " huge");
fc360bd9 1904
c1e8d7c6 1905 /* mmap_lock is held by m_start */
7b86ac33 1906 walk_page_vma(vma, &show_numa_ops, md);
f69ff943
SW
1907
1908 if (!md->pages)
1909 goto out;
1910
1911 if (md->anon)
1912 seq_printf(m, " anon=%lu", md->anon);
1913
1914 if (md->dirty)
1915 seq_printf(m, " dirty=%lu", md->dirty);
1916
1917 if (md->pages != md->anon && md->pages != md->dirty)
1918 seq_printf(m, " mapped=%lu", md->pages);
1919
1920 if (md->mapcount_max > 1)
1921 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1922
1923 if (md->swapcache)
1924 seq_printf(m, " swapcache=%lu", md->swapcache);
1925
1926 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1927 seq_printf(m, " active=%lu", md->active);
1928
1929 if (md->writeback)
1930 seq_printf(m, " writeback=%lu", md->writeback);
1931
948927ee
DR
1932 for_each_node_state(nid, N_MEMORY)
1933 if (md->node[nid])
1934 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
198d1597
RA
1935
1936 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
f69ff943
SW
1937out:
1938 seq_putc(m, '\n');
f69ff943
SW
1939 return 0;
1940}
5b52fc89 1941
03a44825 1942static const struct seq_operations proc_pid_numa_maps_op = {
b7643757
SP
1943 .start = m_start,
1944 .next = m_next,
1945 .stop = m_stop,
871305bb 1946 .show = show_numa_map,
6e21c8f1 1947};
662795de 1948
b7643757
SP
1949static int pid_numa_maps_open(struct inode *inode, struct file *file)
1950{
871305bb
VB
1951 return proc_maps_open(inode, file, &proc_pid_numa_maps_op,
1952 sizeof(struct numa_maps_private));
b7643757
SP
1953}
1954
1955const struct file_operations proc_pid_numa_maps_operations = {
1956 .open = pid_numa_maps_open,
1957 .read = seq_read,
1958 .llseek = seq_lseek,
29a40ace 1959 .release = proc_map_release,
b7643757
SP
1960};
1961
f69ff943 1962#endif /* CONFIG_NUMA */