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