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