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