]> git.saurik.com Git - apple/xnu.git/blame - osfmk/vm/vm_object.c
xnu-4570.1.46.tar.gz
[apple/xnu.git] / osfmk / vm / vm_object.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56/*
57 */
58/*
59 * File: vm/vm_object.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
61 *
62 * Virtual memory object module.
63 */
64
2d21ac55 65#include <debug.h>
1c79356b
A
66#include <mach_pagemap.h>
67#include <task_swapper.h>
68
0b4e3aa0 69#include <mach/mach_types.h>
1c79356b
A
70#include <mach/memory_object.h>
71#include <mach/memory_object_default.h>
72#include <mach/memory_object_control_server.h>
73#include <mach/vm_param.h>
91447636 74
316670eb
A
75#include <mach/sdt.h>
76
91447636 77#include <ipc/ipc_types.h>
1c79356b 78#include <ipc/ipc_port.h>
91447636
A
79
80#include <kern/kern_types.h>
1c79356b 81#include <kern/assert.h>
1c79356b
A
82#include <kern/queue.h>
83#include <kern/xpr.h>
6d2010ae 84#include <kern/kalloc.h>
1c79356b
A
85#include <kern/zalloc.h>
86#include <kern/host.h>
87#include <kern/host_statistics.h>
88#include <kern/processor.h>
91447636 89#include <kern/misc_protos.h>
39037602 90#include <kern/policy_internal.h>
91447636 91
1c79356b 92#include <vm/memory_object.h>
39236c6e 93#include <vm/vm_compressor_pager.h>
1c79356b
A
94#include <vm/vm_fault.h>
95#include <vm/vm_map.h>
96#include <vm/vm_object.h>
97#include <vm/vm_page.h>
98#include <vm/vm_pageout.h>
91447636 99#include <vm/vm_protos.h>
2d21ac55 100#include <vm/vm_purgeable_internal.h>
1c79356b 101
39236c6e
A
102#include <vm/vm_compressor.h>
103
fe8ab488
A
104#if CONFIG_PHANTOM_CACHE
105#include <vm/vm_phantom_cache.h>
106#endif
107
108boolean_t vm_object_collapse_compressor_allowed = TRUE;
109
110struct vm_counters vm_counters;
111
112#if VM_OBJECT_TRACKING
113boolean_t vm_object_tracking_inited = FALSE;
fe8ab488 114btlog_t *vm_object_tracking_btlog;
39037602 115
fe8ab488
A
116void
117vm_object_tracking_init(void)
118{
119 int vm_object_tracking;
120
121 vm_object_tracking = 1;
122 PE_parse_boot_argn("vm_object_tracking", &vm_object_tracking,
123 sizeof (vm_object_tracking));
124
125 if (vm_object_tracking) {
fe8ab488 126 vm_object_tracking_btlog = btlog_create(
39037602 127 VM_OBJECT_TRACKING_NUM_RECORDS,
fe8ab488 128 VM_OBJECT_TRACKING_BTDEPTH,
39037602 129 TRUE /* caller_will_remove_entries_for_element? */);
fe8ab488
A
130 assert(vm_object_tracking_btlog);
131 vm_object_tracking_inited = TRUE;
132 }
133}
134#endif /* VM_OBJECT_TRACKING */
135
1c79356b
A
136/*
137 * Virtual memory objects maintain the actual data
138 * associated with allocated virtual memory. A given
139 * page of memory exists within exactly one object.
140 *
141 * An object is only deallocated when all "references"
0b4e3aa0 142 * are given up.
1c79356b
A
143 *
144 * Associated with each object is a list of all resident
145 * memory pages belonging to that object; this list is
146 * maintained by the "vm_page" module, but locked by the object's
147 * lock.
148 *
0b4e3aa0 149 * Each object also records the memory object reference
1c79356b 150 * that is used by the kernel to request and write
0b4e3aa0 151 * back data (the memory object, field "pager"), etc...
1c79356b
A
152 *
153 * Virtual memory objects are allocated to provide
154 * zero-filled memory (vm_allocate) or map a user-defined
155 * memory object into a virtual address space (vm_map).
156 *
157 * Virtual memory objects that refer to a user-defined
158 * memory object are called "permanent", because all changes
159 * made in virtual memory are reflected back to the
160 * memory manager, which may then store it permanently.
161 * Other virtual memory objects are called "temporary",
162 * meaning that changes need be written back only when
163 * necessary to reclaim pages, and that storage associated
164 * with the object can be discarded once it is no longer
165 * mapped.
166 *
167 * A permanent memory object may be mapped into more
168 * than one virtual address space. Moreover, two threads
169 * may attempt to make the first mapping of a memory
170 * object concurrently. Only one thread is allowed to
171 * complete this mapping; all others wait for the
172 * "pager_initialized" field is asserted, indicating
173 * that the first thread has initialized all of the
174 * necessary fields in the virtual memory object structure.
175 *
176 * The kernel relies on a *default memory manager* to
177 * provide backing storage for the zero-filled virtual
0b4e3aa0 178 * memory objects. The pager memory objects associated
1c79356b 179 * with these temporary virtual memory objects are only
0b4e3aa0
A
180 * requested from the default memory manager when it
181 * becomes necessary. Virtual memory objects
1c79356b
A
182 * that depend on the default memory manager are called
183 * "internal". The "pager_created" field is provided to
184 * indicate whether these ports have ever been allocated.
185 *
186 * The kernel may also create virtual memory objects to
187 * hold changed pages after a copy-on-write operation.
188 * In this case, the virtual memory object (and its
189 * backing storage -- its memory object) only contain
190 * those pages that have been changed. The "shadow"
191 * field refers to the virtual memory object that contains
192 * the remainder of the contents. The "shadow_offset"
193 * field indicates where in the "shadow" these contents begin.
194 * The "copy" field refers to a virtual memory object
195 * to which changed pages must be copied before changing
196 * this object, in order to implement another form
197 * of copy-on-write optimization.
198 *
199 * The virtual memory object structure also records
200 * the attributes associated with its memory object.
201 * The "pager_ready", "can_persist" and "copy_strategy"
202 * fields represent those attributes. The "cached_list"
203 * field is used in the implementation of the persistence
204 * attribute.
205 *
206 * ZZZ Continue this comment.
207 */
208
209/* Forward declarations for internal functions. */
0b4e3aa0 210static kern_return_t vm_object_terminate(
1c79356b
A
211 vm_object_t object);
212
0b4e3aa0 213static kern_return_t vm_object_copy_call(
1c79356b
A
214 vm_object_t src_object,
215 vm_object_offset_t src_offset,
216 vm_object_size_t size,
217 vm_object_t *_result_object);
218
0b4e3aa0 219static void vm_object_do_collapse(
1c79356b
A
220 vm_object_t object,
221 vm_object_t backing_object);
222
0b4e3aa0 223static void vm_object_do_bypass(
1c79356b
A
224 vm_object_t object,
225 vm_object_t backing_object);
226
0b4e3aa0 227static void vm_object_release_pager(
5ba3f43e 228 memory_object_t pager);
1c79356b 229
5ba3f43e 230zone_t vm_object_zone; /* vm backing store zone */
1c79356b
A
231
232/*
233 * All wired-down kernel memory belongs to a single virtual
234 * memory object (kernel_object) to avoid wasting data structures.
235 */
39037602
A
236static struct vm_object kernel_object_store __attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT)));
237vm_object_t kernel_object;
1c79356b 238
39037602 239static struct vm_object compressor_object_store __attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT)));
39236c6e 240vm_object_t compressor_object = &compressor_object_store;
2d21ac55 241
1c79356b
A
242/*
243 * The submap object is used as a placeholder for vm_map_submap
244 * operations. The object is declared in vm_map.c because it
245 * is exported by the vm_map module. The storage is declared
246 * here because it must be initialized here.
247 */
39037602 248static struct vm_object vm_submap_object_store __attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT)));
1c79356b
A
249
250/*
251 * Virtual memory objects are initialized from
252 * a template (see vm_object_allocate).
253 *
254 * When adding a new field to the virtual memory
255 * object structure, be sure to add initialization
0b4e3aa0 256 * (see _vm_object_allocate()).
1c79356b 257 */
0b4e3aa0 258static struct vm_object vm_object_template;
1c79356b 259
b0d623f7
A
260unsigned int vm_page_purged_wired = 0;
261unsigned int vm_page_purged_busy = 0;
262unsigned int vm_page_purged_others = 0;
263
6d2010ae
A
264static queue_head_t vm_object_cached_list;
265static uint32_t vm_object_cache_pages_freed = 0;
266static uint32_t vm_object_cache_pages_moved = 0;
267static uint32_t vm_object_cache_pages_skipped = 0;
268static uint32_t vm_object_cache_adds = 0;
269static uint32_t vm_object_cached_count = 0;
270static lck_mtx_t vm_object_cached_lock_data;
271static lck_mtx_ext_t vm_object_cached_lock_data_ext;
272
273static uint32_t vm_object_page_grab_failed = 0;
274static uint32_t vm_object_page_grab_skipped = 0;
275static uint32_t vm_object_page_grab_returned = 0;
276static uint32_t vm_object_page_grab_pmapped = 0;
277static uint32_t vm_object_page_grab_reactivations = 0;
278
b0d623f7
A
279#define vm_object_cache_lock_spin() \
280 lck_mtx_lock_spin(&vm_object_cached_lock_data)
1c79356b 281#define vm_object_cache_unlock() \
b0d623f7
A
282 lck_mtx_unlock(&vm_object_cached_lock_data)
283
6d2010ae 284static void vm_object_cache_remove_locked(vm_object_t);
b0d623f7 285
1c79356b 286
8f6c56a5
A
287static void vm_object_reap(vm_object_t object);
288static void vm_object_reap_async(vm_object_t object);
289static void vm_object_reaper_thread(void);
b0d623f7
A
290
291static lck_mtx_t vm_object_reaper_lock_data;
292static lck_mtx_ext_t vm_object_reaper_lock_data_ext;
293
294static queue_head_t vm_object_reaper_queue; /* protected by vm_object_reaper_lock() */
8f6c56a5
A
295unsigned int vm_object_reap_count = 0;
296unsigned int vm_object_reap_count_async = 0;
297
b0d623f7
A
298#define vm_object_reaper_lock() \
299 lck_mtx_lock(&vm_object_reaper_lock_data)
300#define vm_object_reaper_lock_spin() \
301 lck_mtx_lock_spin(&vm_object_reaper_lock_data)
302#define vm_object_reaper_unlock() \
303 lck_mtx_unlock(&vm_object_reaper_lock_data)
304
fe8ab488
A
305#if CONFIG_IOSCHED
306/* I/O Re-prioritization request list */
307queue_head_t io_reprioritize_list;
308lck_spin_t io_reprioritize_list_lock;
309
310#define IO_REPRIORITIZE_LIST_LOCK() \
311 lck_spin_lock(&io_reprioritize_list_lock)
312#define IO_REPRIORITIZE_LIST_UNLOCK() \
313 lck_spin_unlock(&io_reprioritize_list_lock)
314
315#define MAX_IO_REPRIORITIZE_REQS 8192
316zone_t io_reprioritize_req_zone;
317
318/* I/O Re-prioritization thread */
319int io_reprioritize_wakeup = 0;
320static void io_reprioritize_thread(void *param __unused, wait_result_t wr __unused);
321
322#define IO_REPRIO_THREAD_WAKEUP() thread_wakeup((event_t)&io_reprioritize_wakeup)
323#define IO_REPRIO_THREAD_CONTINUATION() \
324{ \
325 assert_wait(&io_reprioritize_wakeup, THREAD_UNINT); \
326 thread_block(io_reprioritize_thread); \
327}
328
329void vm_page_request_reprioritize(vm_object_t, uint64_t, uint32_t, int);
330void vm_page_handle_prio_inversion(vm_object_t, vm_page_t);
331void vm_decmp_upl_reprioritize(upl_t, int);
332#endif
333
6d2010ae
A
334#if 0
335#undef KERNEL_DEBUG
336#define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
337#endif
b0d623f7
A
338
339
1c79356b
A
340/*
341 * vm_object_allocate:
342 *
343 * Returns a new object with the given size.
344 */
345
91447636 346__private_extern__ void
1c79356b
A
347_vm_object_allocate(
348 vm_object_size_t size,
349 vm_object_t object)
350{
351 XPR(XPR_VM_OBJECT,
352 "vm_object_allocate, object 0x%X size 0x%X\n",
b0d623f7 353 object, size, 0,0,0);
1c79356b
A
354
355 *object = vm_object_template;
39037602 356 vm_page_queue_init(&object->memq);
fe8ab488 357#if UPL_DEBUG || CONFIG_IOSCHED
1c79356b 358 queue_init(&object->uplq);
fe8ab488 359#endif
1c79356b 360 vm_object_lock_init(object);
6d2010ae 361 object->vo_size = size;
fe8ab488
A
362
363#if VM_OBJECT_TRACKING_OP_CREATED
364 if (vm_object_tracking_inited) {
365 void *bt[VM_OBJECT_TRACKING_BTDEPTH];
366 int numsaved = 0;
367
368 numsaved = OSBacktrace(bt, VM_OBJECT_TRACKING_BTDEPTH);
369 btlog_add_entry(vm_object_tracking_btlog,
370 object,
371 VM_OBJECT_TRACKING_OP_CREATED,
372 bt,
373 numsaved);
374 }
375#endif /* VM_OBJECT_TRACKING_OP_CREATED */
1c79356b
A
376}
377
0b4e3aa0 378__private_extern__ vm_object_t
1c79356b
A
379vm_object_allocate(
380 vm_object_size_t size)
381{
39037602 382 vm_object_t object;
1c79356b
A
383
384 object = (vm_object_t) zalloc(vm_object_zone);
385
0b4e3aa0
A
386// dbgLog(object, size, 0, 2); /* (TEST/DEBUG) */
387
388 if (object != VM_OBJECT_NULL)
389 _vm_object_allocate(size, object);
1c79356b
A
390
391 return object;
392}
393
2d21ac55 394
b0d623f7 395lck_grp_t vm_object_lck_grp;
6d2010ae
A
396lck_grp_t vm_object_cache_lck_grp;
397lck_grp_attr_t vm_object_lck_grp_attr;
b0d623f7
A
398lck_attr_t vm_object_lck_attr;
399lck_attr_t kernel_object_lck_attr;
39236c6e 400lck_attr_t compressor_object_lck_attr;
2d21ac55 401
1c79356b
A
402/*
403 * vm_object_bootstrap:
404 *
405 * Initialize the VM objects module.
406 */
0b4e3aa0 407__private_extern__ void
1c79356b
A
408vm_object_bootstrap(void)
409{
39037602
A
410 vm_size_t vm_object_size;
411
5ba3f43e
A
412 assert(sizeof (mo_ipc_object_bits_t) == sizeof (ipc_object_bits_t));
413
39037602 414 vm_object_size = (sizeof(struct vm_object) + (VM_PACKED_POINTER_ALIGNMENT-1)) & ~(VM_PACKED_POINTER_ALIGNMENT - 1);
1c79356b 415
39037602
A
416 vm_object_zone = zinit(vm_object_size,
417 round_page(512*1024),
418 round_page(12*1024),
419 "vm objects");
6d2010ae 420 zone_change(vm_object_zone, Z_CALLERACCT, FALSE); /* don't charge caller */
0b4c1975 421 zone_change(vm_object_zone, Z_NOENCRYPT, TRUE);
5ba3f43e 422 zone_change(vm_object_zone, Z_ALIGNMENT_REQUIRED, TRUE);
1c79356b 423
b0d623f7
A
424 vm_object_init_lck_grp();
425
1c79356b 426 queue_init(&vm_object_cached_list);
b0d623f7
A
427
428 lck_mtx_init_ext(&vm_object_cached_lock_data,
429 &vm_object_cached_lock_data_ext,
6d2010ae 430 &vm_object_cache_lck_grp,
b0d623f7 431 &vm_object_lck_attr);
6d2010ae 432
b0d623f7
A
433 queue_init(&vm_object_reaper_queue);
434
b0d623f7
A
435 lck_mtx_init_ext(&vm_object_reaper_lock_data,
436 &vm_object_reaper_lock_data_ext,
437 &vm_object_lck_grp,
438 &vm_object_lck_attr);
1c79356b 439
2d21ac55 440
1c79356b
A
441 /*
442 * Fill in a template object, for quick initialization
443 */
444
445 /* memq; Lock; init after allocation */
39037602 446
39037602
A
447 vm_object_template.memq.prev = 0;
448 vm_object_template.memq.next = 0;
2d21ac55
A
449#if 0
450 /*
451 * We can't call vm_object_lock_init() here because that will
452 * allocate some memory and VM is not fully initialized yet.
b0d623f7 453 * The lock will be initialized for each allocated object in
2d21ac55
A
454 * _vm_object_allocate(), so we don't need to initialize it in
455 * the vm_object_template.
456 */
457 vm_object_lock_init(&vm_object_template);
39037602
A
458#endif
459#if DEVELOPMENT || DEBUG
460 vm_object_template.Lock_owner = 0;
2d21ac55 461#endif
6d2010ae 462 vm_object_template.vo_size = 0;
91447636 463 vm_object_template.memq_hint = VM_PAGE_NULL;
1c79356b
A
464 vm_object_template.ref_count = 1;
465#if TASK_SWAPPER
466 vm_object_template.res_count = 1;
467#endif /* TASK_SWAPPER */
468 vm_object_template.resident_page_count = 0;
5ba3f43e
A
469 // static vm_object_template is zeroed
470 // vm_object_template.wired_page_count = 0;
b0d623f7 471 vm_object_template.reusable_page_count = 0;
1c79356b
A
472 vm_object_template.copy = VM_OBJECT_NULL;
473 vm_object_template.shadow = VM_OBJECT_NULL;
6d2010ae 474 vm_object_template.vo_shadow_offset = (vm_object_offset_t) 0;
0b4e3aa0 475 vm_object_template.pager = MEMORY_OBJECT_NULL;
1c79356b 476 vm_object_template.paging_offset = 0;
91447636 477 vm_object_template.pager_control = MEMORY_OBJECT_CONTROL_NULL;
1c79356b 478 vm_object_template.copy_strategy = MEMORY_OBJECT_COPY_SYMMETRIC;
1c79356b 479 vm_object_template.paging_in_progress = 0;
fe8ab488
A
480#if __LP64__
481 vm_object_template.__object1_unused_bits = 0;
482#endif /* __LP64__ */
b0d623f7 483 vm_object_template.activity_in_progress = 0;
1c79356b
A
484
485 /* Begin bitfields */
486 vm_object_template.all_wanted = 0; /* all bits FALSE */
487 vm_object_template.pager_created = FALSE;
488 vm_object_template.pager_initialized = FALSE;
489 vm_object_template.pager_ready = FALSE;
490 vm_object_template.pager_trusted = FALSE;
491 vm_object_template.can_persist = FALSE;
492 vm_object_template.internal = TRUE;
1c79356b
A
493 vm_object_template.private = FALSE;
494 vm_object_template.pageout = FALSE;
495 vm_object_template.alive = TRUE;
2d21ac55 496 vm_object_template.purgable = VM_PURGABLE_DENY;
39236c6e 497 vm_object_template.purgeable_when_ripe = FALSE;
5ba3f43e 498 vm_object_template.purgeable_only_by_kernel = FALSE;
2d21ac55 499 vm_object_template.shadowed = FALSE;
2d21ac55 500 vm_object_template.true_share = FALSE;
1c79356b 501 vm_object_template.terminating = FALSE;
2d21ac55 502 vm_object_template.named = FALSE;
1c79356b
A
503 vm_object_template.shadow_severed = FALSE;
504 vm_object_template.phys_contiguous = FALSE;
0b4e3aa0 505 vm_object_template.nophyscache = FALSE;
1c79356b
A
506 /* End bitfields */
507
2d21ac55
A
508 vm_object_template.cached_list.prev = NULL;
509 vm_object_template.cached_list.next = NULL;
2d21ac55 510
1c79356b 511 vm_object_template.last_alloc = (vm_object_offset_t) 0;
2d21ac55
A
512 vm_object_template.sequential = (vm_object_offset_t) 0;
513 vm_object_template.pages_created = 0;
514 vm_object_template.pages_used = 0;
6d2010ae 515 vm_object_template.scan_collisions = 0;
fe8ab488
A
516#if CONFIG_PHANTOM_CACHE
517 vm_object_template.phantom_object_id = 0;
518#endif
2d21ac55 519 vm_object_template.cow_hint = ~(vm_offset_t)0;
1c79356b 520
2d21ac55 521 /* cache bitfields */
6d2010ae
A
522 vm_object_template.wimg_bits = VM_WIMG_USE_DEFAULT;
523 vm_object_template.set_cache_attr = FALSE;
39236c6e 524 vm_object_template.object_slid = FALSE;
2d21ac55 525 vm_object_template.code_signed = FALSE;
b0d623f7 526 vm_object_template.transposed = FALSE;
593a1d5f 527 vm_object_template.mapping_in_progress = FALSE;
fe8ab488 528 vm_object_template.phantom_isssd = FALSE;
b0d623f7
A
529 vm_object_template.volatile_empty = FALSE;
530 vm_object_template.volatile_fault = FALSE;
531 vm_object_template.all_reusable = FALSE;
532 vm_object_template.blocked_access = FALSE;
533 vm_object_template.__object2_unused_bits = 0;
fe8ab488 534#if CONFIG_IOSCHED || UPL_DEBUG
2d21ac55
A
535 vm_object_template.uplq.prev = NULL;
536 vm_object_template.uplq.next = NULL;
537#endif /* UPL_DEBUG */
538#ifdef VM_PIP_DEBUG
539 bzero(&vm_object_template.pip_holders,
540 sizeof (vm_object_template.pip_holders));
541#endif /* VM_PIP_DEBUG */
542
fe8ab488
A
543 vm_object_template.objq.next = NULL;
544 vm_object_template.objq.prev = NULL;
2d21ac55 545
39236c6e
A
546 vm_object_template.purgeable_queue_type = PURGEABLE_Q_TYPE_MAX;
547 vm_object_template.purgeable_queue_group = 0;
548
6d2010ae 549 vm_object_template.vo_cache_ts = 0;
3e170ce0
A
550
551 vm_object_template.wire_tag = VM_KERN_MEMORY_NONE;
39037602
A
552
553 vm_object_template.io_tracking = FALSE;
554
555#if CONFIG_SECLUDED_MEMORY
556 vm_object_template.eligible_for_secluded = FALSE;
557 vm_object_template.can_grab_secluded = FALSE;
558#else /* CONFIG_SECLUDED_MEMORY */
559 vm_object_template.__object3_unused_bits = 0;
560#endif /* CONFIG_SECLUDED_MEMORY */
2d21ac55 561
fe8ab488
A
562#if DEBUG
563 bzero(&vm_object_template.purgeable_owner_bt[0],
564 sizeof (vm_object_template.purgeable_owner_bt));
565 vm_object_template.vo_purgeable_volatilizer = NULL;
566 bzero(&vm_object_template.purgeable_volatilizer_bt[0],
567 sizeof (vm_object_template.purgeable_volatilizer_bt));
568#endif /* DEBUG */
569
1c79356b
A
570 /*
571 * Initialize the "kernel object"
572 */
573
574 kernel_object = &kernel_object_store;
575
576/*
577 * Note that in the following size specifications, we need to add 1 because
55e303ae 578 * VM_MAX_KERNEL_ADDRESS (vm_last_addr) is a maximum address, not a size.
1c79356b 579 */
55e303ae 580
b0d623f7
A
581 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS + 1,
582 kernel_object);
39236c6e
A
583
584 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS + 1,
585 compressor_object);
55e303ae 586 kernel_object->copy_strategy = MEMORY_OBJECT_COPY_NONE;
39236c6e 587 compressor_object->copy_strategy = MEMORY_OBJECT_COPY_NONE;
5ba3f43e 588 kernel_object->no_tag_update = TRUE;
1c79356b
A
589
590 /*
591 * Initialize the "submap object". Make it as large as the
592 * kernel object so that no limit is imposed on submap sizes.
593 */
594
595 vm_submap_object = &vm_submap_object_store;
b0d623f7
A
596 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS + 1,
597 vm_submap_object);
55e303ae
A
598 vm_submap_object->copy_strategy = MEMORY_OBJECT_COPY_NONE;
599
1c79356b
A
600 /*
601 * Create an "extra" reference to this object so that we never
602 * try to deallocate it; zfree doesn't like to be called with
603 * non-zone memory.
604 */
605 vm_object_reference(vm_submap_object);
1c79356b
A
606}
607
fe8ab488
A
608#if CONFIG_IOSCHED
609void
610vm_io_reprioritize_init(void)
611{
612 kern_return_t result;
613 thread_t thread = THREAD_NULL;
614
615 /* Initialze the I/O reprioritization subsystem */
616 lck_spin_init(&io_reprioritize_list_lock, &vm_object_lck_grp, &vm_object_lck_attr);
617 queue_init(&io_reprioritize_list);
618
619 io_reprioritize_req_zone = zinit(sizeof(struct io_reprioritize_req),
620 MAX_IO_REPRIORITIZE_REQS * sizeof(struct io_reprioritize_req),
813fb2f6
A
621 4096, "io_reprioritize_req");
622 zone_change(io_reprioritize_req_zone, Z_COLLECT, FALSE);
fe8ab488
A
623
624 result = kernel_thread_start_priority(io_reprioritize_thread, NULL, 95 /* MAXPRI_KERNEL */, &thread);
625 if (result == KERN_SUCCESS) {
626 thread_deallocate(thread);
627 } else {
628 panic("Could not create io_reprioritize_thread");
629 }
630}
631#endif
632
8f6c56a5
A
633void
634vm_object_reaper_init(void)
635{
636 kern_return_t kr;
637 thread_t thread;
638
8f6c56a5
A
639 kr = kernel_thread_start_priority(
640 (thread_continue_t) vm_object_reaper_thread,
641 NULL,
5ba3f43e 642 BASEPRI_VM,
8f6c56a5
A
643 &thread);
644 if (kr != KERN_SUCCESS) {
2d21ac55 645 panic("failed to launch vm_object_reaper_thread kr=0x%x", kr);
8f6c56a5
A
646 }
647 thread_deallocate(thread);
648}
649
0b4e3aa0 650__private_extern__ void
1c79356b
A
651vm_object_init(void)
652{
653 /*
654 * Finish initializing the kernel object.
655 */
656}
657
2d21ac55
A
658
659__private_extern__ void
660vm_object_init_lck_grp(void)
661{
b0d623f7 662 /*
2d21ac55
A
663 * initialze the vm_object lock world
664 */
b0d623f7 665 lck_grp_attr_setdefault(&vm_object_lck_grp_attr);
2d21ac55 666 lck_grp_init(&vm_object_lck_grp, "vm_object", &vm_object_lck_grp_attr);
6d2010ae 667 lck_grp_init(&vm_object_cache_lck_grp, "vm_object_cache", &vm_object_lck_grp_attr);
2d21ac55
A
668 lck_attr_setdefault(&vm_object_lck_attr);
669 lck_attr_setdefault(&kernel_object_lck_attr);
670 lck_attr_cleardebug(&kernel_object_lck_attr);
39236c6e
A
671 lck_attr_setdefault(&compressor_object_lck_attr);
672 lck_attr_cleardebug(&compressor_object_lck_attr);
2d21ac55
A
673}
674
1c79356b
A
675
676/*
677 * vm_object_deallocate:
678 *
679 * Release a reference to the specified object,
680 * gained either through a vm_object_allocate
681 * or a vm_object_reference call. When all references
682 * are gone, storage associated with this object
683 * may be relinquished.
684 *
685 * No object may be locked.
686 */
2d21ac55
A
687unsigned long vm_object_deallocate_shared_successes = 0;
688unsigned long vm_object_deallocate_shared_failures = 0;
689unsigned long vm_object_deallocate_shared_swap_failures = 0;
3e170ce0 690
0b4e3aa0 691__private_extern__ void
1c79356b 692vm_object_deallocate(
39037602 693 vm_object_t object)
1c79356b 694{
b0d623f7 695 vm_object_t shadow = VM_OBJECT_NULL;
1c79356b
A
696
697// if(object)dbgLog(object, object->ref_count, object->can_persist, 3); /* (TEST/DEBUG) */
698// else dbgLog(object, 0, 0, 3); /* (TEST/DEBUG) */
699
2d21ac55
A
700 if (object == VM_OBJECT_NULL)
701 return;
702
39236c6e 703 if (object == kernel_object || object == compressor_object) {
b0d623f7
A
704 vm_object_lock_shared(object);
705
706 OSAddAtomic(-1, &object->ref_count);
707
708 if (object->ref_count == 0) {
39236c6e
A
709 if (object == kernel_object)
710 panic("vm_object_deallocate: losing kernel_object\n");
711 else
712 panic("vm_object_deallocate: losing compressor_object\n");
2d21ac55 713 }
b0d623f7 714 vm_object_unlock(object);
2d21ac55
A
715 return;
716 }
717
fe8ab488
A
718 if (object->ref_count == 2 &&
719 object->named) {
720 /*
721 * This "named" object's reference count is about to
722 * drop from 2 to 1:
723 * we'll need to call memory_object_last_unmap().
724 */
725 } else if (object->ref_count == 2 &&
726 object->internal &&
727 object->shadow != VM_OBJECT_NULL) {
728 /*
729 * This internal object's reference count is about to
730 * drop from 2 to 1 and it has a shadow object:
731 * we'll want to try and collapse this object with its
732 * shadow.
733 */
734 } else if (object->ref_count >= 2) {
2d21ac55
A
735 UInt32 original_ref_count;
736 volatile UInt32 *ref_count_p;
737 Boolean atomic_swap;
738
739 /*
740 * The object currently looks like it is not being
741 * kept alive solely by the reference we're about to release.
742 * Let's try and release our reference without taking
743 * all the locks we would need if we had to terminate the
744 * object (cache lock + exclusive object lock).
745 * Lock the object "shared" to make sure we don't race with
746 * anyone holding it "exclusive".
747 */
748 vm_object_lock_shared(object);
749 ref_count_p = (volatile UInt32 *) &object->ref_count;
750 original_ref_count = object->ref_count;
751 /*
752 * Test again as "ref_count" could have changed.
753 * "named" shouldn't change.
754 */
fe8ab488
A
755 if (original_ref_count == 2 &&
756 object->named) {
757 /* need to take slow path for m_o_last_unmap() */
758 atomic_swap = FALSE;
759 } else if (original_ref_count == 2 &&
760 object->internal &&
761 object->shadow != VM_OBJECT_NULL) {
762 /* need to take slow path for vm_object_collapse() */
763 atomic_swap = FALSE;
764 } else if (original_ref_count < 2) {
765 /* need to take slow path for vm_object_terminate() */
766 atomic_swap = FALSE;
767 } else {
768 /* try an atomic update with the shared lock */
2d21ac55
A
769 atomic_swap = OSCompareAndSwap(
770 original_ref_count,
771 original_ref_count - 1,
772 (UInt32 *) &object->ref_count);
773 if (atomic_swap == FALSE) {
774 vm_object_deallocate_shared_swap_failures++;
fe8ab488 775 /* fall back to the slow path... */
2d21ac55 776 }
2d21ac55 777 }
fe8ab488 778
2d21ac55
A
779 vm_object_unlock(object);
780
781 if (atomic_swap) {
b0d623f7
A
782 /*
783 * ref_count was updated atomically !
784 */
2d21ac55
A
785 vm_object_deallocate_shared_successes++;
786 return;
787 }
788
789 /*
790 * Someone else updated the ref_count at the same
791 * time and we lost the race. Fall back to the usual
792 * slow but safe path...
793 */
794 vm_object_deallocate_shared_failures++;
795 }
1c79356b
A
796
797 while (object != VM_OBJECT_NULL) {
798
b0d623f7 799 vm_object_lock(object);
2d21ac55 800
0b4e3aa0
A
801 assert(object->ref_count > 0);
802
803 /*
804 * If the object has a named reference, and only
805 * that reference would remain, inform the pager
806 * about the last "mapping" reference going away.
807 */
808 if ((object->ref_count == 2) && (object->named)) {
809 memory_object_t pager = object->pager;
810
811 /* Notify the Pager that there are no */
812 /* more mappers for this object */
813
814 if (pager != MEMORY_OBJECT_NULL) {
593a1d5f
A
815 vm_object_mapping_wait(object, THREAD_UNINT);
816 vm_object_mapping_begin(object);
0b4e3aa0 817 vm_object_unlock(object);
2d21ac55 818
b0d623f7 819 memory_object_last_unmap(pager);
593a1d5f 820
b0d623f7 821 vm_object_lock(object);
593a1d5f 822 vm_object_mapping_end(object);
0b4e3aa0 823 }
b0d623f7 824 assert(object->ref_count > 0);
0b4e3aa0 825 }
1c79356b
A
826
827 /*
828 * Lose the reference. If other references
829 * remain, then we are done, unless we need
830 * to retry a cache trim.
831 * If it is the last reference, then keep it
832 * until any pending initialization is completed.
833 */
834
0b4e3aa0
A
835 /* if the object is terminating, it cannot go into */
836 /* the cache and we obviously should not call */
837 /* terminate again. */
838
839 if ((object->ref_count > 1) || object->terminating) {
2d21ac55 840 vm_object_lock_assert_exclusive(object);
1c79356b 841 object->ref_count--;
1c79356b 842 vm_object_res_deallocate(object);
91447636
A
843
844 if (object->ref_count == 1 &&
845 object->shadow != VM_OBJECT_NULL) {
846 /*
0c530ab8
A
847 * There's only one reference left on this
848 * VM object. We can't tell if it's a valid
849 * one (from a mapping for example) or if this
850 * object is just part of a possibly stale and
851 * useless shadow chain.
852 * We would like to try and collapse it into
853 * its parent, but we don't have any pointers
854 * back to this parent object.
91447636
A
855 * But we can try and collapse this object with
856 * its own shadows, in case these are useless
857 * too...
0c530ab8
A
858 * We can't bypass this object though, since we
859 * don't know if this last reference on it is
860 * meaningful or not.
91447636 861 */
0c530ab8 862 vm_object_collapse(object, 0, FALSE);
91447636 863 }
91447636 864 vm_object_unlock(object);
1c79356b
A
865 return;
866 }
867
868 /*
869 * We have to wait for initialization
870 * before destroying or caching the object.
871 */
872
873 if (object->pager_created && ! object->pager_initialized) {
874 assert(! object->can_persist);
875 vm_object_assert_wait(object,
876 VM_OBJECT_EVENT_INITIALIZED,
877 THREAD_UNINT);
878 vm_object_unlock(object);
b0d623f7 879
9bccf70c 880 thread_block(THREAD_CONTINUE_NULL);
1c79356b
A
881 continue;
882 }
883
5ba3f43e
A
884 XPR(XPR_VM_OBJECT,
885 "vm_o_deallocate: 0x%X res %d paging_ops %d thread 0x%p ref %d\n",
886 object, object->resident_page_count,
887 object->paging_in_progress,
888 (void *)current_thread(),object->ref_count);
889
890 VM_OBJ_RES_DECR(object); /* XXX ? */
1c79356b 891 /*
5ba3f43e
A
892 * Terminate this object. If it had a shadow,
893 * then deallocate it; otherwise, if we need
894 * to retry a cache trim, do so now; otherwise,
895 * we are done. "pageout" objects have a shadow,
896 * but maintain a "paging reference" rather than
897 * a normal reference.
1c79356b 898 */
5ba3f43e 899 shadow = object->pageout?VM_OBJECT_NULL:object->shadow;
1c79356b 900
5ba3f43e
A
901 if (vm_object_terminate(object) != KERN_SUCCESS) {
902 return;
903 }
904 if (shadow != VM_OBJECT_NULL) {
905 object = shadow;
906 continue;
1c79356b 907 }
5ba3f43e 908 return;
1c79356b 909 }
1c79356b
A
910}
911
b0d623f7 912
6d2010ae
A
913
914vm_page_t
915vm_object_page_grab(
916 vm_object_t object)
917{
918 vm_page_t p, next_p;
919 int p_limit = 0;
920 int p_skipped = 0;
921
922 vm_object_lock_assert_exclusive(object);
923
39037602 924 next_p = (vm_page_t)vm_page_queue_first(&object->memq);
6d2010ae
A
925 p_limit = MIN(50, object->resident_page_count);
926
39037602 927 while (!vm_page_queue_end(&object->memq, (vm_page_queue_entry_t)next_p) && --p_limit > 0) {
6d2010ae
A
928
929 p = next_p;
39037602 930 next_p = (vm_page_t)vm_page_queue_next(&next_p->listq);
6d2010ae 931
316670eb 932 if (VM_PAGE_WIRED(p) || p->busy || p->cleaning || p->laundry || p->fictitious)
6d2010ae
A
933 goto move_page_in_obj;
934
935 if (p->pmapped || p->dirty || p->precious) {
936 vm_page_lockspin_queues();
937
938 if (p->pmapped) {
939 int refmod_state;
940
941 vm_object_page_grab_pmapped++;
942
943 if (p->reference == FALSE || p->dirty == FALSE) {
944
39037602 945 refmod_state = pmap_get_refmod(VM_PAGE_GET_PHYS_PAGE(p));
6d2010ae
A
946
947 if (refmod_state & VM_MEM_REFERENCED)
948 p->reference = TRUE;
316670eb
A
949 if (refmod_state & VM_MEM_MODIFIED) {
950 SET_PAGE_DIRTY(p, FALSE);
951 }
6d2010ae
A
952 }
953 if (p->dirty == FALSE && p->precious == FALSE) {
954
39037602 955 refmod_state = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(p));
6d2010ae
A
956
957 if (refmod_state & VM_MEM_REFERENCED)
958 p->reference = TRUE;
316670eb
A
959 if (refmod_state & VM_MEM_MODIFIED) {
960 SET_PAGE_DIRTY(p, FALSE);
961 }
6d2010ae
A
962
963 if (p->dirty == FALSE)
964 goto take_page;
965 }
966 }
39037602 967 if ((p->vm_page_q_state != VM_PAGE_ON_ACTIVE_Q) && p->reference == TRUE) {
6d2010ae
A
968 vm_page_activate(p);
969
970 VM_STAT_INCR(reactivations);
971 vm_object_page_grab_reactivations++;
972 }
973 vm_page_unlock_queues();
974move_page_in_obj:
39037602
A
975 vm_page_queue_remove(&object->memq, p, vm_page_t, listq);
976 vm_page_queue_enter(&object->memq, p, vm_page_t, listq);
6d2010ae
A
977
978 p_skipped++;
979 continue;
980 }
981 vm_page_lockspin_queues();
982take_page:
983 vm_page_free_prepare_queues(p);
984 vm_object_page_grab_returned++;
985 vm_object_page_grab_skipped += p_skipped;
986
987 vm_page_unlock_queues();
988
989 vm_page_free_prepare_object(p, TRUE);
990
991 return (p);
992 }
993 vm_object_page_grab_skipped += p_skipped;
994 vm_object_page_grab_failed++;
995
996 return (NULL);
997}
998
999
1000
1001#define EVICT_PREPARE_LIMIT 64
1002#define EVICT_AGE 10
1003
1004static clock_sec_t vm_object_cache_aging_ts = 0;
1005
1006static void
1007vm_object_cache_remove_locked(
1008 vm_object_t object)
1009{
39037602
A
1010 assert(object->purgable == VM_PURGABLE_DENY);
1011 assert(object->wired_page_count == 0);
1012
6d2010ae
A
1013 queue_remove(&vm_object_cached_list, object, vm_object_t, objq);
1014 object->objq.next = NULL;
1015 object->objq.prev = NULL;
1016
1017 vm_object_cached_count--;
1018}
1019
1020void
1021vm_object_cache_remove(
1022 vm_object_t object)
1023{
1024 vm_object_cache_lock_spin();
1025
1026 if (object->objq.next || object->objq.prev)
1027 vm_object_cache_remove_locked(object);
1028
1029 vm_object_cache_unlock();
1030}
1031
1032void
1033vm_object_cache_add(
1034 vm_object_t object)
1035{
1036 clock_sec_t sec;
1037 clock_nsec_t nsec;
1038
39037602
A
1039 assert(object->purgable == VM_PURGABLE_DENY);
1040 assert(object->wired_page_count == 0);
1041
6d2010ae
A
1042 if (object->resident_page_count == 0)
1043 return;
1044 clock_get_system_nanotime(&sec, &nsec);
1045
1046 vm_object_cache_lock_spin();
1047
1048 if (object->objq.next == NULL && object->objq.prev == NULL) {
1049 queue_enter(&vm_object_cached_list, object, vm_object_t, objq);
1050 object->vo_cache_ts = sec + EVICT_AGE;
1051 object->vo_cache_pages_to_scan = object->resident_page_count;
1052
1053 vm_object_cached_count++;
1054 vm_object_cache_adds++;
1055 }
1056 vm_object_cache_unlock();
1057}
1058
1059int
1060vm_object_cache_evict(
1061 int num_to_evict,
1062 int max_objects_to_examine)
1063{
1064 vm_object_t object = VM_OBJECT_NULL;
1065 vm_object_t next_obj = VM_OBJECT_NULL;
1066 vm_page_t local_free_q = VM_PAGE_NULL;
1067 vm_page_t p;
1068 vm_page_t next_p;
1069 int object_cnt = 0;
1070 vm_page_t ep_array[EVICT_PREPARE_LIMIT];
1071 int ep_count;
1072 int ep_limit;
1073 int ep_index;
1074 int ep_freed = 0;
1075 int ep_moved = 0;
1076 uint32_t ep_skipped = 0;
1077 clock_sec_t sec;
1078 clock_nsec_t nsec;
1079
1080 KERNEL_DEBUG(0x13001ec | DBG_FUNC_START, 0, 0, 0, 0, 0);
1081 /*
1082 * do a couple of quick checks to see if it's
1083 * worthwhile grabbing the lock
1084 */
1085 if (queue_empty(&vm_object_cached_list)) {
1086 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END, 0, 0, 0, 0, 0);
1087 return (0);
1088 }
1089 clock_get_system_nanotime(&sec, &nsec);
1090
1091 /*
1092 * the object on the head of the queue has not
1093 * yet sufficiently aged
1094 */
1095 if (sec < vm_object_cache_aging_ts) {
1096 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END, 0, 0, 0, 0, 0);
1097 return (0);
1098 }
1099 /*
1100 * don't need the queue lock to find
1101 * and lock an object on the cached list
1102 */
1103 vm_page_unlock_queues();
1104
1105 vm_object_cache_lock_spin();
1106
1107 for (;;) {
1108 next_obj = (vm_object_t)queue_first(&vm_object_cached_list);
1109
1110 while (!queue_end(&vm_object_cached_list, (queue_entry_t)next_obj) && object_cnt++ < max_objects_to_examine) {
1111
1112 object = next_obj;
1113 next_obj = (vm_object_t)queue_next(&next_obj->objq);
39037602
A
1114
1115 assert(object->purgable == VM_PURGABLE_DENY);
1116 assert(object->wired_page_count == 0);
6d2010ae
A
1117
1118 if (sec < object->vo_cache_ts) {
1119 KERNEL_DEBUG(0x130020c, object, object->resident_page_count, object->vo_cache_ts, sec, 0);
1120
1121 vm_object_cache_aging_ts = object->vo_cache_ts;
1122 object = VM_OBJECT_NULL;
1123 break;
1124 }
1125 if (!vm_object_lock_try_scan(object)) {
1126 /*
1127 * just skip over this guy for now... if we find
1128 * an object to steal pages from, we'll revist in a bit...
1129 * hopefully, the lock will have cleared
1130 */
1131 KERNEL_DEBUG(0x13001f8, object, object->resident_page_count, 0, 0, 0);
1132
1133 object = VM_OBJECT_NULL;
1134 continue;
1135 }
39037602 1136 if (vm_page_queue_empty(&object->memq) || object->vo_cache_pages_to_scan == 0) {
6d2010ae
A
1137 /*
1138 * this case really shouldn't happen, but it's not fatal
1139 * so deal with it... if we don't remove the object from
1140 * the list, we'll never move past it.
1141 */
1142 KERNEL_DEBUG(0x13001fc, object, object->resident_page_count, ep_freed, ep_moved, 0);
1143
1144 vm_object_cache_remove_locked(object);
1145 vm_object_unlock(object);
1146 object = VM_OBJECT_NULL;
1147 continue;
1148 }
1149 /*
1150 * we have a locked object with pages...
1151 * time to start harvesting
1152 */
1153 break;
1154 }
1155 vm_object_cache_unlock();
1156
1157 if (object == VM_OBJECT_NULL)
1158 break;
1159
1160 /*
1161 * object is locked at this point and
1162 * has resident pages
1163 */
39037602 1164 next_p = (vm_page_t)vm_page_queue_first(&object->memq);
6d2010ae
A
1165
1166 /*
1167 * break the page scan into 2 pieces to minimize the time spent
1168 * behind the page queue lock...
1169 * the list of pages on these unused objects is likely to be cold
1170 * w/r to the cpu cache which increases the time to scan the list
1171 * tenfold... and we may have a 'run' of pages we can't utilize that
1172 * needs to be skipped over...
1173 */
1174 if ((ep_limit = num_to_evict - (ep_freed + ep_moved)) > EVICT_PREPARE_LIMIT)
1175 ep_limit = EVICT_PREPARE_LIMIT;
1176 ep_count = 0;
1177
39037602 1178 while (!vm_page_queue_end(&object->memq, (vm_page_queue_entry_t)next_p) && object->vo_cache_pages_to_scan && ep_count < ep_limit) {
6d2010ae
A
1179
1180 p = next_p;
39037602 1181 next_p = (vm_page_t)vm_page_queue_next(&next_p->listq);
6d2010ae
A
1182
1183 object->vo_cache_pages_to_scan--;
1184
316670eb 1185 if (VM_PAGE_WIRED(p) || p->busy || p->cleaning || p->laundry) {
39037602
A
1186 vm_page_queue_remove(&object->memq, p, vm_page_t, listq);
1187 vm_page_queue_enter(&object->memq, p, vm_page_t, listq);
6d2010ae
A
1188
1189 ep_skipped++;
1190 continue;
1191 }
1192 if (p->wpmapped || p->dirty || p->precious) {
39037602
A
1193 vm_page_queue_remove(&object->memq, p, vm_page_t, listq);
1194 vm_page_queue_enter(&object->memq, p, vm_page_t, listq);
6d2010ae 1195
39037602 1196 pmap_clear_reference(VM_PAGE_GET_PHYS_PAGE(p));
6d2010ae
A
1197 }
1198 ep_array[ep_count++] = p;
1199 }
1200 KERNEL_DEBUG(0x13001f4 | DBG_FUNC_START, object, object->resident_page_count, ep_freed, ep_moved, 0);
1201
1202 vm_page_lockspin_queues();
1203
1204 for (ep_index = 0; ep_index < ep_count; ep_index++) {
1205
1206 p = ep_array[ep_index];
1207
1208 if (p->wpmapped || p->dirty || p->precious) {
1209 p->reference = FALSE;
1210 p->no_cache = FALSE;
1211
316670eb
A
1212 /*
1213 * we've already filtered out pages that are in the laundry
1214 * so if we get here, this page can't be on the pageout queue
1215 */
39037602 1216 vm_page_queues_remove(p, FALSE);
3e170ce0 1217 vm_page_enqueue_inactive(p, TRUE);
6d2010ae
A
1218
1219 ep_moved++;
1220 } else {
fe8ab488
A
1221#if CONFIG_PHANTOM_CACHE
1222 vm_phantom_cache_add_ghost(p);
1223#endif
6d2010ae
A
1224 vm_page_free_prepare_queues(p);
1225
39037602 1226 assert(p->pageq.next == 0 && p->pageq.prev == 0);
6d2010ae
A
1227 /*
1228 * Add this page to our list of reclaimed pages,
1229 * to be freed later.
1230 */
39037602 1231 p->snext = local_free_q;
6d2010ae
A
1232 local_free_q = p;
1233
1234 ep_freed++;
1235 }
1236 }
1237 vm_page_unlock_queues();
1238
1239 KERNEL_DEBUG(0x13001f4 | DBG_FUNC_END, object, object->resident_page_count, ep_freed, ep_moved, 0);
1240
1241 if (local_free_q) {
1242 vm_page_free_list(local_free_q, TRUE);
1243 local_free_q = VM_PAGE_NULL;
1244 }
1245 if (object->vo_cache_pages_to_scan == 0) {
1246 KERNEL_DEBUG(0x1300208, object, object->resident_page_count, ep_freed, ep_moved, 0);
1247
1248 vm_object_cache_remove(object);
1249
1250 KERNEL_DEBUG(0x13001fc, object, object->resident_page_count, ep_freed, ep_moved, 0);
1251 }
1252 /*
1253 * done with this object
1254 */
1255 vm_object_unlock(object);
1256 object = VM_OBJECT_NULL;
1257
1258 /*
1259 * at this point, we are not holding any locks
1260 */
1261 if ((ep_freed + ep_moved) >= num_to_evict) {
1262 /*
1263 * we've reached our target for the
1264 * number of pages to evict
1265 */
1266 break;
1267 }
1268 vm_object_cache_lock_spin();
1269 }
1270 /*
1271 * put the page queues lock back to the caller's
1272 * idea of it
1273 */
1274 vm_page_lock_queues();
1275
1276 vm_object_cache_pages_freed += ep_freed;
1277 vm_object_cache_pages_moved += ep_moved;
1278 vm_object_cache_pages_skipped += ep_skipped;
1279
1280 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END, ep_freed, 0, 0, 0, 0);
1281 return (ep_freed);
1282}
1283
1c79356b
A
1284/*
1285 * Routine: vm_object_terminate
1286 * Purpose:
1287 * Free all resources associated with a vm_object.
1288 * In/out conditions:
0b4e3aa0 1289 * Upon entry, the object must be locked,
1c79356b
A
1290 * and the object must have exactly one reference.
1291 *
1292 * The shadow object reference is left alone.
1293 *
1294 * The object must be unlocked if its found that pages
1295 * must be flushed to a backing object. If someone
1296 * manages to map the object while it is being flushed
1297 * the object is returned unlocked and unchanged. Otherwise,
1298 * upon exit, the cache will be unlocked, and the
1299 * object will cease to exist.
1300 */
0b4e3aa0 1301static kern_return_t
1c79356b 1302vm_object_terminate(
b0d623f7 1303 vm_object_t object)
1c79356b 1304{
b0d623f7 1305 vm_object_t shadow_object;
1c79356b
A
1306
1307 XPR(XPR_VM_OBJECT, "vm_object_terminate, object 0x%X ref %d\n",
b0d623f7
A
1308 object, object->ref_count, 0, 0, 0);
1309
39037602
A
1310 vm_object_lock_assert_exclusive(object);
1311
5ba3f43e 1312 if (!object->pageout && (!object->internal && object->can_persist) &&
b0d623f7 1313 (object->pager != NULL || object->shadow_severed)) {
1c79356b
A
1314 /*
1315 * Clear pager_trusted bit so that the pages get yanked
1316 * out of the object instead of cleaned in place. This
1317 * prevents a deadlock in XMM and makes more sense anyway.
1318 */
1319 object->pager_trusted = FALSE;
1320
b0d623f7 1321 vm_object_reap_pages(object, REAP_TERMINATE);
1c79356b 1322 }
0b4e3aa0
A
1323 /*
1324 * Make sure the object isn't already being terminated
1325 */
b0d623f7 1326 if (object->terminating) {
2d21ac55
A
1327 vm_object_lock_assert_exclusive(object);
1328 object->ref_count--;
0b4e3aa0 1329 assert(object->ref_count > 0);
0b4e3aa0
A
1330 vm_object_unlock(object);
1331 return KERN_FAILURE;
1332 }
1333
1334 /*
1335 * Did somebody get a reference to the object while we were
1336 * cleaning it?
1337 */
b0d623f7 1338 if (object->ref_count != 1) {
2d21ac55
A
1339 vm_object_lock_assert_exclusive(object);
1340 object->ref_count--;
0b4e3aa0 1341 assert(object->ref_count > 0);
1c79356b 1342 vm_object_res_deallocate(object);
1c79356b
A
1343 vm_object_unlock(object);
1344 return KERN_FAILURE;
1345 }
1346
1c79356b
A
1347 /*
1348 * Make sure no one can look us up now.
1349 */
1350
0b4e3aa0
A
1351 object->terminating = TRUE;
1352 object->alive = FALSE;
1c79356b 1353
6d2010ae
A
1354 if ( !object->internal && (object->objq.next || object->objq.prev))
1355 vm_object_cache_remove(object);
1356
1c79356b
A
1357 /*
1358 * Detach the object from its shadow if we are the shadow's
55e303ae
A
1359 * copy. The reference we hold on the shadow must be dropped
1360 * by our caller.
1c79356b
A
1361 */
1362 if (((shadow_object = object->shadow) != VM_OBJECT_NULL) &&
1363 !(object->pageout)) {
1364 vm_object_lock(shadow_object);
55e303ae
A
1365 if (shadow_object->copy == object)
1366 shadow_object->copy = VM_OBJECT_NULL;
1c79356b
A
1367 vm_object_unlock(shadow_object);
1368 }
1369
b0d623f7
A
1370 if (object->paging_in_progress != 0 ||
1371 object->activity_in_progress != 0) {
8f6c56a5
A
1372 /*
1373 * There are still some paging_in_progress references
1374 * on this object, meaning that there are some paging
1375 * or other I/O operations in progress for this VM object.
1376 * Such operations take some paging_in_progress references
1377 * up front to ensure that the object doesn't go away, but
1378 * they may also need to acquire a reference on the VM object,
1379 * to map it in kernel space, for example. That means that
1380 * they may end up releasing the last reference on the VM
1381 * object, triggering its termination, while still holding
1382 * paging_in_progress references. Waiting for these
1383 * pending paging_in_progress references to go away here would
1384 * deadlock.
1385 *
1386 * To avoid deadlocking, we'll let the vm_object_reaper_thread
1387 * complete the VM object termination if it still holds
1388 * paging_in_progress references at this point.
1389 *
1390 * No new paging_in_progress should appear now that the
1391 * VM object is "terminating" and not "alive".
1392 */
1393 vm_object_reap_async(object);
8f6c56a5 1394 vm_object_unlock(object);
6601e61a
A
1395 /*
1396 * Return KERN_FAILURE to let the caller know that we
1397 * haven't completed the termination and it can't drop this
1398 * object's reference on its shadow object yet.
1399 * The reaper thread will take care of that once it has
1400 * completed this object's termination.
1401 */
1402 return KERN_FAILURE;
8f6c56a5 1403 }
b0d623f7
A
1404 /*
1405 * complete the VM object termination
1406 */
8f6c56a5
A
1407 vm_object_reap(object);
1408 object = VM_OBJECT_NULL;
8f6c56a5 1409
2d21ac55 1410 /*
b0d623f7
A
1411 * the object lock was released by vm_object_reap()
1412 *
2d21ac55
A
1413 * KERN_SUCCESS means that this object has been terminated
1414 * and no longer needs its shadow object but still holds a
1415 * reference on it.
1416 * The caller is responsible for dropping that reference.
1417 * We can't call vm_object_deallocate() here because that
1418 * would create a recursion.
1419 */
8f6c56a5
A
1420 return KERN_SUCCESS;
1421}
1422
b0d623f7 1423
8f6c56a5
A
1424/*
1425 * vm_object_reap():
1426 *
1427 * Complete the termination of a VM object after it's been marked
1428 * as "terminating" and "!alive" by vm_object_terminate().
1429 *
b0d623f7
A
1430 * The VM object must be locked by caller.
1431 * The lock will be released on return and the VM object is no longer valid.
8f6c56a5 1432 */
3e170ce0 1433
8f6c56a5
A
1434void
1435vm_object_reap(
1436 vm_object_t object)
1437{
1438 memory_object_t pager;
8f6c56a5 1439
2d21ac55
A
1440 vm_object_lock_assert_exclusive(object);
1441 assert(object->paging_in_progress == 0);
b0d623f7 1442 assert(object->activity_in_progress == 0);
8f6c56a5
A
1443
1444 vm_object_reap_count++;
1445
fe8ab488
A
1446 /*
1447 * Disown this purgeable object to cleanup its owner's purgeable
1448 * ledgers. We need to do this before disconnecting the object
1449 * from its pager, to properly account for compressed pages.
1450 */
1451 if (object->internal &&
1452 object->purgable != VM_PURGABLE_DENY) {
1453 vm_purgeable_accounting(object,
1454 object->purgable,
1455 TRUE); /* disown */
1456 }
1457
0b4e3aa0
A
1458 pager = object->pager;
1459 object->pager = MEMORY_OBJECT_NULL;
1460
1461 if (pager != MEMORY_OBJECT_NULL)
91447636 1462 memory_object_control_disable(object->pager_control);
0b4e3aa0 1463
1c79356b
A
1464 object->ref_count--;
1465#if TASK_SWAPPER
1466 assert(object->res_count == 0);
1467#endif /* TASK_SWAPPER */
1468
1c79356b
A
1469 assert (object->ref_count == 0);
1470
b0d623f7
A
1471 /*
1472 * remove from purgeable queue if it's on
1473 */
fe8ab488
A
1474 if (object->internal) {
1475 task_t owner;
1476
1477 owner = object->vo_purgeable_owner;
1478
3e170ce0
A
1479 VM_OBJECT_UNWIRED(object);
1480
fe8ab488
A
1481 if (object->purgable == VM_PURGABLE_DENY) {
1482 /* not purgeable: nothing to do */
1483 } else if (object->purgable == VM_PURGABLE_VOLATILE) {
1484 purgeable_q_t queue;
1485
1486 assert(object->vo_purgeable_owner == NULL);
1487
1488 queue = vm_purgeable_object_remove(object);
1489 assert(queue);
1490
1491 if (object->purgeable_when_ripe) {
1492 /*
1493 * Must take page lock for this -
1494 * using it to protect token queue
1495 */
1496 vm_page_lock_queues();
1497 vm_purgeable_token_delete_first(queue);
1498
1499 assert(queue->debug_count_objects>=0);
1500 vm_page_unlock_queues();
1501 }
2d21ac55 1502
39236c6e 1503 /*
fe8ab488
A
1504 * Update "vm_page_purgeable_count" in bulk and mark
1505 * object as VM_PURGABLE_EMPTY to avoid updating
1506 * "vm_page_purgeable_count" again in vm_page_remove()
1507 * when reaping the pages.
39236c6e 1508 */
fe8ab488
A
1509 unsigned int delta;
1510 assert(object->resident_page_count >=
1511 object->wired_page_count);
1512 delta = (object->resident_page_count -
1513 object->wired_page_count);
1514 if (delta != 0) {
1515 assert(vm_page_purgeable_count >= delta);
1516 OSAddAtomic(-delta,
1517 (SInt32 *)&vm_page_purgeable_count);
1518 }
1519 if (object->wired_page_count != 0) {
1520 assert(vm_page_purgeable_wired_count >=
1521 object->wired_page_count);
1522 OSAddAtomic(-object->wired_page_count,
1523 (SInt32 *)&vm_page_purgeable_wired_count);
1524 }
1525 object->purgable = VM_PURGABLE_EMPTY;
1526 }
1527 else if (object->purgable == VM_PURGABLE_NONVOLATILE ||
1528 object->purgable == VM_PURGABLE_EMPTY) {
1529 /* remove from nonvolatile queue */
1530 assert(object->vo_purgeable_owner == TASK_NULL);
1531 vm_purgeable_nonvolatile_dequeue(object);
1532 } else {
1533 panic("object %p in unexpected purgeable state 0x%x\n",
1534 object, object->purgable);
39236c6e 1535 }
fe8ab488
A
1536 assert(object->objq.next == NULL);
1537 assert(object->objq.prev == NULL);
2d21ac55
A
1538 }
1539
1c79356b 1540 if (object->pageout) {
5ba3f43e
A
1541 /*
1542 * free all remaining pages tabled on
1543 * this object
1544 * clean up it's shadow
1545 */
8f6c56a5 1546 assert(object->shadow != VM_OBJECT_NULL);
1c79356b
A
1547
1548 vm_pageout_object_terminate(object);
1549
5ba3f43e
A
1550 } else if (object->resident_page_count) {
1551 /*
1552 * free all remaining pages tabled on
1553 * this object
1554 */
b0d623f7 1555 vm_object_reap_pages(object, REAP_REAP);
1c79356b 1556 }
39037602 1557 assert(vm_page_queue_empty(&object->memq));
1c79356b 1558 assert(object->paging_in_progress == 0);
b0d623f7 1559 assert(object->activity_in_progress == 0);
1c79356b
A
1560 assert(object->ref_count == 0);
1561
1c79356b 1562 /*
0b4e3aa0
A
1563 * If the pager has not already been released by
1564 * vm_object_destroy, we need to terminate it and
1565 * release our reference to it here.
1c79356b 1566 */
0b4e3aa0
A
1567 if (pager != MEMORY_OBJECT_NULL) {
1568 vm_object_unlock(object);
5ba3f43e 1569 vm_object_release_pager(pager);
0b4e3aa0 1570 vm_object_lock(object);
1c79356b 1571 }
0b4e3aa0 1572
1c79356b 1573 /* kick off anyone waiting on terminating */
0b4e3aa0 1574 object->terminating = FALSE;
1c79356b
A
1575 vm_object_paging_begin(object);
1576 vm_object_paging_end(object);
1577 vm_object_unlock(object);
1578
6601e61a
A
1579 object->shadow = VM_OBJECT_NULL;
1580
fe8ab488
A
1581#if VM_OBJECT_TRACKING
1582 if (vm_object_tracking_inited) {
1583 btlog_remove_entries_for_element(vm_object_tracking_btlog,
1584 object);
1585 }
1586#endif /* VM_OBJECT_TRACKING */
1587
2d21ac55 1588 vm_object_lock_destroy(object);
1c79356b
A
1589 /*
1590 * Free the space for the object.
1591 */
91447636 1592 zfree(vm_object_zone, object);
8f6c56a5
A
1593 object = VM_OBJECT_NULL;
1594}
1595
8f6c56a5 1596
6d2010ae 1597unsigned int vm_max_batch = 256;
8f6c56a5 1598
b0d623f7
A
1599#define V_O_R_MAX_BATCH 128
1600
6d2010ae
A
1601#define BATCH_LIMIT(max) (vm_max_batch >= max ? max : vm_max_batch)
1602
b0d623f7
A
1603
1604#define VM_OBJ_REAP_FREELIST(_local_free_q, do_disconnect) \
1605 MACRO_BEGIN \
1606 if (_local_free_q) { \
1607 if (do_disconnect) { \
1608 vm_page_t m; \
1609 for (m = _local_free_q; \
1610 m != VM_PAGE_NULL; \
39037602 1611 m = m->snext) { \
b0d623f7 1612 if (m->pmapped) { \
39037602 1613 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m)); \
b0d623f7
A
1614 } \
1615 } \
1616 } \
1617 vm_page_free_list(_local_free_q, TRUE); \
1618 _local_free_q = VM_PAGE_NULL; \
1619 } \
1620 MACRO_END
1621
8f6c56a5
A
1622
1623void
b0d623f7
A
1624vm_object_reap_pages(
1625 vm_object_t object,
1626 int reap_type)
8f6c56a5 1627{
b0d623f7
A
1628 vm_page_t p;
1629 vm_page_t next;
1630 vm_page_t local_free_q = VM_PAGE_NULL;
1631 int loop_count;
1632 boolean_t disconnect_on_release;
39236c6e 1633 pmap_flush_context pmap_flush_context_storage;
8f6c56a5 1634
b0d623f7 1635 if (reap_type == REAP_DATA_FLUSH) {
2d21ac55 1636 /*
b0d623f7
A
1637 * We need to disconnect pages from all pmaps before
1638 * releasing them to the free list
2d21ac55 1639 */
b0d623f7
A
1640 disconnect_on_release = TRUE;
1641 } else {
1642 /*
1643 * Either the caller has already disconnected the pages
1644 * from all pmaps, or we disconnect them here as we add
1645 * them to out local list of pages to be released.
1646 * No need to re-disconnect them when we release the pages
1647 * to the free list.
1648 */
1649 disconnect_on_release = FALSE;
1650 }
1651
1652restart_after_sleep:
39037602 1653 if (vm_page_queue_empty(&object->memq))
b0d623f7 1654 return;
316670eb 1655 loop_count = BATCH_LIMIT(V_O_R_MAX_BATCH);
b0d623f7 1656
39236c6e
A
1657 if (reap_type == REAP_PURGEABLE)
1658 pmap_flush_context_init(&pmap_flush_context_storage);
1659
b0d623f7
A
1660 vm_page_lockspin_queues();
1661
39037602 1662 next = (vm_page_t)vm_page_queue_first(&object->memq);
b0d623f7 1663
39037602 1664 while (!vm_page_queue_end(&object->memq, (vm_page_queue_entry_t)next)) {
b0d623f7
A
1665
1666 p = next;
39037602 1667 next = (vm_page_t)vm_page_queue_next(&next->listq);
b0d623f7
A
1668
1669 if (--loop_count == 0) {
1670
1671 vm_page_unlock_queues();
1672
1673 if (local_free_q) {
39236c6e
A
1674
1675 if (reap_type == REAP_PURGEABLE) {
1676 pmap_flush(&pmap_flush_context_storage);
1677 pmap_flush_context_init(&pmap_flush_context_storage);
1678 }
b0d623f7
A
1679 /*
1680 * Free the pages we reclaimed so far
1681 * and take a little break to avoid
1682 * hogging the page queue lock too long
1683 */
1684 VM_OBJ_REAP_FREELIST(local_free_q,
1685 disconnect_on_release);
1686 } else
1687 mutex_pause(0);
1688
316670eb 1689 loop_count = BATCH_LIMIT(V_O_R_MAX_BATCH);
b0d623f7
A
1690
1691 vm_page_lockspin_queues();
1692 }
1693 if (reap_type == REAP_DATA_FLUSH || reap_type == REAP_TERMINATE) {
1694
316670eb 1695 if (p->busy || p->cleaning) {
b0d623f7
A
1696
1697 vm_page_unlock_queues();
1698 /*
1699 * free the pages reclaimed so far
1700 */
1701 VM_OBJ_REAP_FREELIST(local_free_q,
1702 disconnect_on_release);
1703
1704 PAGE_SLEEP(object, p, THREAD_UNINT);
1705
1706 goto restart_after_sleep;
1707 }
39037602 1708 if (p->laundry)
316670eb 1709 vm_pageout_steal_laundry(p, TRUE);
b0d623f7
A
1710 }
1711 switch (reap_type) {
1712
1713 case REAP_DATA_FLUSH:
1714 if (VM_PAGE_WIRED(p)) {
1715 /*
1716 * this is an odd case... perhaps we should
1717 * zero-fill this page since we're conceptually
1718 * tossing its data at this point, but leaving
1719 * it on the object to honor the 'wire' contract
1720 */
1721 continue;
1722 }
1723 break;
1724
1725 case REAP_PURGEABLE:
1726 if (VM_PAGE_WIRED(p)) {
316670eb
A
1727 /*
1728 * can't purge a wired page
1729 */
b0d623f7
A
1730 vm_page_purged_wired++;
1731 continue;
1732 }
39037602 1733 if (p->laundry && !p->busy && !p->cleaning)
316670eb 1734 vm_pageout_steal_laundry(p, TRUE);
39037602 1735
fe8ab488 1736 if (p->cleaning || p->laundry || p->absent) {
316670eb
A
1737 /*
1738 * page is being acted upon,
1739 * so don't mess with it
1740 */
1741 vm_page_purged_others++;
1742 continue;
1743 }
b0d623f7
A
1744 if (p->busy) {
1745 /*
1746 * We can't reclaim a busy page but we can
316670eb 1747 * make it more likely to be paged (it's not wired) to make
b0d623f7
A
1748 * sure that it gets considered by
1749 * vm_pageout_scan() later.
1750 */
39037602
A
1751 if (VM_PAGE_PAGEABLE(p))
1752 vm_page_deactivate(p);
b0d623f7
A
1753 vm_page_purged_busy++;
1754 continue;
1755 }
1756
39037602 1757 assert(VM_PAGE_OBJECT(p) != kernel_object);
b0d623f7
A
1758
1759 /*
1760 * we can discard this page...
1761 */
1762 if (p->pmapped == TRUE) {
b0d623f7
A
1763 /*
1764 * unmap the page
1765 */
39037602 1766 pmap_disconnect_options(VM_PAGE_GET_PHYS_PAGE(p), PMAP_OPTIONS_NOFLUSH | PMAP_OPTIONS_NOREFMOD, (void *)&pmap_flush_context_storage);
b0d623f7 1767 }
39236c6e 1768 vm_page_purged_count++;
b0d623f7
A
1769
1770 break;
1771
1772 case REAP_TERMINATE:
1773 if (p->absent || p->private) {
1774 /*
1775 * For private pages, VM_PAGE_FREE just
1776 * leaves the page structure around for
1777 * its owner to clean up. For absent
1778 * pages, the structure is returned to
1779 * the appropriate pool.
1780 */
1781 break;
1782 }
1783 if (p->fictitious) {
39037602 1784 assert (VM_PAGE_GET_PHYS_PAGE(p) == vm_page_guard_addr);
b0d623f7
A
1785 break;
1786 }
1787 if (!p->dirty && p->wpmapped)
39037602 1788 p->dirty = pmap_is_modified(VM_PAGE_GET_PHYS_PAGE(p));
b0d623f7
A
1789
1790 if ((p->dirty || p->precious) && !p->error && object->alive) {
1791
3e170ce0 1792 assert(!object->internal);
39037602
A
1793
1794 p->free_when_done = TRUE;
1795
316670eb 1796 if (!p->laundry) {
39037602 1797 vm_page_queues_remove(p, TRUE);
316670eb
A
1798 /*
1799 * flush page... page will be freed
1800 * upon completion of I/O
1801 */
5ba3f43e 1802 vm_pageout_cluster(p);
316670eb 1803 }
b0d623f7
A
1804 vm_page_unlock_queues();
1805 /*
1806 * free the pages reclaimed so far
1807 */
1808 VM_OBJ_REAP_FREELIST(local_free_q,
1809 disconnect_on_release);
1810
b0d623f7
A
1811 vm_object_paging_wait(object, THREAD_UNINT);
1812
1813 goto restart_after_sleep;
1814 }
1815 break;
1816
1817 case REAP_REAP:
1818 break;
1819 }
1820 vm_page_free_prepare_queues(p);
39037602 1821 assert(p->pageq.next == 0 && p->pageq.prev == 0);
b0d623f7
A
1822 /*
1823 * Add this page to our list of reclaimed pages,
1824 * to be freed later.
1825 */
39037602 1826 p->snext = local_free_q;
b0d623f7
A
1827 local_free_q = p;
1828 }
1829 vm_page_unlock_queues();
1830
1831 /*
1832 * Free the remaining reclaimed pages
1833 */
39236c6e
A
1834 if (reap_type == REAP_PURGEABLE)
1835 pmap_flush(&pmap_flush_context_storage);
1836
b0d623f7
A
1837 VM_OBJ_REAP_FREELIST(local_free_q,
1838 disconnect_on_release);
1839}
1840
1841
1842void
1843vm_object_reap_async(
1844 vm_object_t object)
1845{
1846 vm_object_lock_assert_exclusive(object);
1847
1848 vm_object_reaper_lock_spin();
1849
1850 vm_object_reap_count_async++;
1851
1852 /* enqueue the VM object... */
1853 queue_enter(&vm_object_reaper_queue, object,
1854 vm_object_t, cached_list);
1855
1856 vm_object_reaper_unlock();
1857
1858 /* ... and wake up the reaper thread */
1859 thread_wakeup((event_t) &vm_object_reaper_queue);
1860}
1861
1862
1863void
1864vm_object_reaper_thread(void)
1865{
1866 vm_object_t object, shadow_object;
1867
1868 vm_object_reaper_lock_spin();
1869
1870 while (!queue_empty(&vm_object_reaper_queue)) {
1871 queue_remove_first(&vm_object_reaper_queue,
1872 object,
1873 vm_object_t,
1874 cached_list);
1875
1876 vm_object_reaper_unlock();
1877 vm_object_lock(object);
1878
1879 assert(object->terminating);
1880 assert(!object->alive);
1881
1882 /*
1883 * The pageout daemon might be playing with our pages.
1884 * Now that the object is dead, it won't touch any more
1885 * pages, but some pages might already be on their way out.
1886 * Hence, we wait until the active paging activities have
1887 * ceased before we break the association with the pager
1888 * itself.
1889 */
1890 while (object->paging_in_progress != 0 ||
1891 object->activity_in_progress != 0) {
1892 vm_object_wait(object,
1893 VM_OBJECT_EVENT_PAGING_IN_PROGRESS,
1894 THREAD_UNINT);
1895 vm_object_lock(object);
1896 }
1897
1898 shadow_object =
1899 object->pageout ? VM_OBJECT_NULL : object->shadow;
6601e61a 1900
8f6c56a5
A
1901 vm_object_reap(object);
1902 /* cache is unlocked and object is no longer valid */
1903 object = VM_OBJECT_NULL;
1904
6601e61a
A
1905 if (shadow_object != VM_OBJECT_NULL) {
1906 /*
1907 * Drop the reference "object" was holding on
1908 * its shadow object.
1909 */
1910 vm_object_deallocate(shadow_object);
1911 shadow_object = VM_OBJECT_NULL;
1912 }
b0d623f7 1913 vm_object_reaper_lock_spin();
8f6c56a5
A
1914 }
1915
1916 /* wait for more work... */
1917 assert_wait((event_t) &vm_object_reaper_queue, THREAD_UNINT);
b0d623f7
A
1918
1919 vm_object_reaper_unlock();
1920
8f6c56a5
A
1921 thread_block((thread_continue_t) vm_object_reaper_thread);
1922 /*NOTREACHED*/
1c79356b
A
1923}
1924
1c79356b 1925/*
0b4e3aa0
A
1926 * Routine: vm_object_release_pager
1927 * Purpose: Terminate the pager and, upon completion,
1928 * release our last reference to it.
1c79356b 1929 */
0b4e3aa0
A
1930static void
1931vm_object_release_pager(
5ba3f43e 1932 memory_object_t pager)
1c79356b 1933{
1c79356b 1934
0b4e3aa0
A
1935 /*
1936 * Terminate the pager.
1937 */
1c79356b 1938
0b4e3aa0 1939 (void) memory_object_terminate(pager);
1c79356b 1940
0b4e3aa0
A
1941 /*
1942 * Release reference to pager.
1943 */
1944 memory_object_deallocate(pager);
1945}
1c79356b 1946
1c79356b 1947/*
0b4e3aa0 1948 * Routine: vm_object_destroy
1c79356b 1949 * Purpose:
0b4e3aa0 1950 * Shut down a VM object, despite the
1c79356b
A
1951 * presence of address map (or other) references
1952 * to the vm_object.
1953 */
1954kern_return_t
0b4e3aa0
A
1955vm_object_destroy(
1956 vm_object_t object,
91447636 1957 __unused kern_return_t reason)
1c79356b 1958{
0b4e3aa0 1959 memory_object_t old_pager;
1c79356b
A
1960
1961 if (object == VM_OBJECT_NULL)
1962 return(KERN_SUCCESS);
1963
1964 /*
0b4e3aa0 1965 * Remove the pager association immediately.
1c79356b
A
1966 *
1967 * This will prevent the memory manager from further
1968 * meddling. [If it wanted to flush data or make
1969 * other changes, it should have done so before performing
1970 * the destroy call.]
1971 */
1972
1c79356b 1973 vm_object_lock(object);
1c79356b
A
1974 object->can_persist = FALSE;
1975 object->named = FALSE;
0b4e3aa0 1976 object->alive = FALSE;
1c79356b 1977
0b4e3aa0
A
1978 old_pager = object->pager;
1979 object->pager = MEMORY_OBJECT_NULL;
1980 if (old_pager != MEMORY_OBJECT_NULL)
91447636 1981 memory_object_control_disable(object->pager_control);
1c79356b
A
1982
1983 /*
b0d623f7
A
1984 * Wait for the existing paging activity (that got
1985 * through before we nulled out the pager) to subside.
1986 */
1987
1988 vm_object_paging_wait(object, THREAD_UNINT);
1989 vm_object_unlock(object);
1990
1991 /*
1992 * Terminate the object now.
1993 */
1994 if (old_pager != MEMORY_OBJECT_NULL) {
5ba3f43e 1995 vm_object_release_pager(old_pager);
b0d623f7
A
1996
1997 /*
1998 * JMM - Release the caller's reference. This assumes the
1999 * caller had a reference to release, which is a big (but
2000 * currently valid) assumption if this is driven from the
2001 * vnode pager (it is holding a named reference when making
2002 * this call)..
2003 */
2004 vm_object_deallocate(object);
2005
2006 }
2007 return(KERN_SUCCESS);
2008}
2009
b0d623f7
A
2010/*
2011 * The "chunk" macros are used by routines below when looking for pages to deactivate. These
2012 * exist because of the need to handle shadow chains. When deactivating pages, we only
2013 * want to deactive the ones at the top most level in the object chain. In order to do
2014 * this efficiently, the specified address range is divided up into "chunks" and we use
2015 * a bit map to keep track of which pages have already been processed as we descend down
2016 * the shadow chain. These chunk macros hide the details of the bit map implementation
2017 * as much as we can.
2018 *
2019 * For convenience, we use a 64-bit data type as the bit map, and therefore a chunk is
2020 * set to 64 pages. The bit map is indexed from the low-order end, so that the lowest
2021 * order bit represents page 0 in the current range and highest order bit represents
2022 * page 63.
2023 *
2024 * For further convenience, we also use negative logic for the page state in the bit map.
2025 * The bit is set to 1 to indicate it has not yet been seen, and to 0 to indicate it has
2026 * been processed. This way we can simply test the 64-bit long word to see if it's zero
2027 * to easily tell if the whole range has been processed. Therefore, the bit map starts
2028 * out with all the bits set. The macros below hide all these details from the caller.
2029 */
2030
2031#define PAGES_IN_A_CHUNK 64 /* The number of pages in the chunk must */
2032 /* be the same as the number of bits in */
2033 /* the chunk_state_t type. We use 64 */
2034 /* just for convenience. */
2035
2036#define CHUNK_SIZE (PAGES_IN_A_CHUNK * PAGE_SIZE_64) /* Size of a chunk in bytes */
2037
2038typedef uint64_t chunk_state_t;
2039
2040/*
2041 * The bit map uses negative logic, so we start out with all 64 bits set to indicate
2042 * that no pages have been processed yet. Also, if len is less than the full CHUNK_SIZE,
2043 * then we mark pages beyond the len as having been "processed" so that we don't waste time
2044 * looking at pages in that range. This can save us from unnecessarily chasing down the
2045 * shadow chain.
2046 */
2047
2048#define CHUNK_INIT(c, len) \
2049 MACRO_BEGIN \
2050 uint64_t p; \
2051 \
2052 (c) = 0xffffffffffffffffLL; \
2053 \
2054 for (p = (len) / PAGE_SIZE_64; p < PAGES_IN_A_CHUNK; p++) \
2055 MARK_PAGE_HANDLED(c, p); \
2056 MACRO_END
2057
6d2010ae 2058
b0d623f7
A
2059/*
2060 * Return true if all pages in the chunk have not yet been processed.
2061 */
2062
2063#define CHUNK_NOT_COMPLETE(c) ((c) != 0)
2064
2065/*
2066 * Return true if the page at offset 'p' in the bit map has already been handled
2067 * while processing a higher level object in the shadow chain.
2068 */
2069
2070#define PAGE_ALREADY_HANDLED(c, p) (((c) & (1LL << (p))) == 0)
2071
2072/*
2073 * Mark the page at offset 'p' in the bit map as having been processed.
2074 */
2075
2076#define MARK_PAGE_HANDLED(c, p) \
2077MACRO_BEGIN \
2078 (c) = (c) & ~(1LL << (p)); \
2079MACRO_END
2080
2081
2082/*
2083 * Return true if the page at the given offset has been paged out. Object is
2084 * locked upon entry and returned locked.
2085 */
2086
2087static boolean_t
2088page_is_paged_out(
2089 vm_object_t object,
2090 vm_object_offset_t offset)
2091{
39236c6e
A
2092 if (object->internal &&
2093 object->alive &&
2094 !object->terminating &&
2095 object->pager_ready) {
2096
39037602
A
2097 if (VM_COMPRESSOR_PAGER_STATE_GET(object, offset)
2098 == VM_EXTERNAL_STATE_EXISTS) {
b0d623f7
A
2099 return TRUE;
2100 }
2101 }
b0d623f7
A
2102 return FALSE;
2103}
2104
2105
6d2010ae 2106
39236c6e
A
2107/*
2108 * madvise_free_debug
2109 *
2110 * To help debug madvise(MADV_FREE*) mis-usage, this triggers a
2111 * zero-fill as soon as a page is affected by a madvise(MADV_FREE*), to
2112 * simulate the loss of the page's contents as if the page had been
2113 * reclaimed and then re-faulted.
2114 */
2115#if DEVELOPMENT || DEBUG
2116int madvise_free_debug = 1;
2117#else /* DEBUG */
2118int madvise_free_debug = 0;
2119#endif /* DEBUG */
2120
b0d623f7
A
2121/*
2122 * Deactivate the pages in the specified object and range. If kill_page is set, also discard any
2123 * page modified state from the pmap. Update the chunk_state as we go along. The caller must specify
2124 * a size that is less than or equal to the CHUNK_SIZE.
2125 */
2126
2127static void
2128deactivate_pages_in_object(
2129 vm_object_t object,
2130 vm_object_offset_t offset,
2131 vm_object_size_t size,
2132 boolean_t kill_page,
2133 boolean_t reusable_page,
b0d623f7 2134 boolean_t all_reusable,
39236c6e 2135 chunk_state_t *chunk_state,
3e170ce0
A
2136 pmap_flush_context *pfc,
2137 struct pmap *pmap,
2138 vm_map_offset_t pmap_offset)
b0d623f7
A
2139{
2140 vm_page_t m;
2141 int p;
6d2010ae
A
2142 struct vm_page_delayed_work dw_array[DEFAULT_DELAYED_WORK_LIMIT];
2143 struct vm_page_delayed_work *dwp;
b0d623f7 2144 int dw_count;
6d2010ae 2145 int dw_limit;
b0d623f7
A
2146 unsigned int reusable = 0;
2147
b0d623f7
A
2148 /*
2149 * Examine each page in the chunk. The variable 'p' is the page number relative to the start of the
2150 * chunk. Since this routine is called once for each level in the shadow chain, the chunk_state may
2151 * have pages marked as having been processed already. We stop the loop early if we find we've handled
2152 * all the pages in the chunk.
2153 */
2154
2155 dwp = &dw_array[0];
2156 dw_count = 0;
6d2010ae 2157 dw_limit = DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT);
b0d623f7 2158
3e170ce0 2159 for(p = 0; size && CHUNK_NOT_COMPLETE(*chunk_state); p++, size -= PAGE_SIZE_64, offset += PAGE_SIZE_64, pmap_offset += PAGE_SIZE_64) {
b0d623f7
A
2160
2161 /*
2162 * If this offset has already been found and handled in a higher level object, then don't
2163 * do anything with it in the current shadow object.
2164 */
2165
2166 if (PAGE_ALREADY_HANDLED(*chunk_state, p))
2167 continue;
2168
2169 /*
2170 * See if the page at this offset is around. First check to see if the page is resident,
2171 * then if not, check the existence map or with the pager.
2172 */
2173
2174 if ((m = vm_page_lookup(object, offset)) != VM_PAGE_NULL) {
2175
2176 /*
2177 * We found a page we were looking for. Mark it as "handled" now in the chunk_state
2178 * so that we won't bother looking for a page at this offset again if there are more
2179 * shadow objects. Then deactivate the page.
2180 */
2181
2182 MARK_PAGE_HANDLED(*chunk_state, p);
2183
5ba3f43e
A
2184 if (( !VM_PAGE_WIRED(m)) && (!m->private) && (!m->gobbled) && (!m->busy) &&
2185 (!m->laundry) && (!m->cleaning) && !(m->free_when_done)) {
b0d623f7 2186 int clear_refmod;
fe8ab488 2187 int pmap_options;
b0d623f7 2188
39236c6e
A
2189 dwp->dw_mask = 0;
2190
fe8ab488 2191 pmap_options = 0;
b0d623f7 2192 clear_refmod = VM_MEM_REFERENCED;
39236c6e 2193 dwp->dw_mask |= DW_clear_reference;
b0d623f7
A
2194
2195 if ((kill_page) && (object->internal)) {
39236c6e
A
2196 if (madvise_free_debug) {
2197 /*
2198 * zero-fill the page now
2199 * to simulate it being
2200 * reclaimed and re-faulted.
2201 */
39037602 2202 pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(m));
39236c6e 2203 }
b0d623f7
A
2204 m->precious = FALSE;
2205 m->dirty = FALSE;
2206
2207 clear_refmod |= VM_MEM_MODIFIED;
39037602 2208 if (m->vm_page_q_state == VM_PAGE_ON_THROTTLED_Q) {
d1ecb069
A
2209 /*
2210 * This page is now clean and
2211 * reclaimable. Move it out
2212 * of the throttled queue, so
2213 * that vm_pageout_scan() can
2214 * find it.
2215 */
2216 dwp->dw_mask |= DW_move_page;
2217 }
39037602
A
2218
2219 VM_COMPRESSOR_PAGER_STATE_CLR(object, offset);
b0d623f7
A
2220
2221 if (reusable_page && !m->reusable) {
2222 assert(!all_reusable);
2223 assert(!object->all_reusable);
2224 m->reusable = TRUE;
2225 object->reusable_page_count++;
2226 assert(object->resident_page_count >= object->reusable_page_count);
2227 reusable++;
fe8ab488
A
2228 /*
2229 * Tell pmap this page is now
2230 * "reusable" (to update pmap
2231 * stats for all mappings).
2232 */
2233 pmap_options |= PMAP_OPTIONS_SET_REUSABLE;
b0d623f7
A
2234 }
2235 }
fe8ab488 2236 pmap_options |= PMAP_OPTIONS_NOFLUSH;
39037602 2237 pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE(m),
fe8ab488
A
2238 clear_refmod,
2239 pmap_options,
2240 (void *)pfc);
b0d623f7 2241
39037602 2242 if ((m->vm_page_q_state != VM_PAGE_ON_THROTTLED_Q) && !(reusable_page || all_reusable))
b0d623f7 2243 dwp->dw_mask |= DW_move_page;
6d2010ae 2244
39236c6e
A
2245 if (dwp->dw_mask)
2246 VM_PAGE_ADD_DELAYED_WORK(dwp, m,
2247 dw_count);
b0d623f7 2248
6d2010ae 2249 if (dw_count >= dw_limit) {
b0d623f7
A
2250 if (reusable) {
2251 OSAddAtomic(reusable,
2252 &vm_page_stats_reusable.reusable_count);
2253 vm_page_stats_reusable.reusable += reusable;
2254 reusable = 0;
2255 }
3e170ce0 2256 vm_page_do_delayed_work(object, VM_KERN_MEMORY_NONE, &dw_array[0], dw_count);
b0d623f7
A
2257
2258 dwp = &dw_array[0];
2259 dw_count = 0;
2260 }
2261 }
2262
2263 } else {
2264
2265 /*
2266 * The page at this offset isn't memory resident, check to see if it's
2267 * been paged out. If so, mark it as handled so we don't bother looking
2268 * for it in the shadow chain.
2269 */
2270
2271 if (page_is_paged_out(object, offset)) {
2272 MARK_PAGE_HANDLED(*chunk_state, p);
2273
2274 /*
2275 * If we're killing a non-resident page, then clear the page in the existence
2276 * map so we don't bother paging it back in if it's touched again in the future.
2277 */
2278
2279 if ((kill_page) && (object->internal)) {
39037602
A
2280
2281 VM_COMPRESSOR_PAGER_STATE_CLR(object, offset);
2282
2283 if (pmap != PMAP_NULL) {
3e170ce0
A
2284 /*
2285 * Tell pmap that this page
2286 * is no longer mapped, to
2287 * adjust the footprint ledger
2288 * because this page is no
2289 * longer compressed.
2290 */
2291 pmap_remove_options(
2292 pmap,
2293 pmap_offset,
2294 (pmap_offset +
2295 PAGE_SIZE),
2296 PMAP_OPTIONS_REMOVE);
2297 }
b0d623f7
A
2298 }
2299 }
2300 }
2301 }
2302
2303 if (reusable) {
2304 OSAddAtomic(reusable, &vm_page_stats_reusable.reusable_count);
2305 vm_page_stats_reusable.reusable += reusable;
2306 reusable = 0;
2307 }
2308
2309 if (dw_count)
3e170ce0 2310 vm_page_do_delayed_work(object, VM_KERN_MEMORY_NONE, &dw_array[0], dw_count);
b0d623f7
A
2311}
2312
2313
2314/*
2315 * Deactive a "chunk" of the given range of the object starting at offset. A "chunk"
2316 * will always be less than or equal to the given size. The total range is divided up
2317 * into chunks for efficiency and performance related to the locks and handling the shadow
2318 * chain. This routine returns how much of the given "size" it actually processed. It's
2319 * up to the caler to loop and keep calling this routine until the entire range they want
2320 * to process has been done.
2321 */
2322
2323static vm_object_size_t
2324deactivate_a_chunk(
2325 vm_object_t orig_object,
2326 vm_object_offset_t offset,
2327 vm_object_size_t size,
2328 boolean_t kill_page,
2329 boolean_t reusable_page,
39236c6e 2330 boolean_t all_reusable,
3e170ce0
A
2331 pmap_flush_context *pfc,
2332 struct pmap *pmap,
2333 vm_map_offset_t pmap_offset)
b0d623f7
A
2334{
2335 vm_object_t object;
2336 vm_object_t tmp_object;
2337 vm_object_size_t length;
2338 chunk_state_t chunk_state;
2339
2340
2341 /*
2342 * Get set to do a chunk. We'll do up to CHUNK_SIZE, but no more than the
2343 * remaining size the caller asked for.
2344 */
2345
2346 length = MIN(size, CHUNK_SIZE);
2347
2348 /*
2349 * The chunk_state keeps track of which pages we've already processed if there's
2350 * a shadow chain on this object. At this point, we haven't done anything with this
2351 * range of pages yet, so initialize the state to indicate no pages processed yet.
1c79356b
A
2352 */
2353
b0d623f7
A
2354 CHUNK_INIT(chunk_state, length);
2355 object = orig_object;
1c79356b
A
2356
2357 /*
b0d623f7
A
2358 * Start at the top level object and iterate around the loop once for each object
2359 * in the shadow chain. We stop processing early if we've already found all the pages
2360 * in the range. Otherwise we stop when we run out of shadow objects.
1c79356b 2361 */
0b4e3aa0 2362
b0d623f7
A
2363 while (object && CHUNK_NOT_COMPLETE(chunk_state)) {
2364 vm_object_paging_begin(object);
2365
3e170ce0 2366 deactivate_pages_in_object(object, offset, length, kill_page, reusable_page, all_reusable, &chunk_state, pfc, pmap, pmap_offset);
b0d623f7
A
2367
2368 vm_object_paging_end(object);
2369
2370 /*
2371 * We've finished with this object, see if there's a shadow object. If
2372 * there is, update the offset and lock the new object. We also turn off
2373 * kill_page at this point since we only kill pages in the top most object.
0b4e3aa0 2374 */
1c79356b 2375
b0d623f7
A
2376 tmp_object = object->shadow;
2377
2378 if (tmp_object) {
2379 kill_page = FALSE;
2380 reusable_page = FALSE;
2381 all_reusable = FALSE;
6d2010ae 2382 offset += object->vo_shadow_offset;
b0d623f7
A
2383 vm_object_lock(tmp_object);
2384 }
2385
2386 if (object != orig_object)
2387 vm_object_unlock(object);
2388
2389 object = tmp_object;
1c79356b 2390 }
b0d623f7
A
2391
2392 if (object && object != orig_object)
2393 vm_object_unlock(object);
2394
2395 return length;
1c79356b
A
2396}
2397
b0d623f7
A
2398
2399
1c79356b 2400/*
b0d623f7
A
2401 * Move any resident pages in the specified range to the inactive queue. If kill_page is set,
2402 * we also clear the modified status of the page and "forget" any changes that have been made
2403 * to the page.
1c79356b 2404 */
1c79356b 2405
0b4e3aa0
A
2406__private_extern__ void
2407vm_object_deactivate_pages(
2408 vm_object_t object,
2409 vm_object_offset_t offset,
2410 vm_object_size_t size,
b0d623f7 2411 boolean_t kill_page,
3e170ce0
A
2412 boolean_t reusable_page,
2413 struct pmap *pmap,
2414 vm_map_offset_t pmap_offset)
0b4e3aa0 2415{
b0d623f7
A
2416 vm_object_size_t length;
2417 boolean_t all_reusable;
39236c6e 2418 pmap_flush_context pmap_flush_context_storage;
0b4e3aa0
A
2419
2420 /*
b0d623f7
A
2421 * We break the range up into chunks and do one chunk at a time. This is for
2422 * efficiency and performance while handling the shadow chains and the locks.
2423 * The deactivate_a_chunk() function returns how much of the range it processed.
2424 * We keep calling this routine until the given size is exhausted.
0b4e3aa0 2425 */
0b4e3aa0 2426
0b4e3aa0 2427
b0d623f7 2428 all_reusable = FALSE;
fe8ab488
A
2429#if 11
2430 /*
2431 * For the sake of accurate "reusable" pmap stats, we need
2432 * to tell pmap about each page that is no longer "reusable",
2433 * so we can't do the "all_reusable" optimization.
2434 */
2435#else
b0d623f7 2436 if (reusable_page &&
6d2010ae
A
2437 object->internal &&
2438 object->vo_size != 0 &&
2439 object->vo_size == size &&
b0d623f7
A
2440 object->reusable_page_count == 0) {
2441 all_reusable = TRUE;
2442 reusable_page = FALSE;
2443 }
fe8ab488 2444#endif
0b4e3aa0 2445
d1ecb069
A
2446 if ((reusable_page || all_reusable) && object->all_reusable) {
2447 /* This means MADV_FREE_REUSABLE has been called twice, which
2448 * is probably illegal. */
2449 return;
2450 }
d1ecb069 2451
39236c6e
A
2452 pmap_flush_context_init(&pmap_flush_context_storage);
2453
b0d623f7 2454 while (size) {
3e170ce0 2455 length = deactivate_a_chunk(object, offset, size, kill_page, reusable_page, all_reusable, &pmap_flush_context_storage, pmap, pmap_offset);
0b4e3aa0 2456
b0d623f7
A
2457 size -= length;
2458 offset += length;
3e170ce0 2459 pmap_offset += length;
b0d623f7 2460 }
39236c6e 2461 pmap_flush(&pmap_flush_context_storage);
91447636 2462
b0d623f7
A
2463 if (all_reusable) {
2464 if (!object->all_reusable) {
2465 unsigned int reusable;
2466
2467 object->all_reusable = TRUE;
2468 assert(object->reusable_page_count == 0);
2469 /* update global stats */
2470 reusable = object->resident_page_count;
2471 OSAddAtomic(reusable,
2472 &vm_page_stats_reusable.reusable_count);
2473 vm_page_stats_reusable.reusable += reusable;
2474 vm_page_stats_reusable.all_reusable_calls++;
2475 }
2476 } else if (reusable_page) {
2477 vm_page_stats_reusable.partial_reusable_calls++;
2478 }
2479}
0b4e3aa0 2480
b0d623f7
A
2481void
2482vm_object_reuse_pages(
2483 vm_object_t object,
2484 vm_object_offset_t start_offset,
2485 vm_object_offset_t end_offset,
2486 boolean_t allow_partial_reuse)
2487{
2488 vm_object_offset_t cur_offset;
2489 vm_page_t m;
2490 unsigned int reused, reusable;
0b4e3aa0 2491
b0d623f7
A
2492#define VM_OBJECT_REUSE_PAGE(object, m, reused) \
2493 MACRO_BEGIN \
2494 if ((m) != VM_PAGE_NULL && \
2495 (m)->reusable) { \
2496 assert((object)->reusable_page_count <= \
2497 (object)->resident_page_count); \
2498 assert((object)->reusable_page_count > 0); \
2499 (object)->reusable_page_count--; \
2500 (m)->reusable = FALSE; \
2501 (reused)++; \
fe8ab488
A
2502 /* \
2503 * Tell pmap that this page is no longer \
2504 * "reusable", to update the "reusable" stats \
2505 * for all the pmaps that have mapped this \
2506 * page. \
2507 */ \
39037602 2508 pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE((m)), \
fe8ab488
A
2509 0, /* refmod */ \
2510 (PMAP_OPTIONS_CLEAR_REUSABLE \
2511 | PMAP_OPTIONS_NOFLUSH), \
2512 NULL); \
b0d623f7
A
2513 } \
2514 MACRO_END
2d21ac55 2515
b0d623f7
A
2516 reused = 0;
2517 reusable = 0;
0b4e3aa0 2518
b0d623f7 2519 vm_object_lock_assert_exclusive(object);
0b4e3aa0 2520
b0d623f7 2521 if (object->all_reusable) {
fe8ab488
A
2522 panic("object %p all_reusable: can't update pmap stats\n",
2523 object);
b0d623f7
A
2524 assert(object->reusable_page_count == 0);
2525 object->all_reusable = FALSE;
6d2010ae 2526 if (end_offset - start_offset == object->vo_size ||
b0d623f7
A
2527 !allow_partial_reuse) {
2528 vm_page_stats_reusable.all_reuse_calls++;
2529 reused = object->resident_page_count;
2530 } else {
2531 vm_page_stats_reusable.partial_reuse_calls++;
39037602 2532 vm_page_queue_iterate(&object->memq, m, vm_page_t, listq) {
b0d623f7
A
2533 if (m->offset < start_offset ||
2534 m->offset >= end_offset) {
2535 m->reusable = TRUE;
2536 object->reusable_page_count++;
2537 assert(object->resident_page_count >= object->reusable_page_count);
2538 continue;
2539 } else {
2540 assert(!m->reusable);
2541 reused++;
0b4e3aa0
A
2542 }
2543 }
2544 }
b0d623f7
A
2545 } else if (object->resident_page_count >
2546 ((end_offset - start_offset) >> PAGE_SHIFT)) {
2547 vm_page_stats_reusable.partial_reuse_calls++;
2548 for (cur_offset = start_offset;
2549 cur_offset < end_offset;
2550 cur_offset += PAGE_SIZE_64) {
2551 if (object->reusable_page_count == 0) {
2552 break;
2553 }
2554 m = vm_page_lookup(object, cur_offset);
2555 VM_OBJECT_REUSE_PAGE(object, m, reused);
2556 }
2557 } else {
2558 vm_page_stats_reusable.partial_reuse_calls++;
39037602 2559 vm_page_queue_iterate(&object->memq, m, vm_page_t, listq) {
b0d623f7
A
2560 if (object->reusable_page_count == 0) {
2561 break;
2562 }
2563 if (m->offset < start_offset ||
2564 m->offset >= end_offset) {
2565 continue;
2566 }
2567 VM_OBJECT_REUSE_PAGE(object, m, reused);
2568 }
0b4e3aa0 2569 }
b0d623f7
A
2570
2571 /* update global stats */
2572 OSAddAtomic(reusable-reused, &vm_page_stats_reusable.reusable_count);
2573 vm_page_stats_reusable.reused += reused;
2574 vm_page_stats_reusable.reusable += reusable;
0b4e3aa0 2575}
1c79356b
A
2576
2577/*
2578 * Routine: vm_object_pmap_protect
2579 *
2580 * Purpose:
2581 * Reduces the permission for all physical
2582 * pages in the specified object range.
2583 *
2584 * If removing write permission only, it is
2585 * sufficient to protect only the pages in
2586 * the top-level object; only those pages may
2587 * have write permission.
2588 *
2589 * If removing all access, we must follow the
2590 * shadow chain from the top-level object to
2591 * remove access to all pages in shadowed objects.
2592 *
2593 * The object must *not* be locked. The object must
5ba3f43e 2594 * be internal.
1c79356b
A
2595 *
2596 * If pmap is not NULL, this routine assumes that
2597 * the only mappings for the pages are in that
2598 * pmap.
2599 */
2600
0b4e3aa0 2601__private_extern__ void
1c79356b 2602vm_object_pmap_protect(
39037602
A
2603 vm_object_t object,
2604 vm_object_offset_t offset,
91447636 2605 vm_object_size_t size,
1c79356b 2606 pmap_t pmap,
91447636 2607 vm_map_offset_t pmap_start,
1c79356b
A
2608 vm_prot_t prot)
2609{
39236c6e
A
2610 vm_object_pmap_protect_options(object, offset, size,
2611 pmap, pmap_start, prot, 0);
2612}
2613
2614__private_extern__ void
2615vm_object_pmap_protect_options(
39037602
A
2616 vm_object_t object,
2617 vm_object_offset_t offset,
39236c6e
A
2618 vm_object_size_t size,
2619 pmap_t pmap,
2620 vm_map_offset_t pmap_start,
2621 vm_prot_t prot,
2622 int options)
2623{
2624 pmap_flush_context pmap_flush_context_storage;
2625 boolean_t delayed_pmap_flush = FALSE;
2626
1c79356b 2627 if (object == VM_OBJECT_NULL)
39236c6e 2628 return;
91447636
A
2629 size = vm_object_round_page(size);
2630 offset = vm_object_trunc_page(offset);
1c79356b
A
2631
2632 vm_object_lock(object);
2633
2d21ac55
A
2634 if (object->phys_contiguous) {
2635 if (pmap != NULL) {
2636 vm_object_unlock(object);
39236c6e
A
2637 pmap_protect_options(pmap,
2638 pmap_start,
2639 pmap_start + size,
2640 prot,
2641 options & ~PMAP_OPTIONS_NOFLUSH,
2642 NULL);
2d21ac55
A
2643 } else {
2644 vm_object_offset_t phys_start, phys_end, phys_addr;
2645
6d2010ae 2646 phys_start = object->vo_shadow_offset + offset;
2d21ac55
A
2647 phys_end = phys_start + size;
2648 assert(phys_start <= phys_end);
6d2010ae 2649 assert(phys_end <= object->vo_shadow_offset + object->vo_size);
2d21ac55
A
2650 vm_object_unlock(object);
2651
39236c6e
A
2652 pmap_flush_context_init(&pmap_flush_context_storage);
2653 delayed_pmap_flush = FALSE;
2654
2d21ac55
A
2655 for (phys_addr = phys_start;
2656 phys_addr < phys_end;
2657 phys_addr += PAGE_SIZE_64) {
39236c6e
A
2658 pmap_page_protect_options(
2659 (ppnum_t) (phys_addr >> PAGE_SHIFT),
2660 prot,
2661 options | PMAP_OPTIONS_NOFLUSH,
2662 (void *)&pmap_flush_context_storage);
2663 delayed_pmap_flush = TRUE;
2d21ac55 2664 }
39236c6e
A
2665 if (delayed_pmap_flush == TRUE)
2666 pmap_flush(&pmap_flush_context_storage);
2d21ac55
A
2667 }
2668 return;
2669 }
2670
55e303ae 2671 assert(object->internal);
de355530 2672
1c79356b 2673 while (TRUE) {
91447636 2674 if (ptoa_64(object->resident_page_count) > size/2 && pmap != PMAP_NULL) {
1c79356b 2675 vm_object_unlock(object);
39236c6e
A
2676 pmap_protect_options(pmap, pmap_start, pmap_start + size, prot,
2677 options & ~PMAP_OPTIONS_NOFLUSH, NULL);
1c79356b
A
2678 return;
2679 }
2680
39236c6e
A
2681 pmap_flush_context_init(&pmap_flush_context_storage);
2682 delayed_pmap_flush = FALSE;
2683
2684 /*
2685 * if we are doing large ranges with respect to resident
2686 * page count then we should interate over pages otherwise
2687 * inverse page look-up will be faster
2688 */
91447636 2689 if (ptoa_64(object->resident_page_count / 4) < size) {
9bccf70c
A
2690 vm_page_t p;
2691 vm_object_offset_t end;
1c79356b
A
2692
2693 end = offset + size;
2694
39037602 2695 vm_page_queue_iterate(&object->memq, p, vm_page_t, listq) {
39236c6e
A
2696 if (!p->fictitious && (offset <= p->offset) && (p->offset < end)) {
2697 vm_map_offset_t start;
1c79356b 2698
39236c6e 2699 start = pmap_start + p->offset - offset;
1c79356b 2700
39236c6e
A
2701 if (pmap != PMAP_NULL)
2702 pmap_protect_options(
2703 pmap,
2704 start,
2705 start + PAGE_SIZE_64,
2706 prot,
2707 options | PMAP_OPTIONS_NOFLUSH,
2708 &pmap_flush_context_storage);
2709 else
2710 pmap_page_protect_options(
39037602 2711 VM_PAGE_GET_PHYS_PAGE(p),
39236c6e
A
2712 prot,
2713 options | PMAP_OPTIONS_NOFLUSH,
2714 &pmap_flush_context_storage);
2715 delayed_pmap_flush = TRUE;
2716 }
1c79356b 2717 }
39236c6e 2718
9bccf70c
A
2719 } else {
2720 vm_page_t p;
2721 vm_object_offset_t end;
2722 vm_object_offset_t target_off;
2723
2724 end = offset + size;
2725
39236c6e
A
2726 for (target_off = offset;
2727 target_off < end; target_off += PAGE_SIZE) {
2728
2729 p = vm_page_lookup(object, target_off);
2730
2731 if (p != VM_PAGE_NULL) {
2732 vm_object_offset_t start;
2733
2734 start = pmap_start + (p->offset - offset);
2735
2736 if (pmap != PMAP_NULL)
2737 pmap_protect_options(
2738 pmap,
2739 start,
2740 start + PAGE_SIZE_64,
2741 prot,
2742 options | PMAP_OPTIONS_NOFLUSH,
2743 &pmap_flush_context_storage);
2744 else
2745 pmap_page_protect_options(
39037602 2746 VM_PAGE_GET_PHYS_PAGE(p),
39236c6e
A
2747 prot,
2748 options | PMAP_OPTIONS_NOFLUSH,
2749 &pmap_flush_context_storage);
2750 delayed_pmap_flush = TRUE;
9bccf70c
A
2751 }
2752 }
39236c6e
A
2753 }
2754 if (delayed_pmap_flush == TRUE)
2755 pmap_flush(&pmap_flush_context_storage);
1c79356b
A
2756
2757 if (prot == VM_PROT_NONE) {
2758 /*
2759 * Must follow shadow chain to remove access
2760 * to pages in shadowed objects.
2761 */
39037602 2762 vm_object_t next_object;
1c79356b
A
2763
2764 next_object = object->shadow;
2765 if (next_object != VM_OBJECT_NULL) {
6d2010ae 2766 offset += object->vo_shadow_offset;
1c79356b
A
2767 vm_object_lock(next_object);
2768 vm_object_unlock(object);
2769 object = next_object;
2770 }
2771 else {
2772 /*
2773 * End of chain - we are done.
2774 */
2775 break;
2776 }
2777 }
2778 else {
2779 /*
2780 * Pages in shadowed objects may never have
2781 * write permission - we may stop here.
2782 */
2783 break;
2784 }
2785 }
2786
2787 vm_object_unlock(object);
2788}
2789
2790/*
2791 * Routine: vm_object_copy_slowly
2792 *
2793 * Description:
2794 * Copy the specified range of the source
2795 * virtual memory object without using
2796 * protection-based optimizations (such
2797 * as copy-on-write). The pages in the
2798 * region are actually copied.
2799 *
2800 * In/out conditions:
2801 * The caller must hold a reference and a lock
2802 * for the source virtual memory object. The source
2803 * object will be returned *unlocked*.
2804 *
2805 * Results:
2806 * If the copy is completed successfully, KERN_SUCCESS is
2807 * returned. If the caller asserted the interruptible
2808 * argument, and an interruption occurred while waiting
2809 * for a user-generated event, MACH_SEND_INTERRUPTED is
2810 * returned. Other values may be returned to indicate
2811 * hard errors during the copy operation.
2812 *
2813 * A new virtual memory object is returned in a
2814 * parameter (_result_object). The contents of this
2815 * new object, starting at a zero offset, are a copy
2816 * of the source memory region. In the event of
2817 * an error, this parameter will contain the value
2818 * VM_OBJECT_NULL.
2819 */
0b4e3aa0 2820__private_extern__ kern_return_t
1c79356b 2821vm_object_copy_slowly(
39037602 2822 vm_object_t src_object,
1c79356b
A
2823 vm_object_offset_t src_offset,
2824 vm_object_size_t size,
2825 boolean_t interruptible,
2826 vm_object_t *_result_object) /* OUT */
2827{
2828 vm_object_t new_object;
2829 vm_object_offset_t new_offset;
2830
2d21ac55 2831 struct vm_object_fault_info fault_info;
1c79356b
A
2832
2833 XPR(XPR_VM_OBJECT, "v_o_c_slowly obj 0x%x off 0x%x size 0x%x\n",
2834 src_object, src_offset, size, 0, 0);
2835
2836 if (size == 0) {
2837 vm_object_unlock(src_object);
2838 *_result_object = VM_OBJECT_NULL;
2839 return(KERN_INVALID_ARGUMENT);
2840 }
2841
2842 /*
2843 * Prevent destruction of the source object while we copy.
2844 */
2845
2d21ac55 2846 vm_object_reference_locked(src_object);
1c79356b
A
2847 vm_object_unlock(src_object);
2848
2849 /*
2850 * Create a new object to hold the copied pages.
2851 * A few notes:
2852 * We fill the new object starting at offset 0,
2853 * regardless of the input offset.
2854 * We don't bother to lock the new object within
2855 * this routine, since we have the only reference.
2856 */
2857
2858 new_object = vm_object_allocate(size);
2859 new_offset = 0;
2860
2861 assert(size == trunc_page_64(size)); /* Will the loop terminate? */
2862
2d21ac55
A
2863 fault_info.interruptible = interruptible;
2864 fault_info.behavior = VM_BEHAVIOR_SEQUENTIAL;
fe8ab488
A
2865 fault_info.user_tag = 0;
2866 fault_info.pmap_options = 0;
2d21ac55
A
2867 fault_info.lo_offset = src_offset;
2868 fault_info.hi_offset = src_offset + size;
2869 fault_info.no_cache = FALSE;
b0d623f7 2870 fault_info.stealth = TRUE;
6d2010ae
A
2871 fault_info.io_sync = FALSE;
2872 fault_info.cs_bypass = FALSE;
0b4c1975 2873 fault_info.mark_zf_absent = FALSE;
316670eb 2874 fault_info.batch_pmap_op = FALSE;
2d21ac55 2875
1c79356b
A
2876 for ( ;
2877 size != 0 ;
2878 src_offset += PAGE_SIZE_64,
2879 new_offset += PAGE_SIZE_64, size -= PAGE_SIZE_64
2880 ) {
2881 vm_page_t new_page;
2882 vm_fault_return_t result;
2883
2d21ac55
A
2884 vm_object_lock(new_object);
2885
1c79356b
A
2886 while ((new_page = vm_page_alloc(new_object, new_offset))
2887 == VM_PAGE_NULL) {
2d21ac55
A
2888
2889 vm_object_unlock(new_object);
2890
1c79356b
A
2891 if (!vm_page_wait(interruptible)) {
2892 vm_object_deallocate(new_object);
91447636 2893 vm_object_deallocate(src_object);
1c79356b
A
2894 *_result_object = VM_OBJECT_NULL;
2895 return(MACH_SEND_INTERRUPTED);
2896 }
2d21ac55 2897 vm_object_lock(new_object);
1c79356b 2898 }
2d21ac55 2899 vm_object_unlock(new_object);
1c79356b
A
2900
2901 do {
2902 vm_prot_t prot = VM_PROT_READ;
2903 vm_page_t _result_page;
2904 vm_page_t top_page;
1c79356b
A
2905 vm_page_t result_page;
2906 kern_return_t error_code;
39037602
A
2907 vm_object_t result_page_object;
2908
1c79356b
A
2909
2910 vm_object_lock(src_object);
3e170ce0
A
2911
2912 if (src_object->internal &&
2913 src_object->shadow == VM_OBJECT_NULL &&
2914 (vm_page_lookup(src_object,
2915 src_offset) == VM_PAGE_NULL) &&
2916 (src_object->pager == NULL ||
2917 (VM_COMPRESSOR_PAGER_STATE_GET(src_object,
2918 src_offset) ==
2919 VM_EXTERNAL_STATE_ABSENT))) {
2920 /*
2921 * This page is neither resident nor compressed
2922 * and there's no shadow object below
2923 * "src_object", so this page is really missing.
2924 * There's no need to zero-fill it just to copy
2925 * it: let's leave it missing in "new_object"
2926 * and get zero-filled on demand.
2927 */
2928 vm_object_unlock(src_object);
2929 /* free the unused "new_page"... */
2930 vm_object_lock(new_object);
2931 VM_PAGE_FREE(new_page);
2932 new_page = VM_PAGE_NULL;
2933 vm_object_unlock(new_object);
2934 /* ...and go to next page in "src_object" */
2935 result = VM_FAULT_SUCCESS;
2936 break;
2937 }
2938
1c79356b
A
2939 vm_object_paging_begin(src_object);
2940
b0d623f7
A
2941 if (size > (vm_size_t) -1) {
2942 /* 32-bit overflow */
2943 fault_info.cluster_size = (vm_size_t) (0 - PAGE_SIZE);
2944 } else {
2945 fault_info.cluster_size = (vm_size_t) size;
2946 assert(fault_info.cluster_size == size);
2947 }
2d21ac55 2948
1c79356b 2949 XPR(XPR_VM_FAULT,"vm_object_copy_slowly -> vm_fault_page",0,0,0,0,0);
39236c6e 2950 _result_page = VM_PAGE_NULL;
1c79356b 2951 result = vm_fault_page(src_object, src_offset,
2d21ac55 2952 VM_PROT_READ, FALSE,
39236c6e 2953 FALSE, /* page not looked up */
1c79356b
A
2954 &prot, &_result_page, &top_page,
2955 (int *)0,
2d21ac55 2956 &error_code, FALSE, FALSE, &fault_info);
1c79356b
A
2957
2958 switch(result) {
b0d623f7
A
2959 case VM_FAULT_SUCCESS:
2960 result_page = _result_page;
39037602 2961 result_page_object = VM_PAGE_OBJECT(result_page);
1c79356b 2962
b0d623f7 2963 /*
b0d623f7
A
2964 * Copy the page to the new object.
2965 *
2966 * POLICY DECISION:
2967 * If result_page is clean,
2968 * we could steal it instead
2969 * of copying.
2970 */
1c79356b 2971
b0d623f7 2972 vm_page_copy(result_page, new_page);
39037602 2973 vm_object_unlock(result_page_object);
1c79356b 2974
b0d623f7
A
2975 /*
2976 * Let go of both pages (make them
2977 * not busy, perform wakeup, activate).
2978 */
2979 vm_object_lock(new_object);
316670eb 2980 SET_PAGE_DIRTY(new_page, FALSE);
b0d623f7
A
2981 PAGE_WAKEUP_DONE(new_page);
2982 vm_object_unlock(new_object);
1c79356b 2983
39037602 2984 vm_object_lock(result_page_object);
b0d623f7 2985 PAGE_WAKEUP_DONE(result_page);
1c79356b 2986
b0d623f7 2987 vm_page_lockspin_queues();
39037602
A
2988 if ((result_page->vm_page_q_state == VM_PAGE_ON_SPECULATIVE_Q) ||
2989 (result_page->vm_page_q_state == VM_PAGE_NOT_ON_Q)) {
b0d623f7 2990 vm_page_activate(result_page);
39037602 2991 }
b0d623f7
A
2992 vm_page_activate(new_page);
2993 vm_page_unlock_queues();
1c79356b 2994
b0d623f7
A
2995 /*
2996 * Release paging references and
2997 * top-level placeholder page, if any.
2998 */
2999
39037602 3000 vm_fault_cleanup(result_page_object,
b0d623f7
A
3001 top_page);
3002
3003 break;
1c79356b 3004
b0d623f7
A
3005 case VM_FAULT_RETRY:
3006 break;
3007
b0d623f7
A
3008 case VM_FAULT_MEMORY_SHORTAGE:
3009 if (vm_page_wait(interruptible))
1c79356b 3010 break;
b0d623f7 3011 /* fall thru */
1c79356b 3012
b0d623f7
A
3013 case VM_FAULT_INTERRUPTED:
3014 vm_object_lock(new_object);
3015 VM_PAGE_FREE(new_page);
3016 vm_object_unlock(new_object);
3017
3018 vm_object_deallocate(new_object);
3019 vm_object_deallocate(src_object);
3020 *_result_object = VM_OBJECT_NULL;
3021 return(MACH_SEND_INTERRUPTED);
1c79356b 3022
b0d623f7
A
3023 case VM_FAULT_SUCCESS_NO_VM_PAGE:
3024 /* success but no VM page: fail */
3025 vm_object_paging_end(src_object);
3026 vm_object_unlock(src_object);
3027 /*FALLTHROUGH*/
3028 case VM_FAULT_MEMORY_ERROR:
3029 /*
3030 * A policy choice:
3031 * (a) ignore pages that we can't
3032 * copy
3033 * (b) return the null object if
3034 * any page fails [chosen]
3035 */
593a1d5f 3036
b0d623f7
A
3037 vm_object_lock(new_object);
3038 VM_PAGE_FREE(new_page);
3039 vm_object_unlock(new_object);
1c79356b 3040
b0d623f7
A
3041 vm_object_deallocate(new_object);
3042 vm_object_deallocate(src_object);
3043 *_result_object = VM_OBJECT_NULL;
3044 return(error_code ? error_code:
3045 KERN_MEMORY_ERROR);
1c79356b 3046
b0d623f7
A
3047 default:
3048 panic("vm_object_copy_slowly: unexpected error"
3049 " 0x%x from vm_fault_page()\n", result);
1c79356b
A
3050 }
3051 } while (result != VM_FAULT_SUCCESS);
3052 }
3053
3054 /*
3055 * Lose the extra reference, and return our object.
3056 */
1c79356b
A
3057 vm_object_deallocate(src_object);
3058 *_result_object = new_object;
3059 return(KERN_SUCCESS);
3060}
3061
3062/*
3063 * Routine: vm_object_copy_quickly
3064 *
3065 * Purpose:
3066 * Copy the specified range of the source virtual
3067 * memory object, if it can be done without waiting
3068 * for user-generated events.
3069 *
3070 * Results:
3071 * If the copy is successful, the copy is returned in
3072 * the arguments; otherwise, the arguments are not
3073 * affected.
3074 *
3075 * In/out conditions:
3076 * The object should be unlocked on entry and exit.
3077 */
3078
3079/*ARGSUSED*/
0b4e3aa0 3080__private_extern__ boolean_t
1c79356b
A
3081vm_object_copy_quickly(
3082 vm_object_t *_object, /* INOUT */
91447636
A
3083 __unused vm_object_offset_t offset, /* IN */
3084 __unused vm_object_size_t size, /* IN */
1c79356b
A
3085 boolean_t *_src_needs_copy, /* OUT */
3086 boolean_t *_dst_needs_copy) /* OUT */
3087{
3088 vm_object_t object = *_object;
3089 memory_object_copy_strategy_t copy_strategy;
3090
3091 XPR(XPR_VM_OBJECT, "v_o_c_quickly obj 0x%x off 0x%x size 0x%x\n",
3092 *_object, offset, size, 0, 0);
3093 if (object == VM_OBJECT_NULL) {
3094 *_src_needs_copy = FALSE;
3095 *_dst_needs_copy = FALSE;
3096 return(TRUE);
3097 }
3098
3099 vm_object_lock(object);
3100
3101 copy_strategy = object->copy_strategy;
3102
3103 switch (copy_strategy) {
3104 case MEMORY_OBJECT_COPY_SYMMETRIC:
3105
3106 /*
3107 * Symmetric copy strategy.
3108 * Make another reference to the object.
3109 * Leave object/offset unchanged.
3110 */
3111
2d21ac55 3112 vm_object_reference_locked(object);
1c79356b
A
3113 object->shadowed = TRUE;
3114 vm_object_unlock(object);
3115
3116 /*
3117 * Both source and destination must make
3118 * shadows, and the source must be made
3119 * read-only if not already.
3120 */
3121
3122 *_src_needs_copy = TRUE;
3123 *_dst_needs_copy = TRUE;
3124
3125 break;
3126
3127 case MEMORY_OBJECT_COPY_DELAY:
3128 vm_object_unlock(object);
3129 return(FALSE);
3130
3131 default:
3132 vm_object_unlock(object);
3133 return(FALSE);
3134 }
3135 return(TRUE);
3136}
3137
0b4e3aa0
A
3138static int copy_call_count = 0;
3139static int copy_call_sleep_count = 0;
3140static int copy_call_restart_count = 0;
1c79356b
A
3141
3142/*
3143 * Routine: vm_object_copy_call [internal]
3144 *
3145 * Description:
3146 * Copy the source object (src_object), using the
3147 * user-managed copy algorithm.
3148 *
3149 * In/out conditions:
3150 * The source object must be locked on entry. It
3151 * will be *unlocked* on exit.
3152 *
3153 * Results:
3154 * If the copy is successful, KERN_SUCCESS is returned.
3155 * A new object that represents the copied virtual
3156 * memory is returned in a parameter (*_result_object).
3157 * If the return value indicates an error, this parameter
3158 * is not valid.
3159 */
0b4e3aa0 3160static kern_return_t
1c79356b
A
3161vm_object_copy_call(
3162 vm_object_t src_object,
3163 vm_object_offset_t src_offset,
3164 vm_object_size_t size,
3165 vm_object_t *_result_object) /* OUT */
3166{
3167 kern_return_t kr;
3168 vm_object_t copy;
3169 boolean_t check_ready = FALSE;
2d21ac55 3170 uint32_t try_failed_count = 0;
1c79356b
A
3171
3172 /*
3173 * If a copy is already in progress, wait and retry.
3174 *
3175 * XXX
3176 * Consider making this call interruptable, as Mike
3177 * intended it to be.
3178 *
3179 * XXXO
3180 * Need a counter or version or something to allow
3181 * us to use the copy that the currently requesting
3182 * thread is obtaining -- is it worth adding to the
3183 * vm object structure? Depends how common this case it.
3184 */
3185 copy_call_count++;
3186 while (vm_object_wanted(src_object, VM_OBJECT_EVENT_COPY_CALL)) {
9bccf70c 3187 vm_object_sleep(src_object, VM_OBJECT_EVENT_COPY_CALL,
1c79356b 3188 THREAD_UNINT);
1c79356b
A
3189 copy_call_restart_count++;
3190 }
3191
3192 /*
3193 * Indicate (for the benefit of memory_object_create_copy)
3194 * that we want a copy for src_object. (Note that we cannot
3195 * do a real assert_wait before calling memory_object_copy,
3196 * so we simply set the flag.)
3197 */
3198
3199 vm_object_set_wanted(src_object, VM_OBJECT_EVENT_COPY_CALL);
3200 vm_object_unlock(src_object);
3201
3202 /*
3203 * Ask the memory manager to give us a memory object
3204 * which represents a copy of the src object.
3205 * The memory manager may give us a memory object
3206 * which we already have, or it may give us a
3207 * new memory object. This memory object will arrive
3208 * via memory_object_create_copy.
3209 */
3210
3211 kr = KERN_FAILURE; /* XXX need to change memory_object.defs */
3212 if (kr != KERN_SUCCESS) {
3213 return kr;
3214 }
3215
3216 /*
3217 * Wait for the copy to arrive.
3218 */
3219 vm_object_lock(src_object);
3220 while (vm_object_wanted(src_object, VM_OBJECT_EVENT_COPY_CALL)) {
9bccf70c 3221 vm_object_sleep(src_object, VM_OBJECT_EVENT_COPY_CALL,
1c79356b 3222 THREAD_UNINT);
1c79356b
A
3223 copy_call_sleep_count++;
3224 }
3225Retry:
3226 assert(src_object->copy != VM_OBJECT_NULL);
3227 copy = src_object->copy;
3228 if (!vm_object_lock_try(copy)) {
3229 vm_object_unlock(src_object);
2d21ac55
A
3230
3231 try_failed_count++;
3232 mutex_pause(try_failed_count); /* wait a bit */
3233
1c79356b
A
3234 vm_object_lock(src_object);
3235 goto Retry;
3236 }
6d2010ae
A
3237 if (copy->vo_size < src_offset+size)
3238 copy->vo_size = src_offset+size;
1c79356b
A
3239
3240 if (!copy->pager_ready)
3241 check_ready = TRUE;
3242
3243 /*
3244 * Return the copy.
3245 */
3246 *_result_object = copy;
3247 vm_object_unlock(copy);
3248 vm_object_unlock(src_object);
3249
3250 /* Wait for the copy to be ready. */
3251 if (check_ready == TRUE) {
3252 vm_object_lock(copy);
3253 while (!copy->pager_ready) {
9bccf70c 3254 vm_object_sleep(copy, VM_OBJECT_EVENT_PAGER_READY, THREAD_UNINT);
1c79356b
A
3255 }
3256 vm_object_unlock(copy);
3257 }
3258
3259 return KERN_SUCCESS;
3260}
3261
0b4e3aa0
A
3262static int copy_delayed_lock_collisions = 0;
3263static int copy_delayed_max_collisions = 0;
3264static int copy_delayed_lock_contention = 0;
3265static int copy_delayed_protect_iterate = 0;
1c79356b
A
3266
3267/*
3268 * Routine: vm_object_copy_delayed [internal]
3269 *
3270 * Description:
3271 * Copy the specified virtual memory object, using
3272 * the asymmetric copy-on-write algorithm.
3273 *
3274 * In/out conditions:
55e303ae
A
3275 * The src_object must be locked on entry. It will be unlocked
3276 * on exit - so the caller must also hold a reference to it.
1c79356b
A
3277 *
3278 * This routine will not block waiting for user-generated
3279 * events. It is not interruptible.
3280 */
0b4e3aa0 3281__private_extern__ vm_object_t
1c79356b
A
3282vm_object_copy_delayed(
3283 vm_object_t src_object,
3284 vm_object_offset_t src_offset,
2d21ac55
A
3285 vm_object_size_t size,
3286 boolean_t src_object_shared)
1c79356b
A
3287{
3288 vm_object_t new_copy = VM_OBJECT_NULL;
3289 vm_object_t old_copy;
3290 vm_page_t p;
55e303ae 3291 vm_object_size_t copy_size = src_offset + size;
39236c6e
A
3292 pmap_flush_context pmap_flush_context_storage;
3293 boolean_t delayed_pmap_flush = FALSE;
1c79356b 3294
2d21ac55 3295
1c79356b
A
3296 int collisions = 0;
3297 /*
3298 * The user-level memory manager wants to see all of the changes
3299 * to this object, but it has promised not to make any changes on
3300 * its own.
3301 *
3302 * Perform an asymmetric copy-on-write, as follows:
3303 * Create a new object, called a "copy object" to hold
3304 * pages modified by the new mapping (i.e., the copy,
3305 * not the original mapping).
3306 * Record the original object as the backing object for
3307 * the copy object. If the original mapping does not
3308 * change a page, it may be used read-only by the copy.
3309 * Record the copy object in the original object.
3310 * When the original mapping causes a page to be modified,
3311 * it must be copied to a new page that is "pushed" to
3312 * the copy object.
3313 * Mark the new mapping (the copy object) copy-on-write.
3314 * This makes the copy object itself read-only, allowing
3315 * it to be reused if the original mapping makes no
3316 * changes, and simplifying the synchronization required
3317 * in the "push" operation described above.
3318 *
3319 * The copy-on-write is said to be assymetric because the original
3320 * object is *not* marked copy-on-write. A copied page is pushed
3321 * to the copy object, regardless which party attempted to modify
3322 * the page.
3323 *
3324 * Repeated asymmetric copy operations may be done. If the
3325 * original object has not been changed since the last copy, its
3326 * copy object can be reused. Otherwise, a new copy object can be
3327 * inserted between the original object and its previous copy
3328 * object. Since any copy object is read-only, this cannot affect
3329 * affect the contents of the previous copy object.
3330 *
3331 * Note that a copy object is higher in the object tree than the
3332 * original object; therefore, use of the copy object recorded in
3333 * the original object must be done carefully, to avoid deadlock.
3334 */
3335
3e170ce0 3336 copy_size = vm_object_round_page(copy_size);
1c79356b 3337 Retry:
1c79356b 3338
55e303ae
A
3339 /*
3340 * Wait for paging in progress.
3341 */
b0d623f7
A
3342 if (!src_object->true_share &&
3343 (src_object->paging_in_progress != 0 ||
3344 src_object->activity_in_progress != 0)) {
2d21ac55
A
3345 if (src_object_shared == TRUE) {
3346 vm_object_unlock(src_object);
2d21ac55
A
3347 vm_object_lock(src_object);
3348 src_object_shared = FALSE;
b0d623f7 3349 goto Retry;
2d21ac55 3350 }
55e303ae 3351 vm_object_paging_wait(src_object, THREAD_UNINT);
2d21ac55 3352 }
1c79356b
A
3353 /*
3354 * See whether we can reuse the result of a previous
3355 * copy operation.
3356 */
3357
3358 old_copy = src_object->copy;
3359 if (old_copy != VM_OBJECT_NULL) {
2d21ac55
A
3360 int lock_granted;
3361
1c79356b
A
3362 /*
3363 * Try to get the locks (out of order)
3364 */
2d21ac55
A
3365 if (src_object_shared == TRUE)
3366 lock_granted = vm_object_lock_try_shared(old_copy);
3367 else
3368 lock_granted = vm_object_lock_try(old_copy);
3369
3370 if (!lock_granted) {
1c79356b 3371 vm_object_unlock(src_object);
1c79356b 3372
1c79356b
A
3373 if (collisions++ == 0)
3374 copy_delayed_lock_contention++;
2d21ac55
A
3375 mutex_pause(collisions);
3376
3377 /* Heisenberg Rules */
3378 copy_delayed_lock_collisions++;
1c79356b
A
3379
3380 if (collisions > copy_delayed_max_collisions)
3381 copy_delayed_max_collisions = collisions;
3382
2d21ac55
A
3383 if (src_object_shared == TRUE)
3384 vm_object_lock_shared(src_object);
3385 else
3386 vm_object_lock(src_object);
3387
1c79356b
A
3388 goto Retry;
3389 }
3390
3391 /*
3392 * Determine whether the old copy object has
3393 * been modified.
3394 */
3395
3396 if (old_copy->resident_page_count == 0 &&
3397 !old_copy->pager_created) {
3398 /*
3399 * It has not been modified.
3400 *
3401 * Return another reference to
55e303ae
A
3402 * the existing copy-object if
3403 * we can safely grow it (if
3404 * needed).
de355530 3405 */
1c79356b 3406
6d2010ae 3407 if (old_copy->vo_size < copy_size) {
2d21ac55
A
3408 if (src_object_shared == TRUE) {
3409 vm_object_unlock(old_copy);
3410 vm_object_unlock(src_object);
3411
3412 vm_object_lock(src_object);
3413 src_object_shared = FALSE;
3414 goto Retry;
3415 }
55e303ae
A
3416 /*
3417 * We can't perform a delayed copy if any of the
3418 * pages in the extended range are wired (because
3419 * we can't safely take write permission away from
3420 * wired pages). If the pages aren't wired, then
3421 * go ahead and protect them.
3422 */
3423 copy_delayed_protect_iterate++;
2d21ac55 3424
39236c6e
A
3425 pmap_flush_context_init(&pmap_flush_context_storage);
3426 delayed_pmap_flush = FALSE;
3427
39037602 3428 vm_page_queue_iterate(&src_object->memq, p, vm_page_t, listq) {
55e303ae 3429 if (!p->fictitious &&
6d2010ae 3430 p->offset >= old_copy->vo_size &&
55e303ae 3431 p->offset < copy_size) {
b0d623f7 3432 if (VM_PAGE_WIRED(p)) {
55e303ae
A
3433 vm_object_unlock(old_copy);
3434 vm_object_unlock(src_object);
91447636
A
3435
3436 if (new_copy != VM_OBJECT_NULL) {
3437 vm_object_unlock(new_copy);
3438 vm_object_deallocate(new_copy);
3439 }
39236c6e
A
3440 if (delayed_pmap_flush == TRUE)
3441 pmap_flush(&pmap_flush_context_storage);
91447636 3442
55e303ae
A
3443 return VM_OBJECT_NULL;
3444 } else {
39037602 3445 pmap_page_protect_options(VM_PAGE_GET_PHYS_PAGE(p), (VM_PROT_ALL & ~VM_PROT_WRITE),
39236c6e
A
3446 PMAP_OPTIONS_NOFLUSH, (void *)&pmap_flush_context_storage);
3447 delayed_pmap_flush = TRUE;
55e303ae
A
3448 }
3449 }
3450 }
39236c6e
A
3451 if (delayed_pmap_flush == TRUE)
3452 pmap_flush(&pmap_flush_context_storage);
3453
6d2010ae 3454 old_copy->vo_size = copy_size;
55e303ae 3455 }
2d21ac55
A
3456 if (src_object_shared == TRUE)
3457 vm_object_reference_shared(old_copy);
3458 else
3459 vm_object_reference_locked(old_copy);
d7e50217
A
3460 vm_object_unlock(old_copy);
3461 vm_object_unlock(src_object);
91447636
A
3462
3463 if (new_copy != VM_OBJECT_NULL) {
3464 vm_object_unlock(new_copy);
3465 vm_object_deallocate(new_copy);
3466 }
55e303ae 3467 return(old_copy);
d7e50217 3468 }
2d21ac55
A
3469
3470
de355530
A
3471
3472 /*
3473 * Adjust the size argument so that the newly-created
3474 * copy object will be large enough to back either the
55e303ae 3475 * old copy object or the new mapping.
de355530 3476 */
6d2010ae
A
3477 if (old_copy->vo_size > copy_size)
3478 copy_size = old_copy->vo_size;
55e303ae
A
3479
3480 if (new_copy == VM_OBJECT_NULL) {
3481 vm_object_unlock(old_copy);
3482 vm_object_unlock(src_object);
3483 new_copy = vm_object_allocate(copy_size);
3484 vm_object_lock(src_object);
3485 vm_object_lock(new_copy);
2d21ac55
A
3486
3487 src_object_shared = FALSE;
55e303ae
A
3488 goto Retry;
3489 }
6d2010ae 3490 new_copy->vo_size = copy_size;
1c79356b
A
3491
3492 /*
3493 * The copy-object is always made large enough to
3494 * completely shadow the original object, since
3495 * it may have several users who want to shadow
3496 * the original object at different points.
3497 */
3498
3499 assert((old_copy->shadow == src_object) &&
6d2010ae 3500 (old_copy->vo_shadow_offset == (vm_object_offset_t) 0));
1c79356b 3501
55e303ae
A
3502 } else if (new_copy == VM_OBJECT_NULL) {
3503 vm_object_unlock(src_object);
3504 new_copy = vm_object_allocate(copy_size);
3505 vm_object_lock(src_object);
3506 vm_object_lock(new_copy);
2d21ac55
A
3507
3508 src_object_shared = FALSE;
55e303ae
A
3509 goto Retry;
3510 }
3511
3512 /*
3513 * We now have the src object locked, and the new copy object
3514 * allocated and locked (and potentially the old copy locked).
3515 * Before we go any further, make sure we can still perform
3516 * a delayed copy, as the situation may have changed.
3517 *
3518 * Specifically, we can't perform a delayed copy if any of the
3519 * pages in the range are wired (because we can't safely take
3520 * write permission away from wired pages). If the pages aren't
3521 * wired, then go ahead and protect them.
3522 */
3523 copy_delayed_protect_iterate++;
2d21ac55 3524
39236c6e
A
3525 pmap_flush_context_init(&pmap_flush_context_storage);
3526 delayed_pmap_flush = FALSE;
3527
39037602 3528 vm_page_queue_iterate(&src_object->memq, p, vm_page_t, listq) {
55e303ae 3529 if (!p->fictitious && p->offset < copy_size) {
b0d623f7 3530 if (VM_PAGE_WIRED(p)) {
55e303ae
A
3531 if (old_copy)
3532 vm_object_unlock(old_copy);
3533 vm_object_unlock(src_object);
3534 vm_object_unlock(new_copy);
3535 vm_object_deallocate(new_copy);
39236c6e
A
3536
3537 if (delayed_pmap_flush == TRUE)
3538 pmap_flush(&pmap_flush_context_storage);
3539
55e303ae
A
3540 return VM_OBJECT_NULL;
3541 } else {
39037602 3542 pmap_page_protect_options(VM_PAGE_GET_PHYS_PAGE(p), (VM_PROT_ALL & ~VM_PROT_WRITE),
39236c6e
A
3543 PMAP_OPTIONS_NOFLUSH, (void *)&pmap_flush_context_storage);
3544 delayed_pmap_flush = TRUE;
55e303ae
A
3545 }
3546 }
3547 }
39236c6e
A
3548 if (delayed_pmap_flush == TRUE)
3549 pmap_flush(&pmap_flush_context_storage);
3550
55e303ae 3551 if (old_copy != VM_OBJECT_NULL) {
1c79356b
A
3552 /*
3553 * Make the old copy-object shadow the new one.
3554 * It will receive no more pages from the original
3555 * object.
3556 */
3557
2d21ac55
A
3558 /* remove ref. from old_copy */
3559 vm_object_lock_assert_exclusive(src_object);
3560 src_object->ref_count--;
1c79356b 3561 assert(src_object->ref_count > 0);
2d21ac55 3562 vm_object_lock_assert_exclusive(old_copy);
1c79356b 3563 old_copy->shadow = new_copy;
2d21ac55 3564 vm_object_lock_assert_exclusive(new_copy);
1c79356b
A
3565 assert(new_copy->ref_count > 0);
3566 new_copy->ref_count++; /* for old_copy->shadow ref. */
3567
3568#if TASK_SWAPPER
3569 if (old_copy->res_count) {
3570 VM_OBJ_RES_INCR(new_copy);
3571 VM_OBJ_RES_DECR(src_object);
3572 }
3573#endif
3574
3575 vm_object_unlock(old_copy); /* done with old_copy */
1c79356b
A
3576 }
3577
3578 /*
3579 * Point the new copy at the existing object.
3580 */
2d21ac55 3581 vm_object_lock_assert_exclusive(new_copy);
1c79356b 3582 new_copy->shadow = src_object;
6d2010ae 3583 new_copy->vo_shadow_offset = 0;
1c79356b 3584 new_copy->shadowed = TRUE; /* caller must set needs_copy */
2d21ac55
A
3585
3586 vm_object_lock_assert_exclusive(src_object);
3587 vm_object_reference_locked(src_object);
1c79356b 3588 src_object->copy = new_copy;
55e303ae 3589 vm_object_unlock(src_object);
1c79356b
A
3590 vm_object_unlock(new_copy);
3591
1c79356b
A
3592 XPR(XPR_VM_OBJECT,
3593 "vm_object_copy_delayed: used copy object %X for source %X\n",
b0d623f7 3594 new_copy, src_object, 0, 0, 0);
1c79356b 3595
2d21ac55 3596 return new_copy;
1c79356b
A
3597}
3598
3599/*
3600 * Routine: vm_object_copy_strategically
3601 *
3602 * Purpose:
3603 * Perform a copy according to the source object's
3604 * declared strategy. This operation may block,
3605 * and may be interrupted.
3606 */
0b4e3aa0 3607__private_extern__ kern_return_t
1c79356b 3608vm_object_copy_strategically(
39037602 3609 vm_object_t src_object,
1c79356b
A
3610 vm_object_offset_t src_offset,
3611 vm_object_size_t size,
3612 vm_object_t *dst_object, /* OUT */
3613 vm_object_offset_t *dst_offset, /* OUT */
3614 boolean_t *dst_needs_copy) /* OUT */
3615{
3616 boolean_t result;
3617 boolean_t interruptible = THREAD_ABORTSAFE; /* XXX */
2d21ac55 3618 boolean_t object_lock_shared = FALSE;
1c79356b
A
3619 memory_object_copy_strategy_t copy_strategy;
3620
3621 assert(src_object != VM_OBJECT_NULL);
3622
2d21ac55
A
3623 copy_strategy = src_object->copy_strategy;
3624
3625 if (copy_strategy == MEMORY_OBJECT_COPY_DELAY) {
3626 vm_object_lock_shared(src_object);
3627 object_lock_shared = TRUE;
3628 } else
3629 vm_object_lock(src_object);
1c79356b
A
3630
3631 /*
3632 * The copy strategy is only valid if the memory manager
3633 * is "ready". Internal objects are always ready.
3634 */
3635
3636 while (!src_object->internal && !src_object->pager_ready) {
9bccf70c 3637 wait_result_t wait_result;
1c79356b 3638
2d21ac55
A
3639 if (object_lock_shared == TRUE) {
3640 vm_object_unlock(src_object);
3641 vm_object_lock(src_object);
3642 object_lock_shared = FALSE;
3643 continue;
3644 }
9bccf70c
A
3645 wait_result = vm_object_sleep( src_object,
3646 VM_OBJECT_EVENT_PAGER_READY,
3647 interruptible);
3648 if (wait_result != THREAD_AWAKENED) {
3649 vm_object_unlock(src_object);
1c79356b
A
3650 *dst_object = VM_OBJECT_NULL;
3651 *dst_offset = 0;
3652 *dst_needs_copy = FALSE;
3653 return(MACH_SEND_INTERRUPTED);
3654 }
1c79356b
A
3655 }
3656
1c79356b
A
3657 /*
3658 * Use the appropriate copy strategy.
3659 */
3660
3661 switch (copy_strategy) {
55e303ae
A
3662 case MEMORY_OBJECT_COPY_DELAY:
3663 *dst_object = vm_object_copy_delayed(src_object,
2d21ac55 3664 src_offset, size, object_lock_shared);
55e303ae
A
3665 if (*dst_object != VM_OBJECT_NULL) {
3666 *dst_offset = src_offset;
3667 *dst_needs_copy = TRUE;
3668 result = KERN_SUCCESS;
3669 break;
3670 }
3671 vm_object_lock(src_object);
3672 /* fall thru when delayed copy not allowed */
3673
1c79356b
A
3674 case MEMORY_OBJECT_COPY_NONE:
3675 result = vm_object_copy_slowly(src_object, src_offset, size,
3676 interruptible, dst_object);
3677 if (result == KERN_SUCCESS) {
3678 *dst_offset = 0;
3679 *dst_needs_copy = FALSE;
3680 }
3681 break;
3682
3683 case MEMORY_OBJECT_COPY_CALL:
3684 result = vm_object_copy_call(src_object, src_offset, size,
3685 dst_object);
3686 if (result == KERN_SUCCESS) {
3687 *dst_offset = src_offset;
3688 *dst_needs_copy = TRUE;
3689 }
3690 break;
3691
1c79356b 3692 case MEMORY_OBJECT_COPY_SYMMETRIC:
b0d623f7 3693 XPR(XPR_VM_OBJECT, "v_o_c_strategically obj 0x%x off 0x%x size 0x%x\n", src_object, src_offset, size, 0, 0);
1c79356b
A
3694 vm_object_unlock(src_object);
3695 result = KERN_MEMORY_RESTART_COPY;
3696 break;
3697
3698 default:
3699 panic("copy_strategically: bad strategy");
3700 result = KERN_INVALID_ARGUMENT;
3701 }
3702 return(result);
3703}
3704
3705/*
3706 * vm_object_shadow:
3707 *
3708 * Create a new object which is backed by the
3709 * specified existing object range. The source
3710 * object reference is deallocated.
3711 *
3712 * The new object and offset into that object
3713 * are returned in the source parameters.
3714 */
6d2010ae 3715boolean_t vm_object_shadow_check = TRUE;
1c79356b 3716
0b4e3aa0 3717__private_extern__ boolean_t
1c79356b
A
3718vm_object_shadow(
3719 vm_object_t *object, /* IN/OUT */
3720 vm_object_offset_t *offset, /* IN/OUT */
3721 vm_object_size_t length)
3722{
39037602
A
3723 vm_object_t source;
3724 vm_object_t result;
1c79356b
A
3725
3726 source = *object;
e2d2fc5c
A
3727 assert(source != VM_OBJECT_NULL);
3728 if (source == VM_OBJECT_NULL)
3729 return FALSE;
3730
2d21ac55
A
3731#if 0
3732 /*
3733 * XXX FBDP
3734 * This assertion is valid but it gets triggered by Rosetta for example
3735 * due to a combination of vm_remap() that changes a VM object's
3736 * copy_strategy from SYMMETRIC to DELAY and vm_protect(VM_PROT_COPY)
3737 * that then sets "needs_copy" on its map entry. This creates a
3738 * mapping situation that VM should never see and doesn't know how to
3739 * handle.
3740 * It's not clear if this can create any real problem but we should
3741 * look into fixing this, probably by having vm_protect(VM_PROT_COPY)
3742 * do more than just set "needs_copy" to handle the copy-on-write...
3743 * In the meantime, let's disable the assertion.
3744 */
1c79356b 3745 assert(source->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC);
2d21ac55 3746#endif
1c79356b
A
3747
3748 /*
3749 * Determine if we really need a shadow.
6d2010ae
A
3750 *
3751 * If the source object is larger than what we are trying
3752 * to create, then force the shadow creation even if the
3753 * ref count is 1. This will allow us to [potentially]
3754 * collapse the underlying object away in the future
3755 * (freeing up the extra data it might contain and that
3756 * we don't need).
1c79356b 3757 */
39037602
A
3758
3759 assert(source->copy_strategy != MEMORY_OBJECT_COPY_NONE); /* Purgeable objects shouldn't have shadow objects. */
3760
6d2010ae
A
3761 if (vm_object_shadow_check &&
3762 source->vo_size == length &&
3763 source->ref_count == 1 &&
1c79356b 3764 (source->shadow == VM_OBJECT_NULL ||
6d2010ae 3765 source->shadow->copy == VM_OBJECT_NULL) )
1c79356b 3766 {
39037602
A
3767 /* lock the object and check again */
3768 vm_object_lock(source);
3769 if (source->vo_size == length &&
3770 source->ref_count == 1 &&
3771 (source->shadow == VM_OBJECT_NULL ||
3772 source->shadow->copy == VM_OBJECT_NULL))
3773 {
3774 source->shadowed = FALSE;
3775 vm_object_unlock(source);
3776 return FALSE;
3777 }
3778 /* things changed while we were locking "source"... */
3779 vm_object_unlock(source);
1c79356b
A
3780 }
3781
3782 /*
3783 * Allocate a new object with the given length
3784 */
3785
3786 if ((result = vm_object_allocate(length)) == VM_OBJECT_NULL)
3787 panic("vm_object_shadow: no object for shadowing");
3788
3789 /*
3790 * The new object shadows the source object, adding
3791 * a reference to it. Our caller changes his reference
3792 * to point to the new object, removing a reference to
3793 * the source object. Net result: no change of reference
3794 * count.
3795 */
3796 result->shadow = source;
3797
3798 /*
3799 * Store the offset into the source object,
3800 * and fix up the offset into the new object.
3801 */
3802
6d2010ae 3803 result->vo_shadow_offset = *offset;
1c79356b
A
3804
3805 /*
3806 * Return the new things
3807 */
3808
3809 *offset = 0;
3810 *object = result;
3811 return TRUE;
3812}
3813
3814/*
3815 * The relationship between vm_object structures and
0b4e3aa0 3816 * the memory_object requires careful synchronization.
1c79356b 3817 *
0b4e3aa0 3818 * All associations are created by memory_object_create_named
39037602 3819 * for external pagers and vm_object_compressor_pager_create for internal
0b4e3aa0
A
3820 * objects as follows:
3821 *
3822 * pager: the memory_object itself, supplied by
1c79356b
A
3823 * the user requesting a mapping (or the kernel,
3824 * when initializing internal objects); the
3825 * kernel simulates holding send rights by keeping
3826 * a port reference;
0b4e3aa0 3827 *
1c79356b
A
3828 * pager_request:
3829 * the memory object control port,
3830 * created by the kernel; the kernel holds
3831 * receive (and ownership) rights to this
3832 * port, but no other references.
1c79356b
A
3833 *
3834 * When initialization is complete, the "initialized" field
3835 * is asserted. Other mappings using a particular memory object,
3836 * and any references to the vm_object gained through the
3837 * port association must wait for this initialization to occur.
3838 *
3839 * In order to allow the memory manager to set attributes before
3840 * requests (notably virtual copy operations, but also data or
3841 * unlock requests) are made, a "ready" attribute is made available.
3842 * Only the memory manager may affect the value of this attribute.
3843 * Its value does not affect critical kernel functions, such as
3844 * internal object initialization or destruction. [Furthermore,
3845 * memory objects created by the kernel are assumed to be ready
3846 * immediately; the default memory manager need not explicitly
3847 * set the "ready" attribute.]
3848 *
3849 * [Both the "initialized" and "ready" attribute wait conditions
3850 * use the "pager" field as the wait event.]
3851 *
3852 * The port associations can be broken down by any of the
3853 * following routines:
3854 * vm_object_terminate:
3855 * No references to the vm_object remain, and
3856 * the object cannot (or will not) be cached.
3857 * This is the normal case, and is done even
3858 * though one of the other cases has already been
3859 * done.
1c79356b
A
3860 * memory_object_destroy:
3861 * The memory manager has requested that the
0b4e3aa0
A
3862 * kernel relinquish references to the memory
3863 * object. [The memory manager may not want to
3864 * destroy the memory object, but may wish to
3865 * refuse or tear down existing memory mappings.]
3866 *
1c79356b
A
3867 * Each routine that breaks an association must break all of
3868 * them at once. At some later time, that routine must clear
0b4e3aa0 3869 * the pager field and release the memory object references.
1c79356b
A
3870 * [Furthermore, each routine must cope with the simultaneous
3871 * or previous operations of the others.]
3872 *
0b4e3aa0 3873 * Because the pager field may be cleared spontaneously, it
1c79356b
A
3874 * cannot be used to determine whether a memory object has
3875 * ever been associated with a particular vm_object. [This
2d21ac55
A
3876 * knowledge is important to the shadow object mechanism.]
3877 * For this reason, an additional "created" attribute is
3878 * provided.
3879 *
3880 * During various paging operations, the pager reference found in the
3881 * vm_object must be valid. To prevent this from being released,
3882 * (other than being removed, i.e., made null), routines may use
3883 * the vm_object_paging_begin/end routines [actually, macros].
3884 * The implementation uses the "paging_in_progress" and "wanted" fields.
3885 * [Operations that alter the validity of the pager values include the
3886 * termination routines and vm_object_collapse.]
3887 */
1c79356b 3888
1c79356b
A
3889
3890/*
5ba3f43e 3891 * Routine: vm_object_memory_object_associate
1c79356b 3892 * Purpose:
5ba3f43e
A
3893 * Associate a VM object to the given pager.
3894 * If a VM object is not provided, create one.
3895 * Initialize the pager.
1c79356b
A
3896 */
3897vm_object_t
5ba3f43e 3898vm_object_memory_object_associate(
0b4e3aa0 3899 memory_object_t pager,
5ba3f43e 3900 vm_object_t object,
1c79356b 3901 vm_object_size_t size,
0b4e3aa0 3902 boolean_t named)
1c79356b 3903{
5ba3f43e 3904 memory_object_control_t control;
1c79356b 3905
5ba3f43e 3906 assert(pager != MEMORY_OBJECT_NULL);
1c79356b 3907
5ba3f43e
A
3908 if (object != VM_OBJECT_NULL) {
3909 assert(object->internal);
3910 assert(object->pager_created);
3911 assert(!object->pager_initialized);
3912 assert(!object->pager_ready);
3913 } else {
3914 object = vm_object_allocate(size);
3915 assert(object != VM_OBJECT_NULL);
3916 object->internal = FALSE;
3917 object->pager_trusted = FALSE;
3918 /* copy strategy invalid until set by memory manager */
3919 object->copy_strategy = MEMORY_OBJECT_COPY_INVALID;
3920 }
1c79356b
A
3921
3922 /*
5ba3f43e 3923 * Allocate request port.
1c79356b 3924 */
b0d623f7 3925
5ba3f43e
A
3926 control = memory_object_control_allocate(object);
3927 assert (control != MEMORY_OBJECT_CONTROL_NULL);
1c79356b 3928
5ba3f43e 3929 vm_object_lock(object);
1c79356b 3930
5ba3f43e
A
3931 assert(!object->pager_ready);
3932 assert(!object->pager_initialized);
3933 assert(object->pager == NULL);
3934 assert(object->pager_control == NULL);
1c79356b
A
3935
3936 /*
5ba3f43e 3937 * Copy the reference we were given.
1c79356b
A
3938 */
3939
5ba3f43e
A
3940 memory_object_reference(pager);
3941 object->pager_created = TRUE;
3942 object->pager = pager;
3943 object->pager_control = control;
3944 object->pager_ready = FALSE;
1c79356b 3945
5ba3f43e 3946 vm_object_unlock(object);
1c79356b
A
3947
3948 /*
5ba3f43e 3949 * Let the pager know we're using it.
1c79356b
A
3950 */
3951
5ba3f43e
A
3952 (void) memory_object_init(pager,
3953 object->pager_control,
3954 PAGE_SIZE);
1c79356b 3955
5ba3f43e
A
3956 vm_object_lock(object);
3957 if (named)
3958 object->named = TRUE;
3959 if (object->internal) {
3960 object->pager_ready = TRUE;
3961 vm_object_wakeup(object, VM_OBJECT_EVENT_PAGER_READY);
1c79356b 3962 }
5ba3f43e
A
3963
3964 object->pager_initialized = TRUE;
3965 vm_object_wakeup(object, VM_OBJECT_EVENT_INITIALIZED);
3966
1c79356b
A
3967 vm_object_unlock(object);
3968
5ba3f43e 3969 return object;
1c79356b
A
3970}
3971
3972/*
39037602 3973 * Routine: vm_object_compressor_pager_create
1c79356b
A
3974 * Purpose:
3975 * Create a memory object for an internal object.
3976 * In/out conditions:
3977 * The object is locked on entry and exit;
3978 * it may be unlocked within this call.
3979 * Limitations:
3980 * Only one thread may be performing a
39037602 3981 * vm_object_compressor_pager_create on an object at
1c79356b
A
3982 * a time. Presumably, only the pageout
3983 * daemon will be using this routine.
3984 */
3985
39236c6e
A
3986void
3987vm_object_compressor_pager_create(
39037602 3988 vm_object_t object)
39236c6e
A
3989{
3990 memory_object_t pager;
fe8ab488 3991 vm_object_t pager_object = VM_OBJECT_NULL;
39236c6e
A
3992
3993 assert(object != kernel_object);
3994
3995 /*
3996 * Prevent collapse or termination by holding a paging reference
3997 */
3998
3999 vm_object_paging_begin(object);
4000 if (object->pager_created) {
4001 /*
4002 * Someone else got to it first...
4003 * wait for them to finish initializing the ports
4004 */
4005 while (!object->pager_initialized) {
4006 vm_object_sleep(object,
4007 VM_OBJECT_EVENT_INITIALIZED,
4008 THREAD_UNINT);
4009 }
4010 vm_object_paging_end(object);
4011 return;
4012 }
4013
813fb2f6
A
4014 if ((uint32_t) (object->vo_size/PAGE_SIZE) !=
4015 (object->vo_size/PAGE_SIZE)) {
4016#if DEVELOPMENT || DEBUG
4017 printf("vm_object_compressor_pager_create(%p): "
4018 "object size 0x%llx >= 0x%llx\n",
4019 object,
4020 (uint64_t) object->vo_size,
4021 0x0FFFFFFFFULL*PAGE_SIZE);
4022#endif /* DEVELOPMENT || DEBUG */
4023 vm_object_paging_end(object);
4024 return;
4025 }
4026
39236c6e
A
4027 /*
4028 * Indicate that a memory object has been assigned
4029 * before dropping the lock, to prevent a race.
4030 */
4031
4032 object->pager_created = TRUE;
4033 object->paging_offset = 0;
4034
4035 vm_object_unlock(object);
4036
39236c6e
A
4037 /*
4038 * Create the [internal] pager, and associate it with this object.
4039 *
4040 * We make the association here so that vm_object_enter()
4041 * can look up the object to complete initializing it. No
4042 * user will ever map this object.
4043 */
4044 {
39236c6e 4045 /* create our new memory object */
22ba694c
A
4046 assert((uint32_t) (object->vo_size/PAGE_SIZE) ==
4047 (object->vo_size/PAGE_SIZE));
39236c6e 4048 (void) compressor_memory_object_create(
22ba694c 4049 (memory_object_size_t) object->vo_size,
39236c6e 4050 &pager);
22ba694c
A
4051 if (pager == NULL) {
4052 panic("vm_object_compressor_pager_create(): "
4053 "no pager for object %p size 0x%llx\n",
4054 object, (uint64_t) object->vo_size);
4055 }
39236c6e
A
4056 }
4057
39236c6e
A
4058 /*
4059 * A reference was returned by
4060 * memory_object_create(), and it is
5ba3f43e 4061 * copied by vm_object_memory_object_associate().
39236c6e
A
4062 */
4063
5ba3f43e
A
4064 pager_object = vm_object_memory_object_associate(pager,
4065 object,
4066 object->vo_size,
4067 FALSE);
fe8ab488
A
4068 if (pager_object != object) {
4069 panic("vm_object_compressor_pager_create: mismatch (pager: %p, pager_object: %p, orig_object: %p, orig_object size: 0x%llx)\n", pager, pager_object, object, (uint64_t) object->vo_size);
4070 }
39236c6e
A
4071
4072 /*
4073 * Drop the reference we were passed.
4074 */
4075 memory_object_deallocate(pager);
4076
4077 vm_object_lock(object);
4078
4079 /*
4080 * Release the paging reference
4081 */
4082 vm_object_paging_end(object);
4083}
4084
1c79356b
A
4085/*
4086 * Global variables for vm_object_collapse():
4087 *
4088 * Counts for normal collapses and bypasses.
4089 * Debugging variables, to watch or disable collapse.
4090 */
0b4e3aa0
A
4091static long object_collapses = 0;
4092static long object_bypasses = 0;
1c79356b 4093
0b4e3aa0
A
4094static boolean_t vm_object_collapse_allowed = TRUE;
4095static boolean_t vm_object_bypass_allowed = TRUE;
4096
fe8ab488
A
4097void vm_object_do_collapse_compressor(vm_object_t object,
4098 vm_object_t backing_object);
4099void
4100vm_object_do_collapse_compressor(
4101 vm_object_t object,
4102 vm_object_t backing_object)
4103{
4104 vm_object_offset_t new_offset, backing_offset;
4105 vm_object_size_t size;
4106
4107 vm_counters.do_collapse_compressor++;
4108
4109 vm_object_lock_assert_exclusive(object);
4110 vm_object_lock_assert_exclusive(backing_object);
4111
4112 size = object->vo_size;
4113
4114 /*
4115 * Move all compressed pages from backing_object
4116 * to the parent.
4117 */
4118
4119 for (backing_offset = object->vo_shadow_offset;
4120 backing_offset < object->vo_shadow_offset + object->vo_size;
4121 backing_offset += PAGE_SIZE) {
4122 memory_object_offset_t backing_pager_offset;
4123
4124 /* find the next compressed page at or after this offset */
4125 backing_pager_offset = (backing_offset +
4126 backing_object->paging_offset);
4127 backing_pager_offset = vm_compressor_pager_next_compressed(
4128 backing_object->pager,
4129 backing_pager_offset);
4130 if (backing_pager_offset == (memory_object_offset_t) -1) {
4131 /* no more compressed pages */
4132 break;
4133 }
4134 backing_offset = (backing_pager_offset -
4135 backing_object->paging_offset);
4136
4137 new_offset = backing_offset - object->vo_shadow_offset;
4138
4139 if (new_offset >= object->vo_size) {
4140 /* we're out of the scope of "object": done */
4141 break;
4142 }
4143
4144 if ((vm_page_lookup(object, new_offset) != VM_PAGE_NULL) ||
4145 (vm_compressor_pager_state_get(object->pager,
4146 (new_offset +
4147 object->paging_offset)) ==
4148 VM_EXTERNAL_STATE_EXISTS)) {
4149 /*
4150 * This page already exists in object, resident or
4151 * compressed.
4152 * We don't need this compressed page in backing_object
4153 * and it will be reclaimed when we release
4154 * backing_object.
4155 */
4156 continue;
4157 }
4158
4159 /*
4160 * backing_object has this page in the VM compressor and
4161 * we need to transfer it to object.
4162 */
4163 vm_counters.do_collapse_compressor_pages++;
4164 vm_compressor_pager_transfer(
4165 /* destination: */
4166 object->pager,
4167 (new_offset + object->paging_offset),
4168 /* source: */
4169 backing_object->pager,
4170 (backing_offset + backing_object->paging_offset));
4171 }
4172}
4173
1c79356b 4174/*
0b4e3aa0
A
4175 * Routine: vm_object_do_collapse
4176 * Purpose:
4177 * Collapse an object with the object backing it.
4178 * Pages in the backing object are moved into the
4179 * parent, and the backing object is deallocated.
4180 * Conditions:
4181 * Both objects and the cache are locked; the page
4182 * queues are unlocked.
1c79356b
A
4183 *
4184 */
0b4e3aa0 4185static void
1c79356b
A
4186vm_object_do_collapse(
4187 vm_object_t object,
4188 vm_object_t backing_object)
4189{
4190 vm_page_t p, pp;
4191 vm_object_offset_t new_offset, backing_offset;
4192 vm_object_size_t size;
4193
b0d623f7
A
4194 vm_object_lock_assert_exclusive(object);
4195 vm_object_lock_assert_exclusive(backing_object);
4196
fe8ab488
A
4197 assert(object->purgable == VM_PURGABLE_DENY);
4198 assert(backing_object->purgable == VM_PURGABLE_DENY);
4199
6d2010ae
A
4200 backing_offset = object->vo_shadow_offset;
4201 size = object->vo_size;
1c79356b 4202
1c79356b
A
4203 /*
4204 * Move all in-memory pages from backing_object
4205 * to the parent. Pages that have been paged out
4206 * will be overwritten by any of the parent's
4207 * pages that shadow them.
4208 */
4209
39037602 4210 while (!vm_page_queue_empty(&backing_object->memq)) {
1c79356b 4211
39037602 4212 p = (vm_page_t) vm_page_queue_first(&backing_object->memq);
1c79356b
A
4213
4214 new_offset = (p->offset - backing_offset);
4215
4216 assert(!p->busy || p->absent);
91447636 4217
1c79356b
A
4218 /*
4219 * If the parent has a page here, or if
4220 * this page falls outside the parent,
4221 * dispose of it.
4222 *
4223 * Otherwise, move it as planned.
4224 */
4225
4226 if (p->offset < backing_offset || new_offset >= size) {
4227 VM_PAGE_FREE(p);
4228 } else {
4229 pp = vm_page_lookup(object, new_offset);
4230 if (pp == VM_PAGE_NULL) {
4231
fe8ab488
A
4232 if (VM_COMPRESSOR_PAGER_STATE_GET(object,
4233 new_offset)
4234 == VM_EXTERNAL_STATE_EXISTS) {
4235 /*
4236 * Parent object has this page
4237 * in the VM compressor.
4238 * Throw away the backing
4239 * object's page.
4240 */
4241 VM_PAGE_FREE(p);
4242 } else {
4243 /*
4244 * Parent now has no page.
4245 * Move the backing object's page
4246 * up.
4247 */
5ba3f43e 4248 vm_page_rename(p, object, new_offset);
fe8ab488 4249 }
1c79356b
A
4250 } else {
4251 assert(! pp->absent);
4252
4253 /*
4254 * Parent object has a real page.
4255 * Throw away the backing object's
4256 * page.
4257 */
4258 VM_PAGE_FREE(p);
4259 }
4260 }
4261 }
1c79356b 4262
fe8ab488
A
4263 if (vm_object_collapse_compressor_allowed &&
4264 object->pager != MEMORY_OBJECT_NULL &&
4265 backing_object->pager != MEMORY_OBJECT_NULL) {
4266
4267 /* move compressed pages from backing_object to object */
4268 vm_object_do_collapse_compressor(object, backing_object);
4269
4270 } else if (backing_object->pager != MEMORY_OBJECT_NULL) {
1c79356b 4271
fe8ab488
A
4272 assert((!object->pager_created &&
4273 (object->pager == MEMORY_OBJECT_NULL)) ||
4274 (!backing_object->pager_created &&
4275 (backing_object->pager == MEMORY_OBJECT_NULL)));
1c79356b
A
4276 /*
4277 * Move the pager from backing_object to object.
4278 *
4279 * XXX We're only using part of the paging space
4280 * for keeps now... we ought to discard the
4281 * unused portion.
4282 */
4283
55e303ae 4284 assert(!object->paging_in_progress);
b0d623f7 4285 assert(!object->activity_in_progress);
fe8ab488
A
4286 assert(!object->pager_created);
4287 assert(object->pager == NULL);
1c79356b 4288 object->pager = backing_object->pager;
b0d623f7 4289
1c79356b 4290 object->pager_created = backing_object->pager_created;
91447636 4291 object->pager_control = backing_object->pager_control;
1c79356b
A
4292 object->pager_ready = backing_object->pager_ready;
4293 object->pager_initialized = backing_object->pager_initialized;
1c79356b
A
4294 object->paging_offset =
4295 backing_object->paging_offset + backing_offset;
91447636
A
4296 if (object->pager_control != MEMORY_OBJECT_CONTROL_NULL) {
4297 memory_object_control_collapse(object->pager_control,
0b4e3aa0 4298 object);
1c79356b 4299 }
fe8ab488
A
4300 /* the backing_object has lost its pager: reset all fields */
4301 backing_object->pager_created = FALSE;
4302 backing_object->pager_control = NULL;
4303 backing_object->pager_ready = FALSE;
4304 backing_object->paging_offset = 0;
4305 backing_object->pager = NULL;
1c79356b 4306 }
1c79356b
A
4307 /*
4308 * Object now shadows whatever backing_object did.
4309 * Note that the reference to backing_object->shadow
4310 * moves from within backing_object to within object.
4311 */
4312
91447636
A
4313 assert(!object->phys_contiguous);
4314 assert(!backing_object->phys_contiguous);
1c79356b 4315 object->shadow = backing_object->shadow;
91447636 4316 if (object->shadow) {
6d2010ae 4317 object->vo_shadow_offset += backing_object->vo_shadow_offset;
fe8ab488
A
4318 /* "backing_object" gave its shadow to "object" */
4319 backing_object->shadow = VM_OBJECT_NULL;
4320 backing_object->vo_shadow_offset = 0;
91447636
A
4321 } else {
4322 /* no shadow, therefore no shadow offset... */
6d2010ae 4323 object->vo_shadow_offset = 0;
91447636 4324 }
1c79356b 4325 assert((object->shadow == VM_OBJECT_NULL) ||
55e303ae 4326 (object->shadow->copy != backing_object));
1c79356b
A
4327
4328 /*
4329 * Discard backing_object.
4330 *
4331 * Since the backing object has no pages, no
4332 * pager left, and no object references within it,
4333 * all that is necessary is to dispose of it.
4334 */
fe8ab488 4335 object_collapses++;
1c79356b 4336
fe8ab488
A
4337 assert(backing_object->ref_count == 1);
4338 assert(backing_object->resident_page_count == 0);
4339 assert(backing_object->paging_in_progress == 0);
4340 assert(backing_object->activity_in_progress == 0);
4341 assert(backing_object->shadow == VM_OBJECT_NULL);
4342 assert(backing_object->vo_shadow_offset == 0);
4343
4344 if (backing_object->pager != MEMORY_OBJECT_NULL) {
4345 /* ... unless it has a pager; need to terminate pager too */
4346 vm_counters.do_collapse_terminate++;
4347 if (vm_object_terminate(backing_object) != KERN_SUCCESS) {
4348 vm_counters.do_collapse_terminate_failure++;
4349 }
4350 return;
4351 }
4352
4353 assert(backing_object->pager == NULL);
1c79356b 4354
1c79356b
A
4355 backing_object->alive = FALSE;
4356 vm_object_unlock(backing_object);
4357
4358 XPR(XPR_VM_OBJECT, "vm_object_collapse, collapsed 0x%X\n",
b0d623f7 4359 backing_object, 0,0,0,0);
1c79356b 4360
fe8ab488
A
4361#if VM_OBJECT_TRACKING
4362 if (vm_object_tracking_inited) {
4363 btlog_remove_entries_for_element(vm_object_tracking_btlog,
4364 backing_object);
4365 }
4366#endif /* VM_OBJECT_TRACKING */
4367
2d21ac55
A
4368 vm_object_lock_destroy(backing_object);
4369
91447636 4370 zfree(vm_object_zone, backing_object);
1c79356b 4371
1c79356b
A
4372}
4373
0b4e3aa0 4374static void
1c79356b
A
4375vm_object_do_bypass(
4376 vm_object_t object,
4377 vm_object_t backing_object)
4378{
4379 /*
4380 * Make the parent shadow the next object
4381 * in the chain.
4382 */
4383
b0d623f7 4384 vm_object_lock_assert_exclusive(object);
2d21ac55
A
4385 vm_object_lock_assert_exclusive(backing_object);
4386
1c79356b
A
4387#if TASK_SWAPPER
4388 /*
4389 * Do object reference in-line to
4390 * conditionally increment shadow's
4391 * residence count. If object is not
4392 * resident, leave residence count
4393 * on shadow alone.
4394 */
4395 if (backing_object->shadow != VM_OBJECT_NULL) {
4396 vm_object_lock(backing_object->shadow);
2d21ac55 4397 vm_object_lock_assert_exclusive(backing_object->shadow);
1c79356b
A
4398 backing_object->shadow->ref_count++;
4399 if (object->res_count != 0)
4400 vm_object_res_reference(backing_object->shadow);
4401 vm_object_unlock(backing_object->shadow);
4402 }
4403#else /* TASK_SWAPPER */
4404 vm_object_reference(backing_object->shadow);
4405#endif /* TASK_SWAPPER */
4406
91447636
A
4407 assert(!object->phys_contiguous);
4408 assert(!backing_object->phys_contiguous);
1c79356b 4409 object->shadow = backing_object->shadow;
91447636 4410 if (object->shadow) {
6d2010ae 4411 object->vo_shadow_offset += backing_object->vo_shadow_offset;
91447636
A
4412 } else {
4413 /* no shadow, therefore no shadow offset... */
6d2010ae 4414 object->vo_shadow_offset = 0;
91447636 4415 }
1c79356b
A
4416
4417 /*
4418 * Backing object might have had a copy pointer
4419 * to us. If it did, clear it.
4420 */
4421 if (backing_object->copy == object) {
4422 backing_object->copy = VM_OBJECT_NULL;
4423 }
4424
4425 /*
4426 * Drop the reference count on backing_object.
4427#if TASK_SWAPPER
4428 * Since its ref_count was at least 2, it
4429 * will not vanish; so we don't need to call
4430 * vm_object_deallocate.
593a1d5f 4431 * [with a caveat for "named" objects]
1c79356b
A
4432 *
4433 * The res_count on the backing object is
4434 * conditionally decremented. It's possible
4435 * (via vm_pageout_scan) to get here with
4436 * a "swapped" object, which has a 0 res_count,
4437 * in which case, the backing object res_count
4438 * is already down by one.
4439#else
4440 * Don't call vm_object_deallocate unless
4441 * ref_count drops to zero.
4442 *
4443 * The ref_count can drop to zero here if the
4444 * backing object could be bypassed but not
4445 * collapsed, such as when the backing object
4446 * is temporary and cachable.
4447#endif
4448 */
593a1d5f
A
4449 if (backing_object->ref_count > 2 ||
4450 (!backing_object->named && backing_object->ref_count > 1)) {
2d21ac55 4451 vm_object_lock_assert_exclusive(backing_object);
1c79356b
A
4452 backing_object->ref_count--;
4453#if TASK_SWAPPER
4454 if (object->res_count != 0)
4455 vm_object_res_deallocate(backing_object);
4456 assert(backing_object->ref_count > 0);
4457#endif /* TASK_SWAPPER */
4458 vm_object_unlock(backing_object);
4459 } else {
4460
4461 /*
4462 * Drop locks so that we can deallocate
4463 * the backing object.
4464 */
4465
4466#if TASK_SWAPPER
4467 if (object->res_count == 0) {
4468 /* XXX get a reference for the deallocate below */
4469 vm_object_res_reference(backing_object);
4470 }
4471#endif /* TASK_SWAPPER */
316670eb
A
4472 /*
4473 * vm_object_collapse (the caller of this function) is
4474 * now called from contexts that may not guarantee that a
4475 * valid reference is held on the object... w/o a valid
4476 * reference, it is unsafe and unwise (you will definitely
4477 * regret it) to unlock the object and then retake the lock
4478 * since the object may be terminated and recycled in between.
4479 * The "activity_in_progress" reference will keep the object
4480 * 'stable'.
4481 */
4482 vm_object_activity_begin(object);
1c79356b 4483 vm_object_unlock(object);
316670eb 4484
1c79356b
A
4485 vm_object_unlock(backing_object);
4486 vm_object_deallocate(backing_object);
4487
4488 /*
4489 * Relock object. We don't have to reverify
4490 * its state since vm_object_collapse will
4491 * do that for us as it starts at the
4492 * top of its loop.
4493 */
4494
4495 vm_object_lock(object);
316670eb 4496 vm_object_activity_end(object);
1c79356b
A
4497 }
4498
4499 object_bypasses++;
4500}
0b4e3aa0 4501
1c79356b
A
4502
4503/*
4504 * vm_object_collapse:
4505 *
4506 * Perform an object collapse or an object bypass if appropriate.
4507 * The real work of collapsing and bypassing is performed in
4508 * the routines vm_object_do_collapse and vm_object_do_bypass.
4509 *
4510 * Requires that the object be locked and the page queues be unlocked.
4511 *
4512 */
91447636
A
4513static unsigned long vm_object_collapse_calls = 0;
4514static unsigned long vm_object_collapse_objects = 0;
4515static unsigned long vm_object_collapse_do_collapse = 0;
4516static unsigned long vm_object_collapse_do_bypass = 0;
99c3a104 4517
0b4e3aa0 4518__private_extern__ void
1c79356b 4519vm_object_collapse(
39037602
A
4520 vm_object_t object,
4521 vm_object_offset_t hint_offset,
0c530ab8 4522 boolean_t can_bypass)
1c79356b 4523{
39037602
A
4524 vm_object_t backing_object;
4525 unsigned int rcount;
4526 unsigned int size;
91447636 4527 vm_object_t original_object;
b0d623f7
A
4528 int object_lock_type;
4529 int backing_object_lock_type;
91447636
A
4530
4531 vm_object_collapse_calls++;
0b4e3aa0 4532
0c530ab8
A
4533 if (! vm_object_collapse_allowed &&
4534 ! (can_bypass && vm_object_bypass_allowed)) {
1c79356b
A
4535 return;
4536 }
4537
4538 XPR(XPR_VM_OBJECT, "vm_object_collapse, obj 0x%X\n",
b0d623f7 4539 object, 0,0,0,0);
1c79356b 4540
91447636
A
4541 if (object == VM_OBJECT_NULL)
4542 return;
4543
4544 original_object = object;
4545
b0d623f7
A
4546 /*
4547 * The top object was locked "exclusive" by the caller.
4548 * In the first pass, to determine if we can collapse the shadow chain,
4549 * take a "shared" lock on the shadow objects. If we can collapse,
4550 * we'll have to go down the chain again with exclusive locks.
4551 */
4552 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4553 backing_object_lock_type = OBJECT_LOCK_SHARED;
4554
4555retry:
4556 object = original_object;
4557 vm_object_lock_assert_exclusive(object);
4558
1c79356b 4559 while (TRUE) {
91447636 4560 vm_object_collapse_objects++;
1c79356b
A
4561 /*
4562 * Verify that the conditions are right for either
4563 * collapse or bypass:
1c79356b 4564 */
1c79356b
A
4565
4566 /*
4567 * There is a backing object, and
4568 */
4569
91447636
A
4570 backing_object = object->shadow;
4571 if (backing_object == VM_OBJECT_NULL) {
4572 if (object != original_object) {
4573 vm_object_unlock(object);
4574 }
1c79356b 4575 return;
91447636 4576 }
b0d623f7
A
4577 if (backing_object_lock_type == OBJECT_LOCK_SHARED) {
4578 vm_object_lock_shared(backing_object);
4579 } else {
4580 vm_object_lock(backing_object);
4581 }
4582
91447636
A
4583 /*
4584 * No pages in the object are currently
4585 * being paged out, and
4586 */
b0d623f7
A
4587 if (object->paging_in_progress != 0 ||
4588 object->activity_in_progress != 0) {
91447636 4589 /* try and collapse the rest of the shadow chain */
91447636
A
4590 if (object != original_object) {
4591 vm_object_unlock(object);
4592 }
4593 object = backing_object;
b0d623f7 4594 object_lock_type = backing_object_lock_type;
91447636
A
4595 continue;
4596 }
4597
1c79356b
A
4598 /*
4599 * ...
4600 * The backing object is not read_only,
4601 * and no pages in the backing object are
4602 * currently being paged out.
4603 * The backing object is internal.
4604 *
4605 */
4606
4607 if (!backing_object->internal ||
b0d623f7
A
4608 backing_object->paging_in_progress != 0 ||
4609 backing_object->activity_in_progress != 0) {
91447636
A
4610 /* try and collapse the rest of the shadow chain */
4611 if (object != original_object) {
4612 vm_object_unlock(object);
4613 }
4614 object = backing_object;
b0d623f7 4615 object_lock_type = backing_object_lock_type;
91447636 4616 continue;
1c79356b 4617 }
fe8ab488
A
4618
4619 /*
4620 * Purgeable objects are not supposed to engage in
4621 * copy-on-write activities, so should not have
4622 * any shadow objects or be a shadow object to another
4623 * object.
4624 * Collapsing a purgeable object would require some
4625 * updates to the purgeable compressed ledgers.
4626 */
4627 if (object->purgable != VM_PURGABLE_DENY ||
4628 backing_object->purgable != VM_PURGABLE_DENY) {
4629 panic("vm_object_collapse() attempting to collapse "
4630 "purgeable object: %p(%d) %p(%d)\n",
4631 object, object->purgable,
4632 backing_object, backing_object->purgable);
4633 /* try and collapse the rest of the shadow chain */
4634 if (object != original_object) {
4635 vm_object_unlock(object);
4636 }
4637 object = backing_object;
4638 object_lock_type = backing_object_lock_type;
4639 continue;
4640 }
1c79356b
A
4641
4642 /*
4643 * The backing object can't be a copy-object:
4644 * the shadow_offset for the copy-object must stay
4645 * as 0. Furthermore (for the 'we have all the
4646 * pages' case), if we bypass backing_object and
4647 * just shadow the next object in the chain, old
4648 * pages from that object would then have to be copied
4649 * BOTH into the (former) backing_object and into the
4650 * parent object.
4651 */
4652 if (backing_object->shadow != VM_OBJECT_NULL &&
55e303ae 4653 backing_object->shadow->copy == backing_object) {
91447636
A
4654 /* try and collapse the rest of the shadow chain */
4655 if (object != original_object) {
4656 vm_object_unlock(object);
4657 }
4658 object = backing_object;
b0d623f7 4659 object_lock_type = backing_object_lock_type;
91447636 4660 continue;
1c79356b
A
4661 }
4662
4663 /*
4664 * We can now try to either collapse the backing
4665 * object (if the parent is the only reference to
4666 * it) or (perhaps) remove the parent's reference
4667 * to it.
1c79356b 4668 *
0b4e3aa0
A
4669 * If there is exactly one reference to the backing
4670 * object, we may be able to collapse it into the
4671 * parent.
1c79356b 4672 *
55e303ae
A
4673 * As long as one of the objects is still not known
4674 * to the pager, we can collapse them.
1c79356b 4675 */
1c79356b 4676 if (backing_object->ref_count == 1 &&
fe8ab488
A
4677 (vm_object_collapse_compressor_allowed ||
4678 !object->pager_created
39236c6e 4679 || (!backing_object->pager_created)
55e303ae 4680 ) && vm_object_collapse_allowed) {
1c79356b 4681
1c79356b 4682 /*
b0d623f7 4683 * We need the exclusive lock on the VM objects.
1c79356b 4684 */
b0d623f7
A
4685 if (backing_object_lock_type != OBJECT_LOCK_EXCLUSIVE) {
4686 /*
4687 * We have an object and its shadow locked
4688 * "shared". We can't just upgrade the locks
4689 * to "exclusive", as some other thread might
4690 * also have these objects locked "shared" and
4691 * attempt to upgrade one or the other to
4692 * "exclusive". The upgrades would block
4693 * forever waiting for the other "shared" locks
4694 * to get released.
4695 * So we have to release the locks and go
4696 * down the shadow chain again (since it could
4697 * have changed) with "exclusive" locking.
4698 */
1c79356b 4699 vm_object_unlock(backing_object);
b0d623f7
A
4700 if (object != original_object)
4701 vm_object_unlock(object);
4702 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4703 backing_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4704 goto retry;
1c79356b
A
4705 }
4706
b0d623f7
A
4707 XPR(XPR_VM_OBJECT,
4708 "vm_object_collapse: %x to %x, pager %x, pager_control %x\n",
4709 backing_object, object,
4710 backing_object->pager,
4711 backing_object->pager_control, 0);
4712
1c79356b
A
4713 /*
4714 * Collapse the object with its backing
4715 * object, and try again with the object's
4716 * new backing object.
4717 */
4718
4719 vm_object_do_collapse(object, backing_object);
91447636 4720 vm_object_collapse_do_collapse++;
1c79356b
A
4721 continue;
4722 }
4723
1c79356b
A
4724 /*
4725 * Collapsing the backing object was not possible
4726 * or permitted, so let's try bypassing it.
4727 */
4728
0c530ab8 4729 if (! (can_bypass && vm_object_bypass_allowed)) {
91447636
A
4730 /* try and collapse the rest of the shadow chain */
4731 if (object != original_object) {
4732 vm_object_unlock(object);
4733 }
4734 object = backing_object;
b0d623f7 4735 object_lock_type = backing_object_lock_type;
91447636 4736 continue;
1c79356b
A
4737 }
4738
0b4e3aa0 4739
1c79356b 4740 /*
55e303ae
A
4741 * If the object doesn't have all its pages present,
4742 * we have to make sure no pages in the backing object
4743 * "show through" before bypassing it.
1c79356b 4744 */
39236c6e 4745 size = (unsigned int)atop(object->vo_size);
55e303ae 4746 rcount = object->resident_page_count;
99c3a104 4747
55e303ae 4748 if (rcount != size) {
55e303ae
A
4749 vm_object_offset_t offset;
4750 vm_object_offset_t backing_offset;
4751 unsigned int backing_rcount;
55e303ae
A
4752
4753 /*
4754 * If the backing object has a pager but no pagemap,
4755 * then we cannot bypass it, because we don't know
4756 * what pages it has.
4757 */
39037602 4758 if (backing_object->pager_created) {
91447636
A
4759 /* try and collapse the rest of the shadow chain */
4760 if (object != original_object) {
4761 vm_object_unlock(object);
4762 }
4763 object = backing_object;
b0d623f7 4764 object_lock_type = backing_object_lock_type;
91447636 4765 continue;
55e303ae 4766 }
1c79356b 4767
55e303ae
A
4768 /*
4769 * If the object has a pager but no pagemap,
4770 * then we cannot bypass it, because we don't know
4771 * what pages it has.
4772 */
39037602 4773 if (object->pager_created) {
91447636
A
4774 /* try and collapse the rest of the shadow chain */
4775 if (object != original_object) {
4776 vm_object_unlock(object);
4777 }
4778 object = backing_object;
b0d623f7 4779 object_lock_type = backing_object_lock_type;
91447636 4780 continue;
55e303ae 4781 }
0b4e3aa0 4782
99c3a104
A
4783 backing_offset = object->vo_shadow_offset;
4784 backing_rcount = backing_object->resident_page_count;
4785
4786 if ( (int)backing_rcount - (int)(atop(backing_object->vo_size) - size) > (int)rcount) {
39236c6e 4787 /*
99c3a104
A
4788 * we have enough pages in the backing object to guarantee that
4789 * at least 1 of them must be 'uncovered' by a resident page
4790 * in the object we're evaluating, so move on and
4791 * try to collapse the rest of the shadow chain
4792 */
39236c6e
A
4793 if (object != original_object) {
4794 vm_object_unlock(object);
4795 }
4796 object = backing_object;
4797 object_lock_type = backing_object_lock_type;
4798 continue;
99c3a104
A
4799 }
4800
55e303ae
A
4801 /*
4802 * If all of the pages in the backing object are
4803 * shadowed by the parent object, the parent
4804 * object no longer has to shadow the backing
4805 * object; it can shadow the next one in the
4806 * chain.
4807 *
4808 * If the backing object has existence info,
4809 * we must check examine its existence info
4810 * as well.
4811 *
4812 */
1c79356b 4813
39236c6e
A
4814#define EXISTS_IN_OBJECT(obj, off, rc) \
4815 ((VM_COMPRESSOR_PAGER_STATE_GET((obj), (off)) \
4816 == VM_EXTERNAL_STATE_EXISTS) || \
99c3a104 4817 ((rc) && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
55e303ae
A
4818
4819 /*
4820 * Check the hint location first
4821 * (since it is often the quickest way out of here).
4822 */
4823 if (object->cow_hint != ~(vm_offset_t)0)
4824 hint_offset = (vm_object_offset_t)object->cow_hint;
4825 else
4826 hint_offset = (hint_offset > 8 * PAGE_SIZE_64) ?
4827 (hint_offset - 8 * PAGE_SIZE_64) : 0;
4828
4829 if (EXISTS_IN_OBJECT(backing_object, hint_offset +
4830 backing_offset, backing_rcount) &&
4831 !EXISTS_IN_OBJECT(object, hint_offset, rcount)) {
4832 /* dependency right at the hint */
b0d623f7 4833 object->cow_hint = (vm_offset_t) hint_offset; /* atomic */
91447636
A
4834 /* try and collapse the rest of the shadow chain */
4835 if (object != original_object) {
4836 vm_object_unlock(object);
4837 }
4838 object = backing_object;
b0d623f7 4839 object_lock_type = backing_object_lock_type;
91447636 4840 continue;
0b4e3aa0 4841 }
55e303ae
A
4842
4843 /*
4844 * If the object's window onto the backing_object
4845 * is large compared to the number of resident
4846 * pages in the backing object, it makes sense to
4847 * walk the backing_object's resident pages first.
4848 *
99c3a104
A
4849 * NOTE: Pages may be in both the existence map and/or
4850 * resident, so if we don't find a dependency while
4851 * walking the backing object's resident page list
4852 * directly, and there is an existence map, we'll have
4853 * to run the offset based 2nd pass. Because we may
4854 * have to run both passes, we need to be careful
4855 * not to decrement 'rcount' in the 1st pass
55e303ae 4856 */
99c3a104 4857 if (backing_rcount && backing_rcount < (size / 8)) {
55e303ae
A
4858 unsigned int rc = rcount;
4859 vm_page_t p;
4860
4861 backing_rcount = backing_object->resident_page_count;
39037602 4862 p = (vm_page_t)vm_page_queue_first(&backing_object->memq);
55e303ae 4863 do {
55e303ae 4864 offset = (p->offset - backing_offset);
99c3a104 4865
6d2010ae 4866 if (offset < object->vo_size &&
55e303ae
A
4867 offset != hint_offset &&
4868 !EXISTS_IN_OBJECT(object, offset, rc)) {
4869 /* found a dependency */
b0d623f7
A
4870 object->cow_hint = (vm_offset_t) offset; /* atomic */
4871
91447636 4872 break;
55e303ae 4873 }
39037602 4874 p = (vm_page_t) vm_page_queue_next(&p->listq);
55e303ae
A
4875
4876 } while (--backing_rcount);
91447636
A
4877 if (backing_rcount != 0 ) {
4878 /* try and collapse the rest of the shadow chain */
4879 if (object != original_object) {
4880 vm_object_unlock(object);
4881 }
4882 object = backing_object;
b0d623f7 4883 object_lock_type = backing_object_lock_type;
91447636
A
4884 continue;
4885 }
0b4e3aa0 4886 }
55e303ae
A
4887
4888 /*
4889 * Walk through the offsets looking for pages in the
4890 * backing object that show through to the object.
4891 */
39037602 4892 if (backing_rcount) {
55e303ae
A
4893 offset = hint_offset;
4894
4895 while((offset =
6d2010ae 4896 (offset + PAGE_SIZE_64 < object->vo_size) ?
55e303ae
A
4897 (offset + PAGE_SIZE_64) : 0) != hint_offset) {
4898
55e303ae
A
4899 if (EXISTS_IN_OBJECT(backing_object, offset +
4900 backing_offset, backing_rcount) &&
4901 !EXISTS_IN_OBJECT(object, offset, rcount)) {
4902 /* found a dependency */
b0d623f7 4903 object->cow_hint = (vm_offset_t) offset; /* atomic */
91447636 4904 break;
55e303ae
A
4905 }
4906 }
91447636
A
4907 if (offset != hint_offset) {
4908 /* try and collapse the rest of the shadow chain */
4909 if (object != original_object) {
4910 vm_object_unlock(object);
4911 }
4912 object = backing_object;
b0d623f7 4913 object_lock_type = backing_object_lock_type;
91447636
A
4914 continue;
4915 }
0b4e3aa0
A
4916 }
4917 }
1c79356b 4918
b0d623f7
A
4919 /*
4920 * We need "exclusive" locks on the 2 VM objects.
4921 */
4922 if (backing_object_lock_type != OBJECT_LOCK_EXCLUSIVE) {
4923 vm_object_unlock(backing_object);
4924 if (object != original_object)
4925 vm_object_unlock(object);
4926 object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4927 backing_object_lock_type = OBJECT_LOCK_EXCLUSIVE;
4928 goto retry;
4929 }
4930
55e303ae
A
4931 /* reset the offset hint for any objects deeper in the chain */
4932 object->cow_hint = (vm_offset_t)0;
1c79356b
A
4933
4934 /*
4935 * All interesting pages in the backing object
4936 * already live in the parent or its pager.
4937 * Thus we can bypass the backing object.
4938 */
4939
4940 vm_object_do_bypass(object, backing_object);
91447636 4941 vm_object_collapse_do_bypass++;
1c79356b
A
4942
4943 /*
4944 * Try again with this object's new backing object.
4945 */
4946
4947 continue;
4948 }
91447636 4949
fe8ab488
A
4950 /* NOT REACHED */
4951 /*
91447636
A
4952 if (object != original_object) {
4953 vm_object_unlock(object);
4954 }
fe8ab488 4955 */
1c79356b
A
4956}
4957
4958/*
4959 * Routine: vm_object_page_remove: [internal]
4960 * Purpose:
4961 * Removes all physical pages in the specified
4962 * object range from the object's list of pages.
4963 *
4964 * In/out conditions:
4965 * The object must be locked.
4966 * The object must not have paging_in_progress, usually
4967 * guaranteed by not having a pager.
4968 */
4969unsigned int vm_object_page_remove_lookup = 0;
4970unsigned int vm_object_page_remove_iterate = 0;
4971
0b4e3aa0 4972__private_extern__ void
1c79356b 4973vm_object_page_remove(
39037602
A
4974 vm_object_t object,
4975 vm_object_offset_t start,
4976 vm_object_offset_t end)
1c79356b 4977{
39037602 4978 vm_page_t p, next;
1c79356b
A
4979
4980 /*
4981 * One and two page removals are most popular.
4982 * The factor of 16 here is somewhat arbitrary.
4983 * It balances vm_object_lookup vs iteration.
4984 */
4985
55e303ae 4986 if (atop_64(end - start) < (unsigned)object->resident_page_count/16) {
1c79356b
A
4987 vm_object_page_remove_lookup++;
4988
4989 for (; start < end; start += PAGE_SIZE_64) {
4990 p = vm_page_lookup(object, start);
4991 if (p != VM_PAGE_NULL) {
39037602 4992 assert(!p->cleaning && !p->laundry);
2d21ac55 4993 if (!p->fictitious && p->pmapped)
39037602 4994 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(p));
1c79356b
A
4995 VM_PAGE_FREE(p);
4996 }
4997 }
4998 } else {
4999 vm_object_page_remove_iterate++;
5000
39037602
A
5001 p = (vm_page_t) vm_page_queue_first(&object->memq);
5002 while (!vm_page_queue_end(&object->memq, (vm_page_queue_entry_t) p)) {
5003 next = (vm_page_t) vm_page_queue_next(&p->listq);
1c79356b 5004 if ((start <= p->offset) && (p->offset < end)) {
39037602 5005 assert(!p->cleaning && !p->laundry);
2d21ac55 5006 if (!p->fictitious && p->pmapped)
39037602 5007 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(p));
1c79356b
A
5008 VM_PAGE_FREE(p);
5009 }
5010 p = next;
5011 }
5012 }
5013}
5014
0b4e3aa0 5015
1c79356b
A
5016/*
5017 * Routine: vm_object_coalesce
5018 * Function: Coalesces two objects backing up adjoining
5019 * regions of memory into a single object.
5020 *
5021 * returns TRUE if objects were combined.
5022 *
5023 * NOTE: Only works at the moment if the second object is NULL -
5024 * if it's not, which object do we lock first?
5025 *
5026 * Parameters:
5027 * prev_object First object to coalesce
5028 * prev_offset Offset into prev_object
5029 * next_object Second object into coalesce
5030 * next_offset Offset into next_object
5031 *
5032 * prev_size Size of reference to prev_object
5033 * next_size Size of reference to next_object
5034 *
5035 * Conditions:
5036 * The object(s) must *not* be locked. The map must be locked
5037 * to preserve the reference to the object(s).
5038 */
0b4e3aa0 5039static int vm_object_coalesce_count = 0;
1c79356b 5040
0b4e3aa0 5041__private_extern__ boolean_t
1c79356b 5042vm_object_coalesce(
39037602 5043 vm_object_t prev_object,
1c79356b
A
5044 vm_object_t next_object,
5045 vm_object_offset_t prev_offset,
91447636 5046 __unused vm_object_offset_t next_offset,
1c79356b
A
5047 vm_object_size_t prev_size,
5048 vm_object_size_t next_size)
5049{
5050 vm_object_size_t newsize;
5051
5052#ifdef lint
5053 next_offset++;
5054#endif /* lint */
5055
5056 if (next_object != VM_OBJECT_NULL) {
5057 return(FALSE);
5058 }
5059
5060 if (prev_object == VM_OBJECT_NULL) {
5061 return(TRUE);
5062 }
5063
5064 XPR(XPR_VM_OBJECT,
5065 "vm_object_coalesce: 0x%X prev_off 0x%X prev_size 0x%X next_size 0x%X\n",
b0d623f7 5066 prev_object, prev_offset, prev_size, next_size, 0);
1c79356b
A
5067
5068 vm_object_lock(prev_object);
5069
5070 /*
5071 * Try to collapse the object first
5072 */
0c530ab8 5073 vm_object_collapse(prev_object, prev_offset, TRUE);
1c79356b
A
5074
5075 /*
5076 * Can't coalesce if pages not mapped to
5077 * prev_entry may be in use any way:
5078 * . more than one reference
5079 * . paged out
5080 * . shadows another object
5081 * . has a copy elsewhere
2d21ac55 5082 * . is purgeable
1c79356b
A
5083 * . paging references (pages might be in page-list)
5084 */
5085
5086 if ((prev_object->ref_count > 1) ||
5087 prev_object->pager_created ||
5088 (prev_object->shadow != VM_OBJECT_NULL) ||
5089 (prev_object->copy != VM_OBJECT_NULL) ||
5090 (prev_object->true_share != FALSE) ||
2d21ac55 5091 (prev_object->purgable != VM_PURGABLE_DENY) ||
b0d623f7
A
5092 (prev_object->paging_in_progress != 0) ||
5093 (prev_object->activity_in_progress != 0)) {
1c79356b
A
5094 vm_object_unlock(prev_object);
5095 return(FALSE);
5096 }
5097
5098 vm_object_coalesce_count++;
5099
5100 /*
5101 * Remove any pages that may still be in the object from
5102 * a previous deallocation.
5103 */
5104 vm_object_page_remove(prev_object,
5105 prev_offset + prev_size,
5106 prev_offset + prev_size + next_size);
5107
5108 /*
5109 * Extend the object if necessary.
5110 */
5111 newsize = prev_offset + prev_size + next_size;
6d2010ae 5112 if (newsize > prev_object->vo_size) {
6d2010ae 5113 prev_object->vo_size = newsize;
1c79356b
A
5114 }
5115
5116 vm_object_unlock(prev_object);
5117 return(TRUE);
5118}
5119
0b4e3aa0
A
5120kern_return_t
5121vm_object_populate_with_private(
55e303ae 5122 vm_object_t object,
0b4e3aa0 5123 vm_object_offset_t offset,
55e303ae
A
5124 ppnum_t phys_page,
5125 vm_size_t size)
0b4e3aa0 5126{
55e303ae 5127 ppnum_t base_page;
0b4e3aa0
A
5128 vm_object_offset_t base_offset;
5129
5130
316670eb 5131 if (!object->private)
0b4e3aa0
A
5132 return KERN_FAILURE;
5133
55e303ae 5134 base_page = phys_page;
0b4e3aa0
A
5135
5136 vm_object_lock(object);
316670eb
A
5137
5138 if (!object->phys_contiguous) {
0b4e3aa0 5139 vm_page_t m;
316670eb
A
5140
5141 if ((base_offset = trunc_page_64(offset)) != offset) {
0b4e3aa0
A
5142 vm_object_unlock(object);
5143 return KERN_FAILURE;
5144 }
5145 base_offset += object->paging_offset;
316670eb
A
5146
5147 while (size) {
0b4e3aa0 5148 m = vm_page_lookup(object, base_offset);
316670eb
A
5149
5150 if (m != VM_PAGE_NULL) {
5151 if (m->fictitious) {
39037602 5152 if (VM_PAGE_GET_PHYS_PAGE(m) != vm_page_guard_addr) {
b0d623f7 5153
2d21ac55 5154 vm_page_lockspin_queues();
2d21ac55 5155 m->private = TRUE;
b0d623f7
A
5156 vm_page_unlock_queues();
5157
5158 m->fictitious = FALSE;
39037602 5159 VM_PAGE_SET_PHYS_PAGE(m, base_page);
0b4e3aa0 5160 }
39037602 5161 } else if (VM_PAGE_GET_PHYS_PAGE(m) != base_page) {
316670eb
A
5162
5163 if ( !m->private) {
5164 /*
5165 * we'd leak a real page... that can't be right
5166 */
5167 panic("vm_object_populate_with_private - %p not private", m);
5168 }
5169 if (m->pmapped) {
2d21ac55
A
5170 /*
5171 * pmap call to clear old mapping
5172 */
39037602 5173 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m));
2d21ac55 5174 }
39037602 5175 VM_PAGE_SET_PHYS_PAGE(m, base_page);
0b4e3aa0 5176 }
91447636 5177
0b4e3aa0 5178 } else {
b0d623f7 5179 while ((m = vm_page_grab_fictitious()) == VM_PAGE_NULL)
0b4e3aa0 5180 vm_page_more_fictitious();
b0d623f7
A
5181
5182 /*
5183 * private normally requires lock_queues but since we
5184 * are initializing the page, its not necessary here
5185 */
0b4e3aa0 5186 m->private = TRUE;
b0d623f7 5187 m->fictitious = FALSE;
39037602 5188 VM_PAGE_SET_PHYS_PAGE(m, base_page);
0b4e3aa0 5189 m->unusual = TRUE;
316670eb 5190 m->busy = FALSE;
b0d623f7 5191
0b4e3aa0
A
5192 vm_page_insert(m, object, base_offset);
5193 }
55e303ae 5194 base_page++; /* Go to the next physical page */
0b4e3aa0
A
5195 base_offset += PAGE_SIZE;
5196 size -= PAGE_SIZE;
5197 }
5198 } else {
5199 /* NOTE: we should check the original settings here */
5200 /* if we have a size > zero a pmap call should be made */
5201 /* to disable the range */
5202
5203 /* pmap_? */
5204
5205 /* shadows on contiguous memory are not allowed */
5206 /* we therefore can use the offset field */
6d2010ae
A
5207 object->vo_shadow_offset = (vm_object_offset_t)phys_page << PAGE_SHIFT;
5208 object->vo_size = size;
0b4e3aa0
A
5209 }
5210 vm_object_unlock(object);
316670eb 5211
0b4e3aa0
A
5212 return KERN_SUCCESS;
5213}
5214
1c79356b
A
5215
5216kern_return_t
0b4e3aa0
A
5217memory_object_create_named(
5218 memory_object_t pager,
5219 memory_object_offset_t size,
5220 memory_object_control_t *control)
1c79356b 5221{
0b4e3aa0 5222 vm_object_t object;
1c79356b 5223
0b4e3aa0
A
5224 *control = MEMORY_OBJECT_CONTROL_NULL;
5225 if (pager == MEMORY_OBJECT_NULL)
5226 return KERN_INVALID_ARGUMENT;
1c79356b 5227
5ba3f43e
A
5228 object = vm_object_memory_object_associate(pager,
5229 VM_OBJECT_NULL,
5230 size,
5231 TRUE);
5232 if (object == VM_OBJECT_NULL) {
5233 return KERN_INVALID_OBJECT;
0b4e3aa0
A
5234 }
5235
5236 /* wait for object (if any) to be ready */
5237 if (object != VM_OBJECT_NULL) {
5238 vm_object_lock(object);
5239 object->named = TRUE;
5240 while (!object->pager_ready) {
9bccf70c
A
5241 vm_object_sleep(object,
5242 VM_OBJECT_EVENT_PAGER_READY,
5243 THREAD_UNINT);
0b4e3aa0 5244 }
91447636 5245 *control = object->pager_control;
0b4e3aa0
A
5246 vm_object_unlock(object);
5247 }
5248 return (KERN_SUCCESS);
5249}
1c79356b 5250
1c79356b 5251
0b4e3aa0
A
5252/*
5253 * Routine: memory_object_recover_named [user interface]
5254 * Purpose:
5255 * Attempt to recover a named reference for a VM object.
5256 * VM will verify that the object has not already started
5257 * down the termination path, and if it has, will optionally
5258 * wait for that to finish.
5259 * Returns:
5260 * KERN_SUCCESS - we recovered a named reference on the object
5261 * KERN_FAILURE - we could not recover a reference (object dead)
5262 * KERN_INVALID_ARGUMENT - bad memory object control
5263 */
5264kern_return_t
5265memory_object_recover_named(
5266 memory_object_control_t control,
5267 boolean_t wait_on_terminating)
5268{
5269 vm_object_t object;
1c79356b 5270
0b4e3aa0
A
5271 object = memory_object_control_to_vm_object(control);
5272 if (object == VM_OBJECT_NULL) {
0b4e3aa0
A
5273 return (KERN_INVALID_ARGUMENT);
5274 }
0b4e3aa0
A
5275restart:
5276 vm_object_lock(object);
1c79356b 5277
0b4e3aa0 5278 if (object->terminating && wait_on_terminating) {
0b4e3aa0
A
5279 vm_object_wait(object,
5280 VM_OBJECT_EVENT_PAGING_IN_PROGRESS,
5281 THREAD_UNINT);
0b4e3aa0
A
5282 goto restart;
5283 }
5284
5285 if (!object->alive) {
0b4e3aa0
A
5286 vm_object_unlock(object);
5287 return KERN_FAILURE;
1c79356b
A
5288 }
5289
0b4e3aa0 5290 if (object->named == TRUE) {
0b4e3aa0
A
5291 vm_object_unlock(object);
5292 return KERN_SUCCESS;
5293 }
0b4e3aa0 5294 object->named = TRUE;
2d21ac55 5295 vm_object_lock_assert_exclusive(object);
0b4e3aa0
A
5296 object->ref_count++;
5297 vm_object_res_reference(object);
5298 while (!object->pager_ready) {
9bccf70c
A
5299 vm_object_sleep(object,
5300 VM_OBJECT_EVENT_PAGER_READY,
5301 THREAD_UNINT);
0b4e3aa0
A
5302 }
5303 vm_object_unlock(object);
5304 return (KERN_SUCCESS);
1c79356b
A
5305}
5306
0b4e3aa0
A
5307
5308/*
5309 * vm_object_release_name:
5310 *
5311 * Enforces name semantic on memory_object reference count decrement
5312 * This routine should not be called unless the caller holds a name
5313 * reference gained through the memory_object_create_named.
5314 *
5315 * If the TERMINATE_IDLE flag is set, the call will return if the
5316 * reference count is not 1. i.e. idle with the only remaining reference
5317 * being the name.
5318 * If the decision is made to proceed the name field flag is set to
5319 * false and the reference count is decremented. If the RESPECT_CACHE
5320 * flag is set and the reference count has gone to zero, the
5321 * memory_object is checked to see if it is cacheable otherwise when
5322 * the reference count is zero, it is simply terminated.
5323 */
5324
5325__private_extern__ kern_return_t
5326vm_object_release_name(
5327 vm_object_t object,
5328 int flags)
1c79356b 5329{
0b4e3aa0
A
5330 vm_object_t shadow;
5331 boolean_t original_object = TRUE;
1c79356b 5332
0b4e3aa0 5333 while (object != VM_OBJECT_NULL) {
1c79356b 5334
0b4e3aa0 5335 vm_object_lock(object);
b0d623f7 5336
0b4e3aa0 5337 assert(object->alive);
b0d623f7 5338 if (original_object)
0b4e3aa0
A
5339 assert(object->named);
5340 assert(object->ref_count > 0);
5341
5342 /*
5343 * We have to wait for initialization before
5344 * destroying or caching the object.
5345 */
5346
5347 if (object->pager_created && !object->pager_initialized) {
5348 assert(!object->can_persist);
5349 vm_object_assert_wait(object,
5350 VM_OBJECT_EVENT_INITIALIZED,
5351 THREAD_UNINT);
5352 vm_object_unlock(object);
9bccf70c 5353 thread_block(THREAD_CONTINUE_NULL);
0b4e3aa0 5354 continue;
1c79356b
A
5355 }
5356
0b4e3aa0
A
5357 if (((object->ref_count > 1)
5358 && (flags & MEMORY_OBJECT_TERMINATE_IDLE))
5359 || (object->terminating)) {
5360 vm_object_unlock(object);
0b4e3aa0
A
5361 return KERN_FAILURE;
5362 } else {
5363 if (flags & MEMORY_OBJECT_RELEASE_NO_OP) {
5364 vm_object_unlock(object);
0b4e3aa0 5365 return KERN_SUCCESS;
1c79356b 5366 }
0b4e3aa0
A
5367 }
5368
5369 if ((flags & MEMORY_OBJECT_RESPECT_CACHE) &&
5370 (object->ref_count == 1)) {
b0d623f7 5371 if (original_object)
0b4e3aa0 5372 object->named = FALSE;
1c79356b 5373 vm_object_unlock(object);
0b4e3aa0
A
5374 /* let vm_object_deallocate push this thing into */
5375 /* the cache, if that it is where it is bound */
5376 vm_object_deallocate(object);
5377 return KERN_SUCCESS;
5378 }
5379 VM_OBJ_RES_DECR(object);
5380 shadow = object->pageout?VM_OBJECT_NULL:object->shadow;
b0d623f7
A
5381
5382 if (object->ref_count == 1) {
5383 if (vm_object_terminate(object) != KERN_SUCCESS) {
5384 if (original_object) {
0b4e3aa0
A
5385 return KERN_FAILURE;
5386 } else {
5387 return KERN_SUCCESS;
5388 }
5389 }
5390 if (shadow != VM_OBJECT_NULL) {
5391 original_object = FALSE;
5392 object = shadow;
5393 continue;
5394 }
5395 return KERN_SUCCESS;
5396 } else {
2d21ac55 5397 vm_object_lock_assert_exclusive(object);
0b4e3aa0
A
5398 object->ref_count--;
5399 assert(object->ref_count > 0);
5400 if(original_object)
5401 object->named = FALSE;
5402 vm_object_unlock(object);
0b4e3aa0 5403 return KERN_SUCCESS;
1c79356b 5404 }
1c79356b 5405 }
91447636
A
5406 /*NOTREACHED*/
5407 assert(0);
5408 return KERN_FAILURE;
1c79356b
A
5409}
5410
0b4e3aa0
A
5411
5412__private_extern__ kern_return_t
5413vm_object_lock_request(
5414 vm_object_t object,
5415 vm_object_offset_t offset,
5416 vm_object_size_t size,
5417 memory_object_return_t should_return,
5418 int flags,
5419 vm_prot_t prot)
1c79356b 5420{
91447636
A
5421 __unused boolean_t should_flush;
5422
5423 should_flush = flags & MEMORY_OBJECT_DATA_FLUSH;
1c79356b 5424
0b4e3aa0
A
5425 XPR(XPR_MEMORY_OBJECT,
5426 "vm_o_lock_request, obj 0x%X off 0x%X size 0x%X flags %X prot %X\n",
b0d623f7 5427 object, offset, size,
0b4e3aa0 5428 (((should_return&1)<<1)|should_flush), prot);
1c79356b 5429
0b4e3aa0
A
5430 /*
5431 * Check for bogus arguments.
5432 */
5433 if (object == VM_OBJECT_NULL)
5434 return (KERN_INVALID_ARGUMENT);
1c79356b 5435
0b4e3aa0
A
5436 if ((prot & ~VM_PROT_ALL) != 0 && prot != VM_PROT_NO_CHANGE)
5437 return (KERN_INVALID_ARGUMENT);
1c79356b 5438
55e303ae 5439 size = round_page_64(size);
0b4e3aa0
A
5440
5441 /*
5442 * Lock the object, and acquire a paging reference to
5443 * prevent the memory_object reference from being released.
5444 */
5445 vm_object_lock(object);
5446 vm_object_paging_begin(object);
0b4e3aa0
A
5447
5448 (void)vm_object_update(object,
91447636 5449 offset, size, NULL, NULL, should_return, flags, prot);
0b4e3aa0
A
5450
5451 vm_object_paging_end(object);
5452 vm_object_unlock(object);
5453
5454 return (KERN_SUCCESS);
5455}
5456
91447636 5457/*
2d21ac55 5458 * Empty a purgeable object by grabbing the physical pages assigned to it and
91447636
A
5459 * putting them on the free queue without writing them to backing store, etc.
5460 * When the pages are next touched they will be demand zero-fill pages. We
5461 * skip pages which are busy, being paged in/out, wired, etc. We do _not_
5462 * skip referenced/dirty pages, pages on the active queue, etc. We're more
2d21ac55 5463 * than happy to grab these since this is a purgeable object. We mark the
91447636
A
5464 * object as "empty" after reaping its pages.
5465 *
b0d623f7
A
5466 * On entry the object must be locked and it must be
5467 * purgeable with no delayed copies pending.
91447636 5468 */
b0d623f7 5469void
fe8ab488 5470vm_object_purge(vm_object_t object, int flags)
91447636 5471{
4bd07ac2
A
5472 unsigned int object_page_count = 0;
5473 unsigned int pgcount = 0;
5474 boolean_t skipped_object = FALSE;
5475
b0d623f7 5476 vm_object_lock_assert_exclusive(object);
0b4e3aa0 5477
b0d623f7
A
5478 if (object->purgable == VM_PURGABLE_DENY)
5479 return;
91447636
A
5480
5481 assert(object->copy == VM_OBJECT_NULL);
5482 assert(object->copy_strategy == MEMORY_OBJECT_COPY_NONE);
593a1d5f 5483
fe8ab488
A
5484 /*
5485 * We need to set the object's state to VM_PURGABLE_EMPTY *before*
5486 * reaping its pages. We update vm_page_purgeable_count in bulk
5487 * and we don't want vm_page_remove() to update it again for each
5488 * page we reap later.
5489 *
5490 * For the purgeable ledgers, pages from VOLATILE and EMPTY objects
5491 * are all accounted for in the "volatile" ledgers, so this does not
5492 * make any difference.
5493 * If we transitioned directly from NONVOLATILE to EMPTY,
5494 * vm_page_purgeable_count must have been updated when the object
5495 * was dequeued from its volatile queue and the purgeable ledgers
5496 * must have also been updated accordingly at that time (in
5497 * vm_object_purgable_control()).
5498 */
5499 if (object->purgable == VM_PURGABLE_VOLATILE) {
b0d623f7
A
5500 unsigned int delta;
5501 assert(object->resident_page_count >=
5502 object->wired_page_count);
5503 delta = (object->resident_page_count -
5504 object->wired_page_count);
5505 if (delta != 0) {
5506 assert(vm_page_purgeable_count >=
5507 delta);
5508 OSAddAtomic(-delta,
5509 (SInt32 *)&vm_page_purgeable_count);
91447636 5510 }
b0d623f7
A
5511 if (object->wired_page_count != 0) {
5512 assert(vm_page_purgeable_wired_count >=
5513 object->wired_page_count);
5514 OSAddAtomic(-object->wired_page_count,
5515 (SInt32 *)&vm_page_purgeable_wired_count);
91447636 5516 }
fe8ab488 5517 object->purgable = VM_PURGABLE_EMPTY;
91447636 5518 }
fe8ab488 5519 assert(object->purgable == VM_PURGABLE_EMPTY);
b0d623f7 5520
4bd07ac2
A
5521 object_page_count = object->resident_page_count;
5522
b0d623f7 5523 vm_object_reap_pages(object, REAP_PURGEABLE);
fe8ab488 5524
39037602
A
5525 if (object->pager != NULL) {
5526
5527 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT);
fe8ab488
A
5528
5529 if (object->activity_in_progress == 0 &&
5530 object->paging_in_progress == 0) {
5531 /*
5532 * Also reap any memory coming from this object
5533 * in the VM compressor.
5534 *
5535 * There are no operations in progress on the VM object
5536 * and no operation can start while we're holding the
5537 * VM object lock, so it's safe to reap the compressed
5538 * pages and update the page counts.
5539 */
5540 pgcount = vm_compressor_pager_get_count(object->pager);
5541 if (pgcount) {
5542 pgcount = vm_compressor_pager_reap_pages(object->pager, flags);
5543 vm_compressor_pager_count(object->pager,
5544 -pgcount,
5545 FALSE, /* shared */
5546 object);
5547 vm_purgeable_compressed_update(object,
5548 -pgcount);
5549 }
5550 if ( !(flags & C_DONT_BLOCK)) {
5551 assert(vm_compressor_pager_get_count(object->pager)
5552 == 0);
5553 }
5554 } else {
5555 /*
5556 * There's some kind of paging activity in progress
5557 * for this object, which could result in a page
5558 * being compressed or decompressed, possibly while
5559 * the VM object is not locked, so it could race
5560 * with us.
5561 *
5562 * We can't really synchronize this without possibly
5563 * causing a deadlock when the compressor needs to
5564 * allocate or free memory while compressing or
5565 * decompressing a page from a purgeable object
5566 * mapped in the kernel_map...
5567 *
5568 * So let's not attempt to purge the compressor
5569 * pager if there's any kind of operation in
5570 * progress on the VM object.
5571 */
4bd07ac2 5572 skipped_object = TRUE;
fe8ab488
A
5573 }
5574 }
5575
5576 vm_object_lock_assert_exclusive(object);
4bd07ac2
A
5577
5578 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (MACHDBG_CODE(DBG_MACH_VM, OBJECT_PURGE_ONE)),
5579 VM_KERNEL_UNSLIDE_OR_PERM(object), /* purged object */
5580 object_page_count,
5581 pgcount,
5582 skipped_object,
5583 0);
5584
91447636 5585}
b0d623f7 5586
91447636
A
5587
5588/*
2d21ac55
A
5589 * vm_object_purgeable_control() allows the caller to control and investigate the
5590 * state of a purgeable object. A purgeable object is created via a call to
5591 * vm_allocate() with VM_FLAGS_PURGABLE specified. A purgeable object will
5592 * never be coalesced with any other object -- even other purgeable objects --
5593 * and will thus always remain a distinct object. A purgeable object has
91447636 5594 * special semantics when its reference count is exactly 1. If its reference
2d21ac55 5595 * count is greater than 1, then a purgeable object will behave like a normal
91447636
A
5596 * object and attempts to use this interface will result in an error return
5597 * of KERN_INVALID_ARGUMENT.
5598 *
2d21ac55 5599 * A purgeable object may be put into a "volatile" state which will make the
91447636
A
5600 * object's pages elligable for being reclaimed without paging to backing
5601 * store if the system runs low on memory. If the pages in a volatile
2d21ac55
A
5602 * purgeable object are reclaimed, the purgeable object is said to have been
5603 * "emptied." When a purgeable object is emptied the system will reclaim as
91447636
A
5604 * many pages from the object as it can in a convenient manner (pages already
5605 * en route to backing store or busy for other reasons are left as is). When
2d21ac55 5606 * a purgeable object is made volatile, its pages will generally be reclaimed
91447636
A
5607 * before other pages in the application's working set. This semantic is
5608 * generally used by applications which can recreate the data in the object
5609 * faster than it can be paged in. One such example might be media assets
5610 * which can be reread from a much faster RAID volume.
5611 *
2d21ac55 5612 * A purgeable object may be designated as "non-volatile" which means it will
91447636
A
5613 * behave like all other objects in the system with pages being written to and
5614 * read from backing store as needed to satisfy system memory needs. If the
5615 * object was emptied before the object was made non-volatile, that fact will
2d21ac55 5616 * be returned as the old state of the purgeable object (see
91447636
A
5617 * VM_PURGABLE_SET_STATE below). In this case, any pages of the object which
5618 * were reclaimed as part of emptying the object will be refaulted in as
5619 * zero-fill on demand. It is up to the application to note that an object
5620 * was emptied and recreate the objects contents if necessary. When a
2d21ac55
A
5621 * purgeable object is made non-volatile, its pages will generally not be paged
5622 * out to backing store in the immediate future. A purgeable object may also
91447636
A
5623 * be manually emptied.
5624 *
5625 * Finally, the current state (non-volatile, volatile, volatile & empty) of a
2d21ac55 5626 * volatile purgeable object may be queried at any time. This information may
91447636
A
5627 * be used as a control input to let the application know when the system is
5628 * experiencing memory pressure and is reclaiming memory.
5629 *
2d21ac55 5630 * The specified address may be any address within the purgeable object. If
91447636
A
5631 * the specified address does not represent any object in the target task's
5632 * virtual address space, then KERN_INVALID_ADDRESS will be returned. If the
2d21ac55 5633 * object containing the specified address is not a purgeable object, then
91447636
A
5634 * KERN_INVALID_ARGUMENT will be returned. Otherwise, KERN_SUCCESS will be
5635 * returned.
5636 *
5637 * The control parameter may be any one of VM_PURGABLE_SET_STATE or
5638 * VM_PURGABLE_GET_STATE. For VM_PURGABLE_SET_STATE, the in/out parameter
2d21ac55
A
5639 * state is used to set the new state of the purgeable object and return its
5640 * old state. For VM_PURGABLE_GET_STATE, the current state of the purgeable
91447636
A
5641 * object is returned in the parameter state.
5642 *
5643 * The in/out parameter state may be one of VM_PURGABLE_NONVOLATILE,
5644 * VM_PURGABLE_VOLATILE or VM_PURGABLE_EMPTY. These, respectively, represent
5645 * the non-volatile, volatile and volatile/empty states described above.
2d21ac55 5646 * Setting the state of a purgeable object to VM_PURGABLE_EMPTY will
91447636
A
5647 * immediately reclaim as many pages in the object as can be conveniently
5648 * collected (some may have already been written to backing store or be
5649 * otherwise busy).
5650 *
2d21ac55
A
5651 * The process of making a purgeable object non-volatile and determining its
5652 * previous state is atomic. Thus, if a purgeable object is made
91447636 5653 * VM_PURGABLE_NONVOLATILE and the old state is returned as
2d21ac55 5654 * VM_PURGABLE_VOLATILE, then the purgeable object's previous contents are
91447636
A
5655 * completely intact and will remain so until the object is made volatile
5656 * again. If the old state is returned as VM_PURGABLE_EMPTY then the object
5657 * was reclaimed while it was in a volatile state and its previous contents
5658 * have been lost.
5659 */
5660/*
5661 * The object must be locked.
5662 */
5663kern_return_t
5664vm_object_purgable_control(
5665 vm_object_t object,
5666 vm_purgable_t control,
5667 int *state)
5668{
5669 int old_state;
2d21ac55 5670 int new_state;
91447636
A
5671
5672 if (object == VM_OBJECT_NULL) {
5673 /*
2d21ac55 5674 * Object must already be present or it can't be purgeable.
91447636
A
5675 */
5676 return KERN_INVALID_ARGUMENT;
5677 }
5678
fe8ab488
A
5679 vm_object_lock_assert_exclusive(object);
5680
91447636 5681 /*
2d21ac55 5682 * Get current state of the purgeable object.
91447636 5683 */
2d21ac55
A
5684 old_state = object->purgable;
5685 if (old_state == VM_PURGABLE_DENY)
91447636
A
5686 return KERN_INVALID_ARGUMENT;
5687
2d21ac55 5688 /* purgeable cant have delayed copies - now or in the future */
91447636
A
5689 assert(object->copy == VM_OBJECT_NULL);
5690 assert(object->copy_strategy == MEMORY_OBJECT_COPY_NONE);
5691
5692 /*
5693 * Execute the desired operation.
5694 */
5695 if (control == VM_PURGABLE_GET_STATE) {
5696 *state = old_state;
5697 return KERN_SUCCESS;
5698 }
5699
5ba3f43e
A
5700 if (control == VM_PURGABLE_SET_STATE &&
5701 object->purgeable_only_by_kernel) {
5702 return KERN_PROTECTION_FAILURE;
5703 }
5704
5705 if (control != VM_PURGABLE_SET_STATE &&
5706 control != VM_PURGABLE_SET_STATE_FROM_KERNEL) {
5707 return KERN_INVALID_ARGUMENT;
5708 }
5709
b0d623f7
A
5710 if ((*state) & VM_PURGABLE_DEBUG_EMPTY) {
5711 object->volatile_empty = TRUE;
5712 }
5713 if ((*state) & VM_PURGABLE_DEBUG_FAULT) {
5714 object->volatile_fault = TRUE;
5715 }
5716
2d21ac55 5717 new_state = *state & VM_PURGABLE_STATE_MASK;
813fb2f6
A
5718 if (new_state == VM_PURGABLE_VOLATILE) {
5719 if (old_state == VM_PURGABLE_EMPTY) {
5720 /* what's been emptied must stay empty */
5721 new_state = VM_PURGABLE_EMPTY;
5722 }
5723 if (object->volatile_empty) {
5724 /* debugging mode: go straight to empty */
5725 new_state = VM_PURGABLE_EMPTY;
5726 }
b0d623f7
A
5727 }
5728
2d21ac55
A
5729 switch (new_state) {
5730 case VM_PURGABLE_DENY:
5ba3f43e
A
5731 /*
5732 * Attempting to convert purgeable memory to non-purgeable:
5733 * not allowed.
5734 */
5735 return KERN_INVALID_ARGUMENT;
91447636 5736 case VM_PURGABLE_NONVOLATILE:
2d21ac55
A
5737 object->purgable = new_state;
5738
b0d623f7
A
5739 if (old_state == VM_PURGABLE_VOLATILE) {
5740 unsigned int delta;
5741
5742 assert(object->resident_page_count >=
5743 object->wired_page_count);
5744 delta = (object->resident_page_count -
5745 object->wired_page_count);
5746
5747 assert(vm_page_purgeable_count >= delta);
5748
5749 if (delta != 0) {
5750 OSAddAtomic(-delta,
5751 (SInt32 *)&vm_page_purgeable_count);
5752 }
5753 if (object->wired_page_count != 0) {
5754 assert(vm_page_purgeable_wired_count >=
5755 object->wired_page_count);
5756 OSAddAtomic(-object->wired_page_count,
5757 (SInt32 *)&vm_page_purgeable_wired_count);
5758 }
5759
2d21ac55 5760 vm_page_lock_queues();
b0d623f7 5761
fe8ab488
A
5762 /* object should be on a queue */
5763 assert(object->objq.next != NULL &&
5764 object->objq.prev != NULL);
5765 purgeable_q_t queue;
5766
5767 /*
5768 * Move object from its volatile queue to the
5769 * non-volatile queue...
5770 */
5771 queue = vm_purgeable_object_remove(object);
b0d623f7
A
5772 assert(queue);
5773
39236c6e
A
5774 if (object->purgeable_when_ripe) {
5775 vm_purgeable_token_delete_last(queue);
5776 }
b0d623f7
A
5777 assert(queue->debug_count_objects>=0);
5778
2d21ac55 5779 vm_page_unlock_queues();
91447636 5780 }
fe8ab488
A
5781 if (old_state == VM_PURGABLE_VOLATILE ||
5782 old_state == VM_PURGABLE_EMPTY) {
5783 /*
5784 * Transfer the object's pages from the volatile to
5785 * non-volatile ledgers.
5786 */
5787 vm_purgeable_accounting(object, VM_PURGABLE_VOLATILE,
5788 FALSE);
5789 }
5790
91447636
A
5791 break;
5792
5793 case VM_PURGABLE_VOLATILE:
b0d623f7
A
5794 if (object->volatile_fault) {
5795 vm_page_t p;
5796 int refmod;
5797
39037602 5798 vm_page_queue_iterate(&object->memq, p, vm_page_t, listq) {
b0d623f7
A
5799 if (p->busy ||
5800 VM_PAGE_WIRED(p) ||
5801 p->fictitious) {
5802 continue;
5803 }
39037602 5804 refmod = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(p));
b0d623f7
A
5805 if ((refmod & VM_MEM_MODIFIED) &&
5806 !p->dirty) {
316670eb 5807 SET_PAGE_DIRTY(p, FALSE);
b0d623f7
A
5808 }
5809 }
5810 }
813fb2f6
A
5811
5812 assert(old_state != VM_PURGABLE_EMPTY);
b0d623f7 5813
2d21ac55
A
5814 purgeable_q_t queue;
5815
5816 /* find the correct queue */
5817 if ((*state&VM_PURGABLE_ORDERING_MASK) == VM_PURGABLE_ORDERING_OBSOLETE)
593a1d5f 5818 queue = &purgeable_queues[PURGEABLE_Q_TYPE_OBSOLETE];
2d21ac55
A
5819 else {
5820 if ((*state&VM_PURGABLE_BEHAVIOR_MASK) == VM_PURGABLE_BEHAVIOR_FIFO)
5821 queue = &purgeable_queues[PURGEABLE_Q_TYPE_FIFO];
5822 else
5823 queue = &purgeable_queues[PURGEABLE_Q_TYPE_LIFO];
91447636 5824 }
2d21ac55 5825
593a1d5f
A
5826 if (old_state == VM_PURGABLE_NONVOLATILE ||
5827 old_state == VM_PURGABLE_EMPTY) {
b0d623f7
A
5828 unsigned int delta;
5829
39236c6e
A
5830 if ((*state & VM_PURGABLE_NO_AGING_MASK) ==
5831 VM_PURGABLE_NO_AGING) {
5832 object->purgeable_when_ripe = FALSE;
5833 } else {
5834 object->purgeable_when_ripe = TRUE;
5835 }
5836
5837 if (object->purgeable_when_ripe) {
5838 kern_return_t result;
91447636 5839
39236c6e
A
5840 /* try to add token... this can fail */
5841 vm_page_lock_queues();
5842
5843 result = vm_purgeable_token_add(queue);
5844 if (result != KERN_SUCCESS) {
5845 vm_page_unlock_queues();
5846 return result;
5847 }
5848 vm_page_unlock_queues();
91447636 5849 }
2d21ac55 5850
b0d623f7
A
5851 assert(object->resident_page_count >=
5852 object->wired_page_count);
5853 delta = (object->resident_page_count -
5854 object->wired_page_count);
5855
5856 if (delta != 0) {
5857 OSAddAtomic(delta,
5858 &vm_page_purgeable_count);
5859 }
5860 if (object->wired_page_count != 0) {
5861 OSAddAtomic(object->wired_page_count,
5862 &vm_page_purgeable_wired_count);
5863 }
5864
2d21ac55
A
5865 object->purgable = new_state;
5866
fe8ab488
A
5867 /* object should be on "non-volatile" queue */
5868 assert(object->objq.next != NULL);
5869 assert(object->objq.prev != NULL);
91447636 5870 }
2d21ac55 5871 else if (old_state == VM_PURGABLE_VOLATILE) {
39236c6e
A
5872 purgeable_q_t old_queue;
5873 boolean_t purgeable_when_ripe;
5874
2d21ac55
A
5875 /*
5876 * if reassigning priorities / purgeable groups, we don't change the
5877 * token queue. So moving priorities will not make pages stay around longer.
5878 * Reasoning is that the algorithm gives most priority to the most important
5879 * object. If a new token is added, the most important object' priority is boosted.
5880 * This biases the system already for purgeable queues that move a lot.
5881 * It doesn't seem more biasing is neccessary in this case, where no new object is added.
5882 */
5883 assert(object->objq.next != NULL && object->objq.prev != NULL); /* object should be on a queue */
5884
39236c6e 5885 old_queue = vm_purgeable_object_remove(object);
2d21ac55
A
5886 assert(old_queue);
5887
39236c6e
A
5888 if ((*state & VM_PURGABLE_NO_AGING_MASK) ==
5889 VM_PURGABLE_NO_AGING) {
5890 purgeable_when_ripe = FALSE;
5891 } else {
5892 purgeable_when_ripe = TRUE;
5893 }
5894
5895 if (old_queue != queue ||
5896 (purgeable_when_ripe !=
5897 object->purgeable_when_ripe)) {
2d21ac55
A
5898 kern_return_t result;
5899
5900 /* Changing queue. Have to move token. */
5901 vm_page_lock_queues();
39236c6e
A
5902 if (object->purgeable_when_ripe) {
5903 vm_purgeable_token_delete_last(old_queue);
5904 }
5905 object->purgeable_when_ripe = purgeable_when_ripe;
5906 if (object->purgeable_when_ripe) {
5907 result = vm_purgeable_token_add(queue);
5908 assert(result==KERN_SUCCESS); /* this should never fail since we just freed a token */
5909 }
2d21ac55 5910 vm_page_unlock_queues();
91447636 5911
2d21ac55
A
5912 }
5913 };
5914 vm_purgeable_object_add(object, queue, (*state&VM_VOLATILE_GROUP_MASK)>>VM_VOLATILE_GROUP_SHIFT );
fe8ab488
A
5915 if (old_state == VM_PURGABLE_NONVOLATILE) {
5916 vm_purgeable_accounting(object, VM_PURGABLE_NONVOLATILE,
5917 FALSE);
5918 }
2d21ac55
A
5919
5920 assert(queue->debug_count_objects>=0);
5921
91447636
A
5922 break;
5923
5924
5925 case VM_PURGABLE_EMPTY:
b0d623f7
A
5926 if (object->volatile_fault) {
5927 vm_page_t p;
5928 int refmod;
5929
39037602 5930 vm_page_queue_iterate(&object->memq, p, vm_page_t, listq) {
b0d623f7
A
5931 if (p->busy ||
5932 VM_PAGE_WIRED(p) ||
5933 p->fictitious) {
5934 continue;
5935 }
39037602 5936 refmod = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(p));
b0d623f7
A
5937 if ((refmod & VM_MEM_MODIFIED) &&
5938 !p->dirty) {
316670eb 5939 SET_PAGE_DIRTY(p, FALSE);
b0d623f7 5940 }
2d21ac55 5941 }
b0d623f7
A
5942 }
5943
fe8ab488
A
5944 if (old_state == VM_PURGABLE_VOLATILE) {
5945 purgeable_q_t old_queue;
5946
5947 /* object should be on a queue */
5948 assert(object->objq.next != NULL &&
5949 object->objq.prev != NULL);
5950
5951 old_queue = vm_purgeable_object_remove(object);
5952 assert(old_queue);
5953 if (object->purgeable_when_ripe) {
5954 vm_page_lock_queues();
5955 vm_purgeable_token_delete_first(old_queue);
5956 vm_page_unlock_queues();
2d21ac55 5957 }
91447636 5958 }
91447636 5959
fe8ab488
A
5960 if (old_state == VM_PURGABLE_NONVOLATILE) {
5961 /*
5962 * This object's pages were previously accounted as
5963 * "non-volatile" and now need to be accounted as
5964 * "volatile".
5965 */
5966 vm_purgeable_accounting(object, VM_PURGABLE_NONVOLATILE,
5967 FALSE);
5968 /*
5969 * Set to VM_PURGABLE_EMPTY because the pages are no
5970 * longer accounted in the "non-volatile" ledger
5971 * and are also not accounted for in
5972 * "vm_page_purgeable_count".
5973 */
5974 object->purgable = VM_PURGABLE_EMPTY;
5975 }
5976
5977 (void) vm_object_purge(object, 0);
5978 assert(object->purgable == VM_PURGABLE_EMPTY);
5979
5980 break;
91447636 5981 }
fe8ab488 5982
91447636
A
5983 *state = old_state;
5984
fe8ab488
A
5985 vm_object_lock_assert_exclusive(object);
5986
91447636
A
5987 return KERN_SUCCESS;
5988}
0b4e3aa0 5989
39236c6e
A
5990kern_return_t
5991vm_object_get_page_counts(
5992 vm_object_t object,
5993 vm_object_offset_t offset,
5994 vm_object_size_t size,
5995 unsigned int *resident_page_count,
5996 unsigned int *dirty_page_count)
5997{
5998
5999 kern_return_t kr = KERN_SUCCESS;
6000 boolean_t count_dirty_pages = FALSE;
6001 vm_page_t p = VM_PAGE_NULL;
6002 unsigned int local_resident_count = 0;
6003 unsigned int local_dirty_count = 0;
6004 vm_object_offset_t cur_offset = 0;
6005 vm_object_offset_t end_offset = 0;
6006
6007 if (object == VM_OBJECT_NULL)
6008 return KERN_INVALID_ARGUMENT;
6009
6010
6011 cur_offset = offset;
6012
6013 end_offset = offset + size;
6014
6015 vm_object_lock_assert_exclusive(object);
6016
6017 if (dirty_page_count != NULL) {
6018
6019 count_dirty_pages = TRUE;
6020 }
6021
6022 if (resident_page_count != NULL && count_dirty_pages == FALSE) {
6023 /*
6024 * Fast path when:
6025 * - we only want the resident page count, and,
6026 * - the entire object is exactly covered by the request.
6027 */
6028 if (offset == 0 && (object->vo_size == size)) {
6029
6030 *resident_page_count = object->resident_page_count;
6031 goto out;
6032 }
6033 }
6034
6035 if (object->resident_page_count <= (size >> PAGE_SHIFT)) {
6036
39037602 6037 vm_page_queue_iterate(&object->memq, p, vm_page_t, listq) {
39236c6e
A
6038
6039 if (p->offset >= cur_offset && p->offset < end_offset) {
6040
6041 local_resident_count++;
6042
6043 if (count_dirty_pages) {
6044
39037602 6045 if (p->dirty || (p->wpmapped && pmap_is_modified(VM_PAGE_GET_PHYS_PAGE(p)))) {
39236c6e
A
6046
6047 local_dirty_count++;
6048 }
6049 }
6050 }
6051 }
6052 } else {
6053
6054 for (cur_offset = offset; cur_offset < end_offset; cur_offset += PAGE_SIZE_64) {
6055
6056 p = vm_page_lookup(object, cur_offset);
6057
6058 if (p != VM_PAGE_NULL) {
6059
6060 local_resident_count++;
6061
6062 if (count_dirty_pages) {
6063
39037602 6064 if (p->dirty || (p->wpmapped && pmap_is_modified(VM_PAGE_GET_PHYS_PAGE(p)))) {
39236c6e
A
6065
6066 local_dirty_count++;
6067 }
6068 }
6069 }
6070 }
6071
6072 }
6073
6074 if (resident_page_count != NULL) {
6075 *resident_page_count = local_resident_count;
6076 }
6077
6078 if (dirty_page_count != NULL) {
6079 *dirty_page_count = local_dirty_count;
6080 }
6081
6082out:
6083 return kr;
6084}
6085
6086
0b4e3aa0
A
6087#if TASK_SWAPPER
6088/*
6089 * vm_object_res_deallocate
6090 *
6091 * (recursively) decrement residence counts on vm objects and their shadows.
6092 * Called from vm_object_deallocate and when swapping out an object.
6093 *
6094 * The object is locked, and remains locked throughout the function,
6095 * even as we iterate down the shadow chain. Locks on intermediate objects
6096 * will be dropped, but not the original object.
6097 *
6098 * NOTE: this function used to use recursion, rather than iteration.
6099 */
6100
6101__private_extern__ void
6102vm_object_res_deallocate(
6103 vm_object_t object)
6104{
6105 vm_object_t orig_object = object;
6106 /*
6107 * Object is locked so it can be called directly
6108 * from vm_object_deallocate. Original object is never
6109 * unlocked.
6110 */
6111 assert(object->res_count > 0);
6112 while (--object->res_count == 0) {
6113 assert(object->ref_count >= object->res_count);
6114 vm_object_deactivate_all_pages(object);
6115 /* iterate on shadow, if present */
6116 if (object->shadow != VM_OBJECT_NULL) {
6117 vm_object_t tmp_object = object->shadow;
6118 vm_object_lock(tmp_object);
6119 if (object != orig_object)
6120 vm_object_unlock(object);
6121 object = tmp_object;
6122 assert(object->res_count > 0);
6123 } else
6124 break;
6125 }
6126 if (object != orig_object)
1c79356b 6127 vm_object_unlock(object);
0b4e3aa0
A
6128}
6129
6130/*
6131 * vm_object_res_reference
6132 *
6133 * Internal function to increment residence count on a vm object
6134 * and its shadows. It is called only from vm_object_reference, and
6135 * when swapping in a vm object, via vm_map_swap.
6136 *
6137 * The object is locked, and remains locked throughout the function,
6138 * even as we iterate down the shadow chain. Locks on intermediate objects
6139 * will be dropped, but not the original object.
6140 *
6141 * NOTE: this function used to use recursion, rather than iteration.
6142 */
6143
6144__private_extern__ void
6145vm_object_res_reference(
6146 vm_object_t object)
6147{
6148 vm_object_t orig_object = object;
6149 /*
6150 * Object is locked, so this can be called directly
6151 * from vm_object_reference. This lock is never released.
6152 */
6153 while ((++object->res_count == 1) &&
6154 (object->shadow != VM_OBJECT_NULL)) {
6155 vm_object_t tmp_object = object->shadow;
6156
6157 assert(object->ref_count >= object->res_count);
6158 vm_object_lock(tmp_object);
6159 if (object != orig_object)
6160 vm_object_unlock(object);
6161 object = tmp_object;
1c79356b 6162 }
0b4e3aa0
A
6163 if (object != orig_object)
6164 vm_object_unlock(object);
6165 assert(orig_object->ref_count >= orig_object->res_count);
1c79356b 6166}
0b4e3aa0
A
6167#endif /* TASK_SWAPPER */
6168
6169/*
6170 * vm_object_reference:
6171 *
6172 * Gets another reference to the given object.
6173 */
6174#ifdef vm_object_reference
6175#undef vm_object_reference
6176#endif
6177__private_extern__ void
6178vm_object_reference(
39037602 6179 vm_object_t object)
0b4e3aa0
A
6180{
6181 if (object == VM_OBJECT_NULL)
6182 return;
6183
6184 vm_object_lock(object);
6185 assert(object->ref_count > 0);
6186 vm_object_reference_locked(object);
6187 vm_object_unlock(object);
6188}
6189
91447636
A
6190/*
6191 * vm_object_transpose
6192 *
6193 * This routine takes two VM objects of the same size and exchanges
6194 * their backing store.
6195 * The objects should be "quiesced" via a UPL operation with UPL_SET_IO_WIRE
6196 * and UPL_BLOCK_ACCESS if they are referenced anywhere.
6197 *
6198 * The VM objects must not be locked by caller.
6199 */
b0d623f7 6200unsigned int vm_object_transpose_count = 0;
91447636
A
6201kern_return_t
6202vm_object_transpose(
6203 vm_object_t object1,
6204 vm_object_t object2,
6205 vm_object_size_t transpose_size)
6206{
6207 vm_object_t tmp_object;
6208 kern_return_t retval;
6209 boolean_t object1_locked, object2_locked;
91447636
A
6210 vm_page_t page;
6211 vm_object_offset_t page_offset;
6212
6213 tmp_object = VM_OBJECT_NULL;
6214 object1_locked = FALSE; object2_locked = FALSE;
91447636
A
6215
6216 if (object1 == object2 ||
6217 object1 == VM_OBJECT_NULL ||
6218 object2 == VM_OBJECT_NULL) {
6219 /*
6220 * If the 2 VM objects are the same, there's
6221 * no point in exchanging their backing store.
6222 */
6223 retval = KERN_INVALID_VALUE;
6224 goto done;
6225 }
6226
b0d623f7
A
6227 /*
6228 * Since we need to lock both objects at the same time,
6229 * make sure we always lock them in the same order to
6230 * avoid deadlocks.
6231 */
6232 if (object1 > object2) {
6233 tmp_object = object1;
6234 object1 = object2;
6235 object2 = tmp_object;
6236 }
6237
6238 /*
6239 * Allocate a temporary VM object to hold object1's contents
6240 * while we copy object2 to object1.
6241 */
6242 tmp_object = vm_object_allocate(transpose_size);
6243 vm_object_lock(tmp_object);
6244 tmp_object->can_persist = FALSE;
6245
6246
6247 /*
6248 * Grab control of the 1st VM object.
6249 */
91447636
A
6250 vm_object_lock(object1);
6251 object1_locked = TRUE;
2d21ac55
A
6252 if (!object1->alive || object1->terminating ||
6253 object1->copy || object1->shadow || object1->shadowed ||
6254 object1->purgable != VM_PURGABLE_DENY) {
91447636
A
6255 /*
6256 * We don't deal with copy or shadow objects (yet).
6257 */
6258 retval = KERN_INVALID_VALUE;
6259 goto done;
6260 }
6261 /*
b0d623f7
A
6262 * We're about to mess with the object's backing store and
6263 * taking a "paging_in_progress" reference wouldn't be enough
91447636
A
6264 * to prevent any paging activity on this object, so the caller should
6265 * have "quiesced" the objects beforehand, via a UPL operation with
6266 * UPL_SET_IO_WIRE (to make sure all the pages are there and wired)
6267 * and UPL_BLOCK_ACCESS (to mark the pages "busy").
b0d623f7
A
6268 *
6269 * Wait for any paging operation to complete (but only paging, not
6270 * other kind of activities not linked to the pager). After we're
6271 * statisfied that there's no more paging in progress, we keep the
6272 * object locked, to guarantee that no one tries to access its pager.
91447636 6273 */
b0d623f7 6274 vm_object_paging_only_wait(object1, THREAD_UNINT);
91447636
A
6275
6276 /*
6277 * Same as above for the 2nd object...
6278 */
6279 vm_object_lock(object2);
6280 object2_locked = TRUE;
2d21ac55
A
6281 if (! object2->alive || object2->terminating ||
6282 object2->copy || object2->shadow || object2->shadowed ||
6283 object2->purgable != VM_PURGABLE_DENY) {
91447636
A
6284 retval = KERN_INVALID_VALUE;
6285 goto done;
6286 }
b0d623f7 6287 vm_object_paging_only_wait(object2, THREAD_UNINT);
91447636 6288
91447636 6289
6d2010ae
A
6290 if (object1->vo_size != object2->vo_size ||
6291 object1->vo_size != transpose_size) {
91447636
A
6292 /*
6293 * If the 2 objects don't have the same size, we can't
6294 * exchange their backing stores or one would overflow.
6295 * If their size doesn't match the caller's
6296 * "transpose_size", we can't do it either because the
6297 * transpose operation will affect the entire span of
6298 * the objects.
6299 */
6300 retval = KERN_INVALID_VALUE;
6301 goto done;
6302 }
6303
6304
6305 /*
6306 * Transpose the lists of resident pages.
2d21ac55 6307 * This also updates the resident_page_count and the memq_hint.
91447636 6308 */
39037602 6309 if (object1->phys_contiguous || vm_page_queue_empty(&object1->memq)) {
91447636
A
6310 /*
6311 * No pages in object1, just transfer pages
6312 * from object2 to object1. No need to go through
6313 * an intermediate object.
6314 */
39037602
A
6315 while (!vm_page_queue_empty(&object2->memq)) {
6316 page = (vm_page_t) vm_page_queue_first(&object2->memq);
5ba3f43e 6317 vm_page_rename(page, object1, page->offset);
91447636 6318 }
39037602
A
6319 assert(vm_page_queue_empty(&object2->memq));
6320 } else if (object2->phys_contiguous || vm_page_queue_empty(&object2->memq)) {
91447636
A
6321 /*
6322 * No pages in object2, just transfer pages
6323 * from object1 to object2. No need to go through
6324 * an intermediate object.
6325 */
39037602
A
6326 while (!vm_page_queue_empty(&object1->memq)) {
6327 page = (vm_page_t) vm_page_queue_first(&object1->memq);
5ba3f43e 6328 vm_page_rename(page, object2, page->offset);
91447636 6329 }
39037602 6330 assert(vm_page_queue_empty(&object1->memq));
91447636
A
6331 } else {
6332 /* transfer object1's pages to tmp_object */
39037602
A
6333 while (!vm_page_queue_empty(&object1->memq)) {
6334 page = (vm_page_t) vm_page_queue_first(&object1->memq);
91447636 6335 page_offset = page->offset;
b0d623f7 6336 vm_page_remove(page, TRUE);
91447636 6337 page->offset = page_offset;
39037602 6338 vm_page_queue_enter(&tmp_object->memq, page, vm_page_t, listq);
91447636 6339 }
39037602 6340 assert(vm_page_queue_empty(&object1->memq));
91447636 6341 /* transfer object2's pages to object1 */
39037602
A
6342 while (!vm_page_queue_empty(&object2->memq)) {
6343 page = (vm_page_t) vm_page_queue_first(&object2->memq);
5ba3f43e 6344 vm_page_rename(page, object1, page->offset);
91447636 6345 }
39037602 6346 assert(vm_page_queue_empty(&object2->memq));
3e170ce0 6347 /* transfer tmp_object's pages to object2 */
39037602
A
6348 while (!vm_page_queue_empty(&tmp_object->memq)) {
6349 page = (vm_page_t) vm_page_queue_first(&tmp_object->memq);
6350 vm_page_queue_remove(&tmp_object->memq, page,
6351 vm_page_t, listq);
91447636
A
6352 vm_page_insert(page, object2, page->offset);
6353 }
39037602 6354 assert(vm_page_queue_empty(&tmp_object->memq));
91447636
A
6355 }
6356
91447636
A
6357#define __TRANSPOSE_FIELD(field) \
6358MACRO_BEGIN \
6359 tmp_object->field = object1->field; \
6360 object1->field = object2->field; \
6361 object2->field = tmp_object->field; \
6362MACRO_END
6363
b0d623f7 6364 /* "Lock" refers to the object not its contents */
2d21ac55 6365 /* "size" should be identical */
6d2010ae 6366 assert(object1->vo_size == object2->vo_size);
b0d623f7 6367 /* "memq_hint" was updated above when transposing pages */
2d21ac55 6368 /* "ref_count" refers to the object not its contents */
5ba3f43e
A
6369 assert(object1->ref_count >= 1);
6370 assert(object2->ref_count >= 1);
2d21ac55
A
6371#if TASK_SWAPPER
6372 /* "res_count" refers to the object not its contents */
6373#endif
6374 /* "resident_page_count" was updated above when transposing pages */
b0d623f7
A
6375 /* "wired_page_count" was updated above when transposing pages */
6376 /* "reusable_page_count" was updated above when transposing pages */
2d21ac55 6377 /* there should be no "copy" */
91447636
A
6378 assert(!object1->copy);
6379 assert(!object2->copy);
2d21ac55 6380 /* there should be no "shadow" */
91447636
A
6381 assert(!object1->shadow);
6382 assert(!object2->shadow);
6d2010ae 6383 __TRANSPOSE_FIELD(vo_shadow_offset); /* used by phys_contiguous objects */
91447636
A
6384 __TRANSPOSE_FIELD(pager);
6385 __TRANSPOSE_FIELD(paging_offset);
91447636
A
6386 __TRANSPOSE_FIELD(pager_control);
6387 /* update the memory_objects' pointers back to the VM objects */
6388 if (object1->pager_control != MEMORY_OBJECT_CONTROL_NULL) {
6389 memory_object_control_collapse(object1->pager_control,
6390 object1);
6391 }
6392 if (object2->pager_control != MEMORY_OBJECT_CONTROL_NULL) {
6393 memory_object_control_collapse(object2->pager_control,
6394 object2);
6395 }
2d21ac55
A
6396 __TRANSPOSE_FIELD(copy_strategy);
6397 /* "paging_in_progress" refers to the object not its contents */
b0d623f7
A
6398 assert(!object1->paging_in_progress);
6399 assert(!object2->paging_in_progress);
6400 assert(object1->activity_in_progress);
6401 assert(object2->activity_in_progress);
2d21ac55 6402 /* "all_wanted" refers to the object not its contents */
91447636
A
6403 __TRANSPOSE_FIELD(pager_created);
6404 __TRANSPOSE_FIELD(pager_initialized);
6405 __TRANSPOSE_FIELD(pager_ready);
6406 __TRANSPOSE_FIELD(pager_trusted);
2d21ac55 6407 __TRANSPOSE_FIELD(can_persist);
91447636 6408 __TRANSPOSE_FIELD(internal);
91447636
A
6409 __TRANSPOSE_FIELD(private);
6410 __TRANSPOSE_FIELD(pageout);
2d21ac55
A
6411 /* "alive" should be set */
6412 assert(object1->alive);
6413 assert(object2->alive);
6414 /* "purgeable" should be non-purgeable */
6415 assert(object1->purgable == VM_PURGABLE_DENY);
6416 assert(object2->purgable == VM_PURGABLE_DENY);
6417 /* "shadowed" refers to the the object not its contents */
39236c6e 6418 __TRANSPOSE_FIELD(purgeable_when_ripe);
91447636 6419 __TRANSPOSE_FIELD(true_share);
2d21ac55
A
6420 /* "terminating" should not be set */
6421 assert(!object1->terminating);
6422 assert(!object2->terminating);
5ba3f43e
A
6423 /* transfer "named" reference if needed */
6424 if (object1->named && !object2->named) {
6425 assert(object1->ref_count >= 2);
6426 assert(object2->ref_count >= 1);
6427 object1->ref_count--;
6428 object2->ref_count++;
6429 } else if (!object1->named && object2->named) {
6430 assert(object1->ref_count >= 1);
6431 assert(object2->ref_count >= 2);
6432 object1->ref_count++;
6433 object2->ref_count--;
6434 }
2d21ac55
A
6435 __TRANSPOSE_FIELD(named);
6436 /* "shadow_severed" refers to the object not its contents */
91447636
A
6437 __TRANSPOSE_FIELD(phys_contiguous);
6438 __TRANSPOSE_FIELD(nophyscache);
b0d623f7
A
6439 /* "cached_list.next" points to transposed object */
6440 object1->cached_list.next = (queue_entry_t) object2;
6441 object2->cached_list.next = (queue_entry_t) object1;
6442 /* "cached_list.prev" should be NULL */
2d21ac55 6443 assert(object1->cached_list.prev == NULL);
2d21ac55 6444 assert(object2->cached_list.prev == NULL);
91447636
A
6445 __TRANSPOSE_FIELD(last_alloc);
6446 __TRANSPOSE_FIELD(sequential);
2d21ac55
A
6447 __TRANSPOSE_FIELD(pages_created);
6448 __TRANSPOSE_FIELD(pages_used);
6d2010ae 6449 __TRANSPOSE_FIELD(scan_collisions);
91447636
A
6450 __TRANSPOSE_FIELD(cow_hint);
6451 __TRANSPOSE_FIELD(wimg_bits);
6d2010ae 6452 __TRANSPOSE_FIELD(set_cache_attr);
2d21ac55 6453 __TRANSPOSE_FIELD(code_signed);
b0d623f7
A
6454 object1->transposed = TRUE;
6455 object2->transposed = TRUE;
6456 __TRANSPOSE_FIELD(mapping_in_progress);
6457 __TRANSPOSE_FIELD(volatile_empty);
6458 __TRANSPOSE_FIELD(volatile_fault);
6459 __TRANSPOSE_FIELD(all_reusable);
6460 assert(object1->blocked_access);
6461 assert(object2->blocked_access);
6462 assert(object1->__object2_unused_bits == 0);
6463 assert(object2->__object2_unused_bits == 0);
6464#if UPL_DEBUG
2d21ac55
A
6465 /* "uplq" refers to the object not its contents (see upl_transpose()) */
6466#endif
3e170ce0
A
6467 assert((object1->purgable == VM_PURGABLE_DENY) || (object1->objq.next == NULL));
6468 assert((object1->purgable == VM_PURGABLE_DENY) || (object1->objq.prev == NULL));
6469 assert((object2->purgable == VM_PURGABLE_DENY) || (object2->objq.next == NULL));
6470 assert((object2->purgable == VM_PURGABLE_DENY) || (object2->objq.prev == NULL));
91447636
A
6471
6472#undef __TRANSPOSE_FIELD
6473
6474 retval = KERN_SUCCESS;
6475
6476done:
6477 /*
6478 * Cleanup.
6479 */
6480 if (tmp_object != VM_OBJECT_NULL) {
91447636
A
6481 vm_object_unlock(tmp_object);
6482 /*
6483 * Re-initialize the temporary object to avoid
6484 * deallocating a real pager.
6485 */
6486 _vm_object_allocate(transpose_size, tmp_object);
6487 vm_object_deallocate(tmp_object);
6488 tmp_object = VM_OBJECT_NULL;
6489 }
6490
6491 if (object1_locked) {
6492 vm_object_unlock(object1);
6493 object1_locked = FALSE;
6494 }
6495 if (object2_locked) {
6496 vm_object_unlock(object2);
6497 object2_locked = FALSE;
6498 }
b0d623f7
A
6499
6500 vm_object_transpose_count++;
91447636
A
6501
6502 return retval;
6503}
0c530ab8
A
6504
6505
2d21ac55 6506/*
b0d623f7 6507 * vm_object_cluster_size
2d21ac55
A
6508 *
6509 * Determine how big a cluster we should issue an I/O for...
6510 *
6511 * Inputs: *start == offset of page needed
6512 * *length == maximum cluster pager can handle
6513 * Outputs: *start == beginning offset of cluster
6514 * *length == length of cluster to try
6515 *
6516 * The original *start will be encompassed by the cluster
6517 *
6518 */
6519extern int speculative_reads_disabled;
6d2010ae 6520
39037602
A
6521/*
6522 * Try to always keep these values an even multiple of PAGE_SIZE. We use these values
6523 * to derive min_ph_bytes and max_ph_bytes (IMP: bytes not # of pages) and expect those values to
6524 * always be page-aligned. The derivation could involve operations (e.g. division)
6525 * that could give us non-page-size aligned values if we start out with values that
6526 * are odd multiples of PAGE_SIZE.
6527 */
5ba3f43e
A
6528#if CONFIG_EMBEDDED
6529 unsigned int preheat_max_bytes = (1024 * 512);
6530#else /* CONFIG_EMBEDDED */
39037602 6531 unsigned int preheat_max_bytes = MAX_UPL_TRANSFER_BYTES;
5ba3f43e 6532#endif /* CONFIG_EMBEDDED */
fe8ab488 6533unsigned int preheat_min_bytes = (1024 * 32);
2d21ac55 6534
2d21ac55
A
6535
6536__private_extern__ void
6537vm_object_cluster_size(vm_object_t object, vm_object_offset_t *start,
b0d623f7 6538 vm_size_t *length, vm_object_fault_info_t fault_info, uint32_t *io_streaming)
2d21ac55
A
6539{
6540 vm_size_t pre_heat_size;
6541 vm_size_t tail_size;
6542 vm_size_t head_size;
6543 vm_size_t max_length;
6544 vm_size_t cluster_size;
6545 vm_object_offset_t object_size;
6546 vm_object_offset_t orig_start;
6547 vm_object_offset_t target_start;
6548 vm_object_offset_t offset;
6549 vm_behavior_t behavior;
6550 boolean_t look_behind = TRUE;
6551 boolean_t look_ahead = TRUE;
6d2010ae 6552 boolean_t isSSD = FALSE;
b0d623f7 6553 uint32_t throttle_limit;
2d21ac55
A
6554 int sequential_run;
6555 int sequential_behavior = VM_BEHAVIOR_SEQUENTIAL;
fe8ab488
A
6556 vm_size_t max_ph_size;
6557 vm_size_t min_ph_size;
2d21ac55
A
6558
6559 assert( !(*length & PAGE_MASK));
6560 assert( !(*start & PAGE_MASK_64));
6561
6d2010ae
A
6562 /*
6563 * remember maxiumum length of run requested
6564 */
6565 max_length = *length;
2d21ac55
A
6566 /*
6567 * we'll always return a cluster size of at least
6568 * 1 page, since the original fault must always
6569 * be processed
6570 */
6571 *length = PAGE_SIZE;
b0d623f7 6572 *io_streaming = 0;
2d21ac55 6573
6d2010ae 6574 if (speculative_reads_disabled || fault_info == NULL) {
2d21ac55
A
6575 /*
6576 * no cluster... just fault the page in
6577 */
6578 return;
6579 }
6580 orig_start = *start;
6581 target_start = orig_start;
b0d623f7 6582 cluster_size = round_page(fault_info->cluster_size);
2d21ac55
A
6583 behavior = fault_info->behavior;
6584
6585 vm_object_lock(object);
6586
6d2010ae
A
6587 if (object->pager == MEMORY_OBJECT_NULL)
6588 goto out; /* pager is gone for this object, nothing more to do */
6589
5ba3f43e 6590 vnode_pager_get_isSSD(object->pager, &isSSD);
6d2010ae 6591
fe8ab488
A
6592 min_ph_size = round_page(preheat_min_bytes);
6593 max_ph_size = round_page(preheat_max_bytes);
6d2010ae 6594
5ba3f43e 6595#if !CONFIG_EMBEDDED
6d2010ae
A
6596 if (isSSD) {
6597 min_ph_size /= 2;
6598 max_ph_size /= 8;
39037602
A
6599
6600 if (min_ph_size & PAGE_MASK_64) {
6601 min_ph_size = trunc_page(min_ph_size);
6602 }
6603
6604 if (max_ph_size & PAGE_MASK_64) {
6605 max_ph_size = trunc_page(max_ph_size);
6606 }
6d2010ae 6607 }
5ba3f43e 6608#endif /* !CONFIG_EMBEDDED */
39037602 6609
fe8ab488
A
6610 if (min_ph_size < PAGE_SIZE)
6611 min_ph_size = PAGE_SIZE;
6d2010ae 6612
fe8ab488
A
6613 if (max_ph_size < PAGE_SIZE)
6614 max_ph_size = PAGE_SIZE;
6615 else if (max_ph_size > MAX_UPL_TRANSFER_BYTES)
6616 max_ph_size = MAX_UPL_TRANSFER_BYTES;
6d2010ae 6617
fe8ab488
A
6618 if (max_length > max_ph_size)
6619 max_length = max_ph_size;
6d2010ae
A
6620
6621 if (max_length <= PAGE_SIZE)
6622 goto out;
6623
2d21ac55 6624 if (object->internal)
6d2010ae 6625 object_size = object->vo_size;
2d21ac55 6626 else
6d2010ae 6627 vnode_pager_get_object_size(object->pager, &object_size);
2d21ac55
A
6628
6629 object_size = round_page_64(object_size);
6630
6631 if (orig_start >= object_size) {
6632 /*
6633 * fault occurred beyond the EOF...
6634 * we need to punt w/o changing the
6635 * starting offset
6636 */
6637 goto out;
6638 }
6639 if (object->pages_used > object->pages_created) {
6640 /*
6641 * must have wrapped our 32 bit counters
6642 * so reset
6643 */
6644 object->pages_used = object->pages_created = 0;
6645 }
6646 if ((sequential_run = object->sequential)) {
6647 if (sequential_run < 0) {
6648 sequential_behavior = VM_BEHAVIOR_RSEQNTL;
6649 sequential_run = 0 - sequential_run;
6650 } else {
6651 sequential_behavior = VM_BEHAVIOR_SEQUENTIAL;
6652 }
b0d623f7 6653
2d21ac55 6654 }
6d2010ae 6655 switch (behavior) {
2d21ac55
A
6656
6657 default:
6658 behavior = VM_BEHAVIOR_DEFAULT;
6659
6660 case VM_BEHAVIOR_DEFAULT:
6661 if (object->internal && fault_info->user_tag == VM_MEMORY_STACK)
6662 goto out;
6663
b0d623f7 6664 if (sequential_run >= (3 * PAGE_SIZE)) {
2d21ac55
A
6665 pre_heat_size = sequential_run + PAGE_SIZE;
6666
b0d623f7 6667 if (sequential_behavior == VM_BEHAVIOR_SEQUENTIAL)
2d21ac55
A
6668 look_behind = FALSE;
6669 else
6670 look_ahead = FALSE;
b0d623f7
A
6671
6672 *io_streaming = 1;
2d21ac55 6673 } else {
2d21ac55 6674
fe8ab488 6675 if (object->pages_created < (20 * (min_ph_size >> PAGE_SHIFT))) {
2d21ac55
A
6676 /*
6677 * prime the pump
6678 */
fe8ab488 6679 pre_heat_size = min_ph_size;
6d2010ae
A
6680 } else {
6681 /*
6682 * Linear growth in PH size: The maximum size is max_length...
6683 * this cacluation will result in a size that is neither a
6684 * power of 2 nor a multiple of PAGE_SIZE... so round
6685 * it up to the nearest PAGE_SIZE boundary
6686 */
3e170ce0 6687 pre_heat_size = (max_length * (uint64_t)object->pages_used) / object->pages_created;
fe8ab488
A
6688
6689 if (pre_heat_size < min_ph_size)
6690 pre_heat_size = min_ph_size;
6d2010ae
A
6691 else
6692 pre_heat_size = round_page(pre_heat_size);
2d21ac55 6693 }
2d21ac55
A
6694 }
6695 break;
6696
6697 case VM_BEHAVIOR_RANDOM:
6698 if ((pre_heat_size = cluster_size) <= PAGE_SIZE)
6699 goto out;
6700 break;
6701
6702 case VM_BEHAVIOR_SEQUENTIAL:
6703 if ((pre_heat_size = cluster_size) == 0)
6704 pre_heat_size = sequential_run + PAGE_SIZE;
6705 look_behind = FALSE;
b0d623f7 6706 *io_streaming = 1;
2d21ac55
A
6707
6708 break;
6709
6710 case VM_BEHAVIOR_RSEQNTL:
6711 if ((pre_heat_size = cluster_size) == 0)
6712 pre_heat_size = sequential_run + PAGE_SIZE;
6713 look_ahead = FALSE;
b0d623f7 6714 *io_streaming = 1;
2d21ac55
A
6715
6716 break;
6717
6718 }
b0d623f7
A
6719 throttle_limit = (uint32_t) max_length;
6720 assert(throttle_limit == max_length);
6721
39236c6e 6722 if (vnode_pager_get_throttle_io_limit(object->pager, &throttle_limit) == KERN_SUCCESS) {
b0d623f7
A
6723 if (max_length > throttle_limit)
6724 max_length = throttle_limit;
6725 }
2d21ac55
A
6726 if (pre_heat_size > max_length)
6727 pre_heat_size = max_length;
6728
fe8ab488 6729 if (behavior == VM_BEHAVIOR_DEFAULT && (pre_heat_size > min_ph_size)) {
316670eb
A
6730
6731 unsigned int consider_free = vm_page_free_count + vm_page_cleaned_count;
6732
6733 if (consider_free < vm_page_throttle_limit) {
6d2010ae 6734 pre_heat_size = trunc_page(pre_heat_size / 16);
316670eb 6735 } else if (consider_free < vm_page_free_target) {
6d2010ae 6736 pre_heat_size = trunc_page(pre_heat_size / 4);
316670eb
A
6737 }
6738
fe8ab488
A
6739 if (pre_heat_size < min_ph_size)
6740 pre_heat_size = min_ph_size;
b0d623f7 6741 }
2d21ac55 6742 if (look_ahead == TRUE) {
b0d623f7
A
6743 if (look_behind == TRUE) {
6744 /*
6745 * if we get here its due to a random access...
6746 * so we want to center the original fault address
6747 * within the cluster we will issue... make sure
6748 * to calculate 'head_size' as a multiple of PAGE_SIZE...
6749 * 'pre_heat_size' is a multiple of PAGE_SIZE but not
6750 * necessarily an even number of pages so we need to truncate
6751 * the result to a PAGE_SIZE boundary
6752 */
6753 head_size = trunc_page(pre_heat_size / 2);
2d21ac55 6754
b0d623f7
A
6755 if (target_start > head_size)
6756 target_start -= head_size;
6757 else
6758 target_start = 0;
2d21ac55 6759
b0d623f7
A
6760 /*
6761 * 'target_start' at this point represents the beginning offset
6762 * of the cluster we are considering... 'orig_start' will be in
6763 * the center of this cluster if we didn't have to clip the start
6764 * due to running into the start of the file
6765 */
6766 }
6767 if ((target_start + pre_heat_size) > object_size)
6768 pre_heat_size = (vm_size_t)(round_page_64(object_size - target_start));
6769 /*
6770 * at this point caclulate the number of pages beyond the original fault
6771 * address that we want to consider... this is guaranteed not to extend beyond
6772 * the current EOF...
6773 */
6774 assert((vm_size_t)(orig_start - target_start) == (orig_start - target_start));
6775 tail_size = pre_heat_size - (vm_size_t)(orig_start - target_start) - PAGE_SIZE;
2d21ac55 6776 } else {
6d2010ae
A
6777 if (pre_heat_size > target_start) {
6778 /*
6779 * since pre_heat_size is always smaller then 2^32,
6780 * if it is larger then target_start (a 64 bit value)
6781 * it is safe to clip target_start to 32 bits
6782 */
6783 pre_heat_size = (vm_size_t) target_start;
6784 }
2d21ac55
A
6785 tail_size = 0;
6786 }
b0d623f7 6787 assert( !(target_start & PAGE_MASK_64));
39037602 6788 assert( !(pre_heat_size & PAGE_MASK_64));
b0d623f7 6789
2d21ac55
A
6790 if (pre_heat_size <= PAGE_SIZE)
6791 goto out;
6792
6793 if (look_behind == TRUE) {
6794 /*
6795 * take a look at the pages before the original
b0d623f7
A
6796 * faulting offset... recalculate this in case
6797 * we had to clip 'pre_heat_size' above to keep
6798 * from running past the EOF.
2d21ac55
A
6799 */
6800 head_size = pre_heat_size - tail_size - PAGE_SIZE;
6801
6802 for (offset = orig_start - PAGE_SIZE_64; head_size; offset -= PAGE_SIZE_64, head_size -= PAGE_SIZE) {
6803 /*
6804 * don't poke below the lowest offset
6805 */
6806 if (offset < fault_info->lo_offset)
6807 break;
39037602
A
6808 /*
6809 * for external objects or internal objects w/o a pager,
6810 * VM_COMPRESSOR_PAGER_STATE_GET will return VM_EXTERNAL_STATE_UNKNOWN
2d21ac55 6811 */
39037602 6812 if (VM_COMPRESSOR_PAGER_STATE_GET(object, offset) == VM_EXTERNAL_STATE_ABSENT) {
39236c6e
A
6813 break;
6814 }
2d21ac55
A
6815 if (vm_page_lookup(object, offset) != VM_PAGE_NULL) {
6816 /*
6817 * don't bridge resident pages
6818 */
6819 break;
6820 }
6821 *start = offset;
6822 *length += PAGE_SIZE;
6823 }
6824 }
6825 if (look_ahead == TRUE) {
6826 for (offset = orig_start + PAGE_SIZE_64; tail_size; offset += PAGE_SIZE_64, tail_size -= PAGE_SIZE) {
6827 /*
6828 * don't poke above the highest offset
6829 */
6830 if (offset >= fault_info->hi_offset)
6831 break;
b0d623f7
A
6832 assert(offset < object_size);
6833
39037602
A
6834 /*
6835 * for external objects or internal objects w/o a pager,
6836 * VM_COMPRESSOR_PAGER_STATE_GET will return VM_EXTERNAL_STATE_UNKNOWN
2d21ac55 6837 */
fe8ab488 6838 if (VM_COMPRESSOR_PAGER_STATE_GET(object, offset) == VM_EXTERNAL_STATE_ABSENT) {
39236c6e
A
6839 break;
6840 }
2d21ac55
A
6841 if (vm_page_lookup(object, offset) != VM_PAGE_NULL) {
6842 /*
6843 * don't bridge resident pages
6844 */
6845 break;
6846 }
6847 *length += PAGE_SIZE;
6848 }
6849 }
6850out:
b0d623f7
A
6851 if (*length > max_length)
6852 *length = max_length;
6853
2d21ac55 6854 vm_object_unlock(object);
316670eb
A
6855
6856 DTRACE_VM1(clustersize, vm_size_t, *length);
2d21ac55
A
6857}
6858
6859
6860/*
6861 * Allow manipulation of individual page state. This is actually part of
6862 * the UPL regimen but takes place on the VM object rather than on a UPL
6863 */
0c530ab8
A
6864
6865kern_return_t
6866vm_object_page_op(
6867 vm_object_t object,
6868 vm_object_offset_t offset,
6869 int ops,
6870 ppnum_t *phys_entry,
6871 int *flags)
6872{
6873 vm_page_t dst_page;
6874
6875 vm_object_lock(object);
6876
6877 if(ops & UPL_POP_PHYSICAL) {
6878 if(object->phys_contiguous) {
6879 if (phys_entry) {
6880 *phys_entry = (ppnum_t)
6d2010ae 6881 (object->vo_shadow_offset >> PAGE_SHIFT);
0c530ab8
A
6882 }
6883 vm_object_unlock(object);
6884 return KERN_SUCCESS;
6885 } else {
6886 vm_object_unlock(object);
6887 return KERN_INVALID_OBJECT;
6888 }
6889 }
6890 if(object->phys_contiguous) {
6891 vm_object_unlock(object);
6892 return KERN_INVALID_OBJECT;
6893 }
6894
6895 while(TRUE) {
6896 if((dst_page = vm_page_lookup(object,offset)) == VM_PAGE_NULL) {
6897 vm_object_unlock(object);
6898 return KERN_FAILURE;
6899 }
6900
6901 /* Sync up on getting the busy bit */
6902 if((dst_page->busy || dst_page->cleaning) &&
6903 (((ops & UPL_POP_SET) &&
6904 (ops & UPL_POP_BUSY)) || (ops & UPL_POP_DUMP))) {
6905 /* someone else is playing with the page, we will */
6906 /* have to wait */
6907 PAGE_SLEEP(object, dst_page, THREAD_UNINT);
6908 continue;
6909 }
6910
6911 if (ops & UPL_POP_DUMP) {
2d21ac55 6912 if (dst_page->pmapped == TRUE)
39037602 6913 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(dst_page));
0c530ab8 6914
b0d623f7 6915 VM_PAGE_FREE(dst_page);
0c530ab8
A
6916 break;
6917 }
6918
6919 if (flags) {
6920 *flags = 0;
6921
6922 /* Get the condition of flags before requested ops */
6923 /* are undertaken */
6924
6925 if(dst_page->dirty) *flags |= UPL_POP_DIRTY;
39037602 6926 if(dst_page->free_when_done) *flags |= UPL_POP_PAGEOUT;
0c530ab8
A
6927 if(dst_page->precious) *flags |= UPL_POP_PRECIOUS;
6928 if(dst_page->absent) *flags |= UPL_POP_ABSENT;
6929 if(dst_page->busy) *flags |= UPL_POP_BUSY;
6930 }
6931
6932 /* The caller should have made a call either contingent with */
6933 /* or prior to this call to set UPL_POP_BUSY */
6934 if(ops & UPL_POP_SET) {
6935 /* The protection granted with this assert will */
6936 /* not be complete. If the caller violates the */
6937 /* convention and attempts to change page state */
6938 /* without first setting busy we may not see it */
6939 /* because the page may already be busy. However */
6940 /* if such violations occur we will assert sooner */
6941 /* or later. */
6942 assert(dst_page->busy || (ops & UPL_POP_BUSY));
316670eb
A
6943 if (ops & UPL_POP_DIRTY) {
6944 SET_PAGE_DIRTY(dst_page, FALSE);
6945 }
39037602 6946 if (ops & UPL_POP_PAGEOUT) dst_page->free_when_done = TRUE;
0c530ab8
A
6947 if (ops & UPL_POP_PRECIOUS) dst_page->precious = TRUE;
6948 if (ops & UPL_POP_ABSENT) dst_page->absent = TRUE;
6949 if (ops & UPL_POP_BUSY) dst_page->busy = TRUE;
6950 }
6951
6952 if(ops & UPL_POP_CLR) {
6953 assert(dst_page->busy);
6954 if (ops & UPL_POP_DIRTY) dst_page->dirty = FALSE;
39037602 6955 if (ops & UPL_POP_PAGEOUT) dst_page->free_when_done = FALSE;
0c530ab8
A
6956 if (ops & UPL_POP_PRECIOUS) dst_page->precious = FALSE;
6957 if (ops & UPL_POP_ABSENT) dst_page->absent = FALSE;
6958 if (ops & UPL_POP_BUSY) {
6959 dst_page->busy = FALSE;
6960 PAGE_WAKEUP(dst_page);
6961 }
6962 }
0c530ab8
A
6963 if (phys_entry) {
6964 /*
6965 * The physical page number will remain valid
6966 * only if the page is kept busy.
0c530ab8
A
6967 */
6968 assert(dst_page->busy);
39037602 6969 *phys_entry = VM_PAGE_GET_PHYS_PAGE(dst_page);
0c530ab8
A
6970 }
6971
6972 break;
6973 }
6974
6975 vm_object_unlock(object);
6976 return KERN_SUCCESS;
6977
6978}
6979
6980/*
6981 * vm_object_range_op offers performance enhancement over
6982 * vm_object_page_op for page_op functions which do not require page
6983 * level state to be returned from the call. Page_op was created to provide
6984 * a low-cost alternative to page manipulation via UPLs when only a single
6985 * page was involved. The range_op call establishes the ability in the _op
6986 * family of functions to work on multiple pages where the lack of page level
6987 * state handling allows the caller to avoid the overhead of the upl structures.
6988 */
6989
6990kern_return_t
6991vm_object_range_op(
6992 vm_object_t object,
6993 vm_object_offset_t offset_beg,
6994 vm_object_offset_t offset_end,
6995 int ops,
b0d623f7 6996 uint32_t *range)
0c530ab8
A
6997{
6998 vm_object_offset_t offset;
6999 vm_page_t dst_page;
7000
b0d623f7
A
7001 if (offset_end - offset_beg > (uint32_t) -1) {
7002 /* range is too big and would overflow "*range" */
7003 return KERN_INVALID_ARGUMENT;
7004 }
0c530ab8
A
7005 if (object->resident_page_count == 0) {
7006 if (range) {
b0d623f7 7007 if (ops & UPL_ROP_PRESENT) {
0c530ab8 7008 *range = 0;
b0d623f7
A
7009 } else {
7010 *range = (uint32_t) (offset_end - offset_beg);
7011 assert(*range == (offset_end - offset_beg));
7012 }
0c530ab8
A
7013 }
7014 return KERN_SUCCESS;
7015 }
7016 vm_object_lock(object);
7017
7018 if (object->phys_contiguous) {
7019 vm_object_unlock(object);
7020 return KERN_INVALID_OBJECT;
7021 }
7022
2d21ac55 7023 offset = offset_beg & ~PAGE_MASK_64;
0c530ab8
A
7024
7025 while (offset < offset_end) {
7026 dst_page = vm_page_lookup(object, offset);
7027 if (dst_page != VM_PAGE_NULL) {
7028 if (ops & UPL_ROP_DUMP) {
316670eb 7029 if (dst_page->busy || dst_page->cleaning) {
6d2010ae 7030 /*
0c530ab8
A
7031 * someone else is playing with the
7032 * page, we will have to wait
7033 */
2d21ac55 7034 PAGE_SLEEP(object, dst_page, THREAD_UNINT);
0c530ab8
A
7035 /*
7036 * need to relook the page up since it's
7037 * state may have changed while we slept
7038 * it might even belong to a different object
7039 * at this point
7040 */
7041 continue;
7042 }
39037602 7043 if (dst_page->laundry)
316670eb 7044 vm_pageout_steal_laundry(dst_page, FALSE);
39037602 7045
2d21ac55 7046 if (dst_page->pmapped == TRUE)
39037602 7047 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(dst_page));
0c530ab8 7048
b0d623f7 7049 VM_PAGE_FREE(dst_page);
2d21ac55 7050
3e170ce0
A
7051 } else if ((ops & UPL_ROP_ABSENT)
7052 && (!dst_page->absent || dst_page->busy)) {
7053 break;
7054 }
0c530ab8
A
7055 } else if (ops & UPL_ROP_PRESENT)
7056 break;
7057
7058 offset += PAGE_SIZE;
7059 }
7060 vm_object_unlock(object);
7061
2d21ac55
A
7062 if (range) {
7063 if (offset > offset_end)
7064 offset = offset_end;
b0d623f7
A
7065 if(offset > offset_beg) {
7066 *range = (uint32_t) (offset - offset_beg);
7067 assert(*range == (offset - offset_beg));
7068 } else {
7069 *range = 0;
7070 }
2d21ac55 7071 }
0c530ab8
A
7072 return KERN_SUCCESS;
7073}
2d21ac55 7074
39236c6e
A
7075/*
7076 * Used to point a pager directly to a range of memory (when the pager may be associated
7077 * with a non-device vnode). Takes a virtual address, an offset, and a size. We currently
7078 * expect that the virtual address will denote the start of a range that is physically contiguous.
7079 */
7080kern_return_t pager_map_to_phys_contiguous(
7081 memory_object_control_t object,
7082 memory_object_offset_t offset,
7083 addr64_t base_vaddr,
7084 vm_size_t size)
7085{
7086 ppnum_t page_num;
7087 boolean_t clobbered_private;
7088 kern_return_t retval;
7089 vm_object_t pager_object;
7090
7091 page_num = pmap_find_phys(kernel_pmap, base_vaddr);
7092
7093 if (!page_num) {
7094 retval = KERN_FAILURE;
7095 goto out;
7096 }
7097
7098 pager_object = memory_object_control_to_vm_object(object);
7099
7100 if (!pager_object) {
7101 retval = KERN_FAILURE;
7102 goto out;
7103 }
7104
7105 clobbered_private = pager_object->private;
39037602
A
7106 if (pager_object->private != TRUE) {
7107 vm_object_lock(pager_object);
7108 pager_object->private = TRUE;
7109 vm_object_unlock(pager_object);
7110 }
39236c6e
A
7111 retval = vm_object_populate_with_private(pager_object, offset, page_num, size);
7112
39037602
A
7113 if (retval != KERN_SUCCESS) {
7114 if (pager_object->private != clobbered_private) {
7115 vm_object_lock(pager_object);
7116 pager_object->private = clobbered_private;
7117 vm_object_unlock(pager_object);
7118 }
7119 }
39236c6e
A
7120
7121out:
7122 return retval;
7123}
2d21ac55
A
7124
7125uint32_t scan_object_collision = 0;
7126
7127void
7128vm_object_lock(vm_object_t object)
7129{
7130 if (object == vm_pageout_scan_wants_object) {
7131 scan_object_collision++;
7132 mutex_pause(2);
7133 }
7134 lck_rw_lock_exclusive(&object->Lock);
39037602
A
7135#if DEVELOPMENT || DEBUG
7136 object->Lock_owner = current_thread();
7137#endif
2d21ac55
A
7138}
7139
7140boolean_t
b0d623f7 7141vm_object_lock_avoid(vm_object_t object)
2d21ac55
A
7142{
7143 if (object == vm_pageout_scan_wants_object) {
7144 scan_object_collision++;
b0d623f7 7145 return TRUE;
2d21ac55 7146 }
b0d623f7
A
7147 return FALSE;
7148}
7149
7150boolean_t
7151_vm_object_lock_try(vm_object_t object)
7152{
39037602
A
7153 boolean_t retval;
7154
7155 retval = lck_rw_try_lock_exclusive(&object->Lock);
7156#if DEVELOPMENT || DEBUG
7157 if (retval == TRUE)
7158 object->Lock_owner = current_thread();
7159#endif
7160 return (retval);
2d21ac55
A
7161}
7162
b0d623f7
A
7163boolean_t
7164vm_object_lock_try(vm_object_t object)
7165{
6d2010ae
A
7166 /*
7167 * Called from hibernate path so check before blocking.
7168 */
7169 if (vm_object_lock_avoid(object) && ml_get_interrupts_enabled() && get_preemption_level()==0) {
b0d623f7
A
7170 mutex_pause(2);
7171 }
7172 return _vm_object_lock_try(object);
7173}
6d2010ae 7174
2d21ac55
A
7175void
7176vm_object_lock_shared(vm_object_t object)
7177{
b0d623f7 7178 if (vm_object_lock_avoid(object)) {
2d21ac55
A
7179 mutex_pause(2);
7180 }
7181 lck_rw_lock_shared(&object->Lock);
7182}
7183
5ba3f43e
A
7184boolean_t
7185vm_object_lock_yield_shared(vm_object_t object)
7186{
7187 boolean_t retval = FALSE, force_yield = FALSE;;
7188
7189 vm_object_lock_assert_shared(object);
7190
7191 force_yield = vm_object_lock_avoid(object);
7192
7193 retval = lck_rw_lock_yield_shared(&object->Lock, force_yield);
7194
7195 return (retval);
7196}
7197
2d21ac55
A
7198boolean_t
7199vm_object_lock_try_shared(vm_object_t object)
7200{
b0d623f7 7201 if (vm_object_lock_avoid(object)) {
2d21ac55
A
7202 mutex_pause(2);
7203 }
7204 return (lck_rw_try_lock_shared(&object->Lock));
7205}
6d2010ae 7206
39037602
A
7207boolean_t
7208vm_object_lock_upgrade(vm_object_t object)
7209{ boolean_t retval;
7210
7211 retval = lck_rw_lock_shared_to_exclusive(&object->Lock);
7212#if DEVELOPMENT || DEBUG
7213 if (retval == TRUE)
7214 object->Lock_owner = current_thread();
7215#endif
7216 return (retval);
7217}
7218
7219void
7220vm_object_unlock(vm_object_t object)
7221{
7222#if DEVELOPMENT || DEBUG
7223 if (object->Lock_owner) {
7224 if (object->Lock_owner != current_thread())
7225 panic("vm_object_unlock: not owner - %p\n", object);
7226 object->Lock_owner = 0;
7227 }
7228#endif
7229 lck_rw_done(&object->Lock);
7230}
7231
6d2010ae
A
7232
7233unsigned int vm_object_change_wimg_mode_count = 0;
7234
7235/*
7236 * The object must be locked
7237 */
7238void
7239vm_object_change_wimg_mode(vm_object_t object, unsigned int wimg_mode)
7240{
7241 vm_page_t p;
7242
7243 vm_object_lock_assert_exclusive(object);
7244
7245 vm_object_paging_wait(object, THREAD_UNINT);
7246
39037602 7247 vm_page_queue_iterate(&object->memq, p, vm_page_t, listq) {
6d2010ae
A
7248
7249 if (!p->fictitious)
39037602 7250 pmap_set_cache_attributes(VM_PAGE_GET_PHYS_PAGE(p), wimg_mode);
6d2010ae
A
7251 }
7252 if (wimg_mode == VM_WIMG_USE_DEFAULT)
7253 object->set_cache_attr = FALSE;
7254 else
7255 object->set_cache_attr = TRUE;
7256
7257 object->wimg_bits = wimg_mode;
7258
7259 vm_object_change_wimg_mode_count++;
7260}
7261
7262#if CONFIG_FREEZE
7263
3e170ce0
A
7264/*
7265 * This routine does the "relocation" of previously
7266 * compressed pages belonging to this object that are
7267 * residing in a number of compressed segments into
7268 * a set of compressed segments dedicated to hold
7269 * compressed pages belonging to this object.
7270 */
7271
7272extern void *freezer_chead;
7273extern char *freezer_compressor_scratch_buf;
7274extern int c_freezer_compression_count;
7275extern AbsoluteTime c_freezer_last_yield_ts;
7276
7277#define MAX_FREE_BATCH 32
7278#define FREEZER_DUTY_CYCLE_ON_MS 5
7279#define FREEZER_DUTY_CYCLE_OFF_MS 5
7280
7281static int c_freezer_should_yield(void);
7282
7283
7284static int
7285c_freezer_should_yield()
7286{
7287 AbsoluteTime cur_time;
7288 uint64_t nsecs;
7289
7290 assert(c_freezer_last_yield_ts);
7291 clock_get_uptime(&cur_time);
7292
7293 SUB_ABSOLUTETIME(&cur_time, &c_freezer_last_yield_ts);
7294 absolutetime_to_nanoseconds(cur_time, &nsecs);
7295
7296 if (nsecs > 1000 * 1000 * FREEZER_DUTY_CYCLE_ON_MS)
7297 return (1);
7298 return (0);
7299}
7300
7301
6d2010ae 7302void
3e170ce0
A
7303vm_object_compressed_freezer_done()
7304{
7305 vm_compressor_finished_filling(&freezer_chead);
7306}
7307
7308
7309void
7310vm_object_compressed_freezer_pageout(
6d2010ae
A
7311 vm_object_t object)
7312{
3e170ce0
A
7313 vm_page_t p;
7314 vm_page_t local_freeq = NULL;
7315 int local_freed = 0;
7316 kern_return_t retval = KERN_SUCCESS;
7317 int obj_resident_page_count_snapshot = 0;
7318
7319 assert(object != VM_OBJECT_NULL);
39037602 7320 assert(object->internal);
39236c6e 7321
6d2010ae 7322 vm_object_lock(object);
39236c6e 7323
3e170ce0
A
7324 if (!object->pager_initialized || object->pager == MEMORY_OBJECT_NULL) {
7325
39236c6e 7326 if (!object->pager_initialized) {
3e170ce0
A
7327
7328 vm_object_collapse(object, (vm_object_offset_t) 0, TRUE);
7329
7330 if (!object->pager_initialized)
7331 vm_object_compressor_pager_create(object);
39236c6e 7332 }
fe8ab488 7333
3e170ce0
A
7334 if (!object->pager_initialized || object->pager == MEMORY_OBJECT_NULL) {
7335 vm_object_unlock(object);
7336 return;
7337 }
fe8ab488
A
7338 }
7339
39037602 7340 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
3e170ce0
A
7341 vm_object_offset_t curr_offset = 0;
7342
7343 /*
7344 * Go through the object and make sure that any
7345 * previously compressed pages are relocated into
7346 * a compressed segment associated with our "freezer_chead".
7347 */
7348 while (curr_offset < object->vo_size) {
7349
7350 curr_offset = vm_compressor_pager_next_compressed(object->pager, curr_offset);
7351
7352 if (curr_offset == (vm_object_offset_t) -1)
7353 break;
7354
7355 retval = vm_compressor_pager_relocate(object->pager, curr_offset, &freezer_chead);
7356
7357 if (retval != KERN_SUCCESS)
7358 break;
fe8ab488 7359
3e170ce0
A
7360 curr_offset += PAGE_SIZE_64;
7361 }
39236c6e
A
7362 }
7363
3e170ce0
A
7364 /*
7365 * We can't hold the object lock while heading down into the compressed pager
7366 * layer because we might need the kernel map lock down there to allocate new
7367 * compressor data structures. And if this same object is mapped in the kernel
7368 * and there's a fault on it, then that thread will want the object lock while
7369 * holding the kernel map lock.
7370 *
7371 * Since we are going to drop/grab the object lock repeatedly, we must make sure
7372 * we won't be stuck in an infinite loop if the same page(s) keep getting
7373 * decompressed. So we grab a snapshot of the number of pages in the object and
7374 * we won't process any more than that number of pages.
7375 */
7376
7377 obj_resident_page_count_snapshot = object->resident_page_count;
7378
7379 vm_object_activity_begin(object);
7380
39037602 7381 while ((obj_resident_page_count_snapshot--) && !vm_page_queue_empty(&object->memq)) {
3e170ce0 7382
39037602 7383 p = (vm_page_t)vm_page_queue_first(&object->memq);
3e170ce0
A
7384
7385 KERNEL_DEBUG(0xe0430004 | DBG_FUNC_START, object, local_freed, 0, 0, 0);
6d2010ae 7386
6d2010ae
A
7387 vm_page_lockspin_queues();
7388
3e170ce0 7389 if (p->cleaning || p->fictitious || p->busy || p->absent || p->unusual || p->error || VM_PAGE_WIRED(p)) {
3e170ce0
A
7390
7391 vm_page_unlock_queues();
7392
7393 KERNEL_DEBUG(0xe0430004 | DBG_FUNC_END, object, local_freed, 1, 0, 0);
7394
39037602
A
7395 vm_page_queue_remove(&object->memq, p, vm_page_t, listq);
7396 vm_page_queue_enter(&object->memq, p, vm_page_t, listq);
3e170ce0
A
7397
7398 continue;
7399 }
7400
7401 if (p->pmapped == TRUE) {
7402 int refmod_state, pmap_flags;
7403
7404 if (p->dirty || p->precious) {
7405 pmap_flags = PMAP_OPTIONS_COMPRESSOR;
7406 } else {
7407 pmap_flags = PMAP_OPTIONS_COMPRESSOR_IFF_MODIFIED;
7408 }
7409
39037602 7410 refmod_state = pmap_disconnect_options(VM_PAGE_GET_PHYS_PAGE(p), pmap_flags, NULL);
3e170ce0
A
7411 if (refmod_state & VM_MEM_MODIFIED) {
7412 SET_PAGE_DIRTY(p, FALSE);
7413 }
7414 }
7415
7416 if (p->dirty == FALSE && p->precious == FALSE) {
7417 /*
7418 * Clean and non-precious page.
7419 */
7420 vm_page_unlock_queues();
7421 VM_PAGE_FREE(p);
7422
7423 KERNEL_DEBUG(0xe0430004 | DBG_FUNC_END, object, local_freed, 2, 0, 0);
7424 continue;
7425 }
7426
39037602 7427 if (p->laundry)
3e170ce0 7428 vm_pageout_steal_laundry(p, TRUE);
3e170ce0 7429
39037602
A
7430 vm_page_queues_remove(p, TRUE);
7431
3e170ce0
A
7432 vm_page_unlock_queues();
7433
7434
316670eb 7435 /*
3e170ce0
A
7436 * In case the compressor fails to compress this page, we need it at
7437 * the back of the object memq so that we don't keep trying to process it.
7438 * Make the move here while we have the object lock held.
316670eb 7439 */
39236c6e 7440
39037602
A
7441 vm_page_queue_remove(&object->memq, p, vm_page_t, listq);
7442 vm_page_queue_enter(&object->memq, p, vm_page_t, listq);
39236c6e 7443
3e170ce0
A
7444 /*
7445 * Grab an activity_in_progress here for vm_pageout_compress_page() to consume.
7446 *
7447 * Mark the page busy so no one messes with it while we have the object lock dropped.
7448 */
39236c6e 7449
3e170ce0 7450 p->busy = TRUE;
39236c6e 7451
3e170ce0 7452 vm_object_activity_begin(object);
39236c6e 7453
3e170ce0
A
7454 vm_object_unlock(object);
7455
7456 /*
7457 * arg3 == FALSE tells vm_pageout_compress_page that we don't hold the object lock and the pager may not be initialized.
7458 */
7459 if (vm_pageout_compress_page(&freezer_chead, freezer_compressor_scratch_buf, p, FALSE) == KERN_SUCCESS) {
7460 /*
7461 * page has already been un-tabled from the object via 'vm_page_remove'
7462 */
39037602 7463 p->snext = local_freeq;
3e170ce0
A
7464 local_freeq = p;
7465 local_freed++;
7466
7467 if (local_freed >= MAX_FREE_BATCH) {
7468
7469 vm_page_free_list(local_freeq, TRUE);
39236c6e 7470
3e170ce0
A
7471 local_freeq = NULL;
7472 local_freed = 0;
39236c6e 7473 }
3e170ce0
A
7474 c_freezer_compression_count++;
7475 }
7476 KERNEL_DEBUG(0xe0430004 | DBG_FUNC_END, object, local_freed, 0, 0, 0);
7477
7478 if (local_freed == 0 && c_freezer_should_yield()) {
39236c6e 7479
3e170ce0
A
7480 thread_yield_internal(FREEZER_DUTY_CYCLE_OFF_MS);
7481 clock_get_uptime(&c_freezer_last_yield_ts);
316670eb 7482 }
3e170ce0
A
7483
7484 vm_object_lock(object);
6d2010ae
A
7485 }
7486
3e170ce0
A
7487 if (local_freeq) {
7488 vm_page_free_list(local_freeq, TRUE);
7489
7490 local_freeq = NULL;
7491 local_freed = 0;
7492 }
7493
7494 vm_object_activity_end(object);
7495
6d2010ae 7496 vm_object_unlock(object);
3e170ce0
A
7497
7498 if (c_freezer_should_yield()) {
7499
7500 thread_yield_internal(FREEZER_DUTY_CYCLE_OFF_MS);
7501 clock_get_uptime(&c_freezer_last_yield_ts);
7502 }
6d2010ae
A
7503}
7504
6d2010ae 7505#endif /* CONFIG_FREEZE */
fe8ab488
A
7506
7507
3e170ce0
A
7508void
7509vm_object_pageout(
7510 vm_object_t object)
7511{
7512 vm_page_t p, next;
7513 struct vm_pageout_queue *iq;
3e170ce0 7514
39037602
A
7515 if (!VM_CONFIG_COMPRESSOR_IS_PRESENT)
7516 return;
7517
3e170ce0
A
7518 iq = &vm_pageout_queue_internal;
7519
7520 assert(object != VM_OBJECT_NULL );
3e170ce0
A
7521
7522 vm_object_lock(object);
7523
7524 if (!object->internal ||
7525 object->terminating ||
7526 !object->alive) {
7527 vm_object_unlock(object);
7528 return;
7529 }
7530
7531 if (!object->pager_initialized || object->pager == MEMORY_OBJECT_NULL) {
7532
7533 if (!object->pager_initialized) {
7534
7535 vm_object_collapse(object, (vm_object_offset_t) 0, TRUE);
7536
7537 if (!object->pager_initialized)
7538 vm_object_compressor_pager_create(object);
7539 }
7540
7541 if (!object->pager_initialized || object->pager == MEMORY_OBJECT_NULL) {
7542 vm_object_unlock(object);
7543 return;
7544 }
7545 }
7546
7547ReScan:
39037602 7548 next = (vm_page_t)vm_page_queue_first(&object->memq);
3e170ce0 7549
39037602 7550 while (!vm_page_queue_end(&object->memq, (vm_page_queue_entry_t)next)) {
3e170ce0 7551 p = next;
39037602 7552 next = (vm_page_t)vm_page_queue_next(&next->listq);
3e170ce0 7553
39037602
A
7554 assert(p->vm_page_q_state != VM_PAGE_ON_FREE_Q);
7555
7556 if ((p->vm_page_q_state == VM_PAGE_ON_THROTTLED_Q) ||
3e170ce0
A
7557 p->cleaning ||
7558 p->laundry ||
3e170ce0
A
7559 p->busy ||
7560 p->absent ||
7561 p->error ||
7562 p->fictitious ||
7563 VM_PAGE_WIRED(p)) {
7564 /*
7565 * Page is already being cleaned or can't be cleaned.
7566 */
7567 continue;
7568 }
7569
7570 /* Throw to the pageout queue */
7571
7572 vm_page_lockspin_queues();
3e170ce0
A
7573
7574 if (vm_compressor_low_on_space()) {
7575 vm_page_unlock_queues();
7576 break;
7577 }
7578
7579 if (VM_PAGE_Q_THROTTLED(iq)) {
7580
7581 iq->pgo_draining = TRUE;
7582
7583 assert_wait((event_t) (&iq->pgo_laundry + 1),
7584 THREAD_INTERRUPTIBLE);
7585 vm_page_unlock_queues();
7586 vm_object_unlock(object);
7587
7588 thread_block(THREAD_CONTINUE_NULL);
7589
7590 vm_object_lock(object);
7591 goto ReScan;
7592 }
7593
7594 assert(!p->fictitious);
7595 assert(!p->busy);
7596 assert(!p->absent);
7597 assert(!p->unusual);
7598 assert(!p->error);
7599 assert(!VM_PAGE_WIRED(p));
7600 assert(!p->cleaning);
7601
7602 if (p->pmapped == TRUE) {
7603 int refmod_state;
7604 int pmap_options;
7605
39037602
A
7606 /*
7607 * Tell pmap the page should be accounted
7608 * for as "compressed" if it's been modified.
7609 */
7610 pmap_options =
7611 PMAP_OPTIONS_COMPRESSOR_IFF_MODIFIED;
7612 if (p->dirty || p->precious) {
3e170ce0 7613 /*
39037602
A
7614 * We already know it's been modified,
7615 * so tell pmap to account for it
7616 * as "compressed".
3e170ce0 7617 */
39037602 7618 pmap_options = PMAP_OPTIONS_COMPRESSOR;
3e170ce0 7619 }
39037602 7620 refmod_state = pmap_disconnect_options(VM_PAGE_GET_PHYS_PAGE(p),
3e170ce0
A
7621 pmap_options,
7622 NULL);
7623 if (refmod_state & VM_MEM_MODIFIED) {
7624 SET_PAGE_DIRTY(p, FALSE);
7625 }
7626 }
7627
7628 if (!p->dirty && !p->precious) {
7629 vm_page_unlock_queues();
7630 VM_PAGE_FREE(p);
7631 continue;
7632 }
39037602
A
7633 vm_page_queues_remove(p, TRUE);
7634
5ba3f43e
A
7635 vm_pageout_cluster(p);
7636
7637 vm_page_unlock_queues();
3e170ce0 7638 }
3e170ce0
A
7639 vm_object_unlock(object);
7640}
7641
7642
fe8ab488
A
7643#if CONFIG_IOSCHED
7644void
7645vm_page_request_reprioritize(vm_object_t o, uint64_t blkno, uint32_t len, int prio)
7646{
7647 io_reprioritize_req_t req;
7648 struct vnode *devvp = NULL;
7649
7650 if(vnode_pager_get_object_devvp(o->pager, (uintptr_t *)&devvp) != KERN_SUCCESS)
7651 return;
7652
3e170ce0
A
7653 /*
7654 * Create the request for I/O reprioritization.
7655 * We use the noblock variant of zalloc because we're holding the object
7656 * lock here and we could cause a deadlock in low memory conditions.
7657 */
7658 req = (io_reprioritize_req_t)zalloc_noblock(io_reprioritize_req_zone);
7659 if (req == NULL)
7660 return;
fe8ab488
A
7661 req->blkno = blkno;
7662 req->len = len;
7663 req->priority = prio;
7664 req->devvp = devvp;
7665
7666 /* Insert request into the reprioritization list */
7667 IO_REPRIORITIZE_LIST_LOCK();
7668 queue_enter(&io_reprioritize_list, req, io_reprioritize_req_t, io_reprioritize_list);
7669 IO_REPRIORITIZE_LIST_UNLOCK();
7670
7671 /* Wakeup reprioritize thread */
7672 IO_REPRIO_THREAD_WAKEUP();
7673
7674 return;
7675}
7676
7677void
7678vm_decmp_upl_reprioritize(upl_t upl, int prio)
7679{
7680 int offset;
7681 vm_object_t object;
7682 io_reprioritize_req_t req;
7683 struct vnode *devvp = NULL;
7684 uint64_t blkno;
7685 uint32_t len;
7686 upl_t io_upl;
7687 uint64_t *io_upl_reprio_info;
7688 int io_upl_size;
7689
7690 if ((upl->flags & UPL_TRACKED_BY_OBJECT) == 0 || (upl->flags & UPL_EXPEDITE_SUPPORTED) == 0)
7691 return;
7692
7693 /*
7694 * We dont want to perform any allocations with the upl lock held since that might
7695 * result in a deadlock. If the system is low on memory, the pageout thread would
7696 * try to pageout stuff and might wait on this lock. If we are waiting for the memory to
7697 * be freed up by the pageout thread, it would be a deadlock.
7698 */
7699
7700
7701 /* First step is just to get the size of the upl to find out how big the reprio info is */
a1c7dba1
A
7702 if(!upl_try_lock(upl))
7703 return;
7704
fe8ab488
A
7705 if (upl->decmp_io_upl == NULL) {
7706 /* The real I/O upl was destroyed by the time we came in here. Nothing to do. */
7707 upl_unlock(upl);
7708 return;
7709 }
7710
7711 io_upl = upl->decmp_io_upl;
7712 assert((io_upl->flags & UPL_DECMP_REAL_IO) != 0);
7713 io_upl_size = io_upl->size;
7714 upl_unlock(upl);
7715
7716 /* Now perform the allocation */
7717 io_upl_reprio_info = (uint64_t *)kalloc(sizeof(uint64_t) * (io_upl_size / PAGE_SIZE));
7718 if (io_upl_reprio_info == NULL)
7719 return;
7720
7721 /* Now again take the lock, recheck the state and grab out the required info */
a1c7dba1
A
7722 if(!upl_try_lock(upl))
7723 goto out;
7724
fe8ab488
A
7725 if (upl->decmp_io_upl == NULL || upl->decmp_io_upl != io_upl) {
7726 /* The real I/O upl was destroyed by the time we came in here. Nothing to do. */
7727 upl_unlock(upl);
7728 goto out;
7729 }
7730 memcpy(io_upl_reprio_info, io_upl->upl_reprio_info, sizeof(uint64_t) * (io_upl_size / PAGE_SIZE));
7731
7732 /* Get the VM object for this UPL */
7733 if (io_upl->flags & UPL_SHADOWED) {
7734 object = io_upl->map_object->shadow;
7735 } else {
7736 object = io_upl->map_object;
7737 }
7738
7739 /* Get the dev vnode ptr for this object */
7740 if(!object || !object->pager ||
7741 vnode_pager_get_object_devvp(object->pager, (uintptr_t *)&devvp) != KERN_SUCCESS) {
7742 upl_unlock(upl);
7743 goto out;
7744 }
7745
7746 upl_unlock(upl);
7747
7748 /* Now we have all the information needed to do the expedite */
7749
7750 offset = 0;
7751 while (offset < io_upl_size) {
7752 blkno = io_upl_reprio_info[(offset / PAGE_SIZE)] & UPL_REPRIO_INFO_MASK;
7753 len = (io_upl_reprio_info[(offset / PAGE_SIZE)] >> UPL_REPRIO_INFO_SHIFT) & UPL_REPRIO_INFO_MASK;
7754
7755 /*
7756 * This implementation may cause some spurious expedites due to the
7757 * fact that we dont cleanup the blkno & len from the upl_reprio_info
7758 * even after the I/O is complete.
7759 */
7760
7761 if (blkno != 0 && len != 0) {
7762 /* Create the request for I/O reprioritization */
7763 req = (io_reprioritize_req_t)zalloc(io_reprioritize_req_zone);
7764 assert(req != NULL);
7765 req->blkno = blkno;
7766 req->len = len;
7767 req->priority = prio;
7768 req->devvp = devvp;
7769
7770 /* Insert request into the reprioritization list */
7771 IO_REPRIORITIZE_LIST_LOCK();
7772 queue_enter(&io_reprioritize_list, req, io_reprioritize_req_t, io_reprioritize_list);
7773 IO_REPRIORITIZE_LIST_UNLOCK();
7774
7775 offset += len;
7776 } else {
7777 offset += PAGE_SIZE;
7778 }
7779 }
7780
7781 /* Wakeup reprioritize thread */
7782 IO_REPRIO_THREAD_WAKEUP();
7783
7784out:
7785 kfree(io_upl_reprio_info, sizeof(uint64_t) * (io_upl_size / PAGE_SIZE));
7786 return;
7787}
7788
7789void
7790vm_page_handle_prio_inversion(vm_object_t o, vm_page_t m)
7791{
7792 upl_t upl;
7793 upl_page_info_t *pl;
7794 unsigned int i, num_pages;
7795 int cur_tier;
7796
7797 cur_tier = proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO);
7798
7799 /*
7800 Scan through all UPLs associated with the object to find the
7801 UPL containing the contended page.
7802 */
7803 queue_iterate(&o->uplq, upl, upl_t, uplq) {
7804 if (((upl->flags & UPL_EXPEDITE_SUPPORTED) == 0) || upl->upl_priority <= cur_tier)
7805 continue;
7806 pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
7807 num_pages = (upl->size / PAGE_SIZE);
7808
7809 /*
7810 For each page in the UPL page list, see if it matches the contended
7811 page and was issued as a low prio I/O.
7812 */
7813 for(i=0; i < num_pages; i++) {
39037602 7814 if(UPL_PAGE_PRESENT(pl,i) && VM_PAGE_GET_PHYS_PAGE(m) == pl[i].phys_addr) {
fe8ab488 7815 if ((upl->flags & UPL_DECMP_REQ) && upl->decmp_io_upl) {
d190cdc3
A
7816 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, VM_PAGE_EXPEDITE)) | DBG_FUNC_NONE, VM_KERNEL_UNSLIDE_OR_PERM(upl->upl_creator), VM_KERNEL_UNSLIDE_OR_PERM(m),
7817 VM_KERNEL_UNSLIDE_OR_PERM(upl), upl->upl_priority, 0);
fe8ab488
A
7818 vm_decmp_upl_reprioritize(upl, cur_tier);
7819 break;
7820 }
d190cdc3
A
7821 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, VM_PAGE_EXPEDITE)) | DBG_FUNC_NONE, VM_KERNEL_UNSLIDE_OR_PERM(upl->upl_creator), VM_KERNEL_UNSLIDE_OR_PERM(m),
7822 upl->upl_reprio_info[i], upl->upl_priority, 0);
fe8ab488
A
7823 if (UPL_REPRIO_INFO_BLKNO(upl, i) != 0 && UPL_REPRIO_INFO_LEN(upl, i) != 0)
7824 vm_page_request_reprioritize(o, UPL_REPRIO_INFO_BLKNO(upl, i), UPL_REPRIO_INFO_LEN(upl, i), cur_tier);
7825 break;
7826 }
7827 }
7828 /* Check if we found any hits */
7829 if (i != num_pages)
7830 break;
7831 }
7832
7833 return;
7834}
7835
7836wait_result_t
7837vm_page_sleep(vm_object_t o, vm_page_t m, int interruptible)
7838{
7839 wait_result_t ret;
7840
d190cdc3 7841 KERNEL_DEBUG((MACHDBG_CODE(DBG_MACH_VM, VM_PAGE_SLEEP)) | DBG_FUNC_START, o, m, 0, 0, 0);
fe8ab488
A
7842
7843 if (o->io_tracking && ((m->busy == TRUE) || (m->cleaning == TRUE) || VM_PAGE_WIRED(m))) {
7844 /*
7845 Indicates page is busy due to an I/O. Issue a reprioritize request if necessary.
7846 */
7847 vm_page_handle_prio_inversion(o,m);
7848 }
7849 m->wanted = TRUE;
7850 ret = thread_sleep_vm_object(o, m, interruptible);
7851 KERNEL_DEBUG((MACHDBG_CODE(DBG_MACH_VM, VM_PAGE_SLEEP)) | DBG_FUNC_END, o, m, 0, 0, 0);
7852 return ret;
7853}
7854
7855static void
7856io_reprioritize_thread(void *param __unused, wait_result_t wr __unused)
7857{
7858 io_reprioritize_req_t req = NULL;
7859
7860 while(1) {
7861
7862 IO_REPRIORITIZE_LIST_LOCK();
7863 if (queue_empty(&io_reprioritize_list)) {
7864 IO_REPRIORITIZE_LIST_UNLOCK();
7865 break;
7866 }
7867
7868 queue_remove_first(&io_reprioritize_list, req, io_reprioritize_req_t, io_reprioritize_list);
7869 IO_REPRIORITIZE_LIST_UNLOCK();
7870
7871 vnode_pager_issue_reprioritize_io(req->devvp, req->blkno, req->len, req->priority);
7872 zfree(io_reprioritize_req_zone, req);
7873 }
7874
7875 IO_REPRIO_THREAD_CONTINUATION();
7876}
7877#endif