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