1 /* 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * The Mach Operating System project at Carnegie-Mellon University. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94 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 * $FreeBSD: src/sys/vm/vm_object.c,v 1.171.2.8 2003/05/26 19:17:56 alc Exp $ 61 */ 62 63 /* 64 * Virtual memory object module. 65 */ 66 67 #include <sys/param.h> 68 #include <sys/systm.h> 69 #include <sys/proc.h> /* for curproc, pageproc */ 70 #include <sys/thread.h> 71 #include <sys/vnode.h> 72 #include <sys/vmmeter.h> 73 #include <sys/mman.h> 74 #include <sys/mount.h> 75 #include <sys/kernel.h> 76 #include <sys/sysctl.h> 77 #include <sys/refcount.h> 78 79 #include <vm/vm.h> 80 #include <vm/vm_param.h> 81 #include <vm/pmap.h> 82 #include <vm/vm_map.h> 83 #include <vm/vm_object.h> 84 #include <vm/vm_page.h> 85 #include <vm/vm_pageout.h> 86 #include <vm/vm_pager.h> 87 #include <vm/swap_pager.h> 88 #include <vm/vm_kern.h> 89 #include <vm/vm_extern.h> 90 #include <vm/vm_zone.h> 91 92 #include <machine/specialreg.h> 93 94 #define EASY_SCAN_FACTOR 8 95 96 static void vm_object_qcollapse(vm_object_t object, 97 vm_object_t backing_object); 98 static void vm_object_page_collect_flush(vm_object_t object, vm_page_t p, 99 int pagerflags); 100 static void vm_object_lock_init(vm_object_t); 101 102 103 /* 104 * Virtual memory objects maintain the actual data 105 * associated with allocated virtual memory. A given 106 * page of memory exists within exactly one object. 107 * 108 * An object is only deallocated when all "references" 109 * are given up. Only one "reference" to a given 110 * region of an object should be writeable. 111 * 112 * Associated with each object is a list of all resident 113 * memory pages belonging to that object; this list is 114 * maintained by the "vm_page" module, and locked by the object's 115 * lock. 116 * 117 * Each object also records a "pager" routine which is 118 * used to retrieve (and store) pages to the proper backing 119 * storage. In addition, objects may be backed by other 120 * objects from which they were virtual-copied. 121 * 122 * The only items within the object structure which are 123 * modified after time of creation are: 124 * reference count locked by object's lock 125 * pager routine locked by object's lock 126 * 127 */ 128 129 struct object_q vm_object_list; /* locked by vmobj_token */ 130 struct vm_object kernel_object; 131 132 static long vm_object_count; /* locked by vmobj_token */ 133 134 static long object_collapses; 135 static long object_bypasses; 136 static int next_index; 137 static vm_zone_t obj_zone; 138 static struct vm_zone obj_zone_store; 139 #define VM_OBJECTS_INIT 256 140 static struct vm_object vm_objects_init[VM_OBJECTS_INIT]; 141 142 /* 143 * Misc low level routines 144 */ 145 static void 146 vm_object_lock_init(vm_object_t obj) 147 { 148 #if defined(DEBUG_LOCKS) 149 int i; 150 151 obj->debug_hold_bitmap = 0; 152 obj->debug_hold_ovfl = 0; 153 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) { 154 obj->debug_hold_thrs[i] = NULL; 155 obj->debug_hold_file[i] = NULL; 156 obj->debug_hold_line[i] = 0; 157 } 158 #endif 159 } 160 161 void 162 vm_object_lock_swap(void) 163 { 164 lwkt_token_swap(); 165 } 166 167 void 168 vm_object_lock(vm_object_t obj) 169 { 170 lwkt_gettoken(&obj->token); 171 } 172 173 /* 174 * Returns TRUE on sucesss 175 */ 176 static int 177 vm_object_lock_try(vm_object_t obj) 178 { 179 return(lwkt_trytoken(&obj->token)); 180 } 181 182 void 183 vm_object_lock_shared(vm_object_t obj) 184 { 185 lwkt_gettoken_shared(&obj->token); 186 } 187 188 void 189 vm_object_unlock(vm_object_t obj) 190 { 191 lwkt_reltoken(&obj->token); 192 } 193 194 static __inline void 195 vm_object_assert_held(vm_object_t obj) 196 { 197 ASSERT_LWKT_TOKEN_HELD(&obj->token); 198 } 199 200 void 201 #ifndef DEBUG_LOCKS 202 vm_object_hold(vm_object_t obj) 203 #else 204 debugvm_object_hold(vm_object_t obj, char *file, int line) 205 #endif 206 { 207 KKASSERT(obj != NULL); 208 209 /* 210 * Object must be held (object allocation is stable due to callers 211 * context, typically already holding the token on a parent object) 212 * prior to potentially blocking on the lock, otherwise the object 213 * can get ripped away from us. 214 */ 215 refcount_acquire(&obj->hold_count); 216 vm_object_lock(obj); 217 218 #if defined(DEBUG_LOCKS) 219 int i; 220 u_int mask; 221 222 for (;;) { 223 mask = ~obj->debug_hold_bitmap; 224 cpu_ccfence(); 225 if (mask == 0xFFFFFFFFU) { 226 if (obj->debug_hold_ovfl == 0) 227 obj->debug_hold_ovfl = 1; 228 break; 229 } 230 i = ffs(mask) - 1; 231 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask, 232 ~mask | (1 << i))) { 233 obj->debug_hold_bitmap |= (1 << i); 234 obj->debug_hold_thrs[i] = curthread; 235 obj->debug_hold_file[i] = file; 236 obj->debug_hold_line[i] = line; 237 break; 238 } 239 } 240 #endif 241 } 242 243 int 244 #ifndef DEBUG_LOCKS 245 vm_object_hold_try(vm_object_t obj) 246 #else 247 debugvm_object_hold_try(vm_object_t obj, char *file, int line) 248 #endif 249 { 250 KKASSERT(obj != NULL); 251 252 /* 253 * Object must be held (object allocation is stable due to callers 254 * context, typically already holding the token on a parent object) 255 * prior to potentially blocking on the lock, otherwise the object 256 * can get ripped away from us. 257 */ 258 refcount_acquire(&obj->hold_count); 259 if (vm_object_lock_try(obj) == 0) { 260 if (refcount_release(&obj->hold_count)) { 261 if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD)) 262 zfree(obj_zone, obj); 263 } 264 return(0); 265 } 266 267 #if defined(DEBUG_LOCKS) 268 int i; 269 u_int mask; 270 271 for (;;) { 272 mask = ~obj->debug_hold_bitmap; 273 cpu_ccfence(); 274 if (mask == 0xFFFFFFFFU) { 275 if (obj->debug_hold_ovfl == 0) 276 obj->debug_hold_ovfl = 1; 277 break; 278 } 279 i = ffs(mask) - 1; 280 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask, 281 ~mask | (1 << i))) { 282 obj->debug_hold_bitmap |= (1 << i); 283 obj->debug_hold_thrs[i] = curthread; 284 obj->debug_hold_file[i] = file; 285 obj->debug_hold_line[i] = line; 286 break; 287 } 288 } 289 #endif 290 return(1); 291 } 292 293 void 294 #ifndef DEBUG_LOCKS 295 vm_object_hold_shared(vm_object_t obj) 296 #else 297 debugvm_object_hold_shared(vm_object_t obj, char *file, int line) 298 #endif 299 { 300 KKASSERT(obj != NULL); 301 302 /* 303 * Object must be held (object allocation is stable due to callers 304 * context, typically already holding the token on a parent object) 305 * prior to potentially blocking on the lock, otherwise the object 306 * can get ripped away from us. 307 */ 308 refcount_acquire(&obj->hold_count); 309 vm_object_lock_shared(obj); 310 311 #if defined(DEBUG_LOCKS) 312 int i; 313 u_int mask; 314 315 for (;;) { 316 mask = ~obj->debug_hold_bitmap; 317 cpu_ccfence(); 318 if (mask == 0xFFFFFFFFU) { 319 if (obj->debug_hold_ovfl == 0) 320 obj->debug_hold_ovfl = 1; 321 break; 322 } 323 i = ffs(mask) - 1; 324 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask, 325 ~mask | (1 << i))) { 326 obj->debug_hold_bitmap |= (1 << i); 327 obj->debug_hold_thrs[i] = curthread; 328 obj->debug_hold_file[i] = file; 329 obj->debug_hold_line[i] = line; 330 break; 331 } 332 } 333 #endif 334 } 335 336 /* 337 * Obtain either a shared or exclusive lock on VM object 338 * based on whether this is a terminal vnode object or not. 339 */ 340 int 341 #ifndef DEBUG_LOCKS 342 vm_object_hold_maybe_shared(vm_object_t obj) 343 #else 344 debugvm_object_hold_maybe_shared(vm_object_t obj, char *file, int line) 345 #endif 346 { 347 if (vm_shared_fault && 348 obj->type == OBJT_VNODE && 349 obj->backing_object == NULL) { 350 vm_object_hold_shared(obj); 351 return(1); 352 } else { 353 vm_object_hold(obj); 354 return(0); 355 } 356 } 357 358 /* 359 * Drop the token and hold_count on the object. 360 */ 361 void 362 vm_object_drop(vm_object_t obj) 363 { 364 if (obj == NULL) 365 return; 366 367 #if defined(DEBUG_LOCKS) 368 int found = 0; 369 int i; 370 371 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) { 372 if ((obj->debug_hold_bitmap & (1 << i)) && 373 (obj->debug_hold_thrs[i] == curthread)) { 374 obj->debug_hold_bitmap &= ~(1 << i); 375 obj->debug_hold_thrs[i] = NULL; 376 obj->debug_hold_file[i] = NULL; 377 obj->debug_hold_line[i] = 0; 378 found = 1; 379 break; 380 } 381 } 382 383 if (found == 0 && obj->debug_hold_ovfl == 0) 384 panic("vm_object: attempt to drop hold on non-self-held obj"); 385 #endif 386 387 /* 388 * No new holders should be possible once we drop hold_count 1->0 as 389 * there is no longer any way to reference the object. 390 */ 391 KKASSERT(obj->hold_count > 0); 392 if (refcount_release(&obj->hold_count)) { 393 if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD)) { 394 vm_object_unlock(obj); 395 zfree(obj_zone, obj); 396 } else { 397 vm_object_unlock(obj); 398 } 399 } else { 400 vm_object_unlock(obj); 401 } 402 } 403 404 /* 405 * Initialize a freshly allocated object, returning a held object. 406 * 407 * Used only by vm_object_allocate() and zinitna(). 408 * 409 * No requirements. 410 */ 411 void 412 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object) 413 { 414 int incr; 415 416 RB_INIT(&object->rb_memq); 417 LIST_INIT(&object->shadow_head); 418 lwkt_token_init(&object->token, "vmobj"); 419 420 object->type = type; 421 object->size = size; 422 object->ref_count = 1; 423 object->memattr = VM_MEMATTR_DEFAULT; 424 object->hold_count = 0; 425 object->flags = 0; 426 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP)) 427 vm_object_set_flag(object, OBJ_ONEMAPPING); 428 object->paging_in_progress = 0; 429 object->resident_page_count = 0; 430 object->agg_pv_list_count = 0; 431 object->shadow_count = 0; 432 /* cpu localization twist */ 433 object->pg_color = (int)(intptr_t)curthread; 434 if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1)) 435 incr = PQ_L2_SIZE / 3 + PQ_PRIME1; 436 else 437 incr = size; 438 next_index = (next_index + incr) & PQ_L2_MASK; 439 object->handle = NULL; 440 object->backing_object = NULL; 441 object->backing_object_offset = (vm_ooffset_t)0; 442 443 object->generation++; 444 object->swblock_count = 0; 445 RB_INIT(&object->swblock_root); 446 vm_object_lock_init(object); 447 pmap_object_init(object); 448 449 vm_object_hold(object); 450 lwkt_gettoken(&vmobj_token); 451 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 452 vm_object_count++; 453 lwkt_reltoken(&vmobj_token); 454 } 455 456 /* 457 * Initialize the VM objects module. 458 * 459 * Called from the low level boot code only. 460 */ 461 void 462 vm_object_init(void) 463 { 464 TAILQ_INIT(&vm_object_list); 465 466 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(KvaEnd), 467 &kernel_object); 468 vm_object_drop(&kernel_object); 469 470 obj_zone = &obj_zone_store; 471 zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object), 472 vm_objects_init, VM_OBJECTS_INIT); 473 } 474 475 void 476 vm_object_init2(void) 477 { 478 zinitna(obj_zone, NULL, NULL, 0, 0, ZONE_PANICFAIL, 1); 479 } 480 481 /* 482 * Allocate and return a new object of the specified type and size. 483 * 484 * No requirements. 485 */ 486 vm_object_t 487 vm_object_allocate(objtype_t type, vm_pindex_t size) 488 { 489 vm_object_t result; 490 491 result = (vm_object_t) zalloc(obj_zone); 492 493 _vm_object_allocate(type, size, result); 494 vm_object_drop(result); 495 496 return (result); 497 } 498 499 /* 500 * This version returns a held object, allowing further atomic initialization 501 * of the object. 502 */ 503 vm_object_t 504 vm_object_allocate_hold(objtype_t type, vm_pindex_t size) 505 { 506 vm_object_t result; 507 508 result = (vm_object_t) zalloc(obj_zone); 509 510 _vm_object_allocate(type, size, result); 511 512 return (result); 513 } 514 515 /* 516 * Add an additional reference to a vm_object. The object must already be 517 * held. The original non-lock version is no longer supported. The object 518 * must NOT be chain locked by anyone at the time the reference is added. 519 * 520 * Referencing a chain-locked object can blow up the fairly sensitive 521 * ref_count and shadow_count tests in the deallocator. Most callers 522 * will call vm_object_chain_wait() prior to calling 523 * vm_object_reference_locked() to avoid the case. 524 * 525 * The object must be held, but may be held shared if desired (hence why 526 * we use an atomic op). 527 */ 528 void 529 vm_object_reference_locked(vm_object_t object) 530 { 531 KKASSERT(object != NULL); 532 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 533 KKASSERT((object->flags & OBJ_CHAINLOCK) == 0); 534 atomic_add_int(&object->ref_count, 1); 535 if (object->type == OBJT_VNODE) { 536 vref(object->handle); 537 /* XXX what if the vnode is being destroyed? */ 538 } 539 } 540 541 /* 542 * Object OBJ_CHAINLOCK lock handling. 543 * 544 * The caller can chain-lock backing objects recursively and then 545 * use vm_object_chain_release_all() to undo the whole chain. 546 * 547 * Chain locks are used to prevent collapses and are only applicable 548 * to OBJT_DEFAULT and OBJT_SWAP objects. Chain locking operations 549 * on other object types are ignored. This is also important because 550 * it allows e.g. the vnode underlying a memory mapping to take concurrent 551 * faults. 552 * 553 * The object must usually be held on entry, though intermediate 554 * objects need not be held on release. 555 */ 556 void 557 vm_object_chain_wait(vm_object_t object) 558 { 559 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 560 while (object->flags & OBJ_CHAINLOCK) { 561 vm_object_set_flag(object, OBJ_CHAINWANT); 562 tsleep(object, 0, "objchain", 0); 563 } 564 } 565 566 void 567 vm_object_chain_acquire(vm_object_t object) 568 { 569 if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) { 570 vm_object_chain_wait(object); 571 vm_object_set_flag(object, OBJ_CHAINLOCK); 572 } 573 } 574 575 void 576 vm_object_chain_release(vm_object_t object) 577 { 578 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 579 if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) { 580 KKASSERT(object->flags & OBJ_CHAINLOCK); 581 if (object->flags & OBJ_CHAINWANT) { 582 vm_object_clear_flag(object, 583 OBJ_CHAINLOCK | OBJ_CHAINWANT); 584 wakeup(object); 585 } else { 586 vm_object_clear_flag(object, OBJ_CHAINLOCK); 587 } 588 } 589 } 590 591 /* 592 * This releases the entire chain of objects from first_object to and 593 * including stopobj, flowing through object->backing_object. 594 * 595 * We release stopobj first as an optimization as this object is most 596 * likely to be shared across multiple processes. 597 */ 598 void 599 vm_object_chain_release_all(vm_object_t first_object, vm_object_t stopobj) 600 { 601 vm_object_t backing_object; 602 vm_object_t object; 603 604 vm_object_chain_release(stopobj); 605 object = first_object; 606 607 while (object != stopobj) { 608 KKASSERT(object); 609 if (object != first_object) 610 vm_object_hold(object); 611 backing_object = object->backing_object; 612 vm_object_chain_release(object); 613 if (object != first_object) 614 vm_object_drop(object); 615 object = backing_object; 616 } 617 } 618 619 /* 620 * Dereference an object and its underlying vnode. 621 * 622 * The object must be held exclusively and will remain held on return. 623 * (We don't need an atomic op due to the exclusivity). 624 */ 625 static void 626 vm_object_vndeallocate(vm_object_t object) 627 { 628 struct vnode *vp = (struct vnode *) object->handle; 629 630 KASSERT(object->type == OBJT_VNODE, 631 ("vm_object_vndeallocate: not a vnode object")); 632 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 633 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 634 #ifdef INVARIANTS 635 if (object->ref_count == 0) { 636 vprint("vm_object_vndeallocate", vp); 637 panic("vm_object_vndeallocate: bad object reference count"); 638 } 639 #endif 640 object->ref_count--; 641 if (object->ref_count == 0) 642 vclrflags(vp, VTEXT); 643 vrele(vp); 644 } 645 646 /* 647 * Release a reference to the specified object, gained either through a 648 * vm_object_allocate or a vm_object_reference call. When all references 649 * are gone, storage associated with this object may be relinquished. 650 * 651 * The caller does not have to hold the object locked but must have control 652 * over the reference in question in order to guarantee that the object 653 * does not get ripped out from under us. 654 * 655 * XXX Currently all deallocations require an exclusive lock. 656 */ 657 void 658 vm_object_deallocate(vm_object_t object) 659 { 660 if (object) { 661 vm_object_hold(object); 662 vm_object_deallocate_locked(object); 663 vm_object_drop(object); 664 } 665 } 666 667 void 668 vm_object_deallocate_locked(vm_object_t object) 669 { 670 struct vm_object_dealloc_list *dlist = NULL; 671 struct vm_object_dealloc_list *dtmp; 672 vm_object_t temp; 673 int must_drop = 0; 674 675 /* 676 * We may chain deallocate object, but additional objects may 677 * collect on the dlist which also have to be deallocated. We 678 * must avoid a recursion, vm_object chains can get deep. 679 */ 680 again: 681 while (object != NULL) { 682 ASSERT_LWKT_TOKEN_HELD_EXCL(&object->token); 683 #if 0 684 /* 685 * Don't rip a ref_count out from under an object undergoing 686 * collapse, it will confuse the collapse code. 687 */ 688 vm_object_chain_wait(object); 689 #endif 690 if (object->type == OBJT_VNODE) { 691 vm_object_vndeallocate(object); 692 break; 693 } 694 695 if (object->ref_count == 0) { 696 panic("vm_object_deallocate: object deallocated " 697 "too many times: %d", object->type); 698 } 699 if (object->ref_count > 2) { 700 object->ref_count--; 701 break; 702 } 703 704 /* 705 * Here on ref_count of one or two, which are special cases for 706 * objects. 707 * 708 * Nominal ref_count > 1 case if the second ref is not from 709 * a shadow. 710 * 711 * (ONEMAPPING only applies to DEFAULT AND SWAP objects) 712 */ 713 if (object->ref_count == 2 && object->shadow_count == 0) { 714 if (object->type == OBJT_DEFAULT || 715 object->type == OBJT_SWAP) { 716 vm_object_set_flag(object, OBJ_ONEMAPPING); 717 } 718 object->ref_count--; 719 break; 720 } 721 722 /* 723 * If the second ref is from a shadow we chain along it 724 * upwards if object's handle is exhausted. 725 * 726 * We have to decrement object->ref_count before potentially 727 * collapsing the first shadow object or the collapse code 728 * will not be able to handle the degenerate case to remove 729 * object. However, if we do it too early the object can 730 * get ripped out from under us. 731 */ 732 if (object->ref_count == 2 && object->shadow_count == 1 && 733 object->handle == NULL && (object->type == OBJT_DEFAULT || 734 object->type == OBJT_SWAP)) { 735 temp = LIST_FIRST(&object->shadow_head); 736 KKASSERT(temp != NULL); 737 vm_object_hold(temp); 738 739 /* 740 * Wait for any paging to complete so the collapse 741 * doesn't (or isn't likely to) qcollapse. pip 742 * waiting must occur before we acquire the 743 * chainlock. 744 */ 745 while ( 746 temp->paging_in_progress || 747 object->paging_in_progress 748 ) { 749 vm_object_pip_wait(temp, "objde1"); 750 vm_object_pip_wait(object, "objde2"); 751 } 752 753 /* 754 * If the parent is locked we have to give up, as 755 * otherwise we would be acquiring locks in the 756 * wrong order and potentially deadlock. 757 */ 758 if (temp->flags & OBJ_CHAINLOCK) { 759 vm_object_drop(temp); 760 goto skip; 761 } 762 vm_object_chain_acquire(temp); 763 764 /* 765 * Recheck/retry after the hold and the paging 766 * wait, both of which can block us. 767 */ 768 if (object->ref_count != 2 || 769 object->shadow_count != 1 || 770 object->handle || 771 LIST_FIRST(&object->shadow_head) != temp || 772 (object->type != OBJT_DEFAULT && 773 object->type != OBJT_SWAP)) { 774 vm_object_chain_release(temp); 775 vm_object_drop(temp); 776 continue; 777 } 778 779 /* 780 * We can safely drop object's ref_count now. 781 */ 782 KKASSERT(object->ref_count == 2); 783 object->ref_count--; 784 785 /* 786 * If our single parent is not collapseable just 787 * decrement ref_count (2->1) and stop. 788 */ 789 if (temp->handle || (temp->type != OBJT_DEFAULT && 790 temp->type != OBJT_SWAP)) { 791 vm_object_chain_release(temp); 792 vm_object_drop(temp); 793 break; 794 } 795 796 /* 797 * At this point we have already dropped object's 798 * ref_count so it is possible for a race to 799 * deallocate obj out from under us. Any collapse 800 * will re-check the situation. We must not block 801 * until we are able to collapse. 802 * 803 * Bump temp's ref_count to avoid an unwanted 804 * degenerate recursion (can't call 805 * vm_object_reference_locked() because it asserts 806 * that CHAINLOCK is not set). 807 */ 808 temp->ref_count++; 809 KKASSERT(temp->ref_count > 1); 810 811 /* 812 * Collapse temp, then deallocate the extra ref 813 * formally. 814 */ 815 vm_object_collapse(temp, &dlist); 816 vm_object_chain_release(temp); 817 if (must_drop) { 818 vm_object_lock_swap(); 819 vm_object_drop(object); 820 } 821 object = temp; 822 must_drop = 1; 823 continue; 824 } 825 826 /* 827 * Drop the ref and handle termination on the 1->0 transition. 828 * We may have blocked above so we have to recheck. 829 */ 830 skip: 831 KKASSERT(object->ref_count != 0); 832 if (object->ref_count >= 2) { 833 object->ref_count--; 834 break; 835 } 836 KKASSERT(object->ref_count == 1); 837 838 /* 839 * 1->0 transition. Chain through the backing_object. 840 * Maintain the ref until we've located the backing object, 841 * then re-check. 842 */ 843 while ((temp = object->backing_object) != NULL) { 844 vm_object_hold(temp); 845 if (temp == object->backing_object) 846 break; 847 vm_object_drop(temp); 848 } 849 850 /* 851 * 1->0 transition verified, retry if ref_count is no longer 852 * 1. Otherwise disconnect the backing_object (temp) and 853 * clean up. 854 */ 855 if (object->ref_count != 1) { 856 vm_object_drop(temp); 857 continue; 858 } 859 860 /* 861 * It shouldn't be possible for the object to be chain locked 862 * if we're removing the last ref on it. 863 */ 864 KKASSERT((object->flags & OBJ_CHAINLOCK) == 0); 865 866 if (temp) { 867 LIST_REMOVE(object, shadow_list); 868 temp->shadow_count--; 869 temp->generation++; 870 object->backing_object = NULL; 871 } 872 873 --object->ref_count; 874 if ((object->flags & OBJ_DEAD) == 0) 875 vm_object_terminate(object); 876 if (must_drop && temp) 877 vm_object_lock_swap(); 878 if (must_drop) 879 vm_object_drop(object); 880 object = temp; 881 must_drop = 1; 882 } 883 if (must_drop && object) 884 vm_object_drop(object); 885 886 /* 887 * Additional tail recursion on dlist. Avoid a recursion. Objects 888 * on the dlist have a hold count but are not locked. 889 */ 890 if ((dtmp = dlist) != NULL) { 891 dlist = dtmp->next; 892 object = dtmp->object; 893 kfree(dtmp, M_TEMP); 894 895 vm_object_lock(object); /* already held, add lock */ 896 must_drop = 1; /* and we're responsible for it */ 897 goto again; 898 } 899 } 900 901 /* 902 * Destroy the specified object, freeing up related resources. 903 * 904 * The object must have zero references. 905 * 906 * The object must held. The caller is responsible for dropping the object 907 * after terminate returns. Terminate does NOT drop the object. 908 */ 909 static int vm_object_terminate_callback(vm_page_t p, void *data); 910 911 void 912 vm_object_terminate(vm_object_t object) 913 { 914 /* 915 * Make sure no one uses us. Once we set OBJ_DEAD we should be 916 * able to safely block. 917 */ 918 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 919 KKASSERT((object->flags & OBJ_DEAD) == 0); 920 vm_object_set_flag(object, OBJ_DEAD); 921 922 /* 923 * Wait for the pageout daemon to be done with the object 924 */ 925 vm_object_pip_wait(object, "objtrm1"); 926 927 KASSERT(!object->paging_in_progress, 928 ("vm_object_terminate: pageout in progress")); 929 930 /* 931 * Clean and free the pages, as appropriate. All references to the 932 * object are gone, so we don't need to lock it. 933 */ 934 if (object->type == OBJT_VNODE) { 935 struct vnode *vp; 936 937 /* 938 * Clean pages and flush buffers. 939 * 940 * NOTE! TMPFS buffer flushes do not typically flush the 941 * actual page to swap as this would be highly 942 * inefficient, and normal filesystems usually wrap 943 * page flushes with buffer cache buffers. 944 * 945 * To deal with this we have to call vinvalbuf() both 946 * before and after the vm_object_page_clean(). 947 */ 948 vp = (struct vnode *) object->handle; 949 vinvalbuf(vp, V_SAVE, 0, 0); 950 vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 951 vinvalbuf(vp, V_SAVE, 0, 0); 952 } 953 954 /* 955 * Wait for any I/O to complete, after which there had better not 956 * be any references left on the object. 957 */ 958 vm_object_pip_wait(object, "objtrm2"); 959 960 if (object->ref_count != 0) { 961 panic("vm_object_terminate: object with references, " 962 "ref_count=%d", object->ref_count); 963 } 964 965 /* 966 * Cleanup any shared pmaps associated with this object. 967 */ 968 pmap_object_free(object); 969 970 /* 971 * Now free any remaining pages. For internal objects, this also 972 * removes them from paging queues. Don't free wired pages, just 973 * remove them from the object. 974 */ 975 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL, 976 vm_object_terminate_callback, NULL); 977 978 /* 979 * Let the pager know object is dead. 980 */ 981 vm_pager_deallocate(object); 982 983 /* 984 * Wait for the object hold count to hit 1, clean out pages as 985 * we go. vmobj_token interlocks any race conditions that might 986 * pick the object up from the vm_object_list after we have cleared 987 * rb_memq. 988 */ 989 for (;;) { 990 if (RB_ROOT(&object->rb_memq) == NULL) 991 break; 992 kprintf("vm_object_terminate: Warning, object %p " 993 "still has %d pages\n", 994 object, object->resident_page_count); 995 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL, 996 vm_object_terminate_callback, NULL); 997 } 998 999 /* 1000 * There had better not be any pages left 1001 */ 1002 KKASSERT(object->resident_page_count == 0); 1003 1004 /* 1005 * Remove the object from the global object list. 1006 */ 1007 lwkt_gettoken(&vmobj_token); 1008 TAILQ_REMOVE(&vm_object_list, object, object_list); 1009 vm_object_count--; 1010 lwkt_reltoken(&vmobj_token); 1011 vm_object_dead_wakeup(object); 1012 1013 if (object->ref_count != 0) { 1014 panic("vm_object_terminate2: object with references, " 1015 "ref_count=%d", object->ref_count); 1016 } 1017 1018 /* 1019 * NOTE: The object hold_count is at least 1, so we cannot zfree() 1020 * the object here. See vm_object_drop(). 1021 */ 1022 } 1023 1024 /* 1025 * The caller must hold the object. 1026 */ 1027 static int 1028 vm_object_terminate_callback(vm_page_t p, void *data __unused) 1029 { 1030 vm_object_t object; 1031 1032 object = p->object; 1033 vm_page_busy_wait(p, TRUE, "vmpgtrm"); 1034 if (object != p->object) { 1035 kprintf("vm_object_terminate: Warning: Encountered " 1036 "busied page %p on queue %d\n", p, p->queue); 1037 vm_page_wakeup(p); 1038 } else if (p->wire_count == 0) { 1039 /* 1040 * NOTE: p->dirty and PG_NEED_COMMIT are ignored. 1041 */ 1042 vm_page_free(p); 1043 mycpu->gd_cnt.v_pfree++; 1044 } else { 1045 if (p->queue != PQ_NONE) 1046 kprintf("vm_object_terminate: Warning: Encountered " 1047 "wired page %p on queue %d\n", p, p->queue); 1048 vm_page_remove(p); 1049 vm_page_wakeup(p); 1050 } 1051 lwkt_yield(); 1052 return(0); 1053 } 1054 1055 /* 1056 * The object is dead but still has an object<->pager association. Sleep 1057 * and return. The caller typically retests the association in a loop. 1058 * 1059 * The caller must hold the object. 1060 */ 1061 void 1062 vm_object_dead_sleep(vm_object_t object, const char *wmesg) 1063 { 1064 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 1065 if (object->handle) { 1066 vm_object_set_flag(object, OBJ_DEADWNT); 1067 tsleep(object, 0, wmesg, 0); 1068 /* object may be invalid after this point */ 1069 } 1070 } 1071 1072 /* 1073 * Wakeup anyone waiting for the object<->pager disassociation on 1074 * a dead object. 1075 * 1076 * The caller must hold the object. 1077 */ 1078 void 1079 vm_object_dead_wakeup(vm_object_t object) 1080 { 1081 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 1082 if (object->flags & OBJ_DEADWNT) { 1083 vm_object_clear_flag(object, OBJ_DEADWNT); 1084 wakeup(object); 1085 } 1086 } 1087 1088 /* 1089 * Clean all dirty pages in the specified range of object. Leaves page 1090 * on whatever queue it is currently on. If NOSYNC is set then do not 1091 * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC), 1092 * leaving the object dirty. 1093 * 1094 * When stuffing pages asynchronously, allow clustering. XXX we need a 1095 * synchronous clustering mode implementation. 1096 * 1097 * Odd semantics: if start == end, we clean everything. 1098 * 1099 * The object must be locked? XXX 1100 */ 1101 static int vm_object_page_clean_pass1(struct vm_page *p, void *data); 1102 static int vm_object_page_clean_pass2(struct vm_page *p, void *data); 1103 1104 void 1105 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 1106 int flags) 1107 { 1108 struct rb_vm_page_scan_info info; 1109 struct vnode *vp; 1110 int wholescan; 1111 int pagerflags; 1112 int generation; 1113 1114 vm_object_hold(object); 1115 if (object->type != OBJT_VNODE || 1116 (object->flags & OBJ_MIGHTBEDIRTY) == 0) { 1117 vm_object_drop(object); 1118 return; 1119 } 1120 1121 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? 1122 VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 1123 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0; 1124 1125 vp = object->handle; 1126 1127 /* 1128 * Interlock other major object operations. This allows us to 1129 * temporarily clear OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY. 1130 */ 1131 vm_object_set_flag(object, OBJ_CLEANING); 1132 1133 /* 1134 * Handle 'entire object' case 1135 */ 1136 info.start_pindex = start; 1137 if (end == 0) { 1138 info.end_pindex = object->size - 1; 1139 } else { 1140 info.end_pindex = end - 1; 1141 } 1142 wholescan = (start == 0 && info.end_pindex == object->size - 1); 1143 info.limit = flags; 1144 info.pagerflags = pagerflags; 1145 info.object = object; 1146 1147 /* 1148 * If cleaning the entire object do a pass to mark the pages read-only. 1149 * If everything worked out ok, clear OBJ_WRITEABLE and 1150 * OBJ_MIGHTBEDIRTY. 1151 */ 1152 if (wholescan) { 1153 info.error = 0; 1154 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp, 1155 vm_object_page_clean_pass1, &info); 1156 if (info.error == 0) { 1157 vm_object_clear_flag(object, 1158 OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 1159 if (object->type == OBJT_VNODE && 1160 (vp = (struct vnode *)object->handle) != NULL) { 1161 if (vp->v_flag & VOBJDIRTY) 1162 vclrflags(vp, VOBJDIRTY); 1163 } 1164 } 1165 } 1166 1167 /* 1168 * Do a pass to clean all the dirty pages we find. 1169 */ 1170 do { 1171 info.error = 0; 1172 generation = object->generation; 1173 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp, 1174 vm_object_page_clean_pass2, &info); 1175 } while (info.error || generation != object->generation); 1176 1177 vm_object_clear_flag(object, OBJ_CLEANING); 1178 vm_object_drop(object); 1179 } 1180 1181 /* 1182 * The caller must hold the object. 1183 */ 1184 static 1185 int 1186 vm_object_page_clean_pass1(struct vm_page *p, void *data) 1187 { 1188 struct rb_vm_page_scan_info *info = data; 1189 1190 vm_page_flag_set(p, PG_CLEANCHK); 1191 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) { 1192 info->error = 1; 1193 } else if (vm_page_busy_try(p, FALSE) == 0) { 1194 vm_page_protect(p, VM_PROT_READ); /* must not block */ 1195 vm_page_wakeup(p); 1196 } else { 1197 info->error = 1; 1198 } 1199 lwkt_yield(); 1200 return(0); 1201 } 1202 1203 /* 1204 * The caller must hold the object 1205 */ 1206 static 1207 int 1208 vm_object_page_clean_pass2(struct vm_page *p, void *data) 1209 { 1210 struct rb_vm_page_scan_info *info = data; 1211 int generation; 1212 1213 /* 1214 * Do not mess with pages that were inserted after we started 1215 * the cleaning pass. 1216 */ 1217 if ((p->flags & PG_CLEANCHK) == 0) 1218 goto done; 1219 1220 generation = info->object->generation; 1221 vm_page_busy_wait(p, TRUE, "vpcwai"); 1222 if (p->object != info->object || 1223 info->object->generation != generation) { 1224 info->error = 1; 1225 vm_page_wakeup(p); 1226 goto done; 1227 } 1228 1229 /* 1230 * Before wasting time traversing the pmaps, check for trivial 1231 * cases where the page cannot be dirty. 1232 */ 1233 if (p->valid == 0 || (p->queue - p->pc) == PQ_CACHE) { 1234 KKASSERT((p->dirty & p->valid) == 0 && 1235 (p->flags & PG_NEED_COMMIT) == 0); 1236 vm_page_wakeup(p); 1237 goto done; 1238 } 1239 1240 /* 1241 * Check whether the page is dirty or not. The page has been set 1242 * to be read-only so the check will not race a user dirtying the 1243 * page. 1244 */ 1245 vm_page_test_dirty(p); 1246 if ((p->dirty & p->valid) == 0 && (p->flags & PG_NEED_COMMIT) == 0) { 1247 vm_page_flag_clear(p, PG_CLEANCHK); 1248 vm_page_wakeup(p); 1249 goto done; 1250 } 1251 1252 /* 1253 * If we have been asked to skip nosync pages and this is a 1254 * nosync page, skip it. Note that the object flags were 1255 * not cleared in this case (because pass1 will have returned an 1256 * error), so we do not have to set them. 1257 */ 1258 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) { 1259 vm_page_flag_clear(p, PG_CLEANCHK); 1260 vm_page_wakeup(p); 1261 goto done; 1262 } 1263 1264 /* 1265 * Flush as many pages as we can. PG_CLEANCHK will be cleared on 1266 * the pages that get successfully flushed. Set info->error if 1267 * we raced an object modification. 1268 */ 1269 vm_object_page_collect_flush(info->object, p, info->pagerflags); 1270 vm_wait_nominal(); 1271 done: 1272 lwkt_yield(); 1273 return(0); 1274 } 1275 1276 /* 1277 * Collect the specified page and nearby pages and flush them out. 1278 * The number of pages flushed is returned. The passed page is busied 1279 * by the caller and we are responsible for its disposition. 1280 * 1281 * The caller must hold the object. 1282 */ 1283 static void 1284 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags) 1285 { 1286 int error; 1287 int is; 1288 int ib; 1289 int i; 1290 int page_base; 1291 vm_pindex_t pi; 1292 vm_page_t ma[BLIST_MAX_ALLOC]; 1293 1294 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 1295 1296 pi = p->pindex; 1297 page_base = pi % BLIST_MAX_ALLOC; 1298 ma[page_base] = p; 1299 ib = page_base - 1; 1300 is = page_base + 1; 1301 1302 while (ib >= 0) { 1303 vm_page_t tp; 1304 1305 tp = vm_page_lookup_busy_try(object, pi - page_base + ib, 1306 TRUE, &error); 1307 if (error) 1308 break; 1309 if (tp == NULL) 1310 break; 1311 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 1312 (tp->flags & PG_CLEANCHK) == 0) { 1313 vm_page_wakeup(tp); 1314 break; 1315 } 1316 if ((tp->queue - tp->pc) == PQ_CACHE) { 1317 vm_page_flag_clear(tp, PG_CLEANCHK); 1318 vm_page_wakeup(tp); 1319 break; 1320 } 1321 vm_page_test_dirty(tp); 1322 if ((tp->dirty & tp->valid) == 0 && 1323 (tp->flags & PG_NEED_COMMIT) == 0) { 1324 vm_page_flag_clear(tp, PG_CLEANCHK); 1325 vm_page_wakeup(tp); 1326 break; 1327 } 1328 ma[ib] = tp; 1329 --ib; 1330 } 1331 ++ib; /* fixup */ 1332 1333 while (is < BLIST_MAX_ALLOC && 1334 pi - page_base + is < object->size) { 1335 vm_page_t tp; 1336 1337 tp = vm_page_lookup_busy_try(object, pi - page_base + is, 1338 TRUE, &error); 1339 if (error) 1340 break; 1341 if (tp == NULL) 1342 break; 1343 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 1344 (tp->flags & PG_CLEANCHK) == 0) { 1345 vm_page_wakeup(tp); 1346 break; 1347 } 1348 if ((tp->queue - tp->pc) == PQ_CACHE) { 1349 vm_page_flag_clear(tp, PG_CLEANCHK); 1350 vm_page_wakeup(tp); 1351 break; 1352 } 1353 vm_page_test_dirty(tp); 1354 if ((tp->dirty & tp->valid) == 0 && 1355 (tp->flags & PG_NEED_COMMIT) == 0) { 1356 vm_page_flag_clear(tp, PG_CLEANCHK); 1357 vm_page_wakeup(tp); 1358 break; 1359 } 1360 ma[is] = tp; 1361 ++is; 1362 } 1363 1364 /* 1365 * All pages in the ma[] array are busied now 1366 */ 1367 for (i = ib; i < is; ++i) { 1368 vm_page_flag_clear(ma[i], PG_CLEANCHK); 1369 vm_page_hold(ma[i]); /* XXX need this any more? */ 1370 } 1371 vm_pageout_flush(&ma[ib], is - ib, pagerflags); 1372 for (i = ib; i < is; ++i) /* XXX need this any more? */ 1373 vm_page_unhold(ma[i]); 1374 } 1375 1376 /* 1377 * Same as vm_object_pmap_copy, except range checking really 1378 * works, and is meant for small sections of an object. 1379 * 1380 * This code protects resident pages by making them read-only 1381 * and is typically called on a fork or split when a page 1382 * is converted to copy-on-write. 1383 * 1384 * NOTE: If the page is already at VM_PROT_NONE, calling 1385 * vm_page_protect will have no effect. 1386 */ 1387 void 1388 vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 1389 { 1390 vm_pindex_t idx; 1391 vm_page_t p; 1392 1393 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0) 1394 return; 1395 1396 vm_object_hold(object); 1397 for (idx = start; idx < end; idx++) { 1398 p = vm_page_lookup(object, idx); 1399 if (p == NULL) 1400 continue; 1401 vm_page_protect(p, VM_PROT_READ); 1402 } 1403 vm_object_drop(object); 1404 } 1405 1406 /* 1407 * Removes all physical pages in the specified object range from all 1408 * physical maps. 1409 * 1410 * The object must *not* be locked. 1411 */ 1412 1413 static int vm_object_pmap_remove_callback(vm_page_t p, void *data); 1414 1415 void 1416 vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 1417 { 1418 struct rb_vm_page_scan_info info; 1419 1420 if (object == NULL) 1421 return; 1422 info.start_pindex = start; 1423 info.end_pindex = end - 1; 1424 1425 vm_object_hold(object); 1426 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp, 1427 vm_object_pmap_remove_callback, &info); 1428 if (start == 0 && end == object->size) 1429 vm_object_clear_flag(object, OBJ_WRITEABLE); 1430 vm_object_drop(object); 1431 } 1432 1433 /* 1434 * The caller must hold the object 1435 */ 1436 static int 1437 vm_object_pmap_remove_callback(vm_page_t p, void *data __unused) 1438 { 1439 vm_page_protect(p, VM_PROT_NONE); 1440 return(0); 1441 } 1442 1443 /* 1444 * Implements the madvise function at the object/page level. 1445 * 1446 * MADV_WILLNEED (any object) 1447 * 1448 * Activate the specified pages if they are resident. 1449 * 1450 * MADV_DONTNEED (any object) 1451 * 1452 * Deactivate the specified pages if they are resident. 1453 * 1454 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, OBJ_ONEMAPPING only) 1455 * 1456 * Deactivate and clean the specified pages if they are 1457 * resident. This permits the process to reuse the pages 1458 * without faulting or the kernel to reclaim the pages 1459 * without I/O. 1460 * 1461 * No requirements. 1462 */ 1463 void 1464 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise) 1465 { 1466 vm_pindex_t end, tpindex; 1467 vm_object_t tobject; 1468 vm_object_t xobj; 1469 vm_page_t m; 1470 int error; 1471 1472 if (object == NULL) 1473 return; 1474 1475 end = pindex + count; 1476 1477 vm_object_hold(object); 1478 tobject = object; 1479 1480 /* 1481 * Locate and adjust resident pages 1482 */ 1483 for (; pindex < end; pindex += 1) { 1484 relookup: 1485 if (tobject != object) 1486 vm_object_drop(tobject); 1487 tobject = object; 1488 tpindex = pindex; 1489 shadowlookup: 1490 /* 1491 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages 1492 * and those pages must be OBJ_ONEMAPPING. 1493 */ 1494 if (advise == MADV_FREE) { 1495 if ((tobject->type != OBJT_DEFAULT && 1496 tobject->type != OBJT_SWAP) || 1497 (tobject->flags & OBJ_ONEMAPPING) == 0) { 1498 continue; 1499 } 1500 } 1501 1502 m = vm_page_lookup_busy_try(tobject, tpindex, TRUE, &error); 1503 1504 if (error) { 1505 vm_page_sleep_busy(m, TRUE, "madvpo"); 1506 goto relookup; 1507 } 1508 if (m == NULL) { 1509 /* 1510 * There may be swap even if there is no backing page 1511 */ 1512 if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 1513 swap_pager_freespace(tobject, tpindex, 1); 1514 1515 /* 1516 * next object 1517 */ 1518 while ((xobj = tobject->backing_object) != NULL) { 1519 KKASSERT(xobj != object); 1520 vm_object_hold(xobj); 1521 if (xobj == tobject->backing_object) 1522 break; 1523 vm_object_drop(xobj); 1524 } 1525 if (xobj == NULL) 1526 continue; 1527 tpindex += OFF_TO_IDX(tobject->backing_object_offset); 1528 if (tobject != object) { 1529 vm_object_lock_swap(); 1530 vm_object_drop(tobject); 1531 } 1532 tobject = xobj; 1533 goto shadowlookup; 1534 } 1535 1536 /* 1537 * If the page is not in a normal active state, we skip it. 1538 * If the page is not managed there are no page queues to 1539 * mess with. Things can break if we mess with pages in 1540 * any of the below states. 1541 */ 1542 if (m->wire_count || 1543 (m->flags & (PG_UNMANAGED | PG_NEED_COMMIT)) || 1544 m->valid != VM_PAGE_BITS_ALL 1545 ) { 1546 vm_page_wakeup(m); 1547 continue; 1548 } 1549 1550 /* 1551 * Theoretically once a page is known not to be busy, an 1552 * interrupt cannot come along and rip it out from under us. 1553 */ 1554 1555 if (advise == MADV_WILLNEED) { 1556 vm_page_activate(m); 1557 } else if (advise == MADV_DONTNEED) { 1558 vm_page_dontneed(m); 1559 } else if (advise == MADV_FREE) { 1560 /* 1561 * Mark the page clean. This will allow the page 1562 * to be freed up by the system. However, such pages 1563 * are often reused quickly by malloc()/free() 1564 * so we do not do anything that would cause 1565 * a page fault if we can help it. 1566 * 1567 * Specifically, we do not try to actually free 1568 * the page now nor do we try to put it in the 1569 * cache (which would cause a page fault on reuse). 1570 * 1571 * But we do make the page is freeable as we 1572 * can without actually taking the step of unmapping 1573 * it. 1574 */ 1575 pmap_clear_modify(m); 1576 m->dirty = 0; 1577 m->act_count = 0; 1578 vm_page_dontneed(m); 1579 if (tobject->type == OBJT_SWAP) 1580 swap_pager_freespace(tobject, tpindex, 1); 1581 } 1582 vm_page_wakeup(m); 1583 } 1584 if (tobject != object) 1585 vm_object_drop(tobject); 1586 vm_object_drop(object); 1587 } 1588 1589 /* 1590 * Create a new object which is backed by the specified existing object 1591 * range. Replace the pointer and offset that was pointing at the existing 1592 * object with the pointer/offset for the new object. 1593 * 1594 * No other requirements. 1595 */ 1596 void 1597 vm_object_shadow(vm_object_t *objectp, vm_ooffset_t *offset, vm_size_t length, 1598 int addref) 1599 { 1600 vm_object_t source; 1601 vm_object_t result; 1602 1603 source = *objectp; 1604 1605 /* 1606 * Don't create the new object if the old object isn't shared. 1607 * We have to chain wait before adding the reference to avoid 1608 * racing a collapse or deallocation. 1609 * 1610 * Add the additional ref to source here to avoid racing a later 1611 * collapse or deallocation. Clear the ONEMAPPING flag whether 1612 * addref is TRUE or not in this case because the original object 1613 * will be shadowed. 1614 */ 1615 if (source) { 1616 vm_object_hold(source); 1617 vm_object_chain_wait(source); 1618 if (source->ref_count == 1 && 1619 source->handle == NULL && 1620 (source->type == OBJT_DEFAULT || 1621 source->type == OBJT_SWAP)) { 1622 vm_object_drop(source); 1623 if (addref) { 1624 vm_object_reference_locked(source); 1625 vm_object_clear_flag(source, OBJ_ONEMAPPING); 1626 } 1627 return; 1628 } 1629 vm_object_reference_locked(source); 1630 vm_object_clear_flag(source, OBJ_ONEMAPPING); 1631 } 1632 1633 /* 1634 * Allocate a new object with the given length. The new object 1635 * is returned referenced but we may have to add another one. 1636 * If we are adding a second reference we must clear OBJ_ONEMAPPING. 1637 * (typically because the caller is about to clone a vm_map_entry). 1638 * 1639 * The source object currently has an extra reference to prevent 1640 * collapses into it while we mess with its shadow list, which 1641 * we will remove later in this routine. 1642 */ 1643 if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL) 1644 panic("vm_object_shadow: no object for shadowing"); 1645 vm_object_hold(result); 1646 if (addref) { 1647 vm_object_reference_locked(result); 1648 vm_object_clear_flag(result, OBJ_ONEMAPPING); 1649 } 1650 1651 /* 1652 * The new object shadows the source object. Chain wait before 1653 * adjusting shadow_count or the shadow list to avoid races. 1654 * 1655 * Try to optimize the result object's page color when shadowing 1656 * in order to maintain page coloring consistency in the combined 1657 * shadowed object. 1658 */ 1659 KKASSERT(result->backing_object == NULL); 1660 result->backing_object = source; 1661 if (source) { 1662 vm_object_chain_wait(source); 1663 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list); 1664 source->shadow_count++; 1665 source->generation++; 1666 /* cpu localization twist */ 1667 result->pg_color = (int)(intptr_t)curthread; 1668 } 1669 1670 /* 1671 * Adjust the return storage. Drop the ref on source before 1672 * returning. 1673 */ 1674 result->backing_object_offset = *offset; 1675 vm_object_drop(result); 1676 *offset = 0; 1677 if (source) { 1678 vm_object_deallocate_locked(source); 1679 vm_object_drop(source); 1680 } 1681 1682 /* 1683 * Return the new things 1684 */ 1685 *objectp = result; 1686 } 1687 1688 #define OBSC_TEST_ALL_SHADOWED 0x0001 1689 #define OBSC_COLLAPSE_NOWAIT 0x0002 1690 #define OBSC_COLLAPSE_WAIT 0x0004 1691 1692 static int vm_object_backing_scan_callback(vm_page_t p, void *data); 1693 1694 /* 1695 * The caller must hold the object. 1696 */ 1697 static __inline int 1698 vm_object_backing_scan(vm_object_t object, vm_object_t backing_object, int op) 1699 { 1700 struct rb_vm_page_scan_info info; 1701 1702 vm_object_assert_held(object); 1703 vm_object_assert_held(backing_object); 1704 1705 KKASSERT(backing_object == object->backing_object); 1706 info.backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 1707 1708 /* 1709 * Initial conditions 1710 */ 1711 if (op & OBSC_TEST_ALL_SHADOWED) { 1712 /* 1713 * We do not want to have to test for the existence of 1714 * swap pages in the backing object. XXX but with the 1715 * new swapper this would be pretty easy to do. 1716 * 1717 * XXX what about anonymous MAP_SHARED memory that hasn't 1718 * been ZFOD faulted yet? If we do not test for this, the 1719 * shadow test may succeed! XXX 1720 */ 1721 if (backing_object->type != OBJT_DEFAULT) 1722 return(0); 1723 } 1724 if (op & OBSC_COLLAPSE_WAIT) { 1725 KKASSERT((backing_object->flags & OBJ_DEAD) == 0); 1726 vm_object_set_flag(backing_object, OBJ_DEAD); 1727 lwkt_gettoken(&vmobj_token); 1728 TAILQ_REMOVE(&vm_object_list, backing_object, object_list); 1729 vm_object_count--; 1730 lwkt_reltoken(&vmobj_token); 1731 vm_object_dead_wakeup(backing_object); 1732 } 1733 1734 /* 1735 * Our scan. We have to retry if a negative error code is returned, 1736 * otherwise 0 or 1 will be returned in info.error. 0 Indicates that 1737 * the scan had to be stopped because the parent does not completely 1738 * shadow the child. 1739 */ 1740 info.object = object; 1741 info.backing_object = backing_object; 1742 info.limit = op; 1743 do { 1744 info.error = 1; 1745 vm_page_rb_tree_RB_SCAN(&backing_object->rb_memq, NULL, 1746 vm_object_backing_scan_callback, 1747 &info); 1748 } while (info.error < 0); 1749 1750 return(info.error); 1751 } 1752 1753 /* 1754 * The caller must hold the object. 1755 */ 1756 static int 1757 vm_object_backing_scan_callback(vm_page_t p, void *data) 1758 { 1759 struct rb_vm_page_scan_info *info = data; 1760 vm_object_t backing_object; 1761 vm_object_t object; 1762 vm_pindex_t pindex; 1763 vm_pindex_t new_pindex; 1764 vm_pindex_t backing_offset_index; 1765 int op; 1766 1767 pindex = p->pindex; 1768 new_pindex = pindex - info->backing_offset_index; 1769 op = info->limit; 1770 object = info->object; 1771 backing_object = info->backing_object; 1772 backing_offset_index = info->backing_offset_index; 1773 1774 if (op & OBSC_TEST_ALL_SHADOWED) { 1775 vm_page_t pp; 1776 1777 /* 1778 * Ignore pages outside the parent object's range 1779 * and outside the parent object's mapping of the 1780 * backing object. 1781 * 1782 * note that we do not busy the backing object's 1783 * page. 1784 */ 1785 if (pindex < backing_offset_index || 1786 new_pindex >= object->size 1787 ) { 1788 return(0); 1789 } 1790 1791 /* 1792 * See if the parent has the page or if the parent's 1793 * object pager has the page. If the parent has the 1794 * page but the page is not valid, the parent's 1795 * object pager must have the page. 1796 * 1797 * If this fails, the parent does not completely shadow 1798 * the object and we might as well give up now. 1799 */ 1800 pp = vm_page_lookup(object, new_pindex); 1801 if ((pp == NULL || pp->valid == 0) && 1802 !vm_pager_has_page(object, new_pindex) 1803 ) { 1804 info->error = 0; /* problemo */ 1805 return(-1); /* stop the scan */ 1806 } 1807 } 1808 1809 /* 1810 * Check for busy page. Note that we may have lost (p) when we 1811 * possibly blocked above. 1812 */ 1813 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) { 1814 vm_page_t pp; 1815 1816 if (vm_page_busy_try(p, TRUE)) { 1817 if (op & OBSC_COLLAPSE_NOWAIT) { 1818 return(0); 1819 } else { 1820 /* 1821 * If we slept, anything could have 1822 * happened. Ask that the scan be restarted. 1823 * 1824 * Since the object is marked dead, the 1825 * backing offset should not have changed. 1826 */ 1827 vm_page_sleep_busy(p, TRUE, "vmocol"); 1828 info->error = -1; 1829 return(-1); 1830 } 1831 } 1832 1833 /* 1834 * If (p) is no longer valid restart the scan. 1835 */ 1836 if (p->object != backing_object || p->pindex != pindex) { 1837 kprintf("vm_object_backing_scan: Warning: page " 1838 "%p ripped out from under us\n", p); 1839 vm_page_wakeup(p); 1840 info->error = -1; 1841 return(-1); 1842 } 1843 1844 if (op & OBSC_COLLAPSE_NOWAIT) { 1845 if (p->valid == 0 || 1846 p->wire_count || 1847 (p->flags & PG_NEED_COMMIT)) { 1848 vm_page_wakeup(p); 1849 return(0); 1850 } 1851 } else { 1852 /* XXX what if p->valid == 0 , hold_count, etc? */ 1853 } 1854 1855 KASSERT( 1856 p->object == backing_object, 1857 ("vm_object_qcollapse(): object mismatch") 1858 ); 1859 1860 /* 1861 * Destroy any associated swap 1862 */ 1863 if (backing_object->type == OBJT_SWAP) 1864 swap_pager_freespace(backing_object, p->pindex, 1); 1865 1866 if ( 1867 p->pindex < backing_offset_index || 1868 new_pindex >= object->size 1869 ) { 1870 /* 1871 * Page is out of the parent object's range, we 1872 * can simply destroy it. 1873 */ 1874 vm_page_protect(p, VM_PROT_NONE); 1875 vm_page_free(p); 1876 return(0); 1877 } 1878 1879 pp = vm_page_lookup(object, new_pindex); 1880 if (pp != NULL || vm_pager_has_page(object, new_pindex)) { 1881 /* 1882 * page already exists in parent OR swap exists 1883 * for this location in the parent. Destroy 1884 * the original page from the backing object. 1885 * 1886 * Leave the parent's page alone 1887 */ 1888 vm_page_protect(p, VM_PROT_NONE); 1889 vm_page_free(p); 1890 return(0); 1891 } 1892 1893 /* 1894 * Page does not exist in parent, rename the 1895 * page from the backing object to the main object. 1896 * 1897 * If the page was mapped to a process, it can remain 1898 * mapped through the rename. 1899 */ 1900 if ((p->queue - p->pc) == PQ_CACHE) 1901 vm_page_deactivate(p); 1902 1903 vm_page_rename(p, object, new_pindex); 1904 vm_page_wakeup(p); 1905 /* page automatically made dirty by rename */ 1906 } 1907 return(0); 1908 } 1909 1910 /* 1911 * This version of collapse allows the operation to occur earlier and 1912 * when paging_in_progress is true for an object... This is not a complete 1913 * operation, but should plug 99.9% of the rest of the leaks. 1914 * 1915 * The caller must hold the object and backing_object and both must be 1916 * chainlocked. 1917 * 1918 * (only called from vm_object_collapse) 1919 */ 1920 static void 1921 vm_object_qcollapse(vm_object_t object, vm_object_t backing_object) 1922 { 1923 if (backing_object->ref_count == 1) { 1924 backing_object->ref_count += 2; 1925 vm_object_backing_scan(object, backing_object, 1926 OBSC_COLLAPSE_NOWAIT); 1927 backing_object->ref_count -= 2; 1928 } 1929 } 1930 1931 /* 1932 * Collapse an object with the object backing it. Pages in the backing 1933 * object are moved into the parent, and the backing object is deallocated. 1934 * Any conflict is resolved in favor of the parent's existing pages. 1935 * 1936 * object must be held and chain-locked on call. 1937 * 1938 * The caller must have an extra ref on object to prevent a race from 1939 * destroying it during the collapse. 1940 */ 1941 void 1942 vm_object_collapse(vm_object_t object, struct vm_object_dealloc_list **dlistp) 1943 { 1944 struct vm_object_dealloc_list *dlist = NULL; 1945 vm_object_t backing_object; 1946 1947 /* 1948 * Only one thread is attempting a collapse at any given moment. 1949 * There are few restrictions for (object) that callers of this 1950 * function check so reentrancy is likely. 1951 */ 1952 KKASSERT(object != NULL); 1953 vm_object_assert_held(object); 1954 KKASSERT(object->flags & OBJ_CHAINLOCK); 1955 1956 for (;;) { 1957 vm_object_t bbobj; 1958 int dodealloc; 1959 1960 /* 1961 * We have to hold the backing object, check races. 1962 */ 1963 while ((backing_object = object->backing_object) != NULL) { 1964 vm_object_hold(backing_object); 1965 if (backing_object == object->backing_object) 1966 break; 1967 vm_object_drop(backing_object); 1968 } 1969 1970 /* 1971 * No backing object? Nothing to collapse then. 1972 */ 1973 if (backing_object == NULL) 1974 break; 1975 1976 /* 1977 * You can't collapse with a non-default/non-swap object. 1978 */ 1979 if (backing_object->type != OBJT_DEFAULT && 1980 backing_object->type != OBJT_SWAP) { 1981 vm_object_drop(backing_object); 1982 backing_object = NULL; 1983 break; 1984 } 1985 1986 /* 1987 * Chain-lock the backing object too because if we 1988 * successfully merge its pages into the top object we 1989 * will collapse backing_object->backing_object as the 1990 * new backing_object. Re-check that it is still our 1991 * backing object. 1992 */ 1993 vm_object_chain_acquire(backing_object); 1994 if (backing_object != object->backing_object) { 1995 vm_object_chain_release(backing_object); 1996 vm_object_drop(backing_object); 1997 continue; 1998 } 1999 2000 /* 2001 * we check the backing object first, because it is most likely 2002 * not collapsable. 2003 */ 2004 if (backing_object->handle != NULL || 2005 (backing_object->type != OBJT_DEFAULT && 2006 backing_object->type != OBJT_SWAP) || 2007 (backing_object->flags & OBJ_DEAD) || 2008 object->handle != NULL || 2009 (object->type != OBJT_DEFAULT && 2010 object->type != OBJT_SWAP) || 2011 (object->flags & OBJ_DEAD)) { 2012 break; 2013 } 2014 2015 /* 2016 * If paging is in progress we can't do a normal collapse. 2017 */ 2018 if ( 2019 object->paging_in_progress != 0 || 2020 backing_object->paging_in_progress != 0 2021 ) { 2022 vm_object_qcollapse(object, backing_object); 2023 break; 2024 } 2025 2026 /* 2027 * We know that we can either collapse the backing object (if 2028 * the parent is the only reference to it) or (perhaps) have 2029 * the parent bypass the object if the parent happens to shadow 2030 * all the resident pages in the entire backing object. 2031 * 2032 * This is ignoring pager-backed pages such as swap pages. 2033 * vm_object_backing_scan fails the shadowing test in this 2034 * case. 2035 */ 2036 if (backing_object->ref_count == 1) { 2037 /* 2038 * If there is exactly one reference to the backing 2039 * object, we can collapse it into the parent. 2040 */ 2041 KKASSERT(object->backing_object == backing_object); 2042 vm_object_backing_scan(object, backing_object, 2043 OBSC_COLLAPSE_WAIT); 2044 2045 /* 2046 * Move the pager from backing_object to object. 2047 */ 2048 if (backing_object->type == OBJT_SWAP) { 2049 vm_object_pip_add(backing_object, 1); 2050 2051 /* 2052 * scrap the paging_offset junk and do a 2053 * discrete copy. This also removes major 2054 * assumptions about how the swap-pager 2055 * works from where it doesn't belong. The 2056 * new swapper is able to optimize the 2057 * destroy-source case. 2058 */ 2059 vm_object_pip_add(object, 1); 2060 swap_pager_copy(backing_object, object, 2061 OFF_TO_IDX(object->backing_object_offset), 2062 TRUE); 2063 vm_object_pip_wakeup(object); 2064 vm_object_pip_wakeup(backing_object); 2065 } 2066 2067 /* 2068 * Object now shadows whatever backing_object did. 2069 * Remove object from backing_object's shadow_list. 2070 */ 2071 LIST_REMOVE(object, shadow_list); 2072 KKASSERT(object->backing_object == backing_object); 2073 backing_object->shadow_count--; 2074 backing_object->generation++; 2075 2076 /* 2077 * backing_object->backing_object moves from within 2078 * backing_object to within object. 2079 */ 2080 while ((bbobj = backing_object->backing_object) != NULL) { 2081 vm_object_hold(bbobj); 2082 if (bbobj == backing_object->backing_object) 2083 break; 2084 vm_object_drop(bbobj); 2085 } 2086 if (bbobj) { 2087 LIST_REMOVE(backing_object, shadow_list); 2088 bbobj->shadow_count--; 2089 bbobj->generation++; 2090 backing_object->backing_object = NULL; 2091 } 2092 object->backing_object = bbobj; 2093 if (bbobj) { 2094 LIST_INSERT_HEAD(&bbobj->shadow_head, 2095 object, shadow_list); 2096 bbobj->shadow_count++; 2097 bbobj->generation++; 2098 } 2099 2100 object->backing_object_offset += 2101 backing_object->backing_object_offset; 2102 2103 vm_object_drop(bbobj); 2104 2105 /* 2106 * Discard the old backing_object. Nothing should be 2107 * able to ref it, other than a vm_map_split(), 2108 * and vm_map_split() will stall on our chain lock. 2109 * And we control the parent so it shouldn't be 2110 * possible for it to go away either. 2111 * 2112 * Since the backing object has no pages, no pager 2113 * left, and no object references within it, all 2114 * that is necessary is to dispose of it. 2115 */ 2116 KASSERT(backing_object->ref_count == 1, 2117 ("backing_object %p was somehow " 2118 "re-referenced during collapse!", 2119 backing_object)); 2120 KASSERT(RB_EMPTY(&backing_object->rb_memq), 2121 ("backing_object %p somehow has left " 2122 "over pages during collapse!", 2123 backing_object)); 2124 2125 /* 2126 * The object can be destroyed. 2127 * 2128 * XXX just fall through and dodealloc instead 2129 * of forcing destruction? 2130 */ 2131 --backing_object->ref_count; 2132 if ((backing_object->flags & OBJ_DEAD) == 0) 2133 vm_object_terminate(backing_object); 2134 object_collapses++; 2135 dodealloc = 0; 2136 } else { 2137 /* 2138 * If we do not entirely shadow the backing object, 2139 * there is nothing we can do so we give up. 2140 */ 2141 if (vm_object_backing_scan(object, backing_object, 2142 OBSC_TEST_ALL_SHADOWED) == 0) { 2143 break; 2144 } 2145 2146 /* 2147 * bbobj is backing_object->backing_object. Since 2148 * object completely shadows backing_object we can 2149 * bypass it and become backed by bbobj instead. 2150 */ 2151 while ((bbobj = backing_object->backing_object) != NULL) { 2152 vm_object_hold(bbobj); 2153 if (bbobj == backing_object->backing_object) 2154 break; 2155 vm_object_drop(bbobj); 2156 } 2157 2158 /* 2159 * Make object shadow bbobj instead of backing_object. 2160 * Remove object from backing_object's shadow list. 2161 * 2162 * Deallocating backing_object will not remove 2163 * it, since its reference count is at least 2. 2164 */ 2165 KKASSERT(object->backing_object == backing_object); 2166 LIST_REMOVE(object, shadow_list); 2167 backing_object->shadow_count--; 2168 backing_object->generation++; 2169 2170 /* 2171 * Add a ref to bbobj, bbobj now shadows object. 2172 * 2173 * NOTE: backing_object->backing_object still points 2174 * to bbobj. That relationship remains intact 2175 * because backing_object has > 1 ref, so 2176 * someone else is pointing to it (hence why 2177 * we can't collapse it into object and can 2178 * only handle the all-shadowed bypass case). 2179 */ 2180 if (bbobj) { 2181 vm_object_chain_wait(bbobj); 2182 vm_object_reference_locked(bbobj); 2183 LIST_INSERT_HEAD(&bbobj->shadow_head, 2184 object, shadow_list); 2185 bbobj->shadow_count++; 2186 bbobj->generation++; 2187 object->backing_object_offset += 2188 backing_object->backing_object_offset; 2189 object->backing_object = bbobj; 2190 vm_object_drop(bbobj); 2191 } else { 2192 object->backing_object = NULL; 2193 } 2194 2195 /* 2196 * Drop the reference count on backing_object. To 2197 * handle ref_count races properly we can't assume 2198 * that the ref_count is still at least 2 so we 2199 * have to actually call vm_object_deallocate() 2200 * (after clearing the chainlock). 2201 */ 2202 object_bypasses++; 2203 dodealloc = 1; 2204 } 2205 2206 /* 2207 * Ok, we want to loop on the new object->bbobj association, 2208 * possibly collapsing it further. However if dodealloc is 2209 * non-zero we have to deallocate the backing_object which 2210 * itself can potentially undergo a collapse, creating a 2211 * recursion depth issue with the LWKT token subsystem. 2212 * 2213 * In the case where we must deallocate the backing_object 2214 * it is possible now that the backing_object has a single 2215 * shadow count on some other object (not represented here 2216 * as yet), since it no longer shadows us. Thus when we 2217 * call vm_object_deallocate() it may attempt to collapse 2218 * itself into its remaining parent. 2219 */ 2220 if (dodealloc) { 2221 struct vm_object_dealloc_list *dtmp; 2222 2223 vm_object_chain_release(backing_object); 2224 vm_object_unlock(backing_object); 2225 /* backing_object remains held */ 2226 2227 /* 2228 * Auto-deallocation list for caller convenience. 2229 */ 2230 if (dlistp == NULL) 2231 dlistp = &dlist; 2232 2233 dtmp = kmalloc(sizeof(*dtmp), M_TEMP, M_WAITOK); 2234 dtmp->object = backing_object; 2235 dtmp->next = *dlistp; 2236 *dlistp = dtmp; 2237 } else { 2238 vm_object_chain_release(backing_object); 2239 vm_object_drop(backing_object); 2240 } 2241 /* backing_object = NULL; not needed */ 2242 /* loop */ 2243 } 2244 2245 /* 2246 * Clean up any left over backing_object 2247 */ 2248 if (backing_object) { 2249 vm_object_chain_release(backing_object); 2250 vm_object_drop(backing_object); 2251 } 2252 2253 /* 2254 * Clean up any auto-deallocation list. This is a convenience 2255 * for top-level callers so they don't have to pass &dlist. 2256 * Do not clean up any caller-passed dlistp, the caller will 2257 * do that. 2258 */ 2259 if (dlist) 2260 vm_object_deallocate_list(&dlist); 2261 2262 } 2263 2264 /* 2265 * vm_object_collapse() may collect additional objects in need of 2266 * deallocation. This routine deallocates these objects. The 2267 * deallocation itself can trigger additional collapses (which the 2268 * deallocate function takes care of). This procedure is used to 2269 * reduce procedural recursion since these vm_object shadow chains 2270 * can become quite long. 2271 */ 2272 void 2273 vm_object_deallocate_list(struct vm_object_dealloc_list **dlistp) 2274 { 2275 struct vm_object_dealloc_list *dlist; 2276 2277 while ((dlist = *dlistp) != NULL) { 2278 *dlistp = dlist->next; 2279 vm_object_lock(dlist->object); 2280 vm_object_deallocate_locked(dlist->object); 2281 vm_object_drop(dlist->object); 2282 kfree(dlist, M_TEMP); 2283 } 2284 } 2285 2286 /* 2287 * Removes all physical pages in the specified object range from the 2288 * object's list of pages. 2289 * 2290 * No requirements. 2291 */ 2292 static int vm_object_page_remove_callback(vm_page_t p, void *data); 2293 2294 void 2295 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 2296 boolean_t clean_only) 2297 { 2298 struct rb_vm_page_scan_info info; 2299 int all; 2300 2301 /* 2302 * Degenerate cases and assertions 2303 */ 2304 vm_object_hold(object); 2305 if (object == NULL || 2306 (object->resident_page_count == 0 && object->swblock_count == 0)) { 2307 vm_object_drop(object); 2308 return; 2309 } 2310 KASSERT(object->type != OBJT_PHYS, 2311 ("attempt to remove pages from a physical object")); 2312 2313 /* 2314 * Indicate that paging is occuring on the object 2315 */ 2316 vm_object_pip_add(object, 1); 2317 2318 /* 2319 * Figure out the actual removal range and whether we are removing 2320 * the entire contents of the object or not. If removing the entire 2321 * contents, be sure to get all pages, even those that might be 2322 * beyond the end of the object. 2323 */ 2324 info.start_pindex = start; 2325 if (end == 0) 2326 info.end_pindex = (vm_pindex_t)-1; 2327 else 2328 info.end_pindex = end - 1; 2329 info.limit = clean_only; 2330 all = (start == 0 && info.end_pindex >= object->size - 1); 2331 2332 /* 2333 * Loop until we are sure we have gotten them all. 2334 */ 2335 do { 2336 info.error = 0; 2337 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp, 2338 vm_object_page_remove_callback, &info); 2339 } while (info.error); 2340 2341 /* 2342 * Remove any related swap if throwing away pages, or for 2343 * non-swap objects (the swap is a clean copy in that case). 2344 */ 2345 if (object->type != OBJT_SWAP || clean_only == FALSE) { 2346 if (all) 2347 swap_pager_freespace_all(object); 2348 else 2349 swap_pager_freespace(object, info.start_pindex, 2350 info.end_pindex - info.start_pindex + 1); 2351 } 2352 2353 /* 2354 * Cleanup 2355 */ 2356 vm_object_pip_wakeup(object); 2357 vm_object_drop(object); 2358 } 2359 2360 /* 2361 * The caller must hold the object 2362 */ 2363 static int 2364 vm_object_page_remove_callback(vm_page_t p, void *data) 2365 { 2366 struct rb_vm_page_scan_info *info = data; 2367 2368 if (vm_page_busy_try(p, TRUE)) { 2369 vm_page_sleep_busy(p, TRUE, "vmopar"); 2370 info->error = 1; 2371 return(0); 2372 } 2373 2374 /* 2375 * Wired pages cannot be destroyed, but they can be invalidated 2376 * and we do so if clean_only (limit) is not set. 2377 * 2378 * WARNING! The page may be wired due to being part of a buffer 2379 * cache buffer, and the buffer might be marked B_CACHE. 2380 * This is fine as part of a truncation but VFSs must be 2381 * sure to fix the buffer up when re-extending the file. 2382 * 2383 * NOTE! PG_NEED_COMMIT is ignored. 2384 */ 2385 if (p->wire_count != 0) { 2386 vm_page_protect(p, VM_PROT_NONE); 2387 if (info->limit == 0) 2388 p->valid = 0; 2389 vm_page_wakeup(p); 2390 return(0); 2391 } 2392 2393 /* 2394 * limit is our clean_only flag. If set and the page is dirty or 2395 * requires a commit, do not free it. If set and the page is being 2396 * held by someone, do not free it. 2397 */ 2398 if (info->limit && p->valid) { 2399 vm_page_test_dirty(p); 2400 if ((p->valid & p->dirty) || (p->flags & PG_NEED_COMMIT)) { 2401 vm_page_wakeup(p); 2402 return(0); 2403 } 2404 #if 0 2405 if (p->hold_count) { 2406 vm_page_wakeup(p); 2407 return(0); 2408 } 2409 #endif 2410 } 2411 2412 /* 2413 * Destroy the page 2414 */ 2415 vm_page_protect(p, VM_PROT_NONE); 2416 vm_page_free(p); 2417 return(0); 2418 } 2419 2420 /* 2421 * Coalesces two objects backing up adjoining regions of memory into a 2422 * single object. 2423 * 2424 * returns TRUE if objects were combined. 2425 * 2426 * NOTE: Only works at the moment if the second object is NULL - 2427 * if it's not, which object do we lock first? 2428 * 2429 * Parameters: 2430 * prev_object First object to coalesce 2431 * prev_offset Offset into prev_object 2432 * next_object Second object into coalesce 2433 * next_offset Offset into next_object 2434 * 2435 * prev_size Size of reference to prev_object 2436 * next_size Size of reference to next_object 2437 * 2438 * The caller does not need to hold (prev_object) but must have a stable 2439 * pointer to it (typically by holding the vm_map locked). 2440 */ 2441 boolean_t 2442 vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex, 2443 vm_size_t prev_size, vm_size_t next_size) 2444 { 2445 vm_pindex_t next_pindex; 2446 2447 if (prev_object == NULL) 2448 return (TRUE); 2449 2450 vm_object_hold(prev_object); 2451 2452 if (prev_object->type != OBJT_DEFAULT && 2453 prev_object->type != OBJT_SWAP) { 2454 vm_object_drop(prev_object); 2455 return (FALSE); 2456 } 2457 2458 /* 2459 * Try to collapse the object first 2460 */ 2461 vm_object_chain_acquire(prev_object); 2462 vm_object_collapse(prev_object, NULL); 2463 2464 /* 2465 * Can't coalesce if: . more than one reference . paged out . shadows 2466 * another object . has a copy elsewhere (any of which mean that the 2467 * pages not mapped to prev_entry may be in use anyway) 2468 */ 2469 2470 if (prev_object->backing_object != NULL) { 2471 vm_object_chain_release(prev_object); 2472 vm_object_drop(prev_object); 2473 return (FALSE); 2474 } 2475 2476 prev_size >>= PAGE_SHIFT; 2477 next_size >>= PAGE_SHIFT; 2478 next_pindex = prev_pindex + prev_size; 2479 2480 if ((prev_object->ref_count > 1) && 2481 (prev_object->size != next_pindex)) { 2482 vm_object_chain_release(prev_object); 2483 vm_object_drop(prev_object); 2484 return (FALSE); 2485 } 2486 2487 /* 2488 * Remove any pages that may still be in the object from a previous 2489 * deallocation. 2490 */ 2491 if (next_pindex < prev_object->size) { 2492 vm_object_page_remove(prev_object, 2493 next_pindex, 2494 next_pindex + next_size, FALSE); 2495 if (prev_object->type == OBJT_SWAP) 2496 swap_pager_freespace(prev_object, 2497 next_pindex, next_size); 2498 } 2499 2500 /* 2501 * Extend the object if necessary. 2502 */ 2503 if (next_pindex + next_size > prev_object->size) 2504 prev_object->size = next_pindex + next_size; 2505 2506 vm_object_chain_release(prev_object); 2507 vm_object_drop(prev_object); 2508 return (TRUE); 2509 } 2510 2511 /* 2512 * Make the object writable and flag is being possibly dirty. 2513 * 2514 * The caller must hold the object. XXX called from vm_page_dirty(), 2515 * There is currently no requirement to hold the object. 2516 */ 2517 void 2518 vm_object_set_writeable_dirty(vm_object_t object) 2519 { 2520 struct vnode *vp; 2521 2522 /*vm_object_assert_held(object);*/ 2523 /* 2524 * Avoid contention in vm fault path by checking the state before 2525 * issuing an atomic op on it. 2526 */ 2527 if ((object->flags & (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) != 2528 (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) { 2529 vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 2530 } 2531 if (object->type == OBJT_VNODE && 2532 (vp = (struct vnode *)object->handle) != NULL) { 2533 if ((vp->v_flag & VOBJDIRTY) == 0) { 2534 vsetflags(vp, VOBJDIRTY); 2535 } 2536 } 2537 } 2538 2539 #include "opt_ddb.h" 2540 #ifdef DDB 2541 #include <sys/kernel.h> 2542 2543 #include <sys/cons.h> 2544 2545 #include <ddb/ddb.h> 2546 2547 static int _vm_object_in_map (vm_map_t map, vm_object_t object, 2548 vm_map_entry_t entry); 2549 static int vm_object_in_map (vm_object_t object); 2550 2551 /* 2552 * The caller must hold the object. 2553 */ 2554 static int 2555 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2556 { 2557 vm_map_t tmpm; 2558 vm_map_entry_t tmpe; 2559 vm_object_t obj, nobj; 2560 int entcount; 2561 2562 if (map == 0) 2563 return 0; 2564 if (entry == 0) { 2565 tmpe = map->header.next; 2566 entcount = map->nentries; 2567 while (entcount-- && (tmpe != &map->header)) { 2568 if( _vm_object_in_map(map, object, tmpe)) { 2569 return 1; 2570 } 2571 tmpe = tmpe->next; 2572 } 2573 return (0); 2574 } 2575 switch(entry->maptype) { 2576 case VM_MAPTYPE_SUBMAP: 2577 tmpm = entry->object.sub_map; 2578 tmpe = tmpm->header.next; 2579 entcount = tmpm->nentries; 2580 while (entcount-- && tmpe != &tmpm->header) { 2581 if( _vm_object_in_map(tmpm, object, tmpe)) { 2582 return 1; 2583 } 2584 tmpe = tmpe->next; 2585 } 2586 break; 2587 case VM_MAPTYPE_NORMAL: 2588 case VM_MAPTYPE_VPAGETABLE: 2589 obj = entry->object.vm_object; 2590 while (obj) { 2591 if (obj == object) { 2592 if (obj != entry->object.vm_object) 2593 vm_object_drop(obj); 2594 return 1; 2595 } 2596 while ((nobj = obj->backing_object) != NULL) { 2597 vm_object_hold(nobj); 2598 if (nobj == obj->backing_object) 2599 break; 2600 vm_object_drop(nobj); 2601 } 2602 if (obj != entry->object.vm_object) { 2603 if (nobj) 2604 vm_object_lock_swap(); 2605 vm_object_drop(obj); 2606 } 2607 obj = nobj; 2608 } 2609 break; 2610 default: 2611 break; 2612 } 2613 return 0; 2614 } 2615 2616 static int vm_object_in_map_callback(struct proc *p, void *data); 2617 2618 struct vm_object_in_map_info { 2619 vm_object_t object; 2620 int rv; 2621 }; 2622 2623 /* 2624 * Debugging only 2625 */ 2626 static int 2627 vm_object_in_map(vm_object_t object) 2628 { 2629 struct vm_object_in_map_info info; 2630 2631 info.rv = 0; 2632 info.object = object; 2633 2634 allproc_scan(vm_object_in_map_callback, &info); 2635 if (info.rv) 2636 return 1; 2637 if( _vm_object_in_map(&kernel_map, object, 0)) 2638 return 1; 2639 if( _vm_object_in_map(&pager_map, object, 0)) 2640 return 1; 2641 if( _vm_object_in_map(&buffer_map, object, 0)) 2642 return 1; 2643 return 0; 2644 } 2645 2646 /* 2647 * Debugging only 2648 */ 2649 static int 2650 vm_object_in_map_callback(struct proc *p, void *data) 2651 { 2652 struct vm_object_in_map_info *info = data; 2653 2654 if (p->p_vmspace) { 2655 if (_vm_object_in_map(&p->p_vmspace->vm_map, info->object, 0)) { 2656 info->rv = 1; 2657 return -1; 2658 } 2659 } 2660 return (0); 2661 } 2662 2663 DB_SHOW_COMMAND(vmochk, vm_object_check) 2664 { 2665 vm_object_t object; 2666 2667 /* 2668 * make sure that internal objs are in a map somewhere 2669 * and none have zero ref counts. 2670 */ 2671 for (object = TAILQ_FIRST(&vm_object_list); 2672 object != NULL; 2673 object = TAILQ_NEXT(object, object_list)) { 2674 if (object->type == OBJT_MARKER) 2675 continue; 2676 if (object->handle == NULL && 2677 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 2678 if (object->ref_count == 0) { 2679 db_printf("vmochk: internal obj has zero ref count: %ld\n", 2680 (long)object->size); 2681 } 2682 if (!vm_object_in_map(object)) { 2683 db_printf( 2684 "vmochk: internal obj is not in a map: " 2685 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2686 object->ref_count, (u_long)object->size, 2687 (u_long)object->size, 2688 (void *)object->backing_object); 2689 } 2690 } 2691 } 2692 } 2693 2694 /* 2695 * Debugging only 2696 */ 2697 DB_SHOW_COMMAND(object, vm_object_print_static) 2698 { 2699 /* XXX convert args. */ 2700 vm_object_t object = (vm_object_t)addr; 2701 boolean_t full = have_addr; 2702 2703 vm_page_t p; 2704 2705 /* XXX count is an (unused) arg. Avoid shadowing it. */ 2706 #define count was_count 2707 2708 int count; 2709 2710 if (object == NULL) 2711 return; 2712 2713 db_iprintf( 2714 "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n", 2715 object, (int)object->type, (u_long)object->size, 2716 object->resident_page_count, object->ref_count, object->flags); 2717 /* 2718 * XXX no %qd in kernel. Truncate object->backing_object_offset. 2719 */ 2720 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n", 2721 object->shadow_count, 2722 object->backing_object ? object->backing_object->ref_count : 0, 2723 object->backing_object, (long)object->backing_object_offset); 2724 2725 if (!full) 2726 return; 2727 2728 db_indent += 2; 2729 count = 0; 2730 RB_FOREACH(p, vm_page_rb_tree, &object->rb_memq) { 2731 if (count == 0) 2732 db_iprintf("memory:="); 2733 else if (count == 6) { 2734 db_printf("\n"); 2735 db_iprintf(" ..."); 2736 count = 0; 2737 } else 2738 db_printf(","); 2739 count++; 2740 2741 db_printf("(off=0x%lx,page=0x%lx)", 2742 (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p)); 2743 } 2744 if (count != 0) 2745 db_printf("\n"); 2746 db_indent -= 2; 2747 } 2748 2749 /* XXX. */ 2750 #undef count 2751 2752 /* 2753 * XXX need this non-static entry for calling from vm_map_print. 2754 * 2755 * Debugging only 2756 */ 2757 void 2758 vm_object_print(/* db_expr_t */ long addr, 2759 boolean_t have_addr, 2760 /* db_expr_t */ long count, 2761 char *modif) 2762 { 2763 vm_object_print_static(addr, have_addr, count, modif); 2764 } 2765 2766 /* 2767 * Debugging only 2768 */ 2769 DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 2770 { 2771 vm_object_t object; 2772 int nl = 0; 2773 int c; 2774 for (object = TAILQ_FIRST(&vm_object_list); 2775 object != NULL; 2776 object = TAILQ_NEXT(object, object_list)) { 2777 vm_pindex_t idx, fidx; 2778 vm_pindex_t osize; 2779 vm_paddr_t pa = -1, padiff; 2780 int rcount; 2781 vm_page_t m; 2782 2783 if (object->type == OBJT_MARKER) 2784 continue; 2785 db_printf("new object: %p\n", (void *)object); 2786 if ( nl > 18) { 2787 c = cngetc(); 2788 if (c != ' ') 2789 return; 2790 nl = 0; 2791 } 2792 nl++; 2793 rcount = 0; 2794 fidx = 0; 2795 osize = object->size; 2796 if (osize > 128) 2797 osize = 128; 2798 for (idx = 0; idx < osize; idx++) { 2799 m = vm_page_lookup(object, idx); 2800 if (m == NULL) { 2801 if (rcount) { 2802 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2803 (long)fidx, rcount, (long)pa); 2804 if ( nl > 18) { 2805 c = cngetc(); 2806 if (c != ' ') 2807 return; 2808 nl = 0; 2809 } 2810 nl++; 2811 rcount = 0; 2812 } 2813 continue; 2814 } 2815 2816 2817 if (rcount && 2818 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 2819 ++rcount; 2820 continue; 2821 } 2822 if (rcount) { 2823 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m); 2824 padiff >>= PAGE_SHIFT; 2825 padiff &= PQ_L2_MASK; 2826 if (padiff == 0) { 2827 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE; 2828 ++rcount; 2829 continue; 2830 } 2831 db_printf(" index(%ld)run(%d)pa(0x%lx)", 2832 (long)fidx, rcount, (long)pa); 2833 db_printf("pd(%ld)\n", (long)padiff); 2834 if ( nl > 18) { 2835 c = cngetc(); 2836 if (c != ' ') 2837 return; 2838 nl = 0; 2839 } 2840 nl++; 2841 } 2842 fidx = idx; 2843 pa = VM_PAGE_TO_PHYS(m); 2844 rcount = 1; 2845 } 2846 if (rcount) { 2847 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2848 (long)fidx, rcount, (long)pa); 2849 if ( nl > 18) { 2850 c = cngetc(); 2851 if (c != ' ') 2852 return; 2853 nl = 0; 2854 } 2855 nl++; 2856 } 2857 } 2858 } 2859 #endif /* DDB */ 2860