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