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