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