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