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