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