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