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 object->handle == NULL && 505 (object->type == OBJT_DEFAULT || 506 object->type == OBJT_SWAP)) { 507 vm_object_set_flag(object, OBJ_ONEMAPPING); 508 } else if ((object->shadow_count == 1) && 509 (object->handle == NULL) && 510 (object->type == OBJT_DEFAULT || 511 object->type == OBJT_SWAP)) { 512 vm_object_t robject; 513 514 robject = LIST_FIRST(&object->shadow_head); 515 KASSERT(robject != NULL, 516 ("vm_object_deallocate: ref_count: %d, shadow_count: %d", 517 object->ref_count, 518 object->shadow_count)); 519 if (!VM_OBJECT_TRYLOCK(robject)) { 520 /* 521 * Avoid a potential deadlock. 522 */ 523 object->ref_count++; 524 VM_OBJECT_UNLOCK(object); 525 /* 526 * More likely than not the thread 527 * holding robject's lock has lower 528 * priority than the current thread. 529 * Let the lower priority thread run. 530 */ 531 pause("vmo_de", 1); 532 continue; 533 } 534 /* 535 * Collapse object into its shadow unless its 536 * shadow is dead. In that case, object will 537 * be deallocated by the thread that is 538 * deallocating its shadow. 539 */ 540 if ((robject->flags & OBJ_DEAD) == 0 && 541 (robject->handle == NULL) && 542 (robject->type == OBJT_DEFAULT || 543 robject->type == OBJT_SWAP)) { 544 545 robject->ref_count++; 546 retry: 547 if (robject->paging_in_progress) { 548 VM_OBJECT_UNLOCK(object); 549 vm_object_pip_wait(robject, 550 "objde1"); 551 temp = robject->backing_object; 552 if (object == temp) { 553 VM_OBJECT_LOCK(object); 554 goto retry; 555 } 556 } else if (object->paging_in_progress) { 557 VM_OBJECT_UNLOCK(robject); 558 object->flags |= OBJ_PIPWNT; 559 msleep(object, 560 VM_OBJECT_MTX(object), 561 PDROP | PVM, "objde2", 0); 562 VM_OBJECT_LOCK(robject); 563 temp = robject->backing_object; 564 if (object == temp) { 565 VM_OBJECT_LOCK(object); 566 goto retry; 567 } 568 } else 569 VM_OBJECT_UNLOCK(object); 570 571 if (robject->ref_count == 1) { 572 robject->ref_count--; 573 object = robject; 574 goto doterm; 575 } 576 object = robject; 577 vm_object_collapse(object); 578 VM_OBJECT_UNLOCK(object); 579 continue; 580 } 581 VM_OBJECT_UNLOCK(robject); 582 } 583 VM_OBJECT_UNLOCK(object); 584 return; 585 } 586 doterm: 587 temp = object->backing_object; 588 if (temp != NULL) { 589 VM_OBJECT_LOCK(temp); 590 LIST_REMOVE(object, shadow_list); 591 temp->shadow_count--; 592 temp->generation++; 593 VM_OBJECT_UNLOCK(temp); 594 object->backing_object = NULL; 595 } 596 /* 597 * Don't double-terminate, we could be in a termination 598 * recursion due to the terminate having to sync data 599 * to disk. 600 */ 601 if ((object->flags & OBJ_DEAD) == 0) 602 vm_object_terminate(object); 603 else 604 VM_OBJECT_UNLOCK(object); 605 object = temp; 606 } 607 } 608 609 /* 610 * vm_object_terminate actually destroys the specified object, freeing 611 * up all previously used resources. 612 * 613 * The object must be locked. 614 * This routine may block. 615 */ 616 void 617 vm_object_terminate(vm_object_t object) 618 { 619 vm_page_t p; 620 621 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 622 623 /* 624 * Make sure no one uses us. 625 */ 626 vm_object_set_flag(object, OBJ_DEAD); 627 628 /* 629 * wait for the pageout daemon to be done with the object 630 */ 631 vm_object_pip_wait(object, "objtrm"); 632 633 KASSERT(!object->paging_in_progress, 634 ("vm_object_terminate: pageout in progress")); 635 636 /* 637 * Clean and free the pages, as appropriate. All references to the 638 * object are gone, so we don't need to lock it. 639 */ 640 if (object->type == OBJT_VNODE) { 641 struct vnode *vp = (struct vnode *)object->handle; 642 643 /* 644 * Clean pages and flush buffers. 645 */ 646 vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 647 VM_OBJECT_UNLOCK(object); 648 649 vinvalbuf(vp, V_SAVE, NULL, 0, 0); 650 651 VM_OBJECT_LOCK(object); 652 } 653 654 KASSERT(object->ref_count == 0, 655 ("vm_object_terminate: object with references, ref_count=%d", 656 object->ref_count)); 657 658 /* 659 * Now free any remaining pages. For internal objects, this also 660 * removes them from paging queues. Don't free wired pages, just 661 * remove them from the object. 662 */ 663 vm_page_lock_queues(); 664 while ((p = TAILQ_FIRST(&object->memq)) != NULL) { 665 KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0, 666 ("vm_object_terminate: freeing busy page %p " 667 "p->busy = %d, p->flags %x\n", p, p->busy, p->flags)); 668 if (p->wire_count == 0) { 669 vm_page_free(p); 670 cnt.v_pfree++; 671 } else { 672 vm_page_remove(p); 673 } 674 } 675 vm_page_unlock_queues(); 676 677 #if VM_NRESERVLEVEL > 0 678 if (__predict_false(!LIST_EMPTY(&object->rvq))) 679 vm_reserv_break_all(object); 680 #endif 681 if (__predict_false(object->cache != NULL)) 682 vm_page_cache_free(object, 0, 0); 683 684 /* 685 * Let the pager know object is dead. 686 */ 687 vm_pager_deallocate(object); 688 VM_OBJECT_UNLOCK(object); 689 690 /* 691 * Remove the object from the global object list. 692 */ 693 mtx_lock(&vm_object_list_mtx); 694 TAILQ_REMOVE(&vm_object_list, object, object_list); 695 mtx_unlock(&vm_object_list_mtx); 696 697 /* 698 * Free the space for the object. 699 */ 700 uma_zfree(obj_zone, object); 701 } 702 703 /* 704 * vm_object_page_clean 705 * 706 * Clean all dirty pages in the specified range of object. Leaves page 707 * on whatever queue it is currently on. If NOSYNC is set then do not 708 * write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC), 709 * leaving the object dirty. 710 * 711 * When stuffing pages asynchronously, allow clustering. XXX we need a 712 * synchronous clustering mode implementation. 713 * 714 * Odd semantics: if start == end, we clean everything. 715 * 716 * The object must be locked. 717 */ 718 void 719 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags) 720 { 721 vm_page_t p, np; 722 vm_pindex_t tstart, tend; 723 vm_pindex_t pi; 724 int clearobjflags; 725 int pagerflags; 726 int curgeneration; 727 728 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 729 if (object->type != OBJT_VNODE || 730 (object->flags & OBJ_MIGHTBEDIRTY) == 0) 731 return; 732 733 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 734 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0; 735 736 vm_object_set_flag(object, OBJ_CLEANING); 737 738 tstart = start; 739 if (end == 0) { 740 tend = object->size; 741 } else { 742 tend = end; 743 } 744 745 vm_page_lock_queues(); 746 /* 747 * If the caller is smart and only msync()s a range he knows is 748 * dirty, we may be able to avoid an object scan. This results in 749 * a phenominal improvement in performance. We cannot do this 750 * as a matter of course because the object may be huge - e.g. 751 * the size might be in the gigabytes or terrabytes. 752 */ 753 if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) { 754 vm_pindex_t tscan; 755 int scanlimit; 756 int scanreset; 757 758 scanreset = object->resident_page_count / EASY_SCAN_FACTOR; 759 if (scanreset < 16) 760 scanreset = 16; 761 pagerflags |= VM_PAGER_IGNORE_CLEANCHK; 762 763 scanlimit = scanreset; 764 tscan = tstart; 765 while (tscan < tend) { 766 curgeneration = object->generation; 767 p = vm_page_lookup(object, tscan); 768 if (p == NULL || p->valid == 0) { 769 if (--scanlimit == 0) 770 break; 771 ++tscan; 772 continue; 773 } 774 vm_page_test_dirty(p); 775 if ((p->dirty & p->valid) == 0) { 776 if (--scanlimit == 0) 777 break; 778 ++tscan; 779 continue; 780 } 781 /* 782 * If we have been asked to skip nosync pages and 783 * this is a nosync page, we can't continue. 784 */ 785 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) { 786 if (--scanlimit == 0) 787 break; 788 ++tscan; 789 continue; 790 } 791 scanlimit = scanreset; 792 793 /* 794 * This returns 0 if it was unable to busy the first 795 * page (i.e. had to sleep). 796 */ 797 tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags); 798 } 799 800 /* 801 * If everything was dirty and we flushed it successfully, 802 * and the requested range is not the entire object, we 803 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can 804 * return immediately. 805 */ 806 if (tscan >= tend && (tstart || tend < object->size)) { 807 vm_page_unlock_queues(); 808 vm_object_clear_flag(object, OBJ_CLEANING); 809 return; 810 } 811 pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK; 812 } 813 814 /* 815 * Generally set CLEANCHK interlock and make the page read-only so 816 * we can then clear the object flags. 817 * 818 * However, if this is a nosync mmap then the object is likely to 819 * stay dirty so do not mess with the page and do not clear the 820 * object flags. 821 */ 822 clearobjflags = 1; 823 TAILQ_FOREACH(p, &object->memq, listq) { 824 p->oflags |= VPO_CLEANCHK; 825 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) 826 clearobjflags = 0; 827 else 828 pmap_remove_write(p); 829 } 830 831 if (clearobjflags && (tstart == 0) && (tend == object->size)) { 832 struct vnode *vp; 833 834 vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY); 835 if (object->type == OBJT_VNODE && 836 (vp = (struct vnode *)object->handle) != NULL) { 837 VI_LOCK(vp); 838 if (vp->v_iflag & VI_OBJDIRTY) 839 vp->v_iflag &= ~VI_OBJDIRTY; 840 VI_UNLOCK(vp); 841 } 842 } 843 844 rescan: 845 curgeneration = object->generation; 846 847 for (p = TAILQ_FIRST(&object->memq); p; p = np) { 848 int n; 849 850 np = TAILQ_NEXT(p, listq); 851 852 again: 853 pi = p->pindex; 854 if ((p->oflags & VPO_CLEANCHK) == 0 || 855 (pi < tstart) || (pi >= tend) || 856 p->valid == 0) { 857 p->oflags &= ~VPO_CLEANCHK; 858 continue; 859 } 860 861 vm_page_test_dirty(p); 862 if ((p->dirty & p->valid) == 0) { 863 p->oflags &= ~VPO_CLEANCHK; 864 continue; 865 } 866 867 /* 868 * If we have been asked to skip nosync pages and this is a 869 * nosync page, skip it. Note that the object flags were 870 * not cleared in this case so we do not have to set them. 871 */ 872 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) { 873 p->oflags &= ~VPO_CLEANCHK; 874 continue; 875 } 876 877 n = vm_object_page_collect_flush(object, p, 878 curgeneration, pagerflags); 879 if (n == 0) 880 goto rescan; 881 882 if (object->generation != curgeneration) 883 goto rescan; 884 885 /* 886 * Try to optimize the next page. If we can't we pick up 887 * our (random) scan where we left off. 888 */ 889 if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) { 890 if ((p = vm_page_lookup(object, pi + n)) != NULL) 891 goto again; 892 } 893 } 894 vm_page_unlock_queues(); 895 #if 0 896 VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc); 897 #endif 898 899 vm_object_clear_flag(object, OBJ_CLEANING); 900 return; 901 } 902 903 static int 904 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags) 905 { 906 int runlen; 907 int maxf; 908 int chkb; 909 int maxb; 910 int i; 911 vm_pindex_t pi; 912 vm_page_t maf[vm_pageout_page_count]; 913 vm_page_t mab[vm_pageout_page_count]; 914 vm_page_t ma[vm_pageout_page_count]; 915 916 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 917 pi = p->pindex; 918 while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) { 919 vm_page_lock_queues(); 920 if (object->generation != curgeneration) { 921 return(0); 922 } 923 } 924 maxf = 0; 925 for(i = 1; i < vm_pageout_page_count; i++) { 926 vm_page_t tp; 927 928 if ((tp = vm_page_lookup(object, pi + i)) != NULL) { 929 if ((tp->oflags & VPO_BUSY) || 930 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 931 (tp->oflags & VPO_CLEANCHK) == 0) || 932 (tp->busy != 0)) 933 break; 934 vm_page_test_dirty(tp); 935 if ((tp->dirty & tp->valid) == 0) { 936 tp->oflags &= ~VPO_CLEANCHK; 937 break; 938 } 939 maf[ i - 1 ] = tp; 940 maxf++; 941 continue; 942 } 943 break; 944 } 945 946 maxb = 0; 947 chkb = vm_pageout_page_count - maxf; 948 if (chkb) { 949 for(i = 1; i < chkb;i++) { 950 vm_page_t tp; 951 952 if ((tp = vm_page_lookup(object, pi - i)) != NULL) { 953 if ((tp->oflags & VPO_BUSY) || 954 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 955 (tp->oflags & VPO_CLEANCHK) == 0) || 956 (tp->busy != 0)) 957 break; 958 vm_page_test_dirty(tp); 959 if ((tp->dirty & tp->valid) == 0) { 960 tp->oflags &= ~VPO_CLEANCHK; 961 break; 962 } 963 mab[ i - 1 ] = tp; 964 maxb++; 965 continue; 966 } 967 break; 968 } 969 } 970 971 for(i = 0; i < maxb; i++) { 972 int index = (maxb - i) - 1; 973 ma[index] = mab[i]; 974 ma[index]->oflags &= ~VPO_CLEANCHK; 975 } 976 p->oflags &= ~VPO_CLEANCHK; 977 ma[maxb] = p; 978 for(i = 0; i < maxf; i++) { 979 int index = (maxb + i) + 1; 980 ma[index] = maf[i]; 981 ma[index]->oflags &= ~VPO_CLEANCHK; 982 } 983 runlen = maxb + maxf + 1; 984 985 vm_pageout_flush(ma, runlen, pagerflags); 986 for (i = 0; i < runlen; i++) { 987 if (ma[i]->valid & ma[i]->dirty) { 988 pmap_remove_write(ma[i]); 989 ma[i]->oflags |= VPO_CLEANCHK; 990 991 /* 992 * maxf will end up being the actual number of pages 993 * we wrote out contiguously, non-inclusive of the 994 * first page. We do not count look-behind pages. 995 */ 996 if (i >= maxb + 1 && (maxf > i - maxb - 1)) 997 maxf = i - maxb - 1; 998 } 999 } 1000 return(maxf + 1); 1001 } 1002 1003 /* 1004 * Note that there is absolutely no sense in writing out 1005 * anonymous objects, so we track down the vnode object 1006 * to write out. 1007 * We invalidate (remove) all pages from the address space 1008 * for semantic correctness. 1009 * 1010 * Note: certain anonymous maps, such as MAP_NOSYNC maps, 1011 * may start out with a NULL object. 1012 */ 1013 void 1014 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size, 1015 boolean_t syncio, boolean_t invalidate) 1016 { 1017 vm_object_t backing_object; 1018 struct vnode *vp; 1019 struct mount *mp; 1020 int flags; 1021 1022 if (object == NULL) 1023 return; 1024 VM_OBJECT_LOCK(object); 1025 while ((backing_object = object->backing_object) != NULL) { 1026 VM_OBJECT_LOCK(backing_object); 1027 offset += object->backing_object_offset; 1028 VM_OBJECT_UNLOCK(object); 1029 object = backing_object; 1030 if (object->size < OFF_TO_IDX(offset + size)) 1031 size = IDX_TO_OFF(object->size) - offset; 1032 } 1033 /* 1034 * Flush pages if writing is allowed, invalidate them 1035 * if invalidation requested. Pages undergoing I/O 1036 * will be ignored by vm_object_page_remove(). 1037 * 1038 * We cannot lock the vnode and then wait for paging 1039 * to complete without deadlocking against vm_fault. 1040 * Instead we simply call vm_object_page_remove() and 1041 * allow it to block internally on a page-by-page 1042 * basis when it encounters pages undergoing async 1043 * I/O. 1044 */ 1045 if (object->type == OBJT_VNODE && 1046 (object->flags & OBJ_MIGHTBEDIRTY) != 0) { 1047 int vfslocked; 1048 vp = object->handle; 1049 VM_OBJECT_UNLOCK(object); 1050 (void) vn_start_write(vp, &mp, V_WAIT); 1051 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1052 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1053 flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 1054 flags |= invalidate ? OBJPC_INVAL : 0; 1055 VM_OBJECT_LOCK(object); 1056 vm_object_page_clean(object, 1057 OFF_TO_IDX(offset), 1058 OFF_TO_IDX(offset + size + PAGE_MASK), 1059 flags); 1060 VM_OBJECT_UNLOCK(object); 1061 VOP_UNLOCK(vp, 0); 1062 VFS_UNLOCK_GIANT(vfslocked); 1063 vn_finished_write(mp); 1064 VM_OBJECT_LOCK(object); 1065 } 1066 if ((object->type == OBJT_VNODE || 1067 object->type == OBJT_DEVICE) && invalidate) { 1068 boolean_t purge; 1069 purge = old_msync || (object->type == OBJT_DEVICE); 1070 vm_object_page_remove(object, 1071 OFF_TO_IDX(offset), 1072 OFF_TO_IDX(offset + size + PAGE_MASK), 1073 purge ? FALSE : TRUE); 1074 } 1075 VM_OBJECT_UNLOCK(object); 1076 } 1077 1078 /* 1079 * vm_object_madvise: 1080 * 1081 * Implements the madvise function at the object/page level. 1082 * 1083 * MADV_WILLNEED (any object) 1084 * 1085 * Activate the specified pages if they are resident. 1086 * 1087 * MADV_DONTNEED (any object) 1088 * 1089 * Deactivate the specified pages if they are resident. 1090 * 1091 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 1092 * OBJ_ONEMAPPING only) 1093 * 1094 * Deactivate and clean the specified pages if they are 1095 * resident. This permits the process to reuse the pages 1096 * without faulting or the kernel to reclaim the pages 1097 * without I/O. 1098 */ 1099 void 1100 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise) 1101 { 1102 vm_pindex_t end, tpindex; 1103 vm_object_t backing_object, tobject; 1104 vm_page_t m; 1105 1106 if (object == NULL) 1107 return; 1108 VM_OBJECT_LOCK(object); 1109 end = pindex + count; 1110 /* 1111 * Locate and adjust resident pages 1112 */ 1113 for (; pindex < end; pindex += 1) { 1114 relookup: 1115 tobject = object; 1116 tpindex = pindex; 1117 shadowlookup: 1118 /* 1119 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages 1120 * and those pages must be OBJ_ONEMAPPING. 1121 */ 1122 if (advise == MADV_FREE) { 1123 if ((tobject->type != OBJT_DEFAULT && 1124 tobject->type != OBJT_SWAP) || 1125 (tobject->flags & OBJ_ONEMAPPING) == 0) { 1126 goto unlock_tobject; 1127 } 1128 } 1129 m = vm_page_lookup(tobject, tpindex); 1130 if (m == NULL && advise == MADV_WILLNEED) { 1131 /* 1132 * If the page is cached, reactivate it. 1133 */ 1134 m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED | 1135 VM_ALLOC_NOBUSY); 1136 } 1137 if (m == NULL) { 1138 /* 1139 * There may be swap even if there is no backing page 1140 */ 1141 if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 1142 swap_pager_freespace(tobject, tpindex, 1); 1143 /* 1144 * next object 1145 */ 1146 backing_object = tobject->backing_object; 1147 if (backing_object == NULL) 1148 goto unlock_tobject; 1149 VM_OBJECT_LOCK(backing_object); 1150 tpindex += OFF_TO_IDX(tobject->backing_object_offset); 1151 if (tobject != object) 1152 VM_OBJECT_UNLOCK(tobject); 1153 tobject = backing_object; 1154 goto shadowlookup; 1155 } 1156 /* 1157 * If the page is busy or not in a normal active state, 1158 * we skip it. If the page is not managed there are no 1159 * page queues to mess with. Things can break if we mess 1160 * with pages in any of the below states. 1161 */ 1162 vm_page_lock_queues(); 1163 if (m->hold_count || 1164 m->wire_count || 1165 (m->flags & PG_UNMANAGED) || 1166 m->valid != VM_PAGE_BITS_ALL) { 1167 vm_page_unlock_queues(); 1168 goto unlock_tobject; 1169 } 1170 if ((m->oflags & VPO_BUSY) || m->busy) { 1171 vm_page_flag_set(m, PG_REFERENCED); 1172 vm_page_unlock_queues(); 1173 if (object != tobject) 1174 VM_OBJECT_UNLOCK(object); 1175 m->oflags |= VPO_WANTED; 1176 msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo", 0); 1177 VM_OBJECT_LOCK(object); 1178 goto relookup; 1179 } 1180 if (advise == MADV_WILLNEED) { 1181 vm_page_activate(m); 1182 } else if (advise == MADV_DONTNEED) { 1183 vm_page_dontneed(m); 1184 } else if (advise == MADV_FREE) { 1185 /* 1186 * Mark the page clean. This will allow the page 1187 * to be freed up by the system. However, such pages 1188 * are often reused quickly by malloc()/free() 1189 * so we do not do anything that would cause 1190 * a page fault if we can help it. 1191 * 1192 * Specifically, we do not try to actually free 1193 * the page now nor do we try to put it in the 1194 * cache (which would cause a page fault on reuse). 1195 * 1196 * But we do make the page is freeable as we 1197 * can without actually taking the step of unmapping 1198 * it. 1199 */ 1200 pmap_clear_modify(m); 1201 m->dirty = 0; 1202 m->act_count = 0; 1203 vm_page_dontneed(m); 1204 } 1205 vm_page_unlock_queues(); 1206 if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 1207 swap_pager_freespace(tobject, tpindex, 1); 1208 unlock_tobject: 1209 if (tobject != object) 1210 VM_OBJECT_UNLOCK(tobject); 1211 } 1212 VM_OBJECT_UNLOCK(object); 1213 } 1214 1215 /* 1216 * vm_object_shadow: 1217 * 1218 * Create a new object which is backed by the 1219 * specified existing object range. The source 1220 * object reference is deallocated. 1221 * 1222 * The new object and offset into that object 1223 * are returned in the source parameters. 1224 */ 1225 void 1226 vm_object_shadow( 1227 vm_object_t *object, /* IN/OUT */ 1228 vm_ooffset_t *offset, /* IN/OUT */ 1229 vm_size_t length) 1230 { 1231 vm_object_t source; 1232 vm_object_t result; 1233 1234 source = *object; 1235 1236 /* 1237 * Don't create the new object if the old object isn't shared. 1238 */ 1239 if (source != NULL) { 1240 VM_OBJECT_LOCK(source); 1241 if (source->ref_count == 1 && 1242 source->handle == NULL && 1243 (source->type == OBJT_DEFAULT || 1244 source->type == OBJT_SWAP)) { 1245 VM_OBJECT_UNLOCK(source); 1246 return; 1247 } 1248 VM_OBJECT_UNLOCK(source); 1249 } 1250 1251 /* 1252 * Allocate a new object with the given length. 1253 */ 1254 result = vm_object_allocate(OBJT_DEFAULT, length); 1255 1256 /* 1257 * The new object shadows the source object, adding a reference to it. 1258 * Our caller changes his reference to point to the new object, 1259 * removing a reference to the source object. Net result: no change 1260 * of reference count. 1261 * 1262 * Try to optimize the result object's page color when shadowing 1263 * in order to maintain page coloring consistency in the combined 1264 * shadowed object. 1265 */ 1266 result->backing_object = source; 1267 /* 1268 * Store the offset into the source object, and fix up the offset into 1269 * the new object. 1270 */ 1271 result->backing_object_offset = *offset; 1272 if (source != NULL) { 1273 VM_OBJECT_LOCK(source); 1274 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list); 1275 source->shadow_count++; 1276 source->generation++; 1277 #if VM_NRESERVLEVEL > 0 1278 result->flags |= source->flags & (OBJ_NEEDGIANT | OBJ_COLORED); 1279 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & 1280 ((1 << (VM_NFREEORDER - 1)) - 1); 1281 #else 1282 result->flags |= source->flags & OBJ_NEEDGIANT; 1283 #endif 1284 VM_OBJECT_UNLOCK(source); 1285 } 1286 1287 1288 /* 1289 * Return the new things 1290 */ 1291 *offset = 0; 1292 *object = result; 1293 } 1294 1295 /* 1296 * vm_object_split: 1297 * 1298 * Split the pages in a map entry into a new object. This affords 1299 * easier removal of unused pages, and keeps object inheritance from 1300 * being a negative impact on memory usage. 1301 */ 1302 void 1303 vm_object_split(vm_map_entry_t entry) 1304 { 1305 vm_page_t m, m_next; 1306 vm_object_t orig_object, new_object, source; 1307 vm_pindex_t idx, offidxstart; 1308 vm_size_t size; 1309 1310 orig_object = entry->object.vm_object; 1311 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 1312 return; 1313 if (orig_object->ref_count <= 1) 1314 return; 1315 VM_OBJECT_UNLOCK(orig_object); 1316 1317 offidxstart = OFF_TO_IDX(entry->offset); 1318 size = atop(entry->end - entry->start); 1319 1320 /* 1321 * If swap_pager_copy() is later called, it will convert new_object 1322 * into a swap object. 1323 */ 1324 new_object = vm_object_allocate(OBJT_DEFAULT, size); 1325 1326 /* 1327 * At this point, the new object is still private, so the order in 1328 * which the original and new objects are locked does not matter. 1329 */ 1330 VM_OBJECT_LOCK(new_object); 1331 VM_OBJECT_LOCK(orig_object); 1332 source = orig_object->backing_object; 1333 if (source != NULL) { 1334 VM_OBJECT_LOCK(source); 1335 if ((source->flags & OBJ_DEAD) != 0) { 1336 VM_OBJECT_UNLOCK(source); 1337 VM_OBJECT_UNLOCK(orig_object); 1338 VM_OBJECT_UNLOCK(new_object); 1339 vm_object_deallocate(new_object); 1340 VM_OBJECT_LOCK(orig_object); 1341 return; 1342 } 1343 LIST_INSERT_HEAD(&source->shadow_head, 1344 new_object, shadow_list); 1345 source->shadow_count++; 1346 source->generation++; 1347 vm_object_reference_locked(source); /* for new_object */ 1348 vm_object_clear_flag(source, OBJ_ONEMAPPING); 1349 VM_OBJECT_UNLOCK(source); 1350 new_object->backing_object_offset = 1351 orig_object->backing_object_offset + entry->offset; 1352 new_object->backing_object = source; 1353 } 1354 new_object->flags |= orig_object->flags & OBJ_NEEDGIANT; 1355 retry: 1356 if ((m = TAILQ_FIRST(&orig_object->memq)) != NULL) { 1357 if (m->pindex < offidxstart) { 1358 m = vm_page_splay(offidxstart, orig_object->root); 1359 if ((orig_object->root = m)->pindex < offidxstart) 1360 m = TAILQ_NEXT(m, listq); 1361 } 1362 } 1363 vm_page_lock_queues(); 1364 for (; m != NULL && (idx = m->pindex - offidxstart) < size; 1365 m = m_next) { 1366 m_next = TAILQ_NEXT(m, listq); 1367 1368 /* 1369 * We must wait for pending I/O to complete before we can 1370 * rename the page. 1371 * 1372 * We do not have to VM_PROT_NONE the page as mappings should 1373 * not be changed by this operation. 1374 */ 1375 if ((m->oflags & VPO_BUSY) || m->busy) { 1376 vm_page_flag_set(m, PG_REFERENCED); 1377 vm_page_unlock_queues(); 1378 VM_OBJECT_UNLOCK(new_object); 1379 m->oflags |= VPO_WANTED; 1380 msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0); 1381 VM_OBJECT_LOCK(new_object); 1382 goto retry; 1383 } 1384 vm_page_rename(m, new_object, idx); 1385 /* page automatically made dirty by rename and cache handled */ 1386 vm_page_busy(m); 1387 } 1388 vm_page_unlock_queues(); 1389 if (orig_object->type == OBJT_SWAP) { 1390 /* 1391 * swap_pager_copy() can sleep, in which case the orig_object's 1392 * and new_object's locks are released and reacquired. 1393 */ 1394 swap_pager_copy(orig_object, new_object, offidxstart, 0); 1395 1396 /* 1397 * Transfer any cached pages from orig_object to new_object. 1398 */ 1399 if (__predict_false(orig_object->cache != NULL)) 1400 vm_page_cache_transfer(orig_object, offidxstart, 1401 new_object); 1402 } 1403 VM_OBJECT_UNLOCK(orig_object); 1404 TAILQ_FOREACH(m, &new_object->memq, listq) 1405 vm_page_wakeup(m); 1406 VM_OBJECT_UNLOCK(new_object); 1407 entry->object.vm_object = new_object; 1408 entry->offset = 0LL; 1409 vm_object_deallocate(orig_object); 1410 VM_OBJECT_LOCK(new_object); 1411 } 1412 1413 #define OBSC_TEST_ALL_SHADOWED 0x0001 1414 #define OBSC_COLLAPSE_NOWAIT 0x0002 1415 #define OBSC_COLLAPSE_WAIT 0x0004 1416 1417 static int 1418 vm_object_backing_scan(vm_object_t object, int op) 1419 { 1420 int r = 1; 1421 vm_page_t p; 1422 vm_object_t backing_object; 1423 vm_pindex_t backing_offset_index; 1424 1425 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1426 VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED); 1427 1428 backing_object = object->backing_object; 1429 backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 1430 1431 /* 1432 * Initial conditions 1433 */ 1434 if (op & OBSC_TEST_ALL_SHADOWED) { 1435 /* 1436 * We do not want to have to test for the existence of cache 1437 * or swap pages in the backing object. XXX but with the 1438 * new swapper this would be pretty easy to do. 1439 * 1440 * XXX what about anonymous MAP_SHARED memory that hasn't 1441 * been ZFOD faulted yet? If we do not test for this, the 1442 * shadow test may succeed! XXX 1443 */ 1444 if (backing_object->type != OBJT_DEFAULT) { 1445 return (0); 1446 } 1447 } 1448 if (op & OBSC_COLLAPSE_WAIT) { 1449 vm_object_set_flag(backing_object, OBJ_DEAD); 1450 } 1451 1452 /* 1453 * Our scan 1454 */ 1455 p = TAILQ_FIRST(&backing_object->memq); 1456 while (p) { 1457 vm_page_t next = TAILQ_NEXT(p, listq); 1458 vm_pindex_t new_pindex = p->pindex - backing_offset_index; 1459 1460 if (op & OBSC_TEST_ALL_SHADOWED) { 1461 vm_page_t pp; 1462 1463 /* 1464 * Ignore pages outside the parent object's range 1465 * and outside the parent object's mapping of the 1466 * backing object. 1467 * 1468 * note that we do not busy the backing object's 1469 * page. 1470 */ 1471 if ( 1472 p->pindex < backing_offset_index || 1473 new_pindex >= object->size 1474 ) { 1475 p = next; 1476 continue; 1477 } 1478 1479 /* 1480 * See if the parent has the page or if the parent's 1481 * object pager has the page. If the parent has the 1482 * page but the page is not valid, the parent's 1483 * object pager must have the page. 1484 * 1485 * If this fails, the parent does not completely shadow 1486 * the object and we might as well give up now. 1487 */ 1488 1489 pp = vm_page_lookup(object, new_pindex); 1490 if ( 1491 (pp == NULL || pp->valid == 0) && 1492 !vm_pager_has_page(object, new_pindex, NULL, NULL) 1493 ) { 1494 r = 0; 1495 break; 1496 } 1497 } 1498 1499 /* 1500 * Check for busy page 1501 */ 1502 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) { 1503 vm_page_t pp; 1504 1505 if (op & OBSC_COLLAPSE_NOWAIT) { 1506 if ((p->oflags & VPO_BUSY) || 1507 !p->valid || 1508 p->busy) { 1509 p = next; 1510 continue; 1511 } 1512 } else if (op & OBSC_COLLAPSE_WAIT) { 1513 if ((p->oflags & VPO_BUSY) || p->busy) { 1514 vm_page_lock_queues(); 1515 vm_page_flag_set(p, PG_REFERENCED); 1516 vm_page_unlock_queues(); 1517 VM_OBJECT_UNLOCK(object); 1518 p->oflags |= VPO_WANTED; 1519 msleep(p, VM_OBJECT_MTX(backing_object), 1520 PDROP | PVM, "vmocol", 0); 1521 VM_OBJECT_LOCK(object); 1522 VM_OBJECT_LOCK(backing_object); 1523 /* 1524 * If we slept, anything could have 1525 * happened. Since the object is 1526 * marked dead, the backing offset 1527 * should not have changed so we 1528 * just restart our scan. 1529 */ 1530 p = TAILQ_FIRST(&backing_object->memq); 1531 continue; 1532 } 1533 } 1534 1535 KASSERT( 1536 p->object == backing_object, 1537 ("vm_object_backing_scan: object mismatch") 1538 ); 1539 1540 /* 1541 * Destroy any associated swap 1542 */ 1543 if (backing_object->type == OBJT_SWAP) { 1544 swap_pager_freespace( 1545 backing_object, 1546 p->pindex, 1547 1 1548 ); 1549 } 1550 1551 if ( 1552 p->pindex < backing_offset_index || 1553 new_pindex >= object->size 1554 ) { 1555 /* 1556 * Page is out of the parent object's range, we 1557 * can simply destroy it. 1558 */ 1559 vm_page_lock_queues(); 1560 KASSERT(!pmap_page_is_mapped(p), 1561 ("freeing mapped page %p", p)); 1562 if (p->wire_count == 0) 1563 vm_page_free(p); 1564 else 1565 vm_page_remove(p); 1566 vm_page_unlock_queues(); 1567 p = next; 1568 continue; 1569 } 1570 1571 pp = vm_page_lookup(object, new_pindex); 1572 if ( 1573 pp != NULL || 1574 vm_pager_has_page(object, new_pindex, NULL, NULL) 1575 ) { 1576 /* 1577 * page already exists in parent OR swap exists 1578 * for this location in the parent. Destroy 1579 * the original page from the backing object. 1580 * 1581 * Leave the parent's page alone 1582 */ 1583 vm_page_lock_queues(); 1584 KASSERT(!pmap_page_is_mapped(p), 1585 ("freeing mapped page %p", p)); 1586 if (p->wire_count == 0) 1587 vm_page_free(p); 1588 else 1589 vm_page_remove(p); 1590 vm_page_unlock_queues(); 1591 p = next; 1592 continue; 1593 } 1594 1595 #if VM_NRESERVLEVEL > 0 1596 /* 1597 * Rename the reservation. 1598 */ 1599 vm_reserv_rename(p, object, backing_object, 1600 backing_offset_index); 1601 #endif 1602 1603 /* 1604 * Page does not exist in parent, rename the 1605 * page from the backing object to the main object. 1606 * 1607 * If the page was mapped to a process, it can remain 1608 * mapped through the rename. 1609 */ 1610 vm_page_lock_queues(); 1611 vm_page_rename(p, object, new_pindex); 1612 vm_page_unlock_queues(); 1613 /* page automatically made dirty by rename */ 1614 } 1615 p = next; 1616 } 1617 return (r); 1618 } 1619 1620 1621 /* 1622 * this version of collapse allows the operation to occur earlier and 1623 * when paging_in_progress is true for an object... This is not a complete 1624 * operation, but should plug 99.9% of the rest of the leaks. 1625 */ 1626 static void 1627 vm_object_qcollapse(vm_object_t object) 1628 { 1629 vm_object_t backing_object = object->backing_object; 1630 1631 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1632 VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED); 1633 1634 if (backing_object->ref_count != 1) 1635 return; 1636 1637 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT); 1638 } 1639 1640 /* 1641 * vm_object_collapse: 1642 * 1643 * Collapse an object with the object backing it. 1644 * Pages in the backing object are moved into the 1645 * parent, and the backing object is deallocated. 1646 */ 1647 void 1648 vm_object_collapse(vm_object_t object) 1649 { 1650 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1651 1652 while (TRUE) { 1653 vm_object_t backing_object; 1654 1655 /* 1656 * Verify that the conditions are right for collapse: 1657 * 1658 * The object exists and the backing object exists. 1659 */ 1660 if ((backing_object = object->backing_object) == NULL) 1661 break; 1662 1663 /* 1664 * we check the backing object first, because it is most likely 1665 * not collapsable. 1666 */ 1667 VM_OBJECT_LOCK(backing_object); 1668 if (backing_object->handle != NULL || 1669 (backing_object->type != OBJT_DEFAULT && 1670 backing_object->type != OBJT_SWAP) || 1671 (backing_object->flags & OBJ_DEAD) || 1672 object->handle != NULL || 1673 (object->type != OBJT_DEFAULT && 1674 object->type != OBJT_SWAP) || 1675 (object->flags & OBJ_DEAD)) { 1676 VM_OBJECT_UNLOCK(backing_object); 1677 break; 1678 } 1679 1680 if ( 1681 object->paging_in_progress != 0 || 1682 backing_object->paging_in_progress != 0 1683 ) { 1684 vm_object_qcollapse(object); 1685 VM_OBJECT_UNLOCK(backing_object); 1686 break; 1687 } 1688 /* 1689 * We know that we can either collapse the backing object (if 1690 * the parent is the only reference to it) or (perhaps) have 1691 * the parent bypass the object if the parent happens to shadow 1692 * all the resident pages in the entire backing object. 1693 * 1694 * This is ignoring pager-backed pages such as swap pages. 1695 * vm_object_backing_scan fails the shadowing test in this 1696 * case. 1697 */ 1698 if (backing_object->ref_count == 1) { 1699 /* 1700 * If there is exactly one reference to the backing 1701 * object, we can collapse it into the parent. 1702 */ 1703 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT); 1704 1705 #if VM_NRESERVLEVEL > 0 1706 /* 1707 * Break any reservations from backing_object. 1708 */ 1709 if (__predict_false(!LIST_EMPTY(&backing_object->rvq))) 1710 vm_reserv_break_all(backing_object); 1711 #endif 1712 1713 /* 1714 * Move the pager from backing_object to object. 1715 */ 1716 if (backing_object->type == OBJT_SWAP) { 1717 /* 1718 * swap_pager_copy() can sleep, in which case 1719 * the backing_object's and object's locks are 1720 * released and reacquired. 1721 */ 1722 swap_pager_copy( 1723 backing_object, 1724 object, 1725 OFF_TO_IDX(object->backing_object_offset), TRUE); 1726 1727 /* 1728 * Free any cached pages from backing_object. 1729 */ 1730 if (__predict_false(backing_object->cache != NULL)) 1731 vm_page_cache_free(backing_object, 0, 0); 1732 } 1733 /* 1734 * Object now shadows whatever backing_object did. 1735 * Note that the reference to 1736 * backing_object->backing_object moves from within 1737 * backing_object to within object. 1738 */ 1739 LIST_REMOVE(object, shadow_list); 1740 backing_object->shadow_count--; 1741 backing_object->generation++; 1742 if (backing_object->backing_object) { 1743 VM_OBJECT_LOCK(backing_object->backing_object); 1744 LIST_REMOVE(backing_object, shadow_list); 1745 LIST_INSERT_HEAD( 1746 &backing_object->backing_object->shadow_head, 1747 object, shadow_list); 1748 /* 1749 * The shadow_count has not changed. 1750 */ 1751 backing_object->backing_object->generation++; 1752 VM_OBJECT_UNLOCK(backing_object->backing_object); 1753 } 1754 object->backing_object = backing_object->backing_object; 1755 object->backing_object_offset += 1756 backing_object->backing_object_offset; 1757 1758 /* 1759 * Discard backing_object. 1760 * 1761 * Since the backing object has no pages, no pager left, 1762 * and no object references within it, all that is 1763 * necessary is to dispose of it. 1764 */ 1765 KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object)); 1766 VM_OBJECT_UNLOCK(backing_object); 1767 1768 mtx_lock(&vm_object_list_mtx); 1769 TAILQ_REMOVE( 1770 &vm_object_list, 1771 backing_object, 1772 object_list 1773 ); 1774 mtx_unlock(&vm_object_list_mtx); 1775 1776 uma_zfree(obj_zone, backing_object); 1777 1778 object_collapses++; 1779 } else { 1780 vm_object_t new_backing_object; 1781 1782 /* 1783 * If we do not entirely shadow the backing object, 1784 * there is nothing we can do so we give up. 1785 */ 1786 if (object->resident_page_count != object->size && 1787 vm_object_backing_scan(object, 1788 OBSC_TEST_ALL_SHADOWED) == 0) { 1789 VM_OBJECT_UNLOCK(backing_object); 1790 break; 1791 } 1792 1793 /* 1794 * Make the parent shadow the next object in the 1795 * chain. Deallocating backing_object will not remove 1796 * it, since its reference count is at least 2. 1797 */ 1798 LIST_REMOVE(object, shadow_list); 1799 backing_object->shadow_count--; 1800 backing_object->generation++; 1801 1802 new_backing_object = backing_object->backing_object; 1803 if ((object->backing_object = new_backing_object) != NULL) { 1804 VM_OBJECT_LOCK(new_backing_object); 1805 LIST_INSERT_HEAD( 1806 &new_backing_object->shadow_head, 1807 object, 1808 shadow_list 1809 ); 1810 new_backing_object->shadow_count++; 1811 new_backing_object->generation++; 1812 vm_object_reference_locked(new_backing_object); 1813 VM_OBJECT_UNLOCK(new_backing_object); 1814 object->backing_object_offset += 1815 backing_object->backing_object_offset; 1816 } 1817 1818 /* 1819 * Drop the reference count on backing_object. Since 1820 * its ref_count was at least 2, it will not vanish. 1821 */ 1822 backing_object->ref_count--; 1823 VM_OBJECT_UNLOCK(backing_object); 1824 object_bypasses++; 1825 } 1826 1827 /* 1828 * Try again with this object's new backing object. 1829 */ 1830 } 1831 } 1832 1833 /* 1834 * vm_object_page_remove: 1835 * 1836 * For the given object, either frees or invalidates each of the 1837 * specified pages. In general, a page is freed. However, if a 1838 * page is wired for any reason other than the existence of a 1839 * managed, wired mapping, then it may be invalidated but not 1840 * removed from the object. Pages are specified by the given 1841 * range ["start", "end") and Boolean "clean_only". As a 1842 * special case, if "end" is zero, then the range extends from 1843 * "start" to the end of the object. If "clean_only" is TRUE, 1844 * then only the non-dirty pages within the specified range are 1845 * affected. 1846 * 1847 * In general, this operation should only be performed on objects 1848 * that contain managed pages. There are two exceptions. First, 1849 * it may be performed on the kernel and kmem objects. Second, 1850 * it may be used by msync(..., MS_INVALIDATE) to invalidate 1851 * device-backed pages. 1852 * 1853 * The object must be locked. 1854 */ 1855 void 1856 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 1857 boolean_t clean_only) 1858 { 1859 vm_page_t p, next; 1860 int wirings; 1861 1862 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1863 if (object->resident_page_count == 0) 1864 goto skipmemq; 1865 1866 /* 1867 * Since physically-backed objects do not use managed pages, we can't 1868 * remove pages from the object (we must instead remove the page 1869 * references, and then destroy the object). 1870 */ 1871 KASSERT(object->type != OBJT_PHYS || object == kernel_object || 1872 object == kmem_object, 1873 ("attempt to remove pages from a physical object")); 1874 1875 vm_object_pip_add(object, 1); 1876 again: 1877 vm_page_lock_queues(); 1878 if ((p = TAILQ_FIRST(&object->memq)) != NULL) { 1879 if (p->pindex < start) { 1880 p = vm_page_splay(start, object->root); 1881 if ((object->root = p)->pindex < start) 1882 p = TAILQ_NEXT(p, listq); 1883 } 1884 } 1885 /* 1886 * Assert: the variable p is either (1) the page with the 1887 * least pindex greater than or equal to the parameter pindex 1888 * or (2) NULL. 1889 */ 1890 for (; 1891 p != NULL && (p->pindex < end || end == 0); 1892 p = next) { 1893 next = TAILQ_NEXT(p, listq); 1894 1895 /* 1896 * If the page is wired for any reason besides the 1897 * existence of managed, wired mappings, then it cannot 1898 * be freed. For example, fictitious pages, which 1899 * represent device memory, are inherently wired and 1900 * cannot be freed. They can, however, be invalidated 1901 * if "clean_only" is FALSE. 1902 */ 1903 if ((wirings = p->wire_count) != 0 && 1904 (wirings = pmap_page_wired_mappings(p)) != p->wire_count) { 1905 /* Fictitious pages do not have managed mappings. */ 1906 if ((p->flags & PG_FICTITIOUS) == 0) 1907 pmap_remove_all(p); 1908 /* Account for removal of managed, wired mappings. */ 1909 p->wire_count -= wirings; 1910 if (!clean_only) 1911 p->valid = 0; 1912 continue; 1913 } 1914 if (vm_page_sleep_if_busy(p, TRUE, "vmopar")) 1915 goto again; 1916 KASSERT((p->flags & PG_FICTITIOUS) == 0, 1917 ("vm_object_page_remove: page %p is fictitious", p)); 1918 if (clean_only && p->valid) { 1919 pmap_remove_write(p); 1920 if (p->valid & p->dirty) 1921 continue; 1922 } 1923 pmap_remove_all(p); 1924 /* Account for removal of managed, wired mappings. */ 1925 if (wirings != 0) 1926 p->wire_count -= wirings; 1927 vm_page_free(p); 1928 } 1929 vm_page_unlock_queues(); 1930 vm_object_pip_wakeup(object); 1931 skipmemq: 1932 if (__predict_false(object->cache != NULL)) 1933 vm_page_cache_free(object, start, end); 1934 } 1935 1936 /* 1937 * Routine: vm_object_coalesce 1938 * Function: Coalesces two objects backing up adjoining 1939 * regions of memory into a single object. 1940 * 1941 * returns TRUE if objects were combined. 1942 * 1943 * NOTE: Only works at the moment if the second object is NULL - 1944 * if it's not, which object do we lock first? 1945 * 1946 * Parameters: 1947 * prev_object First object to coalesce 1948 * prev_offset Offset into prev_object 1949 * prev_size Size of reference to prev_object 1950 * next_size Size of reference to the second object 1951 * 1952 * Conditions: 1953 * The object must *not* be locked. 1954 */ 1955 boolean_t 1956 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 1957 vm_size_t prev_size, vm_size_t next_size) 1958 { 1959 vm_pindex_t next_pindex; 1960 1961 if (prev_object == NULL) 1962 return (TRUE); 1963 VM_OBJECT_LOCK(prev_object); 1964 if (prev_object->type != OBJT_DEFAULT && 1965 prev_object->type != OBJT_SWAP) { 1966 VM_OBJECT_UNLOCK(prev_object); 1967 return (FALSE); 1968 } 1969 1970 /* 1971 * Try to collapse the object first 1972 */ 1973 vm_object_collapse(prev_object); 1974 1975 /* 1976 * Can't coalesce if: . more than one reference . paged out . shadows 1977 * another object . has a copy elsewhere (any of which mean that the 1978 * pages not mapped to prev_entry may be in use anyway) 1979 */ 1980 if (prev_object->backing_object != NULL) { 1981 VM_OBJECT_UNLOCK(prev_object); 1982 return (FALSE); 1983 } 1984 1985 prev_size >>= PAGE_SHIFT; 1986 next_size >>= PAGE_SHIFT; 1987 next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 1988 1989 if ((prev_object->ref_count > 1) && 1990 (prev_object->size != next_pindex)) { 1991 VM_OBJECT_UNLOCK(prev_object); 1992 return (FALSE); 1993 } 1994 1995 /* 1996 * Remove any pages that may still be in the object from a previous 1997 * deallocation. 1998 */ 1999 if (next_pindex < prev_object->size) { 2000 vm_object_page_remove(prev_object, 2001 next_pindex, 2002 next_pindex + next_size, FALSE); 2003 if (prev_object->type == OBJT_SWAP) 2004 swap_pager_freespace(prev_object, 2005 next_pindex, next_size); 2006 } 2007 2008 /* 2009 * Extend the object if necessary. 2010 */ 2011 if (next_pindex + next_size > prev_object->size) 2012 prev_object->size = next_pindex + next_size; 2013 2014 VM_OBJECT_UNLOCK(prev_object); 2015 return (TRUE); 2016 } 2017 2018 void 2019 vm_object_set_writeable_dirty(vm_object_t object) 2020 { 2021 struct vnode *vp; 2022 2023 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 2024 if ((object->flags & OBJ_MIGHTBEDIRTY) != 0) 2025 return; 2026 vm_object_set_flag(object, OBJ_MIGHTBEDIRTY); 2027 if (object->type == OBJT_VNODE && 2028 (vp = (struct vnode *)object->handle) != NULL) { 2029 VI_LOCK(vp); 2030 vp->v_iflag |= VI_OBJDIRTY; 2031 VI_UNLOCK(vp); 2032 } 2033 } 2034 2035 #include "opt_ddb.h" 2036 #ifdef DDB 2037 #include <sys/kernel.h> 2038 2039 #include <sys/cons.h> 2040 2041 #include <ddb/ddb.h> 2042 2043 static int 2044 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2045 { 2046 vm_map_t tmpm; 2047 vm_map_entry_t tmpe; 2048 vm_object_t obj; 2049 int entcount; 2050 2051 if (map == 0) 2052 return 0; 2053 2054 if (entry == 0) { 2055 tmpe = map->header.next; 2056 entcount = map->nentries; 2057 while (entcount-- && (tmpe != &map->header)) { 2058 if (_vm_object_in_map(map, object, tmpe)) { 2059 return 1; 2060 } 2061 tmpe = tmpe->next; 2062 } 2063 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 2064 tmpm = entry->object.sub_map; 2065 tmpe = tmpm->header.next; 2066 entcount = tmpm->nentries; 2067 while (entcount-- && tmpe != &tmpm->header) { 2068 if (_vm_object_in_map(tmpm, object, tmpe)) { 2069 return 1; 2070 } 2071 tmpe = tmpe->next; 2072 } 2073 } else if ((obj = entry->object.vm_object) != NULL) { 2074 for (; obj; obj = obj->backing_object) 2075 if (obj == object) { 2076 return 1; 2077 } 2078 } 2079 return 0; 2080 } 2081 2082 static int 2083 vm_object_in_map(vm_object_t object) 2084 { 2085 struct proc *p; 2086 2087 /* sx_slock(&allproc_lock); */ 2088 FOREACH_PROC_IN_SYSTEM(p) { 2089 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2090 continue; 2091 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 2092 /* sx_sunlock(&allproc_lock); */ 2093 return 1; 2094 } 2095 } 2096 /* sx_sunlock(&allproc_lock); */ 2097 if (_vm_object_in_map(kernel_map, object, 0)) 2098 return 1; 2099 if (_vm_object_in_map(kmem_map, object, 0)) 2100 return 1; 2101 if (_vm_object_in_map(pager_map, object, 0)) 2102 return 1; 2103 if (_vm_object_in_map(buffer_map, object, 0)) 2104 return 1; 2105 return 0; 2106 } 2107 2108 DB_SHOW_COMMAND(vmochk, vm_object_check) 2109 { 2110 vm_object_t object; 2111 2112 /* 2113 * make sure that internal objs are in a map somewhere 2114 * and none have zero ref counts. 2115 */ 2116 TAILQ_FOREACH(object, &vm_object_list, object_list) { 2117 if (object->handle == NULL && 2118 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 2119 if (object->ref_count == 0) { 2120 db_printf("vmochk: internal obj has zero ref count: %ld\n", 2121 (long)object->size); 2122 } 2123 if (!vm_object_in_map(object)) { 2124 db_printf( 2125 "vmochk: internal obj is not in a map: " 2126 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2127 object->ref_count, (u_long)object->size, 2128 (u_long)object->size, 2129 (void *)object->backing_object); 2130 } 2131 } 2132 } 2133 } 2134 2135 /* 2136 * vm_object_print: [ debug ] 2137 */ 2138 DB_SHOW_COMMAND(object, vm_object_print_static) 2139 { 2140 /* XXX convert args. */ 2141 vm_object_t object = (vm_object_t)addr; 2142 boolean_t full = have_addr; 2143 2144 vm_page_t p; 2145 2146 /* XXX count is an (unused) arg. Avoid shadowing it. */ 2147 #define count was_count 2148 2149 int count; 2150 2151 if (object == NULL) 2152 return; 2153 2154 db_iprintf( 2155 "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n", 2156 object, (int)object->type, (uintmax_t)object->size, 2157 object->resident_page_count, object->ref_count, object->flags); 2158 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 2159 object->shadow_count, 2160 object->backing_object ? object->backing_object->ref_count : 0, 2161 object->backing_object, (uintmax_t)object->backing_object_offset); 2162 2163 if (!full) 2164 return; 2165 2166 db_indent += 2; 2167 count = 0; 2168 TAILQ_FOREACH(p, &object->memq, listq) { 2169 if (count == 0) 2170 db_iprintf("memory:="); 2171 else if (count == 6) { 2172 db_printf("\n"); 2173 db_iprintf(" ..."); 2174 count = 0; 2175 } else 2176 db_printf(","); 2177 count++; 2178 2179 db_printf("(off=0x%jx,page=0x%jx)", 2180 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2181 } 2182 if (count != 0) 2183 db_printf("\n"); 2184 db_indent -= 2; 2185 } 2186 2187 /* XXX. */ 2188 #undef count 2189 2190 /* XXX need this non-static entry for calling from vm_map_print. */ 2191 void 2192 vm_object_print( 2193 /* db_expr_t */ long addr, 2194 boolean_t have_addr, 2195 /* db_expr_t */ long count, 2196 char *modif) 2197 { 2198 vm_object_print_static(addr, have_addr, count, modif); 2199 } 2200 2201 DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 2202 { 2203 vm_object_t object; 2204 int nl = 0; 2205 int c; 2206 2207 TAILQ_FOREACH(object, &vm_object_list, object_list) { 2208 vm_pindex_t idx, fidx; 2209 vm_pindex_t osize; 2210 vm_paddr_t pa = -1; 2211 int rcount; 2212 vm_page_t m; 2213 2214 db_printf("new object: %p\n", (void *)object); 2215 if (nl > 18) { 2216 c = cngetc(); 2217 if (c != ' ') 2218 return; 2219 nl = 0; 2220 } 2221 nl++; 2222 rcount = 0; 2223 fidx = 0; 2224 osize = object->size; 2225 if (osize > 128) 2226 osize = 128; 2227 for (idx = 0; idx < osize; idx++) { 2228 m = vm_page_lookup(object, idx); 2229 if (m == NULL) { 2230 if (rcount) { 2231 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2232 (long)fidx, rcount, (long)pa); 2233 if (nl > 18) { 2234 c = cngetc(); 2235 if (c != ' ') 2236 return; 2237 nl = 0; 2238 } 2239 nl++; 2240 rcount = 0; 2241 } 2242 continue; 2243 } 2244 2245 2246 if (rcount && 2247 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 2248 ++rcount; 2249 continue; 2250 } 2251 if (rcount) { 2252 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2253 (long)fidx, rcount, (long)pa); 2254 if (nl > 18) { 2255 c = cngetc(); 2256 if (c != ' ') 2257 return; 2258 nl = 0; 2259 } 2260 nl++; 2261 } 2262 fidx = idx; 2263 pa = VM_PAGE_TO_PHYS(m); 2264 rcount = 1; 2265 } 2266 if (rcount) { 2267 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2268 (long)fidx, rcount, (long)pa); 2269 if (nl > 18) { 2270 c = cngetc(); 2271 if (c != ' ') 2272 return; 2273 nl = 0; 2274 } 2275 nl++; 2276 } 2277 } 2278 } 2279 #endif /* DDB */ 2280