kernel - Fix IPI signaling issue, add a few assertions
[dragonfly.git] / sys / vm / vm_object.c
CommitLineData
984263bc 1/*
6846fd23
MD
2 * (MPSAFE)
3 *
984263bc
MD
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94
39 *
40 *
41 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42 * All rights reserved.
43 *
44 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
45 *
46 * Permission to use, copy, modify and distribute this software and
47 * its documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
51 *
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55 *
56 * Carnegie Mellon requests users of this software to return to
57 *
58 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
62 *
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
65 *
66 * $FreeBSD: src/sys/vm/vm_object.c,v 1.171.2.8 2003/05/26 19:17:56 alc Exp $
67 */
68
69/*
70 * Virtual memory object module.
71 */
72
73#include <sys/param.h>
74#include <sys/systm.h>
75#include <sys/proc.h> /* for curproc, pageproc */
e32ad78d 76#include <sys/thread.h>
984263bc
MD
77#include <sys/vnode.h>
78#include <sys/vmmeter.h>
79#include <sys/mman.h>
80#include <sys/mount.h>
81#include <sys/kernel.h>
82#include <sys/sysctl.h>
e1c14c82 83#include <sys/refcount.h>
984263bc
MD
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_zone.h>
97
98#define EASY_SCAN_FACTOR 8
99
b12defdc
MD
100static void vm_object_qcollapse(vm_object_t object,
101 vm_object_t backing_object);
1f804340
MD
102static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
103 int pagerflags);
e1c14c82 104static void vm_object_lock_init(vm_object_t);
e1c14c82 105
984263bc
MD
106
107/*
108 * Virtual memory objects maintain the actual data
109 * associated with allocated virtual memory. A given
110 * page of memory exists within exactly one object.
111 *
112 * An object is only deallocated when all "references"
113 * are given up. Only one "reference" to a given
114 * region of an object should be writeable.
115 *
116 * Associated with each object is a list of all resident
117 * memory pages belonging to that object; this list is
118 * maintained by the "vm_page" module, and locked by the object's
119 * lock.
120 *
121 * Each object also records a "pager" routine which is
122 * used to retrieve (and store) pages to the proper backing
123 * storage. In addition, objects may be backed by other
124 * objects from which they were virtual-copied.
125 *
126 * The only items within the object structure which are
127 * modified after time of creation are:
128 * reference count locked by object's lock
129 * pager routine locked by object's lock
130 *
131 */
132
2de4f77e 133struct object_q vm_object_list; /* locked by vmobj_token */
c439ad8f
MD
134struct vm_object kernel_object;
135
2de4f77e 136static long vm_object_count; /* locked by vmobj_token */
984263bc
MD
137extern int vm_pageout_page_count;
138
139static long object_collapses;
140static long object_bypasses;
141static int next_index;
142static vm_zone_t obj_zone;
143static struct vm_zone obj_zone_store;
984263bc
MD
144#define VM_OBJECTS_INIT 256
145static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
146
212f39f5
MD
147/*
148 * Misc low level routines
149 */
150static void
151vm_object_lock_init(vm_object_t obj)
152{
153#if defined(DEBUG_LOCKS)
154 int i;
155
156 obj->debug_hold_bitmap = 0;
157 obj->debug_hold_ovfl = 0;
158 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
159 obj->debug_hold_thrs[i] = NULL;
18a4c8dc
VS
160 obj->debug_hold_file[i] = NULL;
161 obj->debug_hold_line[i] = 0;
212f39f5
MD
162 }
163#endif
164}
165
a31129d8 166void
212f39f5
MD
167vm_object_lock_swap(void)
168{
169 lwkt_token_swap();
170}
171
172void
173vm_object_lock(vm_object_t obj)
174{
175 lwkt_getpooltoken(obj);
176}
177
178void
179vm_object_unlock(vm_object_t obj)
180{
181 lwkt_relpooltoken(obj);
182}
183
184static __inline void
185vm_object_assert_held(vm_object_t obj)
186{
187 ASSERT_LWKT_TOKEN_HELD(lwkt_token_pool_lookup(obj));
188}
189
190void
18a4c8dc 191#ifndef DEBUG_LOCKS
212f39f5 192vm_object_hold(vm_object_t obj)
18a4c8dc
VS
193#else
194debugvm_object_hold(vm_object_t obj, char *file, int line)
195#endif
212f39f5 196{
b12defdc 197 KKASSERT(obj != NULL);
212f39f5
MD
198
199 /*
200 * Object must be held (object allocation is stable due to callers
201 * context, typically already holding the token on a parent object)
202 * prior to potentially blocking on the lock, otherwise the object
203 * can get ripped away from us.
204 */
205 refcount_acquire(&obj->hold_count);
206 vm_object_lock(obj);
207
208#if defined(DEBUG_LOCKS)
209 int i;
210
211 i = ffs(~obj->debug_hold_bitmap) - 1;
212 if (i == -1) {
213 kprintf("vm_object hold count > VMOBJ_DEBUG_ARRAY_SIZE");
214 obj->debug_hold_ovfl = 1;
215 }
216
217 obj->debug_hold_bitmap |= (1 << i);
218 obj->debug_hold_thrs[i] = curthread;
18a4c8dc
VS
219 obj->debug_hold_file[i] = file;
220 obj->debug_hold_line[i] = line;
212f39f5
MD
221#endif
222}
223
b12defdc
MD
224/*
225 * Drop the token and hold_count on the object.
226 */
212f39f5
MD
227void
228vm_object_drop(vm_object_t obj)
229{
230 if (obj == NULL)
231 return;
232
233#if defined(DEBUG_LOCKS)
234 int found = 0;
235 int i;
236
237 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
238 if ((obj->debug_hold_bitmap & (1 << i)) &&
239 (obj->debug_hold_thrs[i] == curthread)) {
240 obj->debug_hold_bitmap &= ~(1 << i);
241 obj->debug_hold_thrs[i] = NULL;
18a4c8dc
VS
242 obj->debug_hold_file[i] = NULL;
243 obj->debug_hold_line[i] = 0;
212f39f5
MD
244 found = 1;
245 break;
246 }
247 }
248
249 if (found == 0 && obj->debug_hold_ovfl == 0)
250 panic("vm_object: attempt to drop hold on non-self-held obj");
251#endif
252
253 /*
85946b6c
MD
254 * The lock is a pool token, no new holders should be possible once
255 * we drop hold_count 1->0 as there is no longer any way to reference
256 * the object.
212f39f5 257 */
c17a6852 258 KKASSERT(obj->hold_count > 0);
b12defdc
MD
259 if (refcount_release(&obj->hold_count)) {
260 if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD))
261 zfree(obj_zone, obj);
212f39f5 262 }
b12defdc 263 vm_object_unlock(obj); /* uses pool token, ok to call on freed obj */
212f39f5
MD
264}
265
6846fd23
MD
266/*
267 * Initialize a freshly allocated object
268 *
269 * Used only by vm_object_allocate() and zinitna().
270 *
271 * No requirements.
272 */
984263bc 273void
aecf2182 274_vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
984263bc
MD
275{
276 int incr;
6846fd23 277
1f804340 278 RB_INIT(&object->rb_memq);
984263bc
MD
279 LIST_INIT(&object->shadow_head);
280
281 object->type = type;
282 object->size = size;
283 object->ref_count = 1;
e1c14c82 284 object->hold_count = 0;
984263bc
MD
285 object->flags = 0;
286 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
287 vm_object_set_flag(object, OBJ_ONEMAPPING);
288 object->paging_in_progress = 0;
289 object->resident_page_count = 0;
50a55c46 290 object->agg_pv_list_count = 0;
984263bc 291 object->shadow_count = 0;
85946b6c
MD
292#ifdef SMP
293 /* cpu localization twist */
294 object->pg_color = (int)(intptr_t)curthread;
295#else
984263bc 296 object->pg_color = next_index;
85946b6c 297#endif
984263bc
MD
298 if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
299 incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
300 else
301 incr = size;
302 next_index = (next_index + incr) & PQ_L2_MASK;
303 object->handle = NULL;
304 object->backing_object = NULL;
b12defdc 305 object->backing_object_offset = (vm_ooffset_t)0;
984263bc
MD
306
307 object->generation++;
96adc753
MD
308 object->swblock_count = 0;
309 RB_INIT(&object->swblock_root);
e1c14c82 310 vm_object_lock_init(object);
984263bc 311
2de4f77e 312 lwkt_gettoken(&vmobj_token);
984263bc
MD
313 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
314 vm_object_count++;
2de4f77e 315 lwkt_reltoken(&vmobj_token);
984263bc
MD
316}
317
318/*
6846fd23 319 * Initialize the VM objects module.
984263bc 320 *
6846fd23 321 * Called from the low level boot code only.
984263bc
MD
322 */
323void
57e43348 324vm_object_init(void)
984263bc
MD
325{
326 TAILQ_INIT(&vm_object_list);
984263bc 327
e4846942 328 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(KvaEnd),
c439ad8f 329 &kernel_object);
984263bc
MD
330
331 obj_zone = &obj_zone_store;
332 zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
333 vm_objects_init, VM_OBJECTS_INIT);
334}
335
336void
57e43348 337vm_object_init2(void)
c97ef899
MD
338{
339 zinitna(obj_zone, NULL, NULL, 0, 0, ZONE_PANICFAIL, 1);
984263bc
MD
340}
341
342/*
6846fd23 343 * Allocate and return a new object of the specified type and size.
984263bc 344 *
6846fd23 345 * No requirements.
984263bc 346 */
984263bc 347vm_object_t
aecf2182 348vm_object_allocate(objtype_t type, vm_pindex_t size)
984263bc
MD
349{
350 vm_object_t result;
351
352 result = (vm_object_t) zalloc(obj_zone);
353
354 _vm_object_allocate(type, size, result);
355
356 return (result);
357}
358
984263bc 359/*
b12defdc
MD
360 * Add an additional reference to a vm_object. The object must already be
361 * held. The original non-lock version is no longer supported. The object
362 * must NOT be chain locked by anyone at the time the reference is added.
363 *
364 * Referencing a chain-locked object can blow up the fairly sensitive
365 * ref_count and shadow_count tests in the deallocator. Most callers
366 * will call vm_object_chain_wait() prior to calling
367 * vm_object_reference_locked() to avoid the case.
984263bc 368 *
b12defdc 369 * The object must be held.
984263bc
MD
370 */
371void
b12defdc 372vm_object_reference_locked(vm_object_t object)
984263bc 373{
b12defdc
MD
374 KKASSERT(object != NULL);
375 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
376 KKASSERT((object->flags & OBJ_CHAINLOCK) == 0);
377 object->ref_count++;
378 if (object->type == OBJT_VNODE) {
379 vref(object->handle);
380 /* XXX what if the vnode is being destroyed? */
381 }
2de4f77e 382}
984263bc 383
b12defdc
MD
384/*
385 * Object OBJ_CHAINLOCK lock handling.
386 *
387 * The caller can chain-lock backing objects recursively and then
388 * use vm_object_chain_release_all() to undo the whole chain.
389 *
390 * Chain locks are used to prevent collapses and are only applicable
391 * to OBJT_DEFAULT and OBJT_SWAP objects. Chain locking operations
392 * on other object types are ignored. This is also important because
393 * it allows e.g. the vnode underlying a memory mapping to take concurrent
394 * faults.
395 *
396 * The object must usually be held on entry, though intermediate
397 * objects need not be held on release.
398 */
2de4f77e 399void
b12defdc 400vm_object_chain_wait(vm_object_t object)
2de4f77e 401{
b12defdc
MD
402 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
403 while (object->flags & OBJ_CHAINLOCK) {
404 vm_object_set_flag(object, OBJ_CHAINWANT);
405 tsleep(object, 0, "objchain", 0);
406 }
407}
408
409void
410vm_object_chain_acquire(vm_object_t object)
411{
412 if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) {
413 vm_object_chain_wait(object);
414 vm_object_set_flag(object, OBJ_CHAINLOCK);
415 }
416}
417
418void
419vm_object_chain_release(vm_object_t object)
420{
421 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
422 if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) {
423 KKASSERT(object->flags & OBJ_CHAINLOCK);
424 if (object->flags & OBJ_CHAINWANT) {
425 vm_object_clear_flag(object,
426 OBJ_CHAINLOCK | OBJ_CHAINWANT);
427 wakeup(object);
428 } else {
429 vm_object_clear_flag(object, OBJ_CHAINLOCK);
2de4f77e 430 }
984263bc
MD
431 }
432}
433
b12defdc 434/*
d2d8515b
MD
435 * This releases the entire chain of objects from first_object to and
436 * including stopobj, flowing through object->backing_object.
437 *
438 * We release stopobj first as an optimization as this object is most
439 * likely to be shared across multiple processes.
b12defdc
MD
440 */
441void
442vm_object_chain_release_all(vm_object_t first_object, vm_object_t stopobj)
443{
444 vm_object_t backing_object;
445 vm_object_t object;
446
447 vm_object_chain_release(stopobj);
448 object = first_object;
449
450 while (object != stopobj) {
451 KKASSERT(object);
452 if (object != first_object)
453 vm_object_hold(object);
454 backing_object = object->backing_object;
455 vm_object_chain_release(object);
456 if (object != first_object)
457 vm_object_drop(object);
458 object = backing_object;
459 }
460}
461
6846fd23
MD
462/*
463 * Dereference an object and its underlying vnode.
464 *
b12defdc 465 * The object must be held and will be held on return.
6846fd23 466 */
b33a2ac2 467static void
57e43348 468vm_object_vndeallocate(vm_object_t object)
984263bc
MD
469{
470 struct vnode *vp = (struct vnode *) object->handle;
471
472 KASSERT(object->type == OBJT_VNODE,
473 ("vm_object_vndeallocate: not a vnode object"));
474 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
b12defdc 475 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
984263bc
MD
476#ifdef INVARIANTS
477 if (object->ref_count == 0) {
478 vprint("vm_object_vndeallocate", vp);
479 panic("vm_object_vndeallocate: bad object reference count");
480 }
481#endif
984263bc 482 object->ref_count--;
b33a2ac2 483 if (object->ref_count == 0)
2247fe02 484 vclrflags(vp, VTEXT);
984263bc
MD
485 vrele(vp);
486}
487
488/*
6846fd23
MD
489 * Release a reference to the specified object, gained either through a
490 * vm_object_allocate or a vm_object_reference call. When all references
491 * are gone, storage associated with this object may be relinquished.
212f39f5
MD
492 *
493 * The caller does not have to hold the object locked but must have control
494 * over the reference in question in order to guarantee that the object
495 * does not get ripped out from under us.
984263bc
MD
496 */
497void
57e43348 498vm_object_deallocate(vm_object_t object)
2de4f77e 499{
b12defdc
MD
500 if (object) {
501 vm_object_hold(object);
502 vm_object_deallocate_locked(object);
503 vm_object_drop(object);
504 }
2de4f77e
MD
505}
506
507void
508vm_object_deallocate_locked(vm_object_t object)
984263bc 509{
e806bedd
MD
510 struct vm_object_dealloc_list *dlist = NULL;
511 struct vm_object_dealloc_list *dtmp;
984263bc 512 vm_object_t temp;
b12defdc 513 int must_drop = 0;
212f39f5 514
e806bedd
MD
515 /*
516 * We may chain deallocate object, but additional objects may
517 * collect on the dlist which also have to be deallocated. We
518 * must avoid a recursion, vm_object chains can get deep.
519 */
520again:
984263bc 521 while (object != NULL) {
b12defdc
MD
522#if 0
523 /*
524 * Don't rip a ref_count out from under an object undergoing
525 * collapse, it will confuse the collapse code.
526 */
527 vm_object_chain_wait(object);
528#endif
984263bc
MD
529 if (object->type == OBJT_VNODE) {
530 vm_object_vndeallocate(object);
6846fd23 531 break;
984263bc
MD
532 }
533
534 if (object->ref_count == 0) {
6846fd23
MD
535 panic("vm_object_deallocate: object deallocated "
536 "too many times: %d", object->type);
537 }
538 if (object->ref_count > 2) {
984263bc 539 object->ref_count--;
2de4f77e
MD
540 break;
541 }
542
984263bc
MD
543 /*
544 * Here on ref_count of one or two, which are special cases for
545 * objects.
212f39f5
MD
546 *
547 * Nominal ref_count > 1 case if the second ref is not from
548 * a shadow.
984263bc 549 */
212f39f5 550 if (object->ref_count == 2 && object->shadow_count == 0) {
984263bc
MD
551 vm_object_set_flag(object, OBJ_ONEMAPPING);
552 object->ref_count--;
6846fd23
MD
553 break;
554 }
212f39f5
MD
555
556 /*
557 * If the second ref is from a shadow we chain along it
b12defdc 558 * upwards if object's handle is exhausted.
1f8fc82a
MD
559 *
560 * We have to decrement object->ref_count before potentially
561 * collapsing the first shadow object or the collapse code
b12defdc
MD
562 * will not be able to handle the degenerate case to remove
563 * object. However, if we do it too early the object can
564 * get ripped out from under us.
212f39f5 565 */
b12defdc
MD
566 if (object->ref_count == 2 && object->shadow_count == 1 &&
567 object->handle == NULL && (object->type == OBJT_DEFAULT ||
568 object->type == OBJT_SWAP)) {
569 temp = LIST_FIRST(&object->shadow_head);
570 KKASSERT(temp != NULL);
571 vm_object_hold(temp);
572
573 /*
574 * Wait for any paging to complete so the collapse
575 * doesn't (or isn't likely to) qcollapse. pip
576 * waiting must occur before we acquire the
577 * chainlock.
578 */
579 while (
580 temp->paging_in_progress ||
581 object->paging_in_progress
582 ) {
583 vm_object_pip_wait(temp, "objde1");
584 vm_object_pip_wait(object, "objde2");
585 }
586
587 /*
588 * If the parent is locked we have to give up, as
589 * otherwise we would be acquiring locks in the
590 * wrong order and potentially deadlock.
591 */
592 if (temp->flags & OBJ_CHAINLOCK) {
593 vm_object_drop(temp);
594 goto skip;
595 }
596 vm_object_chain_acquire(temp);
597
598 /*
599 * Recheck/retry after the hold and the paging
600 * wait, both of which can block us.
601 */
602 if (object->ref_count != 2 ||
603 object->shadow_count != 1 ||
604 object->handle ||
605 LIST_FIRST(&object->shadow_head) != temp ||
606 (object->type != OBJT_DEFAULT &&
607 object->type != OBJT_SWAP)) {
608 vm_object_chain_release(temp);
609 vm_object_drop(temp);
610 continue;
611 }
612
613 /*
614 * We can safely drop object's ref_count now.
615 */
616 KKASSERT(object->ref_count == 2);
1f8fc82a 617 object->ref_count--;
984263bc 618
b12defdc
MD
619 /*
620 * If our single parent is not collapseable just
621 * decrement ref_count (2->1) and stop.
622 */
623 if (temp->handle || (temp->type != OBJT_DEFAULT &&
624 temp->type != OBJT_SWAP)) {
625 vm_object_chain_release(temp);
626 vm_object_drop(temp);
627 break;
984263bc 628 }
b12defdc
MD
629
630 /*
631 * At this point we have already dropped object's
632 * ref_count so it is possible for a race to
633 * deallocate obj out from under us. Any collapse
634 * will re-check the situation. We must not block
635 * until we are able to collapse.
636 *
637 * Bump temp's ref_count to avoid an unwanted
638 * degenerate recursion (can't call
639 * vm_object_reference_locked() because it asserts
640 * that CHAINLOCK is not set).
641 */
642 temp->ref_count++;
643 KKASSERT(temp->ref_count > 1);
644
645 /*
646 * Collapse temp, then deallocate the extra ref
647 * formally.
648 */
e806bedd 649 vm_object_collapse(temp, &dlist);
b12defdc
MD
650 vm_object_chain_release(temp);
651 if (must_drop) {
652 vm_object_lock_swap();
653 vm_object_drop(object);
654 }
655 object = temp;
656 must_drop = 1;
657 continue;
984263bc
MD
658 }
659
6846fd23 660 /*
b12defdc
MD
661 * Drop the ref and handle termination on the 1->0 transition.
662 * We may have blocked above so we have to recheck.
6846fd23 663 */
b12defdc
MD
664skip:
665 KKASSERT(object->ref_count != 0);
666 if (object->ref_count >= 2) {
667 object->ref_count--;
6846fd23 668 break;
2de4f77e 669 }
b12defdc 670 KKASSERT(object->ref_count == 1);
6846fd23
MD
671
672 /*
b12defdc
MD
673 * 1->0 transition. Chain through the backing_object.
674 * Maintain the ref until we've located the backing object,
675 * then re-check.
6846fd23 676 */
212f39f5 677 while ((temp = object->backing_object) != NULL) {
b12defdc 678 vm_object_hold(temp);
212f39f5
MD
679 if (temp == object->backing_object)
680 break;
b12defdc 681 vm_object_drop(temp);
212f39f5 682 }
b12defdc
MD
683
684 /*
685 * 1->0 transition verified, retry if ref_count is no longer
686 * 1. Otherwise disconnect the backing_object (temp) and
687 * clean up.
688 */
689 if (object->ref_count != 1) {
690 vm_object_drop(temp);
691 continue;
692 }
693
694 /*
695 * It shouldn't be possible for the object to be chain locked
696 * if we're removing the last ref on it.
697 */
698 KKASSERT((object->flags & OBJ_CHAINLOCK) == 0);
699
984263bc
MD
700 if (temp) {
701 LIST_REMOVE(object, shadow_list);
702 temp->shadow_count--;
984263bc
MD
703 temp->generation++;
704 object->backing_object = NULL;
705 }
706
b12defdc
MD
707 --object->ref_count;
708 if ((object->flags & OBJ_DEAD) == 0)
984263bc 709 vm_object_terminate(object);
b12defdc
MD
710 if (must_drop && temp)
711 vm_object_lock_swap();
712 if (must_drop)
713 vm_object_drop(object);
984263bc 714 object = temp;
b12defdc 715 must_drop = 1;
984263bc 716 }
b12defdc
MD
717 if (must_drop && object)
718 vm_object_drop(object);
e806bedd
MD
719
720 /*
721 * Additional tail recursion on dlist. Avoid a recursion. Objects
722 * on the dlist have a hold count but are not locked.
723 */
724 if ((dtmp = dlist) != NULL) {
725 dlist = dtmp->next;
726 object = dtmp->object;
727 kfree(dtmp, M_TEMP);
728
729 vm_object_lock(object); /* already held, add lock */
730 must_drop = 1; /* and we're responsible for it */
731 goto again;
732 }
984263bc
MD
733}
734
735/*
6846fd23
MD
736 * Destroy the specified object, freeing up related resources.
737 *
738 * The object must have zero references.
984263bc 739 *
b12defdc
MD
740 * The object must held. The caller is responsible for dropping the object
741 * after terminate returns. Terminate does NOT drop the object.
984263bc 742 */
1f804340
MD
743static int vm_object_terminate_callback(vm_page_t p, void *data);
744
984263bc 745void
57e43348 746vm_object_terminate(vm_object_t object)
984263bc 747{
984263bc 748 /*
2de4f77e
MD
749 * Make sure no one uses us. Once we set OBJ_DEAD we should be
750 * able to safely block.
984263bc 751 */
b12defdc 752 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
2de4f77e 753 KKASSERT((object->flags & OBJ_DEAD) == 0);
984263bc
MD
754 vm_object_set_flag(object, OBJ_DEAD);
755
756 /*
2de4f77e 757 * Wait for the pageout daemon to be done with the object
984263bc 758 */
e1c14c82 759 vm_object_pip_wait(object, "objtrm1");
984263bc
MD
760
761 KASSERT(!object->paging_in_progress,
762 ("vm_object_terminate: pageout in progress"));
763
764 /*
765 * Clean and free the pages, as appropriate. All references to the
766 * object are gone, so we don't need to lock it.
767 */
768 if (object->type == OBJT_VNODE) {
769 struct vnode *vp;
770
984263bc
MD
771 /*
772 * Clean pages and flush buffers.
773 */
774 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
775
776 vp = (struct vnode *) object->handle;
87de5057 777 vinvalbuf(vp, V_SAVE, 0, 0);
984263bc
MD
778 }
779
780 /*
781 * Wait for any I/O to complete, after which there had better not
782 * be any references left on the object.
783 */
e1c14c82 784 vm_object_pip_wait(object, "objtrm2");
984263bc 785
2de4f77e
MD
786 if (object->ref_count != 0) {
787 panic("vm_object_terminate: object with references, "
788 "ref_count=%d", object->ref_count);
789 }
984263bc
MD
790
791 /*
792 * Now free any remaining pages. For internal objects, this also
793 * removes them from paging queues. Don't free wired pages, just
794 * remove them from the object.
795 */
1f804340
MD
796 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
797 vm_object_terminate_callback, NULL);
984263bc 798
e1c14c82 799 /*
398c240d 800 * Let the pager know object is dead.
e1c14c82 801 */
398c240d 802 vm_pager_deallocate(object);
e1c14c82 803
984263bc 804 /*
b12defdc
MD
805 * Wait for the object hold count to hit 1, clean out pages as
806 * we go. vmobj_token interlocks any race conditions that might
807 * pick the object up from the vm_object_list after we have cleared
808 * rb_memq.
984263bc 809 */
34542daf 810 for (;;) {
34542daf
MD
811 if (RB_ROOT(&object->rb_memq) == NULL)
812 break;
813 kprintf("vm_object_terminate: Warning, object %p "
814 "still has %d pages\n",
815 object, object->resident_page_count);
816 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
817 vm_object_terminate_callback, NULL);
818 }
34542daf
MD
819
820 /*
821 * There had better not be any pages left
822 */
823 KKASSERT(object->resident_page_count == 0);
984263bc
MD
824
825 /*
826 * Remove the object from the global object list.
827 */
b12defdc 828 lwkt_gettoken(&vmobj_token);
984263bc 829 TAILQ_REMOVE(&vm_object_list, object, object_list);
fad57d0e 830 vm_object_count--;
b12defdc 831 lwkt_reltoken(&vmobj_token);
85946b6c 832 vm_object_dead_wakeup(object);
2de4f77e
MD
833
834 if (object->ref_count != 0) {
835 panic("vm_object_terminate2: object with references, "
836 "ref_count=%d", object->ref_count);
837 }
984263bc
MD
838
839 /*
b12defdc
MD
840 * NOTE: The object hold_count is at least 1, so we cannot zfree()
841 * the object here. See vm_object_drop().
984263bc 842 */
984263bc
MD
843}
844
6846fd23 845/*
b12defdc 846 * The caller must hold the object.
6846fd23 847 */
1f804340
MD
848static int
849vm_object_terminate_callback(vm_page_t p, void *data __unused)
850{
b12defdc
MD
851 vm_object_t object;
852
853 object = p->object;
ac952071 854 vm_page_busy_wait(p, TRUE, "vmpgtrm");
b12defdc
MD
855 if (object != p->object) {
856 kprintf("vm_object_terminate: Warning: Encountered "
857 "busied page %p on queue %d\n", p, p->queue);
858 vm_page_wakeup(p);
859 } else if (p->wire_count == 0) {
1f804340
MD
860 vm_page_free(p);
861 mycpu->gd_cnt.v_pfree++;
862 } else {
c4ba48c4 863 if (p->queue != PQ_NONE)
b12defdc
MD
864 kprintf("vm_object_terminate: Warning: Encountered "
865 "wired page %p on queue %d\n", p, p->queue);
1f804340
MD
866 vm_page_remove(p);
867 vm_page_wakeup(p);
868 }
d2d8515b 869 lwkt_yield();
1f804340
MD
870 return(0);
871}
872
9e12ff11
MD
873/*
874 * The object is dead but still has an object<->pager association. Sleep
875 * and return. The caller typically retests the association in a loop.
6846fd23 876 *
b12defdc 877 * The caller must hold the object.
9e12ff11
MD
878 */
879void
880vm_object_dead_sleep(vm_object_t object, const char *wmesg)
881{
b12defdc 882 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
9e12ff11
MD
883 if (object->handle) {
884 vm_object_set_flag(object, OBJ_DEADWNT);
885 tsleep(object, 0, wmesg, 0);
2de4f77e 886 /* object may be invalid after this point */
9e12ff11 887 }
9e12ff11
MD
888}
889
890/*
891 * Wakeup anyone waiting for the object<->pager disassociation on
892 * a dead object.
6846fd23 893 *
b12defdc 894 * The caller must hold the object.
9e12ff11
MD
895 */
896void
897vm_object_dead_wakeup(vm_object_t object)
898{
b12defdc 899 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
9e12ff11
MD
900 if (object->flags & OBJ_DEADWNT) {
901 vm_object_clear_flag(object, OBJ_DEADWNT);
902 wakeup(object);
903 }
904}
905
984263bc 906/*
6846fd23
MD
907 * Clean all dirty pages in the specified range of object. Leaves page
908 * on whatever queue it is currently on. If NOSYNC is set then do not
909 * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
910 * leaving the object dirty.
984263bc 911 *
6846fd23
MD
912 * When stuffing pages asynchronously, allow clustering. XXX we need a
913 * synchronous clustering mode implementation.
984263bc 914 *
6846fd23 915 * Odd semantics: if start == end, we clean everything.
984263bc 916 *
6846fd23 917 * The object must be locked? XXX
984263bc 918 */
1f804340
MD
919static int vm_object_page_clean_pass1(struct vm_page *p, void *data);
920static int vm_object_page_clean_pass2(struct vm_page *p, void *data);
984263bc
MD
921
922void
57e43348 923vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1f804340 924 int flags)
984263bc 925{
1f804340 926 struct rb_vm_page_scan_info info;
984263bc 927 struct vnode *vp;
1f804340 928 int wholescan;
984263bc 929 int pagerflags;
b12defdc 930 int generation;
984263bc 931
edd6da8e 932 vm_object_hold(object);
984263bc 933 if (object->type != OBJT_VNODE ||
6846fd23 934 (object->flags & OBJ_MIGHTBEDIRTY) == 0) {
edd6da8e 935 vm_object_drop(object);
984263bc 936 return;
6846fd23 937 }
984263bc 938
1f804340
MD
939 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ?
940 VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
984263bc
MD
941 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
942
943 vp = object->handle;
944
1f804340
MD
945 /*
946 * Interlock other major object operations. This allows us to
947 * temporarily clear OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY.
948 */
984263bc
MD
949 vm_object_set_flag(object, OBJ_CLEANING);
950
951 /*
952 * Handle 'entire object' case
953 */
1f804340 954 info.start_pindex = start;
984263bc 955 if (end == 0) {
1f804340 956 info.end_pindex = object->size - 1;
984263bc 957 } else {
1f804340 958 info.end_pindex = end - 1;
984263bc 959 }
1f804340
MD
960 wholescan = (start == 0 && info.end_pindex == object->size - 1);
961 info.limit = flags;
962 info.pagerflags = pagerflags;
963 info.object = object;
984263bc
MD
964
965 /*
1f804340
MD
966 * If cleaning the entire object do a pass to mark the pages read-only.
967 * If everything worked out ok, clear OBJ_WRITEABLE and
968 * OBJ_MIGHTBEDIRTY.
984263bc 969 */
1f804340
MD
970 if (wholescan) {
971 info.error = 0;
972 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
973 vm_object_page_clean_pass1, &info);
974 if (info.error == 0) {
975 vm_object_clear_flag(object,
976 OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
977 if (object->type == OBJT_VNODE &&
978 (vp = (struct vnode *)object->handle) != NULL) {
979 if (vp->v_flag & VOBJDIRTY)
980 vclrflags(vp, VOBJDIRTY);
984263bc 981 }
984263bc 982 }
984263bc
MD
983 }
984
985 /*
1f804340 986 * Do a pass to clean all the dirty pages we find.
984263bc 987 */
1f804340
MD
988 do {
989 info.error = 0;
b12defdc 990 generation = object->generation;
1f804340
MD
991 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
992 vm_object_page_clean_pass2, &info);
b12defdc 993 } while (info.error || generation != object->generation);
984263bc 994
1f804340 995 vm_object_clear_flag(object, OBJ_CLEANING);
edd6da8e 996 vm_object_drop(object);
1f804340 997}
984263bc 998
6846fd23 999/*
b12defdc 1000 * The caller must hold the object.
6846fd23 1001 */
1f804340
MD
1002static
1003int
1004vm_object_page_clean_pass1(struct vm_page *p, void *data)
1005{
1006 struct rb_vm_page_scan_info *info = data;
984263bc 1007
1f804340 1008 vm_page_flag_set(p, PG_CLEANCHK);
b12defdc 1009 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1f804340 1010 info->error = 1;
b12defdc 1011 } else if (vm_page_busy_try(p, FALSE) == 0) {
17cde63e 1012 vm_page_protect(p, VM_PROT_READ); /* must not block */
b12defdc
MD
1013 vm_page_wakeup(p);
1014 } else {
1015 info->error = 1;
1016 }
fc9ed34d 1017 lwkt_yield();
1f804340
MD
1018 return(0);
1019}
1020
6846fd23 1021/*
b12defdc 1022 * The caller must hold the object
6846fd23 1023 */
1f804340
MD
1024static
1025int
1026vm_object_page_clean_pass2(struct vm_page *p, void *data)
1027{
1028 struct rb_vm_page_scan_info *info = data;
b12defdc 1029 int generation;
984263bc 1030
06ecca5a 1031 /*
1f804340
MD
1032 * Do not mess with pages that were inserted after we started
1033 * the cleaning pass.
06ecca5a 1034 */
1f804340 1035 if ((p->flags & PG_CLEANCHK) == 0)
fc9ed34d 1036 goto done;
984263bc 1037
b12defdc
MD
1038 generation = info->object->generation;
1039 vm_page_busy_wait(p, TRUE, "vpcwai");
1040 if (p->object != info->object ||
1041 info->object->generation != generation) {
1042 info->error = 1;
1043 vm_page_wakeup(p);
fc9ed34d 1044 goto done;
b12defdc
MD
1045 }
1046
1f804340
MD
1047 /*
1048 * Before wasting time traversing the pmaps, check for trivial
1049 * cases where the page cannot be dirty.
1050 */
1051 if (p->valid == 0 || (p->queue - p->pc) == PQ_CACHE) {
1052 KKASSERT((p->dirty & p->valid) == 0);
b12defdc 1053 vm_page_wakeup(p);
fc9ed34d 1054 goto done;
1f804340 1055 }
984263bc 1056
1f804340
MD
1057 /*
1058 * Check whether the page is dirty or not. The page has been set
1059 * to be read-only so the check will not race a user dirtying the
1060 * page.
1061 */
1062 vm_page_test_dirty(p);
1063 if ((p->dirty & p->valid) == 0) {
1064 vm_page_flag_clear(p, PG_CLEANCHK);
b12defdc 1065 vm_page_wakeup(p);
fc9ed34d 1066 goto done;
1f804340 1067 }
984263bc 1068
1f804340
MD
1069 /*
1070 * If we have been asked to skip nosync pages and this is a
1071 * nosync page, skip it. Note that the object flags were
1072 * not cleared in this case (because pass1 will have returned an
1073 * error), so we do not have to set them.
1074 */
1075 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1076 vm_page_flag_clear(p, PG_CLEANCHK);
b12defdc 1077 vm_page_wakeup(p);
fc9ed34d 1078 goto done;
984263bc
MD
1079 }
1080
1f804340
MD
1081 /*
1082 * Flush as many pages as we can. PG_CLEANCHK will be cleared on
1083 * the pages that get successfully flushed. Set info->error if
1084 * we raced an object modification.
1085 */
b12defdc 1086 vm_object_page_collect_flush(info->object, p, info->pagerflags);
fc9ed34d
MD
1087done:
1088 lwkt_yield();
1f804340 1089 return(0);
984263bc
MD
1090}
1091
06ecca5a 1092/*
6846fd23 1093 * Collect the specified page and nearby pages and flush them out.
b12defdc
MD
1094 * The number of pages flushed is returned. The passed page is busied
1095 * by the caller and we are responsible for its disposition.
06ecca5a 1096 *
b12defdc 1097 * The caller must hold the object.
06ecca5a 1098 */
984263bc 1099static int
1f804340 1100vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags)
984263bc
MD
1101{
1102 int runlen;
b12defdc 1103 int error;
984263bc
MD
1104 int maxf;
1105 int chkb;
1106 int maxb;
1107 int i;
1108 vm_pindex_t pi;
1109 vm_page_t maf[vm_pageout_page_count];
1110 vm_page_t mab[vm_pageout_page_count];
1111 vm_page_t ma[vm_pageout_page_count];
1112
b12defdc 1113 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1f804340 1114
984263bc 1115 pi = p->pindex;
984263bc
MD
1116
1117 maxf = 0;
1118 for(i = 1; i < vm_pageout_page_count; i++) {
1119 vm_page_t tp;
1120
b12defdc
MD
1121 tp = vm_page_lookup_busy_try(object, pi + i, TRUE, &error);
1122 if (error)
1123 break;
1124 if (tp == NULL)
1125 break;
1126 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1127 (tp->flags & PG_CLEANCHK) == 0) {
1128 vm_page_wakeup(tp);
1129 break;
984263bc 1130 }
b12defdc
MD
1131 if ((tp->queue - tp->pc) == PQ_CACHE) {
1132 vm_page_flag_clear(tp, PG_CLEANCHK);
1133 vm_page_wakeup(tp);
1134 break;
1135 }
1136 vm_page_test_dirty(tp);
1137 if ((tp->dirty & tp->valid) == 0) {
1138 vm_page_flag_clear(tp, PG_CLEANCHK);
1139 vm_page_wakeup(tp);
1140 break;
1141 }
1142 maf[i - 1] = tp;
1143 maxf++;
984263bc
MD
1144 }
1145
1146 maxb = 0;
1147 chkb = vm_pageout_page_count - maxf;
b12defdc
MD
1148 /*
1149 * NOTE: chkb can be 0
1150 */
1151 for(i = 1; chkb && i < chkb; i++) {
1152 vm_page_t tp;
1153
1154 tp = vm_page_lookup_busy_try(object, pi - i, TRUE, &error);
1155 if (error)
1156 break;
1157 if (tp == NULL)
1158 break;
1159 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1160 (tp->flags & PG_CLEANCHK) == 0) {
1161 vm_page_wakeup(tp);
1162 break;
1163 }
1164 if ((tp->queue - tp->pc) == PQ_CACHE) {
1165 vm_page_flag_clear(tp, PG_CLEANCHK);
1166 vm_page_wakeup(tp);
984263bc
MD
1167 break;
1168 }
b12defdc
MD
1169 vm_page_test_dirty(tp);
1170 if ((tp->dirty & tp->valid) == 0) {
1171 vm_page_flag_clear(tp, PG_CLEANCHK);
1172 vm_page_wakeup(tp);
1173 break;
1174 }
1175 mab[i - 1] = tp;
1176 maxb++;
984263bc
MD
1177 }
1178
b12defdc
MD
1179 /*
1180 * All pages in the maf[] and mab[] array are busied.
1181 */
1182 for (i = 0; i < maxb; i++) {
984263bc
MD
1183 int index = (maxb - i) - 1;
1184 ma[index] = mab[i];
1185 vm_page_flag_clear(ma[index], PG_CLEANCHK);
1186 }
1187 vm_page_flag_clear(p, PG_CLEANCHK);
1188 ma[maxb] = p;
1189 for(i = 0; i < maxf; i++) {
1190 int index = (maxb + i) + 1;
1191 ma[index] = maf[i];
1192 vm_page_flag_clear(ma[index], PG_CLEANCHK);
1193 }
1194 runlen = maxb + maxf + 1;
1195
b12defdc
MD
1196 for (i = 0; i < runlen; i++)
1197 vm_page_hold(ma[i]);
1198
984263bc 1199 vm_pageout_flush(ma, runlen, pagerflags);
b12defdc 1200
984263bc
MD
1201 for (i = 0; i < runlen; i++) {
1202 if (ma[i]->valid & ma[i]->dirty) {
1203 vm_page_protect(ma[i], VM_PROT_READ);
1204 vm_page_flag_set(ma[i], PG_CLEANCHK);
1205
1206 /*
1207 * maxf will end up being the actual number of pages
1208 * we wrote out contiguously, non-inclusive of the
1209 * first page. We do not count look-behind pages.
1210 */
1211 if (i >= maxb + 1 && (maxf > i - maxb - 1))
1212 maxf = i - maxb - 1;
1213 }
b12defdc 1214 vm_page_unhold(ma[i]);
984263bc
MD
1215 }
1216 return(maxf + 1);
1217}
1218
984263bc
MD
1219/*
1220 * Same as vm_object_pmap_copy, except range checking really
1221 * works, and is meant for small sections of an object.
1222 *
1223 * This code protects resident pages by making them read-only
1224 * and is typically called on a fork or split when a page
1225 * is converted to copy-on-write.
1226 *
1227 * NOTE: If the page is already at VM_PROT_NONE, calling
1228 * vm_page_protect will have no effect.
1229 */
984263bc 1230void
57e43348 1231vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
984263bc
MD
1232{
1233 vm_pindex_t idx;
1234 vm_page_t p;
1235
1236 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
1237 return;
1238
b12defdc 1239 vm_object_hold(object);
984263bc
MD
1240 for (idx = start; idx < end; idx++) {
1241 p = vm_page_lookup(object, idx);
1242 if (p == NULL)
1243 continue;
1244 vm_page_protect(p, VM_PROT_READ);
1245 }
b12defdc 1246 vm_object_drop(object);
984263bc
MD
1247}
1248
1249/*
6846fd23
MD
1250 * Removes all physical pages in the specified object range from all
1251 * physical maps.
984263bc 1252 *
6846fd23 1253 * The object must *not* be locked.
984263bc 1254 */
1f804340
MD
1255
1256static int vm_object_pmap_remove_callback(vm_page_t p, void *data);
1257
984263bc 1258void
57e43348 1259vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
984263bc 1260{
1f804340 1261 struct rb_vm_page_scan_info info;
984263bc
MD
1262
1263 if (object == NULL)
1264 return;
1f804340
MD
1265 info.start_pindex = start;
1266 info.end_pindex = end - 1;
6846fd23 1267
b12defdc 1268 vm_object_hold(object);
1f804340
MD
1269 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1270 vm_object_pmap_remove_callback, &info);
1271 if (start == 0 && end == object->size)
984263bc 1272 vm_object_clear_flag(object, OBJ_WRITEABLE);
b12defdc 1273 vm_object_drop(object);
1f804340
MD
1274}
1275
6846fd23 1276/*
b12defdc 1277 * The caller must hold the object
6846fd23 1278 */
1f804340
MD
1279static int
1280vm_object_pmap_remove_callback(vm_page_t p, void *data __unused)
1281{
1282 vm_page_protect(p, VM_PROT_NONE);
1283 return(0);
984263bc
MD
1284}
1285
1286/*
6846fd23 1287 * Implements the madvise function at the object/page level.
984263bc 1288 *
6846fd23 1289 * MADV_WILLNEED (any object)
984263bc 1290 *
6846fd23 1291 * Activate the specified pages if they are resident.
984263bc 1292 *
6846fd23 1293 * MADV_DONTNEED (any object)
984263bc 1294 *
6846fd23 1295 * Deactivate the specified pages if they are resident.
984263bc 1296 *
6846fd23 1297 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, OBJ_ONEMAPPING only)
984263bc 1298 *
6846fd23
MD
1299 * Deactivate and clean the specified pages if they are
1300 * resident. This permits the process to reuse the pages
1301 * without faulting or the kernel to reclaim the pages
1302 * without I/O.
984263bc 1303 *
6846fd23 1304 * No requirements.
984263bc
MD
1305 */
1306void
57e43348 1307vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
984263bc
MD
1308{
1309 vm_pindex_t end, tpindex;
1310 vm_object_t tobject;
b12defdc 1311 vm_object_t xobj;
984263bc 1312 vm_page_t m;
b12defdc 1313 int error;
984263bc
MD
1314
1315 if (object == NULL)
1316 return;
1317
1318 end = pindex + count;
1319
b12defdc
MD
1320 vm_object_hold(object);
1321 tobject = object;
6846fd23 1322
984263bc
MD
1323 /*
1324 * Locate and adjust resident pages
1325 */
984263bc
MD
1326 for (; pindex < end; pindex += 1) {
1327relookup:
b12defdc
MD
1328 if (tobject != object)
1329 vm_object_drop(tobject);
984263bc
MD
1330 tobject = object;
1331 tpindex = pindex;
1332shadowlookup:
1333 /*
1334 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1335 * and those pages must be OBJ_ONEMAPPING.
1336 */
1337 if (advise == MADV_FREE) {
1338 if ((tobject->type != OBJT_DEFAULT &&
1339 tobject->type != OBJT_SWAP) ||
1340 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1341 continue;
1342 }
1343 }
1344
b12defdc 1345 m = vm_page_lookup_busy_try(tobject, tpindex, TRUE, &error);
984263bc 1346
b12defdc
MD
1347 if (error) {
1348 vm_page_sleep_busy(m, TRUE, "madvpo");
1349 goto relookup;
1350 }
984263bc
MD
1351 if (m == NULL) {
1352 /*
1353 * There may be swap even if there is no backing page
1354 */
1355 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1356 swap_pager_freespace(tobject, tpindex, 1);
1357
1358 /*
1359 * next object
1360 */
b12defdc
MD
1361 while ((xobj = tobject->backing_object) != NULL) {
1362 KKASSERT(xobj != object);
1363 vm_object_hold(xobj);
1364 if (xobj == tobject->backing_object)
1365 break;
1366 vm_object_drop(xobj);
1367 }
1368 if (xobj == NULL)
984263bc
MD
1369 continue;
1370 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
b12defdc
MD
1371 if (tobject != object) {
1372 vm_object_lock_swap();
1373 vm_object_drop(tobject);
1374 }
1375 tobject = xobj;
984263bc
MD
1376 goto shadowlookup;
1377 }
1378
1379 /*
b12defdc
MD
1380 * If the page is not in a normal active state, we skip it.
1381 * If the page is not managed there are no page queues to
1382 * mess with. Things can break if we mess with pages in
1383 * any of the below states.
984263bc
MD
1384 */
1385 if (
b12defdc 1386 /*m->hold_count ||*/
984263bc
MD
1387 m->wire_count ||
1388 (m->flags & PG_UNMANAGED) ||
1389 m->valid != VM_PAGE_BITS_ALL
1390 ) {
b12defdc 1391 vm_page_wakeup(m);
984263bc
MD
1392 continue;
1393 }
1394
06ecca5a
MD
1395 /*
1396 * Theoretically once a page is known not to be busy, an
1397 * interrupt cannot come along and rip it out from under us.
1398 */
984263bc
MD
1399
1400 if (advise == MADV_WILLNEED) {
1401 vm_page_activate(m);
1402 } else if (advise == MADV_DONTNEED) {
1403 vm_page_dontneed(m);
1404 } else if (advise == MADV_FREE) {
1405 /*
1406 * Mark the page clean. This will allow the page
1407 * to be freed up by the system. However, such pages
1408 * are often reused quickly by malloc()/free()
1409 * so we do not do anything that would cause
1410 * a page fault if we can help it.
1411 *
1412 * Specifically, we do not try to actually free
1413 * the page now nor do we try to put it in the
1414 * cache (which would cause a page fault on reuse).
1415 *
1416 * But we do make the page is freeable as we
1417 * can without actually taking the step of unmapping
1418 * it.
1419 */
1420 pmap_clear_modify(m);
1421 m->dirty = 0;
1422 m->act_count = 0;
1423 vm_page_dontneed(m);
1424 if (tobject->type == OBJT_SWAP)
1425 swap_pager_freespace(tobject, tpindex, 1);
1426 }
a491077e 1427 vm_page_wakeup(m);
984263bc 1428 }
b12defdc
MD
1429 if (tobject != object)
1430 vm_object_drop(tobject);
1431 vm_object_drop(object);
984263bc
MD
1432}
1433
1434/*
6846fd23 1435 * Create a new object which is backed by the specified existing object
b12defdc
MD
1436 * range. Replace the pointer and offset that was pointing at the existing
1437 * object with the pointer/offset for the new object.
984263bc 1438 *
6846fd23 1439 * No other requirements.
984263bc 1440 */
984263bc 1441void
b12defdc
MD
1442vm_object_shadow(vm_object_t *objectp, vm_ooffset_t *offset, vm_size_t length,
1443 int addref)
984263bc
MD
1444{
1445 vm_object_t source;
1446 vm_object_t result;
1447
b12defdc 1448 source = *objectp;
984263bc
MD
1449
1450 /*
1451 * Don't create the new object if the old object isn't shared.
b12defdc
MD
1452 * We have to chain wait before adding the reference to avoid
1453 * racing a collapse or deallocation.
1454 *
1455 * Add the additional ref to source here to avoid racing a later
1456 * collapse or deallocation. Clear the ONEMAPPING flag whether
1457 * addref is TRUE or not in this case because the original object
1458 * will be shadowed.
984263bc 1459 */
b12defdc
MD
1460 if (source) {
1461 vm_object_hold(source);
1462 vm_object_chain_wait(source);
1463 if (source->ref_count == 1 &&
1464 source->handle == NULL &&
1465 (source->type == OBJT_DEFAULT ||
1466 source->type == OBJT_SWAP)) {
1467 vm_object_drop(source);
1468 if (addref) {
b12defdc 1469 vm_object_reference_locked(source);
6056eb53 1470 vm_object_clear_flag(source, OBJ_ONEMAPPING);
b12defdc
MD
1471 }
1472 return;
1473 }
1474 vm_object_reference_locked(source);
1475 vm_object_clear_flag(source, OBJ_ONEMAPPING);
6846fd23 1476 }
984263bc
MD
1477
1478 /*
b12defdc
MD
1479 * Allocate a new object with the given length. The new object
1480 * is returned referenced but we may have to add another one.
1481 * If we are adding a second reference we must clear OBJ_ONEMAPPING.
1482 * (typically because the caller is about to clone a vm_map_entry).
1483 *
1484 * The source object currently has an extra reference to prevent
1485 * collapses into it while we mess with its shadow list, which
1486 * we will remove later in this routine.
984263bc 1487 */
984263bc
MD
1488 if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
1489 panic("vm_object_shadow: no object for shadowing");
b12defdc
MD
1490 vm_object_hold(result);
1491 if (addref) {
1492 vm_object_reference_locked(result);
1493 vm_object_clear_flag(result, OBJ_ONEMAPPING);
1494 }
984263bc
MD
1495
1496 /*
b12defdc
MD
1497 * The new object shadows the source object. Chain wait before
1498 * adjusting shadow_count or the shadow list to avoid races.
984263bc
MD
1499 *
1500 * Try to optimize the result object's page color when shadowing
1501 * in order to maintain page coloring consistency in the combined
1502 * shadowed object.
1503 */
b12defdc 1504 KKASSERT(result->backing_object == NULL);
984263bc
MD
1505 result->backing_object = source;
1506 if (source) {
b12defdc 1507 vm_object_chain_wait(source);
984263bc
MD
1508 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1509 source->shadow_count++;
1510 source->generation++;
85946b6c
MD
1511#ifdef SMP
1512 /* cpu localization twist */
1513 result->pg_color = (int)(intptr_t)curthread;
1514#else
b12defdc
MD
1515 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1516 PQ_L2_MASK;
85946b6c 1517#endif
984263bc
MD
1518 }
1519
1520 /*
b12defdc
MD
1521 * Adjust the return storage. Drop the ref on source before
1522 * returning.
984263bc 1523 */
984263bc 1524 result->backing_object_offset = *offset;
b12defdc
MD
1525 vm_object_drop(result);
1526 *offset = 0;
1527 if (source) {
1528 vm_object_deallocate_locked(source);
1529 vm_object_drop(source);
1530 }
984263bc
MD
1531
1532 /*
1533 * Return the new things
1534 */
b12defdc 1535 *objectp = result;
984263bc
MD
1536}
1537
1538#define OBSC_TEST_ALL_SHADOWED 0x0001
1539#define OBSC_COLLAPSE_NOWAIT 0x0002
1540#define OBSC_COLLAPSE_WAIT 0x0004
1541
1f804340
MD
1542static int vm_object_backing_scan_callback(vm_page_t p, void *data);
1543
6846fd23 1544/*
b12defdc 1545 * The caller must hold the object.
6846fd23 1546 */
984263bc 1547static __inline int
b12defdc 1548vm_object_backing_scan(vm_object_t object, vm_object_t backing_object, int op)
984263bc 1549{
1f804340 1550 struct rb_vm_page_scan_info info;
984263bc 1551
b12defdc
MD
1552 vm_object_assert_held(object);
1553 vm_object_assert_held(backing_object);
1554
1555 KKASSERT(backing_object == object->backing_object);
1f804340 1556 info.backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
984263bc
MD
1557
1558 /*
1559 * Initial conditions
1560 */
984263bc
MD
1561 if (op & OBSC_TEST_ALL_SHADOWED) {
1562 /*
1563 * We do not want to have to test for the existence of
1564 * swap pages in the backing object. XXX but with the
1565 * new swapper this would be pretty easy to do.
1566 *
1567 * XXX what about anonymous MAP_SHARED memory that hasn't
1568 * been ZFOD faulted yet? If we do not test for this, the
1569 * shadow test may succeed! XXX
1570 */
b12defdc 1571 if (backing_object->type != OBJT_DEFAULT)
984263bc 1572 return(0);
984263bc
MD
1573 }
1574 if (op & OBSC_COLLAPSE_WAIT) {
fad57d0e 1575 KKASSERT((backing_object->flags & OBJ_DEAD) == 0);
984263bc 1576 vm_object_set_flag(backing_object, OBJ_DEAD);
b12defdc
MD
1577 lwkt_gettoken(&vmobj_token);
1578 TAILQ_REMOVE(&vm_object_list, backing_object, object_list);
1579 vm_object_count--;
b12defdc 1580 lwkt_reltoken(&vmobj_token);
85946b6c 1581 vm_object_dead_wakeup(backing_object);
984263bc
MD
1582 }
1583
1584 /*
1f804340
MD
1585 * Our scan. We have to retry if a negative error code is returned,
1586 * otherwise 0 or 1 will be returned in info.error. 0 Indicates that
1587 * the scan had to be stopped because the parent does not completely
1588 * shadow the child.
984263bc 1589 */
1f804340
MD
1590 info.object = object;
1591 info.backing_object = backing_object;
1592 info.limit = op;
1593 do {
1594 info.error = 1;
1595 vm_page_rb_tree_RB_SCAN(&backing_object->rb_memq, NULL,
1596 vm_object_backing_scan_callback,
1597 &info);
1598 } while (info.error < 0);
a5fc46c9 1599
1f804340
MD
1600 return(info.error);
1601}
984263bc 1602
6846fd23 1603/*
b12defdc 1604 * The caller must hold the object.
6846fd23 1605 */
1f804340
MD
1606static int
1607vm_object_backing_scan_callback(vm_page_t p, void *data)
1608{
1609 struct rb_vm_page_scan_info *info = data;
1610 vm_object_t backing_object;
1611 vm_object_t object;
1612 vm_pindex_t new_pindex;
1613 vm_pindex_t backing_offset_index;
1614 int op;
984263bc 1615
1f804340
MD
1616 new_pindex = p->pindex - info->backing_offset_index;
1617 op = info->limit;
1618 object = info->object;
1619 backing_object = info->backing_object;
1620 backing_offset_index = info->backing_offset_index;
984263bc 1621
1f804340
MD
1622 if (op & OBSC_TEST_ALL_SHADOWED) {
1623 vm_page_t pp;
984263bc 1624
1f804340
MD
1625 /*
1626 * Ignore pages outside the parent object's range
1627 * and outside the parent object's mapping of the
1628 * backing object.
1629 *
1630 * note that we do not busy the backing object's
1631 * page.
1632 */
1633 if (
1634 p->pindex < backing_offset_index ||
1635 new_pindex >= object->size
1636 ) {
1637 return(0);
984263bc
MD
1638 }
1639
1640 /*
1f804340
MD
1641 * See if the parent has the page or if the parent's
1642 * object pager has the page. If the parent has the
1643 * page but the page is not valid, the parent's
1644 * object pager must have the page.
1645 *
1646 * If this fails, the parent does not completely shadow
1647 * the object and we might as well give up now.
984263bc
MD
1648 */
1649
1f804340 1650 pp = vm_page_lookup(object, new_pindex);
1b9d3514
MD
1651 if ((pp == NULL || pp->valid == 0) &&
1652 !vm_pager_has_page(object, new_pindex)
1f804340
MD
1653 ) {
1654 info->error = 0; /* problemo */
1655 return(-1); /* stop the scan */
1656 }
1657 }
984263bc 1658
1f804340
MD
1659 /*
1660 * Check for busy page
1661 */
1f804340
MD
1662 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1663 vm_page_t pp;
984263bc 1664
b12defdc
MD
1665 if (vm_page_busy_try(p, TRUE)) {
1666 if (op & OBSC_COLLAPSE_NOWAIT) {
1f804340 1667 return(0);
b12defdc 1668 } else {
984263bc 1669 /*
1f804340
MD
1670 * If we slept, anything could have
1671 * happened. Ask that the scan be restarted.
984263bc 1672 *
1f804340
MD
1673 * Since the object is marked dead, the
1674 * backing offset should not have changed.
984263bc 1675 */
b12defdc 1676 vm_page_sleep_busy(p, TRUE, "vmocol");
1f804340
MD
1677 info->error = -1;
1678 return(-1);
984263bc 1679 }
1f804340 1680 }
b12defdc
MD
1681 if (op & OBSC_COLLAPSE_NOWAIT) {
1682 if (p->valid == 0 /*|| p->hold_count*/ ||
1683 p->wire_count) {
1684 vm_page_wakeup(p);
1685 return(0);
1686 }
1687 } else {
1688 /* XXX what if p->valid == 0 , hold_count, etc? */
1689 }
984263bc 1690
1f804340
MD
1691 KASSERT(
1692 p->object == backing_object,
1693 ("vm_object_qcollapse(): object mismatch")
1694 );
1695
1696 /*
1697 * Destroy any associated swap
1698 */
8d292090
MD
1699 if (backing_object->type == OBJT_SWAP)
1700 swap_pager_freespace(backing_object, p->pindex, 1);
1f804340
MD
1701
1702 if (
1703 p->pindex < backing_offset_index ||
1704 new_pindex >= object->size
1705 ) {
984263bc 1706 /*
1f804340
MD
1707 * Page is out of the parent object's range, we
1708 * can simply destroy it.
984263bc 1709 */
1f804340
MD
1710 vm_page_protect(p, VM_PROT_NONE);
1711 vm_page_free(p);
1712 return(0);
1713 }
984263bc 1714
1f804340 1715 pp = vm_page_lookup(object, new_pindex);
1b9d3514 1716 if (pp != NULL || vm_pager_has_page(object, new_pindex)) {
1f804340
MD
1717 /*
1718 * page already exists in parent OR swap exists
1719 * for this location in the parent. Destroy
1720 * the original page from the backing object.
1721 *
1722 * Leave the parent's page alone
1723 */
1724 vm_page_protect(p, VM_PROT_NONE);
1725 vm_page_free(p);
1726 return(0);
984263bc 1727 }
1f804340
MD
1728
1729 /*
1730 * Page does not exist in parent, rename the
1731 * page from the backing object to the main object.
1732 *
1733 * If the page was mapped to a process, it can remain
1734 * mapped through the rename.
1735 */
1736 if ((p->queue - p->pc) == PQ_CACHE)
1737 vm_page_deactivate(p);
1738
1739 vm_page_rename(p, object, new_pindex);
b12defdc 1740 vm_page_wakeup(p);
1f804340 1741 /* page automatically made dirty by rename */
984263bc 1742 }
1f804340 1743 return(0);
984263bc
MD
1744}
1745
984263bc 1746/*
6846fd23 1747 * This version of collapse allows the operation to occur earlier and
984263bc
MD
1748 * when paging_in_progress is true for an object... This is not a complete
1749 * operation, but should plug 99.9% of the rest of the leaks.
6846fd23 1750 *
b12defdc
MD
1751 * The caller must hold the object and backing_object and both must be
1752 * chainlocked.
1753 *
2de4f77e 1754 * (only called from vm_object_collapse)
984263bc
MD
1755 */
1756static void
b12defdc 1757vm_object_qcollapse(vm_object_t object, vm_object_t backing_object)
984263bc 1758{
b12defdc
MD
1759 if (backing_object->ref_count == 1) {
1760 backing_object->ref_count += 2;
1761 vm_object_backing_scan(object, backing_object,
1762 OBSC_COLLAPSE_NOWAIT);
1763 backing_object->ref_count -= 2;
1764 }
984263bc
MD
1765}
1766
1767/*
6846fd23
MD
1768 * Collapse an object with the object backing it. Pages in the backing
1769 * object are moved into the parent, and the backing object is deallocated.
e806bedd 1770 * Any conflict is resolved in favor of the parent's existing pages.
212f39f5 1771 *
b12defdc
MD
1772 * object must be held and chain-locked on call.
1773 *
1774 * The caller must have an extra ref on object to prevent a race from
1775 * destroying it during the collapse.
984263bc
MD
1776 */
1777void
e806bedd 1778vm_object_collapse(vm_object_t object, struct vm_object_dealloc_list **dlistp)
984263bc 1779{
e806bedd 1780 struct vm_object_dealloc_list *dlist = NULL;
b12defdc
MD
1781 vm_object_t backing_object;
1782
1783 /*
1784 * Only one thread is attempting a collapse at any given moment.
1785 * There are few restrictions for (object) that callers of this
1786 * function check so reentrancy is likely.
1787 */
1788 KKASSERT(object != NULL);
212f39f5 1789 vm_object_assert_held(object);
b12defdc
MD
1790 KKASSERT(object->flags & OBJ_CHAINLOCK);
1791
1792 for (;;) {
1793 vm_object_t bbobj;
1794 int dodealloc;
6846fd23 1795
b12defdc
MD
1796 /*
1797 * We have to hold the backing object, check races.
1798 */
1799 while ((backing_object = object->backing_object) != NULL) {
1800 vm_object_hold(backing_object);
1801 if (backing_object == object->backing_object)
1802 break;
1803 vm_object_drop(backing_object);
1804 }
984263bc
MD
1805
1806 /*
b12defdc 1807 * No backing object? Nothing to collapse then.
984263bc 1808 */
b12defdc 1809 if (backing_object == NULL)
984263bc
MD
1810 break;
1811
b12defdc
MD
1812 /*
1813 * You can't collapse with a non-default/non-swap object.
1814 */
1815 if (backing_object->type != OBJT_DEFAULT &&
1816 backing_object->type != OBJT_SWAP) {
1817 vm_object_drop(backing_object);
1818 backing_object = NULL;
984263bc 1819 break;
b12defdc 1820 }
984263bc 1821
b12defdc
MD
1822 /*
1823 * Chain-lock the backing object too because if we
1824 * successfully merge its pages into the top object we
1825 * will collapse backing_object->backing_object as the
1826 * new backing_object. Re-check that it is still our
1827 * backing object.
1828 */
1829 vm_object_chain_acquire(backing_object);
212f39f5 1830 if (backing_object != object->backing_object) {
b12defdc 1831 vm_object_chain_release(backing_object);
212f39f5
MD
1832 vm_object_drop(backing_object);
1833 continue;
1834 }
1835
984263bc
MD
1836 /*
1837 * we check the backing object first, because it is most likely
1838 * not collapsable.
1839 */
1840 if (backing_object->handle != NULL ||
1841 (backing_object->type != OBJT_DEFAULT &&
1842 backing_object->type != OBJT_SWAP) ||
1843 (backing_object->flags & OBJ_DEAD) ||
1844 object->handle != NULL ||
1845 (object->type != OBJT_DEFAULT &&
1846 object->type != OBJT_SWAP) ||
1847 (object->flags & OBJ_DEAD)) {
1848 break;
1849 }
1850
b12defdc
MD
1851 /*
1852 * If paging is in progress we can't do a normal collapse.
1853 */
984263bc
MD
1854 if (
1855 object->paging_in_progress != 0 ||
1856 backing_object->paging_in_progress != 0
1857 ) {
b12defdc 1858 vm_object_qcollapse(object, backing_object);
984263bc
MD
1859 break;
1860 }
1861
1862 /*
1863 * We know that we can either collapse the backing object (if
1864 * the parent is the only reference to it) or (perhaps) have
1865 * the parent bypass the object if the parent happens to shadow
1866 * all the resident pages in the entire backing object.
1867 *
1868 * This is ignoring pager-backed pages such as swap pages.
1869 * vm_object_backing_scan fails the shadowing test in this
1870 * case.
1871 */
984263bc
MD
1872 if (backing_object->ref_count == 1) {
1873 /*
1874 * If there is exactly one reference to the backing
1875 * object, we can collapse it into the parent.
1876 */
b12defdc
MD
1877 KKASSERT(object->backing_object == backing_object);
1878 vm_object_backing_scan(object, backing_object,
1879 OBSC_COLLAPSE_WAIT);
984263bc
MD
1880
1881 /*
1882 * Move the pager from backing_object to object.
1883 */
984263bc
MD
1884 if (backing_object->type == OBJT_SWAP) {
1885 vm_object_pip_add(backing_object, 1);
1886
1887 /*
1888 * scrap the paging_offset junk and do a
1889 * discrete copy. This also removes major
1890 * assumptions about how the swap-pager
1891 * works from where it doesn't belong. The
1892 * new swapper is able to optimize the
1893 * destroy-source case.
1894 */
984263bc 1895 vm_object_pip_add(object, 1);
b12defdc
MD
1896 swap_pager_copy(backing_object, object,
1897 OFF_TO_IDX(object->backing_object_offset),
1898 TRUE);
984263bc 1899 vm_object_pip_wakeup(object);
984263bc
MD
1900 vm_object_pip_wakeup(backing_object);
1901 }
b12defdc 1902
984263bc
MD
1903 /*
1904 * Object now shadows whatever backing_object did.
b12defdc 1905 * Remove object from backing_object's shadow_list.
984263bc 1906 */
984263bc 1907 LIST_REMOVE(object, shadow_list);
b12defdc
MD
1908 KKASSERT(object->backing_object == backing_object);
1909 backing_object->shadow_count--;
1910 backing_object->generation++;
1911
e806bedd
MD
1912 /*
1913 * backing_object->backing_object moves from within
1914 * backing_object to within object.
1915 */
b12defdc
MD
1916 while ((bbobj = backing_object->backing_object) != NULL) {
1917 vm_object_hold(bbobj);
1918 if (bbobj == backing_object->backing_object)
1919 break;
1920 vm_object_drop(bbobj);
1921 }
1922 if (bbobj) {
984263bc 1923 LIST_REMOVE(backing_object, shadow_list);
b12defdc
MD
1924 bbobj->shadow_count--;
1925 bbobj->generation++;
e806bedd 1926 backing_object->backing_object = NULL;
984263bc 1927 }
b12defdc
MD
1928 object->backing_object = bbobj;
1929 if (bbobj) {
1930 LIST_INSERT_HEAD(&bbobj->shadow_head,
1931 object, shadow_list);
1932 bbobj->shadow_count++;
1933 bbobj->generation++;
984263bc
MD
1934 }
1935
1936 object->backing_object_offset +=
b12defdc
MD
1937 backing_object->backing_object_offset;
1938
1939 vm_object_drop(bbobj);
984263bc
MD
1940
1941 /*
b12defdc
MD
1942 * Discard the old backing_object. Nothing should be
1943 * able to ref it, other than a vm_map_split(),
1944 * and vm_map_split() will stall on our chain lock.
1945 * And we control the parent so it shouldn't be
1946 * possible for it to go away either.
984263bc 1947 *
b12defdc
MD
1948 * Since the backing object has no pages, no pager
1949 * left, and no object references within it, all
1950 * that is necessary is to dispose of it.
984263bc 1951 */
2de4f77e
MD
1952 KASSERT(backing_object->ref_count == 1,
1953 ("backing_object %p was somehow "
1954 "re-referenced during collapse!",
1955 backing_object));
1956 KASSERT(RB_EMPTY(&backing_object->rb_memq),
1957 ("backing_object %p somehow has left "
1958 "over pages during collapse!",
1959 backing_object));
1960
e1c14c82 1961 /*
b12defdc
MD
1962 * The object can be destroyed.
1963 *
1964 * XXX just fall through and dodealloc instead
1965 * of forcing destruction?
e1c14c82 1966 */
b12defdc
MD
1967 --backing_object->ref_count;
1968 if ((backing_object->flags & OBJ_DEAD) == 0)
1969 vm_object_terminate(backing_object);
984263bc 1970 object_collapses++;
b12defdc 1971 dodealloc = 0;
984263bc 1972 } else {
984263bc
MD
1973 /*
1974 * If we do not entirely shadow the backing object,
1975 * there is nothing we can do so we give up.
1976 */
b12defdc
MD
1977 if (vm_object_backing_scan(object, backing_object,
1978 OBSC_TEST_ALL_SHADOWED) == 0) {
984263bc
MD
1979 break;
1980 }
1981
e806bedd
MD
1982 /*
1983 * bbobj is backing_object->backing_object. Since
1984 * object completely shadows backing_object we can
1985 * bypass it and become backed by bbobj instead.
1986 */
b12defdc
MD
1987 while ((bbobj = backing_object->backing_object) != NULL) {
1988 vm_object_hold(bbobj);
1989 if (bbobj == backing_object->backing_object)
1990 break;
1991 vm_object_drop(bbobj);
1992 }
e806bedd 1993
984263bc 1994 /*
e806bedd
MD
1995 * Make object shadow bbobj instead of backing_object.
1996 * Remove object from backing_object's shadow list.
b12defdc
MD
1997 *
1998 * Deallocating backing_object will not remove
984263bc
MD
1999 * it, since its reference count is at least 2.
2000 */
b12defdc 2001 KKASSERT(object->backing_object == backing_object);
984263bc
MD
2002 LIST_REMOVE(object, shadow_list);
2003 backing_object->shadow_count--;
2004 backing_object->generation++;
2005
b12defdc 2006 /*
e806bedd
MD
2007 * Add a ref to bbobj, bbobj now shadows object.
2008 *
2009 * NOTE: backing_object->backing_object still points
2010 * to bbobj. That relationship remains intact
2011 * because backing_object has > 1 ref, so
2012 * someone else is pointing to it (hence why
2013 * we can't collapse it into object and can
2014 * only handle the all-shadowed bypass case).
b12defdc
MD
2015 */
2016 if (bbobj) {
2017 vm_object_chain_wait(bbobj);
2018 vm_object_reference_locked(bbobj);
2019 LIST_INSERT_HEAD(&bbobj->shadow_head,
2020 object, shadow_list);
2021 bbobj->shadow_count++;
2022 bbobj->generation++;
984263bc
MD
2023 object->backing_object_offset +=
2024 backing_object->backing_object_offset;
b12defdc
MD
2025 object->backing_object = bbobj;
2026 vm_object_drop(bbobj);
2027 } else {
2028 object->backing_object = NULL;
984263bc
MD
2029 }
2030
2031 /*
b12defdc
MD
2032 * Drop the reference count on backing_object. To
2033 * handle ref_count races properly we can't assume
2034 * that the ref_count is still at least 2 so we
2035 * have to actually call vm_object_deallocate()
2036 * (after clearing the chainlock).
984263bc 2037 */
984263bc 2038 object_bypasses++;
b12defdc 2039 dodealloc = 1;
984263bc
MD
2040 }
2041
2042 /*
e806bedd
MD
2043 * Ok, we want to loop on the new object->bbobj association,
2044 * possibly collapsing it further. However if dodealloc is
2045 * non-zero we have to deallocate the backing_object which
2046 * itself can potentially undergo a collapse, creating a
2047 * recursion depth issue with the LWKT token subsystem.
2048 *
2049 * In the case where we must deallocate the backing_object
2050 * it is possible now that the backing_object has a single
2051 * shadow count on some other object (not represented here
2052 * as yet), since it no longer shadows us. Thus when we
2053 * call vm_object_deallocate() it may attempt to collapse
2054 * itself into its remaining parent.
984263bc 2055 */
e806bedd
MD
2056 if (dodealloc) {
2057 struct vm_object_dealloc_list *dtmp;
b12defdc 2058
e806bedd
MD
2059 vm_object_chain_release(backing_object);
2060 vm_object_unlock(backing_object);
2061 /* backing_object remains held */
2062
2063 /*
2064 * Auto-deallocation list for caller convenience.
2065 */
2066 if (dlistp == NULL)
2067 dlistp = &dlist;
2068
2069 dtmp = kmalloc(sizeof(*dtmp), M_TEMP, M_WAITOK);
2070 dtmp->object = backing_object;
2071 dtmp->next = *dlistp;
2072 *dlistp = dtmp;
2073 } else {
2074 vm_object_chain_release(backing_object);
2075 vm_object_drop(backing_object);
2076 }
2077 /* backing_object = NULL; not needed */
b12defdc
MD
2078 /* loop */
2079 }
2080
b59b553b
MD
2081 /*
2082 * Clean up any left over backing_object
2083 */
2084 if (backing_object) {
2085 vm_object_chain_release(backing_object);
2086 vm_object_drop(backing_object);
2087 }
2088
e806bedd
MD
2089 /*
2090 * Clean up any auto-deallocation list. This is a convenience
2091 * for top-level callers so they don't have to pass &dlist.
2092 * Do not clean up any caller-passed dlistp, the caller will
2093 * do that.
2094 */
2095 if (dlist)
2096 vm_object_deallocate_list(&dlist);
2097
984263bc
MD
2098}
2099
e806bedd
MD
2100/*
2101 * vm_object_collapse() may collect additional objects in need of
2102 * deallocation. This routine deallocates these objects. The
2103 * deallocation itself can trigger additional collapses (which the
2104 * deallocate function takes care of). This procedure is used to
2105 * reduce procedural recursion since these vm_object shadow chains
2106 * can become quite long.
2107 */
2108void
2109vm_object_deallocate_list(struct vm_object_dealloc_list **dlistp)
2110{
2111 struct vm_object_dealloc_list *dlist;
2112
2113 while ((dlist = *dlistp) != NULL) {
2114 *dlistp = dlist->next;
2115 vm_object_lock(dlist->object);
2116 vm_object_deallocate_locked(dlist->object);
2117 vm_object_drop(dlist->object);
2118 kfree(dlist, M_TEMP);
2119 }
2120}
2121
984263bc 2122/*
6846fd23
MD
2123 * Removes all physical pages in the specified object range from the
2124 * object's list of pages.
984263bc 2125 *
6846fd23 2126 * No requirements.
984263bc 2127 */
1f804340
MD
2128static int vm_object_page_remove_callback(vm_page_t p, void *data);
2129
984263bc 2130void
57e43348 2131vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1f804340 2132 boolean_t clean_only)
984263bc 2133{
1f804340 2134 struct rb_vm_page_scan_info info;
984263bc
MD
2135 int all;
2136
1f804340
MD
2137 /*
2138 * Degenerate cases and assertions
2139 */
b12defdc 2140 vm_object_hold(object);
b8158020
MD
2141 if (object == NULL ||
2142 (object->resident_page_count == 0 && object->swblock_count == 0)) {
b12defdc 2143 vm_object_drop(object);
984263bc 2144 return;
b8158020 2145 }
1f804340
MD
2146 KASSERT(object->type != OBJT_PHYS,
2147 ("attempt to remove pages from a physical object"));
984263bc 2148
1f804340
MD
2149 /*
2150 * Indicate that paging is occuring on the object
2151 */
1f804340 2152 vm_object_pip_add(object, 1);
984263bc
MD
2153
2154 /*
1f804340
MD
2155 * Figure out the actual removal range and whether we are removing
2156 * the entire contents of the object or not. If removing the entire
2157 * contents, be sure to get all pages, even those that might be
2158 * beyond the end of the object.
984263bc 2159 */
1f804340
MD
2160 info.start_pindex = start;
2161 if (end == 0)
2162 info.end_pindex = (vm_pindex_t)-1;
2163 else
2164 info.end_pindex = end - 1;
2165 info.limit = clean_only;
2166 all = (start == 0 && info.end_pindex >= object->size - 1);
984263bc 2167
06ecca5a 2168 /*
1f804340 2169 * Loop until we are sure we have gotten them all.
06ecca5a 2170 */
1f804340
MD
2171 do {
2172 info.error = 0;
2173 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2174 vm_object_page_remove_callback, &info);
2175 } while (info.error);
984263bc 2176
8d292090 2177 /*
5ac04117
MD
2178 * Remove any related swap if throwing away pages, or for
2179 * non-swap objects (the swap is a clean copy in that case).
8d292090 2180 */
5ac04117 2181 if (object->type != OBJT_SWAP || clean_only == FALSE) {
8d292090
MD
2182 if (all)
2183 swap_pager_freespace_all(object);
2184 else
2185 swap_pager_freespace(object, info.start_pindex,
2186 info.end_pindex - info.start_pindex + 1);
2187 }
2188
1f804340
MD
2189 /*
2190 * Cleanup
2191 */
2192 vm_object_pip_wakeup(object);
b12defdc 2193 vm_object_drop(object);
1f804340 2194}
984263bc 2195
6846fd23 2196/*
b12defdc 2197 * The caller must hold the object
6846fd23 2198 */
1f804340
MD
2199static int
2200vm_object_page_remove_callback(vm_page_t p, void *data)
2201{
2202 struct rb_vm_page_scan_info *info = data;
984263bc 2203
b12defdc
MD
2204 if (vm_page_busy_try(p, TRUE)) {
2205 vm_page_sleep_busy(p, TRUE, "vmopar");
2206 info->error = 1;
2207 return(0);
2208 }
2209
1f804340
MD
2210 /*
2211 * Wired pages cannot be destroyed, but they can be invalidated
2212 * and we do so if clean_only (limit) is not set.
1c9602b3
MD
2213 *
2214 * WARNING! The page may be wired due to being part of a buffer
2215 * cache buffer, and the buffer might be marked B_CACHE.
2216 * This is fine as part of a truncation but VFSs must be
2217 * sure to fix the buffer up when re-extending the file.
1f804340
MD
2218 */
2219 if (p->wire_count != 0) {
2220 vm_page_protect(p, VM_PROT_NONE);
2221 if (info->limit == 0)
2222 p->valid = 0;
b12defdc 2223 vm_page_wakeup(p);
1f804340
MD
2224 return(0);
2225 }
984263bc 2226
1f804340
MD
2227 /*
2228 * limit is our clean_only flag. If set and the page is dirty, do
d309a59d 2229 * not free it. If set and the page is being held by someone, do
1f804340
MD
2230 * not free it.
2231 */
2232 if (info->limit && p->valid) {
2233 vm_page_test_dirty(p);
b12defdc
MD
2234 if (p->valid & p->dirty) {
2235 vm_page_wakeup(p);
1f804340 2236 return(0);
b12defdc
MD
2237 }
2238#if 0
2239 if (p->hold_count) {
2240 vm_page_wakeup(p);
d309a59d 2241 return(0);
b12defdc
MD
2242 }
2243#endif
984263bc 2244 }
1f804340
MD
2245
2246 /*
2247 * Destroy the page
2248 */
1f804340
MD
2249 vm_page_protect(p, VM_PROT_NONE);
2250 vm_page_free(p);
2251 return(0);
984263bc
MD
2252}
2253
2254/*
6846fd23
MD
2255 * Coalesces two objects backing up adjoining regions of memory into a
2256 * single object.
984263bc 2257 *
6846fd23 2258 * returns TRUE if objects were combined.
984263bc 2259 *
6846fd23
MD
2260 * NOTE: Only works at the moment if the second object is NULL -
2261 * if it's not, which object do we lock first?
984263bc 2262 *
6846fd23
MD
2263 * Parameters:
2264 * prev_object First object to coalesce
2265 * prev_offset Offset into prev_object
2266 * next_object Second object into coalesce
2267 * next_offset Offset into next_object
984263bc 2268 *
6846fd23
MD
2269 * prev_size Size of reference to prev_object
2270 * next_size Size of reference to next_object
984263bc 2271 *
212f39f5
MD
2272 * The caller does not need to hold (prev_object) but must have a stable
2273 * pointer to it (typically by holding the vm_map locked).
984263bc
MD
2274 */
2275boolean_t
57e43348 2276vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex,
8d292090 2277 vm_size_t prev_size, vm_size_t next_size)
984263bc
MD
2278{
2279 vm_pindex_t next_pindex;
2280
b12defdc 2281 if (prev_object == NULL)
984263bc 2282 return (TRUE);
984263bc 2283
212f39f5
MD
2284 vm_object_hold(prev_object);
2285
984263bc
MD
2286 if (prev_object->type != OBJT_DEFAULT &&
2287 prev_object->type != OBJT_SWAP) {
212f39f5 2288 vm_object_drop(prev_object);
984263bc
MD
2289 return (FALSE);
2290 }
2291
2292 /*
2293 * Try to collapse the object first
2294 */
b12defdc 2295 vm_object_chain_acquire(prev_object);
e806bedd 2296 vm_object_collapse(prev_object, NULL);
984263bc
MD
2297
2298 /*
2299 * Can't coalesce if: . more than one reference . paged out . shadows
2300 * another object . has a copy elsewhere (any of which mean that the
2301 * pages not mapped to prev_entry may be in use anyway)
2302 */
2303
70de66db 2304 if (prev_object->backing_object != NULL) {
b12defdc 2305 vm_object_chain_release(prev_object);
212f39f5 2306 vm_object_drop(prev_object);
984263bc 2307 return (FALSE);
70de66db 2308 }
984263bc
MD
2309
2310 prev_size >>= PAGE_SHIFT;
2311 next_size >>= PAGE_SHIFT;
2312 next_pindex = prev_pindex + prev_size;
2313
2314 if ((prev_object->ref_count > 1) &&
2315 (prev_object->size != next_pindex)) {
b12defdc 2316 vm_object_chain_release(prev_object);
212f39f5 2317 vm_object_drop(prev_object);
984263bc
MD
2318 return (FALSE);
2319 }
2320
2321 /*
2322 * Remove any pages that may still be in the object from a previous
2323 * deallocation.
2324 */
2325 if (next_pindex < prev_object->size) {
2326 vm_object_page_remove(prev_object,
2327 next_pindex,
2328 next_pindex + next_size, FALSE);
2329 if (prev_object->type == OBJT_SWAP)
2330 swap_pager_freespace(prev_object,
2331 next_pindex, next_size);
2332 }
2333
2334 /*
2335 * Extend the object if necessary.
2336 */
2337 if (next_pindex + next_size > prev_object->size)
2338 prev_object->size = next_pindex + next_size;
70de66db 2339
b12defdc 2340 vm_object_chain_release(prev_object);
212f39f5 2341 vm_object_drop(prev_object);
984263bc
MD
2342 return (TRUE);
2343}
2344
6846fd23
MD
2345/*
2346 * Make the object writable and flag is being possibly dirty.
2347 *
b12defdc
MD
2348 * The caller must hold the object. XXX called from vm_page_dirty(),
2349 * There is currently no requirement to hold the object.
6846fd23 2350 */
984263bc
MD
2351void
2352vm_object_set_writeable_dirty(vm_object_t object)
2353{
2354 struct vnode *vp;
2355
b12defdc 2356 /*vm_object_assert_held(object);*/
984263bc
MD
2357 vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
2358 if (object->type == OBJT_VNODE &&
2359 (vp = (struct vnode *)object->handle) != NULL) {
2360 if ((vp->v_flag & VOBJDIRTY) == 0) {
5fd012e0 2361 vsetflags(vp, VOBJDIRTY);
984263bc
MD
2362 }
2363 }
2364}
2365
984263bc
MD
2366#include "opt_ddb.h"
2367#ifdef DDB
2368#include <sys/kernel.h>
2369
2370#include <sys/cons.h>
2371
2372#include <ddb/ddb.h>
2373
1388df65
RG
2374static int _vm_object_in_map (vm_map_t map, vm_object_t object,
2375 vm_map_entry_t entry);
2376static int vm_object_in_map (vm_object_t object);
984263bc 2377
6846fd23 2378/*
b12defdc 2379 * The caller must hold the object.
6846fd23 2380 */
984263bc 2381static int
57e43348 2382_vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
984263bc
MD
2383{
2384 vm_map_t tmpm;
2385 vm_map_entry_t tmpe;
b12defdc 2386 vm_object_t obj, nobj;
984263bc
MD
2387 int entcount;
2388
2389 if (map == 0)
2390 return 0;
984263bc
MD
2391 if (entry == 0) {
2392 tmpe = map->header.next;
2393 entcount = map->nentries;
2394 while (entcount-- && (tmpe != &map->header)) {
2395 if( _vm_object_in_map(map, object, tmpe)) {
2396 return 1;
2397 }
2398 tmpe = tmpe->next;
2399 }
1b874851
MD
2400 return (0);
2401 }
2402 switch(entry->maptype) {
2403 case VM_MAPTYPE_SUBMAP:
984263bc
MD
2404 tmpm = entry->object.sub_map;
2405 tmpe = tmpm->header.next;
2406 entcount = tmpm->nentries;
2407 while (entcount-- && tmpe != &tmpm->header) {
2408 if( _vm_object_in_map(tmpm, object, tmpe)) {
2409 return 1;
2410 }
2411 tmpe = tmpe->next;
2412 }
1b874851
MD
2413 break;
2414 case VM_MAPTYPE_NORMAL:
2415 case VM_MAPTYPE_VPAGETABLE:
2416 obj = entry->object.vm_object;
2417 while (obj) {
b12defdc
MD
2418 if (obj == object) {
2419 if (obj != entry->object.vm_object)
2420 vm_object_drop(obj);
984263bc 2421 return 1;
b12defdc
MD
2422 }
2423 while ((nobj = obj->backing_object) != NULL) {
2424 vm_object_hold(nobj);
2425 if (nobj == obj->backing_object)
2426 break;
2427 vm_object_drop(nobj);
2428 }
2429 if (obj != entry->object.vm_object) {
2430 if (nobj)
2431 vm_object_lock_swap();
2432 vm_object_drop(obj);
2433 }
2434 obj = nobj;
1b874851
MD
2435 }
2436 break;
2437 default:
2438 break;
984263bc
MD
2439 }
2440 return 0;
2441}
2442
8fa76237
MD
2443static int vm_object_in_map_callback(struct proc *p, void *data);
2444
2445struct vm_object_in_map_info {
2446 vm_object_t object;
2447 int rv;
2448};
2449
6846fd23
MD
2450/*
2451 * Debugging only
2452 */
984263bc 2453static int
57e43348 2454vm_object_in_map(vm_object_t object)
984263bc 2455{
8fa76237
MD
2456 struct vm_object_in_map_info info;
2457
2458 info.rv = 0;
2459 info.object = object;
2460
2461 allproc_scan(vm_object_in_map_callback, &info);
2462 if (info.rv)
2463 return 1;
e4846942 2464 if( _vm_object_in_map(&kernel_map, object, 0))
984263bc 2465 return 1;
e4846942 2466 if( _vm_object_in_map(&pager_map, object, 0))
984263bc 2467 return 1;
e4846942 2468 if( _vm_object_in_map(&buffer_map, object, 0))
984263bc 2469 return 1;
984263bc
MD
2470 return 0;
2471}
2472
6846fd23
MD
2473/*
2474 * Debugging only
2475 */
8fa76237
MD
2476static int
2477vm_object_in_map_callback(struct proc *p, void *data)
2478{
2479 struct vm_object_in_map_info *info = data;
2480
2481 if (p->p_vmspace) {
2482 if (_vm_object_in_map(&p->p_vmspace->vm_map, info->object, 0)) {
2483 info->rv = 1;
2484 return -1;
2485 }
2486 }
2487 return (0);
2488}
2489
984263bc
MD
2490DB_SHOW_COMMAND(vmochk, vm_object_check)
2491{
2492 vm_object_t object;
2493
2494 /*
2495 * make sure that internal objs are in a map somewhere
2496 * and none have zero ref counts.
2497 */
2498 for (object = TAILQ_FIRST(&vm_object_list);
2499 object != NULL;
2500 object = TAILQ_NEXT(object, object_list)) {
00a3fdca
MD
2501 if (object->type == OBJT_MARKER)
2502 continue;
984263bc
MD
2503 if (object->handle == NULL &&
2504 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2505 if (object->ref_count == 0) {
2506 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2507 (long)object->size);
2508 }
2509 if (!vm_object_in_map(object)) {
2510 db_printf(
2511 "vmochk: internal obj is not in a map: "
2512 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2513 object->ref_count, (u_long)object->size,
2514 (u_long)object->size,
2515 (void *)object->backing_object);
2516 }
2517 }
2518 }
2519}
2520
2521/*
6846fd23 2522 * Debugging only
984263bc
MD
2523 */
2524DB_SHOW_COMMAND(object, vm_object_print_static)
2525{
2526 /* XXX convert args. */
2527 vm_object_t object = (vm_object_t)addr;
2528 boolean_t full = have_addr;
2529
2530 vm_page_t p;
2531
2532 /* XXX count is an (unused) arg. Avoid shadowing it. */
2533#define count was_count
2534
2535 int count;
2536
2537 if (object == NULL)
2538 return;
2539
2540 db_iprintf(
2541 "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
2542 object, (int)object->type, (u_long)object->size,
2543 object->resident_page_count, object->ref_count, object->flags);
2544 /*
2545 * XXX no %qd in kernel. Truncate object->backing_object_offset.
2546 */
2547 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
2548 object->shadow_count,
2549 object->backing_object ? object->backing_object->ref_count : 0,
2550 object->backing_object, (long)object->backing_object_offset);
2551
2552 if (!full)
2553 return;
2554
2555 db_indent += 2;
2556 count = 0;
1f804340 2557 RB_FOREACH(p, vm_page_rb_tree, &object->rb_memq) {
984263bc
MD
2558 if (count == 0)
2559 db_iprintf("memory:=");
2560 else if (count == 6) {
2561 db_printf("\n");
2562 db_iprintf(" ...");
2563 count = 0;
2564 } else
2565 db_printf(",");
2566 count++;
2567
2568 db_printf("(off=0x%lx,page=0x%lx)",
2569 (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
2570 }
2571 if (count != 0)
2572 db_printf("\n");
2573 db_indent -= 2;
2574}
2575
2576/* XXX. */
2577#undef count
2578
6846fd23
MD
2579/*
2580 * XXX need this non-static entry for calling from vm_map_print.
2581 *
2582 * Debugging only
2583 */
984263bc 2584void
57e43348
MD
2585vm_object_print(/* db_expr_t */ long addr,
2586 boolean_t have_addr,
2587 /* db_expr_t */ long count,
2588 char *modif)
984263bc
MD
2589{
2590 vm_object_print_static(addr, have_addr, count, modif);
2591}
2592
6846fd23
MD
2593/*
2594 * Debugging only
2595 */
984263bc
MD
2596DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2597{
2598 vm_object_t object;
2599 int nl = 0;
2600 int c;
2601 for (object = TAILQ_FIRST(&vm_object_list);
2602 object != NULL;
2603 object = TAILQ_NEXT(object, object_list)) {
2604 vm_pindex_t idx, fidx;
2605 vm_pindex_t osize;
6ef943a3 2606 vm_paddr_t pa = -1, padiff;
984263bc
MD
2607 int rcount;
2608 vm_page_t m;
2609
00a3fdca
MD
2610 if (object->type == OBJT_MARKER)
2611 continue;
984263bc
MD
2612 db_printf("new object: %p\n", (void *)object);
2613 if ( nl > 18) {
2614 c = cngetc();
2615 if (c != ' ')
2616 return;
2617 nl = 0;
2618 }
2619 nl++;
2620 rcount = 0;
2621 fidx = 0;
2622 osize = object->size;
2623 if (osize > 128)
2624 osize = 128;
06ecca5a 2625 for (idx = 0; idx < osize; idx++) {
984263bc
MD
2626 m = vm_page_lookup(object, idx);
2627 if (m == NULL) {
2628 if (rcount) {
2629 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2630 (long)fidx, rcount, (long)pa);
2631 if ( nl > 18) {
2632 c = cngetc();
2633 if (c != ' ')
2634 return;
2635 nl = 0;
2636 }
2637 nl++;
2638 rcount = 0;
2639 }
2640 continue;
2641 }
2642
2643
2644 if (rcount &&
2645 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2646 ++rcount;
2647 continue;
2648 }
2649 if (rcount) {
2650 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2651 padiff >>= PAGE_SHIFT;
2652 padiff &= PQ_L2_MASK;
2653 if (padiff == 0) {
2654 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2655 ++rcount;
2656 continue;
2657 }
2658 db_printf(" index(%ld)run(%d)pa(0x%lx)",
2659 (long)fidx, rcount, (long)pa);
2660 db_printf("pd(%ld)\n", (long)padiff);
2661 if ( nl > 18) {
2662 c = cngetc();
2663 if (c != ' ')
2664 return;
2665 nl = 0;
2666 }
2667 nl++;
2668 }
2669 fidx = idx;
2670 pa = VM_PAGE_TO_PHYS(m);
2671 rcount = 1;
2672 }
2673 if (rcount) {
2674 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2675 (long)fidx, rcount, (long)pa);
2676 if ( nl > 18) {
2677 c = cngetc();
2678 if (c != ' ')
2679 return;
2680 nl = 0;
2681 }
2682 nl++;
2683 }
2684 }
2685}
2686#endif /* DDB */