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