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