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