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