2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * File: vm/vm_object.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
62 * Virtual memory object module.
66 #include <mach_pagemap.h>
67 #include <task_swapper.h>
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>
77 #include <ipc/ipc_types.h>
78 #include <ipc/ipc_port.h>
80 #include <kern/kern_types.h>
81 #include <kern/assert.h>
82 #include <kern/queue.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>
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>
102 #include <vm/vm_compressor.h>
104 #if CONFIG_PHANTOM_CACHE
105 #include <vm/vm_phantom_cache.h>
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 */
113 boolean_t vm_object_collapse_compressor_allowed
= TRUE
;
115 struct vm_counters vm_counters
;
117 #if VM_OBJECT_TRACKING
118 boolean_t vm_object_tracking_inited
= FALSE
;
119 btlog_t
*vm_object_tracking_btlog
;
122 vm_object_tracking_init(void)
124 int vm_object_tracking
;
126 vm_object_tracking
= 1;
127 PE_parse_boot_argn("vm_object_tracking", &vm_object_tracking
,
128 sizeof(vm_object_tracking
));
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
;
139 #endif /* VM_OBJECT_TRACKING */
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.
146 * An object is only deallocated when all "references"
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
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...
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).
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
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.
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.
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.
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
211 * ZZZ Continue this comment.
214 /* Forward declarations for internal functions. */
215 static kern_return_t
vm_object_terminate(
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
);
224 static void vm_object_do_collapse(
226 vm_object_t backing_object
);
228 static void vm_object_do_bypass(
230 vm_object_t backing_object
);
232 static void vm_object_release_pager(
233 memory_object_t pager
);
235 zone_t vm_object_zone
; /* vm backing store zone */
238 * All wired-down kernel memory belongs to a single virtual
239 * memory object (kernel_object) to avoid wasting data structures.
241 static struct vm_object kernel_object_store
__attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT
)));
242 vm_object_t kernel_object
;
244 static struct vm_object compressor_object_store
__attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT
)));
245 vm_object_t compressor_object
= &compressor_object_store
;
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.
253 static struct vm_object vm_submap_object_store
__attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT
)));
256 * Virtual memory objects are initialized from
257 * a template (see vm_object_allocate).
259 * When adding a new field to the virtual memory
260 * object structure, be sure to add initialization
261 * (see _vm_object_allocate()).
263 static struct vm_object vm_object_template
;
265 unsigned int vm_page_purged_wired
= 0;
266 unsigned int vm_page_purged_busy
= 0;
267 unsigned int vm_page_purged_others
= 0;
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
;
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;
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)
289 static void vm_object_cache_remove_locked(vm_object_t
);
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);
296 static lck_mtx_t vm_object_reaper_lock_data
;
297 static lck_mtx_ext_t vm_object_reaper_lock_data_ext
;
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;
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)
311 /* I/O Re-prioritization request list */
312 queue_head_t io_reprioritize_list
;
313 lck_spin_t io_reprioritize_list_lock
;
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)
320 #define MAX_IO_REPRIORITIZE_REQS 8192
321 zone_t io_reprioritize_req_zone
;
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
);
327 #define IO_REPRIO_THREAD_WAKEUP() thread_wakeup((event_t)&io_reprioritize_wakeup)
328 #define IO_REPRIO_THREAD_CONTINUATION() \
330 assert_wait(&io_reprioritize_wakeup, THREAD_UNINT); \
331 thread_block(io_reprioritize_thread); \
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);
341 #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
346 * vm_object_allocate:
348 * Returns a new object with the given size.
351 __private_extern__
void
353 vm_object_size_t size
,
357 "vm_object_allocate, object 0x%X size 0x%X\n",
358 object
, size
, 0, 0, 0);
360 *object
= vm_object_template
;
361 vm_page_queue_init(&object
->memq
);
362 #if UPL_DEBUG || CONFIG_IOSCHED
363 queue_init(&object
->uplq
);
365 vm_object_lock_init(object
);
366 object
->vo_size
= size
;
368 #if VM_OBJECT_TRACKING_OP_CREATED
369 if (vm_object_tracking_inited
) {
370 void *bt
[VM_OBJECT_TRACKING_BTDEPTH
];
373 numsaved
= OSBacktrace(bt
, VM_OBJECT_TRACKING_BTDEPTH
);
374 btlog_add_entry(vm_object_tracking_btlog
,
376 VM_OBJECT_TRACKING_OP_CREATED
,
380 #endif /* VM_OBJECT_TRACKING_OP_CREATED */
383 __private_extern__ vm_object_t
385 vm_object_size_t size
)
389 object
= (vm_object_t
) zalloc(vm_object_zone
);
391 // dbgLog(object, size, 0, 2); /* (TEST/DEBUG) */
393 if (object
!= VM_OBJECT_NULL
) {
394 _vm_object_allocate(size
, object
);
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
;
408 extern void vm_named_entry_init(void);
410 int workaround_41447923
= 0;
413 * vm_object_bootstrap:
415 * Initialize the VM objects module.
417 __private_extern__
void
418 vm_object_bootstrap(void)
420 vm_size_t vm_object_size
;
422 assert(sizeof(mo_ipc_object_bits_t
) == sizeof(ipc_object_bits_t
));
424 vm_object_size
= (sizeof(struct vm_object
) + (VM_PACKED_POINTER_ALIGNMENT
- 1)) & ~(VM_PACKED_POINTER_ALIGNMENT
- 1);
426 vm_object_zone
= zinit(vm_object_size
,
427 round_page(512 * 1024),
428 round_page(12 * 1024),
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
);
434 vm_object_init_lck_grp();
436 queue_init(&vm_object_cached_list
);
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
);
443 queue_init(&vm_object_reaper_queue
);
445 lck_mtx_init_ext(&vm_object_reaper_lock_data
,
446 &vm_object_reaper_lock_data_ext
,
448 &vm_object_lck_attr
);
452 * Fill in a template object, for quick initialization
455 /* memq; Lock; init after allocation */
457 vm_object_template
.memq
.prev
= 0;
458 vm_object_template
.memq
.next
= 0;
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.
467 vm_object_lock_init(&vm_object_template
);
469 #if DEVELOPMENT || DEBUG
470 vm_object_template
.Lock_owner
= 0;
472 vm_object_template
.vo_size
= 0;
473 vm_object_template
.memq_hint
= VM_PAGE_NULL
;
474 vm_object_template
.ref_count
= 1;
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;
490 vm_object_template
.__object1_unused_bits
= 0;
491 #endif /* __LP64__ */
492 vm_object_template
.activity_in_progress
= 0;
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
;
517 vm_object_template
.cached_list
.prev
= NULL
;
518 vm_object_template
.cached_list
.next
= NULL
;
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;
528 vm_object_template
.cow_hint
= ~(vm_offset_t
)0;
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 */
549 bzero(&vm_object_template
.pip_holders
,
550 sizeof(vm_object_template
.pip_holders
));
551 #endif /* VM_PIP_DEBUG */
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
;
558 vm_object_template
.purgeable_queue_type
= PURGEABLE_Q_TYPE_MAX
;
559 vm_object_template
.purgeable_queue_group
= 0;
561 vm_object_template
.vo_cache_ts
= 0;
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 */
569 vm_object_template
.io_tracking
= FALSE
;
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 */
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 */
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
));
593 * Initialize the "kernel object"
596 kernel_object
= &kernel_object_store
;
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.
603 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
606 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
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
;
613 * Initialize the "submap object". Make it as large as the
614 * kernel object so that no limit is imposed on submap sizes.
617 vm_submap_object
= &vm_submap_object_store
;
618 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
620 vm_submap_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
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
627 vm_object_reference(vm_submap_object
);
629 vm_named_entry_init();
631 PE_parse_boot_argn("workaround_41447923", &workaround_41447923
,
632 sizeof(workaround_41447923
));
637 vm_io_reprioritize_init(void)
639 kern_return_t result
;
640 thread_t thread
= THREAD_NULL
;
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
);
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
);
651 result
= kernel_thread_start_priority(io_reprioritize_thread
, NULL
, 95 /* MAXPRI_KERNEL */, &thread
);
652 if (result
== KERN_SUCCESS
) {
653 thread_deallocate(thread
);
655 panic("Could not create io_reprioritize_thread");
661 vm_object_reaper_init(void)
666 kr
= kernel_thread_start_priority(
667 (thread_continue_t
) vm_object_reaper_thread
,
671 if (kr
!= KERN_SUCCESS
) {
672 panic("failed to launch vm_object_reaper_thread kr=0x%x", kr
);
674 thread_deallocate(thread
);
677 __private_extern__
void
681 * Finish initializing the kernel object.
686 __private_extern__
void
687 vm_object_init_lck_grp(void)
690 * initialze the vm_object lock world
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
);
704 * vm_object_deallocate:
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.
712 * No object may be locked.
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;
718 __private_extern__
void
719 vm_object_deallocate(
722 vm_object_t shadow
= VM_OBJECT_NULL
;
724 // if(object)dbgLog(object, object->ref_count, object->can_persist, 3); /* (TEST/DEBUG) */
725 // else dbgLog(object, 0, 0, 3); /* (TEST/DEBUG) */
727 if (object
== VM_OBJECT_NULL
) {
731 if (object
== kernel_object
|| object
== compressor_object
) {
732 vm_object_lock_shared(object
);
734 OSAddAtomic(-1, &object
->ref_count
);
736 if (object
->ref_count
== 0) {
737 if (object
== kernel_object
) {
738 panic("vm_object_deallocate: losing kernel_object\n");
740 panic("vm_object_deallocate: losing compressor_object\n");
743 vm_object_unlock(object
);
747 if (object
->ref_count
== 2 &&
750 * This "named" object's reference count is about to
752 * we'll need to call memory_object_last_unmap().
754 } else if (object
->ref_count
== 2 &&
756 object
->shadow
!= VM_OBJECT_NULL
) {
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
763 } else if (object
->ref_count
>= 2) {
764 UInt32 original_ref_count
;
765 volatile UInt32
*ref_count_p
;
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".
777 vm_object_lock_shared(object
);
778 ref_count_p
= (volatile UInt32
*) &object
->ref_count
;
779 original_ref_count
= object
->ref_count
;
781 * Test again as "ref_count" could have changed.
782 * "named" shouldn't change.
784 if (original_ref_count
== 2 &&
786 /* need to take slow path for m_o_last_unmap() */
788 } else if (original_ref_count
== 2 &&
790 object
->shadow
!= VM_OBJECT_NULL
) {
791 /* need to take slow path for vm_object_collapse() */
793 } else if (original_ref_count
< 2) {
794 /* need to take slow path for vm_object_terminate() */
797 /* try an atomic update with the shared lock */
798 atomic_swap
= OSCompareAndSwap(
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... */
808 vm_object_unlock(object
);
812 * ref_count was updated atomically !
814 vm_object_deallocate_shared_successes
++;
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...
823 vm_object_deallocate_shared_failures
++;
826 while (object
!= VM_OBJECT_NULL
) {
827 vm_object_lock(object
);
829 assert(object
->ref_count
> 0);
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.
836 if ((object
->ref_count
== 2) && (object
->named
)) {
837 memory_object_t pager
= object
->pager
;
839 /* Notify the Pager that there are no */
840 /* more mappers for this object */
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
);
847 memory_object_last_unmap(pager
);
849 vm_object_lock(object
);
850 vm_object_mapping_end(object
);
852 assert(object
->ref_count
> 0);
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.
863 /* if the object is terminating, it cannot go into */
864 /* the cache and we obviously should not call */
865 /* terminate again. */
867 if ((object
->ref_count
> 1) || object
->terminating
) {
868 vm_object_lock_assert_exclusive(object
);
870 vm_object_res_deallocate(object
);
872 if (object
->ref_count
== 1 &&
873 object
->shadow
!= VM_OBJECT_NULL
) {
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
886 * We can't bypass this object though, since we
887 * don't know if this last reference on it is
890 vm_object_collapse(object
, 0, FALSE
);
892 vm_object_unlock(object
);
897 * We have to wait for initialization
898 * before destroying or caching the object.
901 if (object
->pager_created
&& !object
->pager_initialized
) {
902 assert(!object
->can_persist
);
903 vm_object_assert_wait(object
,
904 VM_OBJECT_EVENT_INITIALIZED
,
906 vm_object_unlock(object
);
908 thread_block(THREAD_CONTINUE_NULL
);
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
);
918 VM_OBJ_RES_DECR(object
); /* XXX ? */
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.
927 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
929 if (vm_object_terminate(object
) != KERN_SUCCESS
) {
932 if (shadow
!= VM_OBJECT_NULL
) {
950 vm_object_lock_assert_exclusive(object
);
952 next_p
= (vm_page_t
)vm_page_queue_first(&object
->memq
);
953 p_limit
= MIN(50, object
->resident_page_count
);
955 while (!vm_page_queue_end(&object
->memq
, (vm_page_queue_entry_t
)next_p
) && --p_limit
> 0) {
957 next_p
= (vm_page_t
)vm_page_queue_next(&next_p
->vmp_listq
);
959 if (VM_PAGE_WIRED(p
) || p
->vmp_busy
|| p
->vmp_cleaning
|| p
->vmp_laundry
|| p
->vmp_fictitious
) {
960 goto move_page_in_obj
;
963 if (p
->vmp_pmapped
|| p
->vmp_dirty
|| p
->vmp_precious
) {
964 vm_page_lockspin_queues();
966 if (p
->vmp_pmapped
) {
969 vm_object_page_grab_pmapped
++;
971 if (p
->vmp_reference
== FALSE
|| p
->vmp_dirty
== FALSE
) {
972 refmod_state
= pmap_get_refmod(VM_PAGE_GET_PHYS_PAGE(p
));
974 if (refmod_state
& VM_MEM_REFERENCED
) {
975 p
->vmp_reference
= TRUE
;
977 if (refmod_state
& VM_MEM_MODIFIED
) {
978 SET_PAGE_DIRTY(p
, FALSE
);
981 if (p
->vmp_dirty
== FALSE
&& p
->vmp_precious
== FALSE
) {
982 refmod_state
= pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(p
));
984 if (refmod_state
& VM_MEM_REFERENCED
) {
985 p
->vmp_reference
= TRUE
;
987 if (refmod_state
& VM_MEM_MODIFIED
) {
988 SET_PAGE_DIRTY(p
, FALSE
);
991 if (p
->vmp_dirty
== FALSE
) {
996 if ((p
->vmp_q_state
!= VM_PAGE_ON_ACTIVE_Q
) && p
->vmp_reference
== TRUE
) {
999 VM_STAT_INCR(reactivations
);
1000 vm_object_page_grab_reactivations
++;
1002 vm_page_unlock_queues();
1004 vm_page_queue_remove(&object
->memq
, p
, vmp_listq
);
1005 vm_page_queue_enter(&object
->memq
, p
, vmp_listq
);
1010 vm_page_lockspin_queues();
1012 vm_page_free_prepare_queues(p
);
1013 vm_object_page_grab_returned
++;
1014 vm_object_page_grab_skipped
+= p_skipped
;
1016 vm_page_unlock_queues();
1018 vm_page_free_prepare_object(p
, TRUE
);
1022 vm_object_page_grab_skipped
+= p_skipped
;
1023 vm_object_page_grab_failed
++;
1030 #define EVICT_PREPARE_LIMIT 64
1031 #define EVICT_AGE 10
1033 static clock_sec_t vm_object_cache_aging_ts
= 0;
1036 vm_object_cache_remove_locked(
1039 assert(object
->purgable
== VM_PURGABLE_DENY
);
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
;
1045 vm_object_cached_count
--;
1049 vm_object_cache_remove(
1052 vm_object_cache_lock_spin();
1054 if (object
->cached_list
.next
&&
1055 object
->cached_list
.prev
) {
1056 vm_object_cache_remove_locked(object
);
1059 vm_object_cache_unlock();
1063 vm_object_cache_add(
1069 assert(object
->purgable
== VM_PURGABLE_DENY
);
1071 if (object
->resident_page_count
== 0) {
1074 clock_get_system_nanotime(&sec
, &nsec
);
1076 vm_object_cache_lock_spin();
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
;
1084 vm_object_cached_count
++;
1085 vm_object_cache_adds
++;
1087 vm_object_cache_unlock();
1091 vm_object_cache_evict(
1093 int max_objects_to_examine
)
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
;
1101 vm_page_t ep_array
[EVICT_PREPARE_LIMIT
];
1107 uint32_t ep_skipped
= 0;
1111 KERNEL_DEBUG(0x13001ec | DBG_FUNC_START
, 0, 0, 0, 0, 0);
1113 * do a couple of quick checks to see if it's
1114 * worthwhile grabbing the lock
1116 if (queue_empty(&vm_object_cached_list
)) {
1117 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END
, 0, 0, 0, 0, 0);
1120 clock_get_system_nanotime(&sec
, &nsec
);
1123 * the object on the head of the queue has not
1124 * yet sufficiently aged
1126 if (sec
< vm_object_cache_aging_ts
) {
1127 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END
, 0, 0, 0, 0, 0);
1131 * don't need the queue lock to find
1132 * and lock an object on the cached list
1134 vm_page_unlock_queues();
1136 vm_object_cache_lock_spin();
1139 next_obj
= (vm_object_t
)queue_first(&vm_object_cached_list
);
1141 while (!queue_end(&vm_object_cached_list
, (queue_entry_t
)next_obj
) && object_cnt
++ < max_objects_to_examine
) {
1143 next_obj
= (vm_object_t
)queue_next(&next_obj
->cached_list
);
1145 assert(object
->purgable
== VM_PURGABLE_DENY
);
1147 if (sec
< object
->vo_cache_ts
) {
1148 KERNEL_DEBUG(0x130020c, object
, object
->resident_page_count
, object
->vo_cache_ts
, sec
, 0);
1150 vm_object_cache_aging_ts
= object
->vo_cache_ts
;
1151 object
= VM_OBJECT_NULL
;
1154 if (!vm_object_lock_try_scan(object
)) {
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
1160 KERNEL_DEBUG(0x13001f8, object
, object
->resident_page_count
, 0, 0, 0);
1162 object
= VM_OBJECT_NULL
;
1165 if (vm_page_queue_empty(&object
->memq
) || object
->vo_cache_pages_to_scan
== 0) {
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.
1171 KERNEL_DEBUG(0x13001fc, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1173 vm_object_cache_remove_locked(object
);
1174 vm_object_unlock(object
);
1175 object
= VM_OBJECT_NULL
;
1179 * we have a locked object with pages...
1180 * time to start harvesting
1184 vm_object_cache_unlock();
1186 if (object
== VM_OBJECT_NULL
) {
1191 * object is locked at this point and
1192 * has resident pages
1194 next_p
= (vm_page_t
)vm_page_queue_first(&object
->memq
);
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...
1204 if ((ep_limit
= num_to_evict
- (ep_freed
+ ep_moved
)) > EVICT_PREPARE_LIMIT
) {
1205 ep_limit
= EVICT_PREPARE_LIMIT
;
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
) {
1211 next_p
= (vm_page_t
)vm_page_queue_next(&next_p
->vmp_listq
);
1213 object
->vo_cache_pages_to_scan
--;
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
);
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
);
1226 pmap_clear_reference(VM_PAGE_GET_PHYS_PAGE(p
));
1228 ep_array
[ep_count
++] = p
;
1230 KERNEL_DEBUG(0x13001f4 | DBG_FUNC_START
, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1232 vm_page_lockspin_queues();
1234 for (ep_index
= 0; ep_index
< ep_count
; ep_index
++) {
1235 p
= ep_array
[ep_index
];
1237 if (p
->vmp_wpmapped
|| p
->vmp_dirty
|| p
->vmp_precious
) {
1238 p
->vmp_reference
= FALSE
;
1239 p
->vmp_no_cache
= FALSE
;
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
1245 vm_page_queues_remove(p
, FALSE
);
1246 vm_page_enqueue_inactive(p
, TRUE
);
1250 #if CONFIG_PHANTOM_CACHE
1251 vm_phantom_cache_add_ghost(p
);
1253 vm_page_free_prepare_queues(p
);
1255 assert(p
->vmp_pageq
.next
== 0 && p
->vmp_pageq
.prev
== 0);
1257 * Add this page to our list of reclaimed pages,
1258 * to be freed later.
1260 p
->vmp_snext
= local_free_q
;
1266 vm_page_unlock_queues();
1268 KERNEL_DEBUG(0x13001f4 | DBG_FUNC_END
, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1271 vm_page_free_list(local_free_q
, TRUE
);
1272 local_free_q
= VM_PAGE_NULL
;
1274 if (object
->vo_cache_pages_to_scan
== 0) {
1275 KERNEL_DEBUG(0x1300208, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1277 vm_object_cache_remove(object
);
1279 KERNEL_DEBUG(0x13001fc, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1282 * done with this object
1284 vm_object_unlock(object
);
1285 object
= VM_OBJECT_NULL
;
1288 * at this point, we are not holding any locks
1290 if ((ep_freed
+ ep_moved
) >= num_to_evict
) {
1292 * we've reached our target for the
1293 * number of pages to evict
1297 vm_object_cache_lock_spin();
1300 * put the page queues lock back to the caller's
1303 vm_page_lock_queues();
1305 vm_object_cache_pages_freed
+= ep_freed
;
1306 vm_object_cache_pages_moved
+= ep_moved
;
1307 vm_object_cache_pages_skipped
+= ep_skipped
;
1309 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END
, ep_freed
, 0, 0, 0, 0);
1314 * Routine: vm_object_terminate
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.
1321 * The shadow object reference is left alone.
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.
1330 static kern_return_t
1331 vm_object_terminate(
1334 vm_object_t shadow_object
;
1336 XPR(XPR_VM_OBJECT
, "vm_object_terminate, object 0x%X ref %d\n",
1337 object
, object
->ref_count
, 0, 0, 0);
1339 vm_object_lock_assert_exclusive(object
);
1341 if (!object
->pageout
&& (!object
->internal
&& object
->can_persist
) &&
1342 (object
->pager
!= NULL
|| object
->shadow_severed
)) {
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.
1348 object
->pager_trusted
= FALSE
;
1350 vm_object_reap_pages(object
, REAP_TERMINATE
);
1353 * Make sure the object isn't already being terminated
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
;
1364 * Did somebody get a reference to the object while we were
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
;
1377 * Make sure no one can look us up now.
1380 object
->terminating
= TRUE
;
1381 object
->alive
= FALSE
;
1383 if (!object
->internal
&&
1384 object
->cached_list
.next
&&
1385 object
->cached_list
.prev
) {
1386 vm_object_cache_remove(object
);
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
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
;
1400 vm_object_unlock(shadow_object
);
1403 if (object
->paging_in_progress
!= 0 ||
1404 object
->activity_in_progress
!= 0) {
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
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.
1423 * No new paging_in_progress should appear now that the
1424 * VM object is "terminating" and not "alive".
1426 vm_object_reap_async(object
);
1427 vm_object_unlock(object
);
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.
1435 return KERN_FAILURE
;
1438 * complete the VM object termination
1440 vm_object_reap(object
);
1441 object
= VM_OBJECT_NULL
;
1444 * the object lock was released by vm_object_reap()
1446 * KERN_SUCCESS means that this object has been terminated
1447 * and no longer needs its shadow object but still holds a
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.
1453 return KERN_SUCCESS
;
1460 * Complete the termination of a VM object after it's been marked
1461 * as "terminating" and "!alive" by vm_object_terminate().
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.
1471 memory_object_t pager
;
1473 vm_object_lock_assert_exclusive(object
);
1474 assert(object
->paging_in_progress
== 0);
1475 assert(object
->activity_in_progress
== 0);
1477 vm_object_reap_count
++;
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.
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
);
1496 pager
= object
->pager
;
1497 object
->pager
= MEMORY_OBJECT_NULL
;
1499 if (pager
!= MEMORY_OBJECT_NULL
) {
1500 memory_object_control_disable(object
->pager_control
);
1503 object
->ref_count
--;
1505 assert(object
->res_count
== 0);
1506 #endif /* TASK_SWAPPER */
1508 assert(object
->ref_count
== 0);
1511 * remove from purgeable queue if it's on
1513 if (object
->internal
) {
1514 assert(VM_OBJECT_OWNER(object
) == TASK_NULL
);
1516 VM_OBJECT_UNWIRED(object
);
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
;
1523 queue
= vm_purgeable_object_remove(object
);
1526 if (object
->purgeable_when_ripe
) {
1528 * Must take page lock for this -
1529 * using it to protect token queue
1531 vm_page_lock_queues();
1532 vm_purgeable_token_delete_first(queue
);
1534 assert(queue
->debug_count_objects
>= 0);
1535 vm_page_unlock_queues();
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.
1545 assert(object
->resident_page_count
>=
1546 object
->wired_page_count
);
1547 delta
= (object
->resident_page_count
-
1548 object
->wired_page_count
);
1550 assert(vm_page_purgeable_count
>= delta
);
1552 (SInt32
*)&vm_page_purgeable_count
);
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
);
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
);
1566 panic("object %p in unexpected purgeable state 0x%x\n",
1567 object
, object
->purgable
);
1569 if (object
->transposed
&&
1570 object
->cached_list
.next
!= NULL
&&
1571 object
->cached_list
.prev
== NULL
) {
1573 * object->cached_list.next "points" to the
1574 * object that was transposed with this object.
1577 assert(object
->cached_list
.next
== NULL
);
1579 assert(object
->cached_list
.prev
== NULL
);
1582 if (object
->pageout
) {
1584 * free all remaining pages tabled on
1586 * clean up it's shadow
1588 assert(object
->shadow
!= VM_OBJECT_NULL
);
1590 vm_pageout_object_terminate(object
);
1591 } else if (object
->resident_page_count
) {
1593 * free all remaining pages tabled on
1596 vm_object_reap_pages(object
, REAP_REAP
);
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);
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.
1608 if (pager
!= MEMORY_OBJECT_NULL
) {
1609 vm_object_unlock(object
);
1610 vm_object_release_pager(pager
);
1611 vm_object_lock(object
);
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
);
1620 object
->shadow
= VM_OBJECT_NULL
;
1622 #if VM_OBJECT_TRACKING
1623 if (vm_object_tracking_inited
) {
1624 btlog_remove_entries_for_element(vm_object_tracking_btlog
,
1627 #endif /* VM_OBJECT_TRACKING */
1629 vm_object_lock_destroy(object
);
1631 * Free the space for the object.
1633 zfree(vm_object_zone
, object
);
1634 object
= VM_OBJECT_NULL
;
1638 unsigned int vm_max_batch
= 256;
1640 #define V_O_R_MAX_BATCH 128
1642 #define BATCH_LIMIT(max) (vm_max_batch >= max ? max : vm_max_batch)
1645 #define VM_OBJ_REAP_FREELIST(_local_free_q, do_disconnect) \
1647 if (_local_free_q) { \
1648 if (do_disconnect) { \
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)); \
1658 vm_page_free_list(_local_free_q, TRUE); \
1659 _local_free_q = VM_PAGE_NULL; \
1665 vm_object_reap_pages(
1671 vm_page_t local_free_q
= VM_PAGE_NULL
;
1673 boolean_t disconnect_on_release
;
1674 pmap_flush_context pmap_flush_context_storage
;
1676 if (reap_type
== REAP_DATA_FLUSH
) {
1678 * We need to disconnect pages from all pmaps before
1679 * releasing them to the free list
1681 disconnect_on_release
= TRUE
;
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
1690 disconnect_on_release
= FALSE
;
1693 restart_after_sleep
:
1694 if (vm_page_queue_empty(&object
->memq
)) {
1697 loop_count
= BATCH_LIMIT(V_O_R_MAX_BATCH
);
1699 if (reap_type
== REAP_PURGEABLE
) {
1700 pmap_flush_context_init(&pmap_flush_context_storage
);
1703 vm_page_lockspin_queues();
1705 next
= (vm_page_t
)vm_page_queue_first(&object
->memq
);
1707 while (!vm_page_queue_end(&object
->memq
, (vm_page_queue_entry_t
)next
)) {
1709 next
= (vm_page_t
)vm_page_queue_next(&next
->vmp_listq
);
1711 if (--loop_count
== 0) {
1712 vm_page_unlock_queues();
1715 if (reap_type
== REAP_PURGEABLE
) {
1716 pmap_flush(&pmap_flush_context_storage
);
1717 pmap_flush_context_init(&pmap_flush_context_storage
);
1720 * Free the pages we reclaimed so far
1721 * and take a little break to avoid
1722 * hogging the page queue lock too long
1724 VM_OBJ_REAP_FREELIST(local_free_q
,
1725 disconnect_on_release
);
1730 loop_count
= BATCH_LIMIT(V_O_R_MAX_BATCH
);
1732 vm_page_lockspin_queues();
1734 if (reap_type
== REAP_DATA_FLUSH
|| reap_type
== REAP_TERMINATE
) {
1735 if (p
->vmp_busy
|| p
->vmp_cleaning
) {
1736 vm_page_unlock_queues();
1738 * free the pages reclaimed so far
1740 VM_OBJ_REAP_FREELIST(local_free_q
,
1741 disconnect_on_release
);
1743 PAGE_SLEEP(object
, p
, THREAD_UNINT
);
1745 goto restart_after_sleep
;
1747 if (p
->vmp_laundry
) {
1748 vm_pageout_steal_laundry(p
, TRUE
);
1751 switch (reap_type
) {
1752 case REAP_DATA_FLUSH
:
1753 if (VM_PAGE_WIRED(p
)) {
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
1764 case REAP_PURGEABLE
:
1765 if (VM_PAGE_WIRED(p
)) {
1767 * can't purge a wired page
1769 vm_page_purged_wired
++;
1772 if (p
->vmp_laundry
&& !p
->vmp_busy
&& !p
->vmp_cleaning
) {
1773 vm_pageout_steal_laundry(p
, TRUE
);
1776 if (p
->vmp_cleaning
|| p
->vmp_laundry
|| p
->vmp_absent
) {
1778 * page is being acted upon,
1779 * so don't mess with it
1781 vm_page_purged_others
++;
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.
1791 if (VM_PAGE_PAGEABLE(p
)) {
1792 vm_page_deactivate(p
);
1794 vm_page_purged_busy
++;
1798 assert(VM_PAGE_OBJECT(p
) != kernel_object
);
1801 * we can discard this page...
1803 if (p
->vmp_pmapped
== TRUE
) {
1807 pmap_disconnect_options(VM_PAGE_GET_PHYS_PAGE(p
), PMAP_OPTIONS_NOFLUSH
| PMAP_OPTIONS_NOREFMOD
, (void *)&pmap_flush_context_storage
);
1809 vm_page_purged_count
++;
1813 case REAP_TERMINATE
:
1814 if (p
->vmp_absent
|| p
->vmp_private
) {
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.
1824 if (p
->vmp_fictitious
) {
1825 assert(VM_PAGE_GET_PHYS_PAGE(p
) == vm_page_guard_addr
);
1828 if (!p
->vmp_dirty
&& p
->vmp_wpmapped
) {
1829 p
->vmp_dirty
= pmap_is_modified(VM_PAGE_GET_PHYS_PAGE(p
));
1832 if ((p
->vmp_dirty
|| p
->vmp_precious
) && !p
->vmp_error
&& object
->alive
) {
1833 assert(!object
->internal
);
1835 p
->vmp_free_when_done
= TRUE
;
1837 if (!p
->vmp_laundry
) {
1838 vm_page_queues_remove(p
, TRUE
);
1840 * flush page... page will be freed
1841 * upon completion of I/O
1843 vm_pageout_cluster(p
);
1845 vm_page_unlock_queues();
1847 * free the pages reclaimed so far
1849 VM_OBJ_REAP_FREELIST(local_free_q
,
1850 disconnect_on_release
);
1852 vm_object_paging_wait(object
, THREAD_UNINT
);
1854 goto restart_after_sleep
;
1861 vm_page_free_prepare_queues(p
);
1862 assert(p
->vmp_pageq
.next
== 0 && p
->vmp_pageq
.prev
== 0);
1864 * Add this page to our list of reclaimed pages,
1865 * to be freed later.
1867 p
->vmp_snext
= local_free_q
;
1870 vm_page_unlock_queues();
1873 * Free the remaining reclaimed pages
1875 if (reap_type
== REAP_PURGEABLE
) {
1876 pmap_flush(&pmap_flush_context_storage
);
1879 VM_OBJ_REAP_FREELIST(local_free_q
,
1880 disconnect_on_release
);
1885 vm_object_reap_async(
1888 vm_object_lock_assert_exclusive(object
);
1890 vm_object_reaper_lock_spin();
1892 vm_object_reap_count_async
++;
1894 /* enqueue the VM object... */
1895 queue_enter(&vm_object_reaper_queue
, object
,
1896 vm_object_t
, cached_list
);
1898 vm_object_reaper_unlock();
1900 /* ... and wake up the reaper thread */
1901 thread_wakeup((event_t
) &vm_object_reaper_queue
);
1906 vm_object_reaper_thread(void)
1908 vm_object_t object
, shadow_object
;
1910 vm_object_reaper_lock_spin();
1912 while (!queue_empty(&vm_object_reaper_queue
)) {
1913 queue_remove_first(&vm_object_reaper_queue
,
1918 vm_object_reaper_unlock();
1919 vm_object_lock(object
);
1921 assert(object
->terminating
);
1922 assert(!object
->alive
);
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
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
,
1937 vm_object_lock(object
);
1941 object
->pageout
? VM_OBJECT_NULL
: object
->shadow
;
1943 vm_object_reap(object
);
1944 /* cache is unlocked and object is no longer valid */
1945 object
= VM_OBJECT_NULL
;
1947 if (shadow_object
!= VM_OBJECT_NULL
) {
1949 * Drop the reference "object" was holding on
1950 * its shadow object.
1952 vm_object_deallocate(shadow_object
);
1953 shadow_object
= VM_OBJECT_NULL
;
1955 vm_object_reaper_lock_spin();
1958 /* wait for more work... */
1959 assert_wait((event_t
) &vm_object_reaper_queue
, THREAD_UNINT
);
1961 vm_object_reaper_unlock();
1963 thread_block((thread_continue_t
) vm_object_reaper_thread
);
1968 * Routine: vm_object_release_pager
1969 * Purpose: Terminate the pager and, upon completion,
1970 * release our last reference to it.
1973 vm_object_release_pager(
1974 memory_object_t pager
)
1977 * Terminate the pager.
1980 (void) memory_object_terminate(pager
);
1983 * Release reference to pager.
1985 memory_object_deallocate(pager
);
1989 * Routine: vm_object_destroy
1991 * Shut down a VM object, despite the
1992 * presence of address map (or other) references
1998 __unused kern_return_t reason
)
2000 memory_object_t old_pager
;
2002 if (object
== VM_OBJECT_NULL
) {
2003 return KERN_SUCCESS
;
2007 * Remove the pager association immediately.
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.]
2015 vm_object_lock(object
);
2016 object
->can_persist
= FALSE
;
2017 object
->named
= FALSE
;
2018 object
->alive
= FALSE
;
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
);
2027 * Wait for the existing paging activity (that got
2028 * through before we nulled out the pager) to subside.
2031 vm_object_paging_wait(object
, THREAD_UNINT
);
2032 vm_object_unlock(object
);
2035 * Terminate the object now.
2037 if (old_pager
!= MEMORY_OBJECT_NULL
) {
2038 vm_object_release_pager(old_pager
);
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
2047 vm_object_deallocate(object
);
2049 return KERN_SUCCESS
;
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.
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
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.
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. */
2078 #define CHUNK_SIZE (PAGES_IN_A_CHUNK * PAGE_SIZE_64) /* Size of a chunk in bytes */
2080 typedef uint64_t chunk_state_t
;
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
2090 #define CHUNK_INIT(c, len) \
2094 (c) = 0xffffffffffffffffLL; \
2096 for (p = (len) / PAGE_SIZE_64; p < PAGES_IN_A_CHUNK; p++) \
2097 MARK_PAGE_HANDLED(c, p); \
2102 * Return true if all pages in the chunk have not yet been processed.
2105 #define CHUNK_NOT_COMPLETE(c) ((c) != 0)
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.
2112 #define PAGE_ALREADY_HANDLED(c, p) (((c) & (1LL << (p))) == 0)
2115 * Mark the page at offset 'p' in the bit map as having been processed.
2118 #define MARK_PAGE_HANDLED(c, p) \
2120 (c) = (c) & ~(1LL << (p)); \
2125 * Return true if the page at the given offset has been paged out. Object is
2126 * locked upon entry and returned locked.
2132 vm_object_offset_t offset
)
2134 if (object
->internal
&&
2136 !object
->terminating
&&
2137 object
->pager_ready
) {
2138 if (VM_COMPRESSOR_PAGER_STATE_GET(object
, offset
)
2139 == VM_EXTERNAL_STATE_EXISTS
) {
2149 * madvise_free_debug
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.
2156 #if DEVELOPMENT || DEBUG
2157 int madvise_free_debug
= 1;
2159 int madvise_free_debug
= 0;
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.
2169 deactivate_pages_in_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
,
2179 vm_map_offset_t pmap_offset
)
2183 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
2184 struct vm_page_delayed_work
*dwp
;
2187 unsigned int reusable
= 0;
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.
2198 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
2200 for (p
= 0; size
&& CHUNK_NOT_COMPLETE(*chunk_state
); p
++, size
-= PAGE_SIZE_64
, offset
+= PAGE_SIZE_64
, pmap_offset
+= PAGE_SIZE_64
) {
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.
2206 if (PAGE_ALREADY_HANDLED(*chunk_state
, p
)) {
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.
2215 if ((m
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
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.
2222 MARK_PAGE_HANDLED(*chunk_state
, p
);
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
)) {
2232 clear_refmod
= VM_MEM_REFERENCED
;
2233 dwp
->dw_mask
|= DW_clear_reference
;
2235 if ((kill_page
) && (object
->internal
)) {
2236 if (madvise_free_debug
) {
2238 * zero-fill the page now
2239 * to simulate it being
2240 * reclaimed and re-faulted.
2242 pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(m
));
2244 m
->vmp_precious
= FALSE
;
2245 m
->vmp_dirty
= FALSE
;
2247 clear_refmod
|= VM_MEM_MODIFIED
;
2248 if (m
->vmp_q_state
== VM_PAGE_ON_THROTTLED_Q
) {
2250 * This page is now clean and
2251 * reclaimable. Move it out
2252 * of the throttled queue, so
2253 * that vm_pageout_scan() can
2256 dwp
->dw_mask
|= DW_move_page
;
2259 VM_COMPRESSOR_PAGER_STATE_CLR(object
, offset
);
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
);
2269 * Tell pmap this page is now
2270 * "reusable" (to update pmap
2271 * stats for all mappings).
2273 pmap_options
|= PMAP_OPTIONS_SET_REUSABLE
;
2276 pmap_options
|= PMAP_OPTIONS_NOFLUSH
;
2277 pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE(m
),
2282 if ((m
->vmp_q_state
!= VM_PAGE_ON_THROTTLED_Q
) && !(reusable_page
|| all_reusable
)) {
2283 dwp
->dw_mask
|= DW_move_page
;
2287 VM_PAGE_ADD_DELAYED_WORK(dwp
, m
,
2291 if (dw_count
>= dw_limit
) {
2293 OSAddAtomic(reusable
,
2294 &vm_page_stats_reusable
.reusable_count
);
2295 vm_page_stats_reusable
.reusable
+= reusable
;
2298 vm_page_do_delayed_work(object
, VM_KERN_MEMORY_NONE
, &dw_array
[0], dw_count
);
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.
2311 if (page_is_paged_out(object
, offset
)) {
2312 MARK_PAGE_HANDLED(*chunk_state
, p
);
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.
2319 if ((kill_page
) && (object
->internal
)) {
2320 VM_COMPRESSOR_PAGER_STATE_CLR(object
, offset
);
2322 if (pmap
!= PMAP_NULL
) {
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.
2330 pmap_remove_options(
2335 PMAP_OPTIONS_REMOVE
);
2343 OSAddAtomic(reusable
, &vm_page_stats_reusable
.reusable_count
);
2344 vm_page_stats_reusable
.reusable
+= reusable
;
2349 vm_page_do_delayed_work(object
, VM_KERN_MEMORY_NONE
, &dw_array
[0], dw_count
);
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.
2363 static vm_object_size_t
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
,
2373 vm_map_offset_t pmap_offset
)
2376 vm_object_t tmp_object
;
2377 vm_object_size_t length
;
2378 chunk_state_t chunk_state
;
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.
2386 length
= MIN(size
, CHUNK_SIZE
);
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.
2394 CHUNK_INIT(chunk_state
, length
);
2395 object
= orig_object
;
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.
2403 while (object
&& CHUNK_NOT_COMPLETE(chunk_state
)) {
2404 vm_object_paging_begin(object
);
2406 deactivate_pages_in_object(object
, offset
, length
, kill_page
, reusable_page
, all_reusable
, &chunk_state
, pfc
, pmap
, pmap_offset
);
2408 vm_object_paging_end(object
);
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.
2416 tmp_object
= object
->shadow
;
2420 reusable_page
= FALSE
;
2421 all_reusable
= FALSE
;
2422 offset
+= object
->vo_shadow_offset
;
2423 vm_object_lock(tmp_object
);
2426 if (object
!= orig_object
) {
2427 vm_object_unlock(object
);
2430 object
= tmp_object
;
2433 if (object
&& object
!= orig_object
) {
2434 vm_object_unlock(object
);
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
2448 __private_extern__
void
2449 vm_object_deactivate_pages(
2451 vm_object_offset_t offset
,
2452 vm_object_size_t size
,
2453 boolean_t kill_page
,
2454 boolean_t reusable_page
,
2456 vm_map_offset_t pmap_offset
)
2458 vm_object_size_t length
;
2459 boolean_t all_reusable
;
2460 pmap_flush_context pmap_flush_context_storage
;
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.
2470 all_reusable
= FALSE
;
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.
2478 if (reusable_page
&&
2480 object
->vo_size
!= 0 &&
2481 object
->vo_size
== size
&&
2482 object
->reusable_page_count
== 0) {
2483 all_reusable
= TRUE
;
2484 reusable_page
= FALSE
;
2488 if ((reusable_page
|| all_reusable
) && object
->all_reusable
) {
2489 /* This means MADV_FREE_REUSABLE has been called twice, which
2490 * is probably illegal. */
2494 pmap_flush_context_init(&pmap_flush_context_storage
);
2497 length
= deactivate_a_chunk(object
, offset
, size
, kill_page
, reusable_page
, all_reusable
, &pmap_flush_context_storage
, pmap
, pmap_offset
);
2501 pmap_offset
+= length
;
2503 pmap_flush(&pmap_flush_context_storage
);
2506 if (!object
->all_reusable
) {
2507 unsigned int reusable
;
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
++;
2518 } else if (reusable_page
) {
2519 vm_page_stats_reusable
.partial_reusable_calls
++;
2524 vm_object_reuse_pages(
2526 vm_object_offset_t start_offset
,
2527 vm_object_offset_t end_offset
,
2528 boolean_t allow_partial_reuse
)
2530 vm_object_offset_t cur_offset
;
2532 unsigned int reused
, reusable
;
2534 #define VM_OBJECT_REUSE_PAGE(object, m, reused) \
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; \
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 \
2550 pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE((m)), \
2552 (PMAP_OPTIONS_CLEAR_REUSABLE \
2553 | PMAP_OPTIONS_NOFLUSH), \
2561 vm_object_lock_assert_exclusive(object
);
2563 if (object
->all_reusable
) {
2564 panic("object %p all_reusable: can't update pmap stats\n",
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
;
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
);
2582 assert(!m
->vmp_reusable
);
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) {
2596 m
= vm_page_lookup(object
, cur_offset
);
2597 VM_OBJECT_REUSE_PAGE(object
, m
, reused
);
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) {
2605 if (m
->vmp_offset
< start_offset
||
2606 m
->vmp_offset
>= end_offset
) {
2609 VM_OBJECT_REUSE_PAGE(object
, m
, reused
);
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
;
2620 * Routine: vm_object_pmap_protect
2623 * Reduces the permission for all physical
2624 * pages in the specified object range.
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.
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.
2635 * The object must *not* be locked. The object must
2638 * If pmap is not NULL, this routine assumes that
2639 * the only mappings for the pages are in that
2643 __private_extern__
void
2644 vm_object_pmap_protect(
2646 vm_object_offset_t offset
,
2647 vm_object_size_t size
,
2649 vm_map_offset_t pmap_start
,
2652 vm_object_pmap_protect_options(object
, offset
, size
,
2653 pmap
, pmap_start
, prot
, 0);
2656 __private_extern__
void
2657 vm_object_pmap_protect_options(
2659 vm_object_offset_t offset
,
2660 vm_object_size_t size
,
2662 vm_map_offset_t pmap_start
,
2666 pmap_flush_context pmap_flush_context_storage
;
2667 boolean_t delayed_pmap_flush
= FALSE
;
2669 if (object
== VM_OBJECT_NULL
) {
2672 size
= vm_object_round_page(size
);
2673 offset
= vm_object_trunc_page(offset
);
2675 vm_object_lock(object
);
2677 if (object
->phys_contiguous
) {
2679 vm_object_unlock(object
);
2680 pmap_protect_options(pmap
,
2684 options
& ~PMAP_OPTIONS_NOFLUSH
,
2687 vm_object_offset_t phys_start
, phys_end
, phys_addr
;
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
);
2695 pmap_flush_context_init(&pmap_flush_context_storage
);
2696 delayed_pmap_flush
= FALSE
;
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
),
2704 options
| PMAP_OPTIONS_NOFLUSH
,
2705 (void *)&pmap_flush_context_storage
);
2706 delayed_pmap_flush
= TRUE
;
2708 if (delayed_pmap_flush
== TRUE
) {
2709 pmap_flush(&pmap_flush_context_storage
);
2715 assert(object
->internal
);
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
);
2725 pmap_flush_context_init(&pmap_flush_context_storage
);
2726 delayed_pmap_flush
= FALSE
;
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
2733 if (ptoa_64(object
->resident_page_count
/ 4) < size
) {
2735 vm_object_offset_t end
;
2737 end
= offset
+ size
;
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
;
2743 start
= pmap_start
+ p
->vmp_offset
- offset
;
2745 if (pmap
!= PMAP_NULL
) {
2746 pmap_protect_options(
2749 start
+ PAGE_SIZE_64
,
2751 options
| PMAP_OPTIONS_NOFLUSH
,
2752 &pmap_flush_context_storage
);
2754 pmap_page_protect_options(
2755 VM_PAGE_GET_PHYS_PAGE(p
),
2757 options
| PMAP_OPTIONS_NOFLUSH
,
2758 &pmap_flush_context_storage
);
2760 delayed_pmap_flush
= TRUE
;
2765 vm_object_offset_t end
;
2766 vm_object_offset_t target_off
;
2768 end
= offset
+ size
;
2770 for (target_off
= offset
;
2771 target_off
< end
; target_off
+= PAGE_SIZE
) {
2772 p
= vm_page_lookup(object
, target_off
);
2774 if (p
!= VM_PAGE_NULL
) {
2775 vm_object_offset_t start
;
2777 start
= pmap_start
+ (p
->vmp_offset
- offset
);
2779 if (pmap
!= PMAP_NULL
) {
2780 pmap_protect_options(
2783 start
+ PAGE_SIZE_64
,
2785 options
| PMAP_OPTIONS_NOFLUSH
,
2786 &pmap_flush_context_storage
);
2788 pmap_page_protect_options(
2789 VM_PAGE_GET_PHYS_PAGE(p
),
2791 options
| PMAP_OPTIONS_NOFLUSH
,
2792 &pmap_flush_context_storage
);
2794 delayed_pmap_flush
= TRUE
;
2798 if (delayed_pmap_flush
== TRUE
) {
2799 pmap_flush(&pmap_flush_context_storage
);
2802 if (prot
== VM_PROT_NONE
) {
2804 * Must follow shadow chain to remove access
2805 * to pages in shadowed objects.
2807 vm_object_t next_object
;
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
;
2817 * End of chain - we are done.
2823 * Pages in shadowed objects may never have
2824 * write permission - we may stop here.
2830 vm_object_unlock(object
);
2833 uint32_t vm_page_busy_absent_skipped
= 0;
2836 * Routine: vm_object_copy_slowly
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.
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*.
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.
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
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 */
2873 vm_object_t new_object
;
2874 vm_object_offset_t new_offset
;
2876 struct vm_object_fault_info fault_info
= {};
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);
2882 vm_object_unlock(src_object
);
2883 *_result_object
= VM_OBJECT_NULL
;
2884 return KERN_INVALID_ARGUMENT
;
2888 * Prevent destruction of the source object while we copy.
2891 vm_object_reference_locked(src_object
);
2892 vm_object_unlock(src_object
);
2895 * Create a new object to hold the copied pages.
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.
2903 new_object
= vm_object_allocate(size
);
2906 assert(size
== trunc_page_64(size
)); /* Will the loop terminate? */
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
;
2916 src_offset
+= PAGE_SIZE_64
,
2917 new_offset
+= PAGE_SIZE_64
, size
-= PAGE_SIZE_64
2920 vm_fault_return_t result
;
2922 vm_object_lock(new_object
);
2924 while ((new_page
= vm_page_alloc(new_object
, new_offset
))
2926 vm_object_unlock(new_object
);
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
;
2934 vm_object_lock(new_object
);
2936 vm_object_unlock(new_object
);
2939 vm_prot_t prot
= VM_PROT_READ
;
2940 vm_page_t _result_page
;
2942 vm_page_t result_page
;
2943 kern_return_t error_code
;
2944 vm_object_t result_page_object
;
2947 vm_object_lock(src_object
);
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
,
2954 VM_EXTERNAL_STATE_ABSENT
))) {
2955 boolean_t can_skip_page
;
2957 _result_page
= vm_page_lookup(src_object
,
2959 if (_result_page
== VM_PAGE_NULL
) {
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
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
) {
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
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
++;
2997 can_skip_page
= FALSE
;
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
;
3012 vm_object_paging_begin(src_object
);
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
;
3019 fault_info
.cluster_size
= cluster_size
;
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
,
3028 &error_code
, FALSE
, FALSE
, &fault_info
);
3031 case VM_FAULT_SUCCESS
:
3032 result_page
= _result_page
;
3033 result_page_object
= VM_PAGE_OBJECT(result_page
);
3036 * Copy the page to the new object.
3039 * If result_page is clean,
3040 * we could steal it instead
3044 vm_page_copy(result_page
, new_page
);
3045 vm_object_unlock(result_page_object
);
3048 * Let go of both pages (make them
3049 * not busy, perform wakeup, activate).
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
);
3056 vm_object_lock(result_page_object
);
3057 PAGE_WAKEUP_DONE(result_page
);
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
);
3064 vm_page_activate(new_page
);
3065 vm_page_unlock_queues();
3068 * Release paging references and
3069 * top-level placeholder page, if any.
3072 vm_fault_cleanup(result_page_object
,
3077 case VM_FAULT_RETRY
:
3080 case VM_FAULT_MEMORY_SHORTAGE
:
3081 if (vm_page_wait(interruptible
)) {
3086 case VM_FAULT_INTERRUPTED
:
3087 vm_object_lock(new_object
);
3088 VM_PAGE_FREE(new_page
);
3089 vm_object_unlock(new_object
);
3091 vm_object_deallocate(new_object
);
3092 vm_object_deallocate(src_object
);
3093 *_result_object
= VM_OBJECT_NULL
;
3094 return MACH_SEND_INTERRUPTED
;
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
);
3101 case VM_FAULT_MEMORY_ERROR
:
3104 * (a) ignore pages that we can't
3106 * (b) return the null object if
3107 * any page fails [chosen]
3110 vm_object_lock(new_object
);
3111 VM_PAGE_FREE(new_page
);
3112 vm_object_unlock(new_object
);
3114 vm_object_deallocate(new_object
);
3115 vm_object_deallocate(src_object
);
3116 *_result_object
= VM_OBJECT_NULL
;
3117 return error_code
? error_code
:
3121 panic("vm_object_copy_slowly: unexpected error"
3122 " 0x%x from vm_fault_page()\n", result
);
3124 } while (result
!= VM_FAULT_SUCCESS
);
3128 * Lose the extra reference, and return our object.
3130 vm_object_deallocate(src_object
);
3131 *_result_object
= new_object
;
3132 return KERN_SUCCESS
;
3136 * Routine: vm_object_copy_quickly
3139 * Copy the specified range of the source virtual
3140 * memory object, if it can be done without waiting
3141 * for user-generated events.
3144 * If the copy is successful, the copy is returned in
3145 * the arguments; otherwise, the arguments are not
3148 * In/out conditions:
3149 * The object should be unlocked on entry and exit.
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 */
3161 vm_object_t object
= *_object
;
3162 memory_object_copy_strategy_t copy_strategy
;
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
;
3172 vm_object_lock(object
);
3174 copy_strategy
= object
->copy_strategy
;
3176 switch (copy_strategy
) {
3177 case MEMORY_OBJECT_COPY_SYMMETRIC
:
3180 * Symmetric copy strategy.
3181 * Make another reference to the object.
3182 * Leave object/offset unchanged.
3185 vm_object_reference_locked(object
);
3186 object
->shadowed
= TRUE
;
3187 vm_object_unlock(object
);
3190 * Both source and destination must make
3191 * shadows, and the source must be made
3192 * read-only if not already.
3195 *_src_needs_copy
= TRUE
;
3196 *_dst_needs_copy
= TRUE
;
3200 case MEMORY_OBJECT_COPY_DELAY
:
3201 vm_object_unlock(object
);
3205 vm_object_unlock(object
);
3211 static int copy_call_count
= 0;
3212 static int copy_call_sleep_count
= 0;
3213 static int copy_call_restart_count
= 0;
3216 * Routine: vm_object_copy_call [internal]
3219 * Copy the source object (src_object), using the
3220 * user-managed copy algorithm.
3222 * In/out conditions:
3223 * The source object must be locked on entry. It
3224 * will be *unlocked* on exit.
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
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 */
3242 boolean_t check_ready
= FALSE
;
3243 uint32_t try_failed_count
= 0;
3246 * If a copy is already in progress, wait and retry.
3249 * Consider making this call interruptable, as Mike
3250 * intended it to be.
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.
3259 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
3260 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
3262 copy_call_restart_count
++;
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.)
3272 vm_object_set_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
);
3273 vm_object_unlock(src_object
);
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.
3284 kr
= KERN_FAILURE
; /* XXX need to change memory_object.defs */
3285 if (kr
!= KERN_SUCCESS
) {
3290 * Wait for the copy to arrive.
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
,
3296 copy_call_sleep_count
++;
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
);
3305 mutex_pause(try_failed_count
); /* wait a bit */
3307 vm_object_lock(src_object
);
3310 if (copy
->vo_size
< src_offset
+ size
) {
3311 copy
->vo_size
= src_offset
+ size
;
3314 if (!copy
->pager_ready
) {
3321 *_result_object
= copy
;
3322 vm_object_unlock(copy
);
3323 vm_object_unlock(src_object
);
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
);
3331 vm_object_unlock(copy
);
3334 return KERN_SUCCESS
;
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;
3343 * Routine: vm_object_copy_delayed [internal]
3346 * Copy the specified virtual memory object, using
3347 * the asymmetric copy-on-write algorithm.
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.
3353 * This routine will not block waiting for user-generated
3354 * events. It is not interruptible.
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
)
3363 vm_object_t new_copy
= VM_OBJECT_NULL
;
3364 vm_object_t old_copy
;
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
;
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
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
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.
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
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.
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.
3411 copy_size
= vm_object_round_page(copy_size
);
3415 * Wait for paging in progress.
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
;
3426 vm_object_paging_wait(src_object
, THREAD_UNINT
);
3429 * See whether we can reuse the result of a previous
3433 old_copy
= src_object
->copy
;
3434 if (old_copy
!= VM_OBJECT_NULL
) {
3438 * Try to get the locks (out of order)
3440 if (src_object_shared
== TRUE
) {
3441 lock_granted
= vm_object_lock_try_shared(old_copy
);
3443 lock_granted
= vm_object_lock_try(old_copy
);
3446 if (!lock_granted
) {
3447 vm_object_unlock(src_object
);
3449 if (collisions
++ == 0) {
3450 copy_delayed_lock_contention
++;
3452 mutex_pause(collisions
);
3454 /* Heisenberg Rules */
3455 copy_delayed_lock_collisions
++;
3457 if (collisions
> copy_delayed_max_collisions
) {
3458 copy_delayed_max_collisions
= collisions
;
3461 if (src_object_shared
== TRUE
) {
3462 vm_object_lock_shared(src_object
);
3464 vm_object_lock(src_object
);
3471 * Determine whether the old copy object has
3475 if (old_copy
->resident_page_count
== 0 &&
3476 !old_copy
->pager_created
) {
3478 * It has not been modified.
3480 * Return another reference to
3481 * the existing copy-object if
3482 * we can safely grow it (if
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
);
3491 vm_object_lock(src_object
);
3492 src_object_shared
= FALSE
;
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.
3502 copy_delayed_protect_iterate
++;
3504 pmap_flush_context_init(&pmap_flush_context_storage
);
3505 delayed_pmap_flush
= FALSE
;
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
);
3515 if (new_copy
!= VM_OBJECT_NULL
) {
3516 vm_object_unlock(new_copy
);
3517 vm_object_deallocate(new_copy
);
3519 if (delayed_pmap_flush
== TRUE
) {
3520 pmap_flush(&pmap_flush_context_storage
);
3523 return VM_OBJECT_NULL
;
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
;
3531 if (delayed_pmap_flush
== TRUE
) {
3532 pmap_flush(&pmap_flush_context_storage
);
3535 old_copy
->vo_size
= copy_size
;
3537 if (src_object_shared
== TRUE
) {
3538 vm_object_reference_shared(old_copy
);
3540 vm_object_reference_locked(old_copy
);
3542 vm_object_unlock(old_copy
);
3543 vm_object_unlock(src_object
);
3545 if (new_copy
!= VM_OBJECT_NULL
) {
3546 vm_object_unlock(new_copy
);
3547 vm_object_deallocate(new_copy
);
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.
3559 if (old_copy
->vo_size
> copy_size
) {
3560 copy_size
= old_copy
->vo_size
;
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
);
3570 src_object_shared
= FALSE
;
3573 new_copy
->vo_size
= copy_size
;
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.
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
);
3590 src_object_shared
= FALSE
;
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.
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.
3605 copy_delayed_protect_iterate
++;
3607 pmap_flush_context_init(&pmap_flush_context_storage
);
3608 delayed_pmap_flush
= FALSE
;
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
)) {
3614 vm_object_unlock(old_copy
);
3616 vm_object_unlock(src_object
);
3617 vm_object_unlock(new_copy
);
3618 vm_object_deallocate(new_copy
);
3620 if (delayed_pmap_flush
== TRUE
) {
3621 pmap_flush(&pmap_flush_context_storage
);
3624 return VM_OBJECT_NULL
;
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
;
3632 if (delayed_pmap_flush
== TRUE
) {
3633 pmap_flush(&pmap_flush_context_storage
);
3636 if (old_copy
!= VM_OBJECT_NULL
) {
3638 * Make the old copy-object shadow the new one.
3639 * It will receive no more pages from the original
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. */
3654 if (old_copy
->res_count
) {
3655 VM_OBJ_RES_INCR(new_copy
);
3656 VM_OBJ_RES_DECR(src_object
);
3660 vm_object_unlock(old_copy
); /* done with old_copy */
3664 * Point the new copy at the existing object.
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 */
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
);
3678 "vm_object_copy_delayed: used copy object %X for source %X\n",
3679 new_copy
, src_object
, 0, 0, 0);
3685 * Routine: vm_object_copy_strategically
3688 * Perform a copy according to the source object's
3689 * declared strategy. This operation may block,
3690 * and may be interrupted.
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 */
3702 boolean_t interruptible
= THREAD_ABORTSAFE
; /* XXX */
3703 boolean_t object_lock_shared
= FALSE
;
3704 memory_object_copy_strategy_t copy_strategy
;
3706 assert(src_object
!= VM_OBJECT_NULL
);
3708 copy_strategy
= src_object
->copy_strategy
;
3710 if (copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
3711 vm_object_lock_shared(src_object
);
3712 object_lock_shared
= TRUE
;
3714 vm_object_lock(src_object
);
3718 * The copy strategy is only valid if the memory manager
3719 * is "ready". Internal objects are always ready.
3722 while (!src_object
->internal
&& !src_object
->pager_ready
) {
3723 wait_result_t wait_result
;
3725 if (object_lock_shared
== TRUE
) {
3726 vm_object_unlock(src_object
);
3727 vm_object_lock(src_object
);
3728 object_lock_shared
= FALSE
;
3731 wait_result
= vm_object_sleep( src_object
,
3732 VM_OBJECT_EVENT_PAGER_READY
,
3734 if (wait_result
!= THREAD_AWAKENED
) {
3735 vm_object_unlock(src_object
);
3736 *dst_object
= VM_OBJECT_NULL
;
3738 *dst_needs_copy
= FALSE
;
3739 return MACH_SEND_INTERRUPTED
;
3744 * Use the appropriate copy strategy.
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
;
3757 vm_object_lock(src_object
);
3758 /* fall thru when delayed copy not allowed */
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
) {
3765 *dst_needs_copy
= FALSE
;
3769 case MEMORY_OBJECT_COPY_CALL
:
3770 result
= vm_object_copy_call(src_object
, src_offset
, size
,
3772 if (result
== KERN_SUCCESS
) {
3773 *dst_offset
= src_offset
;
3774 *dst_needs_copy
= TRUE
;
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
;
3785 panic("copy_strategically: bad strategy");
3786 result
= KERN_INVALID_ARGUMENT
;
3794 * Create a new object which is backed by the
3795 * specified existing object range. The source
3796 * object reference is deallocated.
3798 * The new object and offset into that object
3799 * are returned in the source parameters.
3801 boolean_t vm_object_shadow_check
= TRUE
;
3803 __private_extern__ boolean_t
3805 vm_object_t
*object
, /* IN/OUT */
3806 vm_object_offset_t
*offset
, /* IN/OUT */
3807 vm_object_size_t length
)
3813 assert(source
!= VM_OBJECT_NULL
);
3814 if (source
== VM_OBJECT_NULL
) {
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
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.
3832 assert(source
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
);
3836 * Determine if we really need a shadow.
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
3846 assert(source
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
); /* Purgeable objects shouldn't have shadow objects. */
3848 if (vm_object_shadow_check
&&
3849 source
->vo_size
== length
&&
3850 source
->ref_count
== 1) {
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
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
);
3870 /* things changed while we were locking "source"... */
3871 vm_object_unlock(source
);
3875 * Allocate a new object with the given length
3878 if ((result
= vm_object_allocate(length
)) == VM_OBJECT_NULL
) {
3879 panic("vm_object_shadow: no object for shadowing");
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
3889 result
->shadow
= source
;
3892 * Store the offset into the source object,
3893 * and fix up the offset into the new object.
3896 result
->vo_shadow_offset
= *offset
;
3899 * Return the new things
3908 * The relationship between vm_object structures and
3909 * the memory_object requires careful synchronization.
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:
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
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.
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.
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.]
3942 * [Both the "initialized" and "ready" attribute wait conditions
3943 * use the "pager" field as the wait event.]
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
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.]
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.]
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
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.]
3984 * Routine: vm_object_memory_object_associate
3986 * Associate a VM object to the given pager.
3987 * If a VM object is not provided, create one.
3988 * Initialize the pager.
3991 vm_object_memory_object_associate(
3992 memory_object_t pager
,
3994 vm_object_size_t size
,
3997 memory_object_control_t control
;
3999 assert(pager
!= MEMORY_OBJECT_NULL
);
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
);
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
;
4016 * Allocate request port.
4019 control
= memory_object_control_allocate(object
);
4020 assert(control
!= MEMORY_OBJECT_CONTROL_NULL
);
4022 vm_object_lock(object
);
4024 assert(!object
->pager_ready
);
4025 assert(!object
->pager_initialized
);
4026 assert(object
->pager
== NULL
);
4027 assert(object
->pager_control
== NULL
);
4030 * Copy the reference we were given.
4033 memory_object_reference(pager
);
4034 object
->pager_created
= TRUE
;
4035 object
->pager
= pager
;
4036 object
->pager_control
= control
;
4037 object
->pager_ready
= FALSE
;
4039 vm_object_unlock(object
);
4042 * Let the pager know we're using it.
4045 (void) memory_object_init(pager
,
4046 object
->pager_control
,
4049 vm_object_lock(object
);
4051 object
->named
= TRUE
;
4053 if (object
->internal
) {
4054 object
->pager_ready
= TRUE
;
4055 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
4058 object
->pager_initialized
= TRUE
;
4059 vm_object_wakeup(object
, VM_OBJECT_EVENT_INITIALIZED
);
4061 vm_object_unlock(object
);
4067 * Routine: vm_object_compressor_pager_create
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.
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.
4081 vm_object_compressor_pager_create(
4084 memory_object_t pager
;
4085 vm_object_t pager_object
= VM_OBJECT_NULL
;
4087 assert(object
!= kernel_object
);
4090 * Prevent collapse or termination by holding a paging reference
4093 vm_object_paging_begin(object
);
4094 if (object
->pager_created
) {
4096 * Someone else got to it first...
4097 * wait for them to finish initializing the ports
4099 while (!object
->pager_initialized
) {
4100 vm_object_sleep(object
,
4101 VM_OBJECT_EVENT_INITIALIZED
,
4104 vm_object_paging_end(object
);
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",
4114 (uint64_t) object
->vo_size
,
4115 0x0FFFFFFFFULL
* PAGE_SIZE
);
4116 #endif /* DEVELOPMENT || DEBUG */
4117 vm_object_paging_end(object
);
4122 * Indicate that a memory object has been assigned
4123 * before dropping the lock, to prevent a race.
4126 object
->pager_created
= TRUE
;
4127 object
->paging_offset
= 0;
4129 vm_object_unlock(object
);
4132 * Create the [internal] pager, and associate it with this object.
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.
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
,
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
);
4153 * A reference was returned by
4154 * memory_object_create(), and it is
4155 * copied by vm_object_memory_object_associate().
4158 pager_object
= vm_object_memory_object_associate(pager
,
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
);
4167 * Drop the reference we were passed.
4169 memory_object_deallocate(pager
);
4171 vm_object_lock(object
);
4174 * Release the paging reference
4176 vm_object_paging_end(object
);
4180 * Global variables for vm_object_collapse():
4182 * Counts for normal collapses and bypasses.
4183 * Debugging variables, to watch or disable collapse.
4185 static long object_collapses
= 0;
4186 static long object_bypasses
= 0;
4188 static boolean_t vm_object_collapse_allowed
= TRUE
;
4189 static boolean_t vm_object_bypass_allowed
= TRUE
;
4191 void vm_object_do_collapse_compressor(vm_object_t object
,
4192 vm_object_t backing_object
);
4194 vm_object_do_collapse_compressor(
4196 vm_object_t backing_object
)
4198 vm_object_offset_t new_offset
, backing_offset
;
4199 vm_object_size_t size
;
4201 vm_counters
.do_collapse_compressor
++;
4203 vm_object_lock_assert_exclusive(object
);
4204 vm_object_lock_assert_exclusive(backing_object
);
4206 size
= object
->vo_size
;
4209 * Move all compressed pages from backing_object
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
;
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 */
4228 backing_offset
= (backing_pager_offset
-
4229 backing_object
->paging_offset
);
4231 new_offset
= backing_offset
- object
->vo_shadow_offset
;
4233 if (new_offset
>= object
->vo_size
) {
4234 /* we're out of the scope of "object": done */
4238 if ((vm_page_lookup(object
, new_offset
) != VM_PAGE_NULL
) ||
4239 (vm_compressor_pager_state_get(object
->pager
,
4241 object
->paging_offset
)) ==
4242 VM_EXTERNAL_STATE_EXISTS
)) {
4244 * This page already exists in object, resident or
4246 * We don't need this compressed page in backing_object
4247 * and it will be reclaimed when we release
4254 * backing_object has this page in the VM compressor and
4255 * we need to transfer it to object.
4257 vm_counters
.do_collapse_compressor_pages
++;
4258 vm_compressor_pager_transfer(
4261 (new_offset
+ object
->paging_offset
),
4263 backing_object
->pager
,
4264 (backing_offset
+ backing_object
->paging_offset
));
4269 * Routine: vm_object_do_collapse
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.
4275 * Both objects and the cache are locked; the page
4276 * queues are unlocked.
4280 vm_object_do_collapse(
4282 vm_object_t backing_object
)
4285 vm_object_offset_t new_offset
, backing_offset
;
4286 vm_object_size_t size
;
4288 vm_object_lock_assert_exclusive(object
);
4289 vm_object_lock_assert_exclusive(backing_object
);
4291 assert(object
->purgable
== VM_PURGABLE_DENY
);
4292 assert(backing_object
->purgable
== VM_PURGABLE_DENY
);
4294 backing_offset
= object
->vo_shadow_offset
;
4295 size
= object
->vo_size
;
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.
4304 while (!vm_page_queue_empty(&backing_object
->memq
)) {
4305 p
= (vm_page_t
) vm_page_queue_first(&backing_object
->memq
);
4307 new_offset
= (p
->vmp_offset
- backing_offset
);
4309 assert(!p
->vmp_busy
|| p
->vmp_absent
);
4312 * If the parent has a page here, or if
4313 * this page falls outside the parent,
4316 * Otherwise, move it as planned.
4319 if (p
->vmp_offset
< backing_offset
|| new_offset
>= size
) {
4322 pp
= vm_page_lookup(object
, new_offset
);
4323 if (pp
== VM_PAGE_NULL
) {
4324 if (VM_COMPRESSOR_PAGER_STATE_GET(object
,
4326 == VM_EXTERNAL_STATE_EXISTS
) {
4328 * Parent object has this page
4329 * in the VM compressor.
4330 * Throw away the backing
4336 * Parent now has no page.
4337 * Move the backing object's page
4340 vm_page_rename(p
, object
, new_offset
);
4343 assert(!pp
->vmp_absent
);
4346 * Parent object has a real page.
4347 * Throw away the backing object's
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
)));
4366 * Move the pager from backing_object to object.
4368 * XXX We're only using part of the paging space
4369 * for keeps now... we ought to discard the
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
;
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
,
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
;
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.
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;
4411 /* no shadow, therefore no shadow offset... */
4412 object
->vo_shadow_offset
= 0;
4414 assert((object
->shadow
== VM_OBJECT_NULL
) ||
4415 (object
->shadow
->copy
!= backing_object
));
4418 * Discard backing_object.
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.
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);
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
++;
4442 assert(backing_object
->pager
== NULL
);
4444 backing_object
->alive
= FALSE
;
4445 vm_object_unlock(backing_object
);
4447 XPR(XPR_VM_OBJECT
, "vm_object_collapse, collapsed 0x%X\n",
4448 backing_object
, 0, 0, 0, 0);
4450 #if VM_OBJECT_TRACKING
4451 if (vm_object_tracking_inited
) {
4452 btlog_remove_entries_for_element(vm_object_tracking_btlog
,
4455 #endif /* VM_OBJECT_TRACKING */
4457 vm_object_lock_destroy(backing_object
);
4459 zfree(vm_object_zone
, backing_object
);
4463 vm_object_do_bypass(
4465 vm_object_t backing_object
)
4468 * Make the parent shadow the next object
4472 vm_object_lock_assert_exclusive(object
);
4473 vm_object_lock_assert_exclusive(backing_object
);
4477 * Do object reference in-line to
4478 * conditionally increment shadow's
4479 * residence count. If object is not
4480 * resident, leave residence count
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
);
4490 vm_object_unlock(backing_object
->shadow
);
4492 #else /* TASK_SWAPPER */
4493 vm_object_reference(backing_object
->shadow
);
4494 #endif /* TASK_SWAPPER */
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
;
4502 /* no shadow, therefore no shadow offset... */
4503 object
->vo_shadow_offset
= 0;
4507 * Backing object might have had a copy pointer
4508 * to us. If it did, clear it.
4510 if (backing_object
->copy
== object
) {
4511 backing_object
->copy
= VM_OBJECT_NULL
;
4515 * Drop the reference count on backing_object.
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]
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.
4529 * Don't call vm_object_deallocate unless
4530 * ref_count drops to zero.
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.
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
--;
4543 if (object
->res_count
!= 0) {
4544 vm_object_res_deallocate(backing_object
);
4546 assert(backing_object
->ref_count
> 0);
4547 #endif /* TASK_SWAPPER */
4548 vm_object_unlock(backing_object
);
4551 * Drop locks so that we can deallocate
4552 * the backing object.
4556 if (object
->res_count
== 0) {
4557 /* XXX get a reference for the deallocate below */
4558 vm_object_res_reference(backing_object
);
4560 #endif /* TASK_SWAPPER */
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
4571 vm_object_activity_begin(object
);
4572 vm_object_unlock(object
);
4574 vm_object_unlock(backing_object
);
4575 vm_object_deallocate(backing_object
);
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
4584 vm_object_lock(object
);
4585 vm_object_activity_end(object
);
4593 * vm_object_collapse:
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.
4599 * Requires that the object be locked and the page queues be unlocked.
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;
4607 __private_extern__
void
4610 vm_object_offset_t hint_offset
,
4611 boolean_t can_bypass
)
4613 vm_object_t backing_object
;
4614 unsigned int rcount
;
4616 vm_object_t original_object
;
4617 int object_lock_type
;
4618 int backing_object_lock_type
;
4620 vm_object_collapse_calls
++;
4622 if (!vm_object_collapse_allowed
&&
4623 !(can_bypass
&& vm_object_bypass_allowed
)) {
4627 XPR(XPR_VM_OBJECT
, "vm_object_collapse, obj 0x%X\n",
4628 object
, 0, 0, 0, 0);
4630 if (object
== VM_OBJECT_NULL
) {
4634 original_object
= object
;
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.
4642 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4643 backing_object_lock_type
= OBJECT_LOCK_SHARED
;
4646 object
= original_object
;
4647 vm_object_lock_assert_exclusive(object
);
4650 vm_object_collapse_objects
++;
4652 * Verify that the conditions are right for either
4653 * collapse or bypass:
4657 * There is a backing object, and
4660 backing_object
= object
->shadow
;
4661 if (backing_object
== VM_OBJECT_NULL
) {
4662 if (object
!= original_object
) {
4663 vm_object_unlock(object
);
4667 if (backing_object_lock_type
== OBJECT_LOCK_SHARED
) {
4668 vm_object_lock_shared(backing_object
);
4670 vm_object_lock(backing_object
);
4674 * No pages in the object are currently
4675 * being paged out, and
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
);
4683 object
= backing_object
;
4684 object_lock_type
= backing_object_lock_type
;
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.
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
);
4704 object
= backing_object
;
4705 object_lock_type
= backing_object_lock_type
;
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
4714 * Collapsing a purgeable object would require some
4715 * updates to the purgeable compressed ledgers.
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
);
4727 object
= backing_object
;
4728 object_lock_type
= backing_object_lock_type
;
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
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
);
4748 object
= backing_object
;
4749 object_lock_type
= backing_object_lock_type
;
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
4759 * If there is exactly one reference to the backing
4760 * object, we may be able to collapse it into the
4763 * As long as one of the objects is still not known
4764 * to the pager, we can collapse them.
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
) {
4772 * We need the exclusive lock on the VM objects.
4774 if (backing_object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
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
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.
4788 vm_object_unlock(backing_object
);
4789 if (object
!= original_object
) {
4790 vm_object_unlock(object
);
4792 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4793 backing_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
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);
4804 * Collapse the object with its backing
4805 * object, and try again with the object's
4806 * new backing object.
4809 vm_object_do_collapse(object
, backing_object
);
4810 vm_object_collapse_do_collapse
++;
4815 * Collapsing the backing object was not possible
4816 * or permitted, so let's try bypassing it.
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
);
4824 object
= backing_object
;
4825 object_lock_type
= backing_object_lock_type
;
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.
4835 size
= (unsigned int)atop(object
->vo_size
);
4836 rcount
= object
->resident_page_count
;
4838 if (rcount
!= size
) {
4839 vm_object_offset_t offset
;
4840 vm_object_offset_t backing_offset
;
4841 unsigned int backing_rcount
;
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.
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
);
4853 object
= backing_object
;
4854 object_lock_type
= backing_object_lock_type
;
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.
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
);
4868 object
= backing_object
;
4869 object_lock_type
= backing_object_lock_type
;
4873 backing_offset
= object
->vo_shadow_offset
;
4874 backing_rcount
= backing_object
->resident_page_count
;
4876 if ((int)backing_rcount
- (int)(atop(backing_object
->vo_size
) - size
) > (int)rcount
) {
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
4883 if (object
!= original_object
) {
4884 vm_object_unlock(object
);
4886 object
= backing_object
;
4887 object_lock_type
= backing_object_lock_type
;
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
4898 * If the backing object has existence info,
4899 * we must check examine its existence info
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)--))
4910 * Check the hint location first
4911 * (since it is often the quickest way out of here).
4913 if (object
->cow_hint
!= ~(vm_offset_t
)0) {
4914 hint_offset
= (vm_object_offset_t
)object
->cow_hint
;
4916 hint_offset
= (hint_offset
> 8 * PAGE_SIZE_64
) ?
4917 (hint_offset
- 8 * PAGE_SIZE_64
) : 0;
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
);
4929 object
= backing_object
;
4930 object_lock_type
= backing_object_lock_type
;
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.
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
4948 if (backing_rcount
&& backing_rcount
< (size
/ 8)) {
4949 unsigned int rc
= rcount
;
4952 backing_rcount
= backing_object
->resident_page_count
;
4953 p
= (vm_page_t
)vm_page_queue_first(&backing_object
->memq
);
4955 offset
= (p
->vmp_offset
- backing_offset
);
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 */
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
);
4972 object
= backing_object
;
4973 object_lock_type
= backing_object_lock_type
;
4979 * Walk through the offsets looking for pages in the
4980 * backing object that show through to the object.
4982 if (backing_rcount
) {
4983 offset
= hint_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 */
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
);
5001 object
= backing_object
;
5002 object_lock_type
= backing_object_lock_type
;
5009 * We need "exclusive" locks on the 2 VM objects.
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
);
5016 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
5017 backing_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
5021 /* reset the offset hint for any objects deeper in the chain */
5022 object
->cow_hint
= (vm_offset_t
)0;
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.
5030 vm_object_do_bypass(object
, backing_object
);
5031 vm_object_collapse_do_bypass
++;
5034 * Try again with this object's new backing object.
5042 * if (object != original_object) {
5043 * vm_object_unlock(object);
5049 * Routine: vm_object_page_remove: [internal]
5051 * Removes all physical pages in the specified
5052 * object range from the object's list of pages.
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.
5059 unsigned int vm_object_page_remove_lookup
= 0;
5060 unsigned int vm_object_page_remove_iterate
= 0;
5062 __private_extern__
void
5063 vm_object_page_remove(
5065 vm_object_offset_t start
,
5066 vm_object_offset_t end
)
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.
5076 if (atop_64(end
- start
) < (unsigned)object
->resident_page_count
/ 16) {
5077 vm_object_page_remove_lookup
++;
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
));
5090 vm_object_page_remove_iterate
++;
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
));
5109 * Routine: vm_object_coalesce
5110 * Function: Coalesces two objects backing up adjoining
5111 * regions of memory into a single object.
5113 * returns TRUE if objects were combined.
5115 * NOTE: Only works at the moment if the second object is NULL -
5116 * if it's not, which object do we lock first?
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
5124 * prev_size Size of reference to prev_object
5125 * next_size Size of reference to next_object
5128 * The object(s) must *not* be locked. The map must be locked
5129 * to preserve the reference to the object(s).
5131 static int vm_object_coalesce_count
= 0;
5133 __private_extern__ boolean_t
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
)
5142 vm_object_size_t newsize
;
5148 if (next_object
!= VM_OBJECT_NULL
) {
5152 if (prev_object
== VM_OBJECT_NULL
) {
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);
5160 vm_object_lock(prev_object
);
5163 * Try to collapse the object first
5165 vm_object_collapse(prev_object
, prev_offset
, TRUE
);
5168 * Can't coalesce if pages not mapped to
5169 * prev_entry may be in use any way:
5170 * . more than one reference
5172 * . shadows another object
5173 * . has a copy elsewhere
5175 * . paging references (pages might be in page-list)
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
);
5190 vm_object_coalesce_count
++;
5193 * Remove any pages that may still be in the object from
5194 * a previous deallocation.
5196 vm_object_page_remove(prev_object
,
5197 prev_offset
+ prev_size
,
5198 prev_offset
+ prev_size
+ next_size
);
5201 * Extend the object if necessary.
5203 newsize
= prev_offset
+ prev_size
+ next_size
;
5204 if (newsize
> prev_object
->vo_size
) {
5205 prev_object
->vo_size
= newsize
;
5208 vm_object_unlock(prev_object
);
5213 vm_object_populate_with_private(
5215 vm_object_offset_t offset
,
5220 vm_object_offset_t base_offset
;
5223 if (!object
->private) {
5224 return KERN_FAILURE
;
5227 base_page
= phys_page
;
5229 vm_object_lock(object
);
5231 if (!object
->phys_contiguous
) {
5234 if ((base_offset
= trunc_page_64(offset
)) != offset
) {
5235 vm_object_unlock(object
);
5236 return KERN_FAILURE
;
5238 base_offset
+= object
->paging_offset
;
5241 m
= vm_page_lookup(object
, base_offset
);
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();
5250 m
->vmp_fictitious
= FALSE
;
5251 VM_PAGE_SET_PHYS_PAGE(m
, base_page
);
5253 } else if (VM_PAGE_GET_PHYS_PAGE(m
) != base_page
) {
5254 if (!m
->vmp_private
) {
5256 * we'd leak a real page... that can't be right
5258 panic("vm_object_populate_with_private - %p not private", m
);
5260 if (m
->vmp_pmapped
) {
5262 * pmap call to clear old mapping
5264 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
5266 VM_PAGE_SET_PHYS_PAGE(m
, base_page
);
5269 while ((m
= vm_page_grab_fictitious()) == VM_PAGE_NULL
) {
5270 vm_page_more_fictitious();
5274 * private normally requires lock_queues but since we
5275 * are initializing the page, its not necessary here
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
;
5283 vm_page_insert(m
, object
, base_offset
);
5285 base_page
++; /* Go to the next physical page */
5286 base_offset
+= PAGE_SIZE
;
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 */
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
;
5301 vm_object_unlock(object
);
5303 return KERN_SUCCESS
;
5308 memory_object_create_named(
5309 memory_object_t pager
,
5310 memory_object_offset_t size
,
5311 memory_object_control_t
*control
)
5315 *control
= MEMORY_OBJECT_CONTROL_NULL
;
5316 if (pager
== MEMORY_OBJECT_NULL
) {
5317 return KERN_INVALID_ARGUMENT
;
5320 object
= vm_object_memory_object_associate(pager
,
5324 if (object
== VM_OBJECT_NULL
) {
5325 return KERN_INVALID_OBJECT
;
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
,
5337 *control
= object
->pager_control
;
5338 vm_object_unlock(object
);
5340 return KERN_SUCCESS
;
5345 * Routine: memory_object_recover_named [user interface]
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.
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
5357 memory_object_recover_named(
5358 memory_object_control_t control
,
5359 boolean_t wait_on_terminating
)
5363 object
= memory_object_control_to_vm_object(control
);
5364 if (object
== VM_OBJECT_NULL
) {
5365 return KERN_INVALID_ARGUMENT
;
5368 vm_object_lock(object
);
5370 if (object
->terminating
&& wait_on_terminating
) {
5371 vm_object_wait(object
,
5372 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
5377 if (!object
->alive
) {
5378 vm_object_unlock(object
);
5379 return KERN_FAILURE
;
5382 if (object
->named
== TRUE
) {
5383 vm_object_unlock(object
);
5384 return KERN_SUCCESS
;
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
,
5395 vm_object_unlock(object
);
5396 return KERN_SUCCESS
;
5401 * vm_object_release_name:
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.
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
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.
5417 __private_extern__ kern_return_t
5418 vm_object_release_name(
5423 boolean_t original_object
= TRUE
;
5425 while (object
!= VM_OBJECT_NULL
) {
5426 vm_object_lock(object
);
5428 assert(object
->alive
);
5429 if (original_object
) {
5430 assert(object
->named
);
5432 assert(object
->ref_count
> 0);
5435 * We have to wait for initialization before
5436 * destroying or caching the object.
5439 if (object
->pager_created
&& !object
->pager_initialized
) {
5440 assert(!object
->can_persist
);
5441 vm_object_assert_wait(object
,
5442 VM_OBJECT_EVENT_INITIALIZED
,
5444 vm_object_unlock(object
);
5445 thread_block(THREAD_CONTINUE_NULL
);
5449 if (((object
->ref_count
> 1)
5450 && (flags
& MEMORY_OBJECT_TERMINATE_IDLE
))
5451 || (object
->terminating
)) {
5452 vm_object_unlock(object
);
5453 return KERN_FAILURE
;
5455 if (flags
& MEMORY_OBJECT_RELEASE_NO_OP
) {
5456 vm_object_unlock(object
);
5457 return KERN_SUCCESS
;
5461 if ((flags
& MEMORY_OBJECT_RESPECT_CACHE
) &&
5462 (object
->ref_count
== 1)) {
5463 if (original_object
) {
5464 object
->named
= FALSE
;
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
;
5472 VM_OBJ_RES_DECR(object
);
5473 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
5475 if (object
->ref_count
== 1) {
5476 if (vm_object_terminate(object
) != KERN_SUCCESS
) {
5477 if (original_object
) {
5478 return KERN_FAILURE
;
5480 return KERN_SUCCESS
;
5483 if (shadow
!= VM_OBJECT_NULL
) {
5484 original_object
= FALSE
;
5488 return KERN_SUCCESS
;
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
;
5496 vm_object_unlock(object
);
5497 return KERN_SUCCESS
;
5502 return KERN_FAILURE
;
5506 __private_extern__ kern_return_t
5507 vm_object_lock_request(
5509 vm_object_offset_t offset
,
5510 vm_object_size_t size
,
5511 memory_object_return_t should_return
,
5515 __unused boolean_t should_flush
;
5517 should_flush
= flags
& MEMORY_OBJECT_DATA_FLUSH
;
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
);
5525 * Check for bogus arguments.
5527 if (object
== VM_OBJECT_NULL
) {
5528 return KERN_INVALID_ARGUMENT
;
5531 if ((prot
& ~VM_PROT_ALL
) != 0 && prot
!= VM_PROT_NO_CHANGE
) {
5532 return KERN_INVALID_ARGUMENT
;
5535 size
= round_page_64(size
);
5538 * Lock the object, and acquire a paging reference to
5539 * prevent the memory_object reference from being released.
5541 vm_object_lock(object
);
5542 vm_object_paging_begin(object
);
5544 (void)vm_object_update(object
,
5545 offset
, size
, NULL
, NULL
, should_return
, flags
, prot
);
5547 vm_object_paging_end(object
);
5548 vm_object_unlock(object
);
5550 return KERN_SUCCESS
;
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.
5562 * On entry the object must be locked and it must be
5563 * purgeable with no delayed copies pending.
5566 vm_object_purge(vm_object_t object
, int flags
)
5568 unsigned int object_page_count
= 0, pgcount
= 0;
5569 uint64_t total_purged_pgcount
= 0;
5570 boolean_t skipped_object
= FALSE
;
5572 vm_object_lock_assert_exclusive(object
);
5574 if (object
->purgable
== VM_PURGABLE_DENY
) {
5578 assert(object
->copy
== VM_OBJECT_NULL
);
5579 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
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.
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()).
5596 if (object
->purgable
== VM_PURGABLE_VOLATILE
) {
5598 assert(object
->resident_page_count
>=
5599 object
->wired_page_count
);
5600 delta
= (object
->resident_page_count
-
5601 object
->wired_page_count
);
5603 assert(vm_page_purgeable_count
>=
5606 (SInt32
*)&vm_page_purgeable_count
);
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
);
5614 object
->purgable
= VM_PURGABLE_EMPTY
;
5616 assert(object
->purgable
== VM_PURGABLE_EMPTY
);
5618 object_page_count
= object
->resident_page_count
;
5620 vm_object_reap_pages(object
, REAP_PURGEABLE
);
5622 if (object
->resident_page_count
>= object_page_count
) {
5623 total_purged_pgcount
= 0;
5625 total_purged_pgcount
= object_page_count
- object
->resident_page_count
;
5628 if (object
->pager
!= NULL
) {
5629 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
5631 if (object
->activity_in_progress
== 0 &&
5632 object
->paging_in_progress
== 0) {
5634 * Also reap any memory coming from this object
5635 * in the VM compressor.
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.
5642 pgcount
= vm_compressor_pager_get_count(object
->pager
);
5644 pgcount
= vm_compressor_pager_reap_pages(object
->pager
, flags
);
5645 vm_compressor_pager_count(object
->pager
,
5649 vm_object_owner_compressed_update(object
,
5652 if (!(flags
& C_DONT_BLOCK
)) {
5653 assert(vm_compressor_pager_get_count(object
->pager
)
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
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...
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.
5674 skipped_object
= TRUE
;
5678 vm_object_lock_assert_exclusive(object
);
5680 total_purged_pgcount
+= pgcount
;
5682 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_VM
, OBJECT_PURGE_ONE
)),
5683 VM_KERNEL_UNSLIDE_OR_PERM(object
), /* purged object */
5685 total_purged_pgcount
,
5689 return total_purged_pgcount
;
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.
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.
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.
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.
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
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.
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
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
5766 * The object must be locked.
5769 vm_object_purgable_control(
5771 vm_purgable_t control
,
5777 if (object
== VM_OBJECT_NULL
) {
5779 * Object must already be present or it can't be purgeable.
5781 return KERN_INVALID_ARGUMENT
;
5784 vm_object_lock_assert_exclusive(object
);
5787 * Get current state of the purgeable object.
5789 old_state
= object
->purgable
;
5790 if (old_state
== VM_PURGABLE_DENY
) {
5791 return KERN_INVALID_ARGUMENT
;
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
);
5799 * Execute the desired operation.
5801 if (control
== VM_PURGABLE_GET_STATE
) {
5803 return KERN_SUCCESS
;
5806 if (control
== VM_PURGABLE_SET_STATE
&&
5807 object
->purgeable_only_by_kernel
) {
5808 return KERN_PROTECTION_FAILURE
;
5811 if (control
!= VM_PURGABLE_SET_STATE
&&
5812 control
!= VM_PURGABLE_SET_STATE_FROM_KERNEL
) {
5813 return KERN_INVALID_ARGUMENT
;
5816 if ((*state
) & VM_PURGABLE_DEBUG_EMPTY
) {
5817 object
->volatile_empty
= TRUE
;
5819 if ((*state
) & VM_PURGABLE_DEBUG_FAULT
) {
5820 object
->volatile_fault
= TRUE
;
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
;
5829 if (object
->volatile_empty
) {
5830 /* debugging mode: go straight to empty */
5831 new_state
= VM_PURGABLE_EMPTY
;
5835 switch (new_state
) {
5836 case VM_PURGABLE_DENY
:
5838 * Attempting to convert purgeable memory to non-purgeable:
5841 return KERN_INVALID_ARGUMENT
;
5842 case VM_PURGABLE_NONVOLATILE
:
5843 object
->purgable
= new_state
;
5845 if (old_state
== VM_PURGABLE_VOLATILE
) {
5848 assert(object
->resident_page_count
>=
5849 object
->wired_page_count
);
5850 delta
= (object
->resident_page_count
-
5851 object
->wired_page_count
);
5853 assert(vm_page_purgeable_count
>= delta
);
5857 (SInt32
*)&vm_page_purgeable_count
);
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
);
5866 vm_page_lock_queues();
5868 /* object should be on a queue */
5869 assert(object
->objq
.next
!= NULL
&&
5870 object
->objq
.prev
!= NULL
);
5871 purgeable_q_t queue
;
5874 * Move object from its volatile queue to the
5875 * non-volatile queue...
5877 queue
= vm_purgeable_object_remove(object
);
5880 if (object
->purgeable_when_ripe
) {
5881 vm_purgeable_token_delete_last(queue
);
5883 assert(queue
->debug_count_objects
>= 0);
5885 vm_page_unlock_queues();
5887 if (old_state
== VM_PURGABLE_VOLATILE
||
5888 old_state
== VM_PURGABLE_EMPTY
) {
5890 * Transfer the object's pages from the volatile to
5891 * non-volatile ledgers.
5893 vm_purgeable_accounting(object
, VM_PURGABLE_VOLATILE
);
5898 case VM_PURGABLE_VOLATILE
:
5899 if (object
->volatile_fault
) {
5903 vm_page_queue_iterate(&object
->memq
, p
, vmp_listq
) {
5906 p
->vmp_fictitious
) {
5909 refmod
= pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(p
));
5910 if ((refmod
& VM_MEM_MODIFIED
) &&
5912 SET_PAGE_DIRTY(p
, FALSE
);
5917 assert(old_state
!= VM_PURGABLE_EMPTY
);
5919 purgeable_q_t queue
;
5921 /* find the correct queue */
5922 if ((*state
& VM_PURGABLE_ORDERING_MASK
) == VM_PURGABLE_ORDERING_OBSOLETE
) {
5923 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_OBSOLETE
];
5925 if ((*state
& VM_PURGABLE_BEHAVIOR_MASK
) == VM_PURGABLE_BEHAVIOR_FIFO
) {
5926 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_FIFO
];
5928 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_LIFO
];
5932 if (old_state
== VM_PURGABLE_NONVOLATILE
||
5933 old_state
== VM_PURGABLE_EMPTY
) {
5936 if ((*state
& VM_PURGABLE_NO_AGING_MASK
) ==
5937 VM_PURGABLE_NO_AGING
) {
5938 object
->purgeable_when_ripe
= FALSE
;
5940 object
->purgeable_when_ripe
= TRUE
;
5943 if (object
->purgeable_when_ripe
) {
5944 kern_return_t result
;
5946 /* try to add token... this can fail */
5947 vm_page_lock_queues();
5949 result
= vm_purgeable_token_add(queue
);
5950 if (result
!= KERN_SUCCESS
) {
5951 vm_page_unlock_queues();
5954 vm_page_unlock_queues();
5957 assert(object
->resident_page_count
>=
5958 object
->wired_page_count
);
5959 delta
= (object
->resident_page_count
-
5960 object
->wired_page_count
);
5964 &vm_page_purgeable_count
);
5966 if (object
->wired_page_count
!= 0) {
5967 OSAddAtomic(object
->wired_page_count
,
5968 &vm_page_purgeable_wired_count
);
5971 object
->purgable
= new_state
;
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
;
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.
5988 assert(object
->objq
.next
!= NULL
&& object
->objq
.prev
!= NULL
); /* object should be on a queue */
5990 old_queue
= vm_purgeable_object_remove(object
);
5993 if ((*state
& VM_PURGABLE_NO_AGING_MASK
) ==
5994 VM_PURGABLE_NO_AGING
) {
5995 purgeable_when_ripe
= FALSE
;
5997 purgeable_when_ripe
= TRUE
;
6000 if (old_queue
!= queue
||
6001 (purgeable_when_ripe
!=
6002 object
->purgeable_when_ripe
)) {
6003 kern_return_t result
;
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
);
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 */
6015 vm_page_unlock_queues();
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
);
6025 assert(queue
->debug_count_objects
>= 0);
6030 case VM_PURGABLE_EMPTY
:
6031 if (object
->volatile_fault
) {
6035 vm_page_queue_iterate(&object
->memq
, p
, vmp_listq
) {
6038 p
->vmp_fictitious
) {
6041 refmod
= pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(p
));
6042 if ((refmod
& VM_MEM_MODIFIED
) &&
6044 SET_PAGE_DIRTY(p
, FALSE
);
6049 if (old_state
== VM_PURGABLE_VOLATILE
) {
6050 purgeable_q_t old_queue
;
6052 /* object should be on a queue */
6053 assert(object
->objq
.next
!= NULL
&&
6054 object
->objq
.prev
!= NULL
);
6056 old_queue
= vm_purgeable_object_remove(object
);
6058 if (object
->purgeable_when_ripe
) {
6059 vm_page_lock_queues();
6060 vm_purgeable_token_delete_first(old_queue
);
6061 vm_page_unlock_queues();
6065 if (old_state
== VM_PURGABLE_NONVOLATILE
) {
6067 * This object's pages were previously accounted as
6068 * "non-volatile" and now need to be accounted as
6071 vm_purgeable_accounting(object
,
6072 VM_PURGABLE_NONVOLATILE
);
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".
6079 object
->purgable
= VM_PURGABLE_EMPTY
;
6082 (void) vm_object_purge(object
, 0);
6083 assert(object
->purgable
== VM_PURGABLE_EMPTY
);
6090 vm_object_lock_assert_exclusive(object
);
6092 return KERN_SUCCESS
;
6096 vm_object_get_page_counts(
6098 vm_object_offset_t offset
,
6099 vm_object_size_t size
,
6100 unsigned int *resident_page_count
,
6101 unsigned int *dirty_page_count
)
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;
6111 if (object
== VM_OBJECT_NULL
) {
6112 return KERN_INVALID_ARGUMENT
;
6116 cur_offset
= offset
;
6118 end_offset
= offset
+ size
;
6120 vm_object_lock_assert_exclusive(object
);
6122 if (dirty_page_count
!= NULL
) {
6123 count_dirty_pages
= TRUE
;
6126 if (resident_page_count
!= NULL
&& count_dirty_pages
== FALSE
) {
6129 * - we only want the resident page count, and,
6130 * - the entire object is exactly covered by the request.
6132 if (offset
== 0 && (object
->vo_size
== size
)) {
6133 *resident_page_count
= object
->resident_page_count
;
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
++;
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
++;
6151 for (cur_offset
= offset
; cur_offset
< end_offset
; cur_offset
+= PAGE_SIZE_64
) {
6152 p
= vm_page_lookup(object
, cur_offset
);
6154 if (p
!= VM_PAGE_NULL
) {
6155 local_resident_count
++;
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
++;
6166 if (resident_page_count
!= NULL
) {
6167 *resident_page_count
= local_resident_count
;
6170 if (dirty_page_count
!= NULL
) {
6171 *dirty_page_count
= local_dirty_count
;
6181 * vm_object_res_deallocate
6183 * (recursively) decrement residence counts on vm objects and their shadows.
6184 * Called from vm_object_deallocate and when swapping out an object.
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.
6190 * NOTE: this function used to use recursion, rather than iteration.
6193 __private_extern__
void
6194 vm_object_res_deallocate(
6197 vm_object_t orig_object
= object
;
6199 * Object is locked so it can be called directly
6200 * from vm_object_deallocate. Original object is never
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
);
6214 object
= tmp_object
;
6215 assert(object
->res_count
> 0);
6220 if (object
!= orig_object
) {
6221 vm_object_unlock(object
);
6226 * vm_object_res_reference
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.
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.
6236 * NOTE: this function used to use recursion, rather than iteration.
6239 __private_extern__
void
6240 vm_object_res_reference(
6243 vm_object_t orig_object
= object
;
6245 * Object is locked, so this can be called directly
6246 * from vm_object_reference. This lock is never released.
6248 while ((++object
->res_count
== 1) &&
6249 (object
->shadow
!= VM_OBJECT_NULL
)) {
6250 vm_object_t tmp_object
= object
->shadow
;
6252 assert(object
->ref_count
>= object
->res_count
);
6253 vm_object_lock(tmp_object
);
6254 if (object
!= orig_object
) {
6255 vm_object_unlock(object
);
6257 object
= tmp_object
;
6259 if (object
!= orig_object
) {
6260 vm_object_unlock(object
);
6262 assert(orig_object
->ref_count
>= orig_object
->res_count
);
6264 #endif /* TASK_SWAPPER */
6267 * vm_object_reference:
6269 * Gets another reference to the given object.
6271 #ifdef vm_object_reference
6272 #undef vm_object_reference
6274 __private_extern__
void
6275 vm_object_reference(
6278 if (object
== VM_OBJECT_NULL
) {
6282 vm_object_lock(object
);
6283 assert(object
->ref_count
> 0);
6284 vm_object_reference_locked(object
);
6285 vm_object_unlock(object
);
6289 * vm_object_transpose
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.
6296 * The VM objects must not be locked by caller.
6298 unsigned int vm_object_transpose_count
= 0;
6300 vm_object_transpose(
6301 vm_object_t object1
,
6302 vm_object_t object2
,
6303 vm_object_size_t transpose_size
)
6305 vm_object_t tmp_object
;
6306 kern_return_t retval
;
6307 boolean_t object1_locked
, object2_locked
;
6309 vm_object_offset_t page_offset
;
6311 tmp_object
= VM_OBJECT_NULL
;
6312 object1_locked
= FALSE
; object2_locked
= FALSE
;
6314 if (object1
== object2
||
6315 object1
== VM_OBJECT_NULL
||
6316 object2
== VM_OBJECT_NULL
) {
6318 * If the 2 VM objects are the same, there's
6319 * no point in exchanging their backing store.
6321 retval
= KERN_INVALID_VALUE
;
6326 * Since we need to lock both objects at the same time,
6327 * make sure we always lock them in the same order to
6330 if (object1
> object2
) {
6331 tmp_object
= object1
;
6333 object2
= tmp_object
;
6337 * Allocate a temporary VM object to hold object1's contents
6338 * while we copy object2 to object1.
6340 tmp_object
= vm_object_allocate(transpose_size
);
6341 vm_object_lock(tmp_object
);
6342 tmp_object
->can_persist
= FALSE
;
6346 * Grab control of the 1st VM object.
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
) {
6354 * We don't deal with copy or shadow objects (yet).
6356 retval
= KERN_INVALID_VALUE
;
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").
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.
6372 vm_object_paging_only_wait(object1
, THREAD_UNINT
);
6375 * Same as above for the 2nd object...
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
;
6385 vm_object_paging_only_wait(object2
, THREAD_UNINT
);
6388 if (object1
->vo_size
!= object2
->vo_size
||
6389 object1
->vo_size
!= transpose_size
) {
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
6398 retval
= KERN_INVALID_VALUE
;
6404 * Transpose the lists of resident pages.
6405 * This also updates the resident_page_count and the memq_hint.
6407 if (object1
->phys_contiguous
|| vm_page_queue_empty(&object1
->memq
)) {
6409 * No pages in object1, just transfer pages
6410 * from object2 to object1. No need to go through
6411 * an intermediate object.
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
);
6417 assert(vm_page_queue_empty(&object2
->memq
));
6418 } else if (object2
->phys_contiguous
|| vm_page_queue_empty(&object2
->memq
)) {
6420 * No pages in object2, just transfer pages
6421 * from object1 to object2. No need to go through
6422 * an intermediate object.
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
);
6428 assert(vm_page_queue_empty(&object1
->memq
));
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
);
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
);
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
);
6451 assert(vm_page_queue_empty(&tmp_object
->memq
));
6454 #define __TRANSPOSE_FIELD(field) \
6456 tmp_object->field = object1->field; \
6457 object1->field = object2->field; \
6458 object2->field = tmp_object->field; \
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);
6469 /* "res_count" refers to the object not its contents */
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
,
6492 if (object2
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
6493 memory_object_control_collapse(object2
->pager_control
,
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
--;
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);
6585 /* "uplq" refers to the object not its contents (see upl_transpose()) */
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
));
6592 #undef __TRANSPOSE_FIELD
6594 retval
= KERN_SUCCESS
;
6600 if (tmp_object
!= VM_OBJECT_NULL
) {
6601 vm_object_unlock(tmp_object
);
6603 * Re-initialize the temporary object to avoid
6604 * deallocating a real pager.
6606 _vm_object_allocate(transpose_size
, tmp_object
);
6607 vm_object_deallocate(tmp_object
);
6608 tmp_object
= VM_OBJECT_NULL
;
6611 if (object1_locked
) {
6612 vm_object_unlock(object1
);
6613 object1_locked
= FALSE
;
6615 if (object2_locked
) {
6616 vm_object_unlock(object2
);
6617 object2_locked
= FALSE
;
6620 vm_object_transpose_count
++;
6627 * vm_object_cluster_size
6629 * Determine how big a cluster we should issue an I/O for...
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
6636 * The original *start will be encompassed by the cluster
6639 extern int speculative_reads_disabled
;
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.
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);
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
)
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
;
6675 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
6676 vm_size_t max_ph_size
;
6677 vm_size_t min_ph_size
;
6679 assert( !(*length
& PAGE_MASK
));
6680 assert( !(*start
& PAGE_MASK_64
));
6683 * remember maxiumum length of run requested
6685 max_length
= *length
;
6687 * we'll always return a cluster size of at least
6688 * 1 page, since the original fault must always
6691 *length
= PAGE_SIZE
;
6694 if (speculative_reads_disabled
|| fault_info
== NULL
) {
6696 * no cluster... just fault the page in
6700 orig_start
= *start
;
6701 target_start
= orig_start
;
6702 cluster_size
= round_page(fault_info
->cluster_size
);
6703 behavior
= fault_info
->behavior
;
6705 vm_object_lock(object
);
6707 if (object
->pager
== MEMORY_OBJECT_NULL
) {
6708 goto out
; /* pager is gone for this object, nothing more to do */
6710 vnode_pager_get_isSSD(object
->pager
, &isSSD
);
6712 min_ph_size
= round_page(preheat_min_bytes
);
6713 max_ph_size
= round_page(preheat_max_bytes
);
6715 #if !CONFIG_EMBEDDED
6720 if (min_ph_size
& PAGE_MASK_64
) {
6721 min_ph_size
= trunc_page(min_ph_size
);
6724 if (max_ph_size
& PAGE_MASK_64
) {
6725 max_ph_size
= trunc_page(max_ph_size
);
6728 #endif /* !CONFIG_EMBEDDED */
6730 if (min_ph_size
< PAGE_SIZE
) {
6731 min_ph_size
= PAGE_SIZE
;
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
;
6740 if (max_length
> max_ph_size
) {
6741 max_length
= max_ph_size
;
6744 if (max_length
<= PAGE_SIZE
) {
6748 if (object
->internal
) {
6749 object_size
= object
->vo_size
;
6751 vnode_pager_get_object_size(object
->pager
, &object_size
);
6754 object_size
= round_page_64(object_size
);
6756 if (orig_start
>= object_size
) {
6758 * fault occurred beyond the EOF...
6759 * we need to punt w/o changing the
6764 if (object
->pages_used
> object
->pages_created
) {
6766 * must have wrapped our 32 bit counters
6769 object
->pages_used
= object
->pages_created
= 0;
6771 if ((sequential_run
= object
->sequential
)) {
6772 if (sequential_run
< 0) {
6773 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
6774 sequential_run
= 0 - sequential_run
;
6776 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
6781 behavior
= VM_BEHAVIOR_DEFAULT
;
6783 case VM_BEHAVIOR_DEFAULT
:
6784 if (object
->internal
&& fault_info
->user_tag
== VM_MEMORY_STACK
) {
6788 if (sequential_run
>= (3 * PAGE_SIZE
)) {
6789 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6791 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
) {
6792 look_behind
= FALSE
;
6799 if (object
->pages_created
< (20 * (min_ph_size
>> PAGE_SHIFT
))) {
6803 pre_heat_size
= min_ph_size
;
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
6811 pre_heat_size
= (max_length
* (uint64_t)object
->pages_used
) / object
->pages_created
;
6813 if (pre_heat_size
< min_ph_size
) {
6814 pre_heat_size
= min_ph_size
;
6816 pre_heat_size
= round_page(pre_heat_size
);
6822 case VM_BEHAVIOR_RANDOM
:
6823 if ((pre_heat_size
= cluster_size
) <= PAGE_SIZE
) {
6828 case VM_BEHAVIOR_SEQUENTIAL
:
6829 if ((pre_heat_size
= cluster_size
) == 0) {
6830 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6832 look_behind
= FALSE
;
6837 case VM_BEHAVIOR_RSEQNTL
:
6838 if ((pre_heat_size
= cluster_size
) == 0) {
6839 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6846 throttle_limit
= (uint32_t) max_length
;
6847 assert(throttle_limit
== max_length
);
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
;
6854 if (pre_heat_size
> max_length
) {
6855 pre_heat_size
= max_length
;
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
;
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);
6867 if (pre_heat_size
< min_ph_size
) {
6868 pre_heat_size
= min_ph_size
;
6871 if (look_ahead
== TRUE
) {
6872 if (look_behind
== TRUE
) {
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
6882 head_size
= trunc_page(pre_heat_size
/ 2);
6884 if (target_start
> head_size
) {
6885 target_start
-= head_size
;
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
6897 if ((target_start
+ pre_heat_size
) > object_size
) {
6898 pre_heat_size
= (vm_size_t
)(round_page_64(object_size
- target_start
));
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...
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
;
6908 if (pre_heat_size
> target_start
) {
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
6914 pre_heat_size
= (vm_size_t
) target_start
;
6918 assert( !(target_start
& PAGE_MASK_64
));
6919 assert( !(pre_heat_size
& PAGE_MASK_64
));
6921 if (pre_heat_size
<= PAGE_SIZE
) {
6925 if (look_behind
== TRUE
) {
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.
6932 head_size
= pre_heat_size
- tail_size
- PAGE_SIZE
;
6934 for (offset
= orig_start
- PAGE_SIZE_64
; head_size
; offset
-= PAGE_SIZE_64
, head_size
-= PAGE_SIZE
) {
6936 * don't poke below the lowest offset
6938 if (offset
< fault_info
->lo_offset
) {
6942 * for external objects or internal objects w/o a pager,
6943 * VM_COMPRESSOR_PAGER_STATE_GET will return VM_EXTERNAL_STATE_UNKNOWN
6945 if (VM_COMPRESSOR_PAGER_STATE_GET(object
, offset
) == VM_EXTERNAL_STATE_ABSENT
) {
6948 if (vm_page_lookup(object
, offset
) != VM_PAGE_NULL
) {
6950 * don't bridge resident pages
6955 *length
+= PAGE_SIZE
;
6958 if (look_ahead
== TRUE
) {
6959 for (offset
= orig_start
+ PAGE_SIZE_64
; tail_size
; offset
+= PAGE_SIZE_64
, tail_size
-= PAGE_SIZE
) {
6961 * don't poke above the highest offset
6963 if (offset
>= fault_info
->hi_offset
) {
6966 assert(offset
< object_size
);
6969 * for external objects or internal objects w/o a pager,
6970 * VM_COMPRESSOR_PAGER_STATE_GET will return VM_EXTERNAL_STATE_UNKNOWN
6972 if (VM_COMPRESSOR_PAGER_STATE_GET(object
, offset
) == VM_EXTERNAL_STATE_ABSENT
) {
6975 if (vm_page_lookup(object
, offset
) != VM_PAGE_NULL
) {
6977 * don't bridge resident pages
6981 *length
+= PAGE_SIZE
;
6985 if (*length
> max_length
) {
6986 *length
= max_length
;
6989 vm_object_unlock(object
);
6991 DTRACE_VM1(clustersize
, vm_size_t
, *length
);
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
7003 vm_object_offset_t offset
,
7005 ppnum_t
*phys_entry
,
7010 vm_object_lock(object
);
7012 if (ops
& UPL_POP_PHYSICAL
) {
7013 if (object
->phys_contiguous
) {
7015 *phys_entry
= (ppnum_t
)
7016 (object
->vo_shadow_offset
>> PAGE_SHIFT
);
7018 vm_object_unlock(object
);
7019 return KERN_SUCCESS
;
7021 vm_object_unlock(object
);
7022 return KERN_INVALID_OBJECT
;
7025 if (object
->phys_contiguous
) {
7026 vm_object_unlock(object
);
7027 return KERN_INVALID_OBJECT
;
7031 if ((dst_page
= vm_page_lookup(object
, offset
)) == VM_PAGE_NULL
) {
7032 vm_object_unlock(object
);
7033 return KERN_FAILURE
;
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 */
7042 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
7046 if (ops
& UPL_POP_DUMP
) {
7047 if (dst_page
->vmp_pmapped
== TRUE
) {
7048 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(dst_page
));
7051 VM_PAGE_FREE(dst_page
);
7058 /* Get the condition of flags before requested ops */
7059 /* are undertaken */
7061 if (dst_page
->vmp_dirty
) {
7062 *flags
|= UPL_POP_DIRTY
;
7064 if (dst_page
->vmp_free_when_done
) {
7065 *flags
|= UPL_POP_PAGEOUT
;
7067 if (dst_page
->vmp_precious
) {
7068 *flags
|= UPL_POP_PRECIOUS
;
7070 if (dst_page
->vmp_absent
) {
7071 *flags
|= UPL_POP_ABSENT
;
7073 if (dst_page
->vmp_busy
) {
7074 *flags
|= UPL_POP_BUSY
;
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 */
7088 assert(dst_page
->vmp_busy
|| (ops
& UPL_POP_BUSY
));
7089 if (ops
& UPL_POP_DIRTY
) {
7090 SET_PAGE_DIRTY(dst_page
, FALSE
);
7092 if (ops
& UPL_POP_PAGEOUT
) {
7093 dst_page
->vmp_free_when_done
= TRUE
;
7095 if (ops
& UPL_POP_PRECIOUS
) {
7096 dst_page
->vmp_precious
= TRUE
;
7098 if (ops
& UPL_POP_ABSENT
) {
7099 dst_page
->vmp_absent
= TRUE
;
7101 if (ops
& UPL_POP_BUSY
) {
7102 dst_page
->vmp_busy
= TRUE
;
7106 if (ops
& UPL_POP_CLR
) {
7107 assert(dst_page
->vmp_busy
);
7108 if (ops
& UPL_POP_DIRTY
) {
7109 dst_page
->vmp_dirty
= FALSE
;
7111 if (ops
& UPL_POP_PAGEOUT
) {
7112 dst_page
->vmp_free_when_done
= FALSE
;
7114 if (ops
& UPL_POP_PRECIOUS
) {
7115 dst_page
->vmp_precious
= FALSE
;
7117 if (ops
& UPL_POP_ABSENT
) {
7118 dst_page
->vmp_absent
= FALSE
;
7120 if (ops
& UPL_POP_BUSY
) {
7121 dst_page
->vmp_busy
= FALSE
;
7122 PAGE_WAKEUP(dst_page
);
7127 * The physical page number will remain valid
7128 * only if the page is kept busy.
7130 assert(dst_page
->vmp_busy
);
7131 *phys_entry
= VM_PAGE_GET_PHYS_PAGE(dst_page
);
7137 vm_object_unlock(object
);
7138 return KERN_SUCCESS
;
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.
7154 vm_object_offset_t offset_beg
,
7155 vm_object_offset_t offset_end
,
7159 vm_object_offset_t offset
;
7162 if (offset_end
- offset_beg
> (uint32_t) -1) {
7163 /* range is too big and would overflow "*range" */
7164 return KERN_INVALID_ARGUMENT
;
7166 if (object
->resident_page_count
== 0) {
7168 if (ops
& UPL_ROP_PRESENT
) {
7171 *range
= (uint32_t) (offset_end
- offset_beg
);
7172 assert(*range
== (offset_end
- offset_beg
));
7175 return KERN_SUCCESS
;
7177 vm_object_lock(object
);
7179 if (object
->phys_contiguous
) {
7180 vm_object_unlock(object
);
7181 return KERN_INVALID_OBJECT
;
7184 offset
= offset_beg
& ~PAGE_MASK_64
;
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
) {
7192 * someone else is playing with the
7193 * page, we will have to wait
7195 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
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
7204 if (dst_page
->vmp_laundry
) {
7205 vm_pageout_steal_laundry(dst_page
, FALSE
);
7208 if (dst_page
->vmp_pmapped
== TRUE
) {
7209 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(dst_page
));
7212 VM_PAGE_FREE(dst_page
);
7213 } else if ((ops
& UPL_ROP_ABSENT
)
7214 && (!dst_page
->vmp_absent
|| dst_page
->vmp_busy
)) {
7217 } else if (ops
& UPL_ROP_PRESENT
) {
7221 offset
+= PAGE_SIZE
;
7223 vm_object_unlock(object
);
7226 if (offset
> offset_end
) {
7227 offset
= offset_end
;
7229 if (offset
> offset_beg
) {
7230 *range
= (uint32_t) (offset
- offset_beg
);
7231 assert(*range
== (offset
- offset_beg
));
7236 return KERN_SUCCESS
;
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.
7245 pager_map_to_phys_contiguous(
7246 memory_object_control_t object
,
7247 memory_object_offset_t offset
,
7248 addr64_t base_vaddr
,
7252 boolean_t clobbered_private
;
7253 kern_return_t retval
;
7254 vm_object_t pager_object
;
7256 page_num
= pmap_find_phys(kernel_pmap
, base_vaddr
);
7259 retval
= KERN_FAILURE
;
7263 pager_object
= memory_object_control_to_vm_object(object
);
7265 if (!pager_object
) {
7266 retval
= KERN_FAILURE
;
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
);
7276 retval
= vm_object_populate_with_private(pager_object
, offset
, page_num
, size
);
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
);
7290 uint32_t scan_object_collision
= 0;
7293 vm_object_lock(vm_object_t object
)
7295 if (object
== vm_pageout_scan_wants_object
) {
7296 scan_object_collision
++;
7299 DTRACE_VM(vm_object_lock_w
);
7300 lck_rw_lock_exclusive(&object
->Lock
);
7301 #if DEVELOPMENT || DEBUG
7302 object
->Lock_owner
= current_thread();
7307 vm_object_lock_avoid(vm_object_t object
)
7309 if (object
== vm_pageout_scan_wants_object
) {
7310 scan_object_collision
++;
7317 _vm_object_lock_try(vm_object_t object
)
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();
7332 vm_object_lock_try(vm_object_t object
)
7335 * Called from hibernate path so check before blocking.
7337 if (vm_object_lock_avoid(object
) && ml_get_interrupts_enabled() && get_preemption_level() == 0) {
7340 return _vm_object_lock_try(object
);
7344 vm_object_lock_shared(vm_object_t object
)
7346 if (vm_object_lock_avoid(object
)) {
7349 DTRACE_VM(vm_object_lock_r
);
7350 lck_rw_lock_shared(&object
->Lock
);
7354 vm_object_lock_yield_shared(vm_object_t object
)
7356 boolean_t retval
= FALSE
, force_yield
= FALSE
;;
7358 vm_object_lock_assert_shared(object
);
7360 force_yield
= vm_object_lock_avoid(object
);
7362 retval
= lck_rw_lock_yield_shared(&object
->Lock
, force_yield
);
7364 DTRACE_VM(vm_object_lock_yield
);
7371 vm_object_lock_try_shared(vm_object_t object
)
7375 if (vm_object_lock_avoid(object
)) {
7378 retval
= lck_rw_try_lock_shared(&object
->Lock
);
7380 DTRACE_VM(vm_object_lock_r
);
7386 vm_object_lock_upgrade(vm_object_t object
)
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();
7401 vm_object_unlock(vm_object_t object
)
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
);
7408 object
->Lock_owner
= 0;
7409 DTRACE_VM(vm_object_unlock
);
7412 lck_rw_done(&object
->Lock
);
7416 unsigned int vm_object_change_wimg_mode_count
= 0;
7419 * The object must be locked
7422 vm_object_change_wimg_mode(vm_object_t object
, unsigned int wimg_mode
)
7426 vm_object_lock_assert_exclusive(object
);
7428 vm_object_paging_wait(object
, THREAD_UNINT
);
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
);
7435 if (wimg_mode
== VM_WIMG_USE_DEFAULT
) {
7436 object
->set_cache_attr
= FALSE
;
7438 object
->set_cache_attr
= TRUE
;
7441 object
->wimg_bits
= wimg_mode
;
7443 vm_object_change_wimg_mode_count
++;
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.
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
;
7461 #define MAX_FREE_BATCH 32
7462 #define FREEZER_DUTY_CYCLE_ON_MS 5
7463 #define FREEZER_DUTY_CYCLE_OFF_MS 5
7465 static int c_freezer_should_yield(void);
7469 c_freezer_should_yield()
7471 AbsoluteTime cur_time
;
7474 assert(c_freezer_last_yield_ts
);
7475 clock_get_uptime(&cur_time
);
7477 SUB_ABSOLUTETIME(&cur_time
, &c_freezer_last_yield_ts
);
7478 absolutetime_to_nanoseconds(cur_time
, &nsecs
);
7480 if (nsecs
> 1000 * 1000 * FREEZER_DUTY_CYCLE_ON_MS
) {
7488 vm_object_compressed_freezer_done()
7490 vm_compressor_finished_filling(&freezer_chead
);
7495 vm_object_compressed_freezer_pageout(
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;
7504 assert(object
!= VM_OBJECT_NULL
);
7505 assert(object
->internal
);
7507 vm_object_lock(object
);
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
);
7513 if (!object
->pager_initialized
) {
7514 vm_object_compressor_pager_create(object
);
7518 if (!object
->pager_initialized
|| object
->pager
== MEMORY_OBJECT_NULL
) {
7519 vm_object_unlock(object
);
7524 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
) {
7525 vm_object_offset_t curr_offset
= 0;
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".
7532 while (curr_offset
< object
->vo_size
) {
7533 curr_offset
= vm_compressor_pager_next_compressed(object
->pager
, curr_offset
);
7535 if (curr_offset
== (vm_object_offset_t
) -1) {
7539 retval
= vm_compressor_pager_relocate(object
->pager
, curr_offset
, &freezer_chead
);
7541 if (retval
!= KERN_SUCCESS
) {
7545 curr_offset
+= PAGE_SIZE_64
;
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.
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.
7562 obj_resident_page_count_snapshot
= object
->resident_page_count
;
7564 vm_object_activity_begin(object
);
7566 while ((obj_resident_page_count_snapshot
--) && !vm_page_queue_empty(&object
->memq
)) {
7567 p
= (vm_page_t
)vm_page_queue_first(&object
->memq
);
7569 KERNEL_DEBUG(0xe0430004 | DBG_FUNC_START
, object
, local_freed
, 0, 0, 0);
7571 vm_page_lockspin_queues();
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();
7576 KERNEL_DEBUG(0xe0430004 | DBG_FUNC_END
, object
, local_freed
, 1, 0, 0);
7578 vm_page_queue_remove(&object
->memq
, p
, vmp_listq
);
7579 vm_page_queue_enter(&object
->memq
, p
, vmp_listq
);
7584 if (p
->vmp_pmapped
== TRUE
) {
7585 int refmod_state
, pmap_flags
;
7587 if (p
->vmp_dirty
|| p
->vmp_precious
) {
7588 pmap_flags
= PMAP_OPTIONS_COMPRESSOR
;
7590 pmap_flags
= PMAP_OPTIONS_COMPRESSOR_IFF_MODIFIED
;
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
);
7599 if (p
->vmp_dirty
== FALSE
&& p
->vmp_precious
== FALSE
) {
7601 * Clean and non-precious page.
7603 vm_page_unlock_queues();
7606 KERNEL_DEBUG(0xe0430004 | DBG_FUNC_END
, object
, local_freed
, 2, 0, 0);
7610 if (p
->vmp_laundry
) {
7611 vm_pageout_steal_laundry(p
, TRUE
);
7614 vm_page_queues_remove(p
, TRUE
);
7616 vm_page_unlock_queues();
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.
7625 vm_page_queue_remove(&object
->memq
, p
, vmp_listq
);
7626 vm_page_queue_enter(&object
->memq
, p
, vmp_listq
);
7629 * Grab an activity_in_progress here for vm_pageout_compress_page() to consume.
7631 * Mark the page busy so no one messes with it while we have the object lock dropped.
7635 vm_object_activity_begin(object
);
7637 vm_object_unlock(object
);
7639 if (vm_pageout_compress_page(&freezer_chead
, freezer_compressor_scratch_buf
, p
) == KERN_SUCCESS
) {
7641 * page has already been un-tabled from the object via 'vm_page_remove'
7643 p
->vmp_snext
= local_freeq
;
7647 if (local_freed
>= MAX_FREE_BATCH
) {
7648 OSAddAtomic64(local_freed
, &vm_pageout_vminfo
.vm_pageout_compressions
);
7650 vm_page_free_list(local_freeq
, TRUE
);
7655 c_freezer_compression_count
++;
7657 KERNEL_DEBUG(0xe0430004 | DBG_FUNC_END
, object
, local_freed
, 0, 0, 0);
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
);
7664 vm_object_lock(object
);
7668 OSAddAtomic64(local_freed
, &vm_pageout_vminfo
.vm_pageout_compressions
);
7670 vm_page_free_list(local_freeq
, TRUE
);
7676 vm_object_activity_end(object
);
7678 vm_object_unlock(object
);
7680 if (c_freezer_should_yield()) {
7681 thread_yield_internal(FREEZER_DUTY_CYCLE_OFF_MS
);
7682 clock_get_uptime(&c_freezer_last_yield_ts
);
7686 #endif /* CONFIG_FREEZE */
7694 struct vm_pageout_queue
*iq
;
7696 if (!VM_CONFIG_COMPRESSOR_IS_PRESENT
) {
7700 iq
= &vm_pageout_queue_internal
;
7702 assert(object
!= VM_OBJECT_NULL
);
7704 vm_object_lock(object
);
7706 if (!object
->internal
||
7707 object
->terminating
||
7709 vm_object_unlock(object
);
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
);
7717 if (!object
->pager_initialized
) {
7718 vm_object_compressor_pager_create(object
);
7722 if (!object
->pager_initialized
|| object
->pager
== MEMORY_OBJECT_NULL
) {
7723 vm_object_unlock(object
);
7729 next
= (vm_page_t
)vm_page_queue_first(&object
->memq
);
7731 while (!vm_page_queue_end(&object
->memq
, (vm_page_queue_entry_t
)next
)) {
7733 next
= (vm_page_t
)vm_page_queue_next(&next
->vmp_listq
);
7735 assert(p
->vmp_q_state
!= VM_PAGE_ON_FREE_Q
);
7737 if ((p
->vmp_q_state
== VM_PAGE_ON_THROTTLED_Q
) ||
7743 p
->vmp_fictitious
||
7746 * Page is already being cleaned or can't be cleaned.
7750 if (vm_compressor_low_on_space()) {
7754 /* Throw to the pageout queue */
7756 vm_page_lockspin_queues();
7758 if (VM_PAGE_Q_THROTTLED(iq
)) {
7759 iq
->pgo_draining
= TRUE
;
7761 assert_wait((event_t
) (&iq
->pgo_laundry
+ 1),
7762 THREAD_INTERRUPTIBLE
);
7763 vm_page_unlock_queues();
7764 vm_object_unlock(object
);
7766 thread_block(THREAD_CONTINUE_NULL
);
7768 vm_object_lock(object
);
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
);
7780 if (p
->vmp_pmapped
== TRUE
) {
7785 * Tell pmap the page should be accounted
7786 * for as "compressed" if it's been modified.
7789 PMAP_OPTIONS_COMPRESSOR_IFF_MODIFIED
;
7790 if (p
->vmp_dirty
|| p
->vmp_precious
) {
7792 * We already know it's been modified,
7793 * so tell pmap to account for it
7796 pmap_options
= PMAP_OPTIONS_COMPRESSOR
;
7798 refmod_state
= pmap_disconnect_options(VM_PAGE_GET_PHYS_PAGE(p
),
7801 if (refmod_state
& VM_MEM_MODIFIED
) {
7802 SET_PAGE_DIRTY(p
, FALSE
);
7806 if (!p
->vmp_dirty
&& !p
->vmp_precious
) {
7807 vm_page_unlock_queues();
7811 vm_page_queues_remove(p
, TRUE
);
7813 vm_pageout_cluster(p
);
7815 vm_page_unlock_queues();
7817 vm_object_unlock(object
);
7823 vm_page_request_reprioritize(vm_object_t o
, uint64_t blkno
, uint32_t len
, int prio
)
7825 io_reprioritize_req_t req
;
7826 struct vnode
*devvp
= NULL
;
7828 if (vnode_pager_get_object_devvp(o
->pager
, (uintptr_t *)&devvp
) != KERN_SUCCESS
) {
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.
7837 req
= (io_reprioritize_req_t
)zalloc_noblock(io_reprioritize_req_zone
);
7843 req
->priority
= prio
;
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();
7851 /* Wakeup reprioritize thread */
7852 IO_REPRIO_THREAD_WAKEUP();
7858 vm_decmp_upl_reprioritize(upl_t upl
, int prio
)
7862 io_reprioritize_req_t req
;
7863 struct vnode
*devvp
= NULL
;
7867 uint64_t *io_upl_reprio_info
;
7870 if ((upl
->flags
& UPL_TRACKED_BY_OBJECT
) == 0 || (upl
->flags
& UPL_EXPEDITE_SUPPORTED
) == 0) {
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.
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
)) {
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. */
7893 io_upl
= upl
->decmp_io_upl
;
7894 assert((io_upl
->flags
& UPL_DECMP_REAL_IO
) != 0);
7895 io_upl_size
= io_upl
->size
;
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
) {
7904 /* Now again take the lock, recheck the state and grab out the required info */
7905 if (!upl_try_lock(upl
)) {
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. */
7914 memcpy(io_upl_reprio_info
, io_upl
->upl_reprio_info
, sizeof(uint64_t) * (io_upl_size
/ PAGE_SIZE
));
7916 /* Get the VM object for this UPL */
7917 if (io_upl
->flags
& UPL_SHADOWED
) {
7918 object
= io_upl
->map_object
->shadow
;
7920 object
= io_upl
->map_object
;
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
) {
7932 /* Now we have all the information needed to do the expedite */
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
;
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.
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
);
7951 req
->priority
= prio
;
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();
7961 offset
+= PAGE_SIZE
;
7965 /* Wakeup reprioritize thread */
7966 IO_REPRIO_THREAD_WAKEUP();
7969 kfree(io_upl_reprio_info
, sizeof(uint64_t) * (io_upl_size
/ PAGE_SIZE
));
7974 vm_page_handle_prio_inversion(vm_object_t o
, vm_page_t m
)
7977 upl_page_info_t
*pl
;
7978 unsigned int i
, num_pages
;
7981 cur_tier
= proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO
);
7984 * Scan through all UPLs associated with the object to find the
7985 * UPL containing the contended page.
7987 queue_iterate(&o
->uplq
, upl
, upl_t
, uplq
) {
7988 if (((upl
->flags
& UPL_EXPEDITE_SUPPORTED
) == 0) || upl
->upl_priority
<= cur_tier
) {
7991 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
7992 num_pages
= (upl
->size
/ PAGE_SIZE
);
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.
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
);
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
);
8014 /* Check if we found any hits */
8015 if (i
!= num_pages
) {
8024 vm_page_sleep(vm_object_t o
, vm_page_t m
, int interruptible
)
8028 KERNEL_DEBUG((MACHDBG_CODE(DBG_MACH_VM
, VM_PAGE_SLEEP
)) | DBG_FUNC_START
, o
, m
, 0, 0, 0);
8030 if (o
->io_tracking
&& ((m
->vmp_busy
== TRUE
) || (m
->vmp_cleaning
== TRUE
) || VM_PAGE_WIRED(m
))) {
8032 * Indicates page is busy due to an I/O. Issue a reprioritize request if necessary.
8034 vm_page_handle_prio_inversion(o
, m
);
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);
8043 io_reprioritize_thread(void *param __unused
, wait_result_t wr __unused
)
8045 io_reprioritize_req_t req
= NULL
;
8048 IO_REPRIORITIZE_LIST_LOCK();
8049 if (queue_empty(&io_reprioritize_list
)) {
8050 IO_REPRIORITIZE_LIST_UNLOCK();
8054 queue_remove_first(&io_reprioritize_list
, req
, io_reprioritize_req_t
, io_reprioritize_list
);
8055 IO_REPRIORITIZE_LIST_UNLOCK();
8057 vnode_pager_issue_reprioritize_io(req
->devvp
, req
->blkno
, req
->len
, req
->priority
);
8058 zfree(io_reprioritize_req_zone
, req
);
8061 IO_REPRIO_THREAD_CONTINUATION();
8065 #if VM_OBJECT_ACCESS_TRACKING
8067 vm_object_access_tracking(
8069 int *access_tracking_p
,
8070 uint32_t *access_tracking_reads_p
,
8071 uint32_t *access_tracking_writes_p
)
8073 int access_tracking
;
8075 access_tracking
= !!*access_tracking_p
;
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
;
8082 if (access_tracking_writes_p
) {
8083 *access_tracking_writes_p
= object
->access_tracking_writes
;
8085 object
->access_tracking
= access_tracking
;
8086 object
->access_tracking_reads
= 0;
8087 object
->access_tracking_writes
= 0;
8088 vm_object_unlock(object
);
8090 if (access_tracking
) {
8091 vm_object_pmap_protect_options(object
,
8100 #endif /* VM_OBJECT_ACCESS_TRACKING */
8103 vm_object_ledger_tag_ledgers(
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
)
8111 assert(object
->shadow
== VM_OBJECT_NULL
);
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
;
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
;
8130 case VM_OBJECT_LEDGER_TAG_MEDIA
:
8132 panic("%s: object %p has unsupported ledger_tag %d\n",
8133 __FUNCTION__
, object
, object
->vo_ledger_tag
);
8138 vm_object_ownership_change(
8142 boolean_t task_objq_locked
)
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
;
8153 int ledger_idx_compressed
;
8154 boolean_t do_footprint
;
8156 vm_object_lock_assert_exclusive(object
);
8157 assert(object
->internal
);
8159 old_ledger_tag
= object
->vo_ledger_tag
;
8160 old_owner
= VM_OBJECT_OWNER(object
);
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
);
8167 * Deal with the old owner and/or ledger tag, if needed.
8169 if (old_owner
!= TASK_NULL
&&
8170 ((old_owner
!= new_owner
) /* new owner ... */
8172 (old_ledger_tag
&& /* ... new ledger */
8173 old_ledger_tag
!= new_ledger_tag
))) {
8175 * Take this object off of the old owner's ledgers.
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
,
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
;
8188 ledger_idx
= ledger_idx_nonvolatile
;
8189 ledger_idx_compressed
= ledger_idx_nonvolatile_compressed
;
8191 if (resident_count
) {
8193 * Adjust the appropriate old owners's ledgers by the
8194 * number of resident pages.
8196 ledger_debit(old_owner
->ledger
,
8198 ptoa_64(resident_count
));
8199 /* adjust old owner's 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
));
8209 /* wired pages are always nonvolatile */
8210 ledger_debit(old_owner
->ledger
,
8211 ledger_idx_nonvolatile
,
8212 ptoa_64(wired_count
));
8214 ledger_debit(old_owner
->ledger
,
8215 task_ledgers
.phys_footprint
,
8216 ptoa_64(wired_count
));
8219 if (compressed_count
) {
8221 * Adjust the appropriate old owner's ledgers
8222 * by the number of compressed pages.
8224 ledger_debit(old_owner
->ledger
,
8225 ledger_idx_compressed
,
8226 ptoa_64(compressed_count
));
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
));
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
,
8240 if (!task_objq_locked
) {
8241 task_objq_lock(old_owner
);
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
,
8251 case VM_PURGABLE_VOLATILE
:
8252 vm_purgeable_volatile_owner_update(old_owner
,
8258 if (!task_objq_locked
) {
8259 task_objq_unlock(old_owner
);
8265 * Switch to new ledger tag and/or owner.
8267 object
->vo_ledger_tag
= new_ledger_tag
;
8268 object
->vo_owner
= new_owner
;
8270 if (new_owner
== VM_OBJECT_OWNER_DISOWNED
) {
8271 assert(old_owner
!= kernel_task
);
8272 new_owner
= kernel_task
;
8276 * Deal with the new owner and/or ledger tag, if needed.
8278 if (new_owner
!= TASK_NULL
&&
8279 ((new_owner
!= old_owner
) /* new owner ... */
8281 (new_ledger_tag
&& /* ... new ledger */
8282 new_ledger_tag
!= old_ledger_tag
))) {
8284 * Add this object to the new owner's ledgers.
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
,
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
;
8297 ledger_idx
= ledger_idx_nonvolatile
;
8298 ledger_idx_compressed
= ledger_idx_nonvolatile_compressed
;
8300 if (resident_count
) {
8302 * Adjust the appropriate new owners's ledgers by the
8303 * number of resident pages.
8305 ledger_credit(new_owner
->ledger
,
8307 ptoa_64(resident_count
));
8308 /* adjust new owner's 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
));
8318 /* wired pages are always nonvolatile */
8319 ledger_credit(new_owner
->ledger
,
8320 ledger_idx_nonvolatile
,
8321 ptoa_64(wired_count
));
8323 ledger_credit(new_owner
->ledger
,
8324 task_ledgers
.phys_footprint
,
8325 ptoa_64(wired_count
));
8328 if (compressed_count
) {
8330 * Adjust the new owner's ledgers by the number of
8333 ledger_credit(new_owner
->ledger
,
8334 ledger_idx_compressed
,
8335 ptoa_64(compressed_count
));
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
));
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
,
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
,
8358 case VM_PURGABLE_VOLATILE
:
8359 vm_purgeable_volatile_owner_update(new_owner
,
8365 task_objq_unlock(new_owner
);
8369 return KERN_SUCCESS
;