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