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$ 65 */ 66 67 /* 68 * Virtual memory object module. 69 */ 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/lock.h> 74 #include <sys/mman.h> 75 #include <sys/mount.h> 76 #include <sys/mutex.h> 77 #include <sys/proc.h> /* for curproc, pageproc */ 78 #include <sys/socket.h> 79 #include <sys/vnode.h> 80 #include <sys/vmmeter.h> 81 #include <sys/sx.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/vm_zone.h> 92 #include <vm/swap_pager.h> 93 #include <vm/vm_kern.h> 94 #include <vm/vm_extern.h> 95 96 static void vm_object_qcollapse __P((vm_object_t object)); 97 98 /* 99 * Virtual memory objects maintain the actual data 100 * associated with allocated virtual memory. A given 101 * page of memory exists within exactly one object. 102 * 103 * An object is only deallocated when all "references" 104 * are given up. Only one "reference" to a given 105 * region of an object should be writeable. 106 * 107 * Associated with each object is a list of all resident 108 * memory pages belonging to that object; this list is 109 * maintained by the "vm_page" module, and locked by the object's 110 * lock. 111 * 112 * Each object also records a "pager" routine which is 113 * used to retrieve (and store) pages to the proper backing 114 * storage. In addition, objects may be backed by other 115 * objects from which they were virtual-copied. 116 * 117 * The only items within the object structure which are 118 * modified after time of creation are: 119 * reference count locked by object's lock 120 * pager routine locked by object's lock 121 * 122 */ 123 124 struct object_q vm_object_list; 125 static struct mtx vm_object_list_mtx; /* lock for object list and count */ 126 static long vm_object_count; /* count of all objects */ 127 vm_object_t kernel_object; 128 vm_object_t kmem_object; 129 static struct vm_object kernel_object_store; 130 static struct vm_object kmem_object_store; 131 extern int vm_pageout_page_count; 132 133 static long object_collapses; 134 static long object_bypasses; 135 static int next_index; 136 static vm_zone_t obj_zone; 137 static struct vm_zone obj_zone_store; 138 static int object_hash_rand; 139 #define VM_OBJECTS_INIT 256 140 static struct vm_object vm_objects_init[VM_OBJECTS_INIT]; 141 142 void 143 _vm_object_allocate(type, size, object) 144 objtype_t type; 145 vm_size_t size; 146 vm_object_t object; 147 { 148 int incr; 149 TAILQ_INIT(&object->memq); 150 TAILQ_INIT(&object->shadow_head); 151 152 object->type = type; 153 object->size = size; 154 object->ref_count = 1; 155 object->flags = 0; 156 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP)) 157 vm_object_set_flag(object, OBJ_ONEMAPPING); 158 object->paging_in_progress = 0; 159 object->resident_page_count = 0; 160 object->shadow_count = 0; 161 object->pg_color = next_index; 162 if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1)) 163 incr = PQ_L2_SIZE / 3 + PQ_PRIME1; 164 else 165 incr = size; 166 next_index = (next_index + incr) & PQ_L2_MASK; 167 object->handle = NULL; 168 object->backing_object = NULL; 169 object->backing_object_offset = (vm_ooffset_t) 0; 170 /* 171 * Try to generate a number that will spread objects out in the 172 * hash table. We 'wipe' new objects across the hash in 128 page 173 * increments plus 1 more to offset it a little more by the time 174 * it wraps around. 175 */ 176 object->hash_rand = object_hash_rand - 129; 177 178 object->generation++; 179 180 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 181 vm_object_count++; 182 object_hash_rand = object->hash_rand; 183 } 184 185 /* 186 * vm_object_init: 187 * 188 * Initialize the VM objects module. 189 */ 190 void 191 vm_object_init() 192 { 193 TAILQ_INIT(&vm_object_list); 194 mtx_init(&vm_object_list_mtx, "vm object_list", MTX_DEF); 195 vm_object_count = 0; 196 197 kernel_object = &kernel_object_store; 198 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 199 kernel_object); 200 201 kmem_object = &kmem_object_store; 202 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 203 kmem_object); 204 205 obj_zone = &obj_zone_store; 206 zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object), 207 vm_objects_init, VM_OBJECTS_INIT); 208 } 209 210 void 211 vm_object_init2() { 212 zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1); 213 } 214 215 /* 216 * vm_object_allocate: 217 * 218 * Returns a new object with the given size. 219 */ 220 221 vm_object_t 222 vm_object_allocate(type, size) 223 objtype_t type; 224 vm_size_t size; 225 { 226 vm_object_t result; 227 228 result = (vm_object_t) zalloc(obj_zone); 229 230 _vm_object_allocate(type, size, result); 231 232 return (result); 233 } 234 235 236 /* 237 * vm_object_reference: 238 * 239 * Gets another reference to the given object. 240 */ 241 void 242 vm_object_reference(object) 243 vm_object_t object; 244 { 245 if (object == NULL) 246 return; 247 248 KASSERT(!(object->flags & OBJ_DEAD), 249 ("vm_object_reference: attempting to reference dead obj")); 250 251 object->ref_count++; 252 if (object->type == OBJT_VNODE) { 253 while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) { 254 printf("vm_object_reference: delay in getting object\n"); 255 } 256 } 257 } 258 259 void 260 vm_object_vndeallocate(object) 261 vm_object_t object; 262 { 263 struct vnode *vp = (struct vnode *) object->handle; 264 265 KASSERT(object->type == OBJT_VNODE, 266 ("vm_object_vndeallocate: not a vnode object")); 267 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 268 #ifdef INVARIANTS 269 if (object->ref_count == 0) { 270 vprint("vm_object_vndeallocate", vp); 271 panic("vm_object_vndeallocate: bad object reference count"); 272 } 273 #endif 274 275 object->ref_count--; 276 if (object->ref_count == 0) { 277 vp->v_flag &= ~VTEXT; 278 vm_object_clear_flag(object, OBJ_OPT); 279 } 280 vrele(vp); 281 } 282 283 /* 284 * vm_object_deallocate: 285 * 286 * Release a reference to the specified object, 287 * gained either through a vm_object_allocate 288 * or a vm_object_reference call. When all references 289 * are gone, storage associated with this object 290 * may be relinquished. 291 * 292 * No object may be locked. 293 */ 294 void 295 vm_object_deallocate(object) 296 vm_object_t object; 297 { 298 vm_object_t temp; 299 300 while (object != NULL) { 301 302 if (object->type == OBJT_VNODE) { 303 vm_object_vndeallocate(object); 304 return; 305 } 306 307 KASSERT(object->ref_count != 0, 308 ("vm_object_deallocate: object deallocated too many times: %d", object->type)); 309 310 /* 311 * If the reference count goes to 0 we start calling 312 * vm_object_terminate() on the object chain. 313 * A ref count of 1 may be a special case depending on the 314 * shadow count being 0 or 1. 315 */ 316 object->ref_count--; 317 if (object->ref_count > 1) { 318 return; 319 } else if (object->ref_count == 1) { 320 if (object->shadow_count == 0) { 321 vm_object_set_flag(object, OBJ_ONEMAPPING); 322 } else if ((object->shadow_count == 1) && 323 (object->handle == NULL) && 324 (object->type == OBJT_DEFAULT || 325 object->type == OBJT_SWAP)) { 326 vm_object_t robject; 327 328 robject = TAILQ_FIRST(&object->shadow_head); 329 KASSERT(robject != NULL, 330 ("vm_object_deallocate: ref_count: %d, shadow_count: %d", 331 object->ref_count, 332 object->shadow_count)); 333 if ((robject->handle == NULL) && 334 (robject->type == OBJT_DEFAULT || 335 robject->type == OBJT_SWAP)) { 336 337 robject->ref_count++; 338 339 while ( 340 robject->paging_in_progress || 341 object->paging_in_progress 342 ) { 343 vm_object_pip_sleep(robject, "objde1"); 344 vm_object_pip_sleep(object, "objde2"); 345 } 346 347 if (robject->ref_count == 1) { 348 robject->ref_count--; 349 object = robject; 350 goto doterm; 351 } 352 353 object = robject; 354 vm_object_collapse(object); 355 continue; 356 } 357 } 358 359 return; 360 361 } 362 363 doterm: 364 365 temp = object->backing_object; 366 if (temp) { 367 TAILQ_REMOVE(&temp->shadow_head, object, shadow_list); 368 temp->shadow_count--; 369 if (temp->ref_count == 0) 370 vm_object_clear_flag(temp, OBJ_OPT); 371 temp->generation++; 372 object->backing_object = NULL; 373 } 374 vm_object_terminate(object); 375 /* unlocks and deallocates object */ 376 object = temp; 377 } 378 } 379 380 /* 381 * vm_object_terminate actually destroys the specified object, freeing 382 * up all previously used resources. 383 * 384 * The object must be locked. 385 * This routine may block. 386 */ 387 void 388 vm_object_terminate(object) 389 vm_object_t object; 390 { 391 vm_page_t p; 392 int s; 393 394 /* 395 * Make sure no one uses us. 396 */ 397 vm_object_set_flag(object, OBJ_DEAD); 398 399 /* 400 * wait for the pageout daemon to be done with the object 401 */ 402 vm_object_pip_wait(object, "objtrm"); 403 404 KASSERT(!object->paging_in_progress, 405 ("vm_object_terminate: pageout in progress")); 406 407 /* 408 * Clean and free the pages, as appropriate. All references to the 409 * object are gone, so we don't need to lock it. 410 */ 411 if (object->type == OBJT_VNODE) { 412 struct vnode *vp; 413 414 /* 415 * Freeze optimized copies. 416 */ 417 vm_freeze_copyopts(object, 0, object->size); 418 419 /* 420 * Clean pages and flush buffers. 421 */ 422 vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 423 424 vp = (struct vnode *) object->handle; 425 vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0); 426 } 427 428 KASSERT(object->ref_count == 0, 429 ("vm_object_terminate: object with references, ref_count=%d", 430 object->ref_count)); 431 432 /* 433 * Now free any remaining pages. For internal objects, this also 434 * removes them from paging queues. Don't free wired pages, just 435 * remove them from the object. 436 */ 437 s = splvm(); 438 while ((p = TAILQ_FIRST(&object->memq)) != NULL) { 439 KASSERT(!p->busy && (p->flags & PG_BUSY) == 0, 440 ("vm_object_terminate: freeing busy page %p " 441 "p->busy = %d, p->flags %x\n", p, p->busy, p->flags)); 442 if (p->wire_count == 0) { 443 vm_page_busy(p); 444 vm_page_free(p); 445 cnt.v_pfree++; 446 } else { 447 vm_page_busy(p); 448 vm_page_remove(p); 449 } 450 } 451 splx(s); 452 453 /* 454 * Let the pager know object is dead. 455 */ 456 vm_pager_deallocate(object); 457 458 /* 459 * Remove the object from the global object list. 460 */ 461 mtx_lock(&vm_object_list_mtx); 462 TAILQ_REMOVE(&vm_object_list, object, object_list); 463 mtx_unlock(&vm_object_list_mtx); 464 465 wakeup(object); 466 467 /* 468 * Free the space for the object. 469 */ 470 zfree(obj_zone, object); 471 } 472 473 /* 474 * vm_object_page_clean 475 * 476 * Clean all dirty pages in the specified range of object. Leaves page 477 * on whatever queue it is currently on. If NOSYNC is set then do not 478 * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC), 479 * leaving the object dirty. 480 * 481 * Odd semantics: if start == end, we clean everything. 482 * 483 * The object must be locked. 484 */ 485 486 void 487 vm_object_page_clean(object, start, end, flags) 488 vm_object_t object; 489 vm_pindex_t start; 490 vm_pindex_t end; 491 int flags; 492 { 493 vm_page_t p, np, tp; 494 vm_offset_t tstart, tend; 495 vm_pindex_t pi; 496 int s; 497 struct vnode *vp; 498 int runlen; 499 int maxf; 500 int chkb; 501 int maxb; 502 int i; 503 int clearobjflags; 504 int pagerflags; 505 vm_page_t maf[vm_pageout_page_count]; 506 vm_page_t mab[vm_pageout_page_count]; 507 vm_page_t ma[vm_pageout_page_count]; 508 int curgeneration; 509 510 if (object->type != OBJT_VNODE || 511 (object->flags & OBJ_MIGHTBEDIRTY) == 0) 512 return; 513 514 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0; 515 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0; 516 517 vp = object->handle; 518 519 vm_object_set_flag(object, OBJ_CLEANING); 520 521 tstart = start; 522 if (end == 0) { 523 tend = object->size; 524 } else { 525 tend = end; 526 } 527 528 /* 529 * Generally set CLEANCHK interlock and make the page read-only so 530 * we can then clear the object flags. 531 * 532 * However, if this is a nosync mmap then the object is likely to 533 * stay dirty so do not mess with the page and do not clear the 534 * object flags. 535 */ 536 537 clearobjflags = 1; 538 539 TAILQ_FOREACH(p, &object->memq, listq) { 540 vm_page_flag_set(p, PG_CLEANCHK); 541 if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) 542 clearobjflags = 0; 543 else 544 vm_page_protect(p, VM_PROT_READ); 545 } 546 547 if (clearobjflags && (tstart == 0) && (tend == object->size)) { 548 vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 549 } 550 551 rescan: 552 curgeneration = object->generation; 553 554 for(p = TAILQ_FIRST(&object->memq); p; p = np) { 555 np = TAILQ_NEXT(p, listq); 556 557 pi = p->pindex; 558 if (((p->flags & PG_CLEANCHK) == 0) || 559 (pi < tstart) || (pi >= tend) || 560 (p->valid == 0) || 561 ((p->queue - p->pc) == PQ_CACHE)) { 562 vm_page_flag_clear(p, PG_CLEANCHK); 563 continue; 564 } 565 566 vm_page_test_dirty(p); 567 if ((p->dirty & p->valid) == 0) { 568 vm_page_flag_clear(p, PG_CLEANCHK); 569 continue; 570 } 571 572 /* 573 * If we have been asked to skip nosync pages and this is a 574 * nosync page, skip it. Note that the object flags were 575 * not cleared in this case so we do not have to set them. 576 */ 577 if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) { 578 vm_page_flag_clear(p, PG_CLEANCHK); 579 continue; 580 } 581 582 s = splvm(); 583 while (vm_page_sleep_busy(p, TRUE, "vpcwai")) { 584 if (object->generation != curgeneration) { 585 splx(s); 586 goto rescan; 587 } 588 } 589 590 maxf = 0; 591 for(i=1;i<vm_pageout_page_count;i++) { 592 if ((tp = vm_page_lookup(object, pi + i)) != NULL) { 593 if ((tp->flags & PG_BUSY) || 594 (tp->flags & PG_CLEANCHK) == 0 || 595 (tp->busy != 0)) 596 break; 597 if((tp->queue - tp->pc) == PQ_CACHE) { 598 vm_page_flag_clear(tp, PG_CLEANCHK); 599 break; 600 } 601 vm_page_test_dirty(tp); 602 if ((tp->dirty & tp->valid) == 0) { 603 vm_page_flag_clear(tp, PG_CLEANCHK); 604 break; 605 } 606 maf[ i - 1 ] = tp; 607 maxf++; 608 continue; 609 } 610 break; 611 } 612 613 maxb = 0; 614 chkb = vm_pageout_page_count - maxf; 615 if (chkb) { 616 for(i = 1; i < chkb;i++) { 617 if ((tp = vm_page_lookup(object, pi - i)) != NULL) { 618 if ((tp->flags & PG_BUSY) || 619 (tp->flags & PG_CLEANCHK) == 0 || 620 (tp->busy != 0)) 621 break; 622 if((tp->queue - tp->pc) == PQ_CACHE) { 623 vm_page_flag_clear(tp, PG_CLEANCHK); 624 break; 625 } 626 vm_page_test_dirty(tp); 627 if ((tp->dirty & tp->valid) == 0) { 628 vm_page_flag_clear(tp, PG_CLEANCHK); 629 break; 630 } 631 mab[ i - 1 ] = tp; 632 maxb++; 633 continue; 634 } 635 break; 636 } 637 } 638 639 for(i=0;i<maxb;i++) { 640 int index = (maxb - i) - 1; 641 ma[index] = mab[i]; 642 vm_page_flag_clear(ma[index], PG_CLEANCHK); 643 } 644 vm_page_flag_clear(p, PG_CLEANCHK); 645 ma[maxb] = p; 646 for(i=0;i<maxf;i++) { 647 int index = (maxb + i) + 1; 648 ma[index] = maf[i]; 649 vm_page_flag_clear(ma[index], PG_CLEANCHK); 650 } 651 runlen = maxb + maxf + 1; 652 653 splx(s); 654 vm_pageout_flush(ma, runlen, pagerflags); 655 for (i = 0; i<runlen; i++) { 656 if (ma[i]->valid & ma[i]->dirty) { 657 vm_page_protect(ma[i], VM_PROT_READ); 658 vm_page_flag_set(ma[i], PG_CLEANCHK); 659 } 660 } 661 if (object->generation != curgeneration) 662 goto rescan; 663 } 664 665 #if 0 666 VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc); 667 #endif 668 669 vm_object_clear_flag(object, OBJ_CLEANING); 670 return; 671 } 672 673 #ifdef not_used 674 /* XXX I cannot tell if this should be an exported symbol */ 675 /* 676 * vm_object_deactivate_pages 677 * 678 * Deactivate all pages in the specified object. (Keep its pages 679 * in memory even though it is no longer referenced.) 680 * 681 * The object must be locked. 682 */ 683 static void 684 vm_object_deactivate_pages(object) 685 vm_object_t object; 686 { 687 vm_page_t p, next; 688 689 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) { 690 next = TAILQ_NEXT(p, listq); 691 vm_page_deactivate(p); 692 } 693 } 694 #endif 695 696 /* 697 * Same as vm_object_pmap_copy, except range checking really 698 * works, and is meant for small sections of an object. 699 * 700 * This code protects resident pages by making them read-only 701 * and is typically called on a fork or split when a page 702 * is converted to copy-on-write. 703 * 704 * NOTE: If the page is already at VM_PROT_NONE, calling 705 * vm_page_protect will have no effect. 706 */ 707 708 void 709 vm_object_pmap_copy_1(object, start, end) 710 vm_object_t object; 711 vm_pindex_t start; 712 vm_pindex_t end; 713 { 714 vm_pindex_t idx; 715 vm_page_t p; 716 717 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0) 718 return; 719 720 for (idx = start; idx < end; idx++) { 721 p = vm_page_lookup(object, idx); 722 if (p == NULL) 723 continue; 724 vm_page_protect(p, VM_PROT_READ); 725 } 726 } 727 728 /* 729 * vm_object_pmap_remove: 730 * 731 * Removes all physical pages in the specified 732 * object range from all physical maps. 733 * 734 * The object must *not* be locked. 735 */ 736 void 737 vm_object_pmap_remove(object, start, end) 738 vm_object_t object; 739 vm_pindex_t start; 740 vm_pindex_t end; 741 { 742 vm_page_t p; 743 744 if (object == NULL) 745 return; 746 TAILQ_FOREACH(p, &object->memq, listq) { 747 if (p->pindex >= start && p->pindex < end) 748 vm_page_protect(p, VM_PROT_NONE); 749 } 750 if ((start == 0) && (object->size == end)) 751 vm_object_clear_flag(object, OBJ_WRITEABLE); 752 } 753 754 /* 755 * vm_object_madvise: 756 * 757 * Implements the madvise function at the object/page level. 758 * 759 * MADV_WILLNEED (any object) 760 * 761 * Activate the specified pages if they are resident. 762 * 763 * MADV_DONTNEED (any object) 764 * 765 * Deactivate the specified pages if they are resident. 766 * 767 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 768 * OBJ_ONEMAPPING only) 769 * 770 * Deactivate and clean the specified pages if they are 771 * resident. This permits the process to reuse the pages 772 * without faulting or the kernel to reclaim the pages 773 * without I/O. 774 */ 775 void 776 vm_object_madvise(object, pindex, count, advise) 777 vm_object_t object; 778 vm_pindex_t pindex; 779 int count; 780 int advise; 781 { 782 vm_pindex_t end, tpindex; 783 vm_object_t tobject; 784 vm_page_t m; 785 786 if (object == NULL) 787 return; 788 789 end = pindex + count; 790 791 /* 792 * Locate and adjust resident pages 793 */ 794 795 for (; pindex < end; pindex += 1) { 796 relookup: 797 tobject = object; 798 tpindex = pindex; 799 shadowlookup: 800 /* 801 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages 802 * and those pages must be OBJ_ONEMAPPING. 803 */ 804 if (advise == MADV_FREE) { 805 if ((tobject->type != OBJT_DEFAULT && 806 tobject->type != OBJT_SWAP) || 807 (tobject->flags & OBJ_ONEMAPPING) == 0) { 808 continue; 809 } 810 } 811 812 m = vm_page_lookup(tobject, tpindex); 813 814 if (m == NULL) { 815 /* 816 * There may be swap even if there is no backing page 817 */ 818 if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 819 swap_pager_freespace(tobject, tpindex, 1); 820 821 /* 822 * next object 823 */ 824 tobject = tobject->backing_object; 825 if (tobject == NULL) 826 continue; 827 tpindex += OFF_TO_IDX(tobject->backing_object_offset); 828 goto shadowlookup; 829 } 830 831 /* 832 * If the page is busy or not in a normal active state, 833 * we skip it. If the page is not managed there are no 834 * page queues to mess with. Things can break if we mess 835 * with pages in any of the below states. 836 */ 837 if ( 838 m->hold_count || 839 m->wire_count || 840 (m->flags & PG_UNMANAGED) || 841 m->valid != VM_PAGE_BITS_ALL 842 ) { 843 continue; 844 } 845 846 if (vm_page_sleep_busy(m, TRUE, "madvpo")) 847 goto relookup; 848 849 if (advise == MADV_WILLNEED) { 850 vm_page_activate(m); 851 } else if (advise == MADV_DONTNEED) { 852 vm_page_dontneed(m); 853 } else if (advise == MADV_FREE) { 854 /* 855 * Mark the page clean. This will allow the page 856 * to be freed up by the system. However, such pages 857 * are often reused quickly by malloc()/free() 858 * so we do not do anything that would cause 859 * a page fault if we can help it. 860 * 861 * Specifically, we do not try to actually free 862 * the page now nor do we try to put it in the 863 * cache (which would cause a page fault on reuse). 864 * 865 * But we do make the page is freeable as we 866 * can without actually taking the step of unmapping 867 * it. 868 */ 869 pmap_clear_modify(m); 870 m->dirty = 0; 871 m->act_count = 0; 872 vm_page_dontneed(m); 873 if (tobject->type == OBJT_SWAP) 874 swap_pager_freespace(tobject, tpindex, 1); 875 } 876 } 877 } 878 879 /* 880 * vm_object_shadow: 881 * 882 * Create a new object which is backed by the 883 * specified existing object range. The source 884 * object reference is deallocated. 885 * 886 * The new object and offset into that object 887 * are returned in the source parameters. 888 */ 889 890 void 891 vm_object_shadow(object, offset, length) 892 vm_object_t *object; /* IN/OUT */ 893 vm_ooffset_t *offset; /* IN/OUT */ 894 vm_size_t length; 895 { 896 vm_object_t source; 897 vm_object_t result; 898 899 source = *object; 900 901 /* 902 * Don't create the new object if the old object isn't shared. 903 */ 904 905 if (source != NULL && 906 source->ref_count == 1 && 907 source->handle == NULL && 908 (source->type == OBJT_DEFAULT || 909 source->type == OBJT_SWAP)) 910 return; 911 912 /* 913 * Allocate a new object with the given length 914 */ 915 result = vm_object_allocate(OBJT_DEFAULT, length); 916 KASSERT(result != NULL, ("vm_object_shadow: no object for shadowing")); 917 918 /* 919 * The new object shadows the source object, adding a reference to it. 920 * Our caller changes his reference to point to the new object, 921 * removing a reference to the source object. Net result: no change 922 * of reference count. 923 * 924 * Try to optimize the result object's page color when shadowing 925 * in order to maintain page coloring consistency in the combined 926 * shadowed object. 927 */ 928 result->backing_object = source; 929 if (source) { 930 TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list); 931 source->shadow_count++; 932 source->generation++; 933 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK; 934 } 935 936 /* 937 * Store the offset into the source object, and fix up the offset into 938 * the new object. 939 */ 940 941 result->backing_object_offset = *offset; 942 943 /* 944 * Return the new things 945 */ 946 947 *offset = 0; 948 *object = result; 949 } 950 951 #define OBSC_TEST_ALL_SHADOWED 0x0001 952 #define OBSC_COLLAPSE_NOWAIT 0x0002 953 #define OBSC_COLLAPSE_WAIT 0x0004 954 955 static __inline int 956 vm_object_backing_scan(vm_object_t object, int op) 957 { 958 int s; 959 int r = 1; 960 vm_page_t p; 961 vm_object_t backing_object; 962 vm_pindex_t backing_offset_index; 963 964 s = splvm(); 965 966 backing_object = object->backing_object; 967 backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 968 969 /* 970 * Initial conditions 971 */ 972 973 if (op & OBSC_TEST_ALL_SHADOWED) { 974 /* 975 * We do not want to have to test for the existence of 976 * swap pages in the backing object. XXX but with the 977 * new swapper this would be pretty easy to do. 978 * 979 * XXX what about anonymous MAP_SHARED memory that hasn't 980 * been ZFOD faulted yet? If we do not test for this, the 981 * shadow test may succeed! XXX 982 */ 983 if (backing_object->type != OBJT_DEFAULT) { 984 splx(s); 985 return(0); 986 } 987 } 988 if (op & OBSC_COLLAPSE_WAIT) { 989 vm_object_set_flag(backing_object, OBJ_DEAD); 990 } 991 992 /* 993 * Our scan 994 */ 995 996 p = TAILQ_FIRST(&backing_object->memq); 997 while (p) { 998 vm_page_t next = TAILQ_NEXT(p, listq); 999 vm_pindex_t new_pindex = p->pindex - backing_offset_index; 1000 1001 if (op & OBSC_TEST_ALL_SHADOWED) { 1002 vm_page_t pp; 1003 1004 /* 1005 * Ignore pages outside the parent object's range 1006 * and outside the parent object's mapping of the 1007 * backing object. 1008 * 1009 * note that we do not busy the backing object's 1010 * page. 1011 */ 1012 1013 if ( 1014 p->pindex < backing_offset_index || 1015 new_pindex >= object->size 1016 ) { 1017 p = next; 1018 continue; 1019 } 1020 1021 /* 1022 * See if the parent has the page or if the parent's 1023 * object pager has the page. If the parent has the 1024 * page but the page is not valid, the parent's 1025 * object pager must have the page. 1026 * 1027 * If this fails, the parent does not completely shadow 1028 * the object and we might as well give up now. 1029 */ 1030 1031 pp = vm_page_lookup(object, new_pindex); 1032 if ( 1033 (pp == NULL || pp->valid == 0) && 1034 !vm_pager_has_page(object, new_pindex, NULL, NULL) 1035 ) { 1036 r = 0; 1037 break; 1038 } 1039 } 1040 1041 /* 1042 * Check for busy page 1043 */ 1044 1045 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) { 1046 vm_page_t pp; 1047 1048 if (op & OBSC_COLLAPSE_NOWAIT) { 1049 if ( 1050 (p->flags & PG_BUSY) || 1051 !p->valid || 1052 p->hold_count || 1053 p->wire_count || 1054 p->busy 1055 ) { 1056 p = next; 1057 continue; 1058 } 1059 } else if (op & OBSC_COLLAPSE_WAIT) { 1060 if (vm_page_sleep_busy(p, TRUE, "vmocol")) { 1061 /* 1062 * If we slept, anything could have 1063 * happened. Since the object is 1064 * marked dead, the backing offset 1065 * should not have changed so we 1066 * just restart our scan. 1067 */ 1068 p = TAILQ_FIRST(&backing_object->memq); 1069 continue; 1070 } 1071 } 1072 1073 /* 1074 * Busy the page 1075 */ 1076 vm_page_busy(p); 1077 1078 KASSERT( 1079 p->object == backing_object, 1080 ("vm_object_qcollapse(): object mismatch") 1081 ); 1082 1083 /* 1084 * Destroy any associated swap 1085 */ 1086 if (backing_object->type == OBJT_SWAP) { 1087 swap_pager_freespace( 1088 backing_object, 1089 p->pindex, 1090 1 1091 ); 1092 } 1093 1094 if ( 1095 p->pindex < backing_offset_index || 1096 new_pindex >= object->size 1097 ) { 1098 /* 1099 * Page is out of the parent object's range, we 1100 * can simply destroy it. 1101 */ 1102 vm_page_protect(p, VM_PROT_NONE); 1103 vm_page_free(p); 1104 p = next; 1105 continue; 1106 } 1107 1108 pp = vm_page_lookup(object, new_pindex); 1109 if ( 1110 pp != NULL || 1111 vm_pager_has_page(object, new_pindex, NULL, NULL) 1112 ) { 1113 /* 1114 * page already exists in parent OR swap exists 1115 * for this location in the parent. Destroy 1116 * the original page from the backing object. 1117 * 1118 * Leave the parent's page alone 1119 */ 1120 vm_page_protect(p, VM_PROT_NONE); 1121 vm_page_free(p); 1122 p = next; 1123 continue; 1124 } 1125 1126 /* 1127 * Page does not exist in parent, rename the 1128 * page from the backing object to the main object. 1129 * 1130 * If the page was mapped to a process, it can remain 1131 * mapped through the rename. 1132 */ 1133 if ((p->queue - p->pc) == PQ_CACHE) 1134 vm_page_deactivate(p); 1135 1136 vm_page_rename(p, object, new_pindex); 1137 /* page automatically made dirty by rename */ 1138 } 1139 p = next; 1140 } 1141 splx(s); 1142 return(r); 1143 } 1144 1145 1146 /* 1147 * this version of collapse allows the operation to occur earlier and 1148 * when paging_in_progress is true for an object... This is not a complete 1149 * operation, but should plug 99.9% of the rest of the leaks. 1150 */ 1151 static void 1152 vm_object_qcollapse(object) 1153 vm_object_t object; 1154 { 1155 vm_object_t backing_object = object->backing_object; 1156 1157 if (backing_object->ref_count != 1) 1158 return; 1159 1160 backing_object->ref_count += 2; 1161 1162 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT); 1163 1164 backing_object->ref_count -= 2; 1165 } 1166 1167 /* 1168 * vm_object_collapse: 1169 * 1170 * Collapse an object with the object backing it. 1171 * Pages in the backing object are moved into the 1172 * parent, and the backing object is deallocated. 1173 */ 1174 void 1175 vm_object_collapse(object) 1176 vm_object_t object; 1177 { 1178 while (TRUE) { 1179 vm_object_t backing_object; 1180 1181 /* 1182 * Verify that the conditions are right for collapse: 1183 * 1184 * The object exists and the backing object exists. 1185 */ 1186 if (object == NULL) 1187 break; 1188 1189 if ((backing_object = object->backing_object) == NULL) 1190 break; 1191 1192 /* 1193 * we check the backing object first, because it is most likely 1194 * not collapsable. 1195 */ 1196 if (backing_object->handle != NULL || 1197 (backing_object->type != OBJT_DEFAULT && 1198 backing_object->type != OBJT_SWAP) || 1199 (backing_object->flags & OBJ_DEAD) || 1200 object->handle != NULL || 1201 (object->type != OBJT_DEFAULT && 1202 object->type != OBJT_SWAP) || 1203 (object->flags & OBJ_DEAD)) { 1204 break; 1205 } 1206 1207 if ( 1208 object->paging_in_progress != 0 || 1209 backing_object->paging_in_progress != 0 1210 ) { 1211 vm_object_qcollapse(object); 1212 break; 1213 } 1214 1215 /* 1216 * We know that we can either collapse the backing object (if 1217 * the parent is the only reference to it) or (perhaps) have 1218 * the parent bypass the object if the parent happens to shadow 1219 * all the resident pages in the entire backing object. 1220 * 1221 * This is ignoring pager-backed pages such as swap pages. 1222 * vm_object_backing_scan fails the shadowing test in this 1223 * case. 1224 */ 1225 1226 if (backing_object->ref_count == 1) { 1227 /* 1228 * If there is exactly one reference to the backing 1229 * object, we can collapse it into the parent. 1230 */ 1231 1232 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT); 1233 1234 /* 1235 * Move the pager from backing_object to object. 1236 */ 1237 1238 if (backing_object->type == OBJT_SWAP) { 1239 vm_object_pip_add(backing_object, 1); 1240 1241 /* 1242 * scrap the paging_offset junk and do a 1243 * discrete copy. This also removes major 1244 * assumptions about how the swap-pager 1245 * works from where it doesn't belong. The 1246 * new swapper is able to optimize the 1247 * destroy-source case. 1248 */ 1249 1250 vm_object_pip_add(object, 1); 1251 swap_pager_copy( 1252 backing_object, 1253 object, 1254 OFF_TO_IDX(object->backing_object_offset), TRUE); 1255 vm_object_pip_wakeup(object); 1256 1257 vm_object_pip_wakeup(backing_object); 1258 } 1259 /* 1260 * Object now shadows whatever backing_object did. 1261 * Note that the reference to 1262 * backing_object->backing_object moves from within 1263 * backing_object to within object. 1264 */ 1265 1266 TAILQ_REMOVE( 1267 &object->backing_object->shadow_head, 1268 object, 1269 shadow_list 1270 ); 1271 object->backing_object->shadow_count--; 1272 object->backing_object->generation++; 1273 if (backing_object->backing_object) { 1274 TAILQ_REMOVE( 1275 &backing_object->backing_object->shadow_head, 1276 backing_object, 1277 shadow_list 1278 ); 1279 backing_object->backing_object->shadow_count--; 1280 backing_object->backing_object->generation++; 1281 } 1282 object->backing_object = backing_object->backing_object; 1283 if (object->backing_object) { 1284 TAILQ_INSERT_TAIL( 1285 &object->backing_object->shadow_head, 1286 object, 1287 shadow_list 1288 ); 1289 object->backing_object->shadow_count++; 1290 object->backing_object->generation++; 1291 } 1292 1293 object->backing_object_offset += 1294 backing_object->backing_object_offset; 1295 1296 /* 1297 * Discard backing_object. 1298 * 1299 * Since the backing object has no pages, no pager left, 1300 * and no object references within it, all that is 1301 * necessary is to dispose of it. 1302 */ 1303 1304 TAILQ_REMOVE( 1305 &vm_object_list, 1306 backing_object, 1307 object_list 1308 ); 1309 vm_object_count--; 1310 1311 zfree(obj_zone, backing_object); 1312 1313 object_collapses++; 1314 } else { 1315 vm_object_t new_backing_object; 1316 1317 /* 1318 * If we do not entirely shadow the backing object, 1319 * there is nothing we can do so we give up. 1320 */ 1321 1322 if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) { 1323 break; 1324 } 1325 1326 /* 1327 * Make the parent shadow the next object in the 1328 * chain. Deallocating backing_object will not remove 1329 * it, since its reference count is at least 2. 1330 */ 1331 1332 TAILQ_REMOVE( 1333 &backing_object->shadow_head, 1334 object, 1335 shadow_list 1336 ); 1337 backing_object->shadow_count--; 1338 backing_object->generation++; 1339 1340 new_backing_object = backing_object->backing_object; 1341 if ((object->backing_object = new_backing_object) != NULL) { 1342 vm_object_reference(new_backing_object); 1343 TAILQ_INSERT_TAIL( 1344 &new_backing_object->shadow_head, 1345 object, 1346 shadow_list 1347 ); 1348 new_backing_object->shadow_count++; 1349 new_backing_object->generation++; 1350 object->backing_object_offset += 1351 backing_object->backing_object_offset; 1352 } 1353 1354 /* 1355 * Drop the reference count on backing_object. Since 1356 * its ref_count was at least 2, it will not vanish; 1357 * so we don't need to call vm_object_deallocate, but 1358 * we do anyway. 1359 */ 1360 vm_object_deallocate(backing_object); 1361 object_bypasses++; 1362 } 1363 1364 /* 1365 * Try again with this object's new backing object. 1366 */ 1367 } 1368 } 1369 1370 /* 1371 * vm_object_page_remove: [internal] 1372 * 1373 * Removes all physical pages in the specified 1374 * object range from the object's list of pages. 1375 * 1376 * The object must be locked. 1377 */ 1378 void 1379 vm_object_page_remove(object, start, end, clean_only) 1380 vm_object_t object; 1381 vm_pindex_t start; 1382 vm_pindex_t end; 1383 boolean_t clean_only; 1384 { 1385 vm_page_t p, next; 1386 unsigned int size; 1387 int all; 1388 1389 if (object == NULL || 1390 object->resident_page_count == 0) 1391 return; 1392 1393 all = ((end == 0) && (start == 0)); 1394 1395 /* 1396 * Since physically-backed objects do not use managed pages, we can't 1397 * remove pages from the object (we must instead remove the page 1398 * references, and then destroy the object). 1399 */ 1400 KASSERT(object->type != OBJT_PHYS, ("attempt to remove pages from a physical object")); 1401 1402 vm_object_pip_add(object, 1); 1403 again: 1404 size = end - start; 1405 if (all || size > object->resident_page_count / 4) { 1406 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) { 1407 next = TAILQ_NEXT(p, listq); 1408 if (all || ((start <= p->pindex) && (p->pindex < end))) { 1409 if (p->wire_count != 0) { 1410 vm_page_protect(p, VM_PROT_NONE); 1411 if (!clean_only) 1412 p->valid = 0; 1413 continue; 1414 } 1415 1416 /* 1417 * The busy flags are only cleared at 1418 * interrupt -- minimize the spl transitions 1419 */ 1420 1421 if (vm_page_sleep_busy(p, TRUE, "vmopar")) 1422 goto again; 1423 1424 if (clean_only && p->valid) { 1425 vm_page_test_dirty(p); 1426 if (p->valid & p->dirty) 1427 continue; 1428 } 1429 1430 vm_page_busy(p); 1431 vm_page_protect(p, VM_PROT_NONE); 1432 vm_page_free(p); 1433 } 1434 } 1435 } else { 1436 while (size > 0) { 1437 if ((p = vm_page_lookup(object, start)) != 0) { 1438 1439 if (p->wire_count != 0) { 1440 vm_page_protect(p, VM_PROT_NONE); 1441 if (!clean_only) 1442 p->valid = 0; 1443 start += 1; 1444 size -= 1; 1445 continue; 1446 } 1447 1448 /* 1449 * The busy flags are only cleared at 1450 * interrupt -- minimize the spl transitions 1451 */ 1452 if (vm_page_sleep_busy(p, TRUE, "vmopar")) 1453 goto again; 1454 1455 if (clean_only && p->valid) { 1456 vm_page_test_dirty(p); 1457 if (p->valid & p->dirty) { 1458 start += 1; 1459 size -= 1; 1460 continue; 1461 } 1462 } 1463 1464 vm_page_busy(p); 1465 vm_page_protect(p, VM_PROT_NONE); 1466 vm_page_free(p); 1467 } 1468 start += 1; 1469 size -= 1; 1470 } 1471 } 1472 vm_object_pip_wakeup(object); 1473 } 1474 1475 /* 1476 * Routine: vm_object_coalesce 1477 * Function: Coalesces two objects backing up adjoining 1478 * regions of memory into a single object. 1479 * 1480 * returns TRUE if objects were combined. 1481 * 1482 * NOTE: Only works at the moment if the second object is NULL - 1483 * if it's not, which object do we lock first? 1484 * 1485 * Parameters: 1486 * prev_object First object to coalesce 1487 * prev_offset Offset into prev_object 1488 * next_object Second object into coalesce 1489 * next_offset Offset into next_object 1490 * 1491 * prev_size Size of reference to prev_object 1492 * next_size Size of reference to next_object 1493 * 1494 * Conditions: 1495 * The object must *not* be locked. 1496 */ 1497 boolean_t 1498 vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size) 1499 vm_object_t prev_object; 1500 vm_pindex_t prev_pindex; 1501 vm_size_t prev_size, next_size; 1502 { 1503 vm_pindex_t next_pindex; 1504 1505 if (prev_object == NULL) { 1506 return (TRUE); 1507 } 1508 1509 if (prev_object->type != OBJT_DEFAULT && 1510 prev_object->type != OBJT_SWAP) { 1511 return (FALSE); 1512 } 1513 1514 /* 1515 * Try to collapse the object first 1516 */ 1517 vm_object_collapse(prev_object); 1518 1519 /* 1520 * Can't coalesce if: . more than one reference . paged out . shadows 1521 * another object . has a copy elsewhere (any of which mean that the 1522 * pages not mapped to prev_entry may be in use anyway) 1523 */ 1524 1525 if (prev_object->backing_object != NULL) { 1526 return (FALSE); 1527 } 1528 1529 prev_size >>= PAGE_SHIFT; 1530 next_size >>= PAGE_SHIFT; 1531 next_pindex = prev_pindex + prev_size; 1532 1533 if ((prev_object->ref_count > 1) && 1534 (prev_object->size != next_pindex)) { 1535 return (FALSE); 1536 } 1537 1538 /* 1539 * Remove any pages that may still be in the object from a previous 1540 * deallocation. 1541 */ 1542 if (next_pindex < prev_object->size) { 1543 vm_object_page_remove(prev_object, 1544 next_pindex, 1545 next_pindex + next_size, FALSE); 1546 if (prev_object->type == OBJT_SWAP) 1547 swap_pager_freespace(prev_object, 1548 next_pindex, next_size); 1549 } 1550 1551 /* 1552 * Extend the object if necessary. 1553 */ 1554 if (next_pindex + next_size > prev_object->size) 1555 prev_object->size = next_pindex + next_size; 1556 1557 return (TRUE); 1558 } 1559 1560 #include "opt_ddb.h" 1561 #ifdef DDB 1562 #include <sys/kernel.h> 1563 1564 #include <sys/cons.h> 1565 1566 #include <ddb/ddb.h> 1567 1568 static int _vm_object_in_map __P((vm_map_t map, vm_object_t object, 1569 vm_map_entry_t entry)); 1570 static int vm_object_in_map __P((vm_object_t object)); 1571 1572 static int 1573 _vm_object_in_map(map, object, entry) 1574 vm_map_t map; 1575 vm_object_t object; 1576 vm_map_entry_t entry; 1577 { 1578 vm_map_t tmpm; 1579 vm_map_entry_t tmpe; 1580 vm_object_t obj; 1581 int entcount; 1582 1583 if (map == 0) 1584 return 0; 1585 1586 if (entry == 0) { 1587 tmpe = map->header.next; 1588 entcount = map->nentries; 1589 while (entcount-- && (tmpe != &map->header)) { 1590 if( _vm_object_in_map(map, object, tmpe)) { 1591 return 1; 1592 } 1593 tmpe = tmpe->next; 1594 } 1595 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 1596 tmpm = entry->object.sub_map; 1597 tmpe = tmpm->header.next; 1598 entcount = tmpm->nentries; 1599 while (entcount-- && tmpe != &tmpm->header) { 1600 if( _vm_object_in_map(tmpm, object, tmpe)) { 1601 return 1; 1602 } 1603 tmpe = tmpe->next; 1604 } 1605 } else if ((obj = entry->object.vm_object) != NULL) { 1606 for(; obj; obj=obj->backing_object) 1607 if( obj == object) { 1608 return 1; 1609 } 1610 } 1611 return 0; 1612 } 1613 1614 static int 1615 vm_object_in_map( object) 1616 vm_object_t object; 1617 { 1618 struct proc *p; 1619 1620 sx_slock(&allproc_lock); 1621 LIST_FOREACH(p, &allproc, p_list) { 1622 if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 1623 continue; 1624 if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 1625 sx_sunlock(&allproc_lock); 1626 return 1; 1627 } 1628 } 1629 sx_sunlock(&allproc_lock); 1630 if( _vm_object_in_map( kernel_map, object, 0)) 1631 return 1; 1632 if( _vm_object_in_map( kmem_map, object, 0)) 1633 return 1; 1634 if( _vm_object_in_map( pager_map, object, 0)) 1635 return 1; 1636 if( _vm_object_in_map( buffer_map, object, 0)) 1637 return 1; 1638 if( _vm_object_in_map( mb_map, object, 0)) 1639 return 1; 1640 return 0; 1641 } 1642 1643 DB_SHOW_COMMAND(vmochk, vm_object_check) 1644 { 1645 vm_object_t object; 1646 1647 /* 1648 * make sure that internal objs are in a map somewhere 1649 * and none have zero ref counts. 1650 */ 1651 TAILQ_FOREACH(object, &vm_object_list, object_list) { 1652 if (object->handle == NULL && 1653 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 1654 if (object->ref_count == 0) { 1655 db_printf("vmochk: internal obj has zero ref count: %ld\n", 1656 (long)object->size); 1657 } 1658 if (!vm_object_in_map(object)) { 1659 db_printf( 1660 "vmochk: internal obj is not in a map: " 1661 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 1662 object->ref_count, (u_long)object->size, 1663 (u_long)object->size, 1664 (void *)object->backing_object); 1665 } 1666 } 1667 } 1668 } 1669 1670 /* 1671 * vm_object_print: [ debug ] 1672 */ 1673 DB_SHOW_COMMAND(object, vm_object_print_static) 1674 { 1675 /* XXX convert args. */ 1676 vm_object_t object = (vm_object_t)addr; 1677 boolean_t full = have_addr; 1678 1679 vm_page_t p; 1680 1681 /* XXX count is an (unused) arg. Avoid shadowing it. */ 1682 #define count was_count 1683 1684 int count; 1685 1686 if (object == NULL) 1687 return; 1688 1689 db_iprintf( 1690 "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n", 1691 object, (int)object->type, (u_long)object->size, 1692 object->resident_page_count, object->ref_count, object->flags); 1693 /* 1694 * XXX no %qd in kernel. Truncate object->backing_object_offset. 1695 */ 1696 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n", 1697 object->shadow_count, 1698 object->backing_object ? object->backing_object->ref_count : 0, 1699 object->backing_object, (long)object->backing_object_offset); 1700 1701 if (!full) 1702 return; 1703 1704 db_indent += 2; 1705 count = 0; 1706 TAILQ_FOREACH(p, &object->memq, listq) { 1707 if (count == 0) 1708 db_iprintf("memory:="); 1709 else if (count == 6) { 1710 db_printf("\n"); 1711 db_iprintf(" ..."); 1712 count = 0; 1713 } else 1714 db_printf(","); 1715 count++; 1716 1717 db_printf("(off=0x%lx,page=0x%lx)", 1718 (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p)); 1719 } 1720 if (count != 0) 1721 db_printf("\n"); 1722 db_indent -= 2; 1723 } 1724 1725 /* XXX. */ 1726 #undef count 1727 1728 /* XXX need this non-static entry for calling from vm_map_print. */ 1729 void 1730 vm_object_print(addr, have_addr, count, modif) 1731 /* db_expr_t */ long addr; 1732 boolean_t have_addr; 1733 /* db_expr_t */ long count; 1734 char *modif; 1735 { 1736 vm_object_print_static(addr, have_addr, count, modif); 1737 } 1738 1739 DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 1740 { 1741 vm_object_t object; 1742 int nl = 0; 1743 int c; 1744 1745 TAILQ_FOREACH(object, &vm_object_list, object_list) { 1746 vm_pindex_t idx, fidx; 1747 vm_pindex_t osize; 1748 vm_offset_t pa = -1, padiff; 1749 int rcount; 1750 vm_page_t m; 1751 1752 db_printf("new object: %p\n", (void *)object); 1753 if ( nl > 18) { 1754 c = cngetc(); 1755 if (c != ' ') 1756 return; 1757 nl = 0; 1758 } 1759 nl++; 1760 rcount = 0; 1761 fidx = 0; 1762 osize = object->size; 1763 if (osize > 128) 1764 osize = 128; 1765 for(idx=0;idx<osize;idx++) { 1766 m = vm_page_lookup(object, idx); 1767 if (m == NULL) { 1768 if (rcount) { 1769 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 1770 (long)fidx, rcount, (long)pa); 1771 if ( nl > 18) { 1772 c = cngetc(); 1773 if (c != ' ') 1774 return; 1775 nl = 0; 1776 } 1777 nl++; 1778 rcount = 0; 1779 } 1780 continue; 1781 } 1782 1783 1784 if (rcount && 1785 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 1786 ++rcount; 1787 continue; 1788 } 1789 if (rcount) { 1790 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m); 1791 padiff >>= PAGE_SHIFT; 1792 padiff &= PQ_L2_MASK; 1793 if (padiff == 0) { 1794 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE; 1795 ++rcount; 1796 continue; 1797 } 1798 db_printf(" index(%ld)run(%d)pa(0x%lx)", 1799 (long)fidx, rcount, (long)pa); 1800 db_printf("pd(%ld)\n", (long)padiff); 1801 if ( nl > 18) { 1802 c = cngetc(); 1803 if (c != ' ') 1804 return; 1805 nl = 0; 1806 } 1807 nl++; 1808 } 1809 fidx = idx; 1810 pa = VM_PAGE_TO_PHYS(m); 1811 rcount = 1; 1812 } 1813 if (rcount) { 1814 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 1815 (long)fidx, rcount, (long)pa); 1816 if ( nl > 18) { 1817 c = cngetc(); 1818 if (c != ' ') 1819 return; 1820 nl = 0; 1821 } 1822 nl++; 1823 } 1824 } 1825 } 1826 #endif /* DDB */ 1827