]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/vm_object.c
Merge ^/head r351732 through r352104.
[FreeBSD/FreeBSD.git] / sys / vm / vm_object.c
1 /*-
2  * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
3  *
4  * Copyright (c) 1991, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * The Mach Operating System project at Carnegie-Mellon University.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      from: @(#)vm_object.c   8.5 (Berkeley) 3/22/94
35  *
36  *
37  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38  * All rights reserved.
39  *
40  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41  *
42  * Permission to use, copy, modify and distribute this software and
43  * its documentation is hereby granted, provided that both the copyright
44  * notice and this permission notice appear in all copies of the
45  * software, derivative works or modified versions, and any portions
46  * thereof, and that both notices appear in supporting documentation.
47  *
48  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51  *
52  * Carnegie Mellon requests users of this software to return to
53  *
54  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55  *  School of Computer Science
56  *  Carnegie Mellon University
57  *  Pittsburgh PA 15213-3890
58  *
59  * any improvements or extensions that they make and grant Carnegie the
60  * rights to redistribute these changes.
61  */
62
63 /*
64  *      Virtual memory object module.
65  */
66
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
69
70 #include "opt_vm.h"
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/cpuset.h>
75 #include <sys/lock.h>
76 #include <sys/mman.h>
77 #include <sys/mount.h>
78 #include <sys/kernel.h>
79 #include <sys/pctrie.h>
80 #include <sys/sysctl.h>
81 #include <sys/mutex.h>
82 #include <sys/proc.h>           /* for curproc, pageproc */
83 #include <sys/refcount.h>
84 #include <sys/socket.h>
85 #include <sys/resourcevar.h>
86 #include <sys/rwlock.h>
87 #include <sys/user.h>
88 #include <sys/vnode.h>
89 #include <sys/vmmeter.h>
90 #include <sys/sx.h>
91
92 #include <vm/vm.h>
93 #include <vm/vm_param.h>
94 #include <vm/pmap.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_pageout.h>
99 #include <vm/vm_pager.h>
100 #include <vm/vm_phys.h>
101 #include <vm/vm_pagequeue.h>
102 #include <vm/swap_pager.h>
103 #include <vm/vm_kern.h>
104 #include <vm/vm_extern.h>
105 #include <vm/vm_radix.h>
106 #include <vm/vm_reserv.h>
107 #include <vm/uma.h>
108
109 static int old_msync;
110 SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
111     "Use old (insecure) msync behavior");
112
113 static int      vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
114                     int pagerflags, int flags, boolean_t *clearobjflags,
115                     boolean_t *eio);
116 static boolean_t vm_object_page_remove_write(vm_page_t p, int flags,
117                     boolean_t *clearobjflags);
118 static void     vm_object_qcollapse(vm_object_t object);
119 static void     vm_object_vndeallocate(vm_object_t object);
120
121 /*
122  *      Virtual memory objects maintain the actual data
123  *      associated with allocated virtual memory.  A given
124  *      page of memory exists within exactly one object.
125  *
126  *      An object is only deallocated when all "references"
127  *      are given up.  Only one "reference" to a given
128  *      region of an object should be writeable.
129  *
130  *      Associated with each object is a list of all resident
131  *      memory pages belonging to that object; this list is
132  *      maintained by the "vm_page" module, and locked by the object's
133  *      lock.
134  *
135  *      Each object also records a "pager" routine which is
136  *      used to retrieve (and store) pages to the proper backing
137  *      storage.  In addition, objects may be backed by other
138  *      objects from which they were virtual-copied.
139  *
140  *      The only items within the object structure which are
141  *      modified after time of creation are:
142  *              reference count         locked by object's lock
143  *              pager routine           locked by object's lock
144  *
145  */
146
147 struct object_q vm_object_list;
148 struct mtx vm_object_list_mtx;  /* lock for object list and count */
149
150 struct vm_object kernel_object_store;
151
152 static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0,
153     "VM object stats");
154
155 static counter_u64_t object_collapses = EARLY_COUNTER;
156 SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
157     &object_collapses,
158     "VM object collapses");
159
160 static counter_u64_t object_bypasses = EARLY_COUNTER;
161 SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
162     &object_bypasses,
163     "VM object bypasses");
164
165 static void
166 counter_startup(void)
167 {
168
169         object_collapses = counter_u64_alloc(M_WAITOK);
170         object_bypasses = counter_u64_alloc(M_WAITOK);
171 }
172 SYSINIT(object_counters, SI_SUB_CPU, SI_ORDER_ANY, counter_startup, NULL);
173
174 static uma_zone_t obj_zone;
175
176 static int vm_object_zinit(void *mem, int size, int flags);
177
178 #ifdef INVARIANTS
179 static void vm_object_zdtor(void *mem, int size, void *arg);
180
181 static void
182 vm_object_zdtor(void *mem, int size, void *arg)
183 {
184         vm_object_t object;
185
186         object = (vm_object_t)mem;
187         KASSERT(object->ref_count == 0,
188             ("object %p ref_count = %d", object, object->ref_count));
189         KASSERT(TAILQ_EMPTY(&object->memq),
190             ("object %p has resident pages in its memq", object));
191         KASSERT(vm_radix_is_empty(&object->rtree),
192             ("object %p has resident pages in its trie", object));
193 #if VM_NRESERVLEVEL > 0
194         KASSERT(LIST_EMPTY(&object->rvq),
195             ("object %p has reservations",
196             object));
197 #endif
198         KASSERT(object->paging_in_progress == 0,
199             ("object %p paging_in_progress = %d",
200             object, object->paging_in_progress));
201         KASSERT(object->resident_page_count == 0,
202             ("object %p resident_page_count = %d",
203             object, object->resident_page_count));
204         KASSERT(object->shadow_count == 0,
205             ("object %p shadow_count = %d",
206             object, object->shadow_count));
207         KASSERT(object->type == OBJT_DEAD,
208             ("object %p has non-dead type %d",
209             object, object->type));
210 }
211 #endif
212
213 static int
214 vm_object_zinit(void *mem, int size, int flags)
215 {
216         vm_object_t object;
217
218         object = (vm_object_t)mem;
219         rw_init_flags(&object->lock, "vm object", RW_DUPOK | RW_NEW);
220
221         /* These are true for any object that has been freed */
222         object->type = OBJT_DEAD;
223         object->ref_count = 0;
224         vm_radix_init(&object->rtree);
225         refcount_init(&object->paging_in_progress, 0);
226         object->resident_page_count = 0;
227         object->shadow_count = 0;
228         object->flags = OBJ_DEAD;
229
230         mtx_lock(&vm_object_list_mtx);
231         TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
232         mtx_unlock(&vm_object_list_mtx);
233         return (0);
234 }
235
236 static void
237 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
238 {
239
240         TAILQ_INIT(&object->memq);
241         LIST_INIT(&object->shadow_head);
242
243         object->type = type;
244         if (type == OBJT_SWAP)
245                 pctrie_init(&object->un_pager.swp.swp_blks);
246
247         /*
248          * Ensure that swap_pager_swapoff() iteration over object_list
249          * sees up to date type and pctrie head if it observed
250          * non-dead object.
251          */
252         atomic_thread_fence_rel();
253
254         switch (type) {
255         case OBJT_DEAD:
256                 panic("_vm_object_allocate: can't create OBJT_DEAD");
257         case OBJT_DEFAULT:
258         case OBJT_SWAP:
259                 object->flags = OBJ_ONEMAPPING;
260                 break;
261         case OBJT_DEVICE:
262         case OBJT_SG:
263                 object->flags = OBJ_FICTITIOUS | OBJ_UNMANAGED;
264                 break;
265         case OBJT_MGTDEVICE:
266                 object->flags = OBJ_FICTITIOUS;
267                 break;
268         case OBJT_PHYS:
269                 object->flags = OBJ_UNMANAGED;
270                 break;
271         case OBJT_VNODE:
272                 object->flags = 0;
273                 break;
274         default:
275                 panic("_vm_object_allocate: type %d is undefined", type);
276         }
277         object->size = size;
278         object->domain.dr_policy = NULL;
279         object->generation = 1;
280         object->ref_count = 1;
281         object->memattr = VM_MEMATTR_DEFAULT;
282         object->cred = NULL;
283         object->charge = 0;
284         object->handle = NULL;
285         object->backing_object = NULL;
286         object->backing_object_offset = (vm_ooffset_t) 0;
287 #if VM_NRESERVLEVEL > 0
288         LIST_INIT(&object->rvq);
289 #endif
290         umtx_shm_object_init(object);
291 }
292
293 /*
294  *      vm_object_init:
295  *
296  *      Initialize the VM objects module.
297  */
298 void
299 vm_object_init(void)
300 {
301         TAILQ_INIT(&vm_object_list);
302         mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
303         
304         rw_init(&kernel_object->lock, "kernel vm object");
305         _vm_object_allocate(OBJT_PHYS, atop(VM_MAX_KERNEL_ADDRESS -
306             VM_MIN_KERNEL_ADDRESS), kernel_object);
307 #if VM_NRESERVLEVEL > 0
308         kernel_object->flags |= OBJ_COLORED;
309         kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
310 #endif
311
312         /*
313          * The lock portion of struct vm_object must be type stable due
314          * to vm_pageout_fallback_object_lock locking a vm object
315          * without holding any references to it.
316          */
317         obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
318 #ifdef INVARIANTS
319             vm_object_zdtor,
320 #else
321             NULL,
322 #endif
323             vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
324
325         vm_radix_zinit();
326 }
327
328 void
329 vm_object_clear_flag(vm_object_t object, u_short bits)
330 {
331
332         VM_OBJECT_ASSERT_WLOCKED(object);
333         object->flags &= ~bits;
334 }
335
336 /*
337  *      Sets the default memory attribute for the specified object.  Pages
338  *      that are allocated to this object are by default assigned this memory
339  *      attribute.
340  *
341  *      Presently, this function must be called before any pages are allocated
342  *      to the object.  In the future, this requirement may be relaxed for
343  *      "default" and "swap" objects.
344  */
345 int
346 vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
347 {
348
349         VM_OBJECT_ASSERT_WLOCKED(object);
350         switch (object->type) {
351         case OBJT_DEFAULT:
352         case OBJT_DEVICE:
353         case OBJT_MGTDEVICE:
354         case OBJT_PHYS:
355         case OBJT_SG:
356         case OBJT_SWAP:
357         case OBJT_VNODE:
358                 if (!TAILQ_EMPTY(&object->memq))
359                         return (KERN_FAILURE);
360                 break;
361         case OBJT_DEAD:
362                 return (KERN_INVALID_ARGUMENT);
363         default:
364                 panic("vm_object_set_memattr: object %p is of undefined type",
365                     object);
366         }
367         object->memattr = memattr;
368         return (KERN_SUCCESS);
369 }
370
371 void
372 vm_object_pip_add(vm_object_t object, short i)
373 {
374
375         refcount_acquiren(&object->paging_in_progress, i);
376 }
377
378 void
379 vm_object_pip_wakeup(vm_object_t object)
380 {
381
382         refcount_release(&object->paging_in_progress);
383 }
384
385 void
386 vm_object_pip_wakeupn(vm_object_t object, short i)
387 {
388
389         refcount_releasen(&object->paging_in_progress, i);
390 }
391
392 void
393 vm_object_pip_wait(vm_object_t object, char *waitid)
394 {
395
396         VM_OBJECT_ASSERT_WLOCKED(object);
397
398         while (object->paging_in_progress) {
399                 VM_OBJECT_WUNLOCK(object);
400                 refcount_wait(&object->paging_in_progress, waitid, PVM);
401                 VM_OBJECT_WLOCK(object);
402         }
403 }
404
405 void
406 vm_object_pip_wait_unlocked(vm_object_t object, char *waitid)
407 {
408
409         VM_OBJECT_ASSERT_UNLOCKED(object);
410
411         while (object->paging_in_progress)
412                 refcount_wait(&object->paging_in_progress, waitid, PVM);
413 }
414
415 /*
416  *      vm_object_allocate:
417  *
418  *      Returns a new object with the given size.
419  */
420 vm_object_t
421 vm_object_allocate(objtype_t type, vm_pindex_t size)
422 {
423         vm_object_t object;
424
425         object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
426         _vm_object_allocate(type, size, object);
427         return (object);
428 }
429
430
431 /*
432  *      vm_object_reference:
433  *
434  *      Gets another reference to the given object.  Note: OBJ_DEAD
435  *      objects can be referenced during final cleaning.
436  */
437 void
438 vm_object_reference(vm_object_t object)
439 {
440         if (object == NULL)
441                 return;
442         VM_OBJECT_WLOCK(object);
443         vm_object_reference_locked(object);
444         VM_OBJECT_WUNLOCK(object);
445 }
446
447 /*
448  *      vm_object_reference_locked:
449  *
450  *      Gets another reference to the given object.
451  *
452  *      The object must be locked.
453  */
454 void
455 vm_object_reference_locked(vm_object_t object)
456 {
457         struct vnode *vp;
458
459         VM_OBJECT_ASSERT_WLOCKED(object);
460         object->ref_count++;
461         if (object->type == OBJT_VNODE) {
462                 vp = object->handle;
463                 vref(vp);
464         }
465 }
466
467 /*
468  * Handle deallocating an object of type OBJT_VNODE.
469  */
470 static void
471 vm_object_vndeallocate(vm_object_t object)
472 {
473         struct vnode *vp = (struct vnode *) object->handle;
474
475         VM_OBJECT_ASSERT_WLOCKED(object);
476         KASSERT(object->type == OBJT_VNODE,
477             ("vm_object_vndeallocate: not a vnode object"));
478         KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
479 #ifdef INVARIANTS
480         if (object->ref_count == 0) {
481                 vn_printf(vp, "vm_object_vndeallocate ");
482                 panic("vm_object_vndeallocate: bad object reference count");
483         }
484 #endif
485
486         if (!umtx_shm_vnobj_persistent && object->ref_count == 1)
487                 umtx_shm_object_terminated(object);
488
489         object->ref_count--;
490
491         /* vrele may need the vnode lock. */
492         VM_OBJECT_WUNLOCK(object);
493         vrele(vp);
494 }
495
496 /*
497  *      vm_object_deallocate:
498  *
499  *      Release a reference to the specified object,
500  *      gained either through a vm_object_allocate
501  *      or a vm_object_reference call.  When all references
502  *      are gone, storage associated with this object
503  *      may be relinquished.
504  *
505  *      No object may be locked.
506  */
507 void
508 vm_object_deallocate(vm_object_t object)
509 {
510         vm_object_t temp;
511
512         while (object != NULL) {
513                 VM_OBJECT_WLOCK(object);
514                 if (object->type == OBJT_VNODE) {
515                         vm_object_vndeallocate(object);
516                         return;
517                 }
518
519                 KASSERT(object->ref_count != 0,
520                         ("vm_object_deallocate: object deallocated too many times: %d", object->type));
521
522                 /*
523                  * If the reference count goes to 0 we start calling
524                  * vm_object_terminate() on the object chain.
525                  * A ref count of 1 may be a special case depending on the
526                  * shadow count being 0 or 1.
527                  */
528                 object->ref_count--;
529                 if (object->ref_count > 1) {
530                         VM_OBJECT_WUNLOCK(object);
531                         return;
532                 } else if (object->ref_count == 1) {
533                         if (object->shadow_count == 0 &&
534                             object->handle == NULL &&
535                             (object->type == OBJT_DEFAULT ||
536                             (object->type == OBJT_SWAP &&
537                             (object->flags & OBJ_TMPFS_NODE) == 0))) {
538                                 vm_object_set_flag(object, OBJ_ONEMAPPING);
539                         } else if ((object->shadow_count == 1) &&
540                             (object->handle == NULL) &&
541                             (object->type == OBJT_DEFAULT ||
542                              object->type == OBJT_SWAP)) {
543                                 vm_object_t robject;
544
545                                 robject = LIST_FIRST(&object->shadow_head);
546                                 KASSERT(robject != NULL,
547                                     ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
548                                          object->ref_count,
549                                          object->shadow_count));
550                                 KASSERT((robject->flags & OBJ_TMPFS_NODE) == 0,
551                                     ("shadowed tmpfs v_object %p", object));
552                                 if (!VM_OBJECT_TRYWLOCK(robject)) {
553                                         /*
554                                          * Avoid a potential deadlock.
555                                          */
556                                         object->ref_count++;
557                                         VM_OBJECT_WUNLOCK(object);
558                                         /*
559                                          * More likely than not the thread
560                                          * holding robject's lock has lower
561                                          * priority than the current thread.
562                                          * Let the lower priority thread run.
563                                          */
564                                         pause("vmo_de", 1);
565                                         continue;
566                                 }
567                                 /*
568                                  * Collapse object into its shadow unless its
569                                  * shadow is dead.  In that case, object will
570                                  * be deallocated by the thread that is
571                                  * deallocating its shadow.
572                                  */
573                                 if ((robject->flags & OBJ_DEAD) == 0 &&
574                                     (robject->handle == NULL) &&
575                                     (robject->type == OBJT_DEFAULT ||
576                                      robject->type == OBJT_SWAP)) {
577
578                                         robject->ref_count++;
579 retry:
580                                         if (robject->paging_in_progress) {
581                                                 VM_OBJECT_WUNLOCK(object);
582                                                 vm_object_pip_wait(robject,
583                                                     "objde1");
584                                                 temp = robject->backing_object;
585                                                 if (object == temp) {
586                                                         VM_OBJECT_WLOCK(object);
587                                                         goto retry;
588                                                 }
589                                         } else if (object->paging_in_progress) {
590                                                 VM_OBJECT_WUNLOCK(robject);
591                                                 VM_OBJECT_WUNLOCK(object);
592                                                 refcount_wait(
593                                                     &object->paging_in_progress,
594                                                     "objde2", PVM);
595                                                 VM_OBJECT_WLOCK(robject);
596                                                 temp = robject->backing_object;
597                                                 if (object == temp) {
598                                                         VM_OBJECT_WLOCK(object);
599                                                         goto retry;
600                                                 }
601                                         } else
602                                                 VM_OBJECT_WUNLOCK(object);
603
604                                         if (robject->ref_count == 1) {
605                                                 robject->ref_count--;
606                                                 object = robject;
607                                                 goto doterm;
608                                         }
609                                         object = robject;
610                                         vm_object_collapse(object);
611                                         VM_OBJECT_WUNLOCK(object);
612                                         continue;
613                                 }
614                                 VM_OBJECT_WUNLOCK(robject);
615                         }
616                         VM_OBJECT_WUNLOCK(object);
617                         return;
618                 }
619 doterm:
620                 umtx_shm_object_terminated(object);
621                 temp = object->backing_object;
622                 if (temp != NULL) {
623                         KASSERT((object->flags & OBJ_TMPFS_NODE) == 0,
624                             ("shadowed tmpfs v_object 2 %p", object));
625                         VM_OBJECT_WLOCK(temp);
626                         LIST_REMOVE(object, shadow_list);
627                         temp->shadow_count--;
628                         VM_OBJECT_WUNLOCK(temp);
629                         object->backing_object = NULL;
630                 }
631                 /*
632                  * Don't double-terminate, we could be in a termination
633                  * recursion due to the terminate having to sync data
634                  * to disk.
635                  */
636                 if ((object->flags & OBJ_DEAD) == 0) {
637                         vm_object_set_flag(object, OBJ_DEAD);
638                         vm_object_terminate(object);
639                 } else
640                         VM_OBJECT_WUNLOCK(object);
641                 object = temp;
642         }
643 }
644
645 /*
646  *      vm_object_destroy removes the object from the global object list
647  *      and frees the space for the object.
648  */
649 void
650 vm_object_destroy(vm_object_t object)
651 {
652
653         /*
654          * Release the allocation charge.
655          */
656         if (object->cred != NULL) {
657                 swap_release_by_cred(object->charge, object->cred);
658                 object->charge = 0;
659                 crfree(object->cred);
660                 object->cred = NULL;
661         }
662
663         /*
664          * Free the space for the object.
665          */
666         uma_zfree(obj_zone, object);
667 }
668
669 /*
670  *      vm_object_terminate_pages removes any remaining pageable pages
671  *      from the object and resets the object to an empty state.
672  */
673 static void
674 vm_object_terminate_pages(vm_object_t object)
675 {
676         vm_page_t p, p_next;
677         struct mtx *mtx;
678
679         VM_OBJECT_ASSERT_WLOCKED(object);
680
681         mtx = NULL;
682
683         /*
684          * Free any remaining pageable pages.  This also removes them from the
685          * paging queues.  However, don't free wired pages, just remove them
686          * from the object.  Rather than incrementally removing each page from
687          * the object, the page and object are reset to any empty state. 
688          */
689         TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) {
690                 vm_page_assert_unbusied(p);
691                 if ((object->flags & OBJ_UNMANAGED) == 0)
692                         /*
693                          * vm_page_free_prep() only needs the page
694                          * lock for managed pages.
695                          */
696                         vm_page_change_lock(p, &mtx);
697                 p->object = NULL;
698                 if (vm_page_wired(p))
699                         continue;
700                 VM_CNT_INC(v_pfree);
701                 vm_page_free(p);
702         }
703         if (mtx != NULL)
704                 mtx_unlock(mtx);
705
706         /*
707          * If the object contained any pages, then reset it to an empty state.
708          * None of the object's fields, including "resident_page_count", were
709          * modified by the preceding loop.
710          */
711         if (object->resident_page_count != 0) {
712                 vm_radix_reclaim_allnodes(&object->rtree);
713                 TAILQ_INIT(&object->memq);
714                 object->resident_page_count = 0;
715                 if (object->type == OBJT_VNODE)
716                         vdrop(object->handle);
717         }
718 }
719
720 /*
721  *      vm_object_terminate actually destroys the specified object, freeing
722  *      up all previously used resources.
723  *
724  *      The object must be locked.
725  *      This routine may block.
726  */
727 void
728 vm_object_terminate(vm_object_t object)
729 {
730         VM_OBJECT_ASSERT_WLOCKED(object);
731         KASSERT((object->flags & OBJ_DEAD) != 0,
732             ("terminating non-dead obj %p", object));
733
734         /*
735          * wait for the pageout daemon to be done with the object
736          */
737         vm_object_pip_wait(object, "objtrm");
738
739         KASSERT(!object->paging_in_progress,
740                 ("vm_object_terminate: pageout in progress"));
741
742         KASSERT(object->ref_count == 0, 
743                 ("vm_object_terminate: object with references, ref_count=%d",
744                 object->ref_count));
745
746         if ((object->flags & OBJ_PG_DTOR) == 0)
747                 vm_object_terminate_pages(object);
748
749 #if VM_NRESERVLEVEL > 0
750         if (__predict_false(!LIST_EMPTY(&object->rvq)))
751                 vm_reserv_break_all(object);
752 #endif
753
754         KASSERT(object->cred == NULL || object->type == OBJT_DEFAULT ||
755             object->type == OBJT_SWAP,
756             ("%s: non-swap obj %p has cred", __func__, object));
757
758         /*
759          * Let the pager know object is dead.
760          */
761         vm_pager_deallocate(object);
762         VM_OBJECT_WUNLOCK(object);
763
764         vm_object_destroy(object);
765 }
766
767 /*
768  * Make the page read-only so that we can clear the object flags.  However, if
769  * this is a nosync mmap then the object is likely to stay dirty so do not
770  * mess with the page and do not clear the object flags.  Returns TRUE if the
771  * page should be flushed, and FALSE otherwise.
772  */
773 static boolean_t
774 vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *clearobjflags)
775 {
776
777         /*
778          * If we have been asked to skip nosync pages and this is a
779          * nosync page, skip it.  Note that the object flags were not
780          * cleared in this case so we do not have to set them.
781          */
782         if ((flags & OBJPC_NOSYNC) != 0 && (p->oflags & VPO_NOSYNC) != 0) {
783                 *clearobjflags = FALSE;
784                 return (FALSE);
785         } else {
786                 pmap_remove_write(p);
787                 return (p->dirty != 0);
788         }
789 }
790
791 /*
792  *      vm_object_page_clean
793  *
794  *      Clean all dirty pages in the specified range of object.  Leaves page 
795  *      on whatever queue it is currently on.   If NOSYNC is set then do not
796  *      write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
797  *      leaving the object dirty.
798  *
799  *      When stuffing pages asynchronously, allow clustering.  XXX we need a
800  *      synchronous clustering mode implementation.
801  *
802  *      Odd semantics: if start == end, we clean everything.
803  *
804  *      The object must be locked.
805  *
806  *      Returns FALSE if some page from the range was not written, as
807  *      reported by the pager, and TRUE otherwise.
808  */
809 boolean_t
810 vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end,
811     int flags)
812 {
813         vm_page_t np, p;
814         vm_pindex_t pi, tend, tstart;
815         int curgeneration, n, pagerflags;
816         boolean_t clearobjflags, eio, res;
817
818         VM_OBJECT_ASSERT_WLOCKED(object);
819
820         /*
821          * The OBJ_MIGHTBEDIRTY flag is only set for OBJT_VNODE
822          * objects.  The check below prevents the function from
823          * operating on non-vnode objects.
824          */
825         if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
826             object->resident_page_count == 0)
827                 return (TRUE);
828
829         pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
830             VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
831         pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
832
833         tstart = OFF_TO_IDX(start);
834         tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK);
835         clearobjflags = tstart == 0 && tend >= object->size;
836         res = TRUE;
837
838 rescan:
839         curgeneration = object->generation;
840
841         for (p = vm_page_find_least(object, tstart); p != NULL; p = np) {
842                 pi = p->pindex;
843                 if (pi >= tend)
844                         break;
845                 np = TAILQ_NEXT(p, listq);
846                 if (p->valid == 0)
847                         continue;
848                 if (vm_page_sleep_if_busy(p, "vpcwai")) {
849                         if (object->generation != curgeneration) {
850                                 if ((flags & OBJPC_SYNC) != 0)
851                                         goto rescan;
852                                 else
853                                         clearobjflags = FALSE;
854                         }
855                         np = vm_page_find_least(object, pi);
856                         continue;
857                 }
858                 if (!vm_object_page_remove_write(p, flags, &clearobjflags))
859                         continue;
860
861                 n = vm_object_page_collect_flush(object, p, pagerflags,
862                     flags, &clearobjflags, &eio);
863                 if (eio) {
864                         res = FALSE;
865                         clearobjflags = FALSE;
866                 }
867                 if (object->generation != curgeneration) {
868                         if ((flags & OBJPC_SYNC) != 0)
869                                 goto rescan;
870                         else
871                                 clearobjflags = FALSE;
872                 }
873
874                 /*
875                  * If the VOP_PUTPAGES() did a truncated write, so
876                  * that even the first page of the run is not fully
877                  * written, vm_pageout_flush() returns 0 as the run
878                  * length.  Since the condition that caused truncated
879                  * write may be permanent, e.g. exhausted free space,
880                  * accepting n == 0 would cause an infinite loop.
881                  *
882                  * Forwarding the iterator leaves the unwritten page
883                  * behind, but there is not much we can do there if
884                  * filesystem refuses to write it.
885                  */
886                 if (n == 0) {
887                         n = 1;
888                         clearobjflags = FALSE;
889                 }
890                 np = vm_page_find_least(object, pi + n);
891         }
892 #if 0
893         VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
894 #endif
895
896         if (clearobjflags)
897                 vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
898         return (res);
899 }
900
901 static int
902 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags,
903     int flags, boolean_t *clearobjflags, boolean_t *eio)
904 {
905         vm_page_t ma[vm_pageout_page_count], p_first, tp;
906         int count, i, mreq, runlen;
907
908         vm_page_lock_assert(p, MA_NOTOWNED);
909         VM_OBJECT_ASSERT_WLOCKED(object);
910
911         count = 1;
912         mreq = 0;
913
914         for (tp = p; count < vm_pageout_page_count; count++) {
915                 tp = vm_page_next(tp);
916                 if (tp == NULL || vm_page_busied(tp))
917                         break;
918                 if (!vm_object_page_remove_write(tp, flags, clearobjflags))
919                         break;
920         }
921
922         for (p_first = p; count < vm_pageout_page_count; count++) {
923                 tp = vm_page_prev(p_first);
924                 if (tp == NULL || vm_page_busied(tp))
925                         break;
926                 if (!vm_object_page_remove_write(tp, flags, clearobjflags))
927                         break;
928                 p_first = tp;
929                 mreq++;
930         }
931
932         for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++)
933                 ma[i] = tp;
934
935         vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio);
936         return (runlen);
937 }
938
939 /*
940  * Note that there is absolutely no sense in writing out
941  * anonymous objects, so we track down the vnode object
942  * to write out.
943  * We invalidate (remove) all pages from the address space
944  * for semantic correctness.
945  *
946  * If the backing object is a device object with unmanaged pages, then any
947  * mappings to the specified range of pages must be removed before this
948  * function is called.
949  *
950  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
951  * may start out with a NULL object.
952  */
953 boolean_t
954 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
955     boolean_t syncio, boolean_t invalidate)
956 {
957         vm_object_t backing_object;
958         struct vnode *vp;
959         struct mount *mp;
960         int error, flags, fsync_after;
961         boolean_t res;
962
963         if (object == NULL)
964                 return (TRUE);
965         res = TRUE;
966         error = 0;
967         VM_OBJECT_WLOCK(object);
968         while ((backing_object = object->backing_object) != NULL) {
969                 VM_OBJECT_WLOCK(backing_object);
970                 offset += object->backing_object_offset;
971                 VM_OBJECT_WUNLOCK(object);
972                 object = backing_object;
973                 if (object->size < OFF_TO_IDX(offset + size))
974                         size = IDX_TO_OFF(object->size) - offset;
975         }
976         /*
977          * Flush pages if writing is allowed, invalidate them
978          * if invalidation requested.  Pages undergoing I/O
979          * will be ignored by vm_object_page_remove().
980          *
981          * We cannot lock the vnode and then wait for paging
982          * to complete without deadlocking against vm_fault.
983          * Instead we simply call vm_object_page_remove() and
984          * allow it to block internally on a page-by-page
985          * basis when it encounters pages undergoing async
986          * I/O.
987          */
988         if (object->type == OBJT_VNODE &&
989             (object->flags & OBJ_MIGHTBEDIRTY) != 0 &&
990             ((vp = object->handle)->v_vflag & VV_NOSYNC) == 0) {
991                 VM_OBJECT_WUNLOCK(object);
992                 (void) vn_start_write(vp, &mp, V_WAIT);
993                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
994                 if (syncio && !invalidate && offset == 0 &&
995                     atop(size) == object->size) {
996                         /*
997                          * If syncing the whole mapping of the file,
998                          * it is faster to schedule all the writes in
999                          * async mode, also allowing the clustering,
1000                          * and then wait for i/o to complete.
1001                          */
1002                         flags = 0;
1003                         fsync_after = TRUE;
1004                 } else {
1005                         flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1006                         flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0;
1007                         fsync_after = FALSE;
1008                 }
1009                 VM_OBJECT_WLOCK(object);
1010                 res = vm_object_page_clean(object, offset, offset + size,
1011                     flags);
1012                 VM_OBJECT_WUNLOCK(object);
1013                 if (fsync_after)
1014                         error = VOP_FSYNC(vp, MNT_WAIT, curthread);
1015                 VOP_UNLOCK(vp, 0);
1016                 vn_finished_write(mp);
1017                 if (error != 0)
1018                         res = FALSE;
1019                 VM_OBJECT_WLOCK(object);
1020         }
1021         if ((object->type == OBJT_VNODE ||
1022              object->type == OBJT_DEVICE) && invalidate) {
1023                 if (object->type == OBJT_DEVICE)
1024                         /*
1025                          * The option OBJPR_NOTMAPPED must be passed here
1026                          * because vm_object_page_remove() cannot remove
1027                          * unmanaged mappings.
1028                          */
1029                         flags = OBJPR_NOTMAPPED;
1030                 else if (old_msync)
1031                         flags = 0;
1032                 else
1033                         flags = OBJPR_CLEANONLY;
1034                 vm_object_page_remove(object, OFF_TO_IDX(offset),
1035                     OFF_TO_IDX(offset + size + PAGE_MASK), flags);
1036         }
1037         VM_OBJECT_WUNLOCK(object);
1038         return (res);
1039 }
1040
1041 /*
1042  * Determine whether the given advice can be applied to the object.  Advice is
1043  * not applied to unmanaged pages since they never belong to page queues, and
1044  * since MADV_FREE is destructive, it can apply only to anonymous pages that
1045  * have been mapped at most once.
1046  */
1047 static bool
1048 vm_object_advice_applies(vm_object_t object, int advice)
1049 {
1050
1051         if ((object->flags & OBJ_UNMANAGED) != 0)
1052                 return (false);
1053         if (advice != MADV_FREE)
1054                 return (true);
1055         return ((object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) &&
1056             (object->flags & OBJ_ONEMAPPING) != 0);
1057 }
1058
1059 static void
1060 vm_object_madvise_freespace(vm_object_t object, int advice, vm_pindex_t pindex,
1061     vm_size_t size)
1062 {
1063
1064         if (advice == MADV_FREE && object->type == OBJT_SWAP)
1065                 swap_pager_freespace(object, pindex, size);
1066 }
1067
1068 /*
1069  *      vm_object_madvise:
1070  *
1071  *      Implements the madvise function at the object/page level.
1072  *
1073  *      MADV_WILLNEED   (any object)
1074  *
1075  *          Activate the specified pages if they are resident.
1076  *
1077  *      MADV_DONTNEED   (any object)
1078  *
1079  *          Deactivate the specified pages if they are resident.
1080  *
1081  *      MADV_FREE       (OBJT_DEFAULT/OBJT_SWAP objects,
1082  *                       OBJ_ONEMAPPING only)
1083  *
1084  *          Deactivate and clean the specified pages if they are
1085  *          resident.  This permits the process to reuse the pages
1086  *          without faulting or the kernel to reclaim the pages
1087  *          without I/O.
1088  */
1089 void
1090 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end,
1091     int advice)
1092 {
1093         vm_pindex_t tpindex;
1094         vm_object_t backing_object, tobject;
1095         vm_page_t m, tm;
1096
1097         if (object == NULL)
1098                 return;
1099
1100 relookup:
1101         VM_OBJECT_WLOCK(object);
1102         if (!vm_object_advice_applies(object, advice)) {
1103                 VM_OBJECT_WUNLOCK(object);
1104                 return;
1105         }
1106         for (m = vm_page_find_least(object, pindex); pindex < end; pindex++) {
1107                 tobject = object;
1108
1109                 /*
1110                  * If the next page isn't resident in the top-level object, we
1111                  * need to search the shadow chain.  When applying MADV_FREE, we
1112                  * take care to release any swap space used to store
1113                  * non-resident pages.
1114                  */
1115                 if (m == NULL || pindex < m->pindex) {
1116                         /*
1117                          * Optimize a common case: if the top-level object has
1118                          * no backing object, we can skip over the non-resident
1119                          * range in constant time.
1120                          */
1121                         if (object->backing_object == NULL) {
1122                                 tpindex = (m != NULL && m->pindex < end) ?
1123                                     m->pindex : end;
1124                                 vm_object_madvise_freespace(object, advice,
1125                                     pindex, tpindex - pindex);
1126                                 if ((pindex = tpindex) == end)
1127                                         break;
1128                                 goto next_page;
1129                         }
1130
1131                         tpindex = pindex;
1132                         do {
1133                                 vm_object_madvise_freespace(tobject, advice,
1134                                     tpindex, 1);
1135                                 /*
1136                                  * Prepare to search the next object in the
1137                                  * chain.
1138                                  */
1139                                 backing_object = tobject->backing_object;
1140                                 if (backing_object == NULL)
1141                                         goto next_pindex;
1142                                 VM_OBJECT_WLOCK(backing_object);
1143                                 tpindex +=
1144                                     OFF_TO_IDX(tobject->backing_object_offset);
1145                                 if (tobject != object)
1146                                         VM_OBJECT_WUNLOCK(tobject);
1147                                 tobject = backing_object;
1148                                 if (!vm_object_advice_applies(tobject, advice))
1149                                         goto next_pindex;
1150                         } while ((tm = vm_page_lookup(tobject, tpindex)) ==
1151                             NULL);
1152                 } else {
1153 next_page:
1154                         tm = m;
1155                         m = TAILQ_NEXT(m, listq);
1156                 }
1157
1158                 /*
1159                  * If the page is not in a normal state, skip it.
1160                  */
1161                 if (tm->valid != VM_PAGE_BITS_ALL)
1162                         goto next_pindex;
1163                 vm_page_lock(tm);
1164                 if (vm_page_wired(tm)) {
1165                         vm_page_unlock(tm);
1166                         goto next_pindex;
1167                 }
1168                 KASSERT((tm->flags & PG_FICTITIOUS) == 0,
1169                     ("vm_object_madvise: page %p is fictitious", tm));
1170                 KASSERT((tm->oflags & VPO_UNMANAGED) == 0,
1171                     ("vm_object_madvise: page %p is not managed", tm));
1172                 if (vm_page_busied(tm)) {
1173                         if (object != tobject)
1174                                 VM_OBJECT_WUNLOCK(tobject);
1175                         VM_OBJECT_WUNLOCK(object);
1176                         if (advice == MADV_WILLNEED) {
1177                                 /*
1178                                  * Reference the page before unlocking and
1179                                  * sleeping so that the page daemon is less
1180                                  * likely to reclaim it.
1181                                  */
1182                                 vm_page_aflag_set(tm, PGA_REFERENCED);
1183                         }
1184                         vm_page_busy_sleep(tm, "madvpo", false);
1185                         goto relookup;
1186                 }
1187                 vm_page_advise(tm, advice);
1188                 vm_page_unlock(tm);
1189                 vm_object_madvise_freespace(tobject, advice, tm->pindex, 1);
1190 next_pindex:
1191                 if (tobject != object)
1192                         VM_OBJECT_WUNLOCK(tobject);
1193         }
1194         VM_OBJECT_WUNLOCK(object);
1195 }
1196
1197 /*
1198  *      vm_object_shadow:
1199  *
1200  *      Create a new object which is backed by the
1201  *      specified existing object range.  The source
1202  *      object reference is deallocated.
1203  *
1204  *      The new object and offset into that object
1205  *      are returned in the source parameters.
1206  */
1207 void
1208 vm_object_shadow(
1209         vm_object_t *object,    /* IN/OUT */
1210         vm_ooffset_t *offset,   /* IN/OUT */
1211         vm_size_t length)
1212 {
1213         vm_object_t source;
1214         vm_object_t result;
1215
1216         source = *object;
1217
1218         /*
1219          * Don't create the new object if the old object isn't shared.
1220          */
1221         if (source != NULL) {
1222                 VM_OBJECT_WLOCK(source);
1223                 if (source->ref_count == 1 &&
1224                     source->handle == NULL &&
1225                     (source->type == OBJT_DEFAULT ||
1226                      source->type == OBJT_SWAP)) {
1227                         VM_OBJECT_WUNLOCK(source);
1228                         return;
1229                 }
1230                 VM_OBJECT_WUNLOCK(source);
1231         }
1232
1233         /*
1234          * Allocate a new object with the given length.
1235          */
1236         result = vm_object_allocate(OBJT_DEFAULT, atop(length));
1237
1238         /*
1239          * The new object shadows the source object, adding a reference to it.
1240          * Our caller changes his reference to point to the new object,
1241          * removing a reference to the source object.  Net result: no change
1242          * of reference count.
1243          *
1244          * Try to optimize the result object's page color when shadowing
1245          * in order to maintain page coloring consistency in the combined 
1246          * shadowed object.
1247          */
1248         result->backing_object = source;
1249         /*
1250          * Store the offset into the source object, and fix up the offset into
1251          * the new object.
1252          */
1253         result->backing_object_offset = *offset;
1254         if (source != NULL) {
1255                 VM_OBJECT_WLOCK(source);
1256                 result->domain = source->domain;
1257                 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1258                 source->shadow_count++;
1259 #if VM_NRESERVLEVEL > 0
1260                 result->flags |= source->flags & OBJ_COLORED;
1261                 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1262                     ((1 << (VM_NFREEORDER - 1)) - 1);
1263 #endif
1264                 VM_OBJECT_WUNLOCK(source);
1265         }
1266
1267
1268         /*
1269          * Return the new things
1270          */
1271         *offset = 0;
1272         *object = result;
1273 }
1274
1275 /*
1276  *      vm_object_split:
1277  *
1278  * Split the pages in a map entry into a new object.  This affords
1279  * easier removal of unused pages, and keeps object inheritance from
1280  * being a negative impact on memory usage.
1281  */
1282 void
1283 vm_object_split(vm_map_entry_t entry)
1284 {
1285         vm_page_t m, m_next;
1286         vm_object_t orig_object, new_object, source;
1287         vm_pindex_t idx, offidxstart;
1288         vm_size_t size;
1289
1290         orig_object = entry->object.vm_object;
1291         if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1292                 return;
1293         if (orig_object->ref_count <= 1)
1294                 return;
1295         VM_OBJECT_WUNLOCK(orig_object);
1296
1297         offidxstart = OFF_TO_IDX(entry->offset);
1298         size = atop(entry->end - entry->start);
1299
1300         /*
1301          * If swap_pager_copy() is later called, it will convert new_object
1302          * into a swap object.
1303          */
1304         new_object = vm_object_allocate(OBJT_DEFAULT, size);
1305
1306         /*
1307          * At this point, the new object is still private, so the order in
1308          * which the original and new objects are locked does not matter.
1309          */
1310         VM_OBJECT_WLOCK(new_object);
1311         VM_OBJECT_WLOCK(orig_object);
1312         new_object->domain = orig_object->domain;
1313         source = orig_object->backing_object;
1314         if (source != NULL) {
1315                 VM_OBJECT_WLOCK(source);
1316                 if ((source->flags & OBJ_DEAD) != 0) {
1317                         VM_OBJECT_WUNLOCK(source);
1318                         VM_OBJECT_WUNLOCK(orig_object);
1319                         VM_OBJECT_WUNLOCK(new_object);
1320                         vm_object_deallocate(new_object);
1321                         VM_OBJECT_WLOCK(orig_object);
1322                         return;
1323                 }
1324                 LIST_INSERT_HEAD(&source->shadow_head,
1325                                   new_object, shadow_list);
1326                 source->shadow_count++;
1327                 vm_object_reference_locked(source);     /* for new_object */
1328                 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1329                 VM_OBJECT_WUNLOCK(source);
1330                 new_object->backing_object_offset = 
1331                         orig_object->backing_object_offset + entry->offset;
1332                 new_object->backing_object = source;
1333         }
1334         if (orig_object->cred != NULL) {
1335                 new_object->cred = orig_object->cred;
1336                 crhold(orig_object->cred);
1337                 new_object->charge = ptoa(size);
1338                 KASSERT(orig_object->charge >= ptoa(size),
1339                     ("orig_object->charge < 0"));
1340                 orig_object->charge -= ptoa(size);
1341         }
1342 retry:
1343         m = vm_page_find_least(orig_object, offidxstart);
1344         for (; m != NULL && (idx = m->pindex - offidxstart) < size;
1345             m = m_next) {
1346                 m_next = TAILQ_NEXT(m, listq);
1347
1348                 /*
1349                  * We must wait for pending I/O to complete before we can
1350                  * rename the page.
1351                  *
1352                  * We do not have to VM_PROT_NONE the page as mappings should
1353                  * not be changed by this operation.
1354                  */
1355                 if (vm_page_busied(m)) {
1356                         VM_OBJECT_WUNLOCK(new_object);
1357                         vm_page_lock(m);
1358                         VM_OBJECT_WUNLOCK(orig_object);
1359                         vm_page_busy_sleep(m, "spltwt", false);
1360                         VM_OBJECT_WLOCK(orig_object);
1361                         VM_OBJECT_WLOCK(new_object);
1362                         goto retry;
1363                 }
1364
1365                 /* vm_page_rename() will dirty the page. */
1366                 if (vm_page_rename(m, new_object, idx)) {
1367                         VM_OBJECT_WUNLOCK(new_object);
1368                         VM_OBJECT_WUNLOCK(orig_object);
1369                         vm_radix_wait();
1370                         VM_OBJECT_WLOCK(orig_object);
1371                         VM_OBJECT_WLOCK(new_object);
1372                         goto retry;
1373                 }
1374 #if VM_NRESERVLEVEL > 0
1375                 /*
1376                  * If some of the reservation's allocated pages remain with
1377                  * the original object, then transferring the reservation to
1378                  * the new object is neither particularly beneficial nor
1379                  * particularly harmful as compared to leaving the reservation
1380                  * with the original object.  If, however, all of the
1381                  * reservation's allocated pages are transferred to the new
1382                  * object, then transferring the reservation is typically
1383                  * beneficial.  Determining which of these two cases applies
1384                  * would be more costly than unconditionally renaming the
1385                  * reservation.
1386                  */
1387                 vm_reserv_rename(m, new_object, orig_object, offidxstart);
1388 #endif
1389                 if (orig_object->type == OBJT_SWAP)
1390                         vm_page_xbusy(m);
1391         }
1392         if (orig_object->type == OBJT_SWAP) {
1393                 /*
1394                  * swap_pager_copy() can sleep, in which case the orig_object's
1395                  * and new_object's locks are released and reacquired. 
1396                  */
1397                 swap_pager_copy(orig_object, new_object, offidxstart, 0);
1398                 TAILQ_FOREACH(m, &new_object->memq, listq)
1399                         vm_page_xunbusy(m);
1400         }
1401         VM_OBJECT_WUNLOCK(orig_object);
1402         VM_OBJECT_WUNLOCK(new_object);
1403         entry->object.vm_object = new_object;
1404         entry->offset = 0LL;
1405         vm_object_deallocate(orig_object);
1406         VM_OBJECT_WLOCK(new_object);
1407 }
1408
1409 #define OBSC_COLLAPSE_NOWAIT    0x0002
1410 #define OBSC_COLLAPSE_WAIT      0x0004
1411
1412 static vm_page_t
1413 vm_object_collapse_scan_wait(vm_object_t object, vm_page_t p, vm_page_t next,
1414     int op)
1415 {
1416         vm_object_t backing_object;
1417
1418         VM_OBJECT_ASSERT_WLOCKED(object);
1419         backing_object = object->backing_object;
1420         VM_OBJECT_ASSERT_WLOCKED(backing_object);
1421
1422         KASSERT(p == NULL || vm_page_busied(p), ("unbusy page %p", p));
1423         KASSERT(p == NULL || p->object == object || p->object == backing_object,
1424             ("invalid ownership %p %p %p", p, object, backing_object));
1425         if ((op & OBSC_COLLAPSE_NOWAIT) != 0)
1426                 return (next);
1427         if (p != NULL)
1428                 vm_page_lock(p);
1429         VM_OBJECT_WUNLOCK(object);
1430         VM_OBJECT_WUNLOCK(backing_object);
1431         /* The page is only NULL when rename fails. */
1432         if (p == NULL)
1433                 vm_radix_wait();
1434         else
1435                 vm_page_busy_sleep(p, "vmocol", false);
1436         VM_OBJECT_WLOCK(object);
1437         VM_OBJECT_WLOCK(backing_object);
1438         return (TAILQ_FIRST(&backing_object->memq));
1439 }
1440
1441 static bool
1442 vm_object_scan_all_shadowed(vm_object_t object)
1443 {
1444         vm_object_t backing_object;
1445         vm_page_t p, pp;
1446         vm_pindex_t backing_offset_index, new_pindex, pi, ps;
1447
1448         VM_OBJECT_ASSERT_WLOCKED(object);
1449         VM_OBJECT_ASSERT_WLOCKED(object->backing_object);
1450
1451         backing_object = object->backing_object;
1452
1453         if (backing_object->type != OBJT_DEFAULT &&
1454             backing_object->type != OBJT_SWAP)
1455                 return (false);
1456
1457         pi = backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1458         p = vm_page_find_least(backing_object, pi);
1459         ps = swap_pager_find_least(backing_object, pi);
1460
1461         /*
1462          * Only check pages inside the parent object's range and
1463          * inside the parent object's mapping of the backing object.
1464          */
1465         for (;; pi++) {
1466                 if (p != NULL && p->pindex < pi)
1467                         p = TAILQ_NEXT(p, listq);
1468                 if (ps < pi)
1469                         ps = swap_pager_find_least(backing_object, pi);
1470                 if (p == NULL && ps >= backing_object->size)
1471                         break;
1472                 else if (p == NULL)
1473                         pi = ps;
1474                 else
1475                         pi = MIN(p->pindex, ps);
1476
1477                 new_pindex = pi - backing_offset_index;
1478                 if (new_pindex >= object->size)
1479                         break;
1480
1481                 /*
1482                  * See if the parent has the page or if the parent's object
1483                  * pager has the page.  If the parent has the page but the page
1484                  * is not valid, the parent's object pager must have the page.
1485                  *
1486                  * If this fails, the parent does not completely shadow the
1487                  * object and we might as well give up now.
1488                  */
1489                 pp = vm_page_lookup(object, new_pindex);
1490                 if ((pp == NULL || pp->valid == 0) &&
1491                     !vm_pager_has_page(object, new_pindex, NULL, NULL))
1492                         return (false);
1493         }
1494         return (true);
1495 }
1496
1497 static bool
1498 vm_object_collapse_scan(vm_object_t object, int op)
1499 {
1500         vm_object_t backing_object;
1501         vm_page_t next, p, pp;
1502         vm_pindex_t backing_offset_index, new_pindex;
1503
1504         VM_OBJECT_ASSERT_WLOCKED(object);
1505         VM_OBJECT_ASSERT_WLOCKED(object->backing_object);
1506
1507         backing_object = object->backing_object;
1508         backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1509
1510         /*
1511          * Initial conditions
1512          */
1513         if ((op & OBSC_COLLAPSE_WAIT) != 0)
1514                 vm_object_set_flag(backing_object, OBJ_DEAD);
1515
1516         /*
1517          * Our scan
1518          */
1519         for (p = TAILQ_FIRST(&backing_object->memq); p != NULL; p = next) {
1520                 next = TAILQ_NEXT(p, listq);
1521                 new_pindex = p->pindex - backing_offset_index;
1522
1523                 /*
1524                  * Check for busy page
1525                  */
1526                 if (vm_page_busied(p)) {
1527                         next = vm_object_collapse_scan_wait(object, p, next, op);
1528                         continue;
1529                 }
1530
1531                 KASSERT(p->object == backing_object,
1532                     ("vm_object_collapse_scan: object mismatch"));
1533
1534                 if (p->pindex < backing_offset_index ||
1535                     new_pindex >= object->size) {
1536                         if (backing_object->type == OBJT_SWAP)
1537                                 swap_pager_freespace(backing_object, p->pindex,
1538                                     1);
1539
1540                         /*
1541                          * Page is out of the parent object's range, we can
1542                          * simply destroy it.
1543                          */
1544                         vm_page_lock(p);
1545                         KASSERT(!pmap_page_is_mapped(p),
1546                             ("freeing mapped page %p", p));
1547                         if (vm_page_remove(p))
1548                                 vm_page_free(p);
1549                         vm_page_unlock(p);
1550                         continue;
1551                 }
1552
1553                 pp = vm_page_lookup(object, new_pindex);
1554                 if (pp != NULL && vm_page_busied(pp)) {
1555                         /*
1556                          * The page in the parent is busy and possibly not
1557                          * (yet) valid.  Until its state is finalized by the
1558                          * busy bit owner, we can't tell whether it shadows the
1559                          * original page.  Therefore, we must either skip it
1560                          * and the original (backing_object) page or wait for
1561                          * its state to be finalized.
1562                          *
1563                          * This is due to a race with vm_fault() where we must
1564                          * unbusy the original (backing_obj) page before we can
1565                          * (re)lock the parent.  Hence we can get here.
1566                          */
1567                         next = vm_object_collapse_scan_wait(object, pp, next,
1568                             op);
1569                         continue;
1570                 }
1571
1572                 KASSERT(pp == NULL || pp->valid != 0,
1573                     ("unbusy invalid page %p", pp));
1574
1575                 if (pp != NULL || vm_pager_has_page(object, new_pindex, NULL,
1576                         NULL)) {
1577                         /*
1578                          * The page already exists in the parent OR swap exists
1579                          * for this location in the parent.  Leave the parent's
1580                          * page alone.  Destroy the original page from the
1581                          * backing object.
1582                          */
1583                         if (backing_object->type == OBJT_SWAP)
1584                                 swap_pager_freespace(backing_object, p->pindex,
1585                                     1);
1586                         vm_page_lock(p);
1587                         KASSERT(!pmap_page_is_mapped(p),
1588                             ("freeing mapped page %p", p));
1589                         if (vm_page_remove(p))
1590                                 vm_page_free(p);
1591                         vm_page_unlock(p);
1592                         continue;
1593                 }
1594
1595                 /*
1596                  * Page does not exist in parent, rename the page from the
1597                  * backing object to the main object.
1598                  *
1599                  * If the page was mapped to a process, it can remain mapped
1600                  * through the rename.  vm_page_rename() will dirty the page.
1601                  */
1602                 if (vm_page_rename(p, object, new_pindex)) {
1603                         next = vm_object_collapse_scan_wait(object, NULL, next,
1604                             op);
1605                         continue;
1606                 }
1607
1608                 /* Use the old pindex to free the right page. */
1609                 if (backing_object->type == OBJT_SWAP)
1610                         swap_pager_freespace(backing_object,
1611                             new_pindex + backing_offset_index, 1);
1612
1613 #if VM_NRESERVLEVEL > 0
1614                 /*
1615                  * Rename the reservation.
1616                  */
1617                 vm_reserv_rename(p, object, backing_object,
1618                     backing_offset_index);
1619 #endif
1620         }
1621         return (true);
1622 }
1623
1624
1625 /*
1626  * this version of collapse allows the operation to occur earlier and
1627  * when paging_in_progress is true for an object...  This is not a complete
1628  * operation, but should plug 99.9% of the rest of the leaks.
1629  */
1630 static void
1631 vm_object_qcollapse(vm_object_t object)
1632 {
1633         vm_object_t backing_object = object->backing_object;
1634
1635         VM_OBJECT_ASSERT_WLOCKED(object);
1636         VM_OBJECT_ASSERT_WLOCKED(backing_object);
1637
1638         if (backing_object->ref_count != 1)
1639                 return;
1640
1641         vm_object_collapse_scan(object, OBSC_COLLAPSE_NOWAIT);
1642 }
1643
1644 /*
1645  *      vm_object_collapse:
1646  *
1647  *      Collapse an object with the object backing it.
1648  *      Pages in the backing object are moved into the
1649  *      parent, and the backing object is deallocated.
1650  */
1651 void
1652 vm_object_collapse(vm_object_t object)
1653 {
1654         vm_object_t backing_object, new_backing_object;
1655
1656         VM_OBJECT_ASSERT_WLOCKED(object);
1657
1658         while (TRUE) {
1659                 /*
1660                  * Verify that the conditions are right for collapse:
1661                  *
1662                  * The object exists and the backing object exists.
1663                  */
1664                 if ((backing_object = object->backing_object) == NULL)
1665                         break;
1666
1667                 /*
1668                  * we check the backing object first, because it is most likely
1669                  * not collapsable.
1670                  */
1671                 VM_OBJECT_WLOCK(backing_object);
1672                 if (backing_object->handle != NULL ||
1673                     (backing_object->type != OBJT_DEFAULT &&
1674                     backing_object->type != OBJT_SWAP) ||
1675                     (backing_object->flags & (OBJ_DEAD | OBJ_NOSPLIT)) != 0 ||
1676                     object->handle != NULL ||
1677                     (object->type != OBJT_DEFAULT &&
1678                      object->type != OBJT_SWAP) ||
1679                     (object->flags & OBJ_DEAD)) {
1680                         VM_OBJECT_WUNLOCK(backing_object);
1681                         break;
1682                 }
1683
1684                 if (object->paging_in_progress != 0 ||
1685                     backing_object->paging_in_progress != 0) {
1686                         vm_object_qcollapse(object);
1687                         VM_OBJECT_WUNLOCK(backing_object);
1688                         break;
1689                 }
1690
1691                 /*
1692                  * We know that we can either collapse the backing object (if
1693                  * the parent is the only reference to it) or (perhaps) have
1694                  * the parent bypass the object if the parent happens to shadow
1695                  * all the resident pages in the entire backing object.
1696                  *
1697                  * This is ignoring pager-backed pages such as swap pages.
1698                  * vm_object_collapse_scan fails the shadowing test in this
1699                  * case.
1700                  */
1701                 if (backing_object->ref_count == 1) {
1702                         vm_object_pip_add(object, 1);
1703                         vm_object_pip_add(backing_object, 1);
1704
1705                         /*
1706                          * If there is exactly one reference to the backing
1707                          * object, we can collapse it into the parent.
1708                          */
1709                         vm_object_collapse_scan(object, OBSC_COLLAPSE_WAIT);
1710
1711 #if VM_NRESERVLEVEL > 0
1712                         /*
1713                          * Break any reservations from backing_object.
1714                          */
1715                         if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1716                                 vm_reserv_break_all(backing_object);
1717 #endif
1718
1719                         /*
1720                          * Move the pager from backing_object to object.
1721                          */
1722                         if (backing_object->type == OBJT_SWAP) {
1723                                 /*
1724                                  * swap_pager_copy() can sleep, in which case
1725                                  * the backing_object's and object's locks are
1726                                  * released and reacquired.
1727                                  * Since swap_pager_copy() is being asked to
1728                                  * destroy the source, it will change the
1729                                  * backing_object's type to OBJT_DEFAULT.
1730                                  */
1731                                 swap_pager_copy(
1732                                     backing_object,
1733                                     object,
1734                                     OFF_TO_IDX(object->backing_object_offset), TRUE);
1735                         }
1736                         /*
1737                          * Object now shadows whatever backing_object did.
1738                          * Note that the reference to 
1739                          * backing_object->backing_object moves from within 
1740                          * backing_object to within object.
1741                          */
1742                         LIST_REMOVE(object, shadow_list);
1743                         backing_object->shadow_count--;
1744                         if (backing_object->backing_object) {
1745                                 VM_OBJECT_WLOCK(backing_object->backing_object);
1746                                 LIST_REMOVE(backing_object, shadow_list);
1747                                 LIST_INSERT_HEAD(
1748                                     &backing_object->backing_object->shadow_head,
1749                                     object, shadow_list);
1750                                 /*
1751                                  * The shadow_count has not changed.
1752                                  */
1753                                 VM_OBJECT_WUNLOCK(backing_object->backing_object);
1754                         }
1755                         object->backing_object = backing_object->backing_object;
1756                         object->backing_object_offset +=
1757                             backing_object->backing_object_offset;
1758
1759                         /*
1760                          * Discard backing_object.
1761                          *
1762                          * Since the backing object has no pages, no pager left,
1763                          * and no object references within it, all that is
1764                          * necessary is to dispose of it.
1765                          */
1766                         KASSERT(backing_object->ref_count == 1, (
1767 "backing_object %p was somehow re-referenced during collapse!",
1768                             backing_object));
1769                         vm_object_pip_wakeup(backing_object);
1770                         backing_object->type = OBJT_DEAD;
1771                         backing_object->ref_count = 0;
1772                         VM_OBJECT_WUNLOCK(backing_object);
1773                         vm_object_destroy(backing_object);
1774
1775                         vm_object_pip_wakeup(object);
1776                         counter_u64_add(object_collapses, 1);
1777                 } else {
1778                         /*
1779                          * If we do not entirely shadow the backing object,
1780                          * there is nothing we can do so we give up.
1781                          */
1782                         if (object->resident_page_count != object->size &&
1783                             !vm_object_scan_all_shadowed(object)) {
1784                                 VM_OBJECT_WUNLOCK(backing_object);
1785                                 break;
1786                         }
1787
1788                         /*
1789                          * Make the parent shadow the next object in the
1790                          * chain.  Deallocating backing_object will not remove
1791                          * it, since its reference count is at least 2.
1792                          */
1793                         LIST_REMOVE(object, shadow_list);
1794                         backing_object->shadow_count--;
1795
1796                         new_backing_object = backing_object->backing_object;
1797                         if ((object->backing_object = new_backing_object) != NULL) {
1798                                 VM_OBJECT_WLOCK(new_backing_object);
1799                                 LIST_INSERT_HEAD(
1800                                     &new_backing_object->shadow_head,
1801                                     object,
1802                                     shadow_list
1803                                 );
1804                                 new_backing_object->shadow_count++;
1805                                 vm_object_reference_locked(new_backing_object);
1806                                 VM_OBJECT_WUNLOCK(new_backing_object);
1807                                 object->backing_object_offset +=
1808                                         backing_object->backing_object_offset;
1809                         }
1810
1811                         /*
1812                          * Drop the reference count on backing_object. Since
1813                          * its ref_count was at least 2, it will not vanish.
1814                          */
1815                         backing_object->ref_count--;
1816                         VM_OBJECT_WUNLOCK(backing_object);
1817                         counter_u64_add(object_bypasses, 1);
1818                 }
1819
1820                 /*
1821                  * Try again with this object's new backing object.
1822                  */
1823         }
1824 }
1825
1826 /*
1827  *      vm_object_page_remove:
1828  *
1829  *      For the given object, either frees or invalidates each of the
1830  *      specified pages.  In general, a page is freed.  However, if a page is
1831  *      wired for any reason other than the existence of a managed, wired
1832  *      mapping, then it may be invalidated but not removed from the object.
1833  *      Pages are specified by the given range ["start", "end") and the option
1834  *      OBJPR_CLEANONLY.  As a special case, if "end" is zero, then the range
1835  *      extends from "start" to the end of the object.  If the option
1836  *      OBJPR_CLEANONLY is specified, then only the non-dirty pages within the
1837  *      specified range are affected.  If the option OBJPR_NOTMAPPED is
1838  *      specified, then the pages within the specified range must have no
1839  *      mappings.  Otherwise, if this option is not specified, any mappings to
1840  *      the specified pages are removed before the pages are freed or
1841  *      invalidated.
1842  *
1843  *      In general, this operation should only be performed on objects that
1844  *      contain managed pages.  There are, however, two exceptions.  First, it
1845  *      is performed on the kernel and kmem objects by vm_map_entry_delete().
1846  *      Second, it is used by msync(..., MS_INVALIDATE) to invalidate device-
1847  *      backed pages.  In both of these cases, the option OBJPR_CLEANONLY must
1848  *      not be specified and the option OBJPR_NOTMAPPED must be specified.
1849  *
1850  *      The object must be locked.
1851  */
1852 void
1853 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1854     int options)
1855 {
1856         vm_page_t p, next;
1857         struct mtx *mtx;
1858
1859         VM_OBJECT_ASSERT_WLOCKED(object);
1860         KASSERT((object->flags & OBJ_UNMANAGED) == 0 ||
1861             (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED,
1862             ("vm_object_page_remove: illegal options for object %p", object));
1863         if (object->resident_page_count == 0)
1864                 return;
1865         vm_object_pip_add(object, 1);
1866 again:
1867         p = vm_page_find_least(object, start);
1868         mtx = NULL;
1869
1870         /*
1871          * Here, the variable "p" is either (1) the page with the least pindex
1872          * greater than or equal to the parameter "start" or (2) NULL. 
1873          */
1874         for (; p != NULL && (p->pindex < end || end == 0); p = next) {
1875                 next = TAILQ_NEXT(p, listq);
1876
1877                 /*
1878                  * If the page is wired for any reason besides the existence
1879                  * of managed, wired mappings, then it cannot be freed.  For
1880                  * example, fictitious pages, which represent device memory,
1881                  * are inherently wired and cannot be freed.  They can,
1882                  * however, be invalidated if the option OBJPR_CLEANONLY is
1883                  * not specified.
1884                  */
1885                 vm_page_change_lock(p, &mtx);
1886                 if (vm_page_xbusied(p)) {
1887                         VM_OBJECT_WUNLOCK(object);
1888                         vm_page_busy_sleep(p, "vmopax", true);
1889                         VM_OBJECT_WLOCK(object);
1890                         goto again;
1891                 }
1892                 if (vm_page_wired(p)) {
1893                         if ((options & OBJPR_NOTMAPPED) == 0 &&
1894                             object->ref_count != 0)
1895                                 pmap_remove_all(p);
1896                         if ((options & OBJPR_CLEANONLY) == 0) {
1897                                 p->valid = 0;
1898                                 vm_page_undirty(p);
1899                         }
1900                         continue;
1901                 }
1902                 if (vm_page_busied(p)) {
1903                         VM_OBJECT_WUNLOCK(object);
1904                         vm_page_busy_sleep(p, "vmopar", false);
1905                         VM_OBJECT_WLOCK(object);
1906                         goto again;
1907                 }
1908                 KASSERT((p->flags & PG_FICTITIOUS) == 0,
1909                     ("vm_object_page_remove: page %p is fictitious", p));
1910                 if ((options & OBJPR_CLEANONLY) != 0 && p->valid != 0) {
1911                         if ((options & OBJPR_NOTMAPPED) == 0 &&
1912                             object->ref_count != 0)
1913                                 pmap_remove_write(p);
1914                         if (p->dirty != 0)
1915                                 continue;
1916                 }
1917                 if ((options & OBJPR_NOTMAPPED) == 0 && object->ref_count != 0)
1918                         pmap_remove_all(p);
1919                 vm_page_free(p);
1920         }
1921         if (mtx != NULL)
1922                 mtx_unlock(mtx);
1923         vm_object_pip_wakeup(object);
1924 }
1925
1926 /*
1927  *      vm_object_page_noreuse:
1928  *
1929  *      For the given object, attempt to move the specified pages to
1930  *      the head of the inactive queue.  This bypasses regular LRU
1931  *      operation and allows the pages to be reused quickly under memory
1932  *      pressure.  If a page is wired for any reason, then it will not
1933  *      be queued.  Pages are specified by the range ["start", "end").
1934  *      As a special case, if "end" is zero, then the range extends from
1935  *      "start" to the end of the object.
1936  *
1937  *      This operation should only be performed on objects that
1938  *      contain non-fictitious, managed pages.
1939  *
1940  *      The object must be locked.
1941  */
1942 void
1943 vm_object_page_noreuse(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1944 {
1945         struct mtx *mtx;
1946         vm_page_t p, next;
1947
1948         VM_OBJECT_ASSERT_LOCKED(object);
1949         KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0,
1950             ("vm_object_page_noreuse: illegal object %p", object));
1951         if (object->resident_page_count == 0)
1952                 return;
1953         p = vm_page_find_least(object, start);
1954
1955         /*
1956          * Here, the variable "p" is either (1) the page with the least pindex
1957          * greater than or equal to the parameter "start" or (2) NULL. 
1958          */
1959         mtx = NULL;
1960         for (; p != NULL && (p->pindex < end || end == 0); p = next) {
1961                 next = TAILQ_NEXT(p, listq);
1962                 vm_page_change_lock(p, &mtx);
1963                 vm_page_deactivate_noreuse(p);
1964         }
1965         if (mtx != NULL)
1966                 mtx_unlock(mtx);
1967 }
1968
1969 /*
1970  *      Populate the specified range of the object with valid pages.  Returns
1971  *      TRUE if the range is successfully populated and FALSE otherwise.
1972  *
1973  *      Note: This function should be optimized to pass a larger array of
1974  *      pages to vm_pager_get_pages() before it is applied to a non-
1975  *      OBJT_DEVICE object.
1976  *
1977  *      The object must be locked.
1978  */
1979 boolean_t
1980 vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1981 {
1982         vm_page_t m;
1983         vm_pindex_t pindex;
1984         int rv;
1985
1986         VM_OBJECT_ASSERT_WLOCKED(object);
1987         for (pindex = start; pindex < end; pindex++) {
1988                 m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL);
1989                 if (m->valid != VM_PAGE_BITS_ALL) {
1990                         rv = vm_pager_get_pages(object, &m, 1, NULL, NULL);
1991                         if (rv != VM_PAGER_OK) {
1992                                 vm_page_lock(m);
1993                                 vm_page_free(m);
1994                                 vm_page_unlock(m);
1995                                 break;
1996                         }
1997                 }
1998                 /*
1999                  * Keep "m" busy because a subsequent iteration may unlock
2000                  * the object.
2001                  */
2002         }
2003         if (pindex > start) {
2004                 m = vm_page_lookup(object, start);
2005                 while (m != NULL && m->pindex < pindex) {
2006                         vm_page_xunbusy(m);
2007                         m = TAILQ_NEXT(m, listq);
2008                 }
2009         }
2010         return (pindex == end);
2011 }
2012
2013 /*
2014  *      Routine:        vm_object_coalesce
2015  *      Function:       Coalesces two objects backing up adjoining
2016  *                      regions of memory into a single object.
2017  *
2018  *      returns TRUE if objects were combined.
2019  *
2020  *      NOTE:   Only works at the moment if the second object is NULL -
2021  *              if it's not, which object do we lock first?
2022  *
2023  *      Parameters:
2024  *              prev_object     First object to coalesce
2025  *              prev_offset     Offset into prev_object
2026  *              prev_size       Size of reference to prev_object
2027  *              next_size       Size of reference to the second object
2028  *              reserved        Indicator that extension region has
2029  *                              swap accounted for
2030  *
2031  *      Conditions:
2032  *      The object must *not* be locked.
2033  */
2034 boolean_t
2035 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
2036     vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
2037 {
2038         vm_pindex_t next_pindex;
2039
2040         if (prev_object == NULL)
2041                 return (TRUE);
2042         VM_OBJECT_WLOCK(prev_object);
2043         if ((prev_object->type != OBJT_DEFAULT &&
2044             prev_object->type != OBJT_SWAP) ||
2045             (prev_object->flags & OBJ_NOSPLIT) != 0) {
2046                 VM_OBJECT_WUNLOCK(prev_object);
2047                 return (FALSE);
2048         }
2049
2050         /*
2051          * Try to collapse the object first
2052          */
2053         vm_object_collapse(prev_object);
2054
2055         /*
2056          * Can't coalesce if: . more than one reference . paged out . shadows
2057          * another object . has a copy elsewhere (any of which mean that the
2058          * pages not mapped to prev_entry may be in use anyway)
2059          */
2060         if (prev_object->backing_object != NULL) {
2061                 VM_OBJECT_WUNLOCK(prev_object);
2062                 return (FALSE);
2063         }
2064
2065         prev_size >>= PAGE_SHIFT;
2066         next_size >>= PAGE_SHIFT;
2067         next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
2068
2069         if (prev_object->ref_count > 1 &&
2070             prev_object->size != next_pindex &&
2071             (prev_object->flags & OBJ_ONEMAPPING) == 0) {
2072                 VM_OBJECT_WUNLOCK(prev_object);
2073                 return (FALSE);
2074         }
2075
2076         /*
2077          * Account for the charge.
2078          */
2079         if (prev_object->cred != NULL) {
2080
2081                 /*
2082                  * If prev_object was charged, then this mapping,
2083                  * although not charged now, may become writable
2084                  * later. Non-NULL cred in the object would prevent
2085                  * swap reservation during enabling of the write
2086                  * access, so reserve swap now. Failed reservation
2087                  * cause allocation of the separate object for the map
2088                  * entry, and swap reservation for this entry is
2089                  * managed in appropriate time.
2090                  */
2091                 if (!reserved && !swap_reserve_by_cred(ptoa(next_size),
2092                     prev_object->cred)) {
2093                         VM_OBJECT_WUNLOCK(prev_object);
2094                         return (FALSE);
2095                 }
2096                 prev_object->charge += ptoa(next_size);
2097         }
2098
2099         /*
2100          * Remove any pages that may still be in the object from a previous
2101          * deallocation.
2102          */
2103         if (next_pindex < prev_object->size) {
2104                 vm_object_page_remove(prev_object, next_pindex, next_pindex +
2105                     next_size, 0);
2106                 if (prev_object->type == OBJT_SWAP)
2107                         swap_pager_freespace(prev_object,
2108                                              next_pindex, next_size);
2109 #if 0
2110                 if (prev_object->cred != NULL) {
2111                         KASSERT(prev_object->charge >=
2112                             ptoa(prev_object->size - next_pindex),
2113                             ("object %p overcharged 1 %jx %jx", prev_object,
2114                                 (uintmax_t)next_pindex, (uintmax_t)next_size));
2115                         prev_object->charge -= ptoa(prev_object->size -
2116                             next_pindex);
2117                 }
2118 #endif
2119         }
2120
2121         /*
2122          * Extend the object if necessary.
2123          */
2124         if (next_pindex + next_size > prev_object->size)
2125                 prev_object->size = next_pindex + next_size;
2126
2127         VM_OBJECT_WUNLOCK(prev_object);
2128         return (TRUE);
2129 }
2130
2131 void
2132 vm_object_set_writeable_dirty(vm_object_t object)
2133 {
2134
2135         VM_OBJECT_ASSERT_WLOCKED(object);
2136         if (object->type != OBJT_VNODE) {
2137                 if ((object->flags & OBJ_TMPFS_NODE) != 0) {
2138                         KASSERT(object->type == OBJT_SWAP, ("non-swap tmpfs"));
2139                         vm_object_set_flag(object, OBJ_TMPFS_DIRTY);
2140                 }
2141                 return;
2142         }
2143         object->generation++;
2144         if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
2145                 return;
2146         vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
2147 }
2148
2149 /*
2150  *      vm_object_unwire:
2151  *
2152  *      For each page offset within the specified range of the given object,
2153  *      find the highest-level page in the shadow chain and unwire it.  A page
2154  *      must exist at every page offset, and the highest-level page must be
2155  *      wired.
2156  */
2157 void
2158 vm_object_unwire(vm_object_t object, vm_ooffset_t offset, vm_size_t length,
2159     uint8_t queue)
2160 {
2161         vm_object_t tobject, t1object;
2162         vm_page_t m, tm;
2163         vm_pindex_t end_pindex, pindex, tpindex;
2164         int depth, locked_depth;
2165
2166         KASSERT((offset & PAGE_MASK) == 0,
2167             ("vm_object_unwire: offset is not page aligned"));
2168         KASSERT((length & PAGE_MASK) == 0,
2169             ("vm_object_unwire: length is not a multiple of PAGE_SIZE"));
2170         /* The wired count of a fictitious page never changes. */
2171         if ((object->flags & OBJ_FICTITIOUS) != 0)
2172                 return;
2173         pindex = OFF_TO_IDX(offset);
2174         end_pindex = pindex + atop(length);
2175 again:
2176         locked_depth = 1;
2177         VM_OBJECT_RLOCK(object);
2178         m = vm_page_find_least(object, pindex);
2179         while (pindex < end_pindex) {
2180                 if (m == NULL || pindex < m->pindex) {
2181                         /*
2182                          * The first object in the shadow chain doesn't
2183                          * contain a page at the current index.  Therefore,
2184                          * the page must exist in a backing object.
2185                          */
2186                         tobject = object;
2187                         tpindex = pindex;
2188                         depth = 0;
2189                         do {
2190                                 tpindex +=
2191                                     OFF_TO_IDX(tobject->backing_object_offset);
2192                                 tobject = tobject->backing_object;
2193                                 KASSERT(tobject != NULL,
2194                                     ("vm_object_unwire: missing page"));
2195                                 if ((tobject->flags & OBJ_FICTITIOUS) != 0)
2196                                         goto next_page;
2197                                 depth++;
2198                                 if (depth == locked_depth) {
2199                                         locked_depth++;
2200                                         VM_OBJECT_RLOCK(tobject);
2201                                 }
2202                         } while ((tm = vm_page_lookup(tobject, tpindex)) ==
2203                             NULL);
2204                 } else {
2205                         tm = m;
2206                         m = TAILQ_NEXT(m, listq);
2207                 }
2208                 vm_page_lock(tm);
2209                 if (vm_page_xbusied(tm)) {
2210                         for (tobject = object; locked_depth >= 1;
2211                             locked_depth--) {
2212                                 t1object = tobject->backing_object;
2213                                 VM_OBJECT_RUNLOCK(tobject);
2214                                 tobject = t1object;
2215                         }
2216                         vm_page_busy_sleep(tm, "unwbo", true);
2217                         goto again;
2218                 }
2219                 vm_page_unwire(tm, queue);
2220                 vm_page_unlock(tm);
2221 next_page:
2222                 pindex++;
2223         }
2224         /* Release the accumulated object locks. */
2225         for (tobject = object; locked_depth >= 1; locked_depth--) {
2226                 t1object = tobject->backing_object;
2227                 VM_OBJECT_RUNLOCK(tobject);
2228                 tobject = t1object;
2229         }
2230 }
2231
2232 /*
2233  * Return the vnode for the given object, or NULL if none exists.
2234  * For tmpfs objects, the function may return NULL if there is
2235  * no vnode allocated at the time of the call.
2236  */
2237 struct vnode *
2238 vm_object_vnode(vm_object_t object)
2239 {
2240         struct vnode *vp;
2241
2242         VM_OBJECT_ASSERT_LOCKED(object);
2243         if (object->type == OBJT_VNODE) {
2244                 vp = object->handle;
2245                 KASSERT(vp != NULL, ("%s: OBJT_VNODE has no vnode", __func__));
2246         } else if (object->type == OBJT_SWAP &&
2247             (object->flags & OBJ_TMPFS) != 0) {
2248                 vp = object->un_pager.swp.swp_tmpfs;
2249                 KASSERT(vp != NULL, ("%s: OBJT_TMPFS has no vnode", __func__));
2250         } else {
2251                 vp = NULL;
2252         }
2253         return (vp);
2254 }
2255
2256 /*
2257  * Return the kvme type of the given object.
2258  * If vpp is not NULL, set it to the object's vm_object_vnode() or NULL.
2259  */
2260 int
2261 vm_object_kvme_type(vm_object_t object, struct vnode **vpp)
2262 {
2263
2264         VM_OBJECT_ASSERT_LOCKED(object);
2265         if (vpp != NULL)
2266                 *vpp = vm_object_vnode(object);
2267         switch (object->type) {
2268         case OBJT_DEFAULT:
2269                 return (KVME_TYPE_DEFAULT);
2270         case OBJT_VNODE:
2271                 return (KVME_TYPE_VNODE);
2272         case OBJT_SWAP:
2273                 if ((object->flags & OBJ_TMPFS_NODE) != 0)
2274                         return (KVME_TYPE_VNODE);
2275                 return (KVME_TYPE_SWAP);
2276         case OBJT_DEVICE:
2277                 return (KVME_TYPE_DEVICE);
2278         case OBJT_PHYS:
2279                 return (KVME_TYPE_PHYS);
2280         case OBJT_DEAD:
2281                 return (KVME_TYPE_DEAD);
2282         case OBJT_SG:
2283                 return (KVME_TYPE_SG);
2284         case OBJT_MGTDEVICE:
2285                 return (KVME_TYPE_MGTDEVICE);
2286         default:
2287                 return (KVME_TYPE_UNKNOWN);
2288         }
2289 }
2290
2291 static int
2292 sysctl_vm_object_list(SYSCTL_HANDLER_ARGS)
2293 {
2294         struct kinfo_vmobject *kvo;
2295         char *fullpath, *freepath;
2296         struct vnode *vp;
2297         struct vattr va;
2298         vm_object_t obj;
2299         vm_page_t m;
2300         int count, error;
2301
2302         if (req->oldptr == NULL) {
2303                 /*
2304                  * If an old buffer has not been provided, generate an
2305                  * estimate of the space needed for a subsequent call.
2306                  */
2307                 mtx_lock(&vm_object_list_mtx);
2308                 count = 0;
2309                 TAILQ_FOREACH(obj, &vm_object_list, object_list) {
2310                         if (obj->type == OBJT_DEAD)
2311                                 continue;
2312                         count++;
2313                 }
2314                 mtx_unlock(&vm_object_list_mtx);
2315                 return (SYSCTL_OUT(req, NULL, sizeof(struct kinfo_vmobject) *
2316                     count * 11 / 10));
2317         }
2318
2319         kvo = malloc(sizeof(*kvo), M_TEMP, M_WAITOK);
2320         error = 0;
2321
2322         /*
2323          * VM objects are type stable and are never removed from the
2324          * list once added.  This allows us to safely read obj->object_list
2325          * after reacquiring the VM object lock.
2326          */
2327         mtx_lock(&vm_object_list_mtx);
2328         TAILQ_FOREACH(obj, &vm_object_list, object_list) {
2329                 if (obj->type == OBJT_DEAD)
2330                         continue;
2331                 VM_OBJECT_RLOCK(obj);
2332                 if (obj->type == OBJT_DEAD) {
2333                         VM_OBJECT_RUNLOCK(obj);
2334                         continue;
2335                 }
2336                 mtx_unlock(&vm_object_list_mtx);
2337                 kvo->kvo_size = ptoa(obj->size);
2338                 kvo->kvo_resident = obj->resident_page_count;
2339                 kvo->kvo_ref_count = obj->ref_count;
2340                 kvo->kvo_shadow_count = obj->shadow_count;
2341                 kvo->kvo_memattr = obj->memattr;
2342                 kvo->kvo_active = 0;
2343                 kvo->kvo_inactive = 0;
2344                 TAILQ_FOREACH(m, &obj->memq, listq) {
2345                         /*
2346                          * A page may belong to the object but be
2347                          * dequeued and set to PQ_NONE while the
2348                          * object lock is not held.  This makes the
2349                          * reads of m->queue below racy, and we do not
2350                          * count pages set to PQ_NONE.  However, this
2351                          * sysctl is only meant to give an
2352                          * approximation of the system anyway.
2353                          */
2354                         if (m->queue == PQ_ACTIVE)
2355                                 kvo->kvo_active++;
2356                         else if (m->queue == PQ_INACTIVE)
2357                                 kvo->kvo_inactive++;
2358                 }
2359
2360                 kvo->kvo_vn_fileid = 0;
2361                 kvo->kvo_vn_fsid = 0;
2362                 kvo->kvo_vn_fsid_freebsd11 = 0;
2363                 freepath = NULL;
2364                 fullpath = "";
2365                 kvo->kvo_type = vm_object_kvme_type(obj, &vp);
2366                 if (vp != NULL)
2367                         vref(vp);
2368                 VM_OBJECT_RUNLOCK(obj);
2369                 if (vp != NULL) {
2370                         vn_fullpath(curthread, vp, &fullpath, &freepath);
2371                         vn_lock(vp, LK_SHARED | LK_RETRY);
2372                         if (VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) {
2373                                 kvo->kvo_vn_fileid = va.va_fileid;
2374                                 kvo->kvo_vn_fsid = va.va_fsid;
2375                                 kvo->kvo_vn_fsid_freebsd11 = va.va_fsid;
2376                                                                 /* truncate */
2377                         }
2378                         vput(vp);
2379                 }
2380
2381                 strlcpy(kvo->kvo_path, fullpath, sizeof(kvo->kvo_path));
2382                 if (freepath != NULL)
2383                         free(freepath, M_TEMP);
2384
2385                 /* Pack record size down */
2386                 kvo->kvo_structsize = offsetof(struct kinfo_vmobject, kvo_path)
2387                     + strlen(kvo->kvo_path) + 1;
2388                 kvo->kvo_structsize = roundup(kvo->kvo_structsize,
2389                     sizeof(uint64_t));
2390                 error = SYSCTL_OUT(req, kvo, kvo->kvo_structsize);
2391                 mtx_lock(&vm_object_list_mtx);
2392                 if (error)
2393                         break;
2394         }
2395         mtx_unlock(&vm_object_list_mtx);
2396         free(kvo, M_TEMP);
2397         return (error);
2398 }
2399 SYSCTL_PROC(_vm, OID_AUTO, objects, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP |
2400     CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_object_list, "S,kinfo_vmobject",
2401     "List of VM objects");
2402
2403 #include "opt_ddb.h"
2404 #ifdef DDB
2405 #include <sys/kernel.h>
2406
2407 #include <sys/cons.h>
2408
2409 #include <ddb/ddb.h>
2410
2411 static int
2412 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2413 {
2414         vm_map_t tmpm;
2415         vm_map_entry_t tmpe;
2416         vm_object_t obj;
2417         int entcount;
2418
2419         if (map == 0)
2420                 return 0;
2421
2422         if (entry == 0) {
2423                 tmpe = map->header.next;
2424                 entcount = map->nentries;
2425                 while (entcount-- && (tmpe != &map->header)) {
2426                         if (_vm_object_in_map(map, object, tmpe)) {
2427                                 return 1;
2428                         }
2429                         tmpe = tmpe->next;
2430                 }
2431         } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2432                 tmpm = entry->object.sub_map;
2433                 tmpe = tmpm->header.next;
2434                 entcount = tmpm->nentries;
2435                 while (entcount-- && tmpe != &tmpm->header) {
2436                         if (_vm_object_in_map(tmpm, object, tmpe)) {
2437                                 return 1;
2438                         }
2439                         tmpe = tmpe->next;
2440                 }
2441         } else if ((obj = entry->object.vm_object) != NULL) {
2442                 for (; obj; obj = obj->backing_object)
2443                         if (obj == object) {
2444                                 return 1;
2445                         }
2446         }
2447         return 0;
2448 }
2449
2450 static int
2451 vm_object_in_map(vm_object_t object)
2452 {
2453         struct proc *p;
2454
2455         /* sx_slock(&allproc_lock); */
2456         FOREACH_PROC_IN_SYSTEM(p) {
2457                 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2458                         continue;
2459                 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2460                         /* sx_sunlock(&allproc_lock); */
2461                         return 1;
2462                 }
2463         }
2464         /* sx_sunlock(&allproc_lock); */
2465         if (_vm_object_in_map(kernel_map, object, 0))
2466                 return 1;
2467         return 0;
2468 }
2469
2470 DB_SHOW_COMMAND(vmochk, vm_object_check)
2471 {
2472         vm_object_t object;
2473
2474         /*
2475          * make sure that internal objs are in a map somewhere
2476          * and none have zero ref counts.
2477          */
2478         TAILQ_FOREACH(object, &vm_object_list, object_list) {
2479                 if (object->handle == NULL &&
2480                     (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2481                         if (object->ref_count == 0) {
2482                                 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2483                                         (long)object->size);
2484                         }
2485                         if (!vm_object_in_map(object)) {
2486                                 db_printf(
2487                         "vmochk: internal obj is not in a map: "
2488                         "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2489                                     object->ref_count, (u_long)object->size, 
2490                                     (u_long)object->size,
2491                                     (void *)object->backing_object);
2492                         }
2493                 }
2494         }
2495 }
2496
2497 /*
2498  *      vm_object_print:        [ debug ]
2499  */
2500 DB_SHOW_COMMAND(object, vm_object_print_static)
2501 {
2502         /* XXX convert args. */
2503         vm_object_t object = (vm_object_t)addr;
2504         boolean_t full = have_addr;
2505
2506         vm_page_t p;
2507
2508         /* XXX count is an (unused) arg.  Avoid shadowing it. */
2509 #define count   was_count
2510
2511         int count;
2512
2513         if (object == NULL)
2514                 return;
2515
2516         db_iprintf(
2517             "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n",
2518             object, (int)object->type, (uintmax_t)object->size,
2519             object->resident_page_count, object->ref_count, object->flags,
2520             object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge);
2521         db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2522             object->shadow_count, 
2523             object->backing_object ? object->backing_object->ref_count : 0,
2524             object->backing_object, (uintmax_t)object->backing_object_offset);
2525
2526         if (!full)
2527                 return;
2528
2529         db_indent += 2;
2530         count = 0;
2531         TAILQ_FOREACH(p, &object->memq, listq) {
2532                 if (count == 0)
2533                         db_iprintf("memory:=");
2534                 else if (count == 6) {
2535                         db_printf("\n");
2536                         db_iprintf(" ...");
2537                         count = 0;
2538                 } else
2539                         db_printf(",");
2540                 count++;
2541
2542                 db_printf("(off=0x%jx,page=0x%jx)",
2543                     (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2544         }
2545         if (count != 0)
2546                 db_printf("\n");
2547         db_indent -= 2;
2548 }
2549
2550 /* XXX. */
2551 #undef count
2552
2553 /* XXX need this non-static entry for calling from vm_map_print. */
2554 void
2555 vm_object_print(
2556         /* db_expr_t */ long addr,
2557         boolean_t have_addr,
2558         /* db_expr_t */ long count,
2559         char *modif)
2560 {
2561         vm_object_print_static(addr, have_addr, count, modif);
2562 }
2563
2564 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2565 {
2566         vm_object_t object;
2567         vm_pindex_t fidx;
2568         vm_paddr_t pa;
2569         vm_page_t m, prev_m;
2570         int rcount, nl, c;
2571
2572         nl = 0;
2573         TAILQ_FOREACH(object, &vm_object_list, object_list) {
2574                 db_printf("new object: %p\n", (void *)object);
2575                 if (nl > 18) {
2576                         c = cngetc();
2577                         if (c != ' ')
2578                                 return;
2579                         nl = 0;
2580                 }
2581                 nl++;
2582                 rcount = 0;
2583                 fidx = 0;
2584                 pa = -1;
2585                 TAILQ_FOREACH(m, &object->memq, listq) {
2586                         if (m->pindex > 128)
2587                                 break;
2588                         if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2589                             prev_m->pindex + 1 != m->pindex) {
2590                                 if (rcount) {
2591                                         db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2592                                                 (long)fidx, rcount, (long)pa);
2593                                         if (nl > 18) {
2594                                                 c = cngetc();
2595                                                 if (c != ' ')
2596                                                         return;
2597                                                 nl = 0;
2598                                         }
2599                                         nl++;
2600                                         rcount = 0;
2601                                 }
2602                         }                               
2603                         if (rcount &&
2604                                 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2605                                 ++rcount;
2606                                 continue;
2607                         }
2608                         if (rcount) {
2609                                 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2610                                         (long)fidx, rcount, (long)pa);
2611                                 if (nl > 18) {
2612                                         c = cngetc();
2613                                         if (c != ' ')
2614                                                 return;
2615                                         nl = 0;
2616                                 }
2617                                 nl++;
2618                         }
2619                         fidx = m->pindex;
2620                         pa = VM_PAGE_TO_PHYS(m);
2621                         rcount = 1;
2622                 }
2623                 if (rcount) {
2624                         db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2625                                 (long)fidx, rcount, (long)pa);
2626                         if (nl > 18) {
2627                                 c = cngetc();
2628                                 if (c != ' ')
2629                                         return;
2630                                 nl = 0;
2631                         }
2632                         nl++;
2633                 }
2634         }
2635 }
2636 #endif /* DDB */