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