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