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