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