1 /*- 2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU) 3 * 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * The Mach Operating System project at Carnegie-Mellon University. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * 35 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 36 * All rights reserved. 37 * 38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 39 * 40 * Permission to use, copy, modify and distribute this software and 41 * its documentation is hereby granted, provided that both the copyright 42 * notice and this permission notice appear in all copies of the 43 * software, derivative works or modified versions, and any portions 44 * thereof, and that both notices appear in supporting documentation. 45 * 46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 49 * 50 * Carnegie Mellon requests users of this software to return to 51 * 52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 53 * School of Computer Science 54 * Carnegie Mellon University 55 * Pittsburgh PA 15213-3890 56 * 57 * any improvements or extensions that they make and grant Carnegie the 58 * rights to redistribute these changes. 59 */ 60 61 /* 62 * Virtual memory object module. 63 */ 64 65 #include "opt_vm.h" 66 67 #include <sys/systm.h> 68 #include <sys/blockcount.h> 69 #include <sys/conf.h> 70 #include <sys/cpuset.h> 71 #include <sys/ipc.h> 72 #include <sys/jail.h> 73 #include <sys/limits.h> 74 #include <sys/lock.h> 75 #include <sys/mman.h> 76 #include <sys/mount.h> 77 #include <sys/kernel.h> 78 #include <sys/mutex.h> 79 #include <sys/pctrie.h> 80 #include <sys/proc.h> 81 #include <sys/refcount.h> 82 #include <sys/shm.h> 83 #include <sys/sx.h> 84 #include <sys/sysctl.h> 85 #include <sys/resourcevar.h> 86 #include <sys/refcount.h> 87 #include <sys/rwlock.h> 88 #include <sys/user.h> 89 #include <sys/vnode.h> 90 #include <sys/vmmeter.h> 91 92 #include <vm/vm.h> 93 #include <vm/vm_param.h> 94 #include <vm/pmap.h> 95 #include <vm/vm_map.h> 96 #include <vm/vm_object.h> 97 #include <vm/vm_page.h> 98 #include <vm/vm_pageout.h> 99 #include <vm/vm_pager.h> 100 #include <vm/vm_phys.h> 101 #include <vm/vm_pagequeue.h> 102 #include <vm/swap_pager.h> 103 #include <vm/vm_kern.h> 104 #include <vm/vm_extern.h> 105 #include <vm/vm_radix.h> 106 #include <vm/vm_reserv.h> 107 #include <vm/uma.h> 108 109 static int old_msync; 110 SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0, 111 "Use old (insecure) msync behavior"); 112 113 static boolean_t vm_object_page_remove_write(vm_page_t p, int flags, 114 boolean_t *allclean); 115 static void vm_object_backing_remove(vm_object_t object); 116 117 /* 118 * Virtual memory objects maintain the actual data 119 * associated with allocated virtual memory. A given 120 * page of memory exists within exactly one object. 121 * 122 * An object is only deallocated when all "references" 123 * are given up. Only one "reference" to a given 124 * region of an object should be writeable. 125 * 126 * Associated with each object is a list of all resident 127 * memory pages belonging to that object; this list is 128 * maintained by the "vm_page" module, and locked by the object's 129 * lock. 130 * 131 * Each object also records a "pager" routine which is 132 * used to retrieve (and store) pages to the proper backing 133 * storage. In addition, objects may be backed by other 134 * objects from which they were virtual-copied. 135 * 136 * The only items within the object structure which are 137 * modified after time of creation are: 138 * reference count locked by object's lock 139 * pager routine locked by object's lock 140 * 141 */ 142 143 struct object_q vm_object_list; 144 struct mtx vm_object_list_mtx; /* lock for object list and count */ 145 146 struct vm_object kernel_object_store; 147 148 static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 149 "VM object stats"); 150 151 static COUNTER_U64_DEFINE_EARLY(object_collapses); 152 SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD, 153 &object_collapses, 154 "VM object collapses"); 155 156 static COUNTER_U64_DEFINE_EARLY(object_bypasses); 157 SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD, 158 &object_bypasses, 159 "VM object bypasses"); 160 161 static COUNTER_U64_DEFINE_EARLY(object_collapse_waits); 162 SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, collapse_waits, CTLFLAG_RD, 163 &object_collapse_waits, 164 "Number of sleeps for collapse"); 165 166 static uma_zone_t obj_zone; 167 168 static int vm_object_zinit(void *mem, int size, int flags); 169 170 #ifdef INVARIANTS 171 static void vm_object_zdtor(void *mem, int size, void *arg); 172 173 static void 174 vm_object_zdtor(void *mem, int size, void *arg) 175 { 176 vm_object_t object; 177 178 object = (vm_object_t)mem; 179 KASSERT(object->ref_count == 0, 180 ("object %p ref_count = %d", object, object->ref_count)); 181 KASSERT(vm_radix_is_empty(&object->rtree), 182 ("object %p has resident pages in its trie", object)); 183 #if VM_NRESERVLEVEL > 0 184 KASSERT(LIST_EMPTY(&object->rvq), 185 ("object %p has reservations", 186 object)); 187 #endif 188 KASSERT(!vm_object_busied(object), 189 ("object %p busy = %d", object, blockcount_read(&object->busy))); 190 KASSERT(object->resident_page_count == 0, 191 ("object %p resident_page_count = %d", 192 object, object->resident_page_count)); 193 KASSERT(atomic_load_int(&object->shadow_count) == 0, 194 ("object %p shadow_count = %d", 195 object, atomic_load_int(&object->shadow_count))); 196 KASSERT(object->type == OBJT_DEAD, 197 ("object %p has non-dead type %d", 198 object, object->type)); 199 KASSERT(object->charge == 0 && object->cred == NULL, 200 ("object %p has non-zero charge %ju (%p)", 201 object, (uintmax_t)object->charge, object->cred)); 202 } 203 #endif 204 205 static int 206 vm_object_zinit(void *mem, int size, int flags) 207 { 208 vm_object_t object; 209 210 object = (vm_object_t)mem; 211 rw_init_flags(&object->lock, "vmobject", RW_DUPOK | RW_NEW); 212 213 /* These are true for any object that has been freed */ 214 object->type = OBJT_DEAD; 215 vm_radix_init(&object->rtree); 216 refcount_init(&object->ref_count, 0); 217 blockcount_init(&object->paging_in_progress); 218 blockcount_init(&object->busy); 219 object->resident_page_count = 0; 220 atomic_store_int(&object->shadow_count, 0); 221 object->flags = OBJ_DEAD; 222 223 mtx_lock(&vm_object_list_mtx); 224 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 225 mtx_unlock(&vm_object_list_mtx); 226 return (0); 227 } 228 229 static void 230 _vm_object_allocate(objtype_t type, vm_pindex_t size, u_short flags, 231 vm_object_t object, void *handle) 232 { 233 LIST_INIT(&object->shadow_head); 234 235 object->type = type; 236 object->flags = flags; 237 if ((flags & OBJ_SWAP) != 0) { 238 pctrie_init(&object->un_pager.swp.swp_blks); 239 object->un_pager.swp.writemappings = 0; 240 } 241 242 /* 243 * Ensure that swap_pager_swapoff() iteration over object_list 244 * sees up to date type and pctrie head if it observed 245 * non-dead object. 246 */ 247 atomic_thread_fence_rel(); 248 249 object->pg_color = 0; 250 object->size = size; 251 object->domain.dr_policy = NULL; 252 object->generation = 1; 253 object->cleangeneration = 1; 254 refcount_init(&object->ref_count, 1); 255 object->memattr = VM_MEMATTR_DEFAULT; 256 object->cred = NULL; 257 object->charge = 0; 258 object->handle = handle; 259 object->backing_object = NULL; 260 object->backing_object_offset = (vm_ooffset_t) 0; 261 #if VM_NRESERVLEVEL > 0 262 LIST_INIT(&object->rvq); 263 #endif 264 umtx_shm_object_init(object); 265 } 266 267 /* 268 * vm_object_init: 269 * 270 * Initialize the VM objects module. 271 */ 272 void 273 vm_object_init(void) 274 { 275 TAILQ_INIT(&vm_object_list); 276 mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF); 277 278 rw_init(&kernel_object->lock, "kernel vm object"); 279 vm_radix_init(&kernel_object->rtree); 280 _vm_object_allocate(OBJT_PHYS, atop(VM_MAX_KERNEL_ADDRESS - 281 VM_MIN_KERNEL_ADDRESS), OBJ_UNMANAGED, kernel_object, NULL); 282 #if VM_NRESERVLEVEL > 0 283 kernel_object->flags |= OBJ_COLORED; 284 kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS); 285 #endif 286 kernel_object->un_pager.phys.ops = &default_phys_pg_ops; 287 288 /* 289 * The lock portion of struct vm_object must be type stable due 290 * to vm_pageout_fallback_object_lock locking a vm object 291 * without holding any references to it. 292 * 293 * paging_in_progress is valid always. Lockless references to 294 * the objects may acquire pip and then check OBJ_DEAD. 295 */ 296 obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL, 297 #ifdef INVARIANTS 298 vm_object_zdtor, 299 #else 300 NULL, 301 #endif 302 vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 303 304 vm_radix_zinit(); 305 } 306 307 void 308 vm_object_clear_flag(vm_object_t object, u_short bits) 309 { 310 311 VM_OBJECT_ASSERT_WLOCKED(object); 312 object->flags &= ~bits; 313 } 314 315 /* 316 * Sets the default memory attribute for the specified object. Pages 317 * that are allocated to this object are by default assigned this memory 318 * attribute. 319 * 320 * Presently, this function must be called before any pages are allocated 321 * to the object. In the future, this requirement may be relaxed for 322 * "default" and "swap" objects. 323 */ 324 int 325 vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr) 326 { 327 328 VM_OBJECT_ASSERT_WLOCKED(object); 329 330 if (object->type == OBJT_DEAD) 331 return (KERN_INVALID_ARGUMENT); 332 if (!vm_radix_is_empty(&object->rtree)) 333 return (KERN_FAILURE); 334 335 object->memattr = memattr; 336 return (KERN_SUCCESS); 337 } 338 339 void 340 vm_object_pip_add(vm_object_t object, short i) 341 { 342 343 if (i > 0) 344 blockcount_acquire(&object->paging_in_progress, i); 345 } 346 347 void 348 vm_object_pip_wakeup(vm_object_t object) 349 { 350 351 vm_object_pip_wakeupn(object, 1); 352 } 353 354 void 355 vm_object_pip_wakeupn(vm_object_t object, short i) 356 { 357 358 if (i > 0) 359 blockcount_release(&object->paging_in_progress, i); 360 } 361 362 /* 363 * Atomically drop the object lock and wait for pip to drain. This protects 364 * from sleep/wakeup races due to identity changes. The lock is not re-acquired 365 * on return. 366 */ 367 static void 368 vm_object_pip_sleep(vm_object_t object, const char *waitid) 369 { 370 371 (void)blockcount_sleep(&object->paging_in_progress, &object->lock, 372 waitid, PVM | PDROP); 373 } 374 375 void 376 vm_object_pip_wait(vm_object_t object, const char *waitid) 377 { 378 379 VM_OBJECT_ASSERT_WLOCKED(object); 380 381 blockcount_wait(&object->paging_in_progress, &object->lock, waitid, 382 PVM); 383 } 384 385 void 386 vm_object_pip_wait_unlocked(vm_object_t object, const char *waitid) 387 { 388 389 VM_OBJECT_ASSERT_UNLOCKED(object); 390 391 blockcount_wait(&object->paging_in_progress, NULL, waitid, PVM); 392 } 393 394 /* 395 * vm_object_allocate: 396 * 397 * Returns a new object with the given size. 398 */ 399 vm_object_t 400 vm_object_allocate(objtype_t type, vm_pindex_t size) 401 { 402 vm_object_t object; 403 u_short flags; 404 405 switch (type) { 406 case OBJT_DEAD: 407 panic("vm_object_allocate: can't create OBJT_DEAD"); 408 case OBJT_SWAP: 409 flags = OBJ_COLORED | OBJ_SWAP; 410 break; 411 case OBJT_DEVICE: 412 case OBJT_SG: 413 flags = OBJ_FICTITIOUS | OBJ_UNMANAGED; 414 break; 415 case OBJT_MGTDEVICE: 416 flags = OBJ_FICTITIOUS; 417 break; 418 case OBJT_PHYS: 419 flags = OBJ_UNMANAGED; 420 break; 421 case OBJT_VNODE: 422 flags = 0; 423 break; 424 default: 425 panic("vm_object_allocate: type %d is undefined or dynamic", 426 type); 427 } 428 object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK); 429 _vm_object_allocate(type, size, flags, object, NULL); 430 431 return (object); 432 } 433 434 vm_object_t 435 vm_object_allocate_dyn(objtype_t dyntype, vm_pindex_t size, u_short flags) 436 { 437 vm_object_t object; 438 439 MPASS(dyntype >= OBJT_FIRST_DYN /* && dyntype < nitems(pagertab) */); 440 object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK); 441 _vm_object_allocate(dyntype, size, flags, object, NULL); 442 443 return (object); 444 } 445 446 /* 447 * vm_object_allocate_anon: 448 * 449 * Returns a new default object of the given size and marked as 450 * anonymous memory for special split/collapse handling. Color 451 * to be initialized by the caller. 452 */ 453 vm_object_t 454 vm_object_allocate_anon(vm_pindex_t size, vm_object_t backing_object, 455 struct ucred *cred, vm_size_t charge) 456 { 457 vm_object_t handle, object; 458 459 if (backing_object == NULL) 460 handle = NULL; 461 else if ((backing_object->flags & OBJ_ANON) != 0) 462 handle = backing_object->handle; 463 else 464 handle = backing_object; 465 object = uma_zalloc(obj_zone, M_WAITOK); 466 _vm_object_allocate(OBJT_SWAP, size, 467 OBJ_ANON | OBJ_ONEMAPPING | OBJ_SWAP, object, handle); 468 object->cred = cred; 469 object->charge = cred != NULL ? charge : 0; 470 return (object); 471 } 472 473 static void 474 vm_object_reference_vnode(vm_object_t object) 475 { 476 u_int old; 477 478 /* 479 * vnode objects need the lock for the first reference 480 * to serialize with vnode_object_deallocate(). 481 */ 482 if (!refcount_acquire_if_gt(&object->ref_count, 0)) { 483 VM_OBJECT_RLOCK(object); 484 old = refcount_acquire(&object->ref_count); 485 if (object->type == OBJT_VNODE && old == 0) 486 vref(object->handle); 487 VM_OBJECT_RUNLOCK(object); 488 } 489 } 490 491 /* 492 * vm_object_reference: 493 * 494 * Acquires a reference to the given object. 495 */ 496 void 497 vm_object_reference(vm_object_t object) 498 { 499 500 if (object == NULL) 501 return; 502 503 if (object->type == OBJT_VNODE) 504 vm_object_reference_vnode(object); 505 else 506 refcount_acquire(&object->ref_count); 507 KASSERT((object->flags & OBJ_DEAD) == 0, 508 ("vm_object_reference: Referenced dead object.")); 509 } 510 511 /* 512 * vm_object_reference_locked: 513 * 514 * Gets another reference to the given object. 515 * 516 * The object must be locked. 517 */ 518 void 519 vm_object_reference_locked(vm_object_t object) 520 { 521 u_int old; 522 523 VM_OBJECT_ASSERT_LOCKED(object); 524 old = refcount_acquire(&object->ref_count); 525 if (object->type == OBJT_VNODE && old == 0) 526 vref(object->handle); 527 KASSERT((object->flags & OBJ_DEAD) == 0, 528 ("vm_object_reference: Referenced dead object.")); 529 } 530 531 /* 532 * Handle deallocating an object of type OBJT_VNODE. 533 */ 534 static void 535 vm_object_deallocate_vnode(vm_object_t object) 536 { 537 struct vnode *vp = (struct vnode *) object->handle; 538 bool last; 539 540 KASSERT(object->type == OBJT_VNODE, 541 ("vm_object_deallocate_vnode: not a vnode object")); 542 KASSERT(vp != NULL, ("vm_object_deallocate_vnode: missing vp")); 543 544 /* Object lock to protect handle lookup. */ 545 last = refcount_release(&object->ref_count); 546 VM_OBJECT_RUNLOCK(object); 547 548 if (!last) 549 return; 550 551 if (!umtx_shm_vnobj_persistent) 552 umtx_shm_object_terminated(object); 553 554 /* vrele may need the vnode lock. */ 555 vrele(vp); 556 } 557 558 /* 559 * We dropped a reference on an object and discovered that it had a 560 * single remaining shadow. This is a sibling of the reference we 561 * dropped. Attempt to collapse the sibling and backing object. 562 */ 563 static vm_object_t 564 vm_object_deallocate_anon(vm_object_t backing_object) 565 { 566 vm_object_t object; 567 568 /* Fetch the final shadow. */ 569 object = LIST_FIRST(&backing_object->shadow_head); 570 KASSERT(object != NULL && 571 atomic_load_int(&backing_object->shadow_count) == 1, 572 ("vm_object_anon_deallocate: ref_count: %d, shadow_count: %d", 573 backing_object->ref_count, 574 atomic_load_int(&backing_object->shadow_count))); 575 KASSERT((object->flags & OBJ_ANON) != 0, 576 ("invalid shadow object %p", object)); 577 578 if (!VM_OBJECT_TRYWLOCK(object)) { 579 /* 580 * Prevent object from disappearing since we do not have a 581 * reference. 582 */ 583 vm_object_pip_add(object, 1); 584 VM_OBJECT_WUNLOCK(backing_object); 585 VM_OBJECT_WLOCK(object); 586 vm_object_pip_wakeup(object); 587 } else 588 VM_OBJECT_WUNLOCK(backing_object); 589 590 /* 591 * Check for a collapse/terminate race with the last reference holder. 592 */ 593 if ((object->flags & (OBJ_DEAD | OBJ_COLLAPSING)) != 0 || 594 !refcount_acquire_if_not_zero(&object->ref_count)) { 595 VM_OBJECT_WUNLOCK(object); 596 return (NULL); 597 } 598 backing_object = object->backing_object; 599 if (backing_object != NULL && (backing_object->flags & OBJ_ANON) != 0) 600 vm_object_collapse(object); 601 VM_OBJECT_WUNLOCK(object); 602 603 return (object); 604 } 605 606 /* 607 * vm_object_deallocate: 608 * 609 * Release a reference to the specified object, 610 * gained either through a vm_object_allocate 611 * or a vm_object_reference call. When all references 612 * are gone, storage associated with this object 613 * may be relinquished. 614 * 615 * No object may be locked. 616 */ 617 void 618 vm_object_deallocate(vm_object_t object) 619 { 620 vm_object_t temp; 621 bool released; 622 623 while (object != NULL) { 624 /* 625 * If the reference count goes to 0 we start calling 626 * vm_object_terminate() on the object chain. A ref count 627 * of 1 may be a special case depending on the shadow count 628 * being 0 or 1. These cases require a write lock on the 629 * object. 630 */ 631 if ((object->flags & OBJ_ANON) == 0) 632 released = refcount_release_if_gt(&object->ref_count, 1); 633 else 634 released = refcount_release_if_gt(&object->ref_count, 2); 635 if (released) 636 return; 637 638 if (object->type == OBJT_VNODE) { 639 VM_OBJECT_RLOCK(object); 640 if (object->type == OBJT_VNODE) { 641 vm_object_deallocate_vnode(object); 642 return; 643 } 644 VM_OBJECT_RUNLOCK(object); 645 } 646 647 VM_OBJECT_WLOCK(object); 648 KASSERT(object->ref_count > 0, 649 ("vm_object_deallocate: object deallocated too many times: %d", 650 object->type)); 651 652 /* 653 * If this is not the final reference to an anonymous 654 * object we may need to collapse the shadow chain. 655 */ 656 if (!refcount_release(&object->ref_count)) { 657 if (object->ref_count > 1 || 658 atomic_load_int(&object->shadow_count) == 0) { 659 if ((object->flags & OBJ_ANON) != 0 && 660 object->ref_count == 1) 661 vm_object_set_flag(object, 662 OBJ_ONEMAPPING); 663 VM_OBJECT_WUNLOCK(object); 664 return; 665 } 666 667 /* Handle collapsing last ref on anonymous objects. */ 668 object = vm_object_deallocate_anon(object); 669 continue; 670 } 671 672 /* 673 * Handle the final reference to an object. We restart 674 * the loop with the backing object to avoid recursion. 675 */ 676 umtx_shm_object_terminated(object); 677 temp = object->backing_object; 678 if (temp != NULL) { 679 KASSERT(object->type == OBJT_SWAP, 680 ("shadowed tmpfs v_object 2 %p", object)); 681 vm_object_backing_remove(object); 682 } 683 684 KASSERT((object->flags & OBJ_DEAD) == 0, 685 ("vm_object_deallocate: Terminating dead object.")); 686 vm_object_set_flag(object, OBJ_DEAD); 687 vm_object_terminate(object); 688 object = temp; 689 } 690 } 691 692 void 693 vm_object_destroy(vm_object_t object) 694 { 695 uma_zfree(obj_zone, object); 696 } 697 698 static void 699 vm_object_sub_shadow(vm_object_t object) 700 { 701 KASSERT(object->shadow_count >= 1, 702 ("object %p sub_shadow count zero", object)); 703 atomic_subtract_int(&object->shadow_count, 1); 704 } 705 706 static void 707 vm_object_backing_remove_locked(vm_object_t object) 708 { 709 vm_object_t backing_object; 710 711 backing_object = object->backing_object; 712 VM_OBJECT_ASSERT_WLOCKED(object); 713 VM_OBJECT_ASSERT_WLOCKED(backing_object); 714 715 KASSERT((object->flags & OBJ_COLLAPSING) == 0, 716 ("vm_object_backing_remove: Removing collapsing object.")); 717 718 vm_object_sub_shadow(backing_object); 719 if ((object->flags & OBJ_SHADOWLIST) != 0) { 720 LIST_REMOVE(object, shadow_list); 721 vm_object_clear_flag(object, OBJ_SHADOWLIST); 722 } 723 object->backing_object = NULL; 724 } 725 726 static void 727 vm_object_backing_remove(vm_object_t object) 728 { 729 vm_object_t backing_object; 730 731 VM_OBJECT_ASSERT_WLOCKED(object); 732 733 backing_object = object->backing_object; 734 if ((object->flags & OBJ_SHADOWLIST) != 0) { 735 VM_OBJECT_WLOCK(backing_object); 736 vm_object_backing_remove_locked(object); 737 VM_OBJECT_WUNLOCK(backing_object); 738 } else { 739 object->backing_object = NULL; 740 vm_object_sub_shadow(backing_object); 741 } 742 } 743 744 static void 745 vm_object_backing_insert_locked(vm_object_t object, vm_object_t backing_object) 746 { 747 748 VM_OBJECT_ASSERT_WLOCKED(object); 749 750 atomic_add_int(&backing_object->shadow_count, 1); 751 if ((backing_object->flags & OBJ_ANON) != 0) { 752 VM_OBJECT_ASSERT_WLOCKED(backing_object); 753 LIST_INSERT_HEAD(&backing_object->shadow_head, object, 754 shadow_list); 755 vm_object_set_flag(object, OBJ_SHADOWLIST); 756 } 757 object->backing_object = backing_object; 758 } 759 760 static void 761 vm_object_backing_insert(vm_object_t object, vm_object_t backing_object) 762 { 763 764 VM_OBJECT_ASSERT_WLOCKED(object); 765 766 if ((backing_object->flags & OBJ_ANON) != 0) { 767 VM_OBJECT_WLOCK(backing_object); 768 vm_object_backing_insert_locked(object, backing_object); 769 VM_OBJECT_WUNLOCK(backing_object); 770 } else { 771 object->backing_object = backing_object; 772 atomic_add_int(&backing_object->shadow_count, 1); 773 } 774 } 775 776 /* 777 * Insert an object into a backing_object's shadow list with an additional 778 * reference to the backing_object added. 779 */ 780 static void 781 vm_object_backing_insert_ref(vm_object_t object, vm_object_t backing_object) 782 { 783 784 VM_OBJECT_ASSERT_WLOCKED(object); 785 786 if ((backing_object->flags & OBJ_ANON) != 0) { 787 VM_OBJECT_WLOCK(backing_object); 788 KASSERT((backing_object->flags & OBJ_DEAD) == 0, 789 ("shadowing dead anonymous object")); 790 vm_object_reference_locked(backing_object); 791 vm_object_backing_insert_locked(object, backing_object); 792 vm_object_clear_flag(backing_object, OBJ_ONEMAPPING); 793 VM_OBJECT_WUNLOCK(backing_object); 794 } else { 795 vm_object_reference(backing_object); 796 atomic_add_int(&backing_object->shadow_count, 1); 797 object->backing_object = backing_object; 798 } 799 } 800 801 /* 802 * Transfer a backing reference from backing_object to object. 803 */ 804 static void 805 vm_object_backing_transfer(vm_object_t object, vm_object_t backing_object) 806 { 807 vm_object_t new_backing_object; 808 809 /* 810 * Note that the reference to backing_object->backing_object 811 * moves from within backing_object to within object. 812 */ 813 vm_object_backing_remove_locked(object); 814 new_backing_object = backing_object->backing_object; 815 if (new_backing_object == NULL) 816 return; 817 if ((new_backing_object->flags & OBJ_ANON) != 0) { 818 VM_OBJECT_WLOCK(new_backing_object); 819 vm_object_backing_remove_locked(backing_object); 820 vm_object_backing_insert_locked(object, new_backing_object); 821 VM_OBJECT_WUNLOCK(new_backing_object); 822 } else { 823 /* 824 * shadow_count for new_backing_object is left 825 * unchanged, its reference provided by backing_object 826 * is replaced by object. 827 */ 828 object->backing_object = new_backing_object; 829 backing_object->backing_object = NULL; 830 } 831 } 832 833 /* 834 * Wait for a concurrent collapse to settle. 835 */ 836 static void 837 vm_object_collapse_wait(vm_object_t object) 838 { 839 840 VM_OBJECT_ASSERT_WLOCKED(object); 841 842 while ((object->flags & OBJ_COLLAPSING) != 0) { 843 vm_object_pip_wait(object, "vmcolwait"); 844 counter_u64_add(object_collapse_waits, 1); 845 } 846 } 847 848 /* 849 * Waits for a backing object to clear a pending collapse and returns 850 * it locked if it is an ANON object. 851 */ 852 static vm_object_t 853 vm_object_backing_collapse_wait(vm_object_t object) 854 { 855 vm_object_t backing_object; 856 857 VM_OBJECT_ASSERT_WLOCKED(object); 858 859 for (;;) { 860 backing_object = object->backing_object; 861 if (backing_object == NULL || 862 (backing_object->flags & OBJ_ANON) == 0) 863 return (NULL); 864 VM_OBJECT_WLOCK(backing_object); 865 if ((backing_object->flags & (OBJ_DEAD | OBJ_COLLAPSING)) == 0) 866 break; 867 VM_OBJECT_WUNLOCK(object); 868 vm_object_pip_sleep(backing_object, "vmbckwait"); 869 counter_u64_add(object_collapse_waits, 1); 870 VM_OBJECT_WLOCK(object); 871 } 872 return (backing_object); 873 } 874 875 /* 876 * vm_object_terminate_single_page removes a pageable page from the object, 877 * and removes it from the paging queues and frees it, if it is not wired. 878 * It is invoked via callback from vm_object_terminate_pages. 879 */ 880 static void 881 vm_object_terminate_single_page(vm_page_t p, void *objectv) 882 { 883 vm_object_t object __diagused = objectv; 884 885 vm_page_assert_unbusied(p); 886 KASSERT(p->object == object && 887 (p->ref_count & VPRC_OBJREF) != 0, 888 ("%s: page %p is inconsistent", __func__, p)); 889 p->object = NULL; 890 if (vm_page_drop(p, VPRC_OBJREF) == VPRC_OBJREF) { 891 KASSERT((object->flags & OBJ_UNMANAGED) != 0 || 892 vm_page_astate_load(p).queue != PQ_NONE, 893 ("%s: page %p does not belong to a queue", __func__, p)); 894 VM_CNT_INC(v_pfree); 895 vm_page_free(p); 896 } 897 } 898 899 /* 900 * vm_object_terminate_pages removes any remaining pageable pages 901 * from the object and resets the object to an empty state. 902 */ 903 static void 904 vm_object_terminate_pages(vm_object_t object) 905 { 906 VM_OBJECT_ASSERT_WLOCKED(object); 907 908 /* 909 * If the object contained any pages, then reset it to an empty state. 910 * Rather than incrementally removing each page from the object, the 911 * page and object are reset to any empty state. 912 */ 913 if (object->resident_page_count == 0) 914 return; 915 916 vm_radix_reclaim_callback(&object->rtree, 917 vm_object_terminate_single_page, object); 918 object->resident_page_count = 0; 919 if (object->type == OBJT_VNODE) 920 vdrop(object->handle); 921 } 922 923 /* 924 * vm_object_terminate actually destroys the specified object, freeing 925 * up all previously used resources. 926 * 927 * The object must be locked. 928 * This routine may block. 929 */ 930 void 931 vm_object_terminate(vm_object_t object) 932 { 933 934 VM_OBJECT_ASSERT_WLOCKED(object); 935 KASSERT((object->flags & OBJ_DEAD) != 0, 936 ("terminating non-dead obj %p", object)); 937 KASSERT((object->flags & OBJ_COLLAPSING) == 0, 938 ("terminating collapsing obj %p", object)); 939 KASSERT(object->backing_object == NULL, 940 ("terminating shadow obj %p", object)); 941 942 /* 943 * Wait for the pageout daemon and other current users to be 944 * done with the object. Note that new paging_in_progress 945 * users can come after this wait, but they must check 946 * OBJ_DEAD flag set (without unlocking the object), and avoid 947 * the object being terminated. 948 */ 949 vm_object_pip_wait(object, "objtrm"); 950 951 KASSERT(object->ref_count == 0, 952 ("vm_object_terminate: object with references, ref_count=%d", 953 object->ref_count)); 954 955 if ((object->flags & OBJ_PG_DTOR) == 0) 956 vm_object_terminate_pages(object); 957 958 #if VM_NRESERVLEVEL > 0 959 if (__predict_false(!LIST_EMPTY(&object->rvq))) 960 vm_reserv_break_all(object); 961 #endif 962 963 KASSERT(object->cred == NULL || (object->flags & OBJ_SWAP) != 0, 964 ("%s: non-swap obj %p has cred", __func__, object)); 965 966 /* 967 * Let the pager know object is dead. 968 */ 969 vm_pager_deallocate(object); 970 VM_OBJECT_WUNLOCK(object); 971 972 vm_object_destroy(object); 973 } 974 975 /* 976 * Make the page read-only so that we can clear the object flags. However, if 977 * this is a nosync mmap then the object is likely to stay dirty so do not 978 * mess with the page and do not clear the object flags. Returns TRUE if the 979 * page should be flushed, and FALSE otherwise. 980 */ 981 static boolean_t 982 vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *allclean) 983 { 984 985 vm_page_assert_busied(p); 986 987 /* 988 * If we have been asked to skip nosync pages and this is a 989 * nosync page, skip it. Note that the object flags were not 990 * cleared in this case so we do not have to set them. 991 */ 992 if ((flags & OBJPC_NOSYNC) != 0 && (p->a.flags & PGA_NOSYNC) != 0) { 993 *allclean = FALSE; 994 return (FALSE); 995 } else { 996 pmap_remove_write(p); 997 return (p->dirty != 0); 998 } 999 } 1000 1001 static int 1002 vm_object_page_clean_flush(struct pctrie_iter *pages, vm_page_t p, 1003 int pagerflags, int flags, boolean_t *allclean, boolean_t *eio) 1004 { 1005 vm_page_t ma[vm_pageout_page_count]; 1006 int count, runlen; 1007 1008 vm_page_lock_assert(p, MA_NOTOWNED); 1009 vm_page_assert_xbusied(p); 1010 ma[0] = p; 1011 runlen = vm_radix_iter_lookup_range(pages, p->pindex + 1, 1012 &ma[1], vm_pageout_page_count - 1); 1013 for (count = 1; count <= runlen; count++) { 1014 p = ma[count]; 1015 if (vm_page_tryxbusy(p) == 0) 1016 break; 1017 if (!vm_object_page_remove_write(p, flags, allclean)) { 1018 vm_page_xunbusy(p); 1019 break; 1020 } 1021 } 1022 1023 vm_pageout_flush(ma, count, pagerflags, 0, &runlen, eio); 1024 return (runlen); 1025 } 1026 1027 /* 1028 * vm_object_page_clean 1029 * 1030 * Clean all dirty pages in the specified range of object. Leaves page 1031 * on whatever queue it is currently on. If NOSYNC is set then do not 1032 * write out pages with PGA_NOSYNC set (originally comes from MAP_NOSYNC), 1033 * leaving the object dirty. 1034 * 1035 * For swap objects backing tmpfs regular files, do not flush anything, 1036 * but remove write protection on the mapped pages to update mtime through 1037 * mmaped writes. 1038 * 1039 * When stuffing pages asynchronously, allow clustering. XXX we need a 1040 * synchronous clustering mode implementation. 1041 * 1042 * Odd semantics: if start == end, we clean everything. 1043 * 1044 * The object must be locked. 1045 * 1046 * Returns FALSE if some page from the range was not written, as 1047 * reported by the pager, and TRUE otherwise. 1048 */ 1049 boolean_t 1050 vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end, 1051 int flags) 1052 { 1053 struct pctrie_iter pages; 1054 vm_page_t np, p; 1055 vm_pindex_t pi, tend, tstart; 1056 int curgeneration, n, pagerflags; 1057 boolean_t eio, res, allclean; 1058 1059 VM_OBJECT_ASSERT_WLOCKED(object); 1060 1061 if (!vm_object_mightbedirty(object) || object->resident_page_count == 0) 1062 return (TRUE); 1063 1064 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ? 1065 VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 1066 pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0; 1067 1068 tstart = OFF_TO_IDX(start); 1069 tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK); 1070 allclean = tstart == 0 && tend >= object->size; 1071 res = TRUE; 1072 vm_page_iter_init(&pages, object); 1073 1074 rescan: 1075 curgeneration = object->generation; 1076 1077 for (p = vm_radix_iter_lookup_ge(&pages, tstart); p != NULL; p = np) { 1078 pi = p->pindex; 1079 if (pi >= tend) 1080 break; 1081 if (vm_page_none_valid(p)) { 1082 np = vm_radix_iter_step(&pages); 1083 continue; 1084 } 1085 if (!vm_page_busy_acquire(p, VM_ALLOC_WAITFAIL)) { 1086 pctrie_iter_reset(&pages); 1087 if (object->generation != curgeneration && 1088 (flags & OBJPC_SYNC) != 0) 1089 goto rescan; 1090 np = vm_radix_iter_lookup_ge(&pages, pi); 1091 continue; 1092 } 1093 if (!vm_object_page_remove_write(p, flags, &allclean)) { 1094 np = vm_radix_iter_step(&pages); 1095 vm_page_xunbusy(p); 1096 continue; 1097 } 1098 if (object->type == OBJT_VNODE) { 1099 n = vm_object_page_clean_flush(&pages, p, pagerflags, 1100 flags, &allclean, &eio); 1101 pctrie_iter_reset(&pages); 1102 if (eio) { 1103 res = FALSE; 1104 allclean = FALSE; 1105 } 1106 if (object->generation != curgeneration && 1107 (flags & OBJPC_SYNC) != 0) 1108 goto rescan; 1109 1110 /* 1111 * If the VOP_PUTPAGES() did a truncated write, so 1112 * that even the first page of the run is not fully 1113 * written, vm_pageout_flush() returns 0 as the run 1114 * length. Since the condition that caused truncated 1115 * write may be permanent, e.g. exhausted free space, 1116 * accepting n == 0 would cause an infinite loop. 1117 * 1118 * Forwarding the iterator leaves the unwritten page 1119 * behind, but there is not much we can do there if 1120 * filesystem refuses to write it. 1121 */ 1122 if (n == 0) { 1123 n = 1; 1124 allclean = FALSE; 1125 } 1126 } else { 1127 n = 1; 1128 vm_page_xunbusy(p); 1129 } 1130 np = vm_radix_iter_lookup_ge(&pages, pi + n); 1131 } 1132 #if 0 1133 VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0); 1134 #endif 1135 1136 /* 1137 * Leave updating cleangeneration for tmpfs objects to tmpfs 1138 * scan. It needs to update mtime, which happens for other 1139 * filesystems during page writeouts. 1140 */ 1141 if (allclean && object->type == OBJT_VNODE) 1142 object->cleangeneration = curgeneration; 1143 return (res); 1144 } 1145 1146 /* 1147 * Note that there is absolutely no sense in writing out 1148 * anonymous objects, so we track down the vnode object 1149 * to write out. 1150 * We invalidate (remove) all pages from the address space 1151 * for semantic correctness. 1152 * 1153 * If the backing object is a device object with unmanaged pages, then any 1154 * mappings to the specified range of pages must be removed before this 1155 * function is called. 1156 * 1157 * Note: certain anonymous maps, such as MAP_NOSYNC maps, 1158 * may start out with a NULL object. 1159 */ 1160 boolean_t 1161 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size, 1162 boolean_t syncio, boolean_t invalidate) 1163 { 1164 vm_object_t backing_object; 1165 struct vnode *vp; 1166 struct mount *mp; 1167 int error, flags, fsync_after; 1168 boolean_t res; 1169 1170 if (object == NULL) 1171 return (TRUE); 1172 res = TRUE; 1173 error = 0; 1174 VM_OBJECT_WLOCK(object); 1175 while ((backing_object = object->backing_object) != NULL) { 1176 VM_OBJECT_WLOCK(backing_object); 1177 offset += object->backing_object_offset; 1178 VM_OBJECT_WUNLOCK(object); 1179 object = backing_object; 1180 if (object->size < OFF_TO_IDX(offset + size)) 1181 size = IDX_TO_OFF(object->size) - offset; 1182 } 1183 /* 1184 * Flush pages if writing is allowed, invalidate them 1185 * if invalidation requested. Pages undergoing I/O 1186 * will be ignored by vm_object_page_remove(). 1187 * 1188 * We cannot lock the vnode and then wait for paging 1189 * to complete without deadlocking against vm_fault. 1190 * Instead we simply call vm_object_page_remove() and 1191 * allow it to block internally on a page-by-page 1192 * basis when it encounters pages undergoing async 1193 * I/O. 1194 */ 1195 if (object->type == OBJT_VNODE && 1196 vm_object_mightbedirty(object) != 0 && 1197 ((vp = object->handle)->v_vflag & VV_NOSYNC) == 0) { 1198 VM_OBJECT_WUNLOCK(object); 1199 (void)vn_start_write(vp, &mp, V_WAIT); 1200 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1201 if (syncio && !invalidate && offset == 0 && 1202 atop(size) == object->size) { 1203 /* 1204 * If syncing the whole mapping of the file, 1205 * it is faster to schedule all the writes in 1206 * async mode, also allowing the clustering, 1207 * and then wait for i/o to complete. 1208 */ 1209 flags = 0; 1210 fsync_after = TRUE; 1211 } else { 1212 flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 1213 flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0; 1214 fsync_after = FALSE; 1215 } 1216 VM_OBJECT_WLOCK(object); 1217 res = vm_object_page_clean(object, offset, offset + size, 1218 flags); 1219 VM_OBJECT_WUNLOCK(object); 1220 if (fsync_after) { 1221 for (;;) { 1222 error = VOP_FSYNC(vp, MNT_WAIT, curthread); 1223 if (error != ERELOOKUP) 1224 break; 1225 1226 /* 1227 * Allow SU/bufdaemon to handle more 1228 * dependencies in the meantime. 1229 */ 1230 VOP_UNLOCK(vp); 1231 vn_finished_write(mp); 1232 1233 (void)vn_start_write(vp, &mp, V_WAIT); 1234 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1235 } 1236 } 1237 VOP_UNLOCK(vp); 1238 vn_finished_write(mp); 1239 if (error != 0) 1240 res = FALSE; 1241 VM_OBJECT_WLOCK(object); 1242 } 1243 if ((object->type == OBJT_VNODE || 1244 object->type == OBJT_DEVICE) && invalidate) { 1245 if (object->type == OBJT_DEVICE) 1246 /* 1247 * The option OBJPR_NOTMAPPED must be passed here 1248 * because vm_object_page_remove() cannot remove 1249 * unmanaged mappings. 1250 */ 1251 flags = OBJPR_NOTMAPPED; 1252 else if (old_msync) 1253 flags = 0; 1254 else 1255 flags = OBJPR_CLEANONLY; 1256 vm_object_page_remove(object, OFF_TO_IDX(offset), 1257 OFF_TO_IDX(offset + size + PAGE_MASK), flags); 1258 } 1259 VM_OBJECT_WUNLOCK(object); 1260 return (res); 1261 } 1262 1263 /* 1264 * Determine whether the given advice can be applied to the object. Advice is 1265 * not applied to unmanaged pages since they never belong to page queues, and 1266 * since MADV_FREE is destructive, it can apply only to anonymous pages that 1267 * have been mapped at most once. 1268 */ 1269 static bool 1270 vm_object_advice_applies(vm_object_t object, int advice) 1271 { 1272 1273 if ((object->flags & OBJ_UNMANAGED) != 0) 1274 return (false); 1275 if (advice != MADV_FREE) 1276 return (true); 1277 return ((object->flags & (OBJ_ONEMAPPING | OBJ_ANON)) == 1278 (OBJ_ONEMAPPING | OBJ_ANON)); 1279 } 1280 1281 static void 1282 vm_object_madvise_freespace(vm_object_t object, int advice, vm_pindex_t pindex, 1283 vm_size_t size) 1284 { 1285 1286 if (advice == MADV_FREE) 1287 vm_pager_freespace(object, pindex, size); 1288 } 1289 1290 /* 1291 * vm_object_madvise: 1292 * 1293 * Implements the madvise function at the object/page level. 1294 * 1295 * MADV_WILLNEED (any object) 1296 * 1297 * Activate the specified pages if they are resident. 1298 * 1299 * MADV_DONTNEED (any object) 1300 * 1301 * Deactivate the specified pages if they are resident. 1302 * 1303 * MADV_FREE (OBJT_SWAP objects, OBJ_ONEMAPPING only) 1304 * 1305 * Deactivate and clean the specified pages if they are 1306 * resident. This permits the process to reuse the pages 1307 * without faulting or the kernel to reclaim the pages 1308 * without I/O. 1309 */ 1310 void 1311 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end, 1312 int advice) 1313 { 1314 struct pctrie_iter pages; 1315 vm_pindex_t tpindex; 1316 vm_object_t backing_object, tobject; 1317 vm_page_t m, tm; 1318 1319 if (object == NULL) 1320 return; 1321 1322 vm_page_iter_init(&pages, object); 1323 relookup: 1324 VM_OBJECT_WLOCK(object); 1325 if (!vm_object_advice_applies(object, advice)) { 1326 VM_OBJECT_WUNLOCK(object); 1327 return; 1328 } 1329 for (m = vm_radix_iter_lookup_ge(&pages, pindex); pindex < end; 1330 pindex++) { 1331 tobject = object; 1332 1333 /* 1334 * If the next page isn't resident in the top-level object, we 1335 * need to search the shadow chain. When applying MADV_FREE, we 1336 * take care to release any swap space used to store 1337 * non-resident pages. 1338 */ 1339 if (m == NULL || pindex < m->pindex) { 1340 /* 1341 * Optimize a common case: if the top-level object has 1342 * no backing object, we can skip over the non-resident 1343 * range in constant time. 1344 */ 1345 if (object->backing_object == NULL) { 1346 tpindex = (m != NULL && m->pindex < end) ? 1347 m->pindex : end; 1348 vm_object_madvise_freespace(object, advice, 1349 pindex, tpindex - pindex); 1350 if ((pindex = tpindex) == end) 1351 break; 1352 goto next_page; 1353 } 1354 1355 tpindex = pindex; 1356 do { 1357 vm_object_madvise_freespace(tobject, advice, 1358 tpindex, 1); 1359 /* 1360 * Prepare to search the next object in the 1361 * chain. 1362 */ 1363 backing_object = tobject->backing_object; 1364 if (backing_object == NULL) 1365 goto next_pindex; 1366 VM_OBJECT_WLOCK(backing_object); 1367 tpindex += 1368 OFF_TO_IDX(tobject->backing_object_offset); 1369 if (tobject != object) 1370 VM_OBJECT_WUNLOCK(tobject); 1371 tobject = backing_object; 1372 if (!vm_object_advice_applies(tobject, advice)) 1373 goto next_pindex; 1374 } while ((tm = vm_page_lookup(tobject, tpindex)) == 1375 NULL); 1376 } else { 1377 next_page: 1378 tm = m; 1379 m = vm_radix_iter_step(&pages); 1380 } 1381 1382 /* 1383 * If the page is not in a normal state, skip it. The page 1384 * can not be invalidated while the object lock is held. 1385 */ 1386 if (!vm_page_all_valid(tm) || vm_page_wired(tm)) 1387 goto next_pindex; 1388 KASSERT((tm->flags & PG_FICTITIOUS) == 0, 1389 ("vm_object_madvise: page %p is fictitious", tm)); 1390 KASSERT((tm->oflags & VPO_UNMANAGED) == 0, 1391 ("vm_object_madvise: page %p is not managed", tm)); 1392 if (vm_page_tryxbusy(tm) == 0) { 1393 if (object != tobject) 1394 VM_OBJECT_WUNLOCK(object); 1395 if (advice == MADV_WILLNEED) { 1396 /* 1397 * Reference the page before unlocking and 1398 * sleeping so that the page daemon is less 1399 * likely to reclaim it. 1400 */ 1401 vm_page_aflag_set(tm, PGA_REFERENCED); 1402 } 1403 if (!vm_page_busy_sleep(tm, "madvpo", 0)) 1404 VM_OBJECT_WUNLOCK(tobject); 1405 pctrie_iter_reset(&pages); 1406 goto relookup; 1407 } 1408 vm_page_advise(tm, advice); 1409 vm_page_xunbusy(tm); 1410 vm_object_madvise_freespace(tobject, advice, tm->pindex, 1); 1411 next_pindex: 1412 if (tobject != object) 1413 VM_OBJECT_WUNLOCK(tobject); 1414 } 1415 VM_OBJECT_WUNLOCK(object); 1416 } 1417 1418 /* 1419 * vm_object_shadow: 1420 * 1421 * Create a new object which is backed by the 1422 * specified existing object range. The source 1423 * object reference is deallocated. 1424 * 1425 * The new object and offset into that object 1426 * are returned in the source parameters. 1427 */ 1428 void 1429 vm_object_shadow(vm_object_t *object, vm_ooffset_t *offset, vm_size_t length, 1430 struct ucred *cred, bool shared) 1431 { 1432 vm_object_t source; 1433 vm_object_t result; 1434 1435 source = *object; 1436 1437 /* 1438 * Don't create the new object if the old object isn't shared. 1439 * 1440 * If we hold the only reference we can guarantee that it won't 1441 * increase while we have the map locked. Otherwise the race is 1442 * harmless and we will end up with an extra shadow object that 1443 * will be collapsed later. 1444 */ 1445 if (source != NULL && source->ref_count == 1 && 1446 (source->flags & OBJ_ANON) != 0) 1447 return; 1448 1449 /* 1450 * Allocate a new object with the given length. 1451 */ 1452 result = vm_object_allocate_anon(atop(length), source, cred, length); 1453 1454 /* 1455 * Store the offset into the source object, and fix up the offset into 1456 * the new object. 1457 */ 1458 result->backing_object_offset = *offset; 1459 1460 if (shared || source != NULL) { 1461 VM_OBJECT_WLOCK(result); 1462 1463 /* 1464 * The new object shadows the source object, adding a 1465 * reference to it. Our caller changes his reference 1466 * to point to the new object, removing a reference to 1467 * the source object. Net result: no change of 1468 * reference count, unless the caller needs to add one 1469 * more reference due to forking a shared map entry. 1470 */ 1471 if (shared) { 1472 vm_object_reference_locked(result); 1473 vm_object_clear_flag(result, OBJ_ONEMAPPING); 1474 } 1475 1476 /* 1477 * Try to optimize the result object's page color when 1478 * shadowing in order to maintain page coloring 1479 * consistency in the combined shadowed object. 1480 */ 1481 if (source != NULL) { 1482 vm_object_backing_insert(result, source); 1483 result->domain = source->domain; 1484 #if VM_NRESERVLEVEL > 0 1485 vm_object_set_flag(result, 1486 (source->flags & OBJ_COLORED)); 1487 result->pg_color = (source->pg_color + 1488 OFF_TO_IDX(*offset)) & ((1 << (VM_NFREEORDER - 1489 1)) - 1); 1490 #endif 1491 } 1492 VM_OBJECT_WUNLOCK(result); 1493 } 1494 1495 /* 1496 * Return the new things 1497 */ 1498 *offset = 0; 1499 *object = result; 1500 } 1501 1502 /* 1503 * vm_object_split: 1504 * 1505 * Split the pages in a map entry into a new object. This affords 1506 * easier removal of unused pages, and keeps object inheritance from 1507 * being a negative impact on memory usage. 1508 */ 1509 void 1510 vm_object_split(vm_map_entry_t entry) 1511 { 1512 struct pctrie_iter pages; 1513 vm_page_t m; 1514 vm_object_t orig_object, new_object, backing_object; 1515 vm_pindex_t offidxstart; 1516 vm_size_t size; 1517 1518 orig_object = entry->object.vm_object; 1519 KASSERT((orig_object->flags & OBJ_ONEMAPPING) != 0, 1520 ("vm_object_split: Splitting object with multiple mappings.")); 1521 if ((orig_object->flags & OBJ_ANON) == 0) 1522 return; 1523 if (orig_object->ref_count <= 1) 1524 return; 1525 VM_OBJECT_WUNLOCK(orig_object); 1526 1527 offidxstart = OFF_TO_IDX(entry->offset); 1528 size = atop(entry->end - entry->start); 1529 1530 new_object = vm_object_allocate_anon(size, orig_object, 1531 orig_object->cred, ptoa(size)); 1532 1533 /* 1534 * We must wait for the orig_object to complete any in-progress 1535 * collapse so that the swap blocks are stable below. The 1536 * additional reference on backing_object by new object will 1537 * prevent further collapse operations until split completes. 1538 */ 1539 VM_OBJECT_WLOCK(orig_object); 1540 vm_object_collapse_wait(orig_object); 1541 1542 /* 1543 * At this point, the new object is still private, so the order in 1544 * which the original and new objects are locked does not matter. 1545 */ 1546 VM_OBJECT_WLOCK(new_object); 1547 new_object->domain = orig_object->domain; 1548 backing_object = orig_object->backing_object; 1549 if (backing_object != NULL) { 1550 vm_object_backing_insert_ref(new_object, backing_object); 1551 new_object->backing_object_offset = 1552 orig_object->backing_object_offset + entry->offset; 1553 } 1554 if (orig_object->cred != NULL) { 1555 crhold(orig_object->cred); 1556 KASSERT(orig_object->charge >= ptoa(size), 1557 ("orig_object->charge < 0")); 1558 orig_object->charge -= ptoa(size); 1559 } 1560 1561 /* 1562 * Mark the split operation so that swap_pager_getpages() knows 1563 * that the object is in transition. 1564 */ 1565 vm_object_set_flag(orig_object, OBJ_SPLIT); 1566 vm_page_iter_limit_init(&pages, orig_object, offidxstart + size); 1567 retry: 1568 KASSERT(pctrie_iter_is_reset(&pages), 1569 ("%s: pctrie_iter not reset for retry", __func__)); 1570 for (m = vm_radix_iter_lookup_ge(&pages, offidxstart); m != NULL; 1571 m = vm_radix_iter_step(&pages)) { 1572 /* 1573 * We must wait for pending I/O to complete before we can 1574 * rename the page. 1575 * 1576 * We do not have to VM_PROT_NONE the page as mappings should 1577 * not be changed by this operation. 1578 */ 1579 if (vm_page_tryxbusy(m) == 0) { 1580 VM_OBJECT_WUNLOCK(new_object); 1581 if (vm_page_busy_sleep(m, "spltwt", 0)) 1582 VM_OBJECT_WLOCK(orig_object); 1583 pctrie_iter_reset(&pages); 1584 VM_OBJECT_WLOCK(new_object); 1585 goto retry; 1586 } 1587 1588 /* 1589 * If the page was left invalid, it was likely placed there by 1590 * an incomplete fault. Just remove and ignore. 1591 * 1592 * One other possibility is that the map entry is wired, in 1593 * which case we must hang on to the page to avoid leaking it, 1594 * as the map entry owns the wiring. This case can arise if the 1595 * backing object is truncated by the pager. 1596 */ 1597 if (vm_page_none_valid(m) && entry->wired_count == 0) { 1598 if (vm_page_iter_remove(&pages, m)) 1599 vm_page_free(m); 1600 continue; 1601 } 1602 1603 /* vm_page_iter_rename() will dirty the page if it is valid. */ 1604 if (!vm_page_iter_rename(&pages, m, new_object, m->pindex - 1605 offidxstart)) { 1606 vm_page_xunbusy(m); 1607 VM_OBJECT_WUNLOCK(new_object); 1608 VM_OBJECT_WUNLOCK(orig_object); 1609 vm_radix_wait(); 1610 pctrie_iter_reset(&pages); 1611 VM_OBJECT_WLOCK(orig_object); 1612 VM_OBJECT_WLOCK(new_object); 1613 goto retry; 1614 } 1615 1616 #if VM_NRESERVLEVEL > 0 1617 /* 1618 * If some of the reservation's allocated pages remain with 1619 * the original object, then transferring the reservation to 1620 * the new object is neither particularly beneficial nor 1621 * particularly harmful as compared to leaving the reservation 1622 * with the original object. If, however, all of the 1623 * reservation's allocated pages are transferred to the new 1624 * object, then transferring the reservation is typically 1625 * beneficial. Determining which of these two cases applies 1626 * would be more costly than unconditionally renaming the 1627 * reservation. 1628 */ 1629 vm_reserv_rename(m, new_object, orig_object, offidxstart); 1630 #endif 1631 } 1632 1633 /* 1634 * swap_pager_copy() can sleep, in which case the orig_object's 1635 * and new_object's locks are released and reacquired. 1636 */ 1637 swap_pager_copy(orig_object, new_object, offidxstart, 0); 1638 vm_page_iter_init(&pages, new_object); 1639 VM_RADIX_FOREACH(m, &pages) 1640 vm_page_xunbusy(m); 1641 1642 vm_object_clear_flag(orig_object, OBJ_SPLIT); 1643 VM_OBJECT_WUNLOCK(orig_object); 1644 VM_OBJECT_WUNLOCK(new_object); 1645 entry->object.vm_object = new_object; 1646 entry->offset = 0LL; 1647 vm_object_deallocate(orig_object); 1648 VM_OBJECT_WLOCK(new_object); 1649 } 1650 1651 static vm_page_t 1652 vm_object_collapse_scan_wait(struct pctrie_iter *pages, vm_object_t object, 1653 vm_page_t p) 1654 { 1655 vm_object_t backing_object; 1656 1657 VM_OBJECT_ASSERT_WLOCKED(object); 1658 backing_object = object->backing_object; 1659 VM_OBJECT_ASSERT_WLOCKED(backing_object); 1660 1661 KASSERT(p == NULL || p->object == object || p->object == backing_object, 1662 ("invalid ownership %p %p %p", p, object, backing_object)); 1663 /* The page is only NULL when rename fails. */ 1664 if (p == NULL) { 1665 VM_OBJECT_WUNLOCK(object); 1666 VM_OBJECT_WUNLOCK(backing_object); 1667 vm_radix_wait(); 1668 VM_OBJECT_WLOCK(object); 1669 } else if (p->object == object) { 1670 VM_OBJECT_WUNLOCK(backing_object); 1671 if (vm_page_busy_sleep(p, "vmocol", 0)) 1672 VM_OBJECT_WLOCK(object); 1673 } else { 1674 VM_OBJECT_WUNLOCK(object); 1675 if (!vm_page_busy_sleep(p, "vmocol", 0)) 1676 VM_OBJECT_WUNLOCK(backing_object); 1677 VM_OBJECT_WLOCK(object); 1678 } 1679 VM_OBJECT_WLOCK(backing_object); 1680 vm_page_iter_init(pages, backing_object); 1681 return (vm_radix_iter_lookup_ge(pages, 0)); 1682 } 1683 1684 static void 1685 vm_object_collapse_scan(vm_object_t object) 1686 { 1687 struct pctrie_iter pages; 1688 vm_object_t backing_object; 1689 vm_page_t next, p, pp; 1690 vm_pindex_t backing_offset_index, new_pindex; 1691 1692 VM_OBJECT_ASSERT_WLOCKED(object); 1693 VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 1694 1695 backing_object = object->backing_object; 1696 backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 1697 1698 /* 1699 * Our scan 1700 */ 1701 vm_page_iter_init(&pages, backing_object); 1702 for (p = vm_radix_iter_lookup_ge(&pages, 0); p != NULL; p = next) { 1703 /* 1704 * Check for busy page 1705 */ 1706 if (vm_page_tryxbusy(p) == 0) { 1707 next = vm_object_collapse_scan_wait(&pages, object, p); 1708 continue; 1709 } 1710 1711 KASSERT(object->backing_object == backing_object, 1712 ("vm_object_collapse_scan: backing object mismatch %p != %p", 1713 object->backing_object, backing_object)); 1714 KASSERT(p->object == backing_object, 1715 ("vm_object_collapse_scan: object mismatch %p != %p", 1716 p->object, backing_object)); 1717 1718 if (p->pindex < backing_offset_index || object->size <= 1719 (new_pindex = p->pindex - backing_offset_index)) { 1720 vm_pager_freespace(backing_object, p->pindex, 1); 1721 1722 KASSERT(!pmap_page_is_mapped(p), 1723 ("freeing mapped page %p", p)); 1724 if (vm_page_iter_remove(&pages, p)) 1725 vm_page_free(p); 1726 next = vm_radix_iter_step(&pages); 1727 continue; 1728 } 1729 1730 if (!vm_page_all_valid(p)) { 1731 KASSERT(!pmap_page_is_mapped(p), 1732 ("freeing mapped page %p", p)); 1733 if (vm_page_iter_remove(&pages, p)) 1734 vm_page_free(p); 1735 next = vm_radix_iter_step(&pages); 1736 continue; 1737 } 1738 1739 pp = vm_page_lookup(object, new_pindex); 1740 if (pp != NULL && vm_page_tryxbusy(pp) == 0) { 1741 vm_page_xunbusy(p); 1742 /* 1743 * The page in the parent is busy and possibly not 1744 * (yet) valid. Until its state is finalized by the 1745 * busy bit owner, we can't tell whether it shadows the 1746 * original page. 1747 */ 1748 next = vm_object_collapse_scan_wait(&pages, object, pp); 1749 continue; 1750 } 1751 1752 if (pp != NULL && vm_page_none_valid(pp)) { 1753 /* 1754 * The page was invalid in the parent. Likely placed 1755 * there by an incomplete fault. Just remove and 1756 * ignore. p can replace it. 1757 */ 1758 if (vm_page_remove(pp)) 1759 vm_page_free(pp); 1760 pp = NULL; 1761 } 1762 1763 if (pp != NULL || vm_pager_has_page(object, new_pindex, NULL, 1764 NULL)) { 1765 /* 1766 * The page already exists in the parent OR swap exists 1767 * for this location in the parent. Leave the parent's 1768 * page alone. Destroy the original page from the 1769 * backing object. 1770 */ 1771 vm_pager_freespace(backing_object, p->pindex, 1); 1772 KASSERT(!pmap_page_is_mapped(p), 1773 ("freeing mapped page %p", p)); 1774 if (pp != NULL) 1775 vm_page_xunbusy(pp); 1776 if (vm_page_iter_remove(&pages, p)) 1777 vm_page_free(p); 1778 next = vm_radix_iter_step(&pages); 1779 continue; 1780 } 1781 1782 /* 1783 * Page does not exist in parent, rename the page from the 1784 * backing object to the main object. 1785 * 1786 * If the page was mapped to a process, it can remain mapped 1787 * through the rename. vm_page_iter_rename() will dirty the 1788 * page. 1789 */ 1790 if (!vm_page_iter_rename(&pages, p, object, new_pindex)) { 1791 vm_page_xunbusy(p); 1792 next = vm_object_collapse_scan_wait(&pages, object, 1793 NULL); 1794 continue; 1795 } 1796 1797 /* Use the old pindex to free the right page. */ 1798 vm_pager_freespace(backing_object, new_pindex + 1799 backing_offset_index, 1); 1800 1801 #if VM_NRESERVLEVEL > 0 1802 /* 1803 * Rename the reservation. 1804 */ 1805 vm_reserv_rename(p, object, backing_object, 1806 backing_offset_index); 1807 #endif 1808 vm_page_xunbusy(p); 1809 next = vm_radix_iter_step(&pages); 1810 } 1811 return; 1812 } 1813 1814 /* 1815 * vm_object_collapse: 1816 * 1817 * Collapse an object with the object backing it. 1818 * Pages in the backing object are moved into the 1819 * parent, and the backing object is deallocated. 1820 */ 1821 void 1822 vm_object_collapse(vm_object_t object) 1823 { 1824 vm_object_t backing_object, new_backing_object; 1825 1826 VM_OBJECT_ASSERT_WLOCKED(object); 1827 1828 while (TRUE) { 1829 KASSERT((object->flags & (OBJ_DEAD | OBJ_ANON)) == OBJ_ANON, 1830 ("collapsing invalid object")); 1831 1832 /* 1833 * Wait for the backing_object to finish any pending 1834 * collapse so that the caller sees the shortest possible 1835 * shadow chain. 1836 */ 1837 backing_object = vm_object_backing_collapse_wait(object); 1838 if (backing_object == NULL) 1839 return; 1840 1841 KASSERT(object->ref_count > 0 && 1842 object->ref_count > atomic_load_int(&object->shadow_count), 1843 ("collapse with invalid ref %d or shadow %d count.", 1844 object->ref_count, atomic_load_int(&object->shadow_count))); 1845 KASSERT((backing_object->flags & 1846 (OBJ_COLLAPSING | OBJ_DEAD)) == 0, 1847 ("vm_object_collapse: Backing object already collapsing.")); 1848 KASSERT((object->flags & (OBJ_COLLAPSING | OBJ_DEAD)) == 0, 1849 ("vm_object_collapse: object is already collapsing.")); 1850 1851 /* 1852 * We know that we can either collapse the backing object if 1853 * the parent is the only reference to it, or (perhaps) have 1854 * the parent bypass the object if the parent happens to shadow 1855 * all the resident pages in the entire backing object. 1856 */ 1857 if (backing_object->ref_count == 1) { 1858 KASSERT(atomic_load_int(&backing_object->shadow_count) 1859 == 1, 1860 ("vm_object_collapse: shadow_count: %d", 1861 atomic_load_int(&backing_object->shadow_count))); 1862 vm_object_pip_add(object, 1); 1863 vm_object_set_flag(object, OBJ_COLLAPSING); 1864 vm_object_pip_add(backing_object, 1); 1865 vm_object_set_flag(backing_object, OBJ_DEAD); 1866 1867 /* 1868 * If there is exactly one reference to the backing 1869 * object, we can collapse it into the parent. 1870 */ 1871 vm_object_collapse_scan(object); 1872 1873 /* 1874 * Move the pager from backing_object to object. 1875 * 1876 * swap_pager_copy() can sleep, in which case the 1877 * backing_object's and object's locks are released and 1878 * reacquired. 1879 */ 1880 swap_pager_copy(backing_object, object, 1881 OFF_TO_IDX(object->backing_object_offset), TRUE); 1882 1883 /* 1884 * Object now shadows whatever backing_object did. 1885 */ 1886 vm_object_clear_flag(object, OBJ_COLLAPSING); 1887 vm_object_backing_transfer(object, backing_object); 1888 object->backing_object_offset += 1889 backing_object->backing_object_offset; 1890 VM_OBJECT_WUNLOCK(object); 1891 vm_object_pip_wakeup(object); 1892 1893 /* 1894 * Discard backing_object. 1895 * 1896 * Since the backing object has no pages, no pager left, 1897 * and no object references within it, all that is 1898 * necessary is to dispose of it. 1899 */ 1900 KASSERT(backing_object->ref_count == 1, ( 1901 "backing_object %p was somehow re-referenced during collapse!", 1902 backing_object)); 1903 vm_object_pip_wakeup(backing_object); 1904 (void)refcount_release(&backing_object->ref_count); 1905 umtx_shm_object_terminated(backing_object); 1906 vm_object_terminate(backing_object); 1907 counter_u64_add(object_collapses, 1); 1908 VM_OBJECT_WLOCK(object); 1909 } else { 1910 /* 1911 * If we do not entirely shadow the backing object, 1912 * there is nothing we can do so we give up. 1913 * 1914 * The object lock and backing_object lock must not 1915 * be dropped during this sequence. 1916 */ 1917 if (!swap_pager_scan_all_shadowed(object)) { 1918 VM_OBJECT_WUNLOCK(backing_object); 1919 break; 1920 } 1921 1922 /* 1923 * Make the parent shadow the next object in the 1924 * chain. Deallocating backing_object will not remove 1925 * it, since its reference count is at least 2. 1926 */ 1927 vm_object_backing_remove_locked(object); 1928 new_backing_object = backing_object->backing_object; 1929 if (new_backing_object != NULL) { 1930 vm_object_backing_insert_ref(object, 1931 new_backing_object); 1932 object->backing_object_offset += 1933 backing_object->backing_object_offset; 1934 } 1935 1936 /* 1937 * Drop the reference count on backing_object. Since 1938 * its ref_count was at least 2, it will not vanish. 1939 */ 1940 (void)refcount_release(&backing_object->ref_count); 1941 KASSERT(backing_object->ref_count >= 1, ( 1942 "backing_object %p was somehow dereferenced during collapse!", 1943 backing_object)); 1944 VM_OBJECT_WUNLOCK(backing_object); 1945 counter_u64_add(object_bypasses, 1); 1946 } 1947 1948 /* 1949 * Try again with this object's new backing object. 1950 */ 1951 } 1952 } 1953 1954 /* 1955 * vm_object_page_remove: 1956 * 1957 * For the given object, either frees or invalidates each of the 1958 * specified pages. In general, a page is freed. However, if a page is 1959 * wired for any reason other than the existence of a managed, wired 1960 * mapping, then it may be invalidated but not removed from the object. 1961 * Pages are specified by the given range ["start", "end") and the option 1962 * OBJPR_CLEANONLY. As a special case, if "end" is zero, then the range 1963 * extends from "start" to the end of the object. If the option 1964 * OBJPR_CLEANONLY is specified, then only the non-dirty pages within the 1965 * specified range are affected. If the option OBJPR_NOTMAPPED is 1966 * specified, then the pages within the specified range must have no 1967 * mappings. Otherwise, if this option is not specified, any mappings to 1968 * the specified pages are removed before the pages are freed or 1969 * invalidated. 1970 * 1971 * In general, this operation should only be performed on objects that 1972 * contain managed pages. There are, however, two exceptions. First, it 1973 * is performed on the kernel and kmem objects by vm_map_entry_delete(). 1974 * Second, it is used by msync(..., MS_INVALIDATE) to invalidate device- 1975 * backed pages. In both of these cases, the option OBJPR_CLEANONLY must 1976 * not be specified and the option OBJPR_NOTMAPPED must be specified. 1977 * 1978 * The object must be locked. 1979 */ 1980 void 1981 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 1982 int options) 1983 { 1984 struct pctrie_iter pages; 1985 vm_page_t p; 1986 1987 VM_OBJECT_ASSERT_WLOCKED(object); 1988 KASSERT((object->flags & OBJ_UNMANAGED) == 0 || 1989 (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED, 1990 ("vm_object_page_remove: illegal options for object %p", object)); 1991 if (object->resident_page_count == 0) 1992 return; 1993 vm_object_pip_add(object, 1); 1994 vm_page_iter_limit_init(&pages, object, end); 1995 again: 1996 KASSERT(pctrie_iter_is_reset(&pages), 1997 ("%s: pctrie_iter not reset for retry", __func__)); 1998 for (p = vm_radix_iter_lookup_ge(&pages, start); p != NULL; 1999 p = vm_radix_iter_step(&pages)) { 2000 /* 2001 * Skip invalid pages if asked to do so. Try to avoid acquiring 2002 * the busy lock, as some consumers rely on this to avoid 2003 * deadlocks. 2004 * 2005 * A thread may concurrently transition the page from invalid to 2006 * valid using only the busy lock, so the result of this check 2007 * is immediately stale. It is up to consumers to handle this, 2008 * for instance by ensuring that all invalid->valid transitions 2009 * happen with a mutex held, as may be possible for a 2010 * filesystem. 2011 */ 2012 if ((options & OBJPR_VALIDONLY) != 0 && vm_page_none_valid(p)) 2013 continue; 2014 2015 /* 2016 * If the page is wired for any reason besides the existence 2017 * of managed, wired mappings, then it cannot be freed. For 2018 * example, fictitious pages, which represent device memory, 2019 * are inherently wired and cannot be freed. They can, 2020 * however, be invalidated if the option OBJPR_CLEANONLY is 2021 * not specified. 2022 */ 2023 if (vm_page_tryxbusy(p) == 0) { 2024 if (vm_page_busy_sleep(p, "vmopar", 0)) 2025 VM_OBJECT_WLOCK(object); 2026 pctrie_iter_reset(&pages); 2027 goto again; 2028 } 2029 if ((options & OBJPR_VALIDONLY) != 0 && vm_page_none_valid(p)) { 2030 vm_page_xunbusy(p); 2031 continue; 2032 } 2033 if (vm_page_wired(p)) { 2034 wired: 2035 if ((options & OBJPR_NOTMAPPED) == 0 && 2036 object->ref_count != 0) 2037 pmap_remove_all(p); 2038 if ((options & OBJPR_CLEANONLY) == 0) { 2039 vm_page_invalid(p); 2040 vm_page_undirty(p); 2041 } 2042 vm_page_xunbusy(p); 2043 continue; 2044 } 2045 KASSERT((p->flags & PG_FICTITIOUS) == 0, 2046 ("vm_object_page_remove: page %p is fictitious", p)); 2047 if ((options & OBJPR_CLEANONLY) != 0 && 2048 !vm_page_none_valid(p)) { 2049 if ((options & OBJPR_NOTMAPPED) == 0 && 2050 object->ref_count != 0 && 2051 !vm_page_try_remove_write(p)) 2052 goto wired; 2053 if (p->dirty != 0) { 2054 vm_page_xunbusy(p); 2055 continue; 2056 } 2057 } 2058 if ((options & OBJPR_NOTMAPPED) == 0 && 2059 object->ref_count != 0 && !vm_page_try_remove_all(p)) 2060 goto wired; 2061 vm_page_iter_free(&pages, p); 2062 } 2063 vm_object_pip_wakeup(object); 2064 2065 vm_pager_freespace(object, start, (end == 0 ? object->size : end) - 2066 start); 2067 } 2068 2069 /* 2070 * vm_object_page_noreuse: 2071 * 2072 * For the given object, attempt to move the specified pages to 2073 * the head of the inactive queue. This bypasses regular LRU 2074 * operation and allows the pages to be reused quickly under memory 2075 * pressure. If a page is wired for any reason, then it will not 2076 * be queued. Pages are specified by the range ["start", "end"). 2077 * As a special case, if "end" is zero, then the range extends from 2078 * "start" to the end of the object. 2079 * 2080 * This operation should only be performed on objects that 2081 * contain non-fictitious, managed pages. 2082 * 2083 * The object must be locked. 2084 */ 2085 void 2086 vm_object_page_noreuse(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2087 { 2088 struct pctrie_iter pages; 2089 vm_page_t p; 2090 2091 VM_OBJECT_ASSERT_LOCKED(object); 2092 KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0, 2093 ("vm_object_page_noreuse: illegal object %p", object)); 2094 if (object->resident_page_count == 0) 2095 return; 2096 2097 vm_page_iter_limit_init(&pages, object, end); 2098 VM_RADIX_FOREACH_FROM(p, &pages, start) 2099 vm_page_deactivate_noreuse(p); 2100 } 2101 2102 /* 2103 * Populate the specified range of the object with valid pages. Returns 2104 * TRUE if the range is successfully populated and FALSE otherwise. 2105 * 2106 * Note: This function should be optimized to pass a larger array of 2107 * pages to vm_pager_get_pages() before it is applied to a non- 2108 * OBJT_DEVICE object. 2109 * 2110 * The object must be locked. 2111 */ 2112 boolean_t 2113 vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2114 { 2115 struct pctrie_iter pages; 2116 vm_page_t m; 2117 vm_pindex_t pindex; 2118 int rv; 2119 2120 vm_page_iter_init(&pages, object); 2121 VM_OBJECT_ASSERT_WLOCKED(object); 2122 for (pindex = start; pindex < end; pindex++) { 2123 rv = vm_page_grab_valid_iter(&m, object, pindex, 2124 VM_ALLOC_NORMAL, &pages); 2125 if (rv != VM_PAGER_OK) 2126 break; 2127 2128 /* 2129 * Keep "m" busy because a subsequent iteration may unlock 2130 * the object. 2131 */ 2132 } 2133 if (pindex > start) { 2134 pages.limit = pindex; 2135 VM_RADIX_FORALL_FROM(m, &pages, start) 2136 vm_page_xunbusy(m); 2137 } 2138 return (pindex == end); 2139 } 2140 2141 /* 2142 * Routine: vm_object_coalesce 2143 * Function: Coalesces two objects backing up adjoining 2144 * regions of memory into a single object. 2145 * 2146 * returns TRUE if objects were combined. 2147 * 2148 * NOTE: Only works at the moment if the second object is NULL - 2149 * if it's not, which object do we lock first? 2150 * 2151 * Parameters: 2152 * prev_object First object to coalesce 2153 * prev_offset Offset into prev_object 2154 * prev_size Size of reference to prev_object 2155 * next_size Size of reference to the second object 2156 * reserved Indicator that extension region has 2157 * swap accounted for 2158 * 2159 * Conditions: 2160 * The object must *not* be locked. 2161 */ 2162 boolean_t 2163 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 2164 vm_size_t prev_size, vm_size_t next_size, boolean_t reserved) 2165 { 2166 vm_pindex_t next_pindex; 2167 2168 if (prev_object == NULL) 2169 return (TRUE); 2170 if ((prev_object->flags & OBJ_ANON) == 0) 2171 return (FALSE); 2172 2173 VM_OBJECT_WLOCK(prev_object); 2174 /* 2175 * Try to collapse the object first. 2176 */ 2177 vm_object_collapse(prev_object); 2178 2179 /* 2180 * Can't coalesce if: . more than one reference . paged out . shadows 2181 * another object . has a copy elsewhere (any of which mean that the 2182 * pages not mapped to prev_entry may be in use anyway) 2183 */ 2184 if (prev_object->backing_object != NULL) { 2185 VM_OBJECT_WUNLOCK(prev_object); 2186 return (FALSE); 2187 } 2188 2189 prev_size >>= PAGE_SHIFT; 2190 next_size >>= PAGE_SHIFT; 2191 next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 2192 2193 if (prev_object->ref_count > 1 && 2194 prev_object->size != next_pindex && 2195 (prev_object->flags & OBJ_ONEMAPPING) == 0) { 2196 VM_OBJECT_WUNLOCK(prev_object); 2197 return (FALSE); 2198 } 2199 2200 /* 2201 * Account for the charge. 2202 */ 2203 if (prev_object->cred != NULL) { 2204 /* 2205 * If prev_object was charged, then this mapping, 2206 * although not charged now, may become writable 2207 * later. Non-NULL cred in the object would prevent 2208 * swap reservation during enabling of the write 2209 * access, so reserve swap now. Failed reservation 2210 * cause allocation of the separate object for the map 2211 * entry, and swap reservation for this entry is 2212 * managed in appropriate time. 2213 */ 2214 if (!reserved && !swap_reserve_by_cred(ptoa(next_size), 2215 prev_object->cred)) { 2216 VM_OBJECT_WUNLOCK(prev_object); 2217 return (FALSE); 2218 } 2219 prev_object->charge += ptoa(next_size); 2220 } 2221 2222 /* 2223 * Remove any pages that may still be in the object from a previous 2224 * deallocation. 2225 */ 2226 if (next_pindex < prev_object->size) { 2227 vm_object_page_remove(prev_object, next_pindex, next_pindex + 2228 next_size, 0); 2229 #if 0 2230 if (prev_object->cred != NULL) { 2231 KASSERT(prev_object->charge >= 2232 ptoa(prev_object->size - next_pindex), 2233 ("object %p overcharged 1 %jx %jx", prev_object, 2234 (uintmax_t)next_pindex, (uintmax_t)next_size)); 2235 prev_object->charge -= ptoa(prev_object->size - 2236 next_pindex); 2237 } 2238 #endif 2239 } 2240 2241 /* 2242 * Extend the object if necessary. 2243 */ 2244 if (next_pindex + next_size > prev_object->size) 2245 prev_object->size = next_pindex + next_size; 2246 2247 VM_OBJECT_WUNLOCK(prev_object); 2248 return (TRUE); 2249 } 2250 2251 /* 2252 * Fill in the m_dst array with up to *rbehind optional pages before m_src[0] 2253 * and up to *rahead optional pages after m_src[count - 1]. In both cases, stop 2254 * the filling-in short on encountering a cached page, an object boundary limit, 2255 * or an allocation error. Update *rbehind and *rahead to indicate the number 2256 * of pages allocated. Copy elements of m_src into array elements from 2257 * m_dst[*rbehind] to m_dst[*rbehind + count -1]. 2258 */ 2259 void 2260 vm_object_prepare_buf_pages(vm_object_t object, vm_page_t *ma_dst, int count, 2261 int *rbehind, int *rahead, vm_page_t *ma_src) 2262 { 2263 struct pctrie_iter pages; 2264 vm_pindex_t pindex; 2265 vm_page_t m, mpred, msucc; 2266 2267 vm_page_iter_init(&pages, object); 2268 VM_OBJECT_ASSERT_LOCKED(object); 2269 if (*rbehind != 0) { 2270 m = ma_src[0]; 2271 pindex = m->pindex; 2272 mpred = vm_radix_iter_lookup_lt(&pages, pindex); 2273 *rbehind = MIN(*rbehind, 2274 pindex - (mpred != NULL ? mpred->pindex + 1 : 0)); 2275 for (int i = 0; i < *rbehind; i++) { 2276 m = vm_page_alloc_iter(object, pindex - i - 1, 2277 VM_ALLOC_NORMAL, &pages); 2278 if (m == NULL) { 2279 /* Shift the array. */ 2280 for (int j = 0; j < i; j++) 2281 ma_dst[j] = ma_dst[j + *rbehind - i]; 2282 *rbehind = i; 2283 *rahead = 0; 2284 break; 2285 } 2286 ma_dst[*rbehind - i - 1] = m; 2287 } 2288 } 2289 for (int i = 0; i < count; i++) 2290 ma_dst[*rbehind + i] = ma_src[i]; 2291 if (*rahead != 0) { 2292 m = ma_src[count - 1]; 2293 pindex = m->pindex + 1; 2294 msucc = vm_radix_iter_lookup_ge(&pages, pindex); 2295 *rahead = MIN(*rahead, 2296 (msucc != NULL ? msucc->pindex : object->size) - pindex); 2297 for (int i = 0; i < *rahead; i++) { 2298 m = vm_page_alloc_iter(object, pindex + i, 2299 VM_ALLOC_NORMAL, &pages); 2300 if (m == NULL) { 2301 *rahead = i; 2302 break; 2303 } 2304 ma_dst[*rbehind + count + i] = m; 2305 } 2306 } 2307 } 2308 2309 void 2310 vm_object_set_writeable_dirty_(vm_object_t object) 2311 { 2312 atomic_add_int(&object->generation, 1); 2313 } 2314 2315 bool 2316 vm_object_mightbedirty_(vm_object_t object) 2317 { 2318 return (object->generation != object->cleangeneration); 2319 } 2320 2321 /* 2322 * vm_object_unwire: 2323 * 2324 * For each page offset within the specified range of the given object, 2325 * find the highest-level page in the shadow chain and unwire it. A page 2326 * must exist at every page offset, and the highest-level page must be 2327 * wired. 2328 */ 2329 void 2330 vm_object_unwire(vm_object_t object, vm_ooffset_t offset, vm_size_t length, 2331 uint8_t queue) 2332 { 2333 struct pctrie_iter pages; 2334 vm_object_t tobject, t1object; 2335 vm_page_t m, tm; 2336 vm_pindex_t end_pindex, pindex, tpindex; 2337 int depth, locked_depth; 2338 2339 KASSERT((offset & PAGE_MASK) == 0, 2340 ("vm_object_unwire: offset is not page aligned")); 2341 KASSERT((length & PAGE_MASK) == 0, 2342 ("vm_object_unwire: length is not a multiple of PAGE_SIZE")); 2343 /* The wired count of a fictitious page never changes. */ 2344 if ((object->flags & OBJ_FICTITIOUS) != 0) 2345 return; 2346 pindex = OFF_TO_IDX(offset); 2347 end_pindex = pindex + atop(length); 2348 vm_page_iter_init(&pages, object); 2349 again: 2350 locked_depth = 1; 2351 VM_OBJECT_RLOCK(object); 2352 m = vm_radix_iter_lookup_ge(&pages, pindex); 2353 while (pindex < end_pindex) { 2354 if (m == NULL || pindex < m->pindex) { 2355 /* 2356 * The first object in the shadow chain doesn't 2357 * contain a page at the current index. Therefore, 2358 * the page must exist in a backing object. 2359 */ 2360 tobject = object; 2361 tpindex = pindex; 2362 depth = 0; 2363 do { 2364 tpindex += 2365 OFF_TO_IDX(tobject->backing_object_offset); 2366 tobject = tobject->backing_object; 2367 KASSERT(tobject != NULL, 2368 ("vm_object_unwire: missing page")); 2369 if ((tobject->flags & OBJ_FICTITIOUS) != 0) 2370 goto next_page; 2371 depth++; 2372 if (depth == locked_depth) { 2373 locked_depth++; 2374 VM_OBJECT_RLOCK(tobject); 2375 } 2376 } while ((tm = vm_page_lookup(tobject, tpindex)) == 2377 NULL); 2378 } else { 2379 tm = m; 2380 m = vm_radix_iter_step(&pages); 2381 } 2382 if (vm_page_trysbusy(tm) == 0) { 2383 for (tobject = object; locked_depth >= 1; 2384 locked_depth--) { 2385 t1object = tobject->backing_object; 2386 if (tm->object != tobject) 2387 VM_OBJECT_RUNLOCK(tobject); 2388 tobject = t1object; 2389 } 2390 tobject = tm->object; 2391 if (!vm_page_busy_sleep(tm, "unwbo", 2392 VM_ALLOC_IGN_SBUSY)) 2393 VM_OBJECT_RUNLOCK(tobject); 2394 pctrie_iter_reset(&pages); 2395 goto again; 2396 } 2397 vm_page_unwire(tm, queue); 2398 vm_page_sunbusy(tm); 2399 next_page: 2400 pindex++; 2401 } 2402 /* Release the accumulated object locks. */ 2403 for (tobject = object; locked_depth >= 1; locked_depth--) { 2404 t1object = tobject->backing_object; 2405 VM_OBJECT_RUNLOCK(tobject); 2406 tobject = t1object; 2407 } 2408 } 2409 2410 /* 2411 * Return the vnode for the given object, or NULL if none exists. 2412 * For tmpfs objects, the function may return NULL if there is 2413 * no vnode allocated at the time of the call. 2414 */ 2415 struct vnode * 2416 vm_object_vnode(vm_object_t object) 2417 { 2418 struct vnode *vp; 2419 2420 VM_OBJECT_ASSERT_LOCKED(object); 2421 vm_pager_getvp(object, &vp, NULL); 2422 return (vp); 2423 } 2424 2425 /* 2426 * Busy the vm object. This prevents new pages belonging to the object from 2427 * becoming busy. Existing pages persist as busy. Callers are responsible 2428 * for checking page state before proceeding. 2429 */ 2430 void 2431 vm_object_busy(vm_object_t obj) 2432 { 2433 2434 VM_OBJECT_ASSERT_LOCKED(obj); 2435 2436 blockcount_acquire(&obj->busy, 1); 2437 /* The fence is required to order loads of page busy. */ 2438 atomic_thread_fence_acq_rel(); 2439 } 2440 2441 void 2442 vm_object_unbusy(vm_object_t obj) 2443 { 2444 2445 blockcount_release(&obj->busy, 1); 2446 } 2447 2448 void 2449 vm_object_busy_wait(vm_object_t obj, const char *wmesg) 2450 { 2451 2452 VM_OBJECT_ASSERT_UNLOCKED(obj); 2453 2454 (void)blockcount_sleep(&obj->busy, NULL, wmesg, PVM); 2455 } 2456 2457 /* 2458 * This function aims to determine if the object is mapped, 2459 * specifically, if it is referenced by a vm_map_entry. Because 2460 * objects occasionally acquire transient references that do not 2461 * represent a mapping, the method used here is inexact. However, it 2462 * has very low overhead and is good enough for the advisory 2463 * vm.vmtotal sysctl. 2464 */ 2465 bool 2466 vm_object_is_active(vm_object_t obj) 2467 { 2468 2469 return (obj->ref_count > atomic_load_int(&obj->shadow_count)); 2470 } 2471 2472 static int 2473 vm_object_list_handler(struct sysctl_req *req, bool swap_only) 2474 { 2475 struct pctrie_iter pages; 2476 struct kinfo_vmobject *kvo; 2477 char *fullpath, *freepath; 2478 struct vnode *vp; 2479 struct vattr va; 2480 vm_object_t obj; 2481 vm_page_t m; 2482 u_long sp; 2483 int count, error; 2484 key_t key; 2485 unsigned short seq; 2486 bool want_path; 2487 2488 if (req->oldptr == NULL) { 2489 /* 2490 * If an old buffer has not been provided, generate an 2491 * estimate of the space needed for a subsequent call. 2492 */ 2493 mtx_lock(&vm_object_list_mtx); 2494 count = 0; 2495 TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2496 if (obj->type == OBJT_DEAD) 2497 continue; 2498 count++; 2499 } 2500 mtx_unlock(&vm_object_list_mtx); 2501 return (SYSCTL_OUT(req, NULL, sizeof(struct kinfo_vmobject) * 2502 count * 11 / 10)); 2503 } 2504 2505 want_path = !(swap_only || jailed(curthread->td_ucred)); 2506 kvo = malloc(sizeof(*kvo), M_TEMP, M_WAITOK | M_ZERO); 2507 error = 0; 2508 2509 /* 2510 * VM objects are type stable and are never removed from the 2511 * list once added. This allows us to safely read obj->object_list 2512 * after reacquiring the VM object lock. 2513 */ 2514 mtx_lock(&vm_object_list_mtx); 2515 TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2516 if (obj->type == OBJT_DEAD || 2517 (swap_only && (obj->flags & (OBJ_ANON | OBJ_SWAP)) == 0)) 2518 continue; 2519 VM_OBJECT_RLOCK(obj); 2520 if (obj->type == OBJT_DEAD || 2521 (swap_only && (obj->flags & (OBJ_ANON | OBJ_SWAP)) == 0)) { 2522 VM_OBJECT_RUNLOCK(obj); 2523 continue; 2524 } 2525 mtx_unlock(&vm_object_list_mtx); 2526 kvo->kvo_size = ptoa(obj->size); 2527 kvo->kvo_resident = obj->resident_page_count; 2528 kvo->kvo_ref_count = obj->ref_count; 2529 kvo->kvo_shadow_count = atomic_load_int(&obj->shadow_count); 2530 kvo->kvo_memattr = obj->memattr; 2531 kvo->kvo_active = 0; 2532 kvo->kvo_inactive = 0; 2533 kvo->kvo_flags = 0; 2534 if (!swap_only) { 2535 vm_page_iter_init(&pages, obj); 2536 VM_RADIX_FOREACH(m, &pages) { 2537 /* 2538 * A page may belong to the object but be 2539 * dequeued and set to PQ_NONE while the 2540 * object lock is not held. This makes the 2541 * reads of m->queue below racy, and we do not 2542 * count pages set to PQ_NONE. However, this 2543 * sysctl is only meant to give an 2544 * approximation of the system anyway. 2545 */ 2546 if (vm_page_active(m)) 2547 kvo->kvo_active++; 2548 else if (vm_page_inactive(m)) 2549 kvo->kvo_inactive++; 2550 else if (vm_page_in_laundry(m)) 2551 kvo->kvo_laundry++; 2552 } 2553 } 2554 2555 kvo->kvo_vn_fileid = 0; 2556 kvo->kvo_vn_fsid = 0; 2557 kvo->kvo_vn_fsid_freebsd11 = 0; 2558 freepath = NULL; 2559 fullpath = ""; 2560 vp = NULL; 2561 kvo->kvo_type = vm_object_kvme_type(obj, want_path ? &vp : 2562 NULL); 2563 if (vp != NULL) { 2564 vref(vp); 2565 } else if ((obj->flags & OBJ_ANON) != 0) { 2566 MPASS(kvo->kvo_type == KVME_TYPE_SWAP); 2567 kvo->kvo_me = (uintptr_t)obj; 2568 /* tmpfs objs are reported as vnodes */ 2569 kvo->kvo_backing_obj = (uintptr_t)obj->backing_object; 2570 sp = swap_pager_swapped_pages(obj); 2571 kvo->kvo_swapped = sp > UINT32_MAX ? UINT32_MAX : sp; 2572 } 2573 if (obj->type == OBJT_DEVICE || obj->type == OBJT_MGTDEVICE) { 2574 cdev_pager_get_path(obj, kvo->kvo_path, 2575 sizeof(kvo->kvo_path)); 2576 } 2577 VM_OBJECT_RUNLOCK(obj); 2578 if ((obj->flags & OBJ_SYSVSHM) != 0) { 2579 kvo->kvo_flags |= KVMO_FLAG_SYSVSHM; 2580 shmobjinfo(obj, &key, &seq); 2581 kvo->kvo_vn_fileid = key; 2582 kvo->kvo_vn_fsid_freebsd11 = seq; 2583 } 2584 if ((obj->flags & OBJ_POSIXSHM) != 0) { 2585 kvo->kvo_flags |= KVMO_FLAG_POSIXSHM; 2586 shm_get_path(obj, kvo->kvo_path, 2587 sizeof(kvo->kvo_path)); 2588 } 2589 if (vp != NULL) { 2590 vn_fullpath(vp, &fullpath, &freepath); 2591 vn_lock(vp, LK_SHARED | LK_RETRY); 2592 if (VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) { 2593 kvo->kvo_vn_fileid = va.va_fileid; 2594 kvo->kvo_vn_fsid = va.va_fsid; 2595 kvo->kvo_vn_fsid_freebsd11 = va.va_fsid; 2596 /* truncate */ 2597 } 2598 vput(vp); 2599 strlcpy(kvo->kvo_path, fullpath, sizeof(kvo->kvo_path)); 2600 free(freepath, M_TEMP); 2601 } 2602 2603 /* Pack record size down */ 2604 kvo->kvo_structsize = offsetof(struct kinfo_vmobject, kvo_path) 2605 + strlen(kvo->kvo_path) + 1; 2606 kvo->kvo_structsize = roundup(kvo->kvo_structsize, 2607 sizeof(uint64_t)); 2608 error = SYSCTL_OUT(req, kvo, kvo->kvo_structsize); 2609 maybe_yield(); 2610 mtx_lock(&vm_object_list_mtx); 2611 if (error) 2612 break; 2613 } 2614 mtx_unlock(&vm_object_list_mtx); 2615 free(kvo, M_TEMP); 2616 return (error); 2617 } 2618 2619 static int 2620 sysctl_vm_object_list(SYSCTL_HANDLER_ARGS) 2621 { 2622 return (vm_object_list_handler(req, false)); 2623 } 2624 2625 SYSCTL_PROC(_vm, OID_AUTO, objects, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP | 2626 CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_object_list, "S,kinfo_vmobject", 2627 "List of VM objects"); 2628 2629 static int 2630 sysctl_vm_object_list_swap(SYSCTL_HANDLER_ARGS) 2631 { 2632 return (vm_object_list_handler(req, true)); 2633 } 2634 2635 /* 2636 * This sysctl returns list of the anonymous or swap objects. Intent 2637 * is to provide stripped optimized list useful to analyze swap use. 2638 * Since technically non-swap (default) objects participate in the 2639 * shadow chains, and are converted to swap type as needed by swap 2640 * pager, we must report them. 2641 */ 2642 SYSCTL_PROC(_vm, OID_AUTO, swap_objects, 2643 CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, 0, 2644 sysctl_vm_object_list_swap, "S,kinfo_vmobject", 2645 "List of swap VM objects"); 2646 2647 #include "opt_ddb.h" 2648 #ifdef DDB 2649 #include <sys/kernel.h> 2650 2651 #include <sys/cons.h> 2652 2653 #include <ddb/ddb.h> 2654 2655 static int 2656 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2657 { 2658 vm_map_t tmpm; 2659 vm_map_entry_t tmpe; 2660 vm_object_t obj; 2661 2662 if (map == 0) 2663 return 0; 2664 2665 if (entry == 0) { 2666 VM_MAP_ENTRY_FOREACH(tmpe, map) { 2667 if (_vm_object_in_map(map, object, tmpe)) { 2668 return 1; 2669 } 2670 } 2671 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 2672 tmpm = entry->object.sub_map; 2673 VM_MAP_ENTRY_FOREACH(tmpe, tmpm) { 2674 if (_vm_object_in_map(tmpm, object, tmpe)) { 2675 return 1; 2676 } 2677 } 2678 } else if ((obj = entry->object.vm_object) != NULL) { 2679 for (; obj; obj = obj->backing_object) 2680 if (obj == object) { 2681 return 1; 2682 } 2683 } 2684 return 0; 2685 } 2686 2687 static int 2688 vm_object_in_map(vm_object_t object) 2689 { 2690 struct proc *p; 2691 2692 /* sx_slock(&allproc_lock); */ 2693 FOREACH_PROC_IN_SYSTEM(p) { 2694 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2695 continue; 2696 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 2697 /* sx_sunlock(&allproc_lock); */ 2698 return 1; 2699 } 2700 } 2701 /* sx_sunlock(&allproc_lock); */ 2702 if (_vm_object_in_map(kernel_map, object, 0)) 2703 return 1; 2704 return 0; 2705 } 2706 2707 DB_SHOW_COMMAND_FLAGS(vmochk, vm_object_check, DB_CMD_MEMSAFE) 2708 { 2709 vm_object_t object; 2710 2711 /* 2712 * make sure that internal objs are in a map somewhere 2713 * and none have zero ref counts. 2714 */ 2715 TAILQ_FOREACH(object, &vm_object_list, object_list) { 2716 if ((object->flags & OBJ_ANON) != 0) { 2717 if (object->ref_count == 0) { 2718 db_printf( 2719 "vmochk: internal obj has zero ref count: %lu\n", 2720 (u_long)object->size); 2721 } 2722 if (!vm_object_in_map(object)) { 2723 db_printf( 2724 "vmochk: internal obj is not in a map: " 2725 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2726 object->ref_count, (u_long)object->size, 2727 (u_long)object->size, 2728 (void *)object->backing_object); 2729 } 2730 } 2731 if (db_pager_quit) 2732 return; 2733 } 2734 } 2735 2736 /* 2737 * vm_object_print: [ debug ] 2738 */ 2739 DB_SHOW_COMMAND(object, vm_object_print_static) 2740 { 2741 struct pctrie_iter pages; 2742 /* XXX convert args. */ 2743 vm_object_t object = (vm_object_t)addr; 2744 boolean_t full = have_addr; 2745 2746 vm_page_t p; 2747 2748 /* XXX count is an (unused) arg. Avoid shadowing it. */ 2749 #define count was_count 2750 2751 int count; 2752 2753 if (object == NULL) 2754 return; 2755 2756 db_iprintf("Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x", 2757 object, (int)object->type, (uintmax_t)object->size, 2758 object->resident_page_count, object->ref_count, object->flags); 2759 db_iprintf(" ruid %d charge %jx\n", 2760 object->cred ? object->cred->cr_ruid : -1, 2761 (uintmax_t)object->charge); 2762 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 2763 atomic_load_int(&object->shadow_count), 2764 object->backing_object ? object->backing_object->ref_count : 0, 2765 object->backing_object, (uintmax_t)object->backing_object_offset); 2766 2767 if (!full) 2768 return; 2769 2770 db_indent += 2; 2771 count = 0; 2772 vm_page_iter_init(&pages, object); 2773 VM_RADIX_FOREACH(p, &pages) { 2774 if (count == 0) 2775 db_iprintf("memory:="); 2776 else if (count == 6) { 2777 db_printf("\n"); 2778 db_iprintf(" ..."); 2779 count = 0; 2780 } else 2781 db_printf(","); 2782 count++; 2783 2784 db_printf("(off=0x%jx,page=0x%jx)", 2785 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2786 2787 if (db_pager_quit) 2788 break; 2789 } 2790 if (count != 0) 2791 db_printf("\n"); 2792 db_indent -= 2; 2793 } 2794 2795 /* XXX. */ 2796 #undef count 2797 2798 /* XXX need this non-static entry for calling from vm_map_print. */ 2799 void 2800 vm_object_print( 2801 /* db_expr_t */ long addr, 2802 boolean_t have_addr, 2803 /* db_expr_t */ long count, 2804 char *modif) 2805 { 2806 vm_object_print_static(addr, have_addr, count, modif); 2807 } 2808 2809 DB_SHOW_COMMAND_FLAGS(vmopag, vm_object_print_pages, DB_CMD_MEMSAFE) 2810 { 2811 struct pctrie_iter pages; 2812 vm_object_t object; 2813 vm_page_t m, start_m; 2814 int rcount; 2815 2816 TAILQ_FOREACH(object, &vm_object_list, object_list) { 2817 db_printf("new object: %p\n", (void *)object); 2818 if (db_pager_quit) 2819 return; 2820 start_m = NULL; 2821 vm_page_iter_init(&pages, object); 2822 VM_RADIX_FOREACH(m, &pages) { 2823 if (start_m == NULL) { 2824 start_m = m; 2825 rcount = 0; 2826 } else if (start_m->pindex + rcount != m->pindex || 2827 VM_PAGE_TO_PHYS(start_m) + ptoa(rcount) != 2828 VM_PAGE_TO_PHYS(m)) { 2829 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2830 (long)start_m->pindex, rcount, 2831 (long)VM_PAGE_TO_PHYS(start_m)); 2832 if (db_pager_quit) 2833 return; 2834 start_m = m; 2835 rcount = 0; 2836 } 2837 rcount++; 2838 } 2839 if (start_m != NULL) { 2840 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2841 (long)start_m->pindex, rcount, 2842 (long)VM_PAGE_TO_PHYS(start_m)); 2843 if (db_pager_quit) 2844 return; 2845 } 2846 } 2847 } 2848 #endif /* DDB */ 2849