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 int vm_object_page_collect_flush(struct pctrie_iter *pages, 114 vm_page_t p, int pagerflags, int flags, boolean_t *allclean, 115 boolean_t *eio); 116 static boolean_t vm_object_page_remove_write(vm_page_t p, int flags, 117 boolean_t *allclean); 118 static void vm_object_backing_remove(vm_object_t object); 119 120 /* 121 * Virtual memory objects maintain the actual data 122 * associated with allocated virtual memory. A given 123 * page of memory exists within exactly one object. 124 * 125 * An object is only deallocated when all "references" 126 * are given up. Only one "reference" to a given 127 * region of an object should be writeable. 128 * 129 * Associated with each object is a list of all resident 130 * memory pages belonging to that object; this list is 131 * maintained by the "vm_page" module, and locked by the object's 132 * lock. 133 * 134 * Each object also records a "pager" routine which is 135 * used to retrieve (and store) pages to the proper backing 136 * storage. In addition, objects may be backed by other 137 * objects from which they were virtual-copied. 138 * 139 * The only items within the object structure which are 140 * modified after time of creation are: 141 * reference count locked by object's lock 142 * pager routine locked by object's lock 143 * 144 */ 145 146 struct object_q vm_object_list; 147 struct mtx vm_object_list_mtx; /* lock for object list and count */ 148 149 struct vm_object kernel_object_store; 150 151 static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 152 "VM object stats"); 153 154 static COUNTER_U64_DEFINE_EARLY(object_collapses); 155 SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD, 156 &object_collapses, 157 "VM object collapses"); 158 159 static COUNTER_U64_DEFINE_EARLY(object_bypasses); 160 SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD, 161 &object_bypasses, 162 "VM object bypasses"); 163 164 static COUNTER_U64_DEFINE_EARLY(object_collapse_waits); 165 SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, collapse_waits, CTLFLAG_RD, 166 &object_collapse_waits, 167 "Number of sleeps for collapse"); 168 169 static uma_zone_t obj_zone; 170 171 static int vm_object_zinit(void *mem, int size, int flags); 172 173 #ifdef INVARIANTS 174 static void vm_object_zdtor(void *mem, int size, void *arg); 175 176 static void 177 vm_object_zdtor(void *mem, int size, void *arg) 178 { 179 vm_object_t object; 180 181 object = (vm_object_t)mem; 182 KASSERT(object->ref_count == 0, 183 ("object %p ref_count = %d", object, object->ref_count)); 184 KASSERT(vm_radix_is_empty(&object->rtree), 185 ("object %p has resident pages in its trie", object)); 186 #if VM_NRESERVLEVEL > 0 187 KASSERT(LIST_EMPTY(&object->rvq), 188 ("object %p has reservations", 189 object)); 190 #endif 191 KASSERT(!vm_object_busied(object), 192 ("object %p busy = %d", object, blockcount_read(&object->busy))); 193 KASSERT(object->resident_page_count == 0, 194 ("object %p resident_page_count = %d", 195 object, object->resident_page_count)); 196 KASSERT(atomic_load_int(&object->shadow_count) == 0, 197 ("object %p shadow_count = %d", 198 object, atomic_load_int(&object->shadow_count))); 199 KASSERT(object->type == OBJT_DEAD, 200 ("object %p has non-dead type %d", 201 object, object->type)); 202 KASSERT(object->charge == 0 && object->cred == NULL, 203 ("object %p has non-zero charge %ju (%p)", 204 object, (uintmax_t)object->charge, object->cred)); 205 } 206 #endif 207 208 static int 209 vm_object_zinit(void *mem, int size, int flags) 210 { 211 vm_object_t object; 212 213 object = (vm_object_t)mem; 214 rw_init_flags(&object->lock, "vmobject", RW_DUPOK | RW_NEW); 215 216 /* These are true for any object that has been freed */ 217 object->type = OBJT_DEAD; 218 vm_radix_init(&object->rtree); 219 refcount_init(&object->ref_count, 0); 220 blockcount_init(&object->paging_in_progress); 221 blockcount_init(&object->busy); 222 object->resident_page_count = 0; 223 atomic_store_int(&object->shadow_count, 0); 224 object->flags = OBJ_DEAD; 225 226 mtx_lock(&vm_object_list_mtx); 227 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 228 mtx_unlock(&vm_object_list_mtx); 229 return (0); 230 } 231 232 static void 233 _vm_object_allocate(objtype_t type, vm_pindex_t size, u_short flags, 234 vm_object_t object, void *handle) 235 { 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 (!vm_radix_is_empty(&object->rtree)) 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_single_page removes a pageable page from the object, 880 * and removes it from the paging queues and frees it, if it is not wired. 881 * It is invoked via callback from vm_object_terminate_pages. 882 */ 883 static void 884 vm_object_terminate_single_page(vm_page_t p, void *objectv) 885 { 886 vm_object_t object __diagused = objectv; 887 888 vm_page_assert_unbusied(p); 889 KASSERT(p->object == object && 890 (p->ref_count & VPRC_OBJREF) != 0, 891 ("%s: page %p is inconsistent", __func__, p)); 892 p->object = NULL; 893 if (vm_page_drop(p, VPRC_OBJREF) == VPRC_OBJREF) { 894 KASSERT((object->flags & OBJ_UNMANAGED) != 0 || 895 vm_page_astate_load(p).queue != PQ_NONE, 896 ("%s: page %p does not belong to a queue", __func__, p)); 897 VM_CNT_INC(v_pfree); 898 vm_page_free(p); 899 } 900 } 901 902 /* 903 * vm_object_terminate_pages removes any remaining pageable pages 904 * from the object and resets the object to an empty state. 905 */ 906 static void 907 vm_object_terminate_pages(vm_object_t object) 908 { 909 VM_OBJECT_ASSERT_WLOCKED(object); 910 911 /* 912 * If the object contained any pages, then reset it to an empty state. 913 * Rather than incrementally removing each page from the object, the 914 * page and object are reset to any empty state. 915 */ 916 if (object->resident_page_count == 0) 917 return; 918 919 vm_radix_reclaim_callback(&object->rtree, 920 vm_object_terminate_single_page, object); 921 object->resident_page_count = 0; 922 if (object->type == OBJT_VNODE) 923 vdrop(object->handle); 924 } 925 926 /* 927 * vm_object_terminate actually destroys the specified object, freeing 928 * up all previously used resources. 929 * 930 * The object must be locked. 931 * This routine may block. 932 */ 933 void 934 vm_object_terminate(vm_object_t object) 935 { 936 937 VM_OBJECT_ASSERT_WLOCKED(object); 938 KASSERT((object->flags & OBJ_DEAD) != 0, 939 ("terminating non-dead obj %p", object)); 940 KASSERT((object->flags & OBJ_COLLAPSING) == 0, 941 ("terminating collapsing obj %p", object)); 942 KASSERT(object->backing_object == NULL, 943 ("terminating shadow obj %p", object)); 944 945 /* 946 * Wait for the pageout daemon and other current users to be 947 * done with the object. Note that new paging_in_progress 948 * users can come after this wait, but they must check 949 * OBJ_DEAD flag set (without unlocking the object), and avoid 950 * the object being terminated. 951 */ 952 vm_object_pip_wait(object, "objtrm"); 953 954 KASSERT(object->ref_count == 0, 955 ("vm_object_terminate: object with references, ref_count=%d", 956 object->ref_count)); 957 958 if ((object->flags & OBJ_PG_DTOR) == 0) 959 vm_object_terminate_pages(object); 960 961 #if VM_NRESERVLEVEL > 0 962 if (__predict_false(!LIST_EMPTY(&object->rvq))) 963 vm_reserv_break_all(object); 964 #endif 965 966 KASSERT(object->cred == NULL || (object->flags & OBJ_SWAP) != 0, 967 ("%s: non-swap obj %p has cred", __func__, object)); 968 969 /* 970 * Let the pager know object is dead. 971 */ 972 vm_pager_deallocate(object); 973 VM_OBJECT_WUNLOCK(object); 974 975 vm_object_destroy(object); 976 } 977 978 /* 979 * Make the page read-only so that we can clear the object flags. However, if 980 * this is a nosync mmap then the object is likely to stay dirty so do not 981 * mess with the page and do not clear the object flags. Returns TRUE if the 982 * page should be flushed, and FALSE otherwise. 983 */ 984 static boolean_t 985 vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *allclean) 986 { 987 988 vm_page_assert_busied(p); 989 990 /* 991 * If we have been asked to skip nosync pages and this is a 992 * nosync page, skip it. Note that the object flags were not 993 * cleared in this case so we do not have to set them. 994 */ 995 if ((flags & OBJPC_NOSYNC) != 0 && (p->a.flags & PGA_NOSYNC) != 0) { 996 *allclean = FALSE; 997 return (FALSE); 998 } else { 999 pmap_remove_write(p); 1000 return (p->dirty != 0); 1001 } 1002 } 1003 1004 /* 1005 * vm_object_page_clean 1006 * 1007 * Clean all dirty pages in the specified range of object. Leaves page 1008 * on whatever queue it is currently on. If NOSYNC is set then do not 1009 * write out pages with PGA_NOSYNC set (originally comes from MAP_NOSYNC), 1010 * leaving the object dirty. 1011 * 1012 * For swap objects backing tmpfs regular files, do not flush anything, 1013 * but remove write protection on the mapped pages to update mtime through 1014 * mmaped writes. 1015 * 1016 * When stuffing pages asynchronously, allow clustering. XXX we need a 1017 * synchronous clustering mode implementation. 1018 * 1019 * Odd semantics: if start == end, we clean everything. 1020 * 1021 * The object must be locked. 1022 * 1023 * Returns FALSE if some page from the range was not written, as 1024 * reported by the pager, and TRUE otherwise. 1025 */ 1026 boolean_t 1027 vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end, 1028 int flags) 1029 { 1030 struct pctrie_iter pages; 1031 vm_page_t np, p; 1032 vm_pindex_t pi, tend, tstart; 1033 int curgeneration, n, pagerflags; 1034 boolean_t eio, res, allclean; 1035 1036 VM_OBJECT_ASSERT_WLOCKED(object); 1037 1038 if (!vm_object_mightbedirty(object) || object->resident_page_count == 0) 1039 return (TRUE); 1040 1041 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ? 1042 VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 1043 pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0; 1044 1045 tstart = OFF_TO_IDX(start); 1046 tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK); 1047 allclean = tstart == 0 && tend >= object->size; 1048 res = TRUE; 1049 vm_page_iter_init(&pages, object); 1050 1051 rescan: 1052 curgeneration = object->generation; 1053 1054 for (p = vm_radix_iter_lookup_ge(&pages, tstart); p != NULL; p = np) { 1055 pi = p->pindex; 1056 if (pi >= tend) 1057 break; 1058 if (vm_page_none_valid(p)) { 1059 np = vm_radix_iter_step(&pages); 1060 continue; 1061 } 1062 if (!vm_page_busy_acquire(p, VM_ALLOC_WAITFAIL)) { 1063 pctrie_iter_reset(&pages); 1064 if (object->generation != curgeneration && 1065 (flags & OBJPC_SYNC) != 0) 1066 goto rescan; 1067 np = vm_radix_iter_lookup_ge(&pages, pi); 1068 continue; 1069 } 1070 if (!vm_object_page_remove_write(p, flags, &allclean)) { 1071 np = vm_radix_iter_step(&pages); 1072 vm_page_xunbusy(p); 1073 continue; 1074 } 1075 if (object->type == OBJT_VNODE) { 1076 n = vm_object_page_collect_flush(&pages, p, pagerflags, 1077 flags, &allclean, &eio); 1078 pctrie_iter_reset(&pages); 1079 if (eio) { 1080 res = FALSE; 1081 allclean = FALSE; 1082 } 1083 if (object->generation != curgeneration && 1084 (flags & OBJPC_SYNC) != 0) 1085 goto rescan; 1086 1087 /* 1088 * If the VOP_PUTPAGES() did a truncated write, so 1089 * that even the first page of the run is not fully 1090 * written, vm_pageout_flush() returns 0 as the run 1091 * length. Since the condition that caused truncated 1092 * write may be permanent, e.g. exhausted free space, 1093 * accepting n == 0 would cause an infinite loop. 1094 * 1095 * Forwarding the iterator leaves the unwritten page 1096 * behind, but there is not much we can do there if 1097 * filesystem refuses to write it. 1098 */ 1099 if (n == 0) { 1100 n = 1; 1101 allclean = FALSE; 1102 } 1103 } else { 1104 n = 1; 1105 vm_page_xunbusy(p); 1106 } 1107 np = vm_radix_iter_lookup_ge(&pages, pi + n); 1108 } 1109 #if 0 1110 VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0); 1111 #endif 1112 1113 /* 1114 * Leave updating cleangeneration for tmpfs objects to tmpfs 1115 * scan. It needs to update mtime, which happens for other 1116 * filesystems during page writeouts. 1117 */ 1118 if (allclean && object->type == OBJT_VNODE) 1119 object->cleangeneration = curgeneration; 1120 return (res); 1121 } 1122 1123 static int 1124 vm_object_page_collect_flush(struct pctrie_iter *pages, vm_page_t p, 1125 int pagerflags, int flags, boolean_t *allclean, boolean_t *eio) 1126 { 1127 vm_page_t ma[2 * vm_pageout_page_count - 1]; 1128 int base, count, runlen; 1129 1130 vm_page_lock_assert(p, MA_NOTOWNED); 1131 vm_page_assert_xbusied(p); 1132 base = nitems(ma) / 2; 1133 ma[base] = p; 1134 for (count = 1; count < vm_pageout_page_count; count++) { 1135 p = vm_radix_iter_next(pages); 1136 if (p == NULL || vm_page_tryxbusy(p) == 0) 1137 break; 1138 if (!vm_object_page_remove_write(p, flags, allclean)) { 1139 vm_page_xunbusy(p); 1140 break; 1141 } 1142 ma[base + count] = p; 1143 } 1144 1145 pages->index = ma[base]->pindex; 1146 for (; count < vm_pageout_page_count; count++) { 1147 p = vm_radix_iter_prev(pages); 1148 if (p == NULL || vm_page_tryxbusy(p) == 0) 1149 break; 1150 if (!vm_object_page_remove_write(p, flags, allclean)) { 1151 vm_page_xunbusy(p); 1152 break; 1153 } 1154 ma[--base] = p; 1155 } 1156 1157 vm_pageout_flush(&ma[base], count, pagerflags, nitems(ma) / 2 - base, 1158 &runlen, eio); 1159 return (runlen); 1160 } 1161 1162 /* 1163 * Note that there is absolutely no sense in writing out 1164 * anonymous objects, so we track down the vnode object 1165 * to write out. 1166 * We invalidate (remove) all pages from the address space 1167 * for semantic correctness. 1168 * 1169 * If the backing object is a device object with unmanaged pages, then any 1170 * mappings to the specified range of pages must be removed before this 1171 * function is called. 1172 * 1173 * Note: certain anonymous maps, such as MAP_NOSYNC maps, 1174 * may start out with a NULL object. 1175 */ 1176 boolean_t 1177 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size, 1178 boolean_t syncio, boolean_t invalidate) 1179 { 1180 vm_object_t backing_object; 1181 struct vnode *vp; 1182 struct mount *mp; 1183 int error, flags, fsync_after; 1184 boolean_t res; 1185 1186 if (object == NULL) 1187 return (TRUE); 1188 res = TRUE; 1189 error = 0; 1190 VM_OBJECT_WLOCK(object); 1191 while ((backing_object = object->backing_object) != NULL) { 1192 VM_OBJECT_WLOCK(backing_object); 1193 offset += object->backing_object_offset; 1194 VM_OBJECT_WUNLOCK(object); 1195 object = backing_object; 1196 if (object->size < OFF_TO_IDX(offset + size)) 1197 size = IDX_TO_OFF(object->size) - offset; 1198 } 1199 /* 1200 * Flush pages if writing is allowed, invalidate them 1201 * if invalidation requested. Pages undergoing I/O 1202 * will be ignored by vm_object_page_remove(). 1203 * 1204 * We cannot lock the vnode and then wait for paging 1205 * to complete without deadlocking against vm_fault. 1206 * Instead we simply call vm_object_page_remove() and 1207 * allow it to block internally on a page-by-page 1208 * basis when it encounters pages undergoing async 1209 * I/O. 1210 */ 1211 if (object->type == OBJT_VNODE && 1212 vm_object_mightbedirty(object) != 0 && 1213 ((vp = object->handle)->v_vflag & VV_NOSYNC) == 0) { 1214 VM_OBJECT_WUNLOCK(object); 1215 (void)vn_start_write(vp, &mp, V_WAIT); 1216 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1217 if (syncio && !invalidate && offset == 0 && 1218 atop(size) == object->size) { 1219 /* 1220 * If syncing the whole mapping of the file, 1221 * it is faster to schedule all the writes in 1222 * async mode, also allowing the clustering, 1223 * and then wait for i/o to complete. 1224 */ 1225 flags = 0; 1226 fsync_after = TRUE; 1227 } else { 1228 flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 1229 flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0; 1230 fsync_after = FALSE; 1231 } 1232 VM_OBJECT_WLOCK(object); 1233 res = vm_object_page_clean(object, offset, offset + size, 1234 flags); 1235 VM_OBJECT_WUNLOCK(object); 1236 if (fsync_after) { 1237 for (;;) { 1238 error = VOP_FSYNC(vp, MNT_WAIT, curthread); 1239 if (error != ERELOOKUP) 1240 break; 1241 1242 /* 1243 * Allow SU/bufdaemon to handle more 1244 * dependencies in the meantime. 1245 */ 1246 VOP_UNLOCK(vp); 1247 vn_finished_write(mp); 1248 1249 (void)vn_start_write(vp, &mp, V_WAIT); 1250 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1251 } 1252 } 1253 VOP_UNLOCK(vp); 1254 vn_finished_write(mp); 1255 if (error != 0) 1256 res = FALSE; 1257 VM_OBJECT_WLOCK(object); 1258 } 1259 if ((object->type == OBJT_VNODE || 1260 object->type == OBJT_DEVICE) && invalidate) { 1261 if (object->type == OBJT_DEVICE) 1262 /* 1263 * The option OBJPR_NOTMAPPED must be passed here 1264 * because vm_object_page_remove() cannot remove 1265 * unmanaged mappings. 1266 */ 1267 flags = OBJPR_NOTMAPPED; 1268 else if (old_msync) 1269 flags = 0; 1270 else 1271 flags = OBJPR_CLEANONLY; 1272 vm_object_page_remove(object, OFF_TO_IDX(offset), 1273 OFF_TO_IDX(offset + size + PAGE_MASK), flags); 1274 } 1275 VM_OBJECT_WUNLOCK(object); 1276 return (res); 1277 } 1278 1279 /* 1280 * Determine whether the given advice can be applied to the object. Advice is 1281 * not applied to unmanaged pages since they never belong to page queues, and 1282 * since MADV_FREE is destructive, it can apply only to anonymous pages that 1283 * have been mapped at most once. 1284 */ 1285 static bool 1286 vm_object_advice_applies(vm_object_t object, int advice) 1287 { 1288 1289 if ((object->flags & OBJ_UNMANAGED) != 0) 1290 return (false); 1291 if (advice != MADV_FREE) 1292 return (true); 1293 return ((object->flags & (OBJ_ONEMAPPING | OBJ_ANON)) == 1294 (OBJ_ONEMAPPING | OBJ_ANON)); 1295 } 1296 1297 static void 1298 vm_object_madvise_freespace(vm_object_t object, int advice, vm_pindex_t pindex, 1299 vm_size_t size) 1300 { 1301 1302 if (advice == MADV_FREE) 1303 vm_pager_freespace(object, pindex, size); 1304 } 1305 1306 /* 1307 * vm_object_madvise: 1308 * 1309 * Implements the madvise function at the object/page level. 1310 * 1311 * MADV_WILLNEED (any object) 1312 * 1313 * Activate the specified pages if they are resident. 1314 * 1315 * MADV_DONTNEED (any object) 1316 * 1317 * Deactivate the specified pages if they are resident. 1318 * 1319 * MADV_FREE (OBJT_SWAP objects, OBJ_ONEMAPPING only) 1320 * 1321 * Deactivate and clean the specified pages if they are 1322 * resident. This permits the process to reuse the pages 1323 * without faulting or the kernel to reclaim the pages 1324 * without I/O. 1325 */ 1326 void 1327 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end, 1328 int advice) 1329 { 1330 struct pctrie_iter pages; 1331 vm_pindex_t tpindex; 1332 vm_object_t backing_object, tobject; 1333 vm_page_t m, tm; 1334 1335 if (object == NULL) 1336 return; 1337 1338 vm_page_iter_init(&pages, object); 1339 relookup: 1340 VM_OBJECT_WLOCK(object); 1341 if (!vm_object_advice_applies(object, advice)) { 1342 VM_OBJECT_WUNLOCK(object); 1343 return; 1344 } 1345 for (m = vm_radix_iter_lookup_ge(&pages, pindex); pindex < end; 1346 pindex++) { 1347 tobject = object; 1348 1349 /* 1350 * If the next page isn't resident in the top-level object, we 1351 * need to search the shadow chain. When applying MADV_FREE, we 1352 * take care to release any swap space used to store 1353 * non-resident pages. 1354 */ 1355 if (m == NULL || pindex < m->pindex) { 1356 /* 1357 * Optimize a common case: if the top-level object has 1358 * no backing object, we can skip over the non-resident 1359 * range in constant time. 1360 */ 1361 if (object->backing_object == NULL) { 1362 tpindex = (m != NULL && m->pindex < end) ? 1363 m->pindex : end; 1364 vm_object_madvise_freespace(object, advice, 1365 pindex, tpindex - pindex); 1366 if ((pindex = tpindex) == end) 1367 break; 1368 goto next_page; 1369 } 1370 1371 tpindex = pindex; 1372 do { 1373 vm_object_madvise_freespace(tobject, advice, 1374 tpindex, 1); 1375 /* 1376 * Prepare to search the next object in the 1377 * chain. 1378 */ 1379 backing_object = tobject->backing_object; 1380 if (backing_object == NULL) 1381 goto next_pindex; 1382 VM_OBJECT_WLOCK(backing_object); 1383 tpindex += 1384 OFF_TO_IDX(tobject->backing_object_offset); 1385 if (tobject != object) 1386 VM_OBJECT_WUNLOCK(tobject); 1387 tobject = backing_object; 1388 if (!vm_object_advice_applies(tobject, advice)) 1389 goto next_pindex; 1390 } while ((tm = vm_page_lookup(tobject, tpindex)) == 1391 NULL); 1392 } else { 1393 next_page: 1394 tm = m; 1395 m = vm_radix_iter_step(&pages); 1396 } 1397 1398 /* 1399 * If the page is not in a normal state, skip it. The page 1400 * can not be invalidated while the object lock is held. 1401 */ 1402 if (!vm_page_all_valid(tm) || vm_page_wired(tm)) 1403 goto next_pindex; 1404 KASSERT((tm->flags & PG_FICTITIOUS) == 0, 1405 ("vm_object_madvise: page %p is fictitious", tm)); 1406 KASSERT((tm->oflags & VPO_UNMANAGED) == 0, 1407 ("vm_object_madvise: page %p is not managed", tm)); 1408 if (vm_page_tryxbusy(tm) == 0) { 1409 if (object != tobject) 1410 VM_OBJECT_WUNLOCK(object); 1411 if (advice == MADV_WILLNEED) { 1412 /* 1413 * Reference the page before unlocking and 1414 * sleeping so that the page daemon is less 1415 * likely to reclaim it. 1416 */ 1417 vm_page_aflag_set(tm, PGA_REFERENCED); 1418 } 1419 if (!vm_page_busy_sleep(tm, "madvpo", 0)) 1420 VM_OBJECT_WUNLOCK(tobject); 1421 pctrie_iter_reset(&pages); 1422 goto relookup; 1423 } 1424 vm_page_advise(tm, advice); 1425 vm_page_xunbusy(tm); 1426 vm_object_madvise_freespace(tobject, advice, tm->pindex, 1); 1427 next_pindex: 1428 if (tobject != object) 1429 VM_OBJECT_WUNLOCK(tobject); 1430 } 1431 VM_OBJECT_WUNLOCK(object); 1432 } 1433 1434 /* 1435 * vm_object_shadow: 1436 * 1437 * Create a new object which is backed by the 1438 * specified existing object range. The source 1439 * object reference is deallocated. 1440 * 1441 * The new object and offset into that object 1442 * are returned in the source parameters. 1443 */ 1444 void 1445 vm_object_shadow(vm_object_t *object, vm_ooffset_t *offset, vm_size_t length, 1446 struct ucred *cred, bool shared) 1447 { 1448 vm_object_t source; 1449 vm_object_t result; 1450 1451 source = *object; 1452 1453 /* 1454 * Don't create the new object if the old object isn't shared. 1455 * 1456 * If we hold the only reference we can guarantee that it won't 1457 * increase while we have the map locked. Otherwise the race is 1458 * harmless and we will end up with an extra shadow object that 1459 * will be collapsed later. 1460 */ 1461 if (source != NULL && source->ref_count == 1 && 1462 (source->flags & OBJ_ANON) != 0) 1463 return; 1464 1465 /* 1466 * Allocate a new object with the given length. 1467 */ 1468 result = vm_object_allocate_anon(atop(length), source, cred, length); 1469 1470 /* 1471 * Store the offset into the source object, and fix up the offset into 1472 * the new object. 1473 */ 1474 result->backing_object_offset = *offset; 1475 1476 if (shared || source != NULL) { 1477 VM_OBJECT_WLOCK(result); 1478 1479 /* 1480 * The new object shadows the source object, adding a 1481 * reference to it. Our caller changes his reference 1482 * to point to the new object, removing a reference to 1483 * the source object. Net result: no change of 1484 * reference count, unless the caller needs to add one 1485 * more reference due to forking a shared map entry. 1486 */ 1487 if (shared) { 1488 vm_object_reference_locked(result); 1489 vm_object_clear_flag(result, OBJ_ONEMAPPING); 1490 } 1491 1492 /* 1493 * Try to optimize the result object's page color when 1494 * shadowing in order to maintain page coloring 1495 * consistency in the combined shadowed object. 1496 */ 1497 if (source != NULL) { 1498 vm_object_backing_insert(result, source); 1499 result->domain = source->domain; 1500 #if VM_NRESERVLEVEL > 0 1501 vm_object_set_flag(result, 1502 (source->flags & OBJ_COLORED)); 1503 result->pg_color = (source->pg_color + 1504 OFF_TO_IDX(*offset)) & ((1 << (VM_NFREEORDER - 1505 1)) - 1); 1506 #endif 1507 } 1508 VM_OBJECT_WUNLOCK(result); 1509 } 1510 1511 /* 1512 * Return the new things 1513 */ 1514 *offset = 0; 1515 *object = result; 1516 } 1517 1518 /* 1519 * vm_object_split: 1520 * 1521 * Split the pages in a map entry into a new object. This affords 1522 * easier removal of unused pages, and keeps object inheritance from 1523 * being a negative impact on memory usage. 1524 */ 1525 void 1526 vm_object_split(vm_map_entry_t entry) 1527 { 1528 struct pctrie_iter pages; 1529 vm_page_t m; 1530 vm_object_t orig_object, new_object, backing_object; 1531 vm_pindex_t offidxstart; 1532 vm_size_t size; 1533 1534 orig_object = entry->object.vm_object; 1535 KASSERT((orig_object->flags & OBJ_ONEMAPPING) != 0, 1536 ("vm_object_split: Splitting object with multiple mappings.")); 1537 if ((orig_object->flags & OBJ_ANON) == 0) 1538 return; 1539 if (orig_object->ref_count <= 1) 1540 return; 1541 VM_OBJECT_WUNLOCK(orig_object); 1542 1543 offidxstart = OFF_TO_IDX(entry->offset); 1544 size = atop(entry->end - entry->start); 1545 1546 new_object = vm_object_allocate_anon(size, orig_object, 1547 orig_object->cred, ptoa(size)); 1548 1549 /* 1550 * We must wait for the orig_object to complete any in-progress 1551 * collapse so that the swap blocks are stable below. The 1552 * additional reference on backing_object by new object will 1553 * prevent further collapse operations until split completes. 1554 */ 1555 VM_OBJECT_WLOCK(orig_object); 1556 vm_object_collapse_wait(orig_object); 1557 1558 /* 1559 * At this point, the new object is still private, so the order in 1560 * which the original and new objects are locked does not matter. 1561 */ 1562 VM_OBJECT_WLOCK(new_object); 1563 new_object->domain = orig_object->domain; 1564 backing_object = orig_object->backing_object; 1565 if (backing_object != NULL) { 1566 vm_object_backing_insert_ref(new_object, backing_object); 1567 new_object->backing_object_offset = 1568 orig_object->backing_object_offset + entry->offset; 1569 } 1570 if (orig_object->cred != NULL) { 1571 crhold(orig_object->cred); 1572 KASSERT(orig_object->charge >= ptoa(size), 1573 ("orig_object->charge < 0")); 1574 orig_object->charge -= ptoa(size); 1575 } 1576 1577 /* 1578 * Mark the split operation so that swap_pager_getpages() knows 1579 * that the object is in transition. 1580 */ 1581 vm_object_set_flag(orig_object, OBJ_SPLIT); 1582 vm_page_iter_limit_init(&pages, orig_object, offidxstart + size); 1583 retry: 1584 KASSERT(pctrie_iter_is_reset(&pages), 1585 ("%s: pctrie_iter not reset for retry", __func__)); 1586 for (m = vm_radix_iter_lookup_ge(&pages, offidxstart); m != NULL; 1587 m = vm_radix_iter_step(&pages)) { 1588 /* 1589 * We must wait for pending I/O to complete before we can 1590 * rename the page. 1591 * 1592 * We do not have to VM_PROT_NONE the page as mappings should 1593 * not be changed by this operation. 1594 */ 1595 if (vm_page_tryxbusy(m) == 0) { 1596 VM_OBJECT_WUNLOCK(new_object); 1597 if (vm_page_busy_sleep(m, "spltwt", 0)) 1598 VM_OBJECT_WLOCK(orig_object); 1599 pctrie_iter_reset(&pages); 1600 VM_OBJECT_WLOCK(new_object); 1601 goto retry; 1602 } 1603 1604 /* 1605 * If the page was left invalid, it was likely placed there by 1606 * an incomplete fault. Just remove and ignore. 1607 * 1608 * One other possibility is that the map entry is wired, in 1609 * which case we must hang on to the page to avoid leaking it, 1610 * as the map entry owns the wiring. This case can arise if the 1611 * backing object is truncated by the pager. 1612 */ 1613 if (vm_page_none_valid(m) && entry->wired_count == 0) { 1614 if (vm_page_iter_remove(&pages, m)) 1615 vm_page_free(m); 1616 continue; 1617 } 1618 1619 /* vm_page_iter_rename() will dirty the page if it is valid. */ 1620 if (!vm_page_iter_rename(&pages, m, new_object, m->pindex - 1621 offidxstart)) { 1622 vm_page_xunbusy(m); 1623 VM_OBJECT_WUNLOCK(new_object); 1624 VM_OBJECT_WUNLOCK(orig_object); 1625 vm_radix_wait(); 1626 pctrie_iter_reset(&pages); 1627 VM_OBJECT_WLOCK(orig_object); 1628 VM_OBJECT_WLOCK(new_object); 1629 goto retry; 1630 } 1631 1632 #if VM_NRESERVLEVEL > 0 1633 /* 1634 * If some of the reservation's allocated pages remain with 1635 * the original object, then transferring the reservation to 1636 * the new object is neither particularly beneficial nor 1637 * particularly harmful as compared to leaving the reservation 1638 * with the original object. If, however, all of the 1639 * reservation's allocated pages are transferred to the new 1640 * object, then transferring the reservation is typically 1641 * beneficial. Determining which of these two cases applies 1642 * would be more costly than unconditionally renaming the 1643 * reservation. 1644 */ 1645 vm_reserv_rename(m, new_object, orig_object, offidxstart); 1646 #endif 1647 } 1648 1649 /* 1650 * swap_pager_copy() can sleep, in which case the orig_object's 1651 * and new_object's locks are released and reacquired. 1652 */ 1653 swap_pager_copy(orig_object, new_object, offidxstart, 0); 1654 vm_page_iter_init(&pages, new_object); 1655 VM_RADIX_FOREACH(m, &pages) 1656 vm_page_xunbusy(m); 1657 1658 vm_object_clear_flag(orig_object, OBJ_SPLIT); 1659 VM_OBJECT_WUNLOCK(orig_object); 1660 VM_OBJECT_WUNLOCK(new_object); 1661 entry->object.vm_object = new_object; 1662 entry->offset = 0LL; 1663 vm_object_deallocate(orig_object); 1664 VM_OBJECT_WLOCK(new_object); 1665 } 1666 1667 static vm_page_t 1668 vm_object_collapse_scan_wait(struct pctrie_iter *pages, vm_object_t object, 1669 vm_page_t p) 1670 { 1671 vm_object_t backing_object; 1672 1673 VM_OBJECT_ASSERT_WLOCKED(object); 1674 backing_object = object->backing_object; 1675 VM_OBJECT_ASSERT_WLOCKED(backing_object); 1676 1677 KASSERT(p == NULL || p->object == object || p->object == backing_object, 1678 ("invalid ownership %p %p %p", p, object, backing_object)); 1679 /* The page is only NULL when rename fails. */ 1680 if (p == NULL) { 1681 VM_OBJECT_WUNLOCK(object); 1682 VM_OBJECT_WUNLOCK(backing_object); 1683 vm_radix_wait(); 1684 VM_OBJECT_WLOCK(object); 1685 } else if (p->object == object) { 1686 VM_OBJECT_WUNLOCK(backing_object); 1687 if (vm_page_busy_sleep(p, "vmocol", 0)) 1688 VM_OBJECT_WLOCK(object); 1689 } else { 1690 VM_OBJECT_WUNLOCK(object); 1691 if (!vm_page_busy_sleep(p, "vmocol", 0)) 1692 VM_OBJECT_WUNLOCK(backing_object); 1693 VM_OBJECT_WLOCK(object); 1694 } 1695 VM_OBJECT_WLOCK(backing_object); 1696 vm_page_iter_init(pages, backing_object); 1697 return (vm_radix_iter_lookup_ge(pages, 0)); 1698 } 1699 1700 static void 1701 vm_object_collapse_scan(vm_object_t object) 1702 { 1703 struct pctrie_iter pages; 1704 vm_object_t backing_object; 1705 vm_page_t next, p, pp; 1706 vm_pindex_t backing_offset_index, new_pindex; 1707 1708 VM_OBJECT_ASSERT_WLOCKED(object); 1709 VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 1710 1711 backing_object = object->backing_object; 1712 backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 1713 1714 /* 1715 * Our scan 1716 */ 1717 vm_page_iter_init(&pages, backing_object); 1718 for (p = vm_radix_iter_lookup_ge(&pages, 0); p != NULL; p = next) { 1719 /* 1720 * Check for busy page 1721 */ 1722 if (vm_page_tryxbusy(p) == 0) { 1723 next = vm_object_collapse_scan_wait(&pages, object, p); 1724 continue; 1725 } 1726 1727 KASSERT(object->backing_object == backing_object, 1728 ("vm_object_collapse_scan: backing object mismatch %p != %p", 1729 object->backing_object, backing_object)); 1730 KASSERT(p->object == backing_object, 1731 ("vm_object_collapse_scan: object mismatch %p != %p", 1732 p->object, backing_object)); 1733 1734 if (p->pindex < backing_offset_index || object->size <= 1735 (new_pindex = p->pindex - backing_offset_index)) { 1736 vm_pager_freespace(backing_object, p->pindex, 1); 1737 1738 KASSERT(!pmap_page_is_mapped(p), 1739 ("freeing mapped page %p", p)); 1740 if (vm_page_iter_remove(&pages, p)) 1741 vm_page_free(p); 1742 next = vm_radix_iter_step(&pages); 1743 continue; 1744 } 1745 1746 if (!vm_page_all_valid(p)) { 1747 KASSERT(!pmap_page_is_mapped(p), 1748 ("freeing mapped page %p", p)); 1749 if (vm_page_iter_remove(&pages, p)) 1750 vm_page_free(p); 1751 next = vm_radix_iter_step(&pages); 1752 continue; 1753 } 1754 1755 pp = vm_page_lookup(object, new_pindex); 1756 if (pp != NULL && vm_page_tryxbusy(pp) == 0) { 1757 vm_page_xunbusy(p); 1758 /* 1759 * The page in the parent is busy and possibly not 1760 * (yet) valid. Until its state is finalized by the 1761 * busy bit owner, we can't tell whether it shadows the 1762 * original page. 1763 */ 1764 next = vm_object_collapse_scan_wait(&pages, object, pp); 1765 continue; 1766 } 1767 1768 if (pp != NULL && vm_page_none_valid(pp)) { 1769 /* 1770 * The page was invalid in the parent. Likely placed 1771 * there by an incomplete fault. Just remove and 1772 * ignore. p can replace it. 1773 */ 1774 if (vm_page_remove(pp)) 1775 vm_page_free(pp); 1776 pp = NULL; 1777 } 1778 1779 if (pp != NULL || vm_pager_has_page(object, new_pindex, NULL, 1780 NULL)) { 1781 /* 1782 * The page already exists in the parent OR swap exists 1783 * for this location in the parent. Leave the parent's 1784 * page alone. Destroy the original page from the 1785 * backing object. 1786 */ 1787 vm_pager_freespace(backing_object, p->pindex, 1); 1788 KASSERT(!pmap_page_is_mapped(p), 1789 ("freeing mapped page %p", p)); 1790 if (pp != NULL) 1791 vm_page_xunbusy(pp); 1792 if (vm_page_iter_remove(&pages, p)) 1793 vm_page_free(p); 1794 next = vm_radix_iter_step(&pages); 1795 continue; 1796 } 1797 1798 /* 1799 * Page does not exist in parent, rename the page from the 1800 * backing object to the main object. 1801 * 1802 * If the page was mapped to a process, it can remain mapped 1803 * through the rename. vm_page_iter_rename() will dirty the 1804 * page. 1805 */ 1806 if (!vm_page_iter_rename(&pages, p, object, new_pindex)) { 1807 vm_page_xunbusy(p); 1808 next = vm_object_collapse_scan_wait(&pages, object, 1809 NULL); 1810 continue; 1811 } 1812 1813 /* Use the old pindex to free the right page. */ 1814 vm_pager_freespace(backing_object, new_pindex + 1815 backing_offset_index, 1); 1816 1817 #if VM_NRESERVLEVEL > 0 1818 /* 1819 * Rename the reservation. 1820 */ 1821 vm_reserv_rename(p, object, backing_object, 1822 backing_offset_index); 1823 #endif 1824 vm_page_xunbusy(p); 1825 next = vm_radix_iter_step(&pages); 1826 } 1827 return; 1828 } 1829 1830 /* 1831 * vm_object_collapse: 1832 * 1833 * Collapse an object with the object backing it. 1834 * Pages in the backing object are moved into the 1835 * parent, and the backing object is deallocated. 1836 */ 1837 void 1838 vm_object_collapse(vm_object_t object) 1839 { 1840 vm_object_t backing_object, new_backing_object; 1841 1842 VM_OBJECT_ASSERT_WLOCKED(object); 1843 1844 while (TRUE) { 1845 KASSERT((object->flags & (OBJ_DEAD | OBJ_ANON)) == OBJ_ANON, 1846 ("collapsing invalid object")); 1847 1848 /* 1849 * Wait for the backing_object to finish any pending 1850 * collapse so that the caller sees the shortest possible 1851 * shadow chain. 1852 */ 1853 backing_object = vm_object_backing_collapse_wait(object); 1854 if (backing_object == NULL) 1855 return; 1856 1857 KASSERT(object->ref_count > 0 && 1858 object->ref_count > atomic_load_int(&object->shadow_count), 1859 ("collapse with invalid ref %d or shadow %d count.", 1860 object->ref_count, atomic_load_int(&object->shadow_count))); 1861 KASSERT((backing_object->flags & 1862 (OBJ_COLLAPSING | OBJ_DEAD)) == 0, 1863 ("vm_object_collapse: Backing object already collapsing.")); 1864 KASSERT((object->flags & (OBJ_COLLAPSING | OBJ_DEAD)) == 0, 1865 ("vm_object_collapse: object is already collapsing.")); 1866 1867 /* 1868 * We know that we can either collapse the backing object if 1869 * the parent is the only reference to it, or (perhaps) have 1870 * the parent bypass the object if the parent happens to shadow 1871 * all the resident pages in the entire backing object. 1872 */ 1873 if (backing_object->ref_count == 1) { 1874 KASSERT(atomic_load_int(&backing_object->shadow_count) 1875 == 1, 1876 ("vm_object_collapse: shadow_count: %d", 1877 atomic_load_int(&backing_object->shadow_count))); 1878 vm_object_pip_add(object, 1); 1879 vm_object_set_flag(object, OBJ_COLLAPSING); 1880 vm_object_pip_add(backing_object, 1); 1881 vm_object_set_flag(backing_object, OBJ_DEAD); 1882 1883 /* 1884 * If there is exactly one reference to the backing 1885 * object, we can collapse it into the parent. 1886 */ 1887 vm_object_collapse_scan(object); 1888 1889 /* 1890 * Move the pager from backing_object to object. 1891 * 1892 * swap_pager_copy() can sleep, in which case the 1893 * backing_object's and object's locks are released and 1894 * reacquired. 1895 */ 1896 swap_pager_copy(backing_object, object, 1897 OFF_TO_IDX(object->backing_object_offset), TRUE); 1898 1899 /* 1900 * Object now shadows whatever backing_object did. 1901 */ 1902 vm_object_clear_flag(object, OBJ_COLLAPSING); 1903 vm_object_backing_transfer(object, backing_object); 1904 object->backing_object_offset += 1905 backing_object->backing_object_offset; 1906 VM_OBJECT_WUNLOCK(object); 1907 vm_object_pip_wakeup(object); 1908 1909 /* 1910 * Discard backing_object. 1911 * 1912 * Since the backing object has no pages, no pager left, 1913 * and no object references within it, all that is 1914 * necessary is to dispose of it. 1915 */ 1916 KASSERT(backing_object->ref_count == 1, ( 1917 "backing_object %p was somehow re-referenced during collapse!", 1918 backing_object)); 1919 vm_object_pip_wakeup(backing_object); 1920 (void)refcount_release(&backing_object->ref_count); 1921 umtx_shm_object_terminated(backing_object); 1922 vm_object_terminate(backing_object); 1923 counter_u64_add(object_collapses, 1); 1924 VM_OBJECT_WLOCK(object); 1925 } else { 1926 /* 1927 * If we do not entirely shadow the backing object, 1928 * there is nothing we can do so we give up. 1929 * 1930 * The object lock and backing_object lock must not 1931 * be dropped during this sequence. 1932 */ 1933 if (!swap_pager_scan_all_shadowed(object)) { 1934 VM_OBJECT_WUNLOCK(backing_object); 1935 break; 1936 } 1937 1938 /* 1939 * Make the parent shadow the next object in the 1940 * chain. Deallocating backing_object will not remove 1941 * it, since its reference count is at least 2. 1942 */ 1943 vm_object_backing_remove_locked(object); 1944 new_backing_object = backing_object->backing_object; 1945 if (new_backing_object != NULL) { 1946 vm_object_backing_insert_ref(object, 1947 new_backing_object); 1948 object->backing_object_offset += 1949 backing_object->backing_object_offset; 1950 } 1951 1952 /* 1953 * Drop the reference count on backing_object. Since 1954 * its ref_count was at least 2, it will not vanish. 1955 */ 1956 (void)refcount_release(&backing_object->ref_count); 1957 KASSERT(backing_object->ref_count >= 1, ( 1958 "backing_object %p was somehow dereferenced during collapse!", 1959 backing_object)); 1960 VM_OBJECT_WUNLOCK(backing_object); 1961 counter_u64_add(object_bypasses, 1); 1962 } 1963 1964 /* 1965 * Try again with this object's new backing object. 1966 */ 1967 } 1968 } 1969 1970 /* 1971 * vm_object_page_remove: 1972 * 1973 * For the given object, either frees or invalidates each of the 1974 * specified pages. In general, a page is freed. However, if a page is 1975 * wired for any reason other than the existence of a managed, wired 1976 * mapping, then it may be invalidated but not removed from the object. 1977 * Pages are specified by the given range ["start", "end") and the option 1978 * OBJPR_CLEANONLY. As a special case, if "end" is zero, then the range 1979 * extends from "start" to the end of the object. If the option 1980 * OBJPR_CLEANONLY is specified, then only the non-dirty pages within the 1981 * specified range are affected. If the option OBJPR_NOTMAPPED is 1982 * specified, then the pages within the specified range must have no 1983 * mappings. Otherwise, if this option is not specified, any mappings to 1984 * the specified pages are removed before the pages are freed or 1985 * invalidated. 1986 * 1987 * In general, this operation should only be performed on objects that 1988 * contain managed pages. There are, however, two exceptions. First, it 1989 * is performed on the kernel and kmem objects by vm_map_entry_delete(). 1990 * Second, it is used by msync(..., MS_INVALIDATE) to invalidate device- 1991 * backed pages. In both of these cases, the option OBJPR_CLEANONLY must 1992 * not be specified and the option OBJPR_NOTMAPPED must be specified. 1993 * 1994 * The object must be locked. 1995 */ 1996 void 1997 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 1998 int options) 1999 { 2000 struct pctrie_iter pages; 2001 vm_page_t p; 2002 2003 VM_OBJECT_ASSERT_WLOCKED(object); 2004 KASSERT((object->flags & OBJ_UNMANAGED) == 0 || 2005 (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED, 2006 ("vm_object_page_remove: illegal options for object %p", object)); 2007 if (object->resident_page_count == 0) 2008 return; 2009 vm_object_pip_add(object, 1); 2010 vm_page_iter_limit_init(&pages, object, end); 2011 again: 2012 KASSERT(pctrie_iter_is_reset(&pages), 2013 ("%s: pctrie_iter not reset for retry", __func__)); 2014 for (p = vm_radix_iter_lookup_ge(&pages, start); p != NULL; 2015 p = vm_radix_iter_step(&pages)) { 2016 /* 2017 * Skip invalid pages if asked to do so. Try to avoid acquiring 2018 * the busy lock, as some consumers rely on this to avoid 2019 * deadlocks. 2020 * 2021 * A thread may concurrently transition the page from invalid to 2022 * valid using only the busy lock, so the result of this check 2023 * is immediately stale. It is up to consumers to handle this, 2024 * for instance by ensuring that all invalid->valid transitions 2025 * happen with a mutex held, as may be possible for a 2026 * filesystem. 2027 */ 2028 if ((options & OBJPR_VALIDONLY) != 0 && vm_page_none_valid(p)) 2029 continue; 2030 2031 /* 2032 * If the page is wired for any reason besides the existence 2033 * of managed, wired mappings, then it cannot be freed. For 2034 * example, fictitious pages, which represent device memory, 2035 * are inherently wired and cannot be freed. They can, 2036 * however, be invalidated if the option OBJPR_CLEANONLY is 2037 * not specified. 2038 */ 2039 if (vm_page_tryxbusy(p) == 0) { 2040 if (vm_page_busy_sleep(p, "vmopar", 0)) 2041 VM_OBJECT_WLOCK(object); 2042 pctrie_iter_reset(&pages); 2043 goto again; 2044 } 2045 if ((options & OBJPR_VALIDONLY) != 0 && vm_page_none_valid(p)) { 2046 vm_page_xunbusy(p); 2047 continue; 2048 } 2049 if (vm_page_wired(p)) { 2050 wired: 2051 if ((options & OBJPR_NOTMAPPED) == 0 && 2052 object->ref_count != 0) 2053 pmap_remove_all(p); 2054 if ((options & OBJPR_CLEANONLY) == 0) { 2055 vm_page_invalid(p); 2056 vm_page_undirty(p); 2057 } 2058 vm_page_xunbusy(p); 2059 continue; 2060 } 2061 KASSERT((p->flags & PG_FICTITIOUS) == 0, 2062 ("vm_object_page_remove: page %p is fictitious", p)); 2063 if ((options & OBJPR_CLEANONLY) != 0 && 2064 !vm_page_none_valid(p)) { 2065 if ((options & OBJPR_NOTMAPPED) == 0 && 2066 object->ref_count != 0 && 2067 !vm_page_try_remove_write(p)) 2068 goto wired; 2069 if (p->dirty != 0) { 2070 vm_page_xunbusy(p); 2071 continue; 2072 } 2073 } 2074 if ((options & OBJPR_NOTMAPPED) == 0 && 2075 object->ref_count != 0 && !vm_page_try_remove_all(p)) 2076 goto wired; 2077 vm_page_iter_free(&pages, p); 2078 } 2079 vm_object_pip_wakeup(object); 2080 2081 vm_pager_freespace(object, start, (end == 0 ? object->size : end) - 2082 start); 2083 } 2084 2085 /* 2086 * vm_object_page_noreuse: 2087 * 2088 * For the given object, attempt to move the specified pages to 2089 * the head of the inactive queue. This bypasses regular LRU 2090 * operation and allows the pages to be reused quickly under memory 2091 * pressure. If a page is wired for any reason, then it will not 2092 * be queued. Pages are specified by the range ["start", "end"). 2093 * As a special case, if "end" is zero, then the range extends from 2094 * "start" to the end of the object. 2095 * 2096 * This operation should only be performed on objects that 2097 * contain non-fictitious, managed pages. 2098 * 2099 * The object must be locked. 2100 */ 2101 void 2102 vm_object_page_noreuse(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2103 { 2104 struct pctrie_iter pages; 2105 vm_page_t p; 2106 2107 VM_OBJECT_ASSERT_LOCKED(object); 2108 KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0, 2109 ("vm_object_page_noreuse: illegal object %p", object)); 2110 if (object->resident_page_count == 0) 2111 return; 2112 2113 vm_page_iter_limit_init(&pages, object, end); 2114 VM_RADIX_FOREACH_FROM(p, &pages, start) 2115 vm_page_deactivate_noreuse(p); 2116 } 2117 2118 /* 2119 * Populate the specified range of the object with valid pages. Returns 2120 * TRUE if the range is successfully populated and FALSE otherwise. 2121 * 2122 * Note: This function should be optimized to pass a larger array of 2123 * pages to vm_pager_get_pages() before it is applied to a non- 2124 * OBJT_DEVICE object. 2125 * 2126 * The object must be locked. 2127 */ 2128 boolean_t 2129 vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2130 { 2131 struct pctrie_iter pages; 2132 vm_page_t m; 2133 vm_pindex_t pindex; 2134 int rv; 2135 2136 vm_page_iter_init(&pages, object); 2137 VM_OBJECT_ASSERT_WLOCKED(object); 2138 for (pindex = start; pindex < end; pindex++) { 2139 rv = vm_page_grab_valid_iter(&m, object, pindex, 2140 VM_ALLOC_NORMAL, &pages); 2141 if (rv != VM_PAGER_OK) 2142 break; 2143 2144 /* 2145 * Keep "m" busy because a subsequent iteration may unlock 2146 * the object. 2147 */ 2148 } 2149 if (pindex > start) { 2150 pages.limit = pindex; 2151 VM_RADIX_FORALL_FROM(m, &pages, start) 2152 vm_page_xunbusy(m); 2153 } 2154 return (pindex == end); 2155 } 2156 2157 /* 2158 * Routine: vm_object_coalesce 2159 * Function: Coalesces two objects backing up adjoining 2160 * regions of memory into a single object. 2161 * 2162 * returns TRUE if objects were combined. 2163 * 2164 * NOTE: Only works at the moment if the second object is NULL - 2165 * if it's not, which object do we lock first? 2166 * 2167 * Parameters: 2168 * prev_object First object to coalesce 2169 * prev_offset Offset into prev_object 2170 * prev_size Size of reference to prev_object 2171 * next_size Size of reference to the second object 2172 * reserved Indicator that extension region has 2173 * swap accounted for 2174 * 2175 * Conditions: 2176 * The object must *not* be locked. 2177 */ 2178 boolean_t 2179 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 2180 vm_size_t prev_size, vm_size_t next_size, boolean_t reserved) 2181 { 2182 vm_pindex_t next_pindex; 2183 2184 if (prev_object == NULL) 2185 return (TRUE); 2186 if ((prev_object->flags & OBJ_ANON) == 0) 2187 return (FALSE); 2188 2189 VM_OBJECT_WLOCK(prev_object); 2190 /* 2191 * Try to collapse the object first. 2192 */ 2193 vm_object_collapse(prev_object); 2194 2195 /* 2196 * Can't coalesce if: . more than one reference . paged out . shadows 2197 * another object . has a copy elsewhere (any of which mean that the 2198 * pages not mapped to prev_entry may be in use anyway) 2199 */ 2200 if (prev_object->backing_object != NULL) { 2201 VM_OBJECT_WUNLOCK(prev_object); 2202 return (FALSE); 2203 } 2204 2205 prev_size >>= PAGE_SHIFT; 2206 next_size >>= PAGE_SHIFT; 2207 next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 2208 2209 if (prev_object->ref_count > 1 && 2210 prev_object->size != next_pindex && 2211 (prev_object->flags & OBJ_ONEMAPPING) == 0) { 2212 VM_OBJECT_WUNLOCK(prev_object); 2213 return (FALSE); 2214 } 2215 2216 /* 2217 * Account for the charge. 2218 */ 2219 if (prev_object->cred != NULL) { 2220 /* 2221 * If prev_object was charged, then this mapping, 2222 * although not charged now, may become writable 2223 * later. Non-NULL cred in the object would prevent 2224 * swap reservation during enabling of the write 2225 * access, so reserve swap now. Failed reservation 2226 * cause allocation of the separate object for the map 2227 * entry, and swap reservation for this entry is 2228 * managed in appropriate time. 2229 */ 2230 if (!reserved && !swap_reserve_by_cred(ptoa(next_size), 2231 prev_object->cred)) { 2232 VM_OBJECT_WUNLOCK(prev_object); 2233 return (FALSE); 2234 } 2235 prev_object->charge += ptoa(next_size); 2236 } 2237 2238 /* 2239 * Remove any pages that may still be in the object from a previous 2240 * deallocation. 2241 */ 2242 if (next_pindex < prev_object->size) { 2243 vm_object_page_remove(prev_object, next_pindex, next_pindex + 2244 next_size, 0); 2245 #if 0 2246 if (prev_object->cred != NULL) { 2247 KASSERT(prev_object->charge >= 2248 ptoa(prev_object->size - next_pindex), 2249 ("object %p overcharged 1 %jx %jx", prev_object, 2250 (uintmax_t)next_pindex, (uintmax_t)next_size)); 2251 prev_object->charge -= ptoa(prev_object->size - 2252 next_pindex); 2253 } 2254 #endif 2255 } 2256 2257 /* 2258 * Extend the object if necessary. 2259 */ 2260 if (next_pindex + next_size > prev_object->size) 2261 prev_object->size = next_pindex + next_size; 2262 2263 VM_OBJECT_WUNLOCK(prev_object); 2264 return (TRUE); 2265 } 2266 2267 /* 2268 * Fill in the m_dst array with up to *rbehind optional pages before m_src[0] 2269 * and up to *rahead optional pages after m_src[count - 1]. In both cases, stop 2270 * the filling-in short on encountering a cached page, an object boundary limit, 2271 * or an allocation error. Update *rbehind and *rahead to indicate the number 2272 * of pages allocated. Copy elements of m_src into array elements from 2273 * m_dst[*rbehind] to m_dst[*rbehind + count -1]. 2274 */ 2275 void 2276 vm_object_prepare_buf_pages(vm_object_t object, vm_page_t *ma_dst, int count, 2277 int *rbehind, int *rahead, vm_page_t *ma_src) 2278 { 2279 struct pctrie_iter pages; 2280 vm_pindex_t pindex; 2281 vm_page_t m, mpred, msucc; 2282 2283 vm_page_iter_init(&pages, object); 2284 VM_OBJECT_ASSERT_LOCKED(object); 2285 if (*rbehind != 0) { 2286 m = ma_src[0]; 2287 pindex = m->pindex; 2288 mpred = vm_radix_iter_lookup_lt(&pages, pindex); 2289 *rbehind = MIN(*rbehind, 2290 pindex - (mpred != NULL ? mpred->pindex + 1 : 0)); 2291 for (int i = 0; i < *rbehind; i++) { 2292 m = vm_page_alloc_iter(object, pindex - i - 1, 2293 VM_ALLOC_NORMAL, &pages); 2294 if (m == NULL) { 2295 /* Shift the array. */ 2296 for (int j = 0; j < i; j++) 2297 ma_dst[j] = ma_dst[j + *rbehind - i]; 2298 *rbehind = i; 2299 *rahead = 0; 2300 break; 2301 } 2302 ma_dst[*rbehind - i - 1] = m; 2303 } 2304 } 2305 for (int i = 0; i < count; i++) 2306 ma_dst[*rbehind + i] = ma_src[i]; 2307 if (*rahead != 0) { 2308 m = ma_src[count - 1]; 2309 pindex = m->pindex + 1; 2310 msucc = vm_radix_iter_lookup_ge(&pages, pindex); 2311 *rahead = MIN(*rahead, 2312 (msucc != NULL ? msucc->pindex : object->size) - pindex); 2313 for (int i = 0; i < *rahead; i++) { 2314 m = vm_page_alloc_iter(object, pindex + i, 2315 VM_ALLOC_NORMAL, &pages); 2316 if (m == NULL) { 2317 *rahead = i; 2318 break; 2319 } 2320 ma_dst[*rbehind + count + i] = m; 2321 } 2322 } 2323 } 2324 2325 void 2326 vm_object_set_writeable_dirty_(vm_object_t object) 2327 { 2328 atomic_add_int(&object->generation, 1); 2329 } 2330 2331 bool 2332 vm_object_mightbedirty_(vm_object_t object) 2333 { 2334 return (object->generation != object->cleangeneration); 2335 } 2336 2337 /* 2338 * vm_object_unwire: 2339 * 2340 * For each page offset within the specified range of the given object, 2341 * find the highest-level page in the shadow chain and unwire it. A page 2342 * must exist at every page offset, and the highest-level page must be 2343 * wired. 2344 */ 2345 void 2346 vm_object_unwire(vm_object_t object, vm_ooffset_t offset, vm_size_t length, 2347 uint8_t queue) 2348 { 2349 struct pctrie_iter pages; 2350 vm_object_t tobject, t1object; 2351 vm_page_t m, tm; 2352 vm_pindex_t end_pindex, pindex, tpindex; 2353 int depth, locked_depth; 2354 2355 KASSERT((offset & PAGE_MASK) == 0, 2356 ("vm_object_unwire: offset is not page aligned")); 2357 KASSERT((length & PAGE_MASK) == 0, 2358 ("vm_object_unwire: length is not a multiple of PAGE_SIZE")); 2359 /* The wired count of a fictitious page never changes. */ 2360 if ((object->flags & OBJ_FICTITIOUS) != 0) 2361 return; 2362 pindex = OFF_TO_IDX(offset); 2363 end_pindex = pindex + atop(length); 2364 vm_page_iter_init(&pages, object); 2365 again: 2366 locked_depth = 1; 2367 VM_OBJECT_RLOCK(object); 2368 m = vm_radix_iter_lookup_ge(&pages, pindex); 2369 while (pindex < end_pindex) { 2370 if (m == NULL || pindex < m->pindex) { 2371 /* 2372 * The first object in the shadow chain doesn't 2373 * contain a page at the current index. Therefore, 2374 * the page must exist in a backing object. 2375 */ 2376 tobject = object; 2377 tpindex = pindex; 2378 depth = 0; 2379 do { 2380 tpindex += 2381 OFF_TO_IDX(tobject->backing_object_offset); 2382 tobject = tobject->backing_object; 2383 KASSERT(tobject != NULL, 2384 ("vm_object_unwire: missing page")); 2385 if ((tobject->flags & OBJ_FICTITIOUS) != 0) 2386 goto next_page; 2387 depth++; 2388 if (depth == locked_depth) { 2389 locked_depth++; 2390 VM_OBJECT_RLOCK(tobject); 2391 } 2392 } while ((tm = vm_page_lookup(tobject, tpindex)) == 2393 NULL); 2394 } else { 2395 tm = m; 2396 m = vm_radix_iter_step(&pages); 2397 } 2398 if (vm_page_trysbusy(tm) == 0) { 2399 for (tobject = object; locked_depth >= 1; 2400 locked_depth--) { 2401 t1object = tobject->backing_object; 2402 if (tm->object != tobject) 2403 VM_OBJECT_RUNLOCK(tobject); 2404 tobject = t1object; 2405 } 2406 tobject = tm->object; 2407 if (!vm_page_busy_sleep(tm, "unwbo", 2408 VM_ALLOC_IGN_SBUSY)) 2409 VM_OBJECT_RUNLOCK(tobject); 2410 pctrie_iter_reset(&pages); 2411 goto again; 2412 } 2413 vm_page_unwire(tm, queue); 2414 vm_page_sunbusy(tm); 2415 next_page: 2416 pindex++; 2417 } 2418 /* Release the accumulated object locks. */ 2419 for (tobject = object; locked_depth >= 1; locked_depth--) { 2420 t1object = tobject->backing_object; 2421 VM_OBJECT_RUNLOCK(tobject); 2422 tobject = t1object; 2423 } 2424 } 2425 2426 /* 2427 * Return the vnode for the given object, or NULL if none exists. 2428 * For tmpfs objects, the function may return NULL if there is 2429 * no vnode allocated at the time of the call. 2430 */ 2431 struct vnode * 2432 vm_object_vnode(vm_object_t object) 2433 { 2434 struct vnode *vp; 2435 2436 VM_OBJECT_ASSERT_LOCKED(object); 2437 vm_pager_getvp(object, &vp, NULL); 2438 return (vp); 2439 } 2440 2441 /* 2442 * Busy the vm object. This prevents new pages belonging to the object from 2443 * becoming busy. Existing pages persist as busy. Callers are responsible 2444 * for checking page state before proceeding. 2445 */ 2446 void 2447 vm_object_busy(vm_object_t obj) 2448 { 2449 2450 VM_OBJECT_ASSERT_LOCKED(obj); 2451 2452 blockcount_acquire(&obj->busy, 1); 2453 /* The fence is required to order loads of page busy. */ 2454 atomic_thread_fence_acq_rel(); 2455 } 2456 2457 void 2458 vm_object_unbusy(vm_object_t obj) 2459 { 2460 2461 blockcount_release(&obj->busy, 1); 2462 } 2463 2464 void 2465 vm_object_busy_wait(vm_object_t obj, const char *wmesg) 2466 { 2467 2468 VM_OBJECT_ASSERT_UNLOCKED(obj); 2469 2470 (void)blockcount_sleep(&obj->busy, NULL, wmesg, PVM); 2471 } 2472 2473 /* 2474 * This function aims to determine if the object is mapped, 2475 * specifically, if it is referenced by a vm_map_entry. Because 2476 * objects occasionally acquire transient references that do not 2477 * represent a mapping, the method used here is inexact. However, it 2478 * has very low overhead and is good enough for the advisory 2479 * vm.vmtotal sysctl. 2480 */ 2481 bool 2482 vm_object_is_active(vm_object_t obj) 2483 { 2484 2485 return (obj->ref_count > atomic_load_int(&obj->shadow_count)); 2486 } 2487 2488 static int 2489 vm_object_list_handler(struct sysctl_req *req, bool swap_only) 2490 { 2491 struct pctrie_iter pages; 2492 struct kinfo_vmobject *kvo; 2493 char *fullpath, *freepath; 2494 struct vnode *vp; 2495 struct vattr va; 2496 vm_object_t obj; 2497 vm_page_t m; 2498 u_long sp; 2499 int count, error; 2500 key_t key; 2501 unsigned short seq; 2502 bool want_path; 2503 2504 if (req->oldptr == NULL) { 2505 /* 2506 * If an old buffer has not been provided, generate an 2507 * estimate of the space needed for a subsequent call. 2508 */ 2509 mtx_lock(&vm_object_list_mtx); 2510 count = 0; 2511 TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2512 if (obj->type == OBJT_DEAD) 2513 continue; 2514 count++; 2515 } 2516 mtx_unlock(&vm_object_list_mtx); 2517 return (SYSCTL_OUT(req, NULL, sizeof(struct kinfo_vmobject) * 2518 count * 11 / 10)); 2519 } 2520 2521 want_path = !(swap_only || jailed(curthread->td_ucred)); 2522 kvo = malloc(sizeof(*kvo), M_TEMP, M_WAITOK | M_ZERO); 2523 error = 0; 2524 2525 /* 2526 * VM objects are type stable and are never removed from the 2527 * list once added. This allows us to safely read obj->object_list 2528 * after reacquiring the VM object lock. 2529 */ 2530 mtx_lock(&vm_object_list_mtx); 2531 TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2532 if (obj->type == OBJT_DEAD || 2533 (swap_only && (obj->flags & (OBJ_ANON | OBJ_SWAP)) == 0)) 2534 continue; 2535 VM_OBJECT_RLOCK(obj); 2536 if (obj->type == OBJT_DEAD || 2537 (swap_only && (obj->flags & (OBJ_ANON | OBJ_SWAP)) == 0)) { 2538 VM_OBJECT_RUNLOCK(obj); 2539 continue; 2540 } 2541 mtx_unlock(&vm_object_list_mtx); 2542 kvo->kvo_size = ptoa(obj->size); 2543 kvo->kvo_resident = obj->resident_page_count; 2544 kvo->kvo_ref_count = obj->ref_count; 2545 kvo->kvo_shadow_count = atomic_load_int(&obj->shadow_count); 2546 kvo->kvo_memattr = obj->memattr; 2547 kvo->kvo_active = 0; 2548 kvo->kvo_inactive = 0; 2549 kvo->kvo_flags = 0; 2550 if (!swap_only) { 2551 vm_page_iter_init(&pages, obj); 2552 VM_RADIX_FOREACH(m, &pages) { 2553 /* 2554 * A page may belong to the object but be 2555 * dequeued and set to PQ_NONE while the 2556 * object lock is not held. This makes the 2557 * reads of m->queue below racy, and we do not 2558 * count pages set to PQ_NONE. However, this 2559 * sysctl is only meant to give an 2560 * approximation of the system anyway. 2561 */ 2562 if (vm_page_active(m)) 2563 kvo->kvo_active++; 2564 else if (vm_page_inactive(m)) 2565 kvo->kvo_inactive++; 2566 else if (vm_page_in_laundry(m)) 2567 kvo->kvo_laundry++; 2568 } 2569 } 2570 2571 kvo->kvo_vn_fileid = 0; 2572 kvo->kvo_vn_fsid = 0; 2573 kvo->kvo_vn_fsid_freebsd11 = 0; 2574 freepath = NULL; 2575 fullpath = ""; 2576 vp = NULL; 2577 kvo->kvo_type = vm_object_kvme_type(obj, want_path ? &vp : 2578 NULL); 2579 if (vp != NULL) { 2580 vref(vp); 2581 } else if ((obj->flags & OBJ_ANON) != 0) { 2582 MPASS(kvo->kvo_type == KVME_TYPE_SWAP); 2583 kvo->kvo_me = (uintptr_t)obj; 2584 /* tmpfs objs are reported as vnodes */ 2585 kvo->kvo_backing_obj = (uintptr_t)obj->backing_object; 2586 sp = swap_pager_swapped_pages(obj); 2587 kvo->kvo_swapped = sp > UINT32_MAX ? UINT32_MAX : sp; 2588 } 2589 if (obj->type == OBJT_DEVICE || obj->type == OBJT_MGTDEVICE) { 2590 cdev_pager_get_path(obj, kvo->kvo_path, 2591 sizeof(kvo->kvo_path)); 2592 } 2593 VM_OBJECT_RUNLOCK(obj); 2594 if ((obj->flags & OBJ_SYSVSHM) != 0) { 2595 kvo->kvo_flags |= KVMO_FLAG_SYSVSHM; 2596 shmobjinfo(obj, &key, &seq); 2597 kvo->kvo_vn_fileid = key; 2598 kvo->kvo_vn_fsid_freebsd11 = seq; 2599 } 2600 if ((obj->flags & OBJ_POSIXSHM) != 0) { 2601 kvo->kvo_flags |= KVMO_FLAG_POSIXSHM; 2602 shm_get_path(obj, kvo->kvo_path, 2603 sizeof(kvo->kvo_path)); 2604 } 2605 if (vp != NULL) { 2606 vn_fullpath(vp, &fullpath, &freepath); 2607 vn_lock(vp, LK_SHARED | LK_RETRY); 2608 if (VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) { 2609 kvo->kvo_vn_fileid = va.va_fileid; 2610 kvo->kvo_vn_fsid = va.va_fsid; 2611 kvo->kvo_vn_fsid_freebsd11 = va.va_fsid; 2612 /* truncate */ 2613 } 2614 vput(vp); 2615 strlcpy(kvo->kvo_path, fullpath, sizeof(kvo->kvo_path)); 2616 free(freepath, M_TEMP); 2617 } 2618 2619 /* Pack record size down */ 2620 kvo->kvo_structsize = offsetof(struct kinfo_vmobject, kvo_path) 2621 + strlen(kvo->kvo_path) + 1; 2622 kvo->kvo_structsize = roundup(kvo->kvo_structsize, 2623 sizeof(uint64_t)); 2624 error = SYSCTL_OUT(req, kvo, kvo->kvo_structsize); 2625 maybe_yield(); 2626 mtx_lock(&vm_object_list_mtx); 2627 if (error) 2628 break; 2629 } 2630 mtx_unlock(&vm_object_list_mtx); 2631 free(kvo, M_TEMP); 2632 return (error); 2633 } 2634 2635 static int 2636 sysctl_vm_object_list(SYSCTL_HANDLER_ARGS) 2637 { 2638 return (vm_object_list_handler(req, false)); 2639 } 2640 2641 SYSCTL_PROC(_vm, OID_AUTO, objects, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP | 2642 CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_object_list, "S,kinfo_vmobject", 2643 "List of VM objects"); 2644 2645 static int 2646 sysctl_vm_object_list_swap(SYSCTL_HANDLER_ARGS) 2647 { 2648 return (vm_object_list_handler(req, true)); 2649 } 2650 2651 /* 2652 * This sysctl returns list of the anonymous or swap objects. Intent 2653 * is to provide stripped optimized list useful to analyze swap use. 2654 * Since technically non-swap (default) objects participate in the 2655 * shadow chains, and are converted to swap type as needed by swap 2656 * pager, we must report them. 2657 */ 2658 SYSCTL_PROC(_vm, OID_AUTO, swap_objects, 2659 CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, 0, 2660 sysctl_vm_object_list_swap, "S,kinfo_vmobject", 2661 "List of swap VM objects"); 2662 2663 #include "opt_ddb.h" 2664 #ifdef DDB 2665 #include <sys/kernel.h> 2666 2667 #include <sys/cons.h> 2668 2669 #include <ddb/ddb.h> 2670 2671 static int 2672 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2673 { 2674 vm_map_t tmpm; 2675 vm_map_entry_t tmpe; 2676 vm_object_t obj; 2677 2678 if (map == 0) 2679 return 0; 2680 2681 if (entry == 0) { 2682 VM_MAP_ENTRY_FOREACH(tmpe, map) { 2683 if (_vm_object_in_map(map, object, tmpe)) { 2684 return 1; 2685 } 2686 } 2687 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 2688 tmpm = entry->object.sub_map; 2689 VM_MAP_ENTRY_FOREACH(tmpe, tmpm) { 2690 if (_vm_object_in_map(tmpm, object, tmpe)) { 2691 return 1; 2692 } 2693 } 2694 } else if ((obj = entry->object.vm_object) != NULL) { 2695 for (; obj; obj = obj->backing_object) 2696 if (obj == object) { 2697 return 1; 2698 } 2699 } 2700 return 0; 2701 } 2702 2703 static int 2704 vm_object_in_map(vm_object_t object) 2705 { 2706 struct proc *p; 2707 2708 /* sx_slock(&allproc_lock); */ 2709 FOREACH_PROC_IN_SYSTEM(p) { 2710 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2711 continue; 2712 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 2713 /* sx_sunlock(&allproc_lock); */ 2714 return 1; 2715 } 2716 } 2717 /* sx_sunlock(&allproc_lock); */ 2718 if (_vm_object_in_map(kernel_map, object, 0)) 2719 return 1; 2720 return 0; 2721 } 2722 2723 DB_SHOW_COMMAND_FLAGS(vmochk, vm_object_check, DB_CMD_MEMSAFE) 2724 { 2725 vm_object_t object; 2726 2727 /* 2728 * make sure that internal objs are in a map somewhere 2729 * and none have zero ref counts. 2730 */ 2731 TAILQ_FOREACH(object, &vm_object_list, object_list) { 2732 if ((object->flags & OBJ_ANON) != 0) { 2733 if (object->ref_count == 0) { 2734 db_printf( 2735 "vmochk: internal obj has zero ref count: %lu\n", 2736 (u_long)object->size); 2737 } 2738 if (!vm_object_in_map(object)) { 2739 db_printf( 2740 "vmochk: internal obj is not in a map: " 2741 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2742 object->ref_count, (u_long)object->size, 2743 (u_long)object->size, 2744 (void *)object->backing_object); 2745 } 2746 } 2747 if (db_pager_quit) 2748 return; 2749 } 2750 } 2751 2752 /* 2753 * vm_object_print: [ debug ] 2754 */ 2755 DB_SHOW_COMMAND(object, vm_object_print_static) 2756 { 2757 struct pctrie_iter pages; 2758 /* XXX convert args. */ 2759 vm_object_t object = (vm_object_t)addr; 2760 boolean_t full = have_addr; 2761 2762 vm_page_t p; 2763 2764 /* XXX count is an (unused) arg. Avoid shadowing it. */ 2765 #define count was_count 2766 2767 int count; 2768 2769 if (object == NULL) 2770 return; 2771 2772 db_iprintf("Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x", 2773 object, (int)object->type, (uintmax_t)object->size, 2774 object->resident_page_count, object->ref_count, object->flags); 2775 db_iprintf(" ruid %d charge %jx\n", 2776 object->cred ? object->cred->cr_ruid : -1, 2777 (uintmax_t)object->charge); 2778 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 2779 atomic_load_int(&object->shadow_count), 2780 object->backing_object ? object->backing_object->ref_count : 0, 2781 object->backing_object, (uintmax_t)object->backing_object_offset); 2782 2783 if (!full) 2784 return; 2785 2786 db_indent += 2; 2787 count = 0; 2788 vm_page_iter_init(&pages, object); 2789 VM_RADIX_FOREACH(p, &pages) { 2790 if (count == 0) 2791 db_iprintf("memory:="); 2792 else if (count == 6) { 2793 db_printf("\n"); 2794 db_iprintf(" ..."); 2795 count = 0; 2796 } else 2797 db_printf(","); 2798 count++; 2799 2800 db_printf("(off=0x%jx,page=0x%jx)", 2801 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2802 2803 if (db_pager_quit) 2804 break; 2805 } 2806 if (count != 0) 2807 db_printf("\n"); 2808 db_indent -= 2; 2809 } 2810 2811 /* XXX. */ 2812 #undef count 2813 2814 /* XXX need this non-static entry for calling from vm_map_print. */ 2815 void 2816 vm_object_print( 2817 /* db_expr_t */ long addr, 2818 boolean_t have_addr, 2819 /* db_expr_t */ long count, 2820 char *modif) 2821 { 2822 vm_object_print_static(addr, have_addr, count, modif); 2823 } 2824 2825 DB_SHOW_COMMAND_FLAGS(vmopag, vm_object_print_pages, DB_CMD_MEMSAFE) 2826 { 2827 struct pctrie_iter pages; 2828 vm_object_t object; 2829 vm_page_t m, start_m; 2830 int rcount; 2831 2832 TAILQ_FOREACH(object, &vm_object_list, object_list) { 2833 db_printf("new object: %p\n", (void *)object); 2834 if (db_pager_quit) 2835 return; 2836 start_m = NULL; 2837 vm_page_iter_init(&pages, object); 2838 VM_RADIX_FOREACH(m, &pages) { 2839 if (start_m == NULL) { 2840 start_m = m; 2841 rcount = 0; 2842 } else if (start_m->pindex + rcount != m->pindex || 2843 VM_PAGE_TO_PHYS(start_m) + ptoa(rcount) != 2844 VM_PAGE_TO_PHYS(m)) { 2845 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2846 (long)start_m->pindex, rcount, 2847 (long)VM_PAGE_TO_PHYS(start_m)); 2848 if (db_pager_quit) 2849 return; 2850 start_m = m; 2851 rcount = 0; 2852 } 2853 rcount++; 2854 } 2855 if (start_m != NULL) { 2856 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2857 (long)start_m->pindex, rcount, 2858 (long)VM_PAGE_TO_PHYS(start_m)); 2859 if (db_pager_quit) 2860 return; 2861 } 2862 } 2863 } 2864 #endif /* DDB */ 2865