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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94 37 * 38 * 39 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40 * All rights reserved. 41 * 42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 43 * 44 * Permission to use, copy, modify and distribute this software and 45 * its documentation is hereby granted, provided that both the copyright 46 * notice and this permission notice appear in all copies of the 47 * software, derivative works or modified versions, and any portions 48 * thereof, and that both notices appear in supporting documentation. 49 * 50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 53 * 54 * Carnegie Mellon requests users of this software to return to 55 * 56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 57 * School of Computer Science 58 * Carnegie Mellon University 59 * Pittsburgh PA 15213-3890 60 * 61 * any improvements or extensions that they make and grant Carnegie the 62 * rights to redistribute these changes. 63 * 64 * $FreeBSD: src/sys/vm/vm_object.c,v 1.171.2.8 2003/05/26 19:17:56 alc Exp $ 65 */ 66 67 /* 68 * Virtual memory object module. 69 */ 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/proc.h> /* for curproc, pageproc */ 74 #include <sys/thread.h> 75 #include <sys/vnode.h> 76 #include <sys/vmmeter.h> 77 #include <sys/mman.h> 78 #include <sys/mount.h> 79 #include <sys/kernel.h> 80 #include <sys/sysctl.h> 81 #include <sys/refcount.h> 82 83 #include <vm/vm.h> 84 #include <vm/vm_param.h> 85 #include <vm/pmap.h> 86 #include <vm/vm_map.h> 87 #include <vm/vm_object.h> 88 #include <vm/vm_page.h> 89 #include <vm/vm_pageout.h> 90 #include <vm/vm_pager.h> 91 #include <vm/swap_pager.h> 92 #include <vm/vm_kern.h> 93 #include <vm/vm_extern.h> 94 #include <vm/vm_zone.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 extern int vm_pageout_page_count; 136 137 static long object_collapses; 138 static long object_bypasses; 139 static int next_index; 140 static vm_zone_t obj_zone; 141 static struct vm_zone obj_zone_store; 142 #define VM_OBJECTS_INIT 256 143 static struct vm_object vm_objects_init[VM_OBJECTS_INIT]; 144 145 /* 146 * Misc low level routines 147 */ 148 static void 149 vm_object_lock_init(vm_object_t obj) 150 { 151 #if defined(DEBUG_LOCKS) 152 int i; 153 154 obj->debug_hold_bitmap = 0; 155 obj->debug_hold_ovfl = 0; 156 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) { 157 obj->debug_hold_thrs[i] = NULL; 158 obj->debug_hold_file[i] = NULL; 159 obj->debug_hold_line[i] = 0; 160 } 161 #endif 162 } 163 164 void 165 vm_object_lock_swap(void) 166 { 167 lwkt_token_swap(); 168 } 169 170 void 171 vm_object_lock(vm_object_t obj) 172 { 173 lwkt_gettoken(&obj->token); 174 } 175 176 /* 177 * Returns TRUE on sucesss 178 */ 179 static int 180 vm_object_lock_try(vm_object_t obj) 181 { 182 return(lwkt_trytoken(&obj->token)); 183 } 184 185 void 186 vm_object_lock_shared(vm_object_t obj) 187 { 188 lwkt_gettoken_shared(&obj->token); 189 } 190 191 void 192 vm_object_unlock(vm_object_t obj) 193 { 194 lwkt_reltoken(&obj->token); 195 } 196 197 static __inline void 198 vm_object_assert_held(vm_object_t obj) 199 { 200 ASSERT_LWKT_TOKEN_HELD(&obj->token); 201 } 202 203 void 204 #ifndef DEBUG_LOCKS 205 vm_object_hold(vm_object_t obj) 206 #else 207 debugvm_object_hold(vm_object_t obj, char *file, int line) 208 #endif 209 { 210 KKASSERT(obj != NULL); 211 212 /* 213 * Object must be held (object allocation is stable due to callers 214 * context, typically already holding the token on a parent object) 215 * prior to potentially blocking on the lock, otherwise the object 216 * can get ripped away from us. 217 */ 218 refcount_acquire(&obj->hold_count); 219 vm_object_lock(obj); 220 221 #if defined(DEBUG_LOCKS) 222 int i; 223 u_int mask; 224 225 for (;;) { 226 mask = ~obj->debug_hold_bitmap; 227 cpu_ccfence(); 228 if (mask == 0xFFFFFFFFU) { 229 if (obj->debug_hold_ovfl == 0) 230 obj->debug_hold_ovfl = 1; 231 break; 232 } 233 i = ffs(mask) - 1; 234 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask, 235 ~mask | (1 << i))) { 236 obj->debug_hold_bitmap |= (1 << i); 237 obj->debug_hold_thrs[i] = curthread; 238 obj->debug_hold_file[i] = file; 239 obj->debug_hold_line[i] = line; 240 break; 241 } 242 } 243 #endif 244 } 245 246 int 247 #ifndef DEBUG_LOCKS 248 vm_object_hold_try(vm_object_t obj) 249 #else 250 debugvm_object_hold_try(vm_object_t obj, char *file, int line) 251 #endif 252 { 253 KKASSERT(obj != NULL); 254 255 /* 256 * Object must be held (object allocation is stable due to callers 257 * context, typically already holding the token on a parent object) 258 * prior to potentially blocking on the lock, otherwise the object 259 * can get ripped away from us. 260 */ 261 refcount_acquire(&obj->hold_count); 262 if (vm_object_lock_try(obj) == 0) { 263 if (refcount_release(&obj->hold_count)) { 264 if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD)) 265 zfree(obj_zone, obj); 266 } 267 return(0); 268 } 269 270 #if defined(DEBUG_LOCKS) 271 int i; 272 u_int mask; 273 274 for (;;) { 275 mask = ~obj->debug_hold_bitmap; 276 cpu_ccfence(); 277 if (mask == 0xFFFFFFFFU) { 278 if (obj->debug_hold_ovfl == 0) 279 obj->debug_hold_ovfl = 1; 280 break; 281 } 282 i = ffs(mask) - 1; 283 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask, 284 ~mask | (1 << i))) { 285 obj->debug_hold_bitmap |= (1 << i); 286 obj->debug_hold_thrs[i] = curthread; 287 obj->debug_hold_file[i] = file; 288 obj->debug_hold_line[i] = line; 289 break; 290 } 291 } 292 #endif 293 return(1); 294 } 295 296 void 297 #ifndef DEBUG_LOCKS 298 vm_object_hold_shared(vm_object_t obj) 299 #else 300 debugvm_object_hold_shared(vm_object_t obj, char *file, int line) 301 #endif 302 { 303 KKASSERT(obj != NULL); 304 305 /* 306 * Object must be held (object allocation is stable due to callers 307 * context, typically already holding the token on a parent object) 308 * prior to potentially blocking on the lock, otherwise the object 309 * can get ripped away from us. 310 */ 311 refcount_acquire(&obj->hold_count); 312 vm_object_lock_shared(obj); 313 314 #if defined(DEBUG_LOCKS) 315 int i; 316 u_int mask; 317 318 for (;;) { 319 mask = ~obj->debug_hold_bitmap; 320 cpu_ccfence(); 321 if (mask == 0xFFFFFFFFU) { 322 if (obj->debug_hold_ovfl == 0) 323 obj->debug_hold_ovfl = 1; 324 break; 325 } 326 i = ffs(mask) - 1; 327 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask, 328 ~mask | (1 << i))) { 329 obj->debug_hold_bitmap |= (1 << i); 330 obj->debug_hold_thrs[i] = curthread; 331 obj->debug_hold_file[i] = file; 332 obj->debug_hold_line[i] = line; 333 break; 334 } 335 } 336 #endif 337 } 338 339 /* 340 * Obtain either a shared or exclusive lock on VM object 341 * based on whether this is a terminal vnode object or not. 342 */ 343 int 344 #ifndef DEBUG_LOCKS 345 vm_object_hold_maybe_shared(vm_object_t obj) 346 #else 347 debugvm_object_hold_maybe_shared(vm_object_t obj, char *file, int line) 348 #endif 349 { 350 if (vm_shared_fault && 351 obj->type == OBJT_VNODE && 352 obj->backing_object == NULL) { 353 vm_object_hold_shared(obj); 354 return(1); 355 } else { 356 vm_object_hold(obj); 357 return(0); 358 } 359 } 360 361 /* 362 * Drop the token and hold_count on the object. 363 */ 364 void 365 vm_object_drop(vm_object_t obj) 366 { 367 if (obj == NULL) 368 return; 369 370 #if defined(DEBUG_LOCKS) 371 int found = 0; 372 int i; 373 374 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) { 375 if ((obj->debug_hold_bitmap & (1 << i)) && 376 (obj->debug_hold_thrs[i] == curthread)) { 377 obj->debug_hold_bitmap &= ~(1 << i); 378 obj->debug_hold_thrs[i] = NULL; 379 obj->debug_hold_file[i] = NULL; 380 obj->debug_hold_line[i] = 0; 381 found = 1; 382 break; 383 } 384 } 385 386 if (found == 0 && obj->debug_hold_ovfl == 0) 387 panic("vm_object: attempt to drop hold on non-self-held obj"); 388 #endif 389 390 /* 391 * No new holders should be possible once we drop hold_count 1->0 as 392 * there is no longer any way to reference the object. 393 */ 394 KKASSERT(obj->hold_count > 0); 395 if (refcount_release(&obj->hold_count)) { 396 if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD)) { 397 vm_object_unlock(obj); 398 zfree(obj_zone, obj); 399 } else { 400 vm_object_unlock(obj); 401 } 402 } else { 403 vm_object_unlock(obj); 404 } 405 } 406 407 /* 408 * Initialize a freshly allocated object, returning a held object. 409 * 410 * Used only by vm_object_allocate() and zinitna(). 411 * 412 * No requirements. 413 */ 414 void 415 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object) 416 { 417 int incr; 418 419 RB_INIT(&object->rb_memq); 420 LIST_INIT(&object->shadow_head); 421 lwkt_token_init(&object->token, "vmobj"); 422 423 object->type = type; 424 object->size = size; 425 object->ref_count = 1; 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_flag & VOBJDIRTY) 1164 vclrflags(vp, VOBJDIRTY); 1165 } 1166 } 1167 } 1168 1169 /* 1170 * Do a pass to clean all the dirty pages we find. 1171 */ 1172 do { 1173 info.error = 0; 1174 generation = object->generation; 1175 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp, 1176 vm_object_page_clean_pass2, &info); 1177 } while (info.error || generation != object->generation); 1178 1179 vm_object_clear_flag(object, OBJ_CLEANING); 1180 vm_object_drop(object); 1181 } 1182 1183 /* 1184 * The caller must hold the object. 1185 */ 1186 static 1187 int 1188 vm_object_page_clean_pass1(struct vm_page *p, void *data) 1189 { 1190 struct rb_vm_page_scan_info *info = data; 1191 1192 vm_page_flag_set(p, PG_CLEANCHK); 1193 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) { 1194 info->error = 1; 1195 } else if (vm_page_busy_try(p, FALSE) == 0) { 1196 vm_page_protect(p, VM_PROT_READ); /* must not block */ 1197 vm_page_wakeup(p); 1198 } else { 1199 info->error = 1; 1200 } 1201 lwkt_yield(); 1202 return(0); 1203 } 1204 1205 /* 1206 * The caller must hold the object 1207 */ 1208 static 1209 int 1210 vm_object_page_clean_pass2(struct vm_page *p, void *data) 1211 { 1212 struct rb_vm_page_scan_info *info = data; 1213 int generation; 1214 1215 /* 1216 * Do not mess with pages that were inserted after we started 1217 * the cleaning pass. 1218 */ 1219 if ((p->flags & PG_CLEANCHK) == 0) 1220 goto done; 1221 1222 generation = info->object->generation; 1223 vm_page_busy_wait(p, TRUE, "vpcwai"); 1224 if (p->object != info->object || 1225 info->object->generation != generation) { 1226 info->error = 1; 1227 vm_page_wakeup(p); 1228 goto done; 1229 } 1230 1231 /* 1232 * Before wasting time traversing the pmaps, check for trivial 1233 * cases where the page cannot be dirty. 1234 */ 1235 if (p->valid == 0 || (p->queue - p->pc) == PQ_CACHE) { 1236 KKASSERT((p->dirty & p->valid) == 0 && 1237 (p->flags & PG_NEED_COMMIT) == 0); 1238 vm_page_wakeup(p); 1239 goto done; 1240 } 1241 1242 /* 1243 * Check whether the page is dirty or not. The page has been set 1244 * to be read-only so the check will not race a user dirtying the 1245 * page. 1246 */ 1247 vm_page_test_dirty(p); 1248 if ((p->dirty & p->valid) == 0 && (p->flags & PG_NEED_COMMIT) == 0) { 1249 vm_page_flag_clear(p, PG_CLEANCHK); 1250 vm_page_wakeup(p); 1251 goto done; 1252 } 1253 1254 /* 1255 * If we have been asked to skip nosync pages and this is a 1256 * nosync page, skip it. Note that the object flags were 1257 * not cleared in this case (because pass1 will have returned an 1258 * error), so we do not have to set them. 1259 */ 1260 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) { 1261 vm_page_flag_clear(p, PG_CLEANCHK); 1262 vm_page_wakeup(p); 1263 goto done; 1264 } 1265 1266 /* 1267 * Flush as many pages as we can. PG_CLEANCHK will be cleared on 1268 * the pages that get successfully flushed. Set info->error if 1269 * we raced an object modification. 1270 */ 1271 vm_object_page_collect_flush(info->object, p, info->pagerflags); 1272 vm_wait_nominal(); 1273 done: 1274 lwkt_yield(); 1275 return(0); 1276 } 1277 1278 /* 1279 * Collect the specified page and nearby pages and flush them out. 1280 * The number of pages flushed is returned. The passed page is busied 1281 * by the caller and we are responsible for its disposition. 1282 * 1283 * The caller must hold the object. 1284 */ 1285 static void 1286 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags) 1287 { 1288 int runlen; 1289 int error; 1290 int maxf; 1291 int chkb; 1292 int maxb; 1293 int i; 1294 vm_pindex_t pi; 1295 vm_page_t maf[vm_pageout_page_count]; 1296 vm_page_t mab[vm_pageout_page_count]; 1297 vm_page_t ma[vm_pageout_page_count]; 1298 1299 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 1300 1301 pi = p->pindex; 1302 1303 maxf = 0; 1304 for(i = 1; i < vm_pageout_page_count; i++) { 1305 vm_page_t tp; 1306 1307 tp = vm_page_lookup_busy_try(object, pi + i, TRUE, &error); 1308 if (error) 1309 break; 1310 if (tp == NULL) 1311 break; 1312 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 1313 (tp->flags & PG_CLEANCHK) == 0) { 1314 vm_page_wakeup(tp); 1315 break; 1316 } 1317 if ((tp->queue - tp->pc) == PQ_CACHE) { 1318 vm_page_flag_clear(tp, PG_CLEANCHK); 1319 vm_page_wakeup(tp); 1320 break; 1321 } 1322 vm_page_test_dirty(tp); 1323 if ((tp->dirty & tp->valid) == 0 && 1324 (tp->flags & PG_NEED_COMMIT) == 0) { 1325 vm_page_flag_clear(tp, PG_CLEANCHK); 1326 vm_page_wakeup(tp); 1327 break; 1328 } 1329 maf[i - 1] = tp; 1330 maxf++; 1331 } 1332 1333 maxb = 0; 1334 chkb = vm_pageout_page_count - maxf; 1335 /* 1336 * NOTE: chkb can be 0 1337 */ 1338 for(i = 1; chkb && i < chkb; i++) { 1339 vm_page_t tp; 1340 1341 tp = vm_page_lookup_busy_try(object, pi - i, TRUE, &error); 1342 if (error) 1343 break; 1344 if (tp == NULL) 1345 break; 1346 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 1347 (tp->flags & PG_CLEANCHK) == 0) { 1348 vm_page_wakeup(tp); 1349 break; 1350 } 1351 if ((tp->queue - tp->pc) == PQ_CACHE) { 1352 vm_page_flag_clear(tp, PG_CLEANCHK); 1353 vm_page_wakeup(tp); 1354 break; 1355 } 1356 vm_page_test_dirty(tp); 1357 if ((tp->dirty & tp->valid) == 0 && 1358 (tp->flags & PG_NEED_COMMIT) == 0) { 1359 vm_page_flag_clear(tp, PG_CLEANCHK); 1360 vm_page_wakeup(tp); 1361 break; 1362 } 1363 mab[i - 1] = tp; 1364 maxb++; 1365 } 1366 1367 /* 1368 * All pages in the maf[] and mab[] array are busied. 1369 */ 1370 for (i = 0; i < maxb; i++) { 1371 int index = (maxb - i) - 1; 1372 ma[index] = mab[i]; 1373 vm_page_flag_clear(ma[index], PG_CLEANCHK); 1374 } 1375 vm_page_flag_clear(p, PG_CLEANCHK); 1376 ma[maxb] = p; 1377 for(i = 0; i < maxf; i++) { 1378 int index = (maxb + i) + 1; 1379 ma[index] = maf[i]; 1380 vm_page_flag_clear(ma[index], PG_CLEANCHK); 1381 } 1382 runlen = maxb + maxf + 1; 1383 1384 for (i = 0; i < runlen; i++) /* XXX need this any more? */ 1385 vm_page_hold(ma[i]); 1386 1387 vm_pageout_flush(ma, runlen, pagerflags); 1388 1389 for (i = 0; i < runlen; i++) /* XXX need this any more? */ 1390 vm_page_unhold(ma[i]); 1391 } 1392 1393 /* 1394 * Same as vm_object_pmap_copy, except range checking really 1395 * works, and is meant for small sections of an object. 1396 * 1397 * This code protects resident pages by making them read-only 1398 * and is typically called on a fork or split when a page 1399 * is converted to copy-on-write. 1400 * 1401 * NOTE: If the page is already at VM_PROT_NONE, calling 1402 * vm_page_protect will have no effect. 1403 */ 1404 void 1405 vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 1406 { 1407 vm_pindex_t idx; 1408 vm_page_t p; 1409 1410 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0) 1411 return; 1412 1413 vm_object_hold(object); 1414 for (idx = start; idx < end; idx++) { 1415 p = vm_page_lookup(object, idx); 1416 if (p == NULL) 1417 continue; 1418 vm_page_protect(p, VM_PROT_READ); 1419 } 1420 vm_object_drop(object); 1421 } 1422 1423 /* 1424 * Removes all physical pages in the specified object range from all 1425 * physical maps. 1426 * 1427 * The object must *not* be locked. 1428 */ 1429 1430 static int vm_object_pmap_remove_callback(vm_page_t p, void *data); 1431 1432 void 1433 vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 1434 { 1435 struct rb_vm_page_scan_info info; 1436 1437 if (object == NULL) 1438 return; 1439 info.start_pindex = start; 1440 info.end_pindex = end - 1; 1441 1442 vm_object_hold(object); 1443 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp, 1444 vm_object_pmap_remove_callback, &info); 1445 if (start == 0 && end == object->size) 1446 vm_object_clear_flag(object, OBJ_WRITEABLE); 1447 vm_object_drop(object); 1448 } 1449 1450 /* 1451 * The caller must hold the object 1452 */ 1453 static int 1454 vm_object_pmap_remove_callback(vm_page_t p, void *data __unused) 1455 { 1456 vm_page_protect(p, VM_PROT_NONE); 1457 return(0); 1458 } 1459 1460 /* 1461 * Implements the madvise function at the object/page level. 1462 * 1463 * MADV_WILLNEED (any object) 1464 * 1465 * Activate the specified pages if they are resident. 1466 * 1467 * MADV_DONTNEED (any object) 1468 * 1469 * Deactivate the specified pages if they are resident. 1470 * 1471 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, OBJ_ONEMAPPING only) 1472 * 1473 * Deactivate and clean the specified pages if they are 1474 * resident. This permits the process to reuse the pages 1475 * without faulting or the kernel to reclaim the pages 1476 * without I/O. 1477 * 1478 * No requirements. 1479 */ 1480 void 1481 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise) 1482 { 1483 vm_pindex_t end, tpindex; 1484 vm_object_t tobject; 1485 vm_object_t xobj; 1486 vm_page_t m; 1487 int error; 1488 1489 if (object == NULL) 1490 return; 1491 1492 end = pindex + count; 1493 1494 vm_object_hold(object); 1495 tobject = object; 1496 1497 /* 1498 * Locate and adjust resident pages 1499 */ 1500 for (; pindex < end; pindex += 1) { 1501 relookup: 1502 if (tobject != object) 1503 vm_object_drop(tobject); 1504 tobject = object; 1505 tpindex = pindex; 1506 shadowlookup: 1507 /* 1508 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages 1509 * and those pages must be OBJ_ONEMAPPING. 1510 */ 1511 if (advise == MADV_FREE) { 1512 if ((tobject->type != OBJT_DEFAULT && 1513 tobject->type != OBJT_SWAP) || 1514 (tobject->flags & OBJ_ONEMAPPING) == 0) { 1515 continue; 1516 } 1517 } 1518 1519 m = vm_page_lookup_busy_try(tobject, tpindex, TRUE, &error); 1520 1521 if (error) { 1522 vm_page_sleep_busy(m, TRUE, "madvpo"); 1523 goto relookup; 1524 } 1525 if (m == NULL) { 1526 /* 1527 * There may be swap even if there is no backing page 1528 */ 1529 if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 1530 swap_pager_freespace(tobject, tpindex, 1); 1531 1532 /* 1533 * next object 1534 */ 1535 while ((xobj = tobject->backing_object) != NULL) { 1536 KKASSERT(xobj != object); 1537 vm_object_hold(xobj); 1538 if (xobj == tobject->backing_object) 1539 break; 1540 vm_object_drop(xobj); 1541 } 1542 if (xobj == NULL) 1543 continue; 1544 tpindex += OFF_TO_IDX(tobject->backing_object_offset); 1545 if (tobject != object) { 1546 vm_object_lock_swap(); 1547 vm_object_drop(tobject); 1548 } 1549 tobject = xobj; 1550 goto shadowlookup; 1551 } 1552 1553 /* 1554 * If the page is not in a normal active state, we skip it. 1555 * If the page is not managed there are no page queues to 1556 * mess with. Things can break if we mess with pages in 1557 * any of the below states. 1558 */ 1559 if (m->wire_count || 1560 (m->flags & (PG_UNMANAGED | PG_NEED_COMMIT)) || 1561 m->valid != VM_PAGE_BITS_ALL 1562 ) { 1563 vm_page_wakeup(m); 1564 continue; 1565 } 1566 1567 /* 1568 * Theoretically once a page is known not to be busy, an 1569 * interrupt cannot come along and rip it out from under us. 1570 */ 1571 1572 if (advise == MADV_WILLNEED) { 1573 vm_page_activate(m); 1574 } else if (advise == MADV_DONTNEED) { 1575 vm_page_dontneed(m); 1576 } else if (advise == MADV_FREE) { 1577 /* 1578 * Mark the page clean. This will allow the page 1579 * to be freed up by the system. However, such pages 1580 * are often reused quickly by malloc()/free() 1581 * so we do not do anything that would cause 1582 * a page fault if we can help it. 1583 * 1584 * Specifically, we do not try to actually free 1585 * the page now nor do we try to put it in the 1586 * cache (which would cause a page fault on reuse). 1587 * 1588 * But we do make the page is freeable as we 1589 * can without actually taking the step of unmapping 1590 * it. 1591 */ 1592 pmap_clear_modify(m); 1593 m->dirty = 0; 1594 m->act_count = 0; 1595 vm_page_dontneed(m); 1596 if (tobject->type == OBJT_SWAP) 1597 swap_pager_freespace(tobject, tpindex, 1); 1598 } 1599 vm_page_wakeup(m); 1600 } 1601 if (tobject != object) 1602 vm_object_drop(tobject); 1603 vm_object_drop(object); 1604 } 1605 1606 /* 1607 * Create a new object which is backed by the specified existing object 1608 * range. Replace the pointer and offset that was pointing at the existing 1609 * object with the pointer/offset for the new object. 1610 * 1611 * No other requirements. 1612 */ 1613 void 1614 vm_object_shadow(vm_object_t *objectp, vm_ooffset_t *offset, vm_size_t length, 1615 int addref) 1616 { 1617 vm_object_t source; 1618 vm_object_t result; 1619 1620 source = *objectp; 1621 1622 /* 1623 * Don't create the new object if the old object isn't shared. 1624 * We have to chain wait before adding the reference to avoid 1625 * racing a collapse or deallocation. 1626 * 1627 * Add the additional ref to source here to avoid racing a later 1628 * collapse or deallocation. Clear the ONEMAPPING flag whether 1629 * addref is TRUE or not in this case because the original object 1630 * will be shadowed. 1631 */ 1632 if (source) { 1633 vm_object_hold(source); 1634 vm_object_chain_wait(source); 1635 if (source->ref_count == 1 && 1636 source->handle == NULL && 1637 (source->type == OBJT_DEFAULT || 1638 source->type == OBJT_SWAP)) { 1639 vm_object_drop(source); 1640 if (addref) { 1641 vm_object_reference_locked(source); 1642 vm_object_clear_flag(source, OBJ_ONEMAPPING); 1643 } 1644 return; 1645 } 1646 vm_object_reference_locked(source); 1647 vm_object_clear_flag(source, OBJ_ONEMAPPING); 1648 } 1649 1650 /* 1651 * Allocate a new object with the given length. The new object 1652 * is returned referenced but we may have to add another one. 1653 * If we are adding a second reference we must clear OBJ_ONEMAPPING. 1654 * (typically because the caller is about to clone a vm_map_entry). 1655 * 1656 * The source object currently has an extra reference to prevent 1657 * collapses into it while we mess with its shadow list, which 1658 * we will remove later in this routine. 1659 */ 1660 if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL) 1661 panic("vm_object_shadow: no object for shadowing"); 1662 vm_object_hold(result); 1663 if (addref) { 1664 vm_object_reference_locked(result); 1665 vm_object_clear_flag(result, OBJ_ONEMAPPING); 1666 } 1667 1668 /* 1669 * The new object shadows the source object. Chain wait before 1670 * adjusting shadow_count or the shadow list to avoid races. 1671 * 1672 * Try to optimize the result object's page color when shadowing 1673 * in order to maintain page coloring consistency in the combined 1674 * shadowed object. 1675 */ 1676 KKASSERT(result->backing_object == NULL); 1677 result->backing_object = source; 1678 if (source) { 1679 vm_object_chain_wait(source); 1680 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list); 1681 source->shadow_count++; 1682 source->generation++; 1683 /* cpu localization twist */ 1684 result->pg_color = (int)(intptr_t)curthread; 1685 } 1686 1687 /* 1688 * Adjust the return storage. Drop the ref on source before 1689 * returning. 1690 */ 1691 result->backing_object_offset = *offset; 1692 vm_object_drop(result); 1693 *offset = 0; 1694 if (source) { 1695 vm_object_deallocate_locked(source); 1696 vm_object_drop(source); 1697 } 1698 1699 /* 1700 * Return the new things 1701 */ 1702 *objectp = result; 1703 } 1704 1705 #define OBSC_TEST_ALL_SHADOWED 0x0001 1706 #define OBSC_COLLAPSE_NOWAIT 0x0002 1707 #define OBSC_COLLAPSE_WAIT 0x0004 1708 1709 static int vm_object_backing_scan_callback(vm_page_t p, void *data); 1710 1711 /* 1712 * The caller must hold the object. 1713 */ 1714 static __inline int 1715 vm_object_backing_scan(vm_object_t object, vm_object_t backing_object, int op) 1716 { 1717 struct rb_vm_page_scan_info info; 1718 1719 vm_object_assert_held(object); 1720 vm_object_assert_held(backing_object); 1721 1722 KKASSERT(backing_object == object->backing_object); 1723 info.backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 1724 1725 /* 1726 * Initial conditions 1727 */ 1728 if (op & OBSC_TEST_ALL_SHADOWED) { 1729 /* 1730 * We do not want to have to test for the existence of 1731 * swap pages in the backing object. XXX but with the 1732 * new swapper this would be pretty easy to do. 1733 * 1734 * XXX what about anonymous MAP_SHARED memory that hasn't 1735 * been ZFOD faulted yet? If we do not test for this, the 1736 * shadow test may succeed! XXX 1737 */ 1738 if (backing_object->type != OBJT_DEFAULT) 1739 return(0); 1740 } 1741 if (op & OBSC_COLLAPSE_WAIT) { 1742 KKASSERT((backing_object->flags & OBJ_DEAD) == 0); 1743 vm_object_set_flag(backing_object, OBJ_DEAD); 1744 lwkt_gettoken(&vmobj_token); 1745 TAILQ_REMOVE(&vm_object_list, backing_object, object_list); 1746 vm_object_count--; 1747 lwkt_reltoken(&vmobj_token); 1748 vm_object_dead_wakeup(backing_object); 1749 } 1750 1751 /* 1752 * Our scan. We have to retry if a negative error code is returned, 1753 * otherwise 0 or 1 will be returned in info.error. 0 Indicates that 1754 * the scan had to be stopped because the parent does not completely 1755 * shadow the child. 1756 */ 1757 info.object = object; 1758 info.backing_object = backing_object; 1759 info.limit = op; 1760 do { 1761 info.error = 1; 1762 vm_page_rb_tree_RB_SCAN(&backing_object->rb_memq, NULL, 1763 vm_object_backing_scan_callback, 1764 &info); 1765 } while (info.error < 0); 1766 1767 return(info.error); 1768 } 1769 1770 /* 1771 * The caller must hold the object. 1772 */ 1773 static int 1774 vm_object_backing_scan_callback(vm_page_t p, void *data) 1775 { 1776 struct rb_vm_page_scan_info *info = data; 1777 vm_object_t backing_object; 1778 vm_object_t object; 1779 vm_pindex_t pindex; 1780 vm_pindex_t new_pindex; 1781 vm_pindex_t backing_offset_index; 1782 int op; 1783 1784 pindex = p->pindex; 1785 new_pindex = pindex - info->backing_offset_index; 1786 op = info->limit; 1787 object = info->object; 1788 backing_object = info->backing_object; 1789 backing_offset_index = info->backing_offset_index; 1790 1791 if (op & OBSC_TEST_ALL_SHADOWED) { 1792 vm_page_t pp; 1793 1794 /* 1795 * Ignore pages outside the parent object's range 1796 * and outside the parent object's mapping of the 1797 * backing object. 1798 * 1799 * note that we do not busy the backing object's 1800 * page. 1801 */ 1802 if (pindex < backing_offset_index || 1803 new_pindex >= object->size 1804 ) { 1805 return(0); 1806 } 1807 1808 /* 1809 * See if the parent has the page or if the parent's 1810 * object pager has the page. If the parent has the 1811 * page but the page is not valid, the parent's 1812 * object pager must have the page. 1813 * 1814 * If this fails, the parent does not completely shadow 1815 * the object and we might as well give up now. 1816 */ 1817 pp = vm_page_lookup(object, new_pindex); 1818 if ((pp == NULL || pp->valid == 0) && 1819 !vm_pager_has_page(object, new_pindex) 1820 ) { 1821 info->error = 0; /* problemo */ 1822 return(-1); /* stop the scan */ 1823 } 1824 } 1825 1826 /* 1827 * Check for busy page. Note that we may have lost (p) when we 1828 * possibly blocked above. 1829 */ 1830 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) { 1831 vm_page_t pp; 1832 1833 if (vm_page_busy_try(p, TRUE)) { 1834 if (op & OBSC_COLLAPSE_NOWAIT) { 1835 return(0); 1836 } else { 1837 /* 1838 * If we slept, anything could have 1839 * happened. Ask that the scan be restarted. 1840 * 1841 * Since the object is marked dead, the 1842 * backing offset should not have changed. 1843 */ 1844 vm_page_sleep_busy(p, TRUE, "vmocol"); 1845 info->error = -1; 1846 return(-1); 1847 } 1848 } 1849 1850 /* 1851 * If (p) is no longer valid restart the scan. 1852 */ 1853 if (p->object != backing_object || p->pindex != pindex) { 1854 kprintf("vm_object_backing_scan: Warning: page " 1855 "%p ripped out from under us\n", p); 1856 vm_page_wakeup(p); 1857 info->error = -1; 1858 return(-1); 1859 } 1860 1861 if (op & OBSC_COLLAPSE_NOWAIT) { 1862 if (p->valid == 0 || 1863 p->wire_count || 1864 (p->flags & PG_NEED_COMMIT)) { 1865 vm_page_wakeup(p); 1866 return(0); 1867 } 1868 } else { 1869 /* XXX what if p->valid == 0 , hold_count, etc? */ 1870 } 1871 1872 KASSERT( 1873 p->object == backing_object, 1874 ("vm_object_qcollapse(): object mismatch") 1875 ); 1876 1877 /* 1878 * Destroy any associated swap 1879 */ 1880 if (backing_object->type == OBJT_SWAP) 1881 swap_pager_freespace(backing_object, p->pindex, 1); 1882 1883 if ( 1884 p->pindex < backing_offset_index || 1885 new_pindex >= object->size 1886 ) { 1887 /* 1888 * Page is out of the parent object's range, we 1889 * can simply destroy it. 1890 */ 1891 vm_page_protect(p, VM_PROT_NONE); 1892 vm_page_free(p); 1893 return(0); 1894 } 1895 1896 pp = vm_page_lookup(object, new_pindex); 1897 if (pp != NULL || vm_pager_has_page(object, new_pindex)) { 1898 /* 1899 * page already exists in parent OR swap exists 1900 * for this location in the parent. Destroy 1901 * the original page from the backing object. 1902 * 1903 * Leave the parent's page alone 1904 */ 1905 vm_page_protect(p, VM_PROT_NONE); 1906 vm_page_free(p); 1907 return(0); 1908 } 1909 1910 /* 1911 * Page does not exist in parent, rename the 1912 * page from the backing object to the main object. 1913 * 1914 * If the page was mapped to a process, it can remain 1915 * mapped through the rename. 1916 */ 1917 if ((p->queue - p->pc) == PQ_CACHE) 1918 vm_page_deactivate(p); 1919 1920 vm_page_rename(p, object, new_pindex); 1921 vm_page_wakeup(p); 1922 /* page automatically made dirty by rename */ 1923 } 1924 return(0); 1925 } 1926 1927 /* 1928 * This version of collapse allows the operation to occur earlier and 1929 * when paging_in_progress is true for an object... This is not a complete 1930 * operation, but should plug 99.9% of the rest of the leaks. 1931 * 1932 * The caller must hold the object and backing_object and both must be 1933 * chainlocked. 1934 * 1935 * (only called from vm_object_collapse) 1936 */ 1937 static void 1938 vm_object_qcollapse(vm_object_t object, vm_object_t backing_object) 1939 { 1940 if (backing_object->ref_count == 1) { 1941 backing_object->ref_count += 2; 1942 vm_object_backing_scan(object, backing_object, 1943 OBSC_COLLAPSE_NOWAIT); 1944 backing_object->ref_count -= 2; 1945 } 1946 } 1947 1948 /* 1949 * Collapse an object with the object backing it. Pages in the backing 1950 * object are moved into the parent, and the backing object is deallocated. 1951 * Any conflict is resolved in favor of the parent's existing pages. 1952 * 1953 * object must be held and chain-locked on call. 1954 * 1955 * The caller must have an extra ref on object to prevent a race from 1956 * destroying it during the collapse. 1957 */ 1958 void 1959 vm_object_collapse(vm_object_t object, struct vm_object_dealloc_list **dlistp) 1960 { 1961 struct vm_object_dealloc_list *dlist = NULL; 1962 vm_object_t backing_object; 1963 1964 /* 1965 * Only one thread is attempting a collapse at any given moment. 1966 * There are few restrictions for (object) that callers of this 1967 * function check so reentrancy is likely. 1968 */ 1969 KKASSERT(object != NULL); 1970 vm_object_assert_held(object); 1971 KKASSERT(object->flags & OBJ_CHAINLOCK); 1972 1973 for (;;) { 1974 vm_object_t bbobj; 1975 int dodealloc; 1976 1977 /* 1978 * We have to hold the backing object, check races. 1979 */ 1980 while ((backing_object = object->backing_object) != NULL) { 1981 vm_object_hold(backing_object); 1982 if (backing_object == object->backing_object) 1983 break; 1984 vm_object_drop(backing_object); 1985 } 1986 1987 /* 1988 * No backing object? Nothing to collapse then. 1989 */ 1990 if (backing_object == NULL) 1991 break; 1992 1993 /* 1994 * You can't collapse with a non-default/non-swap object. 1995 */ 1996 if (backing_object->type != OBJT_DEFAULT && 1997 backing_object->type != OBJT_SWAP) { 1998 vm_object_drop(backing_object); 1999 backing_object = NULL; 2000 break; 2001 } 2002 2003 /* 2004 * Chain-lock the backing object too because if we 2005 * successfully merge its pages into the top object we 2006 * will collapse backing_object->backing_object as the 2007 * new backing_object. Re-check that it is still our 2008 * backing object. 2009 */ 2010 vm_object_chain_acquire(backing_object); 2011 if (backing_object != object->backing_object) { 2012 vm_object_chain_release(backing_object); 2013 vm_object_drop(backing_object); 2014 continue; 2015 } 2016 2017 /* 2018 * we check the backing object first, because it is most likely 2019 * not collapsable. 2020 */ 2021 if (backing_object->handle != NULL || 2022 (backing_object->type != OBJT_DEFAULT && 2023 backing_object->type != OBJT_SWAP) || 2024 (backing_object->flags & OBJ_DEAD) || 2025 object->handle != NULL || 2026 (object->type != OBJT_DEFAULT && 2027 object->type != OBJT_SWAP) || 2028 (object->flags & OBJ_DEAD)) { 2029 break; 2030 } 2031 2032 /* 2033 * If paging is in progress we can't do a normal collapse. 2034 */ 2035 if ( 2036 object->paging_in_progress != 0 || 2037 backing_object->paging_in_progress != 0 2038 ) { 2039 vm_object_qcollapse(object, backing_object); 2040 break; 2041 } 2042 2043 /* 2044 * We know that we can either collapse the backing object (if 2045 * the parent is the only reference to it) or (perhaps) have 2046 * the parent bypass the object if the parent happens to shadow 2047 * all the resident pages in the entire backing object. 2048 * 2049 * This is ignoring pager-backed pages such as swap pages. 2050 * vm_object_backing_scan fails the shadowing test in this 2051 * case. 2052 */ 2053 if (backing_object->ref_count == 1) { 2054 /* 2055 * If there is exactly one reference to the backing 2056 * object, we can collapse it into the parent. 2057 */ 2058 KKASSERT(object->backing_object == backing_object); 2059 vm_object_backing_scan(object, backing_object, 2060 OBSC_COLLAPSE_WAIT); 2061 2062 /* 2063 * Move the pager from backing_object to object. 2064 */ 2065 if (backing_object->type == OBJT_SWAP) { 2066 vm_object_pip_add(backing_object, 1); 2067 2068 /* 2069 * scrap the paging_offset junk and do a 2070 * discrete copy. This also removes major 2071 * assumptions about how the swap-pager 2072 * works from where it doesn't belong. The 2073 * new swapper is able to optimize the 2074 * destroy-source case. 2075 */ 2076 vm_object_pip_add(object, 1); 2077 swap_pager_copy(backing_object, object, 2078 OFF_TO_IDX(object->backing_object_offset), 2079 TRUE); 2080 vm_object_pip_wakeup(object); 2081 vm_object_pip_wakeup(backing_object); 2082 } 2083 2084 /* 2085 * Object now shadows whatever backing_object did. 2086 * Remove object from backing_object's shadow_list. 2087 */ 2088 LIST_REMOVE(object, shadow_list); 2089 KKASSERT(object->backing_object == backing_object); 2090 backing_object->shadow_count--; 2091 backing_object->generation++; 2092 2093 /* 2094 * backing_object->backing_object moves from within 2095 * backing_object to within object. 2096 */ 2097 while ((bbobj = backing_object->backing_object) != NULL) { 2098 vm_object_hold(bbobj); 2099 if (bbobj == backing_object->backing_object) 2100 break; 2101 vm_object_drop(bbobj); 2102 } 2103 if (bbobj) { 2104 LIST_REMOVE(backing_object, shadow_list); 2105 bbobj->shadow_count--; 2106 bbobj->generation++; 2107 backing_object->backing_object = NULL; 2108 } 2109 object->backing_object = bbobj; 2110 if (bbobj) { 2111 LIST_INSERT_HEAD(&bbobj->shadow_head, 2112 object, shadow_list); 2113 bbobj->shadow_count++; 2114 bbobj->generation++; 2115 } 2116 2117 object->backing_object_offset += 2118 backing_object->backing_object_offset; 2119 2120 vm_object_drop(bbobj); 2121 2122 /* 2123 * Discard the old backing_object. Nothing should be 2124 * able to ref it, other than a vm_map_split(), 2125 * and vm_map_split() will stall on our chain lock. 2126 * And we control the parent so it shouldn't be 2127 * possible for it to go away either. 2128 * 2129 * Since the backing object has no pages, no pager 2130 * left, and no object references within it, all 2131 * that is necessary is to dispose of it. 2132 */ 2133 KASSERT(backing_object->ref_count == 1, 2134 ("backing_object %p was somehow " 2135 "re-referenced during collapse!", 2136 backing_object)); 2137 KASSERT(RB_EMPTY(&backing_object->rb_memq), 2138 ("backing_object %p somehow has left " 2139 "over pages during collapse!", 2140 backing_object)); 2141 2142 /* 2143 * The object can be destroyed. 2144 * 2145 * XXX just fall through and dodealloc instead 2146 * of forcing destruction? 2147 */ 2148 --backing_object->ref_count; 2149 if ((backing_object->flags & OBJ_DEAD) == 0) 2150 vm_object_terminate(backing_object); 2151 object_collapses++; 2152 dodealloc = 0; 2153 } else { 2154 /* 2155 * If we do not entirely shadow the backing object, 2156 * there is nothing we can do so we give up. 2157 */ 2158 if (vm_object_backing_scan(object, backing_object, 2159 OBSC_TEST_ALL_SHADOWED) == 0) { 2160 break; 2161 } 2162 2163 /* 2164 * bbobj is backing_object->backing_object. Since 2165 * object completely shadows backing_object we can 2166 * bypass it and become backed by bbobj instead. 2167 */ 2168 while ((bbobj = backing_object->backing_object) != NULL) { 2169 vm_object_hold(bbobj); 2170 if (bbobj == backing_object->backing_object) 2171 break; 2172 vm_object_drop(bbobj); 2173 } 2174 2175 /* 2176 * Make object shadow bbobj instead of backing_object. 2177 * Remove object from backing_object's shadow list. 2178 * 2179 * Deallocating backing_object will not remove 2180 * it, since its reference count is at least 2. 2181 */ 2182 KKASSERT(object->backing_object == backing_object); 2183 LIST_REMOVE(object, shadow_list); 2184 backing_object->shadow_count--; 2185 backing_object->generation++; 2186 2187 /* 2188 * Add a ref to bbobj, bbobj now shadows object. 2189 * 2190 * NOTE: backing_object->backing_object still points 2191 * to bbobj. That relationship remains intact 2192 * because backing_object has > 1 ref, so 2193 * someone else is pointing to it (hence why 2194 * we can't collapse it into object and can 2195 * only handle the all-shadowed bypass case). 2196 */ 2197 if (bbobj) { 2198 vm_object_chain_wait(bbobj); 2199 vm_object_reference_locked(bbobj); 2200 LIST_INSERT_HEAD(&bbobj->shadow_head, 2201 object, shadow_list); 2202 bbobj->shadow_count++; 2203 bbobj->generation++; 2204 object->backing_object_offset += 2205 backing_object->backing_object_offset; 2206 object->backing_object = bbobj; 2207 vm_object_drop(bbobj); 2208 } else { 2209 object->backing_object = NULL; 2210 } 2211 2212 /* 2213 * Drop the reference count on backing_object. To 2214 * handle ref_count races properly we can't assume 2215 * that the ref_count is still at least 2 so we 2216 * have to actually call vm_object_deallocate() 2217 * (after clearing the chainlock). 2218 */ 2219 object_bypasses++; 2220 dodealloc = 1; 2221 } 2222 2223 /* 2224 * Ok, we want to loop on the new object->bbobj association, 2225 * possibly collapsing it further. However if dodealloc is 2226 * non-zero we have to deallocate the backing_object which 2227 * itself can potentially undergo a collapse, creating a 2228 * recursion depth issue with the LWKT token subsystem. 2229 * 2230 * In the case where we must deallocate the backing_object 2231 * it is possible now that the backing_object has a single 2232 * shadow count on some other object (not represented here 2233 * as yet), since it no longer shadows us. Thus when we 2234 * call vm_object_deallocate() it may attempt to collapse 2235 * itself into its remaining parent. 2236 */ 2237 if (dodealloc) { 2238 struct vm_object_dealloc_list *dtmp; 2239 2240 vm_object_chain_release(backing_object); 2241 vm_object_unlock(backing_object); 2242 /* backing_object remains held */ 2243 2244 /* 2245 * Auto-deallocation list for caller convenience. 2246 */ 2247 if (dlistp == NULL) 2248 dlistp = &dlist; 2249 2250 dtmp = kmalloc(sizeof(*dtmp), M_TEMP, M_WAITOK); 2251 dtmp->object = backing_object; 2252 dtmp->next = *dlistp; 2253 *dlistp = dtmp; 2254 } else { 2255 vm_object_chain_release(backing_object); 2256 vm_object_drop(backing_object); 2257 } 2258 /* backing_object = NULL; not needed */ 2259 /* loop */ 2260 } 2261 2262 /* 2263 * Clean up any left over backing_object 2264 */ 2265 if (backing_object) { 2266 vm_object_chain_release(backing_object); 2267 vm_object_drop(backing_object); 2268 } 2269 2270 /* 2271 * Clean up any auto-deallocation list. This is a convenience 2272 * for top-level callers so they don't have to pass &dlist. 2273 * Do not clean up any caller-passed dlistp, the caller will 2274 * do that. 2275 */ 2276 if (dlist) 2277 vm_object_deallocate_list(&dlist); 2278 2279 } 2280 2281 /* 2282 * vm_object_collapse() may collect additional objects in need of 2283 * deallocation. This routine deallocates these objects. The 2284 * deallocation itself can trigger additional collapses (which the 2285 * deallocate function takes care of). This procedure is used to 2286 * reduce procedural recursion since these vm_object shadow chains 2287 * can become quite long. 2288 */ 2289 void 2290 vm_object_deallocate_list(struct vm_object_dealloc_list **dlistp) 2291 { 2292 struct vm_object_dealloc_list *dlist; 2293 2294 while ((dlist = *dlistp) != NULL) { 2295 *dlistp = dlist->next; 2296 vm_object_lock(dlist->object); 2297 vm_object_deallocate_locked(dlist->object); 2298 vm_object_drop(dlist->object); 2299 kfree(dlist, M_TEMP); 2300 } 2301 } 2302 2303 /* 2304 * Removes all physical pages in the specified object range from the 2305 * object's list of pages. 2306 * 2307 * No requirements. 2308 */ 2309 static int vm_object_page_remove_callback(vm_page_t p, void *data); 2310 2311 void 2312 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 2313 boolean_t clean_only) 2314 { 2315 struct rb_vm_page_scan_info info; 2316 int all; 2317 2318 /* 2319 * Degenerate cases and assertions 2320 */ 2321 vm_object_hold(object); 2322 if (object == NULL || 2323 (object->resident_page_count == 0 && object->swblock_count == 0)) { 2324 vm_object_drop(object); 2325 return; 2326 } 2327 KASSERT(object->type != OBJT_PHYS, 2328 ("attempt to remove pages from a physical object")); 2329 2330 /* 2331 * Indicate that paging is occuring on the object 2332 */ 2333 vm_object_pip_add(object, 1); 2334 2335 /* 2336 * Figure out the actual removal range and whether we are removing 2337 * the entire contents of the object or not. If removing the entire 2338 * contents, be sure to get all pages, even those that might be 2339 * beyond the end of the object. 2340 */ 2341 info.start_pindex = start; 2342 if (end == 0) 2343 info.end_pindex = (vm_pindex_t)-1; 2344 else 2345 info.end_pindex = end - 1; 2346 info.limit = clean_only; 2347 all = (start == 0 && info.end_pindex >= object->size - 1); 2348 2349 /* 2350 * Loop until we are sure we have gotten them all. 2351 */ 2352 do { 2353 info.error = 0; 2354 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp, 2355 vm_object_page_remove_callback, &info); 2356 } while (info.error); 2357 2358 /* 2359 * Remove any related swap if throwing away pages, or for 2360 * non-swap objects (the swap is a clean copy in that case). 2361 */ 2362 if (object->type != OBJT_SWAP || clean_only == FALSE) { 2363 if (all) 2364 swap_pager_freespace_all(object); 2365 else 2366 swap_pager_freespace(object, info.start_pindex, 2367 info.end_pindex - info.start_pindex + 1); 2368 } 2369 2370 /* 2371 * Cleanup 2372 */ 2373 vm_object_pip_wakeup(object); 2374 vm_object_drop(object); 2375 } 2376 2377 /* 2378 * The caller must hold the object 2379 */ 2380 static int 2381 vm_object_page_remove_callback(vm_page_t p, void *data) 2382 { 2383 struct rb_vm_page_scan_info *info = data; 2384 2385 if (vm_page_busy_try(p, TRUE)) { 2386 vm_page_sleep_busy(p, TRUE, "vmopar"); 2387 info->error = 1; 2388 return(0); 2389 } 2390 2391 /* 2392 * Wired pages cannot be destroyed, but they can be invalidated 2393 * and we do so if clean_only (limit) is not set. 2394 * 2395 * WARNING! The page may be wired due to being part of a buffer 2396 * cache buffer, and the buffer might be marked B_CACHE. 2397 * This is fine as part of a truncation but VFSs must be 2398 * sure to fix the buffer up when re-extending the file. 2399 * 2400 * NOTE! PG_NEED_COMMIT is ignored. 2401 */ 2402 if (p->wire_count != 0) { 2403 vm_page_protect(p, VM_PROT_NONE); 2404 if (info->limit == 0) 2405 p->valid = 0; 2406 vm_page_wakeup(p); 2407 return(0); 2408 } 2409 2410 /* 2411 * limit is our clean_only flag. If set and the page is dirty or 2412 * requires a commit, do not free it. If set and the page is being 2413 * held by someone, do not free it. 2414 */ 2415 if (info->limit && p->valid) { 2416 vm_page_test_dirty(p); 2417 if ((p->valid & p->dirty) || (p->flags & PG_NEED_COMMIT)) { 2418 vm_page_wakeup(p); 2419 return(0); 2420 } 2421 #if 0 2422 if (p->hold_count) { 2423 vm_page_wakeup(p); 2424 return(0); 2425 } 2426 #endif 2427 } 2428 2429 /* 2430 * Destroy the page 2431 */ 2432 vm_page_protect(p, VM_PROT_NONE); 2433 vm_page_free(p); 2434 return(0); 2435 } 2436 2437 /* 2438 * Coalesces two objects backing up adjoining regions of memory into a 2439 * single object. 2440 * 2441 * returns TRUE if objects were combined. 2442 * 2443 * NOTE: Only works at the moment if the second object is NULL - 2444 * if it's not, which object do we lock first? 2445 * 2446 * Parameters: 2447 * prev_object First object to coalesce 2448 * prev_offset Offset into prev_object 2449 * next_object Second object into coalesce 2450 * next_offset Offset into next_object 2451 * 2452 * prev_size Size of reference to prev_object 2453 * next_size Size of reference to next_object 2454 * 2455 * The caller does not need to hold (prev_object) but must have a stable 2456 * pointer to it (typically by holding the vm_map locked). 2457 */ 2458 boolean_t 2459 vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex, 2460 vm_size_t prev_size, vm_size_t next_size) 2461 { 2462 vm_pindex_t next_pindex; 2463 2464 if (prev_object == NULL) 2465 return (TRUE); 2466 2467 vm_object_hold(prev_object); 2468 2469 if (prev_object->type != OBJT_DEFAULT && 2470 prev_object->type != OBJT_SWAP) { 2471 vm_object_drop(prev_object); 2472 return (FALSE); 2473 } 2474 2475 /* 2476 * Try to collapse the object first 2477 */ 2478 vm_object_chain_acquire(prev_object); 2479 vm_object_collapse(prev_object, NULL); 2480 2481 /* 2482 * Can't coalesce if: . more than one reference . paged out . shadows 2483 * another object . has a copy elsewhere (any of which mean that the 2484 * pages not mapped to prev_entry may be in use anyway) 2485 */ 2486 2487 if (prev_object->backing_object != NULL) { 2488 vm_object_chain_release(prev_object); 2489 vm_object_drop(prev_object); 2490 return (FALSE); 2491 } 2492 2493 prev_size >>= PAGE_SHIFT; 2494 next_size >>= PAGE_SHIFT; 2495 next_pindex = prev_pindex + prev_size; 2496 2497 if ((prev_object->ref_count > 1) && 2498 (prev_object->size != next_pindex)) { 2499 vm_object_chain_release(prev_object); 2500 vm_object_drop(prev_object); 2501 return (FALSE); 2502 } 2503 2504 /* 2505 * Remove any pages that may still be in the object from a previous 2506 * deallocation. 2507 */ 2508 if (next_pindex < prev_object->size) { 2509 vm_object_page_remove(prev_object, 2510 next_pindex, 2511 next_pindex + next_size, FALSE); 2512 if (prev_object->type == OBJT_SWAP) 2513 swap_pager_freespace(prev_object, 2514 next_pindex, next_size); 2515 } 2516 2517 /* 2518 * Extend the object if necessary. 2519 */ 2520 if (next_pindex + next_size > prev_object->size) 2521 prev_object->size = next_pindex + next_size; 2522 2523 vm_object_chain_release(prev_object); 2524 vm_object_drop(prev_object); 2525 return (TRUE); 2526 } 2527 2528 /* 2529 * Make the object writable and flag is being possibly dirty. 2530 * 2531 * The caller must hold the object. XXX called from vm_page_dirty(), 2532 * There is currently no requirement to hold the object. 2533 */ 2534 void 2535 vm_object_set_writeable_dirty(vm_object_t object) 2536 { 2537 struct vnode *vp; 2538 2539 /*vm_object_assert_held(object);*/ 2540 /* 2541 * Avoid contention in vm fault path by checking the state before 2542 * issuing an atomic op on it. 2543 */ 2544 if ((object->flags & (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) != 2545 (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) { 2546 vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 2547 } 2548 if (object->type == OBJT_VNODE && 2549 (vp = (struct vnode *)object->handle) != NULL) { 2550 if ((vp->v_flag & VOBJDIRTY) == 0) { 2551 vsetflags(vp, VOBJDIRTY); 2552 } 2553 } 2554 } 2555 2556 #include "opt_ddb.h" 2557 #ifdef DDB 2558 #include <sys/kernel.h> 2559 2560 #include <sys/cons.h> 2561 2562 #include <ddb/ddb.h> 2563 2564 static int _vm_object_in_map (vm_map_t map, vm_object_t object, 2565 vm_map_entry_t entry); 2566 static int vm_object_in_map (vm_object_t object); 2567 2568 /* 2569 * The caller must hold the object. 2570 */ 2571 static int 2572 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2573 { 2574 vm_map_t tmpm; 2575 vm_map_entry_t tmpe; 2576 vm_object_t obj, nobj; 2577 int entcount; 2578 2579 if (map == 0) 2580 return 0; 2581 if (entry == 0) { 2582 tmpe = map->header.next; 2583 entcount = map->nentries; 2584 while (entcount-- && (tmpe != &map->header)) { 2585 if( _vm_object_in_map(map, object, tmpe)) { 2586 return 1; 2587 } 2588 tmpe = tmpe->next; 2589 } 2590 return (0); 2591 } 2592 switch(entry->maptype) { 2593 case VM_MAPTYPE_SUBMAP: 2594 tmpm = entry->object.sub_map; 2595 tmpe = tmpm->header.next; 2596 entcount = tmpm->nentries; 2597 while (entcount-- && tmpe != &tmpm->header) { 2598 if( _vm_object_in_map(tmpm, object, tmpe)) { 2599 return 1; 2600 } 2601 tmpe = tmpe->next; 2602 } 2603 break; 2604 case VM_MAPTYPE_NORMAL: 2605 case VM_MAPTYPE_VPAGETABLE: 2606 obj = entry->object.vm_object; 2607 while (obj) { 2608 if (obj == object) { 2609 if (obj != entry->object.vm_object) 2610 vm_object_drop(obj); 2611 return 1; 2612 } 2613 while ((nobj = obj->backing_object) != NULL) { 2614 vm_object_hold(nobj); 2615 if (nobj == obj->backing_object) 2616 break; 2617 vm_object_drop(nobj); 2618 } 2619 if (obj != entry->object.vm_object) { 2620 if (nobj) 2621 vm_object_lock_swap(); 2622 vm_object_drop(obj); 2623 } 2624 obj = nobj; 2625 } 2626 break; 2627 default: 2628 break; 2629 } 2630 return 0; 2631 } 2632 2633 static int vm_object_in_map_callback(struct proc *p, void *data); 2634 2635 struct vm_object_in_map_info { 2636 vm_object_t object; 2637 int rv; 2638 }; 2639 2640 /* 2641 * Debugging only 2642 */ 2643 static int 2644 vm_object_in_map(vm_object_t object) 2645 { 2646 struct vm_object_in_map_info info; 2647 2648 info.rv = 0; 2649 info.object = object; 2650 2651 allproc_scan(vm_object_in_map_callback, &info); 2652 if (info.rv) 2653 return 1; 2654 if( _vm_object_in_map(&kernel_map, object, 0)) 2655 return 1; 2656 if( _vm_object_in_map(&pager_map, object, 0)) 2657 return 1; 2658 if( _vm_object_in_map(&buffer_map, object, 0)) 2659 return 1; 2660 return 0; 2661 } 2662 2663 /* 2664 * Debugging only 2665 */ 2666 static int 2667 vm_object_in_map_callback(struct proc *p, void *data) 2668 { 2669 struct vm_object_in_map_info *info = data; 2670 2671 if (p->p_vmspace) { 2672 if (_vm_object_in_map(&p->p_vmspace->vm_map, info->object, 0)) { 2673 info->rv = 1; 2674 return -1; 2675 } 2676 } 2677 return (0); 2678 } 2679 2680 DB_SHOW_COMMAND(vmochk, vm_object_check) 2681 { 2682 vm_object_t object; 2683 2684 /* 2685 * make sure that internal objs are in a map somewhere 2686 * and none have zero ref counts. 2687 */ 2688 for (object = TAILQ_FIRST(&vm_object_list); 2689 object != NULL; 2690 object = TAILQ_NEXT(object, object_list)) { 2691 if (object->type == OBJT_MARKER) 2692 continue; 2693 if (object->handle == NULL && 2694 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 2695 if (object->ref_count == 0) { 2696 db_printf("vmochk: internal obj has zero ref count: %ld\n", 2697 (long)object->size); 2698 } 2699 if (!vm_object_in_map(object)) { 2700 db_printf( 2701 "vmochk: internal obj is not in a map: " 2702 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2703 object->ref_count, (u_long)object->size, 2704 (u_long)object->size, 2705 (void *)object->backing_object); 2706 } 2707 } 2708 } 2709 } 2710 2711 /* 2712 * Debugging only 2713 */ 2714 DB_SHOW_COMMAND(object, vm_object_print_static) 2715 { 2716 /* XXX convert args. */ 2717 vm_object_t object = (vm_object_t)addr; 2718 boolean_t full = have_addr; 2719 2720 vm_page_t p; 2721 2722 /* XXX count is an (unused) arg. Avoid shadowing it. */ 2723 #define count was_count 2724 2725 int count; 2726 2727 if (object == NULL) 2728 return; 2729 2730 db_iprintf( 2731 "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n", 2732 object, (int)object->type, (u_long)object->size, 2733 object->resident_page_count, object->ref_count, object->flags); 2734 /* 2735 * XXX no %qd in kernel. Truncate object->backing_object_offset. 2736 */ 2737 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n", 2738 object->shadow_count, 2739 object->backing_object ? object->backing_object->ref_count : 0, 2740 object->backing_object, (long)object->backing_object_offset); 2741 2742 if (!full) 2743 return; 2744 2745 db_indent += 2; 2746 count = 0; 2747 RB_FOREACH(p, vm_page_rb_tree, &object->rb_memq) { 2748 if (count == 0) 2749 db_iprintf("memory:="); 2750 else if (count == 6) { 2751 db_printf("\n"); 2752 db_iprintf(" ..."); 2753 count = 0; 2754 } else 2755 db_printf(","); 2756 count++; 2757 2758 db_printf("(off=0x%lx,page=0x%lx)", 2759 (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p)); 2760 } 2761 if (count != 0) 2762 db_printf("\n"); 2763 db_indent -= 2; 2764 } 2765 2766 /* XXX. */ 2767 #undef count 2768 2769 /* 2770 * XXX need this non-static entry for calling from vm_map_print. 2771 * 2772 * Debugging only 2773 */ 2774 void 2775 vm_object_print(/* db_expr_t */ long addr, 2776 boolean_t have_addr, 2777 /* db_expr_t */ long count, 2778 char *modif) 2779 { 2780 vm_object_print_static(addr, have_addr, count, modif); 2781 } 2782 2783 /* 2784 * Debugging only 2785 */ 2786 DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 2787 { 2788 vm_object_t object; 2789 int nl = 0; 2790 int c; 2791 for (object = TAILQ_FIRST(&vm_object_list); 2792 object != NULL; 2793 object = TAILQ_NEXT(object, object_list)) { 2794 vm_pindex_t idx, fidx; 2795 vm_pindex_t osize; 2796 vm_paddr_t pa = -1, padiff; 2797 int rcount; 2798 vm_page_t m; 2799 2800 if (object->type == OBJT_MARKER) 2801 continue; 2802 db_printf("new object: %p\n", (void *)object); 2803 if ( nl > 18) { 2804 c = cngetc(); 2805 if (c != ' ') 2806 return; 2807 nl = 0; 2808 } 2809 nl++; 2810 rcount = 0; 2811 fidx = 0; 2812 osize = object->size; 2813 if (osize > 128) 2814 osize = 128; 2815 for (idx = 0; idx < osize; idx++) { 2816 m = vm_page_lookup(object, idx); 2817 if (m == NULL) { 2818 if (rcount) { 2819 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2820 (long)fidx, rcount, (long)pa); 2821 if ( nl > 18) { 2822 c = cngetc(); 2823 if (c != ' ') 2824 return; 2825 nl = 0; 2826 } 2827 nl++; 2828 rcount = 0; 2829 } 2830 continue; 2831 } 2832 2833 2834 if (rcount && 2835 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 2836 ++rcount; 2837 continue; 2838 } 2839 if (rcount) { 2840 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m); 2841 padiff >>= PAGE_SHIFT; 2842 padiff &= PQ_L2_MASK; 2843 if (padiff == 0) { 2844 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE; 2845 ++rcount; 2846 continue; 2847 } 2848 db_printf(" index(%ld)run(%d)pa(0x%lx)", 2849 (long)fidx, rcount, (long)pa); 2850 db_printf("pd(%ld)\n", (long)padiff); 2851 if ( nl > 18) { 2852 c = cngetc(); 2853 if (c != ' ') 2854 return; 2855 nl = 0; 2856 } 2857 nl++; 2858 } 2859 fidx = idx; 2860 pa = VM_PAGE_TO_PHYS(m); 2861 rcount = 1; 2862 } 2863 if (rcount) { 2864 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2865 (long)fidx, rcount, (long)pa); 2866 if ( nl > 18) { 2867 c = cngetc(); 2868 if (c != ' ') 2869 return; 2870 nl = 0; 2871 } 2872 nl++; 2873 } 2874 } 2875 } 2876 #endif /* DDB */ 2877