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