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