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