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