1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/pagewalk.h> 3 #include <linux/mm_inline.h> 4 #include <linux/hugetlb.h> 5 #include <linux/huge_mm.h> 6 #include <linux/mount.h> 7 #include <linux/ksm.h> 8 #include <linux/seq_file.h> 9 #include <linux/highmem.h> 10 #include <linux/ptrace.h> 11 #include <linux/slab.h> 12 #include <linux/pagemap.h> 13 #include <linux/mempolicy.h> 14 #include <linux/rmap.h> 15 #include <linux/swap.h> 16 #include <linux/sched/mm.h> 17 #include <linux/swapops.h> 18 #include <linux/mmu_notifier.h> 19 #include <linux/page_idle.h> 20 #include <linux/shmem_fs.h> 21 #include <linux/uaccess.h> 22 #include <linux/pkeys.h> 23 #include <linux/minmax.h> 24 #include <linux/overflow.h> 25 #include <linux/buildid.h> 26 27 #include <asm/elf.h> 28 #include <asm/tlb.h> 29 #include <asm/tlbflush.h> 30 #include "internal.h" 31 32 #define SEQ_PUT_DEC(str, val) \ 33 seq_put_decimal_ull_width(m, str, (val) << (PAGE_SHIFT-10), 8) 34 void task_mem(struct seq_file *m, struct mm_struct *mm) 35 { 36 unsigned long text, lib, swap, anon, file, shmem; 37 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss; 38 39 anon = get_mm_counter(mm, MM_ANONPAGES); 40 file = get_mm_counter(mm, MM_FILEPAGES); 41 shmem = get_mm_counter(mm, MM_SHMEMPAGES); 42 43 /* 44 * Note: to minimize their overhead, mm maintains hiwater_vm and 45 * hiwater_rss only when about to *lower* total_vm or rss. Any 46 * collector of these hiwater stats must therefore get total_vm 47 * and rss too, which will usually be the higher. Barriers? not 48 * worth the effort, such snapshots can always be inconsistent. 49 */ 50 hiwater_vm = total_vm = mm->total_vm; 51 if (hiwater_vm < mm->hiwater_vm) 52 hiwater_vm = mm->hiwater_vm; 53 hiwater_rss = total_rss = anon + file + shmem; 54 if (hiwater_rss < mm->hiwater_rss) 55 hiwater_rss = mm->hiwater_rss; 56 57 /* split executable areas between text and lib */ 58 text = PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK); 59 text = min(text, mm->exec_vm << PAGE_SHIFT); 60 lib = (mm->exec_vm << PAGE_SHIFT) - text; 61 62 swap = get_mm_counter(mm, MM_SWAPENTS); 63 SEQ_PUT_DEC("VmPeak:\t", hiwater_vm); 64 SEQ_PUT_DEC(" kB\nVmSize:\t", total_vm); 65 SEQ_PUT_DEC(" kB\nVmLck:\t", mm->locked_vm); 66 SEQ_PUT_DEC(" kB\nVmPin:\t", atomic64_read(&mm->pinned_vm)); 67 SEQ_PUT_DEC(" kB\nVmHWM:\t", hiwater_rss); 68 SEQ_PUT_DEC(" kB\nVmRSS:\t", total_rss); 69 SEQ_PUT_DEC(" kB\nRssAnon:\t", anon); 70 SEQ_PUT_DEC(" kB\nRssFile:\t", file); 71 SEQ_PUT_DEC(" kB\nRssShmem:\t", shmem); 72 SEQ_PUT_DEC(" kB\nVmData:\t", mm->data_vm); 73 SEQ_PUT_DEC(" kB\nVmStk:\t", mm->stack_vm); 74 seq_put_decimal_ull_width(m, 75 " kB\nVmExe:\t", text >> 10, 8); 76 seq_put_decimal_ull_width(m, 77 " kB\nVmLib:\t", lib >> 10, 8); 78 seq_put_decimal_ull_width(m, 79 " kB\nVmPTE:\t", mm_pgtables_bytes(mm) >> 10, 8); 80 SEQ_PUT_DEC(" kB\nVmSwap:\t", swap); 81 seq_puts(m, " kB\n"); 82 hugetlb_report_usage(m, mm); 83 } 84 #undef SEQ_PUT_DEC 85 86 unsigned long task_vsize(struct mm_struct *mm) 87 { 88 return PAGE_SIZE * mm->total_vm; 89 } 90 91 unsigned long task_statm(struct mm_struct *mm, 92 unsigned long *shared, unsigned long *text, 93 unsigned long *data, unsigned long *resident) 94 { 95 *shared = get_mm_counter(mm, MM_FILEPAGES) + 96 get_mm_counter(mm, MM_SHMEMPAGES); 97 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) 98 >> PAGE_SHIFT; 99 *data = mm->data_vm + mm->stack_vm; 100 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES); 101 return mm->total_vm; 102 } 103 104 #ifdef CONFIG_NUMA 105 /* 106 * Save get_task_policy() for show_numa_map(). 107 */ 108 static void hold_task_mempolicy(struct proc_maps_private *priv) 109 { 110 struct task_struct *task = priv->task; 111 112 task_lock(task); 113 priv->task_mempolicy = get_task_policy(task); 114 mpol_get(priv->task_mempolicy); 115 task_unlock(task); 116 } 117 static void release_task_mempolicy(struct proc_maps_private *priv) 118 { 119 mpol_put(priv->task_mempolicy); 120 } 121 #else 122 static void hold_task_mempolicy(struct proc_maps_private *priv) 123 { 124 } 125 static void release_task_mempolicy(struct proc_maps_private *priv) 126 { 127 } 128 #endif 129 130 static struct vm_area_struct *proc_get_vma(struct proc_maps_private *priv, 131 loff_t *ppos) 132 { 133 struct vm_area_struct *vma = vma_next(&priv->iter); 134 135 if (vma) { 136 *ppos = vma->vm_start; 137 } else { 138 *ppos = -2UL; 139 vma = get_gate_vma(priv->mm); 140 } 141 142 return vma; 143 } 144 145 static void *m_start(struct seq_file *m, loff_t *ppos) 146 { 147 struct proc_maps_private *priv = m->private; 148 unsigned long last_addr = *ppos; 149 struct mm_struct *mm; 150 151 /* See m_next(). Zero at the start or after lseek. */ 152 if (last_addr == -1UL) 153 return NULL; 154 155 priv->task = get_proc_task(priv->inode); 156 if (!priv->task) 157 return ERR_PTR(-ESRCH); 158 159 mm = priv->mm; 160 if (!mm || !mmget_not_zero(mm)) { 161 put_task_struct(priv->task); 162 priv->task = NULL; 163 return NULL; 164 } 165 166 if (mmap_read_lock_killable(mm)) { 167 mmput(mm); 168 put_task_struct(priv->task); 169 priv->task = NULL; 170 return ERR_PTR(-EINTR); 171 } 172 173 vma_iter_init(&priv->iter, mm, last_addr); 174 hold_task_mempolicy(priv); 175 if (last_addr == -2UL) 176 return get_gate_vma(mm); 177 178 return proc_get_vma(priv, ppos); 179 } 180 181 static void *m_next(struct seq_file *m, void *v, loff_t *ppos) 182 { 183 if (*ppos == -2UL) { 184 *ppos = -1UL; 185 return NULL; 186 } 187 return proc_get_vma(m->private, ppos); 188 } 189 190 static void m_stop(struct seq_file *m, void *v) 191 { 192 struct proc_maps_private *priv = m->private; 193 struct mm_struct *mm = priv->mm; 194 195 if (!priv->task) 196 return; 197 198 release_task_mempolicy(priv); 199 mmap_read_unlock(mm); 200 mmput(mm); 201 put_task_struct(priv->task); 202 priv->task = NULL; 203 } 204 205 static int proc_maps_open(struct inode *inode, struct file *file, 206 const struct seq_operations *ops, int psize) 207 { 208 struct proc_maps_private *priv = __seq_open_private(file, ops, psize); 209 210 if (!priv) 211 return -ENOMEM; 212 213 priv->inode = inode; 214 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ); 215 if (IS_ERR(priv->mm)) { 216 int err = PTR_ERR(priv->mm); 217 218 seq_release_private(inode, file); 219 return err; 220 } 221 222 return 0; 223 } 224 225 static int proc_map_release(struct inode *inode, struct file *file) 226 { 227 struct seq_file *seq = file->private_data; 228 struct proc_maps_private *priv = seq->private; 229 230 if (priv->mm) 231 mmdrop(priv->mm); 232 233 return seq_release_private(inode, file); 234 } 235 236 static int do_maps_open(struct inode *inode, struct file *file, 237 const struct seq_operations *ops) 238 { 239 return proc_maps_open(inode, file, ops, 240 sizeof(struct proc_maps_private)); 241 } 242 243 static void get_vma_name(struct vm_area_struct *vma, 244 const struct path **path, 245 const char **name, 246 const char **name_fmt) 247 { 248 struct anon_vma_name *anon_name = vma->vm_mm ? anon_vma_name(vma) : NULL; 249 250 *name = NULL; 251 *path = NULL; 252 *name_fmt = NULL; 253 254 /* 255 * Print the dentry name for named mappings, and a 256 * special [heap] marker for the heap: 257 */ 258 if (vma->vm_file) { 259 /* 260 * If user named this anon shared memory via 261 * prctl(PR_SET_VMA ..., use the provided name. 262 */ 263 if (anon_name) { 264 *name_fmt = "[anon_shmem:%s]"; 265 *name = anon_name->name; 266 } else { 267 *path = file_user_path(vma->vm_file); 268 } 269 return; 270 } 271 272 if (vma->vm_ops && vma->vm_ops->name) { 273 *name = vma->vm_ops->name(vma); 274 if (*name) 275 return; 276 } 277 278 *name = arch_vma_name(vma); 279 if (*name) 280 return; 281 282 if (!vma->vm_mm) { 283 *name = "[vdso]"; 284 return; 285 } 286 287 if (vma_is_initial_heap(vma)) { 288 *name = "[heap]"; 289 return; 290 } 291 292 if (vma_is_initial_stack(vma)) { 293 *name = "[stack]"; 294 return; 295 } 296 297 if (anon_name) { 298 *name_fmt = "[anon:%s]"; 299 *name = anon_name->name; 300 return; 301 } 302 } 303 304 static void show_vma_header_prefix(struct seq_file *m, 305 unsigned long start, unsigned long end, 306 vm_flags_t flags, unsigned long long pgoff, 307 dev_t dev, unsigned long ino) 308 { 309 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1); 310 seq_put_hex_ll(m, NULL, start, 8); 311 seq_put_hex_ll(m, "-", end, 8); 312 seq_putc(m, ' '); 313 seq_putc(m, flags & VM_READ ? 'r' : '-'); 314 seq_putc(m, flags & VM_WRITE ? 'w' : '-'); 315 seq_putc(m, flags & VM_EXEC ? 'x' : '-'); 316 seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p'); 317 seq_put_hex_ll(m, " ", pgoff, 8); 318 seq_put_hex_ll(m, " ", MAJOR(dev), 2); 319 seq_put_hex_ll(m, ":", MINOR(dev), 2); 320 seq_put_decimal_ull(m, " ", ino); 321 seq_putc(m, ' '); 322 } 323 324 static void 325 show_map_vma(struct seq_file *m, struct vm_area_struct *vma) 326 { 327 const struct path *path; 328 const char *name_fmt, *name; 329 vm_flags_t flags = vma->vm_flags; 330 unsigned long ino = 0; 331 unsigned long long pgoff = 0; 332 unsigned long start, end; 333 dev_t dev = 0; 334 335 if (vma->vm_file) { 336 const struct inode *inode = file_user_inode(vma->vm_file); 337 338 dev = inode->i_sb->s_dev; 339 ino = inode->i_ino; 340 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT; 341 } 342 343 start = vma->vm_start; 344 end = vma->vm_end; 345 show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino); 346 347 get_vma_name(vma, &path, &name, &name_fmt); 348 if (path) { 349 seq_pad(m, ' '); 350 seq_path(m, path, "\n"); 351 } else if (name_fmt) { 352 seq_pad(m, ' '); 353 seq_printf(m, name_fmt, name); 354 } else if (name) { 355 seq_pad(m, ' '); 356 seq_puts(m, name); 357 } 358 seq_putc(m, '\n'); 359 } 360 361 static int show_map(struct seq_file *m, void *v) 362 { 363 show_map_vma(m, v); 364 return 0; 365 } 366 367 static const struct seq_operations proc_pid_maps_op = { 368 .start = m_start, 369 .next = m_next, 370 .stop = m_stop, 371 .show = show_map 372 }; 373 374 static int pid_maps_open(struct inode *inode, struct file *file) 375 { 376 return do_maps_open(inode, file, &proc_pid_maps_op); 377 } 378 379 #define PROCMAP_QUERY_VMA_FLAGS ( \ 380 PROCMAP_QUERY_VMA_READABLE | \ 381 PROCMAP_QUERY_VMA_WRITABLE | \ 382 PROCMAP_QUERY_VMA_EXECUTABLE | \ 383 PROCMAP_QUERY_VMA_SHARED \ 384 ) 385 386 #define PROCMAP_QUERY_VALID_FLAGS_MASK ( \ 387 PROCMAP_QUERY_COVERING_OR_NEXT_VMA | \ 388 PROCMAP_QUERY_FILE_BACKED_VMA | \ 389 PROCMAP_QUERY_VMA_FLAGS \ 390 ) 391 392 static int query_vma_setup(struct mm_struct *mm) 393 { 394 return mmap_read_lock_killable(mm); 395 } 396 397 static void query_vma_teardown(struct mm_struct *mm, struct vm_area_struct *vma) 398 { 399 mmap_read_unlock(mm); 400 } 401 402 static struct vm_area_struct *query_vma_find_by_addr(struct mm_struct *mm, unsigned long addr) 403 { 404 return find_vma(mm, addr); 405 } 406 407 static struct vm_area_struct *query_matching_vma(struct mm_struct *mm, 408 unsigned long addr, u32 flags) 409 { 410 struct vm_area_struct *vma; 411 412 next_vma: 413 vma = query_vma_find_by_addr(mm, addr); 414 if (!vma) 415 goto no_vma; 416 417 /* user requested only file-backed VMA, keep iterating */ 418 if ((flags & PROCMAP_QUERY_FILE_BACKED_VMA) && !vma->vm_file) 419 goto skip_vma; 420 421 /* VMA permissions should satisfy query flags */ 422 if (flags & PROCMAP_QUERY_VMA_FLAGS) { 423 u32 perm = 0; 424 425 if (flags & PROCMAP_QUERY_VMA_READABLE) 426 perm |= VM_READ; 427 if (flags & PROCMAP_QUERY_VMA_WRITABLE) 428 perm |= VM_WRITE; 429 if (flags & PROCMAP_QUERY_VMA_EXECUTABLE) 430 perm |= VM_EXEC; 431 if (flags & PROCMAP_QUERY_VMA_SHARED) 432 perm |= VM_MAYSHARE; 433 434 if ((vma->vm_flags & perm) != perm) 435 goto skip_vma; 436 } 437 438 /* found covering VMA or user is OK with the matching next VMA */ 439 if ((flags & PROCMAP_QUERY_COVERING_OR_NEXT_VMA) || vma->vm_start <= addr) 440 return vma; 441 442 skip_vma: 443 /* 444 * If the user needs closest matching VMA, keep iterating. 445 */ 446 addr = vma->vm_end; 447 if (flags & PROCMAP_QUERY_COVERING_OR_NEXT_VMA) 448 goto next_vma; 449 450 no_vma: 451 return ERR_PTR(-ENOENT); 452 } 453 454 static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg) 455 { 456 struct procmap_query karg; 457 struct vm_area_struct *vma; 458 struct mm_struct *mm; 459 const char *name = NULL; 460 char build_id_buf[BUILD_ID_SIZE_MAX], *name_buf = NULL; 461 __u64 usize; 462 int err; 463 464 if (copy_from_user(&usize, (void __user *)uarg, sizeof(usize))) 465 return -EFAULT; 466 /* argument struct can never be that large, reject abuse */ 467 if (usize > PAGE_SIZE) 468 return -E2BIG; 469 /* argument struct should have at least query_flags and query_addr fields */ 470 if (usize < offsetofend(struct procmap_query, query_addr)) 471 return -EINVAL; 472 err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize); 473 if (err) 474 return err; 475 476 /* reject unknown flags */ 477 if (karg.query_flags & ~PROCMAP_QUERY_VALID_FLAGS_MASK) 478 return -EINVAL; 479 /* either both buffer address and size are set, or both should be zero */ 480 if (!!karg.vma_name_size != !!karg.vma_name_addr) 481 return -EINVAL; 482 if (!!karg.build_id_size != !!karg.build_id_addr) 483 return -EINVAL; 484 485 mm = priv->mm; 486 if (!mm || !mmget_not_zero(mm)) 487 return -ESRCH; 488 489 err = query_vma_setup(mm); 490 if (err) { 491 mmput(mm); 492 return err; 493 } 494 495 vma = query_matching_vma(mm, karg.query_addr, karg.query_flags); 496 if (IS_ERR(vma)) { 497 err = PTR_ERR(vma); 498 vma = NULL; 499 goto out; 500 } 501 502 karg.vma_start = vma->vm_start; 503 karg.vma_end = vma->vm_end; 504 505 karg.vma_flags = 0; 506 if (vma->vm_flags & VM_READ) 507 karg.vma_flags |= PROCMAP_QUERY_VMA_READABLE; 508 if (vma->vm_flags & VM_WRITE) 509 karg.vma_flags |= PROCMAP_QUERY_VMA_WRITABLE; 510 if (vma->vm_flags & VM_EXEC) 511 karg.vma_flags |= PROCMAP_QUERY_VMA_EXECUTABLE; 512 if (vma->vm_flags & VM_MAYSHARE) 513 karg.vma_flags |= PROCMAP_QUERY_VMA_SHARED; 514 515 karg.vma_page_size = vma_kernel_pagesize(vma); 516 517 if (vma->vm_file) { 518 const struct inode *inode = file_user_inode(vma->vm_file); 519 520 karg.vma_offset = ((__u64)vma->vm_pgoff) << PAGE_SHIFT; 521 karg.dev_major = MAJOR(inode->i_sb->s_dev); 522 karg.dev_minor = MINOR(inode->i_sb->s_dev); 523 karg.inode = inode->i_ino; 524 } else { 525 karg.vma_offset = 0; 526 karg.dev_major = 0; 527 karg.dev_minor = 0; 528 karg.inode = 0; 529 } 530 531 if (karg.build_id_size) { 532 __u32 build_id_sz; 533 534 err = build_id_parse(vma, build_id_buf, &build_id_sz); 535 if (err) { 536 karg.build_id_size = 0; 537 } else { 538 if (karg.build_id_size < build_id_sz) { 539 err = -ENAMETOOLONG; 540 goto out; 541 } 542 karg.build_id_size = build_id_sz; 543 } 544 } 545 546 if (karg.build_id_size) { 547 __u32 build_id_sz; 548 549 err = build_id_parse(vma, build_id_buf, &build_id_sz); 550 if (err) { 551 karg.build_id_size = 0; 552 } else { 553 if (karg.build_id_size < build_id_sz) { 554 err = -ENAMETOOLONG; 555 goto out; 556 } 557 karg.build_id_size = build_id_sz; 558 } 559 } 560 561 if (karg.vma_name_size) { 562 size_t name_buf_sz = min_t(size_t, PATH_MAX, karg.vma_name_size); 563 const struct path *path; 564 const char *name_fmt; 565 size_t name_sz = 0; 566 567 get_vma_name(vma, &path, &name, &name_fmt); 568 569 if (path || name_fmt || name) { 570 name_buf = kmalloc(name_buf_sz, GFP_KERNEL); 571 if (!name_buf) { 572 err = -ENOMEM; 573 goto out; 574 } 575 } 576 if (path) { 577 name = d_path(path, name_buf, name_buf_sz); 578 if (IS_ERR(name)) { 579 err = PTR_ERR(name); 580 goto out; 581 } 582 name_sz = name_buf + name_buf_sz - name; 583 } else if (name || name_fmt) { 584 name_sz = 1 + snprintf(name_buf, name_buf_sz, name_fmt ?: "%s", name); 585 name = name_buf; 586 } 587 if (name_sz > name_buf_sz) { 588 err = -ENAMETOOLONG; 589 goto out; 590 } 591 karg.vma_name_size = name_sz; 592 } 593 594 /* unlock vma or mmap_lock, and put mm_struct before copying data to user */ 595 query_vma_teardown(mm, vma); 596 mmput(mm); 597 598 if (karg.vma_name_size && copy_to_user(u64_to_user_ptr(karg.vma_name_addr), 599 name, karg.vma_name_size)) { 600 kfree(name_buf); 601 return -EFAULT; 602 } 603 kfree(name_buf); 604 605 if (karg.build_id_size && copy_to_user(u64_to_user_ptr(karg.build_id_addr), 606 build_id_buf, karg.build_id_size)) 607 return -EFAULT; 608 609 if (copy_to_user(uarg, &karg, min_t(size_t, sizeof(karg), usize))) 610 return -EFAULT; 611 612 return 0; 613 614 out: 615 query_vma_teardown(mm, vma); 616 mmput(mm); 617 kfree(name_buf); 618 return err; 619 } 620 621 static long procfs_procmap_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 622 { 623 struct seq_file *seq = file->private_data; 624 struct proc_maps_private *priv = seq->private; 625 626 switch (cmd) { 627 case PROCMAP_QUERY: 628 return do_procmap_query(priv, (void __user *)arg); 629 default: 630 return -ENOIOCTLCMD; 631 } 632 } 633 634 const struct file_operations proc_pid_maps_operations = { 635 .open = pid_maps_open, 636 .read = seq_read, 637 .llseek = seq_lseek, 638 .release = proc_map_release, 639 .unlocked_ioctl = procfs_procmap_ioctl, 640 .compat_ioctl = compat_ptr_ioctl, 641 }; 642 643 /* 644 * Proportional Set Size(PSS): my share of RSS. 645 * 646 * PSS of a process is the count of pages it has in memory, where each 647 * page is divided by the number of processes sharing it. So if a 648 * process has 1000 pages all to itself, and 1000 shared with one other 649 * process, its PSS will be 1500. 650 * 651 * To keep (accumulated) division errors low, we adopt a 64bit 652 * fixed-point pss counter to minimize division errors. So (pss >> 653 * PSS_SHIFT) would be the real byte count. 654 * 655 * A shift of 12 before division means (assuming 4K page size): 656 * - 1M 3-user-pages add up to 8KB errors; 657 * - supports mapcount up to 2^24, or 16M; 658 * - supports PSS up to 2^52 bytes, or 4PB. 659 */ 660 #define PSS_SHIFT 12 661 662 #ifdef CONFIG_PROC_PAGE_MONITOR 663 struct mem_size_stats { 664 unsigned long resident; 665 unsigned long shared_clean; 666 unsigned long shared_dirty; 667 unsigned long private_clean; 668 unsigned long private_dirty; 669 unsigned long referenced; 670 unsigned long anonymous; 671 unsigned long lazyfree; 672 unsigned long anonymous_thp; 673 unsigned long shmem_thp; 674 unsigned long file_thp; 675 unsigned long swap; 676 unsigned long shared_hugetlb; 677 unsigned long private_hugetlb; 678 unsigned long ksm; 679 u64 pss; 680 u64 pss_anon; 681 u64 pss_file; 682 u64 pss_shmem; 683 u64 pss_dirty; 684 u64 pss_locked; 685 u64 swap_pss; 686 }; 687 688 static void smaps_page_accumulate(struct mem_size_stats *mss, 689 struct folio *folio, unsigned long size, unsigned long pss, 690 bool dirty, bool locked, bool private) 691 { 692 mss->pss += pss; 693 694 if (folio_test_anon(folio)) 695 mss->pss_anon += pss; 696 else if (folio_test_swapbacked(folio)) 697 mss->pss_shmem += pss; 698 else 699 mss->pss_file += pss; 700 701 if (locked) 702 mss->pss_locked += pss; 703 704 if (dirty || folio_test_dirty(folio)) { 705 mss->pss_dirty += pss; 706 if (private) 707 mss->private_dirty += size; 708 else 709 mss->shared_dirty += size; 710 } else { 711 if (private) 712 mss->private_clean += size; 713 else 714 mss->shared_clean += size; 715 } 716 } 717 718 static void smaps_account(struct mem_size_stats *mss, struct page *page, 719 bool compound, bool young, bool dirty, bool locked, 720 bool present) 721 { 722 struct folio *folio = page_folio(page); 723 int i, nr = compound ? compound_nr(page) : 1; 724 unsigned long size = nr * PAGE_SIZE; 725 726 /* 727 * First accumulate quantities that depend only on |size| and the type 728 * of the compound page. 729 */ 730 if (folio_test_anon(folio)) { 731 mss->anonymous += size; 732 if (!folio_test_swapbacked(folio) && !dirty && 733 !folio_test_dirty(folio)) 734 mss->lazyfree += size; 735 } 736 737 if (folio_test_ksm(folio)) 738 mss->ksm += size; 739 740 mss->resident += size; 741 /* Accumulate the size in pages that have been accessed. */ 742 if (young || folio_test_young(folio) || folio_test_referenced(folio)) 743 mss->referenced += size; 744 745 /* 746 * Then accumulate quantities that may depend on sharing, or that may 747 * differ page-by-page. 748 * 749 * refcount == 1 for present entries guarantees that the folio is mapped 750 * exactly once. For large folios this implies that exactly one 751 * PTE/PMD/... maps (a part of) this folio. 752 * 753 * Treat all non-present entries (where relying on the mapcount and 754 * refcount doesn't make sense) as "maybe shared, but not sure how 755 * often". We treat device private entries as being fake-present. 756 * 757 * Note that it would not be safe to read the mapcount especially for 758 * pages referenced by migration entries, even with the PTL held. 759 */ 760 if (folio_ref_count(folio) == 1 || !present) { 761 smaps_page_accumulate(mss, folio, size, size << PSS_SHIFT, 762 dirty, locked, present); 763 return; 764 } 765 /* 766 * We obtain a snapshot of the mapcount. Without holding the folio lock 767 * this snapshot can be slightly wrong as we cannot always read the 768 * mapcount atomically. 769 */ 770 for (i = 0; i < nr; i++, page++) { 771 int mapcount = folio_precise_page_mapcount(folio, page); 772 unsigned long pss = PAGE_SIZE << PSS_SHIFT; 773 if (mapcount >= 2) 774 pss /= mapcount; 775 smaps_page_accumulate(mss, folio, PAGE_SIZE, pss, 776 dirty, locked, mapcount < 2); 777 } 778 } 779 780 #ifdef CONFIG_SHMEM 781 static int smaps_pte_hole(unsigned long addr, unsigned long end, 782 __always_unused int depth, struct mm_walk *walk) 783 { 784 struct mem_size_stats *mss = walk->private; 785 struct vm_area_struct *vma = walk->vma; 786 787 mss->swap += shmem_partial_swap_usage(walk->vma->vm_file->f_mapping, 788 linear_page_index(vma, addr), 789 linear_page_index(vma, end)); 790 791 return 0; 792 } 793 #else 794 #define smaps_pte_hole NULL 795 #endif /* CONFIG_SHMEM */ 796 797 static void smaps_pte_hole_lookup(unsigned long addr, struct mm_walk *walk) 798 { 799 #ifdef CONFIG_SHMEM 800 if (walk->ops->pte_hole) { 801 /* depth is not used */ 802 smaps_pte_hole(addr, addr + PAGE_SIZE, 0, walk); 803 } 804 #endif 805 } 806 807 static void smaps_pte_entry(pte_t *pte, unsigned long addr, 808 struct mm_walk *walk) 809 { 810 struct mem_size_stats *mss = walk->private; 811 struct vm_area_struct *vma = walk->vma; 812 bool locked = !!(vma->vm_flags & VM_LOCKED); 813 struct page *page = NULL; 814 bool present = false, young = false, dirty = false; 815 pte_t ptent = ptep_get(pte); 816 817 if (pte_present(ptent)) { 818 page = vm_normal_page(vma, addr, ptent); 819 young = pte_young(ptent); 820 dirty = pte_dirty(ptent); 821 present = true; 822 } else if (is_swap_pte(ptent)) { 823 swp_entry_t swpent = pte_to_swp_entry(ptent); 824 825 if (!non_swap_entry(swpent)) { 826 int mapcount; 827 828 mss->swap += PAGE_SIZE; 829 mapcount = swp_swapcount(swpent); 830 if (mapcount >= 2) { 831 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT; 832 833 do_div(pss_delta, mapcount); 834 mss->swap_pss += pss_delta; 835 } else { 836 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT; 837 } 838 } else if (is_pfn_swap_entry(swpent)) { 839 if (is_device_private_entry(swpent)) 840 present = true; 841 page = pfn_swap_entry_to_page(swpent); 842 } 843 } else { 844 smaps_pte_hole_lookup(addr, walk); 845 return; 846 } 847 848 if (!page) 849 return; 850 851 smaps_account(mss, page, false, young, dirty, locked, present); 852 } 853 854 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 855 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, 856 struct mm_walk *walk) 857 { 858 struct mem_size_stats *mss = walk->private; 859 struct vm_area_struct *vma = walk->vma; 860 bool locked = !!(vma->vm_flags & VM_LOCKED); 861 struct page *page = NULL; 862 bool present = false; 863 struct folio *folio; 864 865 if (pmd_present(*pmd)) { 866 page = vm_normal_page_pmd(vma, addr, *pmd); 867 present = true; 868 } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) { 869 swp_entry_t entry = pmd_to_swp_entry(*pmd); 870 871 if (is_pfn_swap_entry(entry)) 872 page = pfn_swap_entry_to_page(entry); 873 } 874 if (IS_ERR_OR_NULL(page)) 875 return; 876 folio = page_folio(page); 877 if (folio_test_anon(folio)) 878 mss->anonymous_thp += HPAGE_PMD_SIZE; 879 else if (folio_test_swapbacked(folio)) 880 mss->shmem_thp += HPAGE_PMD_SIZE; 881 else if (folio_is_zone_device(folio)) 882 /* pass */; 883 else 884 mss->file_thp += HPAGE_PMD_SIZE; 885 886 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), 887 locked, present); 888 } 889 #else 890 static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, 891 struct mm_walk *walk) 892 { 893 } 894 #endif 895 896 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 897 struct mm_walk *walk) 898 { 899 struct vm_area_struct *vma = walk->vma; 900 pte_t *pte; 901 spinlock_t *ptl; 902 903 ptl = pmd_trans_huge_lock(pmd, vma); 904 if (ptl) { 905 smaps_pmd_entry(pmd, addr, walk); 906 spin_unlock(ptl); 907 goto out; 908 } 909 910 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 911 if (!pte) { 912 walk->action = ACTION_AGAIN; 913 return 0; 914 } 915 for (; addr != end; pte++, addr += PAGE_SIZE) 916 smaps_pte_entry(pte, addr, walk); 917 pte_unmap_unlock(pte - 1, ptl); 918 out: 919 cond_resched(); 920 return 0; 921 } 922 923 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) 924 { 925 /* 926 * Don't forget to update Documentation/ on changes. 927 */ 928 static const char mnemonics[BITS_PER_LONG][2] = { 929 /* 930 * In case if we meet a flag we don't know about. 931 */ 932 [0 ... (BITS_PER_LONG-1)] = "??", 933 934 [ilog2(VM_READ)] = "rd", 935 [ilog2(VM_WRITE)] = "wr", 936 [ilog2(VM_EXEC)] = "ex", 937 [ilog2(VM_SHARED)] = "sh", 938 [ilog2(VM_MAYREAD)] = "mr", 939 [ilog2(VM_MAYWRITE)] = "mw", 940 [ilog2(VM_MAYEXEC)] = "me", 941 [ilog2(VM_MAYSHARE)] = "ms", 942 [ilog2(VM_GROWSDOWN)] = "gd", 943 [ilog2(VM_PFNMAP)] = "pf", 944 [ilog2(VM_LOCKED)] = "lo", 945 [ilog2(VM_IO)] = "io", 946 [ilog2(VM_SEQ_READ)] = "sr", 947 [ilog2(VM_RAND_READ)] = "rr", 948 [ilog2(VM_DONTCOPY)] = "dc", 949 [ilog2(VM_DONTEXPAND)] = "de", 950 [ilog2(VM_LOCKONFAULT)] = "lf", 951 [ilog2(VM_ACCOUNT)] = "ac", 952 [ilog2(VM_NORESERVE)] = "nr", 953 [ilog2(VM_HUGETLB)] = "ht", 954 [ilog2(VM_SYNC)] = "sf", 955 [ilog2(VM_ARCH_1)] = "ar", 956 [ilog2(VM_WIPEONFORK)] = "wf", 957 [ilog2(VM_DONTDUMP)] = "dd", 958 #ifdef CONFIG_ARM64_BTI 959 [ilog2(VM_ARM64_BTI)] = "bt", 960 #endif 961 #ifdef CONFIG_MEM_SOFT_DIRTY 962 [ilog2(VM_SOFTDIRTY)] = "sd", 963 #endif 964 [ilog2(VM_MIXEDMAP)] = "mm", 965 [ilog2(VM_HUGEPAGE)] = "hg", 966 [ilog2(VM_NOHUGEPAGE)] = "nh", 967 [ilog2(VM_MERGEABLE)] = "mg", 968 [ilog2(VM_UFFD_MISSING)]= "um", 969 [ilog2(VM_UFFD_WP)] = "uw", 970 #ifdef CONFIG_ARM64_MTE 971 [ilog2(VM_MTE)] = "mt", 972 [ilog2(VM_MTE_ALLOWED)] = "", 973 #endif 974 #ifdef CONFIG_ARCH_HAS_PKEYS 975 /* These come out via ProtectionKey: */ 976 [ilog2(VM_PKEY_BIT0)] = "", 977 [ilog2(VM_PKEY_BIT1)] = "", 978 [ilog2(VM_PKEY_BIT2)] = "", 979 [ilog2(VM_PKEY_BIT3)] = "", 980 #if VM_PKEY_BIT4 981 [ilog2(VM_PKEY_BIT4)] = "", 982 #endif 983 #endif /* CONFIG_ARCH_HAS_PKEYS */ 984 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR 985 [ilog2(VM_UFFD_MINOR)] = "ui", 986 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ 987 #ifdef CONFIG_X86_USER_SHADOW_STACK 988 [ilog2(VM_SHADOW_STACK)] = "ss", 989 #endif 990 #ifdef CONFIG_64BIT 991 [ilog2(VM_SEALED)] = "sl", 992 #endif 993 }; 994 size_t i; 995 996 seq_puts(m, "VmFlags: "); 997 for (i = 0; i < BITS_PER_LONG; i++) { 998 if (!mnemonics[i][0]) 999 continue; 1000 if (vma->vm_flags & (1UL << i)) { 1001 seq_putc(m, mnemonics[i][0]); 1002 seq_putc(m, mnemonics[i][1]); 1003 seq_putc(m, ' '); 1004 } 1005 } 1006 seq_putc(m, '\n'); 1007 } 1008 1009 #ifdef CONFIG_HUGETLB_PAGE 1010 static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask, 1011 unsigned long addr, unsigned long end, 1012 struct mm_walk *walk) 1013 { 1014 struct mem_size_stats *mss = walk->private; 1015 struct vm_area_struct *vma = walk->vma; 1016 pte_t ptent = huge_ptep_get(walk->mm, addr, pte); 1017 struct folio *folio = NULL; 1018 bool present = false; 1019 1020 if (pte_present(ptent)) { 1021 folio = page_folio(pte_page(ptent)); 1022 present = true; 1023 } else if (is_swap_pte(ptent)) { 1024 swp_entry_t swpent = pte_to_swp_entry(ptent); 1025 1026 if (is_pfn_swap_entry(swpent)) 1027 folio = pfn_swap_entry_folio(swpent); 1028 } 1029 1030 if (folio) { 1031 /* We treat non-present entries as "maybe shared". */ 1032 if (!present || folio_likely_mapped_shared(folio) || 1033 hugetlb_pmd_shared(pte)) 1034 mss->shared_hugetlb += huge_page_size(hstate_vma(vma)); 1035 else 1036 mss->private_hugetlb += huge_page_size(hstate_vma(vma)); 1037 } 1038 return 0; 1039 } 1040 #else 1041 #define smaps_hugetlb_range NULL 1042 #endif /* HUGETLB_PAGE */ 1043 1044 static const struct mm_walk_ops smaps_walk_ops = { 1045 .pmd_entry = smaps_pte_range, 1046 .hugetlb_entry = smaps_hugetlb_range, 1047 .walk_lock = PGWALK_RDLOCK, 1048 }; 1049 1050 static const struct mm_walk_ops smaps_shmem_walk_ops = { 1051 .pmd_entry = smaps_pte_range, 1052 .hugetlb_entry = smaps_hugetlb_range, 1053 .pte_hole = smaps_pte_hole, 1054 .walk_lock = PGWALK_RDLOCK, 1055 }; 1056 1057 /* 1058 * Gather mem stats from @vma with the indicated beginning 1059 * address @start, and keep them in @mss. 1060 * 1061 * Use vm_start of @vma as the beginning address if @start is 0. 1062 */ 1063 static void smap_gather_stats(struct vm_area_struct *vma, 1064 struct mem_size_stats *mss, unsigned long start) 1065 { 1066 const struct mm_walk_ops *ops = &smaps_walk_ops; 1067 1068 /* Invalid start */ 1069 if (start >= vma->vm_end) 1070 return; 1071 1072 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) { 1073 /* 1074 * For shared or readonly shmem mappings we know that all 1075 * swapped out pages belong to the shmem object, and we can 1076 * obtain the swap value much more efficiently. For private 1077 * writable mappings, we might have COW pages that are 1078 * not affected by the parent swapped out pages of the shmem 1079 * object, so we have to distinguish them during the page walk. 1080 * Unless we know that the shmem object (or the part mapped by 1081 * our VMA) has no swapped out pages at all. 1082 */ 1083 unsigned long shmem_swapped = shmem_swap_usage(vma); 1084 1085 if (!start && (!shmem_swapped || (vma->vm_flags & VM_SHARED) || 1086 !(vma->vm_flags & VM_WRITE))) { 1087 mss->swap += shmem_swapped; 1088 } else { 1089 ops = &smaps_shmem_walk_ops; 1090 } 1091 } 1092 1093 /* mmap_lock is held in m_start */ 1094 if (!start) 1095 walk_page_vma(vma, ops, mss); 1096 else 1097 walk_page_range(vma->vm_mm, start, vma->vm_end, ops, mss); 1098 } 1099 1100 #define SEQ_PUT_DEC(str, val) \ 1101 seq_put_decimal_ull_width(m, str, (val) >> 10, 8) 1102 1103 /* Show the contents common for smaps and smaps_rollup */ 1104 static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss, 1105 bool rollup_mode) 1106 { 1107 SEQ_PUT_DEC("Rss: ", mss->resident); 1108 SEQ_PUT_DEC(" kB\nPss: ", mss->pss >> PSS_SHIFT); 1109 SEQ_PUT_DEC(" kB\nPss_Dirty: ", mss->pss_dirty >> PSS_SHIFT); 1110 if (rollup_mode) { 1111 /* 1112 * These are meaningful only for smaps_rollup, otherwise two of 1113 * them are zero, and the other one is the same as Pss. 1114 */ 1115 SEQ_PUT_DEC(" kB\nPss_Anon: ", 1116 mss->pss_anon >> PSS_SHIFT); 1117 SEQ_PUT_DEC(" kB\nPss_File: ", 1118 mss->pss_file >> PSS_SHIFT); 1119 SEQ_PUT_DEC(" kB\nPss_Shmem: ", 1120 mss->pss_shmem >> PSS_SHIFT); 1121 } 1122 SEQ_PUT_DEC(" kB\nShared_Clean: ", mss->shared_clean); 1123 SEQ_PUT_DEC(" kB\nShared_Dirty: ", mss->shared_dirty); 1124 SEQ_PUT_DEC(" kB\nPrivate_Clean: ", mss->private_clean); 1125 SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty); 1126 SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced); 1127 SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous); 1128 SEQ_PUT_DEC(" kB\nKSM: ", mss->ksm); 1129 SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree); 1130 SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp); 1131 SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp); 1132 SEQ_PUT_DEC(" kB\nFilePmdMapped: ", mss->file_thp); 1133 SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb); 1134 seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ", 1135 mss->private_hugetlb >> 10, 7); 1136 SEQ_PUT_DEC(" kB\nSwap: ", mss->swap); 1137 SEQ_PUT_DEC(" kB\nSwapPss: ", 1138 mss->swap_pss >> PSS_SHIFT); 1139 SEQ_PUT_DEC(" kB\nLocked: ", 1140 mss->pss_locked >> PSS_SHIFT); 1141 seq_puts(m, " kB\n"); 1142 } 1143 1144 static int show_smap(struct seq_file *m, void *v) 1145 { 1146 struct vm_area_struct *vma = v; 1147 struct mem_size_stats mss = {}; 1148 1149 smap_gather_stats(vma, &mss, 0); 1150 1151 show_map_vma(m, vma); 1152 1153 SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start); 1154 SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma)); 1155 SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma)); 1156 seq_puts(m, " kB\n"); 1157 1158 __show_smap(m, &mss, false); 1159 1160 seq_printf(m, "THPeligible: %8u\n", 1161 !!thp_vma_allowable_orders(vma, vma->vm_flags, 1162 TVA_SMAPS | TVA_ENFORCE_SYSFS, THP_ORDERS_ALL)); 1163 1164 if (arch_pkeys_enabled()) 1165 seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma)); 1166 show_smap_vma_flags(m, vma); 1167 1168 return 0; 1169 } 1170 1171 static int show_smaps_rollup(struct seq_file *m, void *v) 1172 { 1173 struct proc_maps_private *priv = m->private; 1174 struct mem_size_stats mss = {}; 1175 struct mm_struct *mm = priv->mm; 1176 struct vm_area_struct *vma; 1177 unsigned long vma_start = 0, last_vma_end = 0; 1178 int ret = 0; 1179 VMA_ITERATOR(vmi, mm, 0); 1180 1181 priv->task = get_proc_task(priv->inode); 1182 if (!priv->task) 1183 return -ESRCH; 1184 1185 if (!mm || !mmget_not_zero(mm)) { 1186 ret = -ESRCH; 1187 goto out_put_task; 1188 } 1189 1190 ret = mmap_read_lock_killable(mm); 1191 if (ret) 1192 goto out_put_mm; 1193 1194 hold_task_mempolicy(priv); 1195 vma = vma_next(&vmi); 1196 1197 if (unlikely(!vma)) 1198 goto empty_set; 1199 1200 vma_start = vma->vm_start; 1201 do { 1202 smap_gather_stats(vma, &mss, 0); 1203 last_vma_end = vma->vm_end; 1204 1205 /* 1206 * Release mmap_lock temporarily if someone wants to 1207 * access it for write request. 1208 */ 1209 if (mmap_lock_is_contended(mm)) { 1210 vma_iter_invalidate(&vmi); 1211 mmap_read_unlock(mm); 1212 ret = mmap_read_lock_killable(mm); 1213 if (ret) { 1214 release_task_mempolicy(priv); 1215 goto out_put_mm; 1216 } 1217 1218 /* 1219 * After dropping the lock, there are four cases to 1220 * consider. See the following example for explanation. 1221 * 1222 * +------+------+-----------+ 1223 * | VMA1 | VMA2 | VMA3 | 1224 * +------+------+-----------+ 1225 * | | | | 1226 * 4k 8k 16k 400k 1227 * 1228 * Suppose we drop the lock after reading VMA2 due to 1229 * contention, then we get: 1230 * 1231 * last_vma_end = 16k 1232 * 1233 * 1) VMA2 is freed, but VMA3 exists: 1234 * 1235 * vma_next(vmi) will return VMA3. 1236 * In this case, just continue from VMA3. 1237 * 1238 * 2) VMA2 still exists: 1239 * 1240 * vma_next(vmi) will return VMA3. 1241 * In this case, just continue from VMA3. 1242 * 1243 * 3) No more VMAs can be found: 1244 * 1245 * vma_next(vmi) will return NULL. 1246 * No more things to do, just break. 1247 * 1248 * 4) (last_vma_end - 1) is the middle of a vma (VMA'): 1249 * 1250 * vma_next(vmi) will return VMA' whose range 1251 * contains last_vma_end. 1252 * Iterate VMA' from last_vma_end. 1253 */ 1254 vma = vma_next(&vmi); 1255 /* Case 3 above */ 1256 if (!vma) 1257 break; 1258 1259 /* Case 1 and 2 above */ 1260 if (vma->vm_start >= last_vma_end) { 1261 smap_gather_stats(vma, &mss, 0); 1262 last_vma_end = vma->vm_end; 1263 continue; 1264 } 1265 1266 /* Case 4 above */ 1267 if (vma->vm_end > last_vma_end) { 1268 smap_gather_stats(vma, &mss, last_vma_end); 1269 last_vma_end = vma->vm_end; 1270 } 1271 } 1272 } for_each_vma(vmi, vma); 1273 1274 empty_set: 1275 show_vma_header_prefix(m, vma_start, last_vma_end, 0, 0, 0, 0); 1276 seq_pad(m, ' '); 1277 seq_puts(m, "[rollup]\n"); 1278 1279 __show_smap(m, &mss, true); 1280 1281 release_task_mempolicy(priv); 1282 mmap_read_unlock(mm); 1283 1284 out_put_mm: 1285 mmput(mm); 1286 out_put_task: 1287 put_task_struct(priv->task); 1288 priv->task = NULL; 1289 1290 return ret; 1291 } 1292 #undef SEQ_PUT_DEC 1293 1294 static const struct seq_operations proc_pid_smaps_op = { 1295 .start = m_start, 1296 .next = m_next, 1297 .stop = m_stop, 1298 .show = show_smap 1299 }; 1300 1301 static int pid_smaps_open(struct inode *inode, struct file *file) 1302 { 1303 return do_maps_open(inode, file, &proc_pid_smaps_op); 1304 } 1305 1306 static int smaps_rollup_open(struct inode *inode, struct file *file) 1307 { 1308 int ret; 1309 struct proc_maps_private *priv; 1310 1311 priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT); 1312 if (!priv) 1313 return -ENOMEM; 1314 1315 ret = single_open(file, show_smaps_rollup, priv); 1316 if (ret) 1317 goto out_free; 1318 1319 priv->inode = inode; 1320 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ); 1321 if (IS_ERR(priv->mm)) { 1322 ret = PTR_ERR(priv->mm); 1323 1324 single_release(inode, file); 1325 goto out_free; 1326 } 1327 1328 return 0; 1329 1330 out_free: 1331 kfree(priv); 1332 return ret; 1333 } 1334 1335 static int smaps_rollup_release(struct inode *inode, struct file *file) 1336 { 1337 struct seq_file *seq = file->private_data; 1338 struct proc_maps_private *priv = seq->private; 1339 1340 if (priv->mm) 1341 mmdrop(priv->mm); 1342 1343 kfree(priv); 1344 return single_release(inode, file); 1345 } 1346 1347 const struct file_operations proc_pid_smaps_operations = { 1348 .open = pid_smaps_open, 1349 .read = seq_read, 1350 .llseek = seq_lseek, 1351 .release = proc_map_release, 1352 }; 1353 1354 const struct file_operations proc_pid_smaps_rollup_operations = { 1355 .open = smaps_rollup_open, 1356 .read = seq_read, 1357 .llseek = seq_lseek, 1358 .release = smaps_rollup_release, 1359 }; 1360 1361 enum clear_refs_types { 1362 CLEAR_REFS_ALL = 1, 1363 CLEAR_REFS_ANON, 1364 CLEAR_REFS_MAPPED, 1365 CLEAR_REFS_SOFT_DIRTY, 1366 CLEAR_REFS_MM_HIWATER_RSS, 1367 CLEAR_REFS_LAST, 1368 }; 1369 1370 struct clear_refs_private { 1371 enum clear_refs_types type; 1372 }; 1373 1374 #ifdef CONFIG_MEM_SOFT_DIRTY 1375 1376 static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte) 1377 { 1378 struct folio *folio; 1379 1380 if (!pte_write(pte)) 1381 return false; 1382 if (!is_cow_mapping(vma->vm_flags)) 1383 return false; 1384 if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags))) 1385 return false; 1386 folio = vm_normal_folio(vma, addr, pte); 1387 if (!folio) 1388 return false; 1389 return folio_maybe_dma_pinned(folio); 1390 } 1391 1392 static inline void clear_soft_dirty(struct vm_area_struct *vma, 1393 unsigned long addr, pte_t *pte) 1394 { 1395 /* 1396 * The soft-dirty tracker uses #PF-s to catch writes 1397 * to pages, so write-protect the pte as well. See the 1398 * Documentation/admin-guide/mm/soft-dirty.rst for full description 1399 * of how soft-dirty works. 1400 */ 1401 pte_t ptent = ptep_get(pte); 1402 1403 if (pte_present(ptent)) { 1404 pte_t old_pte; 1405 1406 if (pte_is_pinned(vma, addr, ptent)) 1407 return; 1408 old_pte = ptep_modify_prot_start(vma, addr, pte); 1409 ptent = pte_wrprotect(old_pte); 1410 ptent = pte_clear_soft_dirty(ptent); 1411 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent); 1412 } else if (is_swap_pte(ptent)) { 1413 ptent = pte_swp_clear_soft_dirty(ptent); 1414 set_pte_at(vma->vm_mm, addr, pte, ptent); 1415 } 1416 } 1417 #else 1418 static inline void clear_soft_dirty(struct vm_area_struct *vma, 1419 unsigned long addr, pte_t *pte) 1420 { 1421 } 1422 #endif 1423 1424 #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE) 1425 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma, 1426 unsigned long addr, pmd_t *pmdp) 1427 { 1428 pmd_t old, pmd = *pmdp; 1429 1430 if (pmd_present(pmd)) { 1431 /* See comment in change_huge_pmd() */ 1432 old = pmdp_invalidate(vma, addr, pmdp); 1433 if (pmd_dirty(old)) 1434 pmd = pmd_mkdirty(pmd); 1435 if (pmd_young(old)) 1436 pmd = pmd_mkyoung(pmd); 1437 1438 pmd = pmd_wrprotect(pmd); 1439 pmd = pmd_clear_soft_dirty(pmd); 1440 1441 set_pmd_at(vma->vm_mm, addr, pmdp, pmd); 1442 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) { 1443 pmd = pmd_swp_clear_soft_dirty(pmd); 1444 set_pmd_at(vma->vm_mm, addr, pmdp, pmd); 1445 } 1446 } 1447 #else 1448 static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma, 1449 unsigned long addr, pmd_t *pmdp) 1450 { 1451 } 1452 #endif 1453 1454 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr, 1455 unsigned long end, struct mm_walk *walk) 1456 { 1457 struct clear_refs_private *cp = walk->private; 1458 struct vm_area_struct *vma = walk->vma; 1459 pte_t *pte, ptent; 1460 spinlock_t *ptl; 1461 struct folio *folio; 1462 1463 ptl = pmd_trans_huge_lock(pmd, vma); 1464 if (ptl) { 1465 if (cp->type == CLEAR_REFS_SOFT_DIRTY) { 1466 clear_soft_dirty_pmd(vma, addr, pmd); 1467 goto out; 1468 } 1469 1470 if (!pmd_present(*pmd)) 1471 goto out; 1472 1473 folio = pmd_folio(*pmd); 1474 1475 /* Clear accessed and referenced bits. */ 1476 pmdp_test_and_clear_young(vma, addr, pmd); 1477 folio_test_clear_young(folio); 1478 folio_clear_referenced(folio); 1479 out: 1480 spin_unlock(ptl); 1481 return 0; 1482 } 1483 1484 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 1485 if (!pte) { 1486 walk->action = ACTION_AGAIN; 1487 return 0; 1488 } 1489 for (; addr != end; pte++, addr += PAGE_SIZE) { 1490 ptent = ptep_get(pte); 1491 1492 if (cp->type == CLEAR_REFS_SOFT_DIRTY) { 1493 clear_soft_dirty(vma, addr, pte); 1494 continue; 1495 } 1496 1497 if (!pte_present(ptent)) 1498 continue; 1499 1500 folio = vm_normal_folio(vma, addr, ptent); 1501 if (!folio) 1502 continue; 1503 1504 /* Clear accessed and referenced bits. */ 1505 ptep_test_and_clear_young(vma, addr, pte); 1506 folio_test_clear_young(folio); 1507 folio_clear_referenced(folio); 1508 } 1509 pte_unmap_unlock(pte - 1, ptl); 1510 cond_resched(); 1511 return 0; 1512 } 1513 1514 static int clear_refs_test_walk(unsigned long start, unsigned long end, 1515 struct mm_walk *walk) 1516 { 1517 struct clear_refs_private *cp = walk->private; 1518 struct vm_area_struct *vma = walk->vma; 1519 1520 if (vma->vm_flags & VM_PFNMAP) 1521 return 1; 1522 1523 /* 1524 * Writing 1 to /proc/pid/clear_refs affects all pages. 1525 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages. 1526 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages. 1527 * Writing 4 to /proc/pid/clear_refs affects all pages. 1528 */ 1529 if (cp->type == CLEAR_REFS_ANON && vma->vm_file) 1530 return 1; 1531 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file) 1532 return 1; 1533 return 0; 1534 } 1535 1536 static const struct mm_walk_ops clear_refs_walk_ops = { 1537 .pmd_entry = clear_refs_pte_range, 1538 .test_walk = clear_refs_test_walk, 1539 .walk_lock = PGWALK_WRLOCK, 1540 }; 1541 1542 static ssize_t clear_refs_write(struct file *file, const char __user *buf, 1543 size_t count, loff_t *ppos) 1544 { 1545 struct task_struct *task; 1546 char buffer[PROC_NUMBUF] = {}; 1547 struct mm_struct *mm; 1548 struct vm_area_struct *vma; 1549 enum clear_refs_types type; 1550 int itype; 1551 int rv; 1552 1553 if (count > sizeof(buffer) - 1) 1554 count = sizeof(buffer) - 1; 1555 if (copy_from_user(buffer, buf, count)) 1556 return -EFAULT; 1557 rv = kstrtoint(strstrip(buffer), 10, &itype); 1558 if (rv < 0) 1559 return rv; 1560 type = (enum clear_refs_types)itype; 1561 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST) 1562 return -EINVAL; 1563 1564 task = get_proc_task(file_inode(file)); 1565 if (!task) 1566 return -ESRCH; 1567 mm = get_task_mm(task); 1568 if (mm) { 1569 VMA_ITERATOR(vmi, mm, 0); 1570 struct mmu_notifier_range range; 1571 struct clear_refs_private cp = { 1572 .type = type, 1573 }; 1574 1575 if (mmap_write_lock_killable(mm)) { 1576 count = -EINTR; 1577 goto out_mm; 1578 } 1579 if (type == CLEAR_REFS_MM_HIWATER_RSS) { 1580 /* 1581 * Writing 5 to /proc/pid/clear_refs resets the peak 1582 * resident set size to this mm's current rss value. 1583 */ 1584 reset_mm_hiwater_rss(mm); 1585 goto out_unlock; 1586 } 1587 1588 if (type == CLEAR_REFS_SOFT_DIRTY) { 1589 for_each_vma(vmi, vma) { 1590 if (!(vma->vm_flags & VM_SOFTDIRTY)) 1591 continue; 1592 vm_flags_clear(vma, VM_SOFTDIRTY); 1593 vma_set_page_prot(vma); 1594 } 1595 1596 inc_tlb_flush_pending(mm); 1597 mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY, 1598 0, mm, 0, -1UL); 1599 mmu_notifier_invalidate_range_start(&range); 1600 } 1601 walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp); 1602 if (type == CLEAR_REFS_SOFT_DIRTY) { 1603 mmu_notifier_invalidate_range_end(&range); 1604 flush_tlb_mm(mm); 1605 dec_tlb_flush_pending(mm); 1606 } 1607 out_unlock: 1608 mmap_write_unlock(mm); 1609 out_mm: 1610 mmput(mm); 1611 } 1612 put_task_struct(task); 1613 1614 return count; 1615 } 1616 1617 const struct file_operations proc_clear_refs_operations = { 1618 .write = clear_refs_write, 1619 .llseek = noop_llseek, 1620 }; 1621 1622 typedef struct { 1623 u64 pme; 1624 } pagemap_entry_t; 1625 1626 struct pagemapread { 1627 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */ 1628 pagemap_entry_t *buffer; 1629 bool show_pfn; 1630 }; 1631 1632 #define PAGEMAP_WALK_SIZE (PMD_SIZE) 1633 #define PAGEMAP_WALK_MASK (PMD_MASK) 1634 1635 #define PM_ENTRY_BYTES sizeof(pagemap_entry_t) 1636 #define PM_PFRAME_BITS 55 1637 #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0) 1638 #define PM_SOFT_DIRTY BIT_ULL(55) 1639 #define PM_MMAP_EXCLUSIVE BIT_ULL(56) 1640 #define PM_UFFD_WP BIT_ULL(57) 1641 #define PM_FILE BIT_ULL(61) 1642 #define PM_SWAP BIT_ULL(62) 1643 #define PM_PRESENT BIT_ULL(63) 1644 1645 #define PM_END_OF_BUFFER 1 1646 1647 static inline pagemap_entry_t make_pme(u64 frame, u64 flags) 1648 { 1649 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags }; 1650 } 1651 1652 static int add_to_pagemap(pagemap_entry_t *pme, struct pagemapread *pm) 1653 { 1654 pm->buffer[pm->pos++] = *pme; 1655 if (pm->pos >= pm->len) 1656 return PM_END_OF_BUFFER; 1657 return 0; 1658 } 1659 1660 static int pagemap_pte_hole(unsigned long start, unsigned long end, 1661 __always_unused int depth, struct mm_walk *walk) 1662 { 1663 struct pagemapread *pm = walk->private; 1664 unsigned long addr = start; 1665 int err = 0; 1666 1667 while (addr < end) { 1668 struct vm_area_struct *vma = find_vma(walk->mm, addr); 1669 pagemap_entry_t pme = make_pme(0, 0); 1670 /* End of address space hole, which we mark as non-present. */ 1671 unsigned long hole_end; 1672 1673 if (vma) 1674 hole_end = min(end, vma->vm_start); 1675 else 1676 hole_end = end; 1677 1678 for (; addr < hole_end; addr += PAGE_SIZE) { 1679 err = add_to_pagemap(&pme, pm); 1680 if (err) 1681 goto out; 1682 } 1683 1684 if (!vma) 1685 break; 1686 1687 /* Addresses in the VMA. */ 1688 if (vma->vm_flags & VM_SOFTDIRTY) 1689 pme = make_pme(0, PM_SOFT_DIRTY); 1690 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) { 1691 err = add_to_pagemap(&pme, pm); 1692 if (err) 1693 goto out; 1694 } 1695 } 1696 out: 1697 return err; 1698 } 1699 1700 static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm, 1701 struct vm_area_struct *vma, unsigned long addr, pte_t pte) 1702 { 1703 u64 frame = 0, flags = 0; 1704 struct page *page = NULL; 1705 struct folio *folio; 1706 1707 if (pte_present(pte)) { 1708 if (pm->show_pfn) 1709 frame = pte_pfn(pte); 1710 flags |= PM_PRESENT; 1711 page = vm_normal_page(vma, addr, pte); 1712 if (pte_soft_dirty(pte)) 1713 flags |= PM_SOFT_DIRTY; 1714 if (pte_uffd_wp(pte)) 1715 flags |= PM_UFFD_WP; 1716 } else if (is_swap_pte(pte)) { 1717 swp_entry_t entry; 1718 if (pte_swp_soft_dirty(pte)) 1719 flags |= PM_SOFT_DIRTY; 1720 if (pte_swp_uffd_wp(pte)) 1721 flags |= PM_UFFD_WP; 1722 entry = pte_to_swp_entry(pte); 1723 if (pm->show_pfn) { 1724 pgoff_t offset; 1725 /* 1726 * For PFN swap offsets, keeping the offset field 1727 * to be PFN only to be compatible with old smaps. 1728 */ 1729 if (is_pfn_swap_entry(entry)) 1730 offset = swp_offset_pfn(entry); 1731 else 1732 offset = swp_offset(entry); 1733 frame = swp_type(entry) | 1734 (offset << MAX_SWAPFILES_SHIFT); 1735 } 1736 flags |= PM_SWAP; 1737 if (is_pfn_swap_entry(entry)) 1738 page = pfn_swap_entry_to_page(entry); 1739 if (pte_marker_entry_uffd_wp(entry)) 1740 flags |= PM_UFFD_WP; 1741 } 1742 1743 if (page) { 1744 folio = page_folio(page); 1745 if (!folio_test_anon(folio)) 1746 flags |= PM_FILE; 1747 if ((flags & PM_PRESENT) && 1748 folio_precise_page_mapcount(folio, page) == 1) 1749 flags |= PM_MMAP_EXCLUSIVE; 1750 } 1751 if (vma->vm_flags & VM_SOFTDIRTY) 1752 flags |= PM_SOFT_DIRTY; 1753 1754 return make_pme(frame, flags); 1755 } 1756 1757 static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end, 1758 struct mm_walk *walk) 1759 { 1760 struct vm_area_struct *vma = walk->vma; 1761 struct pagemapread *pm = walk->private; 1762 spinlock_t *ptl; 1763 pte_t *pte, *orig_pte; 1764 int err = 0; 1765 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1766 1767 ptl = pmd_trans_huge_lock(pmdp, vma); 1768 if (ptl) { 1769 unsigned int idx = (addr & ~PMD_MASK) >> PAGE_SHIFT; 1770 u64 flags = 0, frame = 0; 1771 pmd_t pmd = *pmdp; 1772 struct page *page = NULL; 1773 struct folio *folio = NULL; 1774 1775 if (vma->vm_flags & VM_SOFTDIRTY) 1776 flags |= PM_SOFT_DIRTY; 1777 1778 if (pmd_present(pmd)) { 1779 page = pmd_page(pmd); 1780 1781 flags |= PM_PRESENT; 1782 if (pmd_soft_dirty(pmd)) 1783 flags |= PM_SOFT_DIRTY; 1784 if (pmd_uffd_wp(pmd)) 1785 flags |= PM_UFFD_WP; 1786 if (pm->show_pfn) 1787 frame = pmd_pfn(pmd) + idx; 1788 } 1789 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 1790 else if (is_swap_pmd(pmd)) { 1791 swp_entry_t entry = pmd_to_swp_entry(pmd); 1792 unsigned long offset; 1793 1794 if (pm->show_pfn) { 1795 if (is_pfn_swap_entry(entry)) 1796 offset = swp_offset_pfn(entry) + idx; 1797 else 1798 offset = swp_offset(entry) + idx; 1799 frame = swp_type(entry) | 1800 (offset << MAX_SWAPFILES_SHIFT); 1801 } 1802 flags |= PM_SWAP; 1803 if (pmd_swp_soft_dirty(pmd)) 1804 flags |= PM_SOFT_DIRTY; 1805 if (pmd_swp_uffd_wp(pmd)) 1806 flags |= PM_UFFD_WP; 1807 VM_BUG_ON(!is_pmd_migration_entry(pmd)); 1808 page = pfn_swap_entry_to_page(entry); 1809 } 1810 #endif 1811 1812 if (page) { 1813 folio = page_folio(page); 1814 if (!folio_test_anon(folio)) 1815 flags |= PM_FILE; 1816 } 1817 1818 for (; addr != end; addr += PAGE_SIZE, idx++) { 1819 unsigned long cur_flags = flags; 1820 pagemap_entry_t pme; 1821 1822 if (folio && (flags & PM_PRESENT) && 1823 folio_precise_page_mapcount(folio, page + idx) == 1) 1824 cur_flags |= PM_MMAP_EXCLUSIVE; 1825 1826 pme = make_pme(frame, cur_flags); 1827 err = add_to_pagemap(&pme, pm); 1828 if (err) 1829 break; 1830 if (pm->show_pfn) { 1831 if (flags & PM_PRESENT) 1832 frame++; 1833 else if (flags & PM_SWAP) 1834 frame += (1 << MAX_SWAPFILES_SHIFT); 1835 } 1836 } 1837 spin_unlock(ptl); 1838 return err; 1839 } 1840 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 1841 1842 /* 1843 * We can assume that @vma always points to a valid one and @end never 1844 * goes beyond vma->vm_end. 1845 */ 1846 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl); 1847 if (!pte) { 1848 walk->action = ACTION_AGAIN; 1849 return err; 1850 } 1851 for (; addr < end; pte++, addr += PAGE_SIZE) { 1852 pagemap_entry_t pme; 1853 1854 pme = pte_to_pagemap_entry(pm, vma, addr, ptep_get(pte)); 1855 err = add_to_pagemap(&pme, pm); 1856 if (err) 1857 break; 1858 } 1859 pte_unmap_unlock(orig_pte, ptl); 1860 1861 cond_resched(); 1862 1863 return err; 1864 } 1865 1866 #ifdef CONFIG_HUGETLB_PAGE 1867 /* This function walks within one hugetlb entry in the single call */ 1868 static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask, 1869 unsigned long addr, unsigned long end, 1870 struct mm_walk *walk) 1871 { 1872 struct pagemapread *pm = walk->private; 1873 struct vm_area_struct *vma = walk->vma; 1874 u64 flags = 0, frame = 0; 1875 int err = 0; 1876 pte_t pte; 1877 1878 if (vma->vm_flags & VM_SOFTDIRTY) 1879 flags |= PM_SOFT_DIRTY; 1880 1881 pte = huge_ptep_get(walk->mm, addr, ptep); 1882 if (pte_present(pte)) { 1883 struct folio *folio = page_folio(pte_page(pte)); 1884 1885 if (!folio_test_anon(folio)) 1886 flags |= PM_FILE; 1887 1888 if (!folio_likely_mapped_shared(folio) && 1889 !hugetlb_pmd_shared(ptep)) 1890 flags |= PM_MMAP_EXCLUSIVE; 1891 1892 if (huge_pte_uffd_wp(pte)) 1893 flags |= PM_UFFD_WP; 1894 1895 flags |= PM_PRESENT; 1896 if (pm->show_pfn) 1897 frame = pte_pfn(pte) + 1898 ((addr & ~hmask) >> PAGE_SHIFT); 1899 } else if (pte_swp_uffd_wp_any(pte)) { 1900 flags |= PM_UFFD_WP; 1901 } 1902 1903 for (; addr != end; addr += PAGE_SIZE) { 1904 pagemap_entry_t pme = make_pme(frame, flags); 1905 1906 err = add_to_pagemap(&pme, pm); 1907 if (err) 1908 return err; 1909 if (pm->show_pfn && (flags & PM_PRESENT)) 1910 frame++; 1911 } 1912 1913 cond_resched(); 1914 1915 return err; 1916 } 1917 #else 1918 #define pagemap_hugetlb_range NULL 1919 #endif /* HUGETLB_PAGE */ 1920 1921 static const struct mm_walk_ops pagemap_ops = { 1922 .pmd_entry = pagemap_pmd_range, 1923 .pte_hole = pagemap_pte_hole, 1924 .hugetlb_entry = pagemap_hugetlb_range, 1925 .walk_lock = PGWALK_RDLOCK, 1926 }; 1927 1928 /* 1929 * /proc/pid/pagemap - an array mapping virtual pages to pfns 1930 * 1931 * For each page in the address space, this file contains one 64-bit entry 1932 * consisting of the following: 1933 * 1934 * Bits 0-54 page frame number (PFN) if present 1935 * Bits 0-4 swap type if swapped 1936 * Bits 5-54 swap offset if swapped 1937 * Bit 55 pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst) 1938 * Bit 56 page exclusively mapped 1939 * Bit 57 pte is uffd-wp write-protected 1940 * Bits 58-60 zero 1941 * Bit 61 page is file-page or shared-anon 1942 * Bit 62 page swapped 1943 * Bit 63 page present 1944 * 1945 * If the page is not present but in swap, then the PFN contains an 1946 * encoding of the swap file number and the page's offset into the 1947 * swap. Unmapped pages return a null PFN. This allows determining 1948 * precisely which pages are mapped (or in swap) and comparing mapped 1949 * pages between processes. 1950 * 1951 * Efficient users of this interface will use /proc/pid/maps to 1952 * determine which areas of memory are actually mapped and llseek to 1953 * skip over unmapped regions. 1954 */ 1955 static ssize_t pagemap_read(struct file *file, char __user *buf, 1956 size_t count, loff_t *ppos) 1957 { 1958 struct mm_struct *mm = file->private_data; 1959 struct pagemapread pm; 1960 unsigned long src; 1961 unsigned long svpfn; 1962 unsigned long start_vaddr; 1963 unsigned long end_vaddr; 1964 int ret = 0, copied = 0; 1965 1966 if (!mm || !mmget_not_zero(mm)) 1967 goto out; 1968 1969 ret = -EINVAL; 1970 /* file position must be aligned */ 1971 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES)) 1972 goto out_mm; 1973 1974 ret = 0; 1975 if (!count) 1976 goto out_mm; 1977 1978 /* do not disclose physical addresses: attack vector */ 1979 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN); 1980 1981 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT); 1982 pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL); 1983 ret = -ENOMEM; 1984 if (!pm.buffer) 1985 goto out_mm; 1986 1987 src = *ppos; 1988 svpfn = src / PM_ENTRY_BYTES; 1989 end_vaddr = mm->task_size; 1990 1991 /* watch out for wraparound */ 1992 start_vaddr = end_vaddr; 1993 if (svpfn <= (ULONG_MAX >> PAGE_SHIFT)) { 1994 unsigned long end; 1995 1996 ret = mmap_read_lock_killable(mm); 1997 if (ret) 1998 goto out_free; 1999 start_vaddr = untagged_addr_remote(mm, svpfn << PAGE_SHIFT); 2000 mmap_read_unlock(mm); 2001 2002 end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT); 2003 if (end >= start_vaddr && end < mm->task_size) 2004 end_vaddr = end; 2005 } 2006 2007 /* Ensure the address is inside the task */ 2008 if (start_vaddr > mm->task_size) 2009 start_vaddr = end_vaddr; 2010 2011 ret = 0; 2012 while (count && (start_vaddr < end_vaddr)) { 2013 int len; 2014 unsigned long end; 2015 2016 pm.pos = 0; 2017 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK; 2018 /* overflow ? */ 2019 if (end < start_vaddr || end > end_vaddr) 2020 end = end_vaddr; 2021 ret = mmap_read_lock_killable(mm); 2022 if (ret) 2023 goto out_free; 2024 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm); 2025 mmap_read_unlock(mm); 2026 start_vaddr = end; 2027 2028 len = min(count, PM_ENTRY_BYTES * pm.pos); 2029 if (copy_to_user(buf, pm.buffer, len)) { 2030 ret = -EFAULT; 2031 goto out_free; 2032 } 2033 copied += len; 2034 buf += len; 2035 count -= len; 2036 } 2037 *ppos += copied; 2038 if (!ret || ret == PM_END_OF_BUFFER) 2039 ret = copied; 2040 2041 out_free: 2042 kfree(pm.buffer); 2043 out_mm: 2044 mmput(mm); 2045 out: 2046 return ret; 2047 } 2048 2049 static int pagemap_open(struct inode *inode, struct file *file) 2050 { 2051 struct mm_struct *mm; 2052 2053 mm = proc_mem_open(inode, PTRACE_MODE_READ); 2054 if (IS_ERR(mm)) 2055 return PTR_ERR(mm); 2056 file->private_data = mm; 2057 return 0; 2058 } 2059 2060 static int pagemap_release(struct inode *inode, struct file *file) 2061 { 2062 struct mm_struct *mm = file->private_data; 2063 2064 if (mm) 2065 mmdrop(mm); 2066 return 0; 2067 } 2068 2069 #define PM_SCAN_CATEGORIES (PAGE_IS_WPALLOWED | PAGE_IS_WRITTEN | \ 2070 PAGE_IS_FILE | PAGE_IS_PRESENT | \ 2071 PAGE_IS_SWAPPED | PAGE_IS_PFNZERO | \ 2072 PAGE_IS_HUGE | PAGE_IS_SOFT_DIRTY) 2073 #define PM_SCAN_FLAGS (PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC) 2074 2075 struct pagemap_scan_private { 2076 struct pm_scan_arg arg; 2077 unsigned long masks_of_interest, cur_vma_category; 2078 struct page_region *vec_buf; 2079 unsigned long vec_buf_len, vec_buf_index, found_pages; 2080 struct page_region __user *vec_out; 2081 }; 2082 2083 static unsigned long pagemap_page_category(struct pagemap_scan_private *p, 2084 struct vm_area_struct *vma, 2085 unsigned long addr, pte_t pte) 2086 { 2087 unsigned long categories = 0; 2088 2089 if (pte_present(pte)) { 2090 struct page *page; 2091 2092 categories |= PAGE_IS_PRESENT; 2093 if (!pte_uffd_wp(pte)) 2094 categories |= PAGE_IS_WRITTEN; 2095 2096 if (p->masks_of_interest & PAGE_IS_FILE) { 2097 page = vm_normal_page(vma, addr, pte); 2098 if (page && !PageAnon(page)) 2099 categories |= PAGE_IS_FILE; 2100 } 2101 2102 if (is_zero_pfn(pte_pfn(pte))) 2103 categories |= PAGE_IS_PFNZERO; 2104 if (pte_soft_dirty(pte)) 2105 categories |= PAGE_IS_SOFT_DIRTY; 2106 } else if (is_swap_pte(pte)) { 2107 swp_entry_t swp; 2108 2109 categories |= PAGE_IS_SWAPPED; 2110 if (!pte_swp_uffd_wp_any(pte)) 2111 categories |= PAGE_IS_WRITTEN; 2112 2113 if (p->masks_of_interest & PAGE_IS_FILE) { 2114 swp = pte_to_swp_entry(pte); 2115 if (is_pfn_swap_entry(swp) && 2116 !folio_test_anon(pfn_swap_entry_folio(swp))) 2117 categories |= PAGE_IS_FILE; 2118 } 2119 if (pte_swp_soft_dirty(pte)) 2120 categories |= PAGE_IS_SOFT_DIRTY; 2121 } 2122 2123 return categories; 2124 } 2125 2126 static void make_uffd_wp_pte(struct vm_area_struct *vma, 2127 unsigned long addr, pte_t *pte, pte_t ptent) 2128 { 2129 if (pte_present(ptent)) { 2130 pte_t old_pte; 2131 2132 old_pte = ptep_modify_prot_start(vma, addr, pte); 2133 ptent = pte_mkuffd_wp(old_pte); 2134 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent); 2135 } else if (is_swap_pte(ptent)) { 2136 ptent = pte_swp_mkuffd_wp(ptent); 2137 set_pte_at(vma->vm_mm, addr, pte, ptent); 2138 } else { 2139 set_pte_at(vma->vm_mm, addr, pte, 2140 make_pte_marker(PTE_MARKER_UFFD_WP)); 2141 } 2142 } 2143 2144 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 2145 static unsigned long pagemap_thp_category(struct pagemap_scan_private *p, 2146 struct vm_area_struct *vma, 2147 unsigned long addr, pmd_t pmd) 2148 { 2149 unsigned long categories = PAGE_IS_HUGE; 2150 2151 if (pmd_present(pmd)) { 2152 struct page *page; 2153 2154 categories |= PAGE_IS_PRESENT; 2155 if (!pmd_uffd_wp(pmd)) 2156 categories |= PAGE_IS_WRITTEN; 2157 2158 if (p->masks_of_interest & PAGE_IS_FILE) { 2159 page = vm_normal_page_pmd(vma, addr, pmd); 2160 if (page && !PageAnon(page)) 2161 categories |= PAGE_IS_FILE; 2162 } 2163 2164 if (is_zero_pfn(pmd_pfn(pmd))) 2165 categories |= PAGE_IS_PFNZERO; 2166 if (pmd_soft_dirty(pmd)) 2167 categories |= PAGE_IS_SOFT_DIRTY; 2168 } else if (is_swap_pmd(pmd)) { 2169 swp_entry_t swp; 2170 2171 categories |= PAGE_IS_SWAPPED; 2172 if (!pmd_swp_uffd_wp(pmd)) 2173 categories |= PAGE_IS_WRITTEN; 2174 if (pmd_swp_soft_dirty(pmd)) 2175 categories |= PAGE_IS_SOFT_DIRTY; 2176 2177 if (p->masks_of_interest & PAGE_IS_FILE) { 2178 swp = pmd_to_swp_entry(pmd); 2179 if (is_pfn_swap_entry(swp) && 2180 !folio_test_anon(pfn_swap_entry_folio(swp))) 2181 categories |= PAGE_IS_FILE; 2182 } 2183 } 2184 2185 return categories; 2186 } 2187 2188 static void make_uffd_wp_pmd(struct vm_area_struct *vma, 2189 unsigned long addr, pmd_t *pmdp) 2190 { 2191 pmd_t old, pmd = *pmdp; 2192 2193 if (pmd_present(pmd)) { 2194 old = pmdp_invalidate_ad(vma, addr, pmdp); 2195 pmd = pmd_mkuffd_wp(old); 2196 set_pmd_at(vma->vm_mm, addr, pmdp, pmd); 2197 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) { 2198 pmd = pmd_swp_mkuffd_wp(pmd); 2199 set_pmd_at(vma->vm_mm, addr, pmdp, pmd); 2200 } 2201 } 2202 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 2203 2204 #ifdef CONFIG_HUGETLB_PAGE 2205 static unsigned long pagemap_hugetlb_category(pte_t pte) 2206 { 2207 unsigned long categories = PAGE_IS_HUGE; 2208 2209 /* 2210 * According to pagemap_hugetlb_range(), file-backed HugeTLB 2211 * page cannot be swapped. So PAGE_IS_FILE is not checked for 2212 * swapped pages. 2213 */ 2214 if (pte_present(pte)) { 2215 categories |= PAGE_IS_PRESENT; 2216 if (!huge_pte_uffd_wp(pte)) 2217 categories |= PAGE_IS_WRITTEN; 2218 if (!PageAnon(pte_page(pte))) 2219 categories |= PAGE_IS_FILE; 2220 if (is_zero_pfn(pte_pfn(pte))) 2221 categories |= PAGE_IS_PFNZERO; 2222 if (pte_soft_dirty(pte)) 2223 categories |= PAGE_IS_SOFT_DIRTY; 2224 } else if (is_swap_pte(pte)) { 2225 categories |= PAGE_IS_SWAPPED; 2226 if (!pte_swp_uffd_wp_any(pte)) 2227 categories |= PAGE_IS_WRITTEN; 2228 if (pte_swp_soft_dirty(pte)) 2229 categories |= PAGE_IS_SOFT_DIRTY; 2230 } 2231 2232 return categories; 2233 } 2234 2235 static void make_uffd_wp_huge_pte(struct vm_area_struct *vma, 2236 unsigned long addr, pte_t *ptep, 2237 pte_t ptent) 2238 { 2239 unsigned long psize; 2240 2241 if (is_hugetlb_entry_hwpoisoned(ptent) || is_pte_marker(ptent)) 2242 return; 2243 2244 psize = huge_page_size(hstate_vma(vma)); 2245 2246 if (is_hugetlb_entry_migration(ptent)) 2247 set_huge_pte_at(vma->vm_mm, addr, ptep, 2248 pte_swp_mkuffd_wp(ptent), psize); 2249 else if (!huge_pte_none(ptent)) 2250 huge_ptep_modify_prot_commit(vma, addr, ptep, ptent, 2251 huge_pte_mkuffd_wp(ptent)); 2252 else 2253 set_huge_pte_at(vma->vm_mm, addr, ptep, 2254 make_pte_marker(PTE_MARKER_UFFD_WP), psize); 2255 } 2256 #endif /* CONFIG_HUGETLB_PAGE */ 2257 2258 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLB_PAGE) 2259 static void pagemap_scan_backout_range(struct pagemap_scan_private *p, 2260 unsigned long addr, unsigned long end) 2261 { 2262 struct page_region *cur_buf = &p->vec_buf[p->vec_buf_index]; 2263 2264 if (cur_buf->start != addr) 2265 cur_buf->end = addr; 2266 else 2267 cur_buf->start = cur_buf->end = 0; 2268 2269 p->found_pages -= (end - addr) / PAGE_SIZE; 2270 } 2271 #endif 2272 2273 static bool pagemap_scan_is_interesting_page(unsigned long categories, 2274 const struct pagemap_scan_private *p) 2275 { 2276 categories ^= p->arg.category_inverted; 2277 if ((categories & p->arg.category_mask) != p->arg.category_mask) 2278 return false; 2279 if (p->arg.category_anyof_mask && !(categories & p->arg.category_anyof_mask)) 2280 return false; 2281 2282 return true; 2283 } 2284 2285 static bool pagemap_scan_is_interesting_vma(unsigned long categories, 2286 const struct pagemap_scan_private *p) 2287 { 2288 unsigned long required = p->arg.category_mask & PAGE_IS_WPALLOWED; 2289 2290 categories ^= p->arg.category_inverted; 2291 if ((categories & required) != required) 2292 return false; 2293 2294 return true; 2295 } 2296 2297 static int pagemap_scan_test_walk(unsigned long start, unsigned long end, 2298 struct mm_walk *walk) 2299 { 2300 struct pagemap_scan_private *p = walk->private; 2301 struct vm_area_struct *vma = walk->vma; 2302 unsigned long vma_category = 0; 2303 bool wp_allowed = userfaultfd_wp_async(vma) && 2304 userfaultfd_wp_use_markers(vma); 2305 2306 if (!wp_allowed) { 2307 /* User requested explicit failure over wp-async capability */ 2308 if (p->arg.flags & PM_SCAN_CHECK_WPASYNC) 2309 return -EPERM; 2310 /* 2311 * User requires wr-protect, and allows silently skipping 2312 * unsupported vmas. 2313 */ 2314 if (p->arg.flags & PM_SCAN_WP_MATCHING) 2315 return 1; 2316 /* 2317 * Then the request doesn't involve wr-protects at all, 2318 * fall through to the rest checks, and allow vma walk. 2319 */ 2320 } 2321 2322 if (vma->vm_flags & VM_PFNMAP) 2323 return 1; 2324 2325 if (wp_allowed) 2326 vma_category |= PAGE_IS_WPALLOWED; 2327 2328 if (vma->vm_flags & VM_SOFTDIRTY) 2329 vma_category |= PAGE_IS_SOFT_DIRTY; 2330 2331 if (!pagemap_scan_is_interesting_vma(vma_category, p)) 2332 return 1; 2333 2334 p->cur_vma_category = vma_category; 2335 2336 return 0; 2337 } 2338 2339 static bool pagemap_scan_push_range(unsigned long categories, 2340 struct pagemap_scan_private *p, 2341 unsigned long addr, unsigned long end) 2342 { 2343 struct page_region *cur_buf = &p->vec_buf[p->vec_buf_index]; 2344 2345 /* 2346 * When there is no output buffer provided at all, the sentinel values 2347 * won't match here. There is no other way for `cur_buf->end` to be 2348 * non-zero other than it being non-empty. 2349 */ 2350 if (addr == cur_buf->end && categories == cur_buf->categories) { 2351 cur_buf->end = end; 2352 return true; 2353 } 2354 2355 if (cur_buf->end) { 2356 if (p->vec_buf_index >= p->vec_buf_len - 1) 2357 return false; 2358 2359 cur_buf = &p->vec_buf[++p->vec_buf_index]; 2360 } 2361 2362 cur_buf->start = addr; 2363 cur_buf->end = end; 2364 cur_buf->categories = categories; 2365 2366 return true; 2367 } 2368 2369 static int pagemap_scan_output(unsigned long categories, 2370 struct pagemap_scan_private *p, 2371 unsigned long addr, unsigned long *end) 2372 { 2373 unsigned long n_pages, total_pages; 2374 int ret = 0; 2375 2376 if (!p->vec_buf) 2377 return 0; 2378 2379 categories &= p->arg.return_mask; 2380 2381 n_pages = (*end - addr) / PAGE_SIZE; 2382 if (check_add_overflow(p->found_pages, n_pages, &total_pages) || 2383 total_pages > p->arg.max_pages) { 2384 size_t n_too_much = total_pages - p->arg.max_pages; 2385 *end -= n_too_much * PAGE_SIZE; 2386 n_pages -= n_too_much; 2387 ret = -ENOSPC; 2388 } 2389 2390 if (!pagemap_scan_push_range(categories, p, addr, *end)) { 2391 *end = addr; 2392 n_pages = 0; 2393 ret = -ENOSPC; 2394 } 2395 2396 p->found_pages += n_pages; 2397 if (ret) 2398 p->arg.walk_end = *end; 2399 2400 return ret; 2401 } 2402 2403 static int pagemap_scan_thp_entry(pmd_t *pmd, unsigned long start, 2404 unsigned long end, struct mm_walk *walk) 2405 { 2406 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 2407 struct pagemap_scan_private *p = walk->private; 2408 struct vm_area_struct *vma = walk->vma; 2409 unsigned long categories; 2410 spinlock_t *ptl; 2411 int ret = 0; 2412 2413 ptl = pmd_trans_huge_lock(pmd, vma); 2414 if (!ptl) 2415 return -ENOENT; 2416 2417 categories = p->cur_vma_category | 2418 pagemap_thp_category(p, vma, start, *pmd); 2419 2420 if (!pagemap_scan_is_interesting_page(categories, p)) 2421 goto out_unlock; 2422 2423 ret = pagemap_scan_output(categories, p, start, &end); 2424 if (start == end) 2425 goto out_unlock; 2426 2427 if (~p->arg.flags & PM_SCAN_WP_MATCHING) 2428 goto out_unlock; 2429 if (~categories & PAGE_IS_WRITTEN) 2430 goto out_unlock; 2431 2432 /* 2433 * Break huge page into small pages if the WP operation 2434 * needs to be performed on a portion of the huge page. 2435 */ 2436 if (end != start + HPAGE_SIZE) { 2437 spin_unlock(ptl); 2438 split_huge_pmd(vma, pmd, start); 2439 pagemap_scan_backout_range(p, start, end); 2440 /* Report as if there was no THP */ 2441 return -ENOENT; 2442 } 2443 2444 make_uffd_wp_pmd(vma, start, pmd); 2445 flush_tlb_range(vma, start, end); 2446 out_unlock: 2447 spin_unlock(ptl); 2448 return ret; 2449 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 2450 return -ENOENT; 2451 #endif 2452 } 2453 2454 static int pagemap_scan_pmd_entry(pmd_t *pmd, unsigned long start, 2455 unsigned long end, struct mm_walk *walk) 2456 { 2457 struct pagemap_scan_private *p = walk->private; 2458 struct vm_area_struct *vma = walk->vma; 2459 unsigned long addr, flush_end = 0; 2460 pte_t *pte, *start_pte; 2461 spinlock_t *ptl; 2462 int ret; 2463 2464 arch_enter_lazy_mmu_mode(); 2465 2466 ret = pagemap_scan_thp_entry(pmd, start, end, walk); 2467 if (ret != -ENOENT) { 2468 arch_leave_lazy_mmu_mode(); 2469 return ret; 2470 } 2471 2472 ret = 0; 2473 start_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl); 2474 if (!pte) { 2475 arch_leave_lazy_mmu_mode(); 2476 walk->action = ACTION_AGAIN; 2477 return 0; 2478 } 2479 2480 if ((p->arg.flags & PM_SCAN_WP_MATCHING) && !p->vec_out) { 2481 /* Fast path for performing exclusive WP */ 2482 for (addr = start; addr != end; pte++, addr += PAGE_SIZE) { 2483 pte_t ptent = ptep_get(pte); 2484 2485 if ((pte_present(ptent) && pte_uffd_wp(ptent)) || 2486 pte_swp_uffd_wp_any(ptent)) 2487 continue; 2488 make_uffd_wp_pte(vma, addr, pte, ptent); 2489 if (!flush_end) 2490 start = addr; 2491 flush_end = addr + PAGE_SIZE; 2492 } 2493 goto flush_and_return; 2494 } 2495 2496 if (!p->arg.category_anyof_mask && !p->arg.category_inverted && 2497 p->arg.category_mask == PAGE_IS_WRITTEN && 2498 p->arg.return_mask == PAGE_IS_WRITTEN) { 2499 for (addr = start; addr < end; pte++, addr += PAGE_SIZE) { 2500 unsigned long next = addr + PAGE_SIZE; 2501 pte_t ptent = ptep_get(pte); 2502 2503 if ((pte_present(ptent) && pte_uffd_wp(ptent)) || 2504 pte_swp_uffd_wp_any(ptent)) 2505 continue; 2506 ret = pagemap_scan_output(p->cur_vma_category | PAGE_IS_WRITTEN, 2507 p, addr, &next); 2508 if (next == addr) 2509 break; 2510 if (~p->arg.flags & PM_SCAN_WP_MATCHING) 2511 continue; 2512 make_uffd_wp_pte(vma, addr, pte, ptent); 2513 if (!flush_end) 2514 start = addr; 2515 flush_end = next; 2516 } 2517 goto flush_and_return; 2518 } 2519 2520 for (addr = start; addr != end; pte++, addr += PAGE_SIZE) { 2521 pte_t ptent = ptep_get(pte); 2522 unsigned long categories = p->cur_vma_category | 2523 pagemap_page_category(p, vma, addr, ptent); 2524 unsigned long next = addr + PAGE_SIZE; 2525 2526 if (!pagemap_scan_is_interesting_page(categories, p)) 2527 continue; 2528 2529 ret = pagemap_scan_output(categories, p, addr, &next); 2530 if (next == addr) 2531 break; 2532 2533 if (~p->arg.flags & PM_SCAN_WP_MATCHING) 2534 continue; 2535 if (~categories & PAGE_IS_WRITTEN) 2536 continue; 2537 2538 make_uffd_wp_pte(vma, addr, pte, ptent); 2539 if (!flush_end) 2540 start = addr; 2541 flush_end = next; 2542 } 2543 2544 flush_and_return: 2545 if (flush_end) 2546 flush_tlb_range(vma, start, addr); 2547 2548 pte_unmap_unlock(start_pte, ptl); 2549 arch_leave_lazy_mmu_mode(); 2550 2551 cond_resched(); 2552 return ret; 2553 } 2554 2555 #ifdef CONFIG_HUGETLB_PAGE 2556 static int pagemap_scan_hugetlb_entry(pte_t *ptep, unsigned long hmask, 2557 unsigned long start, unsigned long end, 2558 struct mm_walk *walk) 2559 { 2560 struct pagemap_scan_private *p = walk->private; 2561 struct vm_area_struct *vma = walk->vma; 2562 unsigned long categories; 2563 spinlock_t *ptl; 2564 int ret = 0; 2565 pte_t pte; 2566 2567 if (~p->arg.flags & PM_SCAN_WP_MATCHING) { 2568 /* Go the short route when not write-protecting pages. */ 2569 2570 pte = huge_ptep_get(walk->mm, start, ptep); 2571 categories = p->cur_vma_category | pagemap_hugetlb_category(pte); 2572 2573 if (!pagemap_scan_is_interesting_page(categories, p)) 2574 return 0; 2575 2576 return pagemap_scan_output(categories, p, start, &end); 2577 } 2578 2579 i_mmap_lock_write(vma->vm_file->f_mapping); 2580 ptl = huge_pte_lock(hstate_vma(vma), vma->vm_mm, ptep); 2581 2582 pte = huge_ptep_get(walk->mm, start, ptep); 2583 categories = p->cur_vma_category | pagemap_hugetlb_category(pte); 2584 2585 if (!pagemap_scan_is_interesting_page(categories, p)) 2586 goto out_unlock; 2587 2588 ret = pagemap_scan_output(categories, p, start, &end); 2589 if (start == end) 2590 goto out_unlock; 2591 2592 if (~categories & PAGE_IS_WRITTEN) 2593 goto out_unlock; 2594 2595 if (end != start + HPAGE_SIZE) { 2596 /* Partial HugeTLB page WP isn't possible. */ 2597 pagemap_scan_backout_range(p, start, end); 2598 p->arg.walk_end = start; 2599 ret = 0; 2600 goto out_unlock; 2601 } 2602 2603 make_uffd_wp_huge_pte(vma, start, ptep, pte); 2604 flush_hugetlb_tlb_range(vma, start, end); 2605 2606 out_unlock: 2607 spin_unlock(ptl); 2608 i_mmap_unlock_write(vma->vm_file->f_mapping); 2609 2610 return ret; 2611 } 2612 #else 2613 #define pagemap_scan_hugetlb_entry NULL 2614 #endif 2615 2616 static int pagemap_scan_pte_hole(unsigned long addr, unsigned long end, 2617 int depth, struct mm_walk *walk) 2618 { 2619 struct pagemap_scan_private *p = walk->private; 2620 struct vm_area_struct *vma = walk->vma; 2621 int ret, err; 2622 2623 if (!vma || !pagemap_scan_is_interesting_page(p->cur_vma_category, p)) 2624 return 0; 2625 2626 ret = pagemap_scan_output(p->cur_vma_category, p, addr, &end); 2627 if (addr == end) 2628 return ret; 2629 2630 if (~p->arg.flags & PM_SCAN_WP_MATCHING) 2631 return ret; 2632 2633 err = uffd_wp_range(vma, addr, end - addr, true); 2634 if (err < 0) 2635 ret = err; 2636 2637 return ret; 2638 } 2639 2640 static const struct mm_walk_ops pagemap_scan_ops = { 2641 .test_walk = pagemap_scan_test_walk, 2642 .pmd_entry = pagemap_scan_pmd_entry, 2643 .pte_hole = pagemap_scan_pte_hole, 2644 .hugetlb_entry = pagemap_scan_hugetlb_entry, 2645 }; 2646 2647 static int pagemap_scan_get_args(struct pm_scan_arg *arg, 2648 unsigned long uarg) 2649 { 2650 if (copy_from_user(arg, (void __user *)uarg, sizeof(*arg))) 2651 return -EFAULT; 2652 2653 if (arg->size != sizeof(struct pm_scan_arg)) 2654 return -EINVAL; 2655 2656 /* Validate requested features */ 2657 if (arg->flags & ~PM_SCAN_FLAGS) 2658 return -EINVAL; 2659 if ((arg->category_inverted | arg->category_mask | 2660 arg->category_anyof_mask | arg->return_mask) & ~PM_SCAN_CATEGORIES) 2661 return -EINVAL; 2662 2663 arg->start = untagged_addr((unsigned long)arg->start); 2664 arg->end = untagged_addr((unsigned long)arg->end); 2665 arg->vec = untagged_addr((unsigned long)arg->vec); 2666 2667 /* Validate memory pointers */ 2668 if (!IS_ALIGNED(arg->start, PAGE_SIZE)) 2669 return -EINVAL; 2670 if (!access_ok((void __user *)(long)arg->start, arg->end - arg->start)) 2671 return -EFAULT; 2672 if (!arg->vec && arg->vec_len) 2673 return -EINVAL; 2674 if (arg->vec && !access_ok((void __user *)(long)arg->vec, 2675 arg->vec_len * sizeof(struct page_region))) 2676 return -EFAULT; 2677 2678 /* Fixup default values */ 2679 arg->end = ALIGN(arg->end, PAGE_SIZE); 2680 arg->walk_end = 0; 2681 if (!arg->max_pages) 2682 arg->max_pages = ULONG_MAX; 2683 2684 return 0; 2685 } 2686 2687 static int pagemap_scan_writeback_args(struct pm_scan_arg *arg, 2688 unsigned long uargl) 2689 { 2690 struct pm_scan_arg __user *uarg = (void __user *)uargl; 2691 2692 if (copy_to_user(&uarg->walk_end, &arg->walk_end, sizeof(arg->walk_end))) 2693 return -EFAULT; 2694 2695 return 0; 2696 } 2697 2698 static int pagemap_scan_init_bounce_buffer(struct pagemap_scan_private *p) 2699 { 2700 if (!p->arg.vec_len) 2701 return 0; 2702 2703 p->vec_buf_len = min_t(size_t, PAGEMAP_WALK_SIZE >> PAGE_SHIFT, 2704 p->arg.vec_len); 2705 p->vec_buf = kmalloc_array(p->vec_buf_len, sizeof(*p->vec_buf), 2706 GFP_KERNEL); 2707 if (!p->vec_buf) 2708 return -ENOMEM; 2709 2710 p->vec_buf->start = p->vec_buf->end = 0; 2711 p->vec_out = (struct page_region __user *)(long)p->arg.vec; 2712 2713 return 0; 2714 } 2715 2716 static long pagemap_scan_flush_buffer(struct pagemap_scan_private *p) 2717 { 2718 const struct page_region *buf = p->vec_buf; 2719 long n = p->vec_buf_index; 2720 2721 if (!p->vec_buf) 2722 return 0; 2723 2724 if (buf[n].end != buf[n].start) 2725 n++; 2726 2727 if (!n) 2728 return 0; 2729 2730 if (copy_to_user(p->vec_out, buf, n * sizeof(*buf))) 2731 return -EFAULT; 2732 2733 p->arg.vec_len -= n; 2734 p->vec_out += n; 2735 2736 p->vec_buf_index = 0; 2737 p->vec_buf_len = min_t(size_t, p->vec_buf_len, p->arg.vec_len); 2738 p->vec_buf->start = p->vec_buf->end = 0; 2739 2740 return n; 2741 } 2742 2743 static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg) 2744 { 2745 struct pagemap_scan_private p = {0}; 2746 unsigned long walk_start; 2747 size_t n_ranges_out = 0; 2748 int ret; 2749 2750 ret = pagemap_scan_get_args(&p.arg, uarg); 2751 if (ret) 2752 return ret; 2753 2754 p.masks_of_interest = p.arg.category_mask | p.arg.category_anyof_mask | 2755 p.arg.return_mask; 2756 ret = pagemap_scan_init_bounce_buffer(&p); 2757 if (ret) 2758 return ret; 2759 2760 for (walk_start = p.arg.start; walk_start < p.arg.end; 2761 walk_start = p.arg.walk_end) { 2762 struct mmu_notifier_range range; 2763 long n_out; 2764 2765 if (fatal_signal_pending(current)) { 2766 ret = -EINTR; 2767 break; 2768 } 2769 2770 ret = mmap_read_lock_killable(mm); 2771 if (ret) 2772 break; 2773 2774 /* Protection change for the range is going to happen. */ 2775 if (p.arg.flags & PM_SCAN_WP_MATCHING) { 2776 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 0, 2777 mm, walk_start, p.arg.end); 2778 mmu_notifier_invalidate_range_start(&range); 2779 } 2780 2781 ret = walk_page_range(mm, walk_start, p.arg.end, 2782 &pagemap_scan_ops, &p); 2783 2784 if (p.arg.flags & PM_SCAN_WP_MATCHING) 2785 mmu_notifier_invalidate_range_end(&range); 2786 2787 mmap_read_unlock(mm); 2788 2789 n_out = pagemap_scan_flush_buffer(&p); 2790 if (n_out < 0) 2791 ret = n_out; 2792 else 2793 n_ranges_out += n_out; 2794 2795 if (ret != -ENOSPC) 2796 break; 2797 2798 if (p.arg.vec_len == 0 || p.found_pages == p.arg.max_pages) 2799 break; 2800 } 2801 2802 /* ENOSPC signifies early stop (buffer full) from the walk. */ 2803 if (!ret || ret == -ENOSPC) 2804 ret = n_ranges_out; 2805 2806 /* The walk_end isn't set when ret is zero */ 2807 if (!p.arg.walk_end) 2808 p.arg.walk_end = p.arg.end; 2809 if (pagemap_scan_writeback_args(&p.arg, uarg)) 2810 ret = -EFAULT; 2811 2812 kfree(p.vec_buf); 2813 return ret; 2814 } 2815 2816 static long do_pagemap_cmd(struct file *file, unsigned int cmd, 2817 unsigned long arg) 2818 { 2819 struct mm_struct *mm = file->private_data; 2820 2821 switch (cmd) { 2822 case PAGEMAP_SCAN: 2823 return do_pagemap_scan(mm, arg); 2824 2825 default: 2826 return -EINVAL; 2827 } 2828 } 2829 2830 const struct file_operations proc_pagemap_operations = { 2831 .llseek = mem_lseek, /* borrow this */ 2832 .read = pagemap_read, 2833 .open = pagemap_open, 2834 .release = pagemap_release, 2835 .unlocked_ioctl = do_pagemap_cmd, 2836 .compat_ioctl = do_pagemap_cmd, 2837 }; 2838 #endif /* CONFIG_PROC_PAGE_MONITOR */ 2839 2840 #ifdef CONFIG_NUMA 2841 2842 struct numa_maps { 2843 unsigned long pages; 2844 unsigned long anon; 2845 unsigned long active; 2846 unsigned long writeback; 2847 unsigned long mapcount_max; 2848 unsigned long dirty; 2849 unsigned long swapcache; 2850 unsigned long node[MAX_NUMNODES]; 2851 }; 2852 2853 struct numa_maps_private { 2854 struct proc_maps_private proc_maps; 2855 struct numa_maps md; 2856 }; 2857 2858 static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty, 2859 unsigned long nr_pages) 2860 { 2861 struct folio *folio = page_folio(page); 2862 int count = folio_precise_page_mapcount(folio, page); 2863 2864 md->pages += nr_pages; 2865 if (pte_dirty || folio_test_dirty(folio)) 2866 md->dirty += nr_pages; 2867 2868 if (folio_test_swapcache(folio)) 2869 md->swapcache += nr_pages; 2870 2871 if (folio_test_active(folio) || folio_test_unevictable(folio)) 2872 md->active += nr_pages; 2873 2874 if (folio_test_writeback(folio)) 2875 md->writeback += nr_pages; 2876 2877 if (folio_test_anon(folio)) 2878 md->anon += nr_pages; 2879 2880 if (count > md->mapcount_max) 2881 md->mapcount_max = count; 2882 2883 md->node[folio_nid(folio)] += nr_pages; 2884 } 2885 2886 static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma, 2887 unsigned long addr) 2888 { 2889 struct page *page; 2890 int nid; 2891 2892 if (!pte_present(pte)) 2893 return NULL; 2894 2895 page = vm_normal_page(vma, addr, pte); 2896 if (!page || is_zone_device_page(page)) 2897 return NULL; 2898 2899 if (PageReserved(page)) 2900 return NULL; 2901 2902 nid = page_to_nid(page); 2903 if (!node_isset(nid, node_states[N_MEMORY])) 2904 return NULL; 2905 2906 return page; 2907 } 2908 2909 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 2910 static struct page *can_gather_numa_stats_pmd(pmd_t pmd, 2911 struct vm_area_struct *vma, 2912 unsigned long addr) 2913 { 2914 struct page *page; 2915 int nid; 2916 2917 if (!pmd_present(pmd)) 2918 return NULL; 2919 2920 page = vm_normal_page_pmd(vma, addr, pmd); 2921 if (!page) 2922 return NULL; 2923 2924 if (PageReserved(page)) 2925 return NULL; 2926 2927 nid = page_to_nid(page); 2928 if (!node_isset(nid, node_states[N_MEMORY])) 2929 return NULL; 2930 2931 return page; 2932 } 2933 #endif 2934 2935 static int gather_pte_stats(pmd_t *pmd, unsigned long addr, 2936 unsigned long end, struct mm_walk *walk) 2937 { 2938 struct numa_maps *md = walk->private; 2939 struct vm_area_struct *vma = walk->vma; 2940 spinlock_t *ptl; 2941 pte_t *orig_pte; 2942 pte_t *pte; 2943 2944 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 2945 ptl = pmd_trans_huge_lock(pmd, vma); 2946 if (ptl) { 2947 struct page *page; 2948 2949 page = can_gather_numa_stats_pmd(*pmd, vma, addr); 2950 if (page) 2951 gather_stats(page, md, pmd_dirty(*pmd), 2952 HPAGE_PMD_SIZE/PAGE_SIZE); 2953 spin_unlock(ptl); 2954 return 0; 2955 } 2956 #endif 2957 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); 2958 if (!pte) { 2959 walk->action = ACTION_AGAIN; 2960 return 0; 2961 } 2962 do { 2963 pte_t ptent = ptep_get(pte); 2964 struct page *page = can_gather_numa_stats(ptent, vma, addr); 2965 if (!page) 2966 continue; 2967 gather_stats(page, md, pte_dirty(ptent), 1); 2968 2969 } while (pte++, addr += PAGE_SIZE, addr != end); 2970 pte_unmap_unlock(orig_pte, ptl); 2971 cond_resched(); 2972 return 0; 2973 } 2974 #ifdef CONFIG_HUGETLB_PAGE 2975 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask, 2976 unsigned long addr, unsigned long end, struct mm_walk *walk) 2977 { 2978 pte_t huge_pte = huge_ptep_get(walk->mm, addr, pte); 2979 struct numa_maps *md; 2980 struct page *page; 2981 2982 if (!pte_present(huge_pte)) 2983 return 0; 2984 2985 page = pte_page(huge_pte); 2986 2987 md = walk->private; 2988 gather_stats(page, md, pte_dirty(huge_pte), 1); 2989 return 0; 2990 } 2991 2992 #else 2993 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask, 2994 unsigned long addr, unsigned long end, struct mm_walk *walk) 2995 { 2996 return 0; 2997 } 2998 #endif 2999 3000 static const struct mm_walk_ops show_numa_ops = { 3001 .hugetlb_entry = gather_hugetlb_stats, 3002 .pmd_entry = gather_pte_stats, 3003 .walk_lock = PGWALK_RDLOCK, 3004 }; 3005 3006 /* 3007 * Display pages allocated per node and memory policy via /proc. 3008 */ 3009 static int show_numa_map(struct seq_file *m, void *v) 3010 { 3011 struct numa_maps_private *numa_priv = m->private; 3012 struct proc_maps_private *proc_priv = &numa_priv->proc_maps; 3013 struct vm_area_struct *vma = v; 3014 struct numa_maps *md = &numa_priv->md; 3015 struct file *file = vma->vm_file; 3016 struct mm_struct *mm = vma->vm_mm; 3017 char buffer[64]; 3018 struct mempolicy *pol; 3019 pgoff_t ilx; 3020 int nid; 3021 3022 if (!mm) 3023 return 0; 3024 3025 /* Ensure we start with an empty set of numa_maps statistics. */ 3026 memset(md, 0, sizeof(*md)); 3027 3028 pol = __get_vma_policy(vma, vma->vm_start, &ilx); 3029 if (pol) { 3030 mpol_to_str(buffer, sizeof(buffer), pol); 3031 mpol_cond_put(pol); 3032 } else { 3033 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy); 3034 } 3035 3036 seq_printf(m, "%08lx %s", vma->vm_start, buffer); 3037 3038 if (file) { 3039 seq_puts(m, " file="); 3040 seq_path(m, file_user_path(file), "\n\t= "); 3041 } else if (vma_is_initial_heap(vma)) { 3042 seq_puts(m, " heap"); 3043 } else if (vma_is_initial_stack(vma)) { 3044 seq_puts(m, " stack"); 3045 } 3046 3047 if (is_vm_hugetlb_page(vma)) 3048 seq_puts(m, " huge"); 3049 3050 /* mmap_lock is held by m_start */ 3051 walk_page_vma(vma, &show_numa_ops, md); 3052 3053 if (!md->pages) 3054 goto out; 3055 3056 if (md->anon) 3057 seq_printf(m, " anon=%lu", md->anon); 3058 3059 if (md->dirty) 3060 seq_printf(m, " dirty=%lu", md->dirty); 3061 3062 if (md->pages != md->anon && md->pages != md->dirty) 3063 seq_printf(m, " mapped=%lu", md->pages); 3064 3065 if (md->mapcount_max > 1) 3066 seq_printf(m, " mapmax=%lu", md->mapcount_max); 3067 3068 if (md->swapcache) 3069 seq_printf(m, " swapcache=%lu", md->swapcache); 3070 3071 if (md->active < md->pages && !is_vm_hugetlb_page(vma)) 3072 seq_printf(m, " active=%lu", md->active); 3073 3074 if (md->writeback) 3075 seq_printf(m, " writeback=%lu", md->writeback); 3076 3077 for_each_node_state(nid, N_MEMORY) 3078 if (md->node[nid]) 3079 seq_printf(m, " N%d=%lu", nid, md->node[nid]); 3080 3081 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10); 3082 out: 3083 seq_putc(m, '\n'); 3084 return 0; 3085 } 3086 3087 static const struct seq_operations proc_pid_numa_maps_op = { 3088 .start = m_start, 3089 .next = m_next, 3090 .stop = m_stop, 3091 .show = show_numa_map, 3092 }; 3093 3094 static int pid_numa_maps_open(struct inode *inode, struct file *file) 3095 { 3096 return proc_maps_open(inode, file, &proc_pid_numa_maps_op, 3097 sizeof(struct numa_maps_private)); 3098 } 3099 3100 const struct file_operations proc_pid_numa_maps_operations = { 3101 .open = pid_numa_maps_open, 3102 .read = seq_read, 3103 .llseek = seq_lseek, 3104 .release = proc_map_release, 3105 }; 3106 3107 #endif /* CONFIG_NUMA */ 3108