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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94 33 * 34 * 35 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 36 * All rights reserved. 37 * 38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 39 * 40 * Permission to use, copy, modify and distribute this software and 41 * its documentation is hereby granted, provided that both the copyright 42 * notice and this permission notice appear in all copies of the 43 * software, derivative works or modified versions, and any portions 44 * thereof, and that both notices appear in supporting documentation. 45 * 46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 49 * 50 * Carnegie Mellon requests users of this software to return to 51 * 52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 53 * School of Computer Science 54 * Carnegie Mellon University 55 * Pittsburgh PA 15213-3890 56 * 57 * any improvements or extensions that they make and grant Carnegie the 58 * rights to redistribute these changes. 59 */ 60 61 /* 62 * Virtual memory object module. 63 */ 64 65 #include <sys/cdefs.h> 66 __FBSDID("$FreeBSD$"); 67 68 #include "opt_vm.h" 69 70 #include <sys/param.h> 71 #include <sys/systm.h> 72 #include <sys/lock.h> 73 #include <sys/mman.h> 74 #include <sys/mount.h> 75 #include <sys/kernel.h> 76 #include <sys/sysctl.h> 77 #include <sys/mutex.h> 78 #include <sys/proc.h> /* for curproc, pageproc */ 79 #include <sys/socket.h> 80 #include <sys/vnode.h> 81 #include <sys/vmmeter.h> 82 #include <sys/sx.h> 83 84 #include <vm/vm.h> 85 #include <vm/vm_param.h> 86 #include <vm/pmap.h> 87 #include <vm/vm_map.h> 88 #include <vm/vm_object.h> 89 #include <vm/vm_page.h> 90 #include <vm/vm_pageout.h> 91 #include <vm/vm_pager.h> 92 #include <vm/swap_pager.h> 93 #include <vm/vm_kern.h> 94 #include <vm/vm_extern.h> 95 #include <vm/vm_reserv.h> 96 #include <vm/uma.h> 97 98 #define EASY_SCAN_FACTOR 8 99 100 #define MSYNC_FLUSH_HARDSEQ 0x01 101 #define MSYNC_FLUSH_SOFTSEQ 0x02 102 103 /* 104 * msync / VM object flushing optimizations 105 */ 106 static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ; 107 SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags, 108 CTLFLAG_RW, &msync_flush_flags, 0, ""); 109 110 static int old_msync; 111 SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0, 112 "Use old (insecure) msync behavior"); 113 114 static void vm_object_qcollapse(vm_object_t object); 115 static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags); 116 static void vm_object_vndeallocate(vm_object_t object); 117 118 /* 119 * Virtual memory objects maintain the actual data 120 * associated with allocated virtual memory. A given 121 * page of memory exists within exactly one object. 122 * 123 * An object is only deallocated when all "references" 124 * are given up. Only one "reference" to a given 125 * region of an object should be writeable. 126 * 127 * Associated with each object is a list of all resident 128 * memory pages belonging to that object; this list is 129 * maintained by the "vm_page" module, and locked by the object's 130 * lock. 131 * 132 * Each object also records a "pager" routine which is 133 * used to retrieve (and store) pages to the proper backing 134 * storage. In addition, objects may be backed by other 135 * objects from which they were virtual-copied. 136 * 137 * The only items within the object structure which are 138 * modified after time of creation are: 139 * reference count locked by object's lock 140 * pager routine locked by object's lock 141 * 142 */ 143 144 struct object_q vm_object_list; 145 struct mtx vm_object_list_mtx; /* lock for object list and count */ 146 147 struct vm_object kernel_object_store; 148 struct vm_object kmem_object_store; 149 150 SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats"); 151 152 static long object_collapses; 153 SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD, 154 &object_collapses, 0, "VM object collapses"); 155 156 static long object_bypasses; 157 SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD, 158 &object_bypasses, 0, "VM object bypasses"); 159 160 static uma_zone_t obj_zone; 161 162 static int vm_object_zinit(void *mem, int size, int flags); 163 164 #ifdef INVARIANTS 165 static void vm_object_zdtor(void *mem, int size, void *arg); 166 167 static void 168 vm_object_zdtor(void *mem, int size, void *arg) 169 { 170 vm_object_t object; 171 172 object = (vm_object_t)mem; 173 KASSERT(TAILQ_EMPTY(&object->memq), 174 ("object %p has resident pages", 175 object)); 176 #if VM_NRESERVLEVEL > 0 177 KASSERT(LIST_EMPTY(&object->rvq), 178 ("object %p has reservations", 179 object)); 180 #endif 181 KASSERT(object->cache == NULL, 182 ("object %p has cached pages", 183 object)); 184 KASSERT(object->paging_in_progress == 0, 185 ("object %p paging_in_progress = %d", 186 object, object->paging_in_progress)); 187 KASSERT(object->resident_page_count == 0, 188 ("object %p resident_page_count = %d", 189 object, object->resident_page_count)); 190 KASSERT(object->shadow_count == 0, 191 ("object %p shadow_count = %d", 192 object, object->shadow_count)); 193 } 194 #endif 195 196 static int 197 vm_object_zinit(void *mem, int size, int flags) 198 { 199 vm_object_t object; 200 201 object = (vm_object_t)mem; 202 bzero(&object->mtx, sizeof(object->mtx)); 203 VM_OBJECT_LOCK_INIT(object, "standard object"); 204 205 /* These are true for any object that has been freed */ 206 object->paging_in_progress = 0; 207 object->resident_page_count = 0; 208 object->shadow_count = 0; 209 return (0); 210 } 211 212 void 213 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object) 214 { 215 216 TAILQ_INIT(&object->memq); 217 LIST_INIT(&object->shadow_head); 218 219 object->root = NULL; 220 object->type = type; 221 object->size = size; 222 object->generation = 1; 223 object->ref_count = 1; 224 object->flags = 0; 225 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP)) 226 object->flags = OBJ_ONEMAPPING; 227 object->pg_color = 0; 228 object->handle = NULL; 229 object->backing_object = NULL; 230 object->backing_object_offset = (vm_ooffset_t) 0; 231 #if VM_NRESERVLEVEL > 0 232 LIST_INIT(&object->rvq); 233 #endif 234 object->cache = NULL; 235 236 mtx_lock(&vm_object_list_mtx); 237 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 238 mtx_unlock(&vm_object_list_mtx); 239 } 240 241 /* 242 * vm_object_init: 243 * 244 * Initialize the VM objects module. 245 */ 246 void 247 vm_object_init(void) 248 { 249 TAILQ_INIT(&vm_object_list); 250 mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF); 251 252 VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object"); 253 _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 254 kernel_object); 255 #if VM_NRESERVLEVEL > 0 256 kernel_object->flags |= OBJ_COLORED; 257 kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS); 258 #endif 259 260 VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object"); 261 _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 262 kmem_object); 263 #if VM_NRESERVLEVEL > 0 264 kmem_object->flags |= OBJ_COLORED; 265 kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS); 266 #endif 267 268 /* 269 * The lock portion of struct vm_object must be type stable due 270 * to vm_pageout_fallback_object_lock locking a vm object 271 * without holding any references to it. 272 */ 273 obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL, 274 #ifdef INVARIANTS 275 vm_object_zdtor, 276 #else 277 NULL, 278 #endif 279 vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE); 280 } 281 282 void 283 vm_object_clear_flag(vm_object_t object, u_short bits) 284 { 285 286 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 287 object->flags &= ~bits; 288 } 289 290 void 291 vm_object_pip_add(vm_object_t object, short i) 292 { 293 294 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 295 object->paging_in_progress += i; 296 } 297 298 void 299 vm_object_pip_subtract(vm_object_t object, short i) 300 { 301 302 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 303 object->paging_in_progress -= i; 304 } 305 306 void 307 vm_object_pip_wakeup(vm_object_t object) 308 { 309 310 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 311 object->paging_in_progress--; 312 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 313 vm_object_clear_flag(object, OBJ_PIPWNT); 314 wakeup(object); 315 } 316 } 317 318 void 319 vm_object_pip_wakeupn(vm_object_t object, short i) 320 { 321 322 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 323 if (i) 324 object->paging_in_progress -= i; 325 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 326 vm_object_clear_flag(object, OBJ_PIPWNT); 327 wakeup(object); 328 } 329 } 330 331 void 332 vm_object_pip_wait(vm_object_t object, char *waitid) 333 { 334 335 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 336 while (object->paging_in_progress) { 337 object->flags |= OBJ_PIPWNT; 338 msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0); 339 } 340 } 341 342 /* 343 * vm_object_allocate: 344 * 345 * Returns a new object with the given size. 346 */ 347 vm_object_t 348 vm_object_allocate(objtype_t type, vm_pindex_t size) 349 { 350 vm_object_t object; 351 352 object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK); 353 _vm_object_allocate(type, size, object); 354 return (object); 355 } 356 357 358 /* 359 * vm_object_reference: 360 * 361 * Gets another reference to the given object. Note: OBJ_DEAD 362 * objects can be referenced during final cleaning. 363 */ 364 void 365 vm_object_reference(vm_object_t object) 366 { 367 struct vnode *vp; 368 369 if (object == NULL) 370 return; 371 VM_OBJECT_LOCK(object); 372 object->ref_count++; 373 if (object->type == OBJT_VNODE) { 374 int vfslocked; 375 376 vp = object->handle; 377 VM_OBJECT_UNLOCK(object); 378 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 379 vget(vp, LK_RETRY, curthread); 380 VFS_UNLOCK_GIANT(vfslocked); 381 } else 382 VM_OBJECT_UNLOCK(object); 383 } 384 385 /* 386 * vm_object_reference_locked: 387 * 388 * Gets another reference to the given object. 389 * 390 * The object must be locked. 391 */ 392 void 393 vm_object_reference_locked(vm_object_t object) 394 { 395 struct vnode *vp; 396 397 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 398 KASSERT((object->flags & OBJ_DEAD) == 0, 399 ("vm_object_reference_locked: dead object referenced")); 400 object->ref_count++; 401 if (object->type == OBJT_VNODE) { 402 vp = object->handle; 403 vref(vp); 404 } 405 } 406 407 /* 408 * Handle deallocating an object of type OBJT_VNODE. 409 */ 410 static void 411 vm_object_vndeallocate(vm_object_t object) 412 { 413 struct vnode *vp = (struct vnode *) object->handle; 414 415 VFS_ASSERT_GIANT(vp->v_mount); 416 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 417 KASSERT(object->type == OBJT_VNODE, 418 ("vm_object_vndeallocate: not a vnode object")); 419 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 420 #ifdef INVARIANTS 421 if (object->ref_count == 0) { 422 vprint("vm_object_vndeallocate", vp); 423 panic("vm_object_vndeallocate: bad object reference count"); 424 } 425 #endif 426 427 object->ref_count--; 428 if (object->ref_count == 0) { 429 mp_fixme("Unlocked vflag access."); 430 vp->v_vflag &= ~VV_TEXT; 431 } 432 VM_OBJECT_UNLOCK(object); 433 /* 434 * vrele may need a vop lock 435 */ 436 vrele(vp); 437 } 438 439 /* 440 * vm_object_deallocate: 441 * 442 * Release a reference to the specified object, 443 * gained either through a vm_object_allocate 444 * or a vm_object_reference call. When all references 445 * are gone, storage associated with this object 446 * may be relinquished. 447 * 448 * No object may be locked. 449 */ 450 void 451 vm_object_deallocate(vm_object_t object) 452 { 453 vm_object_t temp; 454 455 while (object != NULL) { 456 int vfslocked; 457 458 vfslocked = 0; 459 restart: 460 VM_OBJECT_LOCK(object); 461 if (object->type == OBJT_VNODE) { 462 struct vnode *vp = (struct vnode *) object->handle; 463 464 /* 465 * Conditionally acquire Giant for a vnode-backed 466 * object. We have to be careful since the type of 467 * a vnode object can change while the object is 468 * unlocked. 469 */ 470 if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) { 471 vfslocked = 1; 472 if (!mtx_trylock(&Giant)) { 473 VM_OBJECT_UNLOCK(object); 474 mtx_lock(&Giant); 475 goto restart; 476 } 477 } 478 vm_object_vndeallocate(object); 479 VFS_UNLOCK_GIANT(vfslocked); 480 return; 481 } else 482 /* 483 * This is to handle the case that the object 484 * changed type while we dropped its lock to 485 * obtain Giant. 486 */ 487 VFS_UNLOCK_GIANT(vfslocked); 488 489 KASSERT(object->ref_count != 0, 490 ("vm_object_deallocate: object deallocated too many times: %d", object->type)); 491 492 /* 493 * If the reference count goes to 0 we start calling 494 * vm_object_terminate() on the object chain. 495 * A ref count of 1 may be a special case depending on the 496 * shadow count being 0 or 1. 497 */ 498 object->ref_count--; 499 if (object->ref_count > 1) { 500 VM_OBJECT_UNLOCK(object); 501 return; 502 } else if (object->ref_count == 1) { 503 if (object->shadow_count == 0) { 504 vm_object_set_flag(object, OBJ_ONEMAPPING); 505 } else if ((object->shadow_count == 1) && 506 (object->handle == NULL) && 507 (object->type == OBJT_DEFAULT || 508 object->type == OBJT_SWAP)) { 509 vm_object_t robject; 510 511 robject = LIST_FIRST(&object->shadow_head); 512 KASSERT(robject != NULL, 513 ("vm_object_deallocate: ref_count: %d, shadow_count: %d", 514 object->ref_count, 515 object->shadow_count)); 516 if (!VM_OBJECT_TRYLOCK(robject)) { 517 /* 518 * Avoid a potential deadlock. 519 */ 520 object->ref_count++; 521 VM_OBJECT_UNLOCK(object); 522 /* 523 * More likely than not the thread 524 * holding robject's lock has lower 525 * priority than the current thread. 526 * Let the lower priority thread run. 527 */ 528 pause("vmo_de", 1); 529 continue; 530 } 531 /* 532 * Collapse object into its shadow unless its 533 * shadow is dead. In that case, object will 534 * be deallocated by the thread that is 535 * deallocating its shadow. 536 */ 537 if ((robject->flags & OBJ_DEAD) == 0 && 538 (robject->handle == NULL) && 539 (robject->type == OBJT_DEFAULT || 540 robject->type == OBJT_SWAP)) { 541 542 robject->ref_count++; 543 retry: 544 if (robject->paging_in_progress) { 545 VM_OBJECT_UNLOCK(object); 546 vm_object_pip_wait(robject, 547 "objde1"); 548 temp = robject->backing_object; 549 if (object == temp) { 550 VM_OBJECT_LOCK(object); 551 goto retry; 552 } 553 } else if (object->paging_in_progress) { 554 VM_OBJECT_UNLOCK(robject); 555 object->flags |= OBJ_PIPWNT; 556 msleep(object, 557 VM_OBJECT_MTX(object), 558 PDROP | PVM, "objde2", 0); 559 VM_OBJECT_LOCK(robject); 560 temp = robject->backing_object; 561 if (object == temp) { 562 VM_OBJECT_LOCK(object); 563 goto retry; 564 } 565 } else 566 VM_OBJECT_UNLOCK(object); 567 568 if (robject->ref_count == 1) { 569 robject->ref_count--; 570 object = robject; 571 goto doterm; 572 } 573 object = robject; 574 vm_object_collapse(object); 575 VM_OBJECT_UNLOCK(object); 576 continue; 577 } 578 VM_OBJECT_UNLOCK(robject); 579 } 580 VM_OBJECT_UNLOCK(object); 581 return; 582 } 583 doterm: 584 temp = object->backing_object; 585 if (temp != NULL) { 586 VM_OBJECT_LOCK(temp); 587 LIST_REMOVE(object, shadow_list); 588 temp->shadow_count--; 589 temp->generation++; 590 VM_OBJECT_UNLOCK(temp); 591 object->backing_object = NULL; 592 } 593 /* 594 * Don't double-terminate, we could be in a termination 595 * recursion due to the terminate having to sync data 596 * to disk. 597 */ 598 if ((object->flags & OBJ_DEAD) == 0) 599 vm_object_terminate(object); 600 else 601 VM_OBJECT_UNLOCK(object); 602 object = temp; 603 } 604 } 605 606 /* 607 * vm_object_terminate actually destroys the specified object, freeing 608 * up all previously used resources. 609 * 610 * The object must be locked. 611 * This routine may block. 612 */ 613 void 614 vm_object_terminate(vm_object_t object) 615 { 616 vm_page_t p; 617 618 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 619 620 /* 621 * Make sure no one uses us. 622 */ 623 vm_object_set_flag(object, OBJ_DEAD); 624 625 /* 626 * wait for the pageout daemon to be done with the object 627 */ 628 vm_object_pip_wait(object, "objtrm"); 629 630 KASSERT(!object->paging_in_progress, 631 ("vm_object_terminate: pageout in progress")); 632 633 /* 634 * Clean and free the pages, as appropriate. All references to the 635 * object are gone, so we don't need to lock it. 636 */ 637 if (object->type == OBJT_VNODE) { 638 struct vnode *vp = (struct vnode *)object->handle; 639 640 /* 641 * Clean pages and flush buffers. 642 */ 643 vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 644 VM_OBJECT_UNLOCK(object); 645 646 vinvalbuf(vp, V_SAVE, NULL, 0, 0); 647 648 VM_OBJECT_LOCK(object); 649 } 650 651 KASSERT(object->ref_count == 0, 652 ("vm_object_terminate: object with references, ref_count=%d", 653 object->ref_count)); 654 655 /* 656 * Now free any remaining pages. For internal objects, this also 657 * removes them from paging queues. Don't free wired pages, just 658 * remove them from the object. 659 */ 660 vm_page_lock_queues(); 661 while ((p = TAILQ_FIRST(&object->memq)) != NULL) { 662 KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0, 663 ("vm_object_terminate: freeing busy page %p " 664 "p->busy = %d, p->flags %x\n", p, p->busy, p->flags)); 665 if (p->wire_count == 0) { 666 vm_page_free(p); 667 cnt.v_pfree++; 668 } else { 669 vm_page_remove(p); 670 } 671 } 672 vm_page_unlock_queues(); 673 674 #if VM_NRESERVLEVEL > 0 675 if (__predict_false(!LIST_EMPTY(&object->rvq))) 676 vm_reserv_break_all(object); 677 #endif 678 if (__predict_false(object->cache != NULL)) 679 vm_page_cache_free(object, 0, 0); 680 681 /* 682 * Let the pager know object is dead. 683 */ 684 vm_pager_deallocate(object); 685 VM_OBJECT_UNLOCK(object); 686 687 /* 688 * Remove the object from the global object list. 689 */ 690 mtx_lock(&vm_object_list_mtx); 691 TAILQ_REMOVE(&vm_object_list, object, object_list); 692 mtx_unlock(&vm_object_list_mtx); 693 694 /* 695 * Free the space for the object. 696 */ 697 uma_zfree(obj_zone, object); 698 } 699 700 /* 701 * vm_object_page_clean 702 * 703 * Clean all dirty pages in the specified range of object. Leaves page 704 * on whatever queue it is currently on. If NOSYNC is set then do not 705 * write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC), 706 * leaving the object dirty. 707 * 708 * When stuffing pages asynchronously, allow clustering. XXX we need a 709 * synchronous clustering mode implementation. 710 * 711 * Odd semantics: if start == end, we clean everything. 712 * 713 * The object must be locked. 714 */ 715 void 716 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags) 717 { 718 vm_page_t p, np; 719 vm_pindex_t tstart, tend; 720 vm_pindex_t pi; 721 int clearobjflags; 722 int pagerflags; 723 int curgeneration; 724 725 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 726 if (object->type != OBJT_VNODE || 727 (object->flags & OBJ_MIGHTBEDIRTY) == 0) 728 return; 729 730 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 731 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0; 732 733 vm_object_set_flag(object, OBJ_CLEANING); 734 735 tstart = start; 736 if (end == 0) { 737 tend = object->size; 738 } else { 739 tend = end; 740 } 741 742 vm_page_lock_queues(); 743 /* 744 * If the caller is smart and only msync()s a range he knows is 745 * dirty, we may be able to avoid an object scan. This results in 746 * a phenominal improvement in performance. We cannot do this 747 * as a matter of course because the object may be huge - e.g. 748 * the size might be in the gigabytes or terrabytes. 749 */ 750 if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) { 751 vm_pindex_t tscan; 752 int scanlimit; 753 int scanreset; 754 755 scanreset = object->resident_page_count / EASY_SCAN_FACTOR; 756 if (scanreset < 16) 757 scanreset = 16; 758 pagerflags |= VM_PAGER_IGNORE_CLEANCHK; 759 760 scanlimit = scanreset; 761 tscan = tstart; 762 while (tscan < tend) { 763 curgeneration = object->generation; 764 p = vm_page_lookup(object, tscan); 765 if (p == NULL || p->valid == 0) { 766 if (--scanlimit == 0) 767 break; 768 ++tscan; 769 continue; 770 } 771 vm_page_test_dirty(p); 772 if ((p->dirty & p->valid) == 0) { 773 if (--scanlimit == 0) 774 break; 775 ++tscan; 776 continue; 777 } 778 /* 779 * If we have been asked to skip nosync pages and 780 * this is a nosync page, we can't continue. 781 */ 782 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) { 783 if (--scanlimit == 0) 784 break; 785 ++tscan; 786 continue; 787 } 788 scanlimit = scanreset; 789 790 /* 791 * This returns 0 if it was unable to busy the first 792 * page (i.e. had to sleep). 793 */ 794 tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags); 795 } 796 797 /* 798 * If everything was dirty and we flushed it successfully, 799 * and the requested range is not the entire object, we 800 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can 801 * return immediately. 802 */ 803 if (tscan >= tend && (tstart || tend < object->size)) { 804 vm_page_unlock_queues(); 805 vm_object_clear_flag(object, OBJ_CLEANING); 806 return; 807 } 808 pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK; 809 } 810 811 /* 812 * Generally set CLEANCHK interlock and make the page read-only so 813 * we can then clear the object flags. 814 * 815 * However, if this is a nosync mmap then the object is likely to 816 * stay dirty so do not mess with the page and do not clear the 817 * object flags. 818 */ 819 clearobjflags = 1; 820 TAILQ_FOREACH(p, &object->memq, listq) { 821 p->oflags |= VPO_CLEANCHK; 822 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) 823 clearobjflags = 0; 824 else 825 pmap_remove_write(p); 826 } 827 828 if (clearobjflags && (tstart == 0) && (tend == object->size)) { 829 struct vnode *vp; 830 831 vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY); 832 if (object->type == OBJT_VNODE && 833 (vp = (struct vnode *)object->handle) != NULL) { 834 VI_LOCK(vp); 835 if (vp->v_iflag & VI_OBJDIRTY) 836 vp->v_iflag &= ~VI_OBJDIRTY; 837 VI_UNLOCK(vp); 838 } 839 } 840 841 rescan: 842 curgeneration = object->generation; 843 844 for (p = TAILQ_FIRST(&object->memq); p; p = np) { 845 int n; 846 847 np = TAILQ_NEXT(p, listq); 848 849 again: 850 pi = p->pindex; 851 if ((p->oflags & VPO_CLEANCHK) == 0 || 852 (pi < tstart) || (pi >= tend) || 853 p->valid == 0) { 854 p->oflags &= ~VPO_CLEANCHK; 855 continue; 856 } 857 858 vm_page_test_dirty(p); 859 if ((p->dirty & p->valid) == 0) { 860 p->oflags &= ~VPO_CLEANCHK; 861 continue; 862 } 863 864 /* 865 * If we have been asked to skip nosync pages and this is a 866 * nosync page, skip it. Note that the object flags were 867 * not cleared in this case so we do not have to set them. 868 */ 869 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) { 870 p->oflags &= ~VPO_CLEANCHK; 871 continue; 872 } 873 874 n = vm_object_page_collect_flush(object, p, 875 curgeneration, pagerflags); 876 if (n == 0) 877 goto rescan; 878 879 if (object->generation != curgeneration) 880 goto rescan; 881 882 /* 883 * Try to optimize the next page. If we can't we pick up 884 * our (random) scan where we left off. 885 */ 886 if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) { 887 if ((p = vm_page_lookup(object, pi + n)) != NULL) 888 goto again; 889 } 890 } 891 vm_page_unlock_queues(); 892 #if 0 893 VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc); 894 #endif 895 896 vm_object_clear_flag(object, OBJ_CLEANING); 897 return; 898 } 899 900 static int 901 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags) 902 { 903 int runlen; 904 int maxf; 905 int chkb; 906 int maxb; 907 int i; 908 vm_pindex_t pi; 909 vm_page_t maf[vm_pageout_page_count]; 910 vm_page_t mab[vm_pageout_page_count]; 911 vm_page_t ma[vm_pageout_page_count]; 912 913 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 914 pi = p->pindex; 915 while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) { 916 vm_page_lock_queues(); 917 if (object->generation != curgeneration) { 918 return(0); 919 } 920 } 921 maxf = 0; 922 for(i = 1; i < vm_pageout_page_count; i++) { 923 vm_page_t tp; 924 925 if ((tp = vm_page_lookup(object, pi + i)) != NULL) { 926 if ((tp->oflags & VPO_BUSY) || 927 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 928 (tp->oflags & VPO_CLEANCHK) == 0) || 929 (tp->busy != 0)) 930 break; 931 vm_page_test_dirty(tp); 932 if ((tp->dirty & tp->valid) == 0) { 933 tp->oflags &= ~VPO_CLEANCHK; 934 break; 935 } 936 maf[ i - 1 ] = tp; 937 maxf++; 938 continue; 939 } 940 break; 941 } 942 943 maxb = 0; 944 chkb = vm_pageout_page_count - maxf; 945 if (chkb) { 946 for(i = 1; i < chkb;i++) { 947 vm_page_t tp; 948 949 if ((tp = vm_page_lookup(object, pi - i)) != NULL) { 950 if ((tp->oflags & VPO_BUSY) || 951 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 952 (tp->oflags & VPO_CLEANCHK) == 0) || 953 (tp->busy != 0)) 954 break; 955 vm_page_test_dirty(tp); 956 if ((tp->dirty & tp->valid) == 0) { 957 tp->oflags &= ~VPO_CLEANCHK; 958 break; 959 } 960 mab[ i - 1 ] = tp; 961 maxb++; 962 continue; 963 } 964 break; 965 } 966 } 967 968 for(i = 0; i < maxb; i++) { 969 int index = (maxb - i) - 1; 970 ma[index] = mab[i]; 971 ma[index]->oflags &= ~VPO_CLEANCHK; 972 } 973 p->oflags &= ~VPO_CLEANCHK; 974 ma[maxb] = p; 975 for(i = 0; i < maxf; i++) { 976 int index = (maxb + i) + 1; 977 ma[index] = maf[i]; 978 ma[index]->oflags &= ~VPO_CLEANCHK; 979 } 980 runlen = maxb + maxf + 1; 981 982 vm_pageout_flush(ma, runlen, pagerflags); 983 for (i = 0; i < runlen; i++) { 984 if (ma[i]->valid & ma[i]->dirty) { 985 pmap_remove_write(ma[i]); 986 ma[i]->oflags |= VPO_CLEANCHK; 987 988 /* 989 * maxf will end up being the actual number of pages 990 * we wrote out contiguously, non-inclusive of the 991 * first page. We do not count look-behind pages. 992 */ 993 if (i >= maxb + 1 && (maxf > i - maxb - 1)) 994 maxf = i - maxb - 1; 995 } 996 } 997 return(maxf + 1); 998 } 999 1000 /* 1001 * Note that there is absolutely no sense in writing out 1002 * anonymous objects, so we track down the vnode object 1003 * to write out. 1004 * We invalidate (remove) all pages from the address space 1005 * for semantic correctness. 1006 * 1007 * Note: certain anonymous maps, such as MAP_NOSYNC maps, 1008 * may start out with a NULL object. 1009 */ 1010 void 1011 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size, 1012 boolean_t syncio, boolean_t invalidate) 1013 { 1014 vm_object_t backing_object; 1015 struct vnode *vp; 1016 struct mount *mp; 1017 int flags; 1018 1019 if (object == NULL) 1020 return; 1021 VM_OBJECT_LOCK(object); 1022 while ((backing_object = object->backing_object) != NULL) { 1023 VM_OBJECT_LOCK(backing_object); 1024 offset += object->backing_object_offset; 1025 VM_OBJECT_UNLOCK(object); 1026 object = backing_object; 1027 if (object->size < OFF_TO_IDX(offset + size)) 1028 size = IDX_TO_OFF(object->size) - offset; 1029 } 1030 /* 1031 * Flush pages if writing is allowed, invalidate them 1032 * if invalidation requested. Pages undergoing I/O 1033 * will be ignored by vm_object_page_remove(). 1034 * 1035 * We cannot lock the vnode and then wait for paging 1036 * to complete without deadlocking against vm_fault. 1037 * Instead we simply call vm_object_page_remove() and 1038 * allow it to block internally on a page-by-page 1039 * basis when it encounters pages undergoing async 1040 * I/O. 1041 */ 1042 if (object->type == OBJT_VNODE && 1043 (object->flags & OBJ_MIGHTBEDIRTY) != 0) { 1044 int vfslocked; 1045 vp = object->handle; 1046 VM_OBJECT_UNLOCK(object); 1047 (void) vn_start_write(vp, &mp, V_WAIT); 1048 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1049 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1050 flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 1051 flags |= invalidate ? OBJPC_INVAL : 0; 1052 VM_OBJECT_LOCK(object); 1053 vm_object_page_clean(object, 1054 OFF_TO_IDX(offset), 1055 OFF_TO_IDX(offset + size + PAGE_MASK), 1056 flags); 1057 VM_OBJECT_UNLOCK(object); 1058 VOP_UNLOCK(vp, 0); 1059 VFS_UNLOCK_GIANT(vfslocked); 1060 vn_finished_write(mp); 1061 VM_OBJECT_LOCK(object); 1062 } 1063 if ((object->type == OBJT_VNODE || 1064 object->type == OBJT_DEVICE) && invalidate) { 1065 boolean_t purge; 1066 purge = old_msync || (object->type == OBJT_DEVICE); 1067 vm_object_page_remove(object, 1068 OFF_TO_IDX(offset), 1069 OFF_TO_IDX(offset + size + PAGE_MASK), 1070 purge ? FALSE : TRUE); 1071 } 1072 VM_OBJECT_UNLOCK(object); 1073 } 1074 1075 /* 1076 * vm_object_madvise: 1077 * 1078 * Implements the madvise function at the object/page level. 1079 * 1080 * MADV_WILLNEED (any object) 1081 * 1082 * Activate the specified pages if they are resident. 1083 * 1084 * MADV_DONTNEED (any object) 1085 * 1086 * Deactivate the specified pages if they are resident. 1087 * 1088 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 1089 * OBJ_ONEMAPPING only) 1090 * 1091 * Deactivate and clean the specified pages if they are 1092 * resident. This permits the process to reuse the pages 1093 * without faulting or the kernel to reclaim the pages 1094 * without I/O. 1095 */ 1096 void 1097 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise) 1098 { 1099 vm_pindex_t end, tpindex; 1100 vm_object_t backing_object, tobject; 1101 vm_page_t m; 1102 1103 if (object == NULL) 1104 return; 1105 VM_OBJECT_LOCK(object); 1106 end = pindex + count; 1107 /* 1108 * Locate and adjust resident pages 1109 */ 1110 for (; pindex < end; pindex += 1) { 1111 relookup: 1112 tobject = object; 1113 tpindex = pindex; 1114 shadowlookup: 1115 /* 1116 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages 1117 * and those pages must be OBJ_ONEMAPPING. 1118 */ 1119 if (advise == MADV_FREE) { 1120 if ((tobject->type != OBJT_DEFAULT && 1121 tobject->type != OBJT_SWAP) || 1122 (tobject->flags & OBJ_ONEMAPPING) == 0) { 1123 goto unlock_tobject; 1124 } 1125 } 1126 m = vm_page_lookup(tobject, tpindex); 1127 if (m == NULL && advise == MADV_WILLNEED) { 1128 /* 1129 * If the page is cached, reactivate it. 1130 */ 1131 m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED | 1132 VM_ALLOC_NOBUSY); 1133 } 1134 if (m == NULL) { 1135 /* 1136 * There may be swap even if there is no backing page 1137 */ 1138 if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 1139 swap_pager_freespace(tobject, tpindex, 1); 1140 /* 1141 * next object 1142 */ 1143 backing_object = tobject->backing_object; 1144 if (backing_object == NULL) 1145 goto unlock_tobject; 1146 VM_OBJECT_LOCK(backing_object); 1147 tpindex += OFF_TO_IDX(tobject->backing_object_offset); 1148 if (tobject != object) 1149 VM_OBJECT_UNLOCK(tobject); 1150 tobject = backing_object; 1151 goto shadowlookup; 1152 } 1153 /* 1154 * If the page is busy or not in a normal active state, 1155 * we skip it. If the page is not managed there are no 1156 * page queues to mess with. Things can break if we mess 1157 * with pages in any of the below states. 1158 */ 1159 vm_page_lock_queues(); 1160 if (m->hold_count || 1161 m->wire_count || 1162 (m->flags & PG_UNMANAGED) || 1163 m->valid != VM_PAGE_BITS_ALL) { 1164 vm_page_unlock_queues(); 1165 goto unlock_tobject; 1166 } 1167 if ((m->oflags & VPO_BUSY) || m->busy) { 1168 vm_page_flag_set(m, PG_REFERENCED); 1169 vm_page_unlock_queues(); 1170 if (object != tobject) 1171 VM_OBJECT_UNLOCK(object); 1172 m->oflags |= VPO_WANTED; 1173 msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo", 0); 1174 VM_OBJECT_LOCK(object); 1175 goto relookup; 1176 } 1177 if (advise == MADV_WILLNEED) { 1178 vm_page_activate(m); 1179 } else if (advise == MADV_DONTNEED) { 1180 vm_page_dontneed(m); 1181 } else if (advise == MADV_FREE) { 1182 /* 1183 * Mark the page clean. This will allow the page 1184 * to be freed up by the system. However, such pages 1185 * are often reused quickly by malloc()/free() 1186 * so we do not do anything that would cause 1187 * a page fault if we can help it. 1188 * 1189 * Specifically, we do not try to actually free 1190 * the page now nor do we try to put it in the 1191 * cache (which would cause a page fault on reuse). 1192 * 1193 * But we do make the page is freeable as we 1194 * can without actually taking the step of unmapping 1195 * it. 1196 */ 1197 pmap_clear_modify(m); 1198 m->dirty = 0; 1199 m->act_count = 0; 1200 vm_page_dontneed(m); 1201 } 1202 vm_page_unlock_queues(); 1203 if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 1204 swap_pager_freespace(tobject, tpindex, 1); 1205 unlock_tobject: 1206 if (tobject != object) 1207 VM_OBJECT_UNLOCK(tobject); 1208 } 1209 VM_OBJECT_UNLOCK(object); 1210 } 1211 1212 /* 1213 * vm_object_shadow: 1214 * 1215 * Create a new object which is backed by the 1216 * specified existing object range. The source 1217 * object reference is deallocated. 1218 * 1219 * The new object and offset into that object 1220 * are returned in the source parameters. 1221 */ 1222 void 1223 vm_object_shadow( 1224 vm_object_t *object, /* IN/OUT */ 1225 vm_ooffset_t *offset, /* IN/OUT */ 1226 vm_size_t length) 1227 { 1228 vm_object_t source; 1229 vm_object_t result; 1230 1231 source = *object; 1232 1233 /* 1234 * Don't create the new object if the old object isn't shared. 1235 */ 1236 if (source != NULL) { 1237 VM_OBJECT_LOCK(source); 1238 if (source->ref_count == 1 && 1239 source->handle == NULL && 1240 (source->type == OBJT_DEFAULT || 1241 source->type == OBJT_SWAP)) { 1242 VM_OBJECT_UNLOCK(source); 1243 return; 1244 } 1245 VM_OBJECT_UNLOCK(source); 1246 } 1247 1248 /* 1249 * Allocate a new object with the given length. 1250 */ 1251 result = vm_object_allocate(OBJT_DEFAULT, length); 1252 1253 /* 1254 * The new object shadows the source object, adding a reference to it. 1255 * Our caller changes his reference to point to the new object, 1256 * removing a reference to the source object. Net result: no change 1257 * of reference count. 1258 * 1259 * Try to optimize the result object's page color when shadowing 1260 * in order to maintain page coloring consistency in the combined 1261 * shadowed object. 1262 */ 1263 result->backing_object = source; 1264 /* 1265 * Store the offset into the source object, and fix up the offset into 1266 * the new object. 1267 */ 1268 result->backing_object_offset = *offset; 1269 if (source != NULL) { 1270 VM_OBJECT_LOCK(source); 1271 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list); 1272 source->shadow_count++; 1273 source->generation++; 1274 #if VM_NRESERVLEVEL > 0 1275 result->flags |= source->flags & (OBJ_NEEDGIANT | OBJ_COLORED); 1276 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & 1277 ((1 << (VM_NFREEORDER - 1)) - 1); 1278 #else 1279 result->flags |= source->flags & OBJ_NEEDGIANT; 1280 #endif 1281 VM_OBJECT_UNLOCK(source); 1282 } 1283 1284 1285 /* 1286 * Return the new things 1287 */ 1288 *offset = 0; 1289 *object = result; 1290 } 1291 1292 /* 1293 * vm_object_split: 1294 * 1295 * Split the pages in a map entry into a new object. This affords 1296 * easier removal of unused pages, and keeps object inheritance from 1297 * being a negative impact on memory usage. 1298 */ 1299 void 1300 vm_object_split(vm_map_entry_t entry) 1301 { 1302 vm_page_t m, m_next; 1303 vm_object_t orig_object, new_object, source; 1304 vm_pindex_t idx, offidxstart; 1305 vm_size_t size; 1306 1307 orig_object = entry->object.vm_object; 1308 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 1309 return; 1310 if (orig_object->ref_count <= 1) 1311 return; 1312 VM_OBJECT_UNLOCK(orig_object); 1313 1314 offidxstart = OFF_TO_IDX(entry->offset); 1315 size = atop(entry->end - entry->start); 1316 1317 /* 1318 * If swap_pager_copy() is later called, it will convert new_object 1319 * into a swap object. 1320 */ 1321 new_object = vm_object_allocate(OBJT_DEFAULT, size); 1322 1323 /* 1324 * At this point, the new object is still private, so the order in 1325 * which the original and new objects are locked does not matter. 1326 */ 1327 VM_OBJECT_LOCK(new_object); 1328 VM_OBJECT_LOCK(orig_object); 1329 source = orig_object->backing_object; 1330 if (source != NULL) { 1331 VM_OBJECT_LOCK(source); 1332 if ((source->flags & OBJ_DEAD) != 0) { 1333 VM_OBJECT_UNLOCK(source); 1334 VM_OBJECT_UNLOCK(orig_object); 1335 VM_OBJECT_UNLOCK(new_object); 1336 vm_object_deallocate(new_object); 1337 VM_OBJECT_LOCK(orig_object); 1338 return; 1339 } 1340 LIST_INSERT_HEAD(&source->shadow_head, 1341 new_object, shadow_list); 1342 source->shadow_count++; 1343 source->generation++; 1344 vm_object_reference_locked(source); /* for new_object */ 1345 vm_object_clear_flag(source, OBJ_ONEMAPPING); 1346 VM_OBJECT_UNLOCK(source); 1347 new_object->backing_object_offset = 1348 orig_object->backing_object_offset + entry->offset; 1349 new_object->backing_object = source; 1350 } 1351 new_object->flags |= orig_object->flags & OBJ_NEEDGIANT; 1352 retry: 1353 if ((m = TAILQ_FIRST(&orig_object->memq)) != NULL) { 1354 if (m->pindex < offidxstart) { 1355 m = vm_page_splay(offidxstart, orig_object->root); 1356 if ((orig_object->root = m)->pindex < offidxstart) 1357 m = TAILQ_NEXT(m, listq); 1358 } 1359 } 1360 vm_page_lock_queues(); 1361 for (; m != NULL && (idx = m->pindex - offidxstart) < size; 1362 m = m_next) { 1363 m_next = TAILQ_NEXT(m, listq); 1364 1365 /* 1366 * We must wait for pending I/O to complete before we can 1367 * rename the page. 1368 * 1369 * We do not have to VM_PROT_NONE the page as mappings should 1370 * not be changed by this operation. 1371 */ 1372 if ((m->oflags & VPO_BUSY) || m->busy) { 1373 vm_page_flag_set(m, PG_REFERENCED); 1374 vm_page_unlock_queues(); 1375 VM_OBJECT_UNLOCK(new_object); 1376 m->oflags |= VPO_WANTED; 1377 msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0); 1378 VM_OBJECT_LOCK(new_object); 1379 goto retry; 1380 } 1381 vm_page_rename(m, new_object, idx); 1382 /* page automatically made dirty by rename and cache handled */ 1383 vm_page_busy(m); 1384 } 1385 vm_page_unlock_queues(); 1386 if (orig_object->type == OBJT_SWAP) { 1387 /* 1388 * swap_pager_copy() can sleep, in which case the orig_object's 1389 * and new_object's locks are released and reacquired. 1390 */ 1391 swap_pager_copy(orig_object, new_object, offidxstart, 0); 1392 1393 /* 1394 * Transfer any cached pages from orig_object to new_object. 1395 */ 1396 if (__predict_false(orig_object->cache != NULL)) 1397 vm_page_cache_transfer(orig_object, offidxstart, 1398 new_object); 1399 } 1400 VM_OBJECT_UNLOCK(orig_object); 1401 TAILQ_FOREACH(m, &new_object->memq, listq) 1402 vm_page_wakeup(m); 1403 VM_OBJECT_UNLOCK(new_object); 1404 entry->object.vm_object = new_object; 1405 entry->offset = 0LL; 1406 vm_object_deallocate(orig_object); 1407 VM_OBJECT_LOCK(new_object); 1408 } 1409 1410 #define OBSC_TEST_ALL_SHADOWED 0x0001 1411 #define OBSC_COLLAPSE_NOWAIT 0x0002 1412 #define OBSC_COLLAPSE_WAIT 0x0004 1413 1414 static int 1415 vm_object_backing_scan(vm_object_t object, int op) 1416 { 1417 int r = 1; 1418 vm_page_t p; 1419 vm_object_t backing_object; 1420 vm_pindex_t backing_offset_index; 1421 1422 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1423 VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED); 1424 1425 backing_object = object->backing_object; 1426 backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 1427 1428 /* 1429 * Initial conditions 1430 */ 1431 if (op & OBSC_TEST_ALL_SHADOWED) { 1432 /* 1433 * We do not want to have to test for the existence of cache 1434 * or swap pages in the backing object. XXX but with the 1435 * new swapper this would be pretty easy to do. 1436 * 1437 * XXX what about anonymous MAP_SHARED memory that hasn't 1438 * been ZFOD faulted yet? If we do not test for this, the 1439 * shadow test may succeed! XXX 1440 */ 1441 if (backing_object->type != OBJT_DEFAULT) { 1442 return (0); 1443 } 1444 } 1445 if (op & OBSC_COLLAPSE_WAIT) { 1446 vm_object_set_flag(backing_object, OBJ_DEAD); 1447 } 1448 1449 /* 1450 * Our scan 1451 */ 1452 p = TAILQ_FIRST(&backing_object->memq); 1453 while (p) { 1454 vm_page_t next = TAILQ_NEXT(p, listq); 1455 vm_pindex_t new_pindex = p->pindex - backing_offset_index; 1456 1457 if (op & OBSC_TEST_ALL_SHADOWED) { 1458 vm_page_t pp; 1459 1460 /* 1461 * Ignore pages outside the parent object's range 1462 * and outside the parent object's mapping of the 1463 * backing object. 1464 * 1465 * note that we do not busy the backing object's 1466 * page. 1467 */ 1468 if ( 1469 p->pindex < backing_offset_index || 1470 new_pindex >= object->size 1471 ) { 1472 p = next; 1473 continue; 1474 } 1475 1476 /* 1477 * See if the parent has the page or if the parent's 1478 * object pager has the page. If the parent has the 1479 * page but the page is not valid, the parent's 1480 * object pager must have the page. 1481 * 1482 * If this fails, the parent does not completely shadow 1483 * the object and we might as well give up now. 1484 */ 1485 1486 pp = vm_page_lookup(object, new_pindex); 1487 if ( 1488 (pp == NULL || pp->valid == 0) && 1489 !vm_pager_has_page(object, new_pindex, NULL, NULL) 1490 ) { 1491 r = 0; 1492 break; 1493 } 1494 } 1495 1496 /* 1497 * Check for busy page 1498 */ 1499 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) { 1500 vm_page_t pp; 1501 1502 if (op & OBSC_COLLAPSE_NOWAIT) { 1503 if ((p->oflags & VPO_BUSY) || 1504 !p->valid || 1505 p->busy) { 1506 p = next; 1507 continue; 1508 } 1509 } else if (op & OBSC_COLLAPSE_WAIT) { 1510 if ((p->oflags & VPO_BUSY) || p->busy) { 1511 vm_page_lock_queues(); 1512 vm_page_flag_set(p, PG_REFERENCED); 1513 vm_page_unlock_queues(); 1514 VM_OBJECT_UNLOCK(object); 1515 p->oflags |= VPO_WANTED; 1516 msleep(p, VM_OBJECT_MTX(backing_object), 1517 PDROP | PVM, "vmocol", 0); 1518 VM_OBJECT_LOCK(object); 1519 VM_OBJECT_LOCK(backing_object); 1520 /* 1521 * If we slept, anything could have 1522 * happened. Since the object is 1523 * marked dead, the backing offset 1524 * should not have changed so we 1525 * just restart our scan. 1526 */ 1527 p = TAILQ_FIRST(&backing_object->memq); 1528 continue; 1529 } 1530 } 1531 1532 KASSERT( 1533 p->object == backing_object, 1534 ("vm_object_backing_scan: object mismatch") 1535 ); 1536 1537 /* 1538 * Destroy any associated swap 1539 */ 1540 if (backing_object->type == OBJT_SWAP) { 1541 swap_pager_freespace( 1542 backing_object, 1543 p->pindex, 1544 1 1545 ); 1546 } 1547 1548 if ( 1549 p->pindex < backing_offset_index || 1550 new_pindex >= object->size 1551 ) { 1552 /* 1553 * Page is out of the parent object's range, we 1554 * can simply destroy it. 1555 */ 1556 vm_page_lock_queues(); 1557 KASSERT(!pmap_page_is_mapped(p), 1558 ("freeing mapped page %p", p)); 1559 if (p->wire_count == 0) 1560 vm_page_free(p); 1561 else 1562 vm_page_remove(p); 1563 vm_page_unlock_queues(); 1564 p = next; 1565 continue; 1566 } 1567 1568 pp = vm_page_lookup(object, new_pindex); 1569 if ( 1570 pp != NULL || 1571 vm_pager_has_page(object, new_pindex, NULL, NULL) 1572 ) { 1573 /* 1574 * page already exists in parent OR swap exists 1575 * for this location in the parent. Destroy 1576 * the original page from the backing object. 1577 * 1578 * Leave the parent's page alone 1579 */ 1580 vm_page_lock_queues(); 1581 KASSERT(!pmap_page_is_mapped(p), 1582 ("freeing mapped page %p", p)); 1583 if (p->wire_count == 0) 1584 vm_page_free(p); 1585 else 1586 vm_page_remove(p); 1587 vm_page_unlock_queues(); 1588 p = next; 1589 continue; 1590 } 1591 1592 #if VM_NRESERVLEVEL > 0 1593 /* 1594 * Rename the reservation. 1595 */ 1596 vm_reserv_rename(p, object, backing_object, 1597 backing_offset_index); 1598 #endif 1599 1600 /* 1601 * Page does not exist in parent, rename the 1602 * page from the backing object to the main object. 1603 * 1604 * If the page was mapped to a process, it can remain 1605 * mapped through the rename. 1606 */ 1607 vm_page_lock_queues(); 1608 vm_page_rename(p, object, new_pindex); 1609 vm_page_unlock_queues(); 1610 /* page automatically made dirty by rename */ 1611 } 1612 p = next; 1613 } 1614 return (r); 1615 } 1616 1617 1618 /* 1619 * this version of collapse allows the operation to occur earlier and 1620 * when paging_in_progress is true for an object... This is not a complete 1621 * operation, but should plug 99.9% of the rest of the leaks. 1622 */ 1623 static void 1624 vm_object_qcollapse(vm_object_t object) 1625 { 1626 vm_object_t backing_object = object->backing_object; 1627 1628 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1629 VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED); 1630 1631 if (backing_object->ref_count != 1) 1632 return; 1633 1634 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT); 1635 } 1636 1637 /* 1638 * vm_object_collapse: 1639 * 1640 * Collapse an object with the object backing it. 1641 * Pages in the backing object are moved into the 1642 * parent, and the backing object is deallocated. 1643 */ 1644 void 1645 vm_object_collapse(vm_object_t object) 1646 { 1647 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1648 1649 while (TRUE) { 1650 vm_object_t backing_object; 1651 1652 /* 1653 * Verify that the conditions are right for collapse: 1654 * 1655 * The object exists and the backing object exists. 1656 */ 1657 if ((backing_object = object->backing_object) == NULL) 1658 break; 1659 1660 /* 1661 * we check the backing object first, because it is most likely 1662 * not collapsable. 1663 */ 1664 VM_OBJECT_LOCK(backing_object); 1665 if (backing_object->handle != NULL || 1666 (backing_object->type != OBJT_DEFAULT && 1667 backing_object->type != OBJT_SWAP) || 1668 (backing_object->flags & OBJ_DEAD) || 1669 object->handle != NULL || 1670 (object->type != OBJT_DEFAULT && 1671 object->type != OBJT_SWAP) || 1672 (object->flags & OBJ_DEAD)) { 1673 VM_OBJECT_UNLOCK(backing_object); 1674 break; 1675 } 1676 1677 if ( 1678 object->paging_in_progress != 0 || 1679 backing_object->paging_in_progress != 0 1680 ) { 1681 vm_object_qcollapse(object); 1682 VM_OBJECT_UNLOCK(backing_object); 1683 break; 1684 } 1685 /* 1686 * We know that we can either collapse the backing object (if 1687 * the parent is the only reference to it) or (perhaps) have 1688 * the parent bypass the object if the parent happens to shadow 1689 * all the resident pages in the entire backing object. 1690 * 1691 * This is ignoring pager-backed pages such as swap pages. 1692 * vm_object_backing_scan fails the shadowing test in this 1693 * case. 1694 */ 1695 if (backing_object->ref_count == 1) { 1696 /* 1697 * If there is exactly one reference to the backing 1698 * object, we can collapse it into the parent. 1699 */ 1700 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT); 1701 1702 #if VM_NRESERVLEVEL > 0 1703 /* 1704 * Break any reservations from backing_object. 1705 */ 1706 if (__predict_false(!LIST_EMPTY(&backing_object->rvq))) 1707 vm_reserv_break_all(backing_object); 1708 #endif 1709 1710 /* 1711 * Move the pager from backing_object to object. 1712 */ 1713 if (backing_object->type == OBJT_SWAP) { 1714 /* 1715 * swap_pager_copy() can sleep, in which case 1716 * the backing_object's and object's locks are 1717 * released and reacquired. 1718 */ 1719 swap_pager_copy( 1720 backing_object, 1721 object, 1722 OFF_TO_IDX(object->backing_object_offset), TRUE); 1723 1724 /* 1725 * Free any cached pages from backing_object. 1726 */ 1727 if (__predict_false(backing_object->cache != NULL)) 1728 vm_page_cache_free(backing_object, 0, 0); 1729 } 1730 /* 1731 * Object now shadows whatever backing_object did. 1732 * Note that the reference to 1733 * backing_object->backing_object moves from within 1734 * backing_object to within object. 1735 */ 1736 LIST_REMOVE(object, shadow_list); 1737 backing_object->shadow_count--; 1738 backing_object->generation++; 1739 if (backing_object->backing_object) { 1740 VM_OBJECT_LOCK(backing_object->backing_object); 1741 LIST_REMOVE(backing_object, shadow_list); 1742 LIST_INSERT_HEAD( 1743 &backing_object->backing_object->shadow_head, 1744 object, shadow_list); 1745 /* 1746 * The shadow_count has not changed. 1747 */ 1748 backing_object->backing_object->generation++; 1749 VM_OBJECT_UNLOCK(backing_object->backing_object); 1750 } 1751 object->backing_object = backing_object->backing_object; 1752 object->backing_object_offset += 1753 backing_object->backing_object_offset; 1754 1755 /* 1756 * Discard backing_object. 1757 * 1758 * Since the backing object has no pages, no pager left, 1759 * and no object references within it, all that is 1760 * necessary is to dispose of it. 1761 */ 1762 KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object)); 1763 VM_OBJECT_UNLOCK(backing_object); 1764 1765 mtx_lock(&vm_object_list_mtx); 1766 TAILQ_REMOVE( 1767 &vm_object_list, 1768 backing_object, 1769 object_list 1770 ); 1771 mtx_unlock(&vm_object_list_mtx); 1772 1773 uma_zfree(obj_zone, backing_object); 1774 1775 object_collapses++; 1776 } else { 1777 vm_object_t new_backing_object; 1778 1779 /* 1780 * If we do not entirely shadow the backing object, 1781 * there is nothing we can do so we give up. 1782 */ 1783 if (object->resident_page_count != object->size && 1784 vm_object_backing_scan(object, 1785 OBSC_TEST_ALL_SHADOWED) == 0) { 1786 VM_OBJECT_UNLOCK(backing_object); 1787 break; 1788 } 1789 1790 /* 1791 * Make the parent shadow the next object in the 1792 * chain. Deallocating backing_object will not remove 1793 * it, since its reference count is at least 2. 1794 */ 1795 LIST_REMOVE(object, shadow_list); 1796 backing_object->shadow_count--; 1797 backing_object->generation++; 1798 1799 new_backing_object = backing_object->backing_object; 1800 if ((object->backing_object = new_backing_object) != NULL) { 1801 VM_OBJECT_LOCK(new_backing_object); 1802 LIST_INSERT_HEAD( 1803 &new_backing_object->shadow_head, 1804 object, 1805 shadow_list 1806 ); 1807 new_backing_object->shadow_count++; 1808 new_backing_object->generation++; 1809 vm_object_reference_locked(new_backing_object); 1810 VM_OBJECT_UNLOCK(new_backing_object); 1811 object->backing_object_offset += 1812 backing_object->backing_object_offset; 1813 } 1814 1815 /* 1816 * Drop the reference count on backing_object. Since 1817 * its ref_count was at least 2, it will not vanish. 1818 */ 1819 backing_object->ref_count--; 1820 VM_OBJECT_UNLOCK(backing_object); 1821 object_bypasses++; 1822 } 1823 1824 /* 1825 * Try again with this object's new backing object. 1826 */ 1827 } 1828 } 1829 1830 /* 1831 * vm_object_page_remove: 1832 * 1833 * Removes all physical pages in the given range from the 1834 * object's list of pages. If the range's end is zero, all 1835 * physical pages from the range's start to the end of the object 1836 * are deleted. 1837 * 1838 * The object must be locked. 1839 */ 1840 void 1841 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 1842 boolean_t clean_only) 1843 { 1844 vm_page_t p, next; 1845 int wirings; 1846 1847 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1848 if (object->resident_page_count == 0) 1849 goto skipmemq; 1850 1851 /* 1852 * Since physically-backed objects do not use managed pages, we can't 1853 * remove pages from the object (we must instead remove the page 1854 * references, and then destroy the object). 1855 */ 1856 KASSERT(object->type != OBJT_PHYS || object == kernel_object || 1857 object == kmem_object, 1858 ("attempt to remove pages from a physical object")); 1859 1860 vm_object_pip_add(object, 1); 1861 again: 1862 vm_page_lock_queues(); 1863 if ((p = TAILQ_FIRST(&object->memq)) != NULL) { 1864 if (p->pindex < start) { 1865 p = vm_page_splay(start, object->root); 1866 if ((object->root = p)->pindex < start) 1867 p = TAILQ_NEXT(p, listq); 1868 } 1869 } 1870 /* 1871 * Assert: the variable p is either (1) the page with the 1872 * least pindex greater than or equal to the parameter pindex 1873 * or (2) NULL. 1874 */ 1875 for (; 1876 p != NULL && (p->pindex < end || end == 0); 1877 p = next) { 1878 next = TAILQ_NEXT(p, listq); 1879 1880 /* 1881 * If the page is wired for any reason besides the 1882 * existence of managed, wired mappings, then it cannot 1883 * be freed. 1884 */ 1885 if ((wirings = p->wire_count) != 0 && 1886 (wirings = pmap_page_wired_mappings(p)) != p->wire_count) { 1887 pmap_remove_all(p); 1888 /* Account for removal of managed, wired mappings. */ 1889 p->wire_count -= wirings; 1890 if (!clean_only) 1891 p->valid = 0; 1892 continue; 1893 } 1894 if (vm_page_sleep_if_busy(p, TRUE, "vmopar")) 1895 goto again; 1896 if (clean_only && p->valid) { 1897 pmap_remove_write(p); 1898 if (p->valid & p->dirty) 1899 continue; 1900 } 1901 pmap_remove_all(p); 1902 /* Account for removal of managed, wired mappings. */ 1903 if (wirings != 0) 1904 p->wire_count -= wirings; 1905 vm_page_free(p); 1906 } 1907 vm_page_unlock_queues(); 1908 vm_object_pip_wakeup(object); 1909 skipmemq: 1910 if (__predict_false(object->cache != NULL)) 1911 vm_page_cache_free(object, start, end); 1912 } 1913 1914 /* 1915 * Routine: vm_object_coalesce 1916 * Function: Coalesces two objects backing up adjoining 1917 * regions of memory into a single object. 1918 * 1919 * returns TRUE if objects were combined. 1920 * 1921 * NOTE: Only works at the moment if the second object is NULL - 1922 * if it's not, which object do we lock first? 1923 * 1924 * Parameters: 1925 * prev_object First object to coalesce 1926 * prev_offset Offset into prev_object 1927 * prev_size Size of reference to prev_object 1928 * next_size Size of reference to the second object 1929 * 1930 * Conditions: 1931 * The object must *not* be locked. 1932 */ 1933 boolean_t 1934 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 1935 vm_size_t prev_size, vm_size_t next_size) 1936 { 1937 vm_pindex_t next_pindex; 1938 1939 if (prev_object == NULL) 1940 return (TRUE); 1941 VM_OBJECT_LOCK(prev_object); 1942 if (prev_object->type != OBJT_DEFAULT && 1943 prev_object->type != OBJT_SWAP) { 1944 VM_OBJECT_UNLOCK(prev_object); 1945 return (FALSE); 1946 } 1947 1948 /* 1949 * Try to collapse the object first 1950 */ 1951 vm_object_collapse(prev_object); 1952 1953 /* 1954 * Can't coalesce if: . more than one reference . paged out . shadows 1955 * another object . has a copy elsewhere (any of which mean that the 1956 * pages not mapped to prev_entry may be in use anyway) 1957 */ 1958 if (prev_object->backing_object != NULL) { 1959 VM_OBJECT_UNLOCK(prev_object); 1960 return (FALSE); 1961 } 1962 1963 prev_size >>= PAGE_SHIFT; 1964 next_size >>= PAGE_SHIFT; 1965 next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 1966 1967 if ((prev_object->ref_count > 1) && 1968 (prev_object->size != next_pindex)) { 1969 VM_OBJECT_UNLOCK(prev_object); 1970 return (FALSE); 1971 } 1972 1973 /* 1974 * Remove any pages that may still be in the object from a previous 1975 * deallocation. 1976 */ 1977 if (next_pindex < prev_object->size) { 1978 vm_object_page_remove(prev_object, 1979 next_pindex, 1980 next_pindex + next_size, FALSE); 1981 if (prev_object->type == OBJT_SWAP) 1982 swap_pager_freespace(prev_object, 1983 next_pindex, next_size); 1984 } 1985 1986 /* 1987 * Extend the object if necessary. 1988 */ 1989 if (next_pindex + next_size > prev_object->size) 1990 prev_object->size = next_pindex + next_size; 1991 1992 VM_OBJECT_UNLOCK(prev_object); 1993 return (TRUE); 1994 } 1995 1996 void 1997 vm_object_set_writeable_dirty(vm_object_t object) 1998 { 1999 struct vnode *vp; 2000 2001 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 2002 if ((object->flags & OBJ_MIGHTBEDIRTY) != 0) 2003 return; 2004 vm_object_set_flag(object, OBJ_MIGHTBEDIRTY); 2005 if (object->type == OBJT_VNODE && 2006 (vp = (struct vnode *)object->handle) != NULL) { 2007 VI_LOCK(vp); 2008 vp->v_iflag |= VI_OBJDIRTY; 2009 VI_UNLOCK(vp); 2010 } 2011 } 2012 2013 #include "opt_ddb.h" 2014 #ifdef DDB 2015 #include <sys/kernel.h> 2016 2017 #include <sys/cons.h> 2018 2019 #include <ddb/ddb.h> 2020 2021 static int 2022 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2023 { 2024 vm_map_t tmpm; 2025 vm_map_entry_t tmpe; 2026 vm_object_t obj; 2027 int entcount; 2028 2029 if (map == 0) 2030 return 0; 2031 2032 if (entry == 0) { 2033 tmpe = map->header.next; 2034 entcount = map->nentries; 2035 while (entcount-- && (tmpe != &map->header)) { 2036 if (_vm_object_in_map(map, object, tmpe)) { 2037 return 1; 2038 } 2039 tmpe = tmpe->next; 2040 } 2041 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 2042 tmpm = entry->object.sub_map; 2043 tmpe = tmpm->header.next; 2044 entcount = tmpm->nentries; 2045 while (entcount-- && tmpe != &tmpm->header) { 2046 if (_vm_object_in_map(tmpm, object, tmpe)) { 2047 return 1; 2048 } 2049 tmpe = tmpe->next; 2050 } 2051 } else if ((obj = entry->object.vm_object) != NULL) { 2052 for (; obj; obj = obj->backing_object) 2053 if (obj == object) { 2054 return 1; 2055 } 2056 } 2057 return 0; 2058 } 2059 2060 static int 2061 vm_object_in_map(vm_object_t object) 2062 { 2063 struct proc *p; 2064 2065 /* sx_slock(&allproc_lock); */ 2066 FOREACH_PROC_IN_SYSTEM(p) { 2067 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2068 continue; 2069 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 2070 /* sx_sunlock(&allproc_lock); */ 2071 return 1; 2072 } 2073 } 2074 /* sx_sunlock(&allproc_lock); */ 2075 if (_vm_object_in_map(kernel_map, object, 0)) 2076 return 1; 2077 if (_vm_object_in_map(kmem_map, object, 0)) 2078 return 1; 2079 if (_vm_object_in_map(pager_map, object, 0)) 2080 return 1; 2081 if (_vm_object_in_map(buffer_map, object, 0)) 2082 return 1; 2083 return 0; 2084 } 2085 2086 DB_SHOW_COMMAND(vmochk, vm_object_check) 2087 { 2088 vm_object_t object; 2089 2090 /* 2091 * make sure that internal objs are in a map somewhere 2092 * and none have zero ref counts. 2093 */ 2094 TAILQ_FOREACH(object, &vm_object_list, object_list) { 2095 if (object->handle == NULL && 2096 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 2097 if (object->ref_count == 0) { 2098 db_printf("vmochk: internal obj has zero ref count: %ld\n", 2099 (long)object->size); 2100 } 2101 if (!vm_object_in_map(object)) { 2102 db_printf( 2103 "vmochk: internal obj is not in a map: " 2104 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2105 object->ref_count, (u_long)object->size, 2106 (u_long)object->size, 2107 (void *)object->backing_object); 2108 } 2109 } 2110 } 2111 } 2112 2113 /* 2114 * vm_object_print: [ debug ] 2115 */ 2116 DB_SHOW_COMMAND(object, vm_object_print_static) 2117 { 2118 /* XXX convert args. */ 2119 vm_object_t object = (vm_object_t)addr; 2120 boolean_t full = have_addr; 2121 2122 vm_page_t p; 2123 2124 /* XXX count is an (unused) arg. Avoid shadowing it. */ 2125 #define count was_count 2126 2127 int count; 2128 2129 if (object == NULL) 2130 return; 2131 2132 db_iprintf( 2133 "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n", 2134 object, (int)object->type, (uintmax_t)object->size, 2135 object->resident_page_count, object->ref_count, object->flags); 2136 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 2137 object->shadow_count, 2138 object->backing_object ? object->backing_object->ref_count : 0, 2139 object->backing_object, (uintmax_t)object->backing_object_offset); 2140 2141 if (!full) 2142 return; 2143 2144 db_indent += 2; 2145 count = 0; 2146 TAILQ_FOREACH(p, &object->memq, listq) { 2147 if (count == 0) 2148 db_iprintf("memory:="); 2149 else if (count == 6) { 2150 db_printf("\n"); 2151 db_iprintf(" ..."); 2152 count = 0; 2153 } else 2154 db_printf(","); 2155 count++; 2156 2157 db_printf("(off=0x%jx,page=0x%jx)", 2158 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2159 } 2160 if (count != 0) 2161 db_printf("\n"); 2162 db_indent -= 2; 2163 } 2164 2165 /* XXX. */ 2166 #undef count 2167 2168 /* XXX need this non-static entry for calling from vm_map_print. */ 2169 void 2170 vm_object_print( 2171 /* db_expr_t */ long addr, 2172 boolean_t have_addr, 2173 /* db_expr_t */ long count, 2174 char *modif) 2175 { 2176 vm_object_print_static(addr, have_addr, count, modif); 2177 } 2178 2179 DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 2180 { 2181 vm_object_t object; 2182 int nl = 0; 2183 int c; 2184 2185 TAILQ_FOREACH(object, &vm_object_list, object_list) { 2186 vm_pindex_t idx, fidx; 2187 vm_pindex_t osize; 2188 vm_paddr_t pa = -1; 2189 int rcount; 2190 vm_page_t m; 2191 2192 db_printf("new object: %p\n", (void *)object); 2193 if (nl > 18) { 2194 c = cngetc(); 2195 if (c != ' ') 2196 return; 2197 nl = 0; 2198 } 2199 nl++; 2200 rcount = 0; 2201 fidx = 0; 2202 osize = object->size; 2203 if (osize > 128) 2204 osize = 128; 2205 for (idx = 0; idx < osize; idx++) { 2206 m = vm_page_lookup(object, idx); 2207 if (m == NULL) { 2208 if (rcount) { 2209 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2210 (long)fidx, rcount, (long)pa); 2211 if (nl > 18) { 2212 c = cngetc(); 2213 if (c != ' ') 2214 return; 2215 nl = 0; 2216 } 2217 nl++; 2218 rcount = 0; 2219 } 2220 continue; 2221 } 2222 2223 2224 if (rcount && 2225 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 2226 ++rcount; 2227 continue; 2228 } 2229 if (rcount) { 2230 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2231 (long)fidx, rcount, (long)pa); 2232 if (nl > 18) { 2233 c = cngetc(); 2234 if (c != ' ') 2235 return; 2236 nl = 0; 2237 } 2238 nl++; 2239 } 2240 fidx = idx; 2241 pa = VM_PAGE_TO_PHYS(m); 2242 rcount = 1; 2243 } 2244 if (rcount) { 2245 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2246 (long)fidx, rcount, (long)pa); 2247 if (nl > 18) { 2248 c = cngetc(); 2249 if (c != ' ') 2250 return; 2251 nl = 0; 2252 } 2253 nl++; 2254 } 2255 } 2256 } 2257 #endif /* DDB */ 2258