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