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/lock.h>
83 #include <kern/queue.h>
85 #include <kern/kalloc.h>
86 #include <kern/zalloc.h>
87 #include <kern/host.h>
88 #include <kern/host_statistics.h>
89 #include <kern/processor.h>
90 #include <kern/misc_protos.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>
105 * Virtual memory objects maintain the actual data
106 * associated with allocated virtual memory. A given
107 * page of memory exists within exactly one object.
109 * An object is only deallocated when all "references"
112 * Associated with each object is a list of all resident
113 * memory pages belonging to that object; this list is
114 * maintained by the "vm_page" module, but locked by the object's
117 * Each object also records the memory object reference
118 * that is used by the kernel to request and write
119 * back data (the memory object, field "pager"), etc...
121 * Virtual memory objects are allocated to provide
122 * zero-filled memory (vm_allocate) or map a user-defined
123 * memory object into a virtual address space (vm_map).
125 * Virtual memory objects that refer to a user-defined
126 * memory object are called "permanent", because all changes
127 * made in virtual memory are reflected back to the
128 * memory manager, which may then store it permanently.
129 * Other virtual memory objects are called "temporary",
130 * meaning that changes need be written back only when
131 * necessary to reclaim pages, and that storage associated
132 * with the object can be discarded once it is no longer
135 * A permanent memory object may be mapped into more
136 * than one virtual address space. Moreover, two threads
137 * may attempt to make the first mapping of a memory
138 * object concurrently. Only one thread is allowed to
139 * complete this mapping; all others wait for the
140 * "pager_initialized" field is asserted, indicating
141 * that the first thread has initialized all of the
142 * necessary fields in the virtual memory object structure.
144 * The kernel relies on a *default memory manager* to
145 * provide backing storage for the zero-filled virtual
146 * memory objects. The pager memory objects associated
147 * with these temporary virtual memory objects are only
148 * requested from the default memory manager when it
149 * becomes necessary. Virtual memory objects
150 * that depend on the default memory manager are called
151 * "internal". The "pager_created" field is provided to
152 * indicate whether these ports have ever been allocated.
154 * The kernel may also create virtual memory objects to
155 * hold changed pages after a copy-on-write operation.
156 * In this case, the virtual memory object (and its
157 * backing storage -- its memory object) only contain
158 * those pages that have been changed. The "shadow"
159 * field refers to the virtual memory object that contains
160 * the remainder of the contents. The "shadow_offset"
161 * field indicates where in the "shadow" these contents begin.
162 * The "copy" field refers to a virtual memory object
163 * to which changed pages must be copied before changing
164 * this object, in order to implement another form
165 * of copy-on-write optimization.
167 * The virtual memory object structure also records
168 * the attributes associated with its memory object.
169 * The "pager_ready", "can_persist" and "copy_strategy"
170 * fields represent those attributes. The "cached_list"
171 * field is used in the implementation of the persistence
174 * ZZZ Continue this comment.
177 /* Forward declarations for internal functions. */
178 static kern_return_t
vm_object_terminate(
181 extern void vm_object_remove(
184 static kern_return_t
vm_object_copy_call(
185 vm_object_t src_object
,
186 vm_object_offset_t src_offset
,
187 vm_object_size_t size
,
188 vm_object_t
*_result_object
);
190 static void vm_object_do_collapse(
192 vm_object_t backing_object
);
194 static void vm_object_do_bypass(
196 vm_object_t backing_object
);
198 static void vm_object_release_pager(
199 memory_object_t pager
,
202 static zone_t vm_object_zone
; /* vm backing store zone */
205 * All wired-down kernel memory belongs to a single virtual
206 * memory object (kernel_object) to avoid wasting data structures.
208 static struct vm_object kernel_object_store
;
209 vm_object_t kernel_object
;
211 static struct vm_object compressor_object_store
;
212 vm_object_t compressor_object
= &compressor_object_store
;
215 * The submap object is used as a placeholder for vm_map_submap
216 * operations. The object is declared in vm_map.c because it
217 * is exported by the vm_map module. The storage is declared
218 * here because it must be initialized here.
220 static struct vm_object vm_submap_object_store
;
223 * Virtual memory objects are initialized from
224 * a template (see vm_object_allocate).
226 * When adding a new field to the virtual memory
227 * object structure, be sure to add initialization
228 * (see _vm_object_allocate()).
230 static struct vm_object vm_object_template
;
232 unsigned int vm_page_purged_wired
= 0;
233 unsigned int vm_page_purged_busy
= 0;
234 unsigned int vm_page_purged_others
= 0;
238 * Virtual memory objects that are not referenced by
239 * any address maps, but that are allowed to persist
240 * (an attribute specified by the associated memory manager),
241 * are kept in a queue (vm_object_cached_list).
243 * When an object from this queue is referenced again,
244 * for example to make another address space mapping,
245 * it must be removed from the queue. That is, the
246 * queue contains *only* objects with zero references.
248 * The kernel may choose to terminate objects from this
249 * queue in order to reclaim storage. The current policy
250 * is to permit a fixed maximum number of unreferenced
251 * objects (vm_object_cached_max).
253 * A spin lock (accessed by routines
254 * vm_object_cache_{lock,lock_try,unlock}) governs the
255 * object cache. It must be held when objects are
256 * added to or removed from the cache (in vm_object_terminate).
257 * The routines that acquire a reference to a virtual
258 * memory object based on one of the memory object ports
259 * must also lock the cache.
261 * Ideally, the object cache should be more isolated
262 * from the reference mechanism, so that the lock need
263 * not be held to make simple references.
265 static vm_object_t
vm_object_cache_trim(
266 boolean_t called_from_vm_object_deallocate
);
268 static void vm_object_deactivate_all_pages(
271 static int vm_object_cached_high
; /* highest # cached objects */
272 static int vm_object_cached_max
= 512; /* may be patched*/
274 #define vm_object_cache_lock() \
275 lck_mtx_lock(&vm_object_cached_lock_data)
276 #define vm_object_cache_lock_try() \
277 lck_mtx_try_lock(&vm_object_cached_lock_data)
279 #endif /* VM_OBJECT_CACHE */
281 static queue_head_t vm_object_cached_list
;
282 static uint32_t vm_object_cache_pages_freed
= 0;
283 static uint32_t vm_object_cache_pages_moved
= 0;
284 static uint32_t vm_object_cache_pages_skipped
= 0;
285 static uint32_t vm_object_cache_adds
= 0;
286 static uint32_t vm_object_cached_count
= 0;
287 static lck_mtx_t vm_object_cached_lock_data
;
288 static lck_mtx_ext_t vm_object_cached_lock_data_ext
;
290 static uint32_t vm_object_page_grab_failed
= 0;
291 static uint32_t vm_object_page_grab_skipped
= 0;
292 static uint32_t vm_object_page_grab_returned
= 0;
293 static uint32_t vm_object_page_grab_pmapped
= 0;
294 static uint32_t vm_object_page_grab_reactivations
= 0;
296 #define vm_object_cache_lock_spin() \
297 lck_mtx_lock_spin(&vm_object_cached_lock_data)
298 #define vm_object_cache_unlock() \
299 lck_mtx_unlock(&vm_object_cached_lock_data)
301 static void vm_object_cache_remove_locked(vm_object_t
);
304 #define VM_OBJECT_HASH_COUNT 1024
305 #define VM_OBJECT_HASH_LOCK_COUNT 512
307 static lck_mtx_t vm_object_hashed_lock_data
[VM_OBJECT_HASH_LOCK_COUNT
];
308 static lck_mtx_ext_t vm_object_hashed_lock_data_ext
[VM_OBJECT_HASH_LOCK_COUNT
];
310 static queue_head_t vm_object_hashtable
[VM_OBJECT_HASH_COUNT
];
311 static struct zone
*vm_object_hash_zone
;
313 struct vm_object_hash_entry
{
314 queue_chain_t hash_link
; /* hash chain link */
315 memory_object_t pager
; /* pager we represent */
316 vm_object_t object
; /* corresponding object */
317 boolean_t waiting
; /* someone waiting for
321 typedef struct vm_object_hash_entry
*vm_object_hash_entry_t
;
322 #define VM_OBJECT_HASH_ENTRY_NULL ((vm_object_hash_entry_t) 0)
324 #define VM_OBJECT_HASH_SHIFT 5
325 #define vm_object_hash(pager) \
326 ((int)((((uintptr_t)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_COUNT))
328 #define vm_object_lock_hash(pager) \
329 ((int)((((uintptr_t)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_LOCK_COUNT))
331 void vm_object_hash_entry_free(
332 vm_object_hash_entry_t entry
);
334 static void vm_object_reap(vm_object_t object
);
335 static void vm_object_reap_async(vm_object_t object
);
336 static void vm_object_reaper_thread(void);
338 static lck_mtx_t vm_object_reaper_lock_data
;
339 static lck_mtx_ext_t vm_object_reaper_lock_data_ext
;
341 static queue_head_t vm_object_reaper_queue
; /* protected by vm_object_reaper_lock() */
342 unsigned int vm_object_reap_count
= 0;
343 unsigned int vm_object_reap_count_async
= 0;
345 #define vm_object_reaper_lock() \
346 lck_mtx_lock(&vm_object_reaper_lock_data)
347 #define vm_object_reaper_lock_spin() \
348 lck_mtx_lock_spin(&vm_object_reaper_lock_data)
349 #define vm_object_reaper_unlock() \
350 lck_mtx_unlock(&vm_object_reaper_lock_data)
354 #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
359 vm_object_hash_lock_spin(
360 memory_object_t pager
)
364 index
= vm_object_lock_hash(pager
);
366 lck_mtx_lock_spin(&vm_object_hashed_lock_data
[index
]);
368 return (&vm_object_hashed_lock_data
[index
]);
372 vm_object_hash_unlock(lck_mtx_t
*lck
)
379 * vm_object_hash_lookup looks up a pager in the hashtable
380 * and returns the corresponding entry, with optional removal.
382 static vm_object_hash_entry_t
383 vm_object_hash_lookup(
384 memory_object_t pager
,
385 boolean_t remove_entry
)
388 vm_object_hash_entry_t entry
;
390 bucket
= &vm_object_hashtable
[vm_object_hash(pager
)];
392 entry
= (vm_object_hash_entry_t
)queue_first(bucket
);
393 while (!queue_end(bucket
, (queue_entry_t
)entry
)) {
394 if (entry
->pager
== pager
) {
396 queue_remove(bucket
, entry
,
397 vm_object_hash_entry_t
, hash_link
);
401 entry
= (vm_object_hash_entry_t
)queue_next(&entry
->hash_link
);
403 return(VM_OBJECT_HASH_ENTRY_NULL
);
407 * vm_object_hash_enter enters the specified
408 * pager / cache object association in the hashtable.
412 vm_object_hash_insert(
413 vm_object_hash_entry_t entry
,
418 bucket
= &vm_object_hashtable
[vm_object_hash(entry
->pager
)];
420 queue_enter(bucket
, entry
, vm_object_hash_entry_t
, hash_link
);
422 entry
->object
= object
;
423 object
->hashed
= TRUE
;
426 static vm_object_hash_entry_t
427 vm_object_hash_entry_alloc(
428 memory_object_t pager
)
430 vm_object_hash_entry_t entry
;
432 entry
= (vm_object_hash_entry_t
)zalloc(vm_object_hash_zone
);
433 entry
->pager
= pager
;
434 entry
->object
= VM_OBJECT_NULL
;
435 entry
->waiting
= FALSE
;
441 vm_object_hash_entry_free(
442 vm_object_hash_entry_t entry
)
444 zfree(vm_object_hash_zone
, entry
);
448 * vm_object_allocate:
450 * Returns a new object with the given size.
453 __private_extern__
void
455 vm_object_size_t size
,
459 "vm_object_allocate, object 0x%X size 0x%X\n",
460 object
, size
, 0,0,0);
462 *object
= vm_object_template
;
463 queue_init(&object
->memq
);
464 queue_init(&object
->msr_q
);
466 queue_init(&object
->uplq
);
467 #endif /* UPL_DEBUG */
468 vm_object_lock_init(object
);
469 object
->vo_size
= size
;
472 __private_extern__ vm_object_t
474 vm_object_size_t size
)
476 register vm_object_t object
;
478 object
= (vm_object_t
) zalloc(vm_object_zone
);
480 // dbgLog(object, size, 0, 2); /* (TEST/DEBUG) */
482 if (object
!= VM_OBJECT_NULL
)
483 _vm_object_allocate(size
, object
);
489 lck_grp_t vm_object_lck_grp
;
490 lck_grp_t vm_object_cache_lck_grp
;
491 lck_grp_attr_t vm_object_lck_grp_attr
;
492 lck_attr_t vm_object_lck_attr
;
493 lck_attr_t kernel_object_lck_attr
;
494 lck_attr_t compressor_object_lck_attr
;
497 * vm_object_bootstrap:
499 * Initialize the VM objects module.
501 __private_extern__
void
502 vm_object_bootstrap(void)
506 vm_object_zone
= zinit((vm_size_t
) sizeof(struct vm_object
),
507 round_page(512*1024),
510 zone_change(vm_object_zone
, Z_CALLERACCT
, FALSE
); /* don't charge caller */
511 zone_change(vm_object_zone
, Z_NOENCRYPT
, TRUE
);
513 vm_object_init_lck_grp();
515 queue_init(&vm_object_cached_list
);
517 lck_mtx_init_ext(&vm_object_cached_lock_data
,
518 &vm_object_cached_lock_data_ext
,
519 &vm_object_cache_lck_grp
,
520 &vm_object_lck_attr
);
522 queue_init(&vm_object_reaper_queue
);
524 for (i
= 0; i
< VM_OBJECT_HASH_LOCK_COUNT
; i
++) {
525 lck_mtx_init_ext(&vm_object_hashed_lock_data
[i
],
526 &vm_object_hashed_lock_data_ext
[i
],
528 &vm_object_lck_attr
);
530 lck_mtx_init_ext(&vm_object_reaper_lock_data
,
531 &vm_object_reaper_lock_data_ext
,
533 &vm_object_lck_attr
);
535 vm_object_hash_zone
=
536 zinit((vm_size_t
) sizeof (struct vm_object_hash_entry
),
537 round_page(512*1024),
539 "vm object hash entries");
540 zone_change(vm_object_hash_zone
, Z_CALLERACCT
, FALSE
);
541 zone_change(vm_object_hash_zone
, Z_NOENCRYPT
, TRUE
);
543 for (i
= 0; i
< VM_OBJECT_HASH_COUNT
; i
++)
544 queue_init(&vm_object_hashtable
[i
]);
548 * Fill in a template object, for quick initialization
551 /* memq; Lock; init after allocation */
552 vm_object_template
.memq
.prev
= NULL
;
553 vm_object_template
.memq
.next
= NULL
;
556 * We can't call vm_object_lock_init() here because that will
557 * allocate some memory and VM is not fully initialized yet.
558 * The lock will be initialized for each allocated object in
559 * _vm_object_allocate(), so we don't need to initialize it in
560 * the vm_object_template.
562 vm_object_lock_init(&vm_object_template
);
564 vm_object_template
.vo_size
= 0;
565 vm_object_template
.memq_hint
= VM_PAGE_NULL
;
566 vm_object_template
.ref_count
= 1;
568 vm_object_template
.res_count
= 1;
569 #endif /* TASK_SWAPPER */
570 vm_object_template
.resident_page_count
= 0;
571 vm_object_template
.wired_page_count
= 0;
572 vm_object_template
.reusable_page_count
= 0;
573 vm_object_template
.copy
= VM_OBJECT_NULL
;
574 vm_object_template
.shadow
= VM_OBJECT_NULL
;
575 vm_object_template
.vo_shadow_offset
= (vm_object_offset_t
) 0;
576 vm_object_template
.pager
= MEMORY_OBJECT_NULL
;
577 vm_object_template
.paging_offset
= 0;
578 vm_object_template
.pager_control
= MEMORY_OBJECT_CONTROL_NULL
;
579 vm_object_template
.copy_strategy
= MEMORY_OBJECT_COPY_SYMMETRIC
;
580 vm_object_template
.paging_in_progress
= 0;
581 vm_object_template
.activity_in_progress
= 0;
583 /* Begin bitfields */
584 vm_object_template
.all_wanted
= 0; /* all bits FALSE */
585 vm_object_template
.pager_created
= FALSE
;
586 vm_object_template
.pager_initialized
= FALSE
;
587 vm_object_template
.pager_ready
= FALSE
;
588 vm_object_template
.pager_trusted
= FALSE
;
589 vm_object_template
.can_persist
= FALSE
;
590 vm_object_template
.internal
= TRUE
;
591 vm_object_template
.temporary
= TRUE
;
592 vm_object_template
.private = FALSE
;
593 vm_object_template
.pageout
= FALSE
;
594 vm_object_template
.alive
= TRUE
;
595 vm_object_template
.purgable
= VM_PURGABLE_DENY
;
596 vm_object_template
.purgeable_when_ripe
= FALSE
;
597 vm_object_template
.shadowed
= FALSE
;
598 vm_object_template
.advisory_pageout
= FALSE
;
599 vm_object_template
.true_share
= FALSE
;
600 vm_object_template
.terminating
= FALSE
;
601 vm_object_template
.named
= FALSE
;
602 vm_object_template
.shadow_severed
= FALSE
;
603 vm_object_template
.phys_contiguous
= FALSE
;
604 vm_object_template
.nophyscache
= FALSE
;
607 vm_object_template
.cached_list
.prev
= NULL
;
608 vm_object_template
.cached_list
.next
= NULL
;
609 vm_object_template
.msr_q
.prev
= NULL
;
610 vm_object_template
.msr_q
.next
= NULL
;
612 vm_object_template
.last_alloc
= (vm_object_offset_t
) 0;
613 vm_object_template
.sequential
= (vm_object_offset_t
) 0;
614 vm_object_template
.pages_created
= 0;
615 vm_object_template
.pages_used
= 0;
616 vm_object_template
.scan_collisions
= 0;
619 vm_object_template
.existence_map
= VM_EXTERNAL_NULL
;
620 #endif /* MACH_PAGEMAP */
621 vm_object_template
.cow_hint
= ~(vm_offset_t
)0;
623 vm_object_template
.paging_object
= VM_OBJECT_NULL
;
624 #endif /* MACH_ASSERT */
626 /* cache bitfields */
627 vm_object_template
.wimg_bits
= VM_WIMG_USE_DEFAULT
;
628 vm_object_template
.set_cache_attr
= FALSE
;
629 vm_object_template
.object_slid
= FALSE
;
630 vm_object_template
.code_signed
= FALSE
;
631 vm_object_template
.hashed
= FALSE
;
632 vm_object_template
.transposed
= FALSE
;
633 vm_object_template
.mapping_in_progress
= FALSE
;
634 vm_object_template
.volatile_empty
= FALSE
;
635 vm_object_template
.volatile_fault
= FALSE
;
636 vm_object_template
.all_reusable
= FALSE
;
637 vm_object_template
.blocked_access
= FALSE
;
638 vm_object_template
.__object2_unused_bits
= 0;
640 vm_object_template
.uplq
.prev
= NULL
;
641 vm_object_template
.uplq
.next
= NULL
;
642 #endif /* UPL_DEBUG */
644 bzero(&vm_object_template
.pip_holders
,
645 sizeof (vm_object_template
.pip_holders
));
646 #endif /* VM_PIP_DEBUG */
648 vm_object_template
.objq
.next
=NULL
;
649 vm_object_template
.objq
.prev
=NULL
;
651 vm_object_template
.purgeable_queue_type
= PURGEABLE_Q_TYPE_MAX
;
652 vm_object_template
.purgeable_queue_group
= 0;
654 vm_object_template
.vo_cache_ts
= 0;
657 * Initialize the "kernel object"
660 kernel_object
= &kernel_object_store
;
663 * Note that in the following size specifications, we need to add 1 because
664 * VM_MAX_KERNEL_ADDRESS (vm_last_addr) is a maximum address, not a size.
668 _vm_object_allocate(vm_last_addr
+ 1,
671 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
674 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
677 kernel_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
678 compressor_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
681 * Initialize the "submap object". Make it as large as the
682 * kernel object so that no limit is imposed on submap sizes.
685 vm_submap_object
= &vm_submap_object_store
;
687 _vm_object_allocate(vm_last_addr
+ 1,
690 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
693 vm_submap_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
696 * Create an "extra" reference to this object so that we never
697 * try to deallocate it; zfree doesn't like to be called with
700 vm_object_reference(vm_submap_object
);
703 vm_external_module_initialize();
704 #endif /* MACH_PAGEMAP */
708 vm_object_reaper_init(void)
713 kr
= kernel_thread_start_priority(
714 (thread_continue_t
) vm_object_reaper_thread
,
718 if (kr
!= KERN_SUCCESS
) {
719 panic("failed to launch vm_object_reaper_thread kr=0x%x", kr
);
721 thread_deallocate(thread
);
724 __private_extern__
void
728 * Finish initializing the kernel object.
733 __private_extern__
void
734 vm_object_init_lck_grp(void)
737 * initialze the vm_object lock world
739 lck_grp_attr_setdefault(&vm_object_lck_grp_attr
);
740 lck_grp_init(&vm_object_lck_grp
, "vm_object", &vm_object_lck_grp_attr
);
741 lck_grp_init(&vm_object_cache_lck_grp
, "vm_object_cache", &vm_object_lck_grp_attr
);
742 lck_attr_setdefault(&vm_object_lck_attr
);
743 lck_attr_setdefault(&kernel_object_lck_attr
);
744 lck_attr_cleardebug(&kernel_object_lck_attr
);
745 lck_attr_setdefault(&compressor_object_lck_attr
);
746 lck_attr_cleardebug(&compressor_object_lck_attr
);
750 #define MIGHT_NOT_CACHE_SHADOWS 1
751 #if MIGHT_NOT_CACHE_SHADOWS
752 static int cache_shadows
= TRUE
;
753 #endif /* MIGHT_NOT_CACHE_SHADOWS */
757 * vm_object_deallocate:
759 * Release a reference to the specified object,
760 * gained either through a vm_object_allocate
761 * or a vm_object_reference call. When all references
762 * are gone, storage associated with this object
763 * may be relinquished.
765 * No object may be locked.
767 unsigned long vm_object_deallocate_shared_successes
= 0;
768 unsigned long vm_object_deallocate_shared_failures
= 0;
769 unsigned long vm_object_deallocate_shared_swap_failures
= 0;
770 __private_extern__
void
771 vm_object_deallocate(
772 register vm_object_t object
)
775 boolean_t retry_cache_trim
= FALSE
;
776 uint32_t try_failed_count
= 0;
778 vm_object_t shadow
= VM_OBJECT_NULL
;
780 // if(object)dbgLog(object, object->ref_count, object->can_persist, 3); /* (TEST/DEBUG) */
781 // else dbgLog(object, 0, 0, 3); /* (TEST/DEBUG) */
783 if (object
== VM_OBJECT_NULL
)
786 if (object
== kernel_object
|| object
== compressor_object
) {
787 vm_object_lock_shared(object
);
789 OSAddAtomic(-1, &object
->ref_count
);
791 if (object
->ref_count
== 0) {
792 if (object
== kernel_object
)
793 panic("vm_object_deallocate: losing kernel_object\n");
795 panic("vm_object_deallocate: losing compressor_object\n");
797 vm_object_unlock(object
);
801 if (object
->ref_count
> 2 ||
802 (!object
->named
&& object
->ref_count
> 1)) {
803 UInt32 original_ref_count
;
804 volatile UInt32
*ref_count_p
;
808 * The object currently looks like it is not being
809 * kept alive solely by the reference we're about to release.
810 * Let's try and release our reference without taking
811 * all the locks we would need if we had to terminate the
812 * object (cache lock + exclusive object lock).
813 * Lock the object "shared" to make sure we don't race with
814 * anyone holding it "exclusive".
816 vm_object_lock_shared(object
);
817 ref_count_p
= (volatile UInt32
*) &object
->ref_count
;
818 original_ref_count
= object
->ref_count
;
820 * Test again as "ref_count" could have changed.
821 * "named" shouldn't change.
823 if (original_ref_count
> 2 ||
824 (!object
->named
&& original_ref_count
> 1)) {
825 atomic_swap
= OSCompareAndSwap(
827 original_ref_count
- 1,
828 (UInt32
*) &object
->ref_count
);
829 if (atomic_swap
== FALSE
) {
830 vm_object_deallocate_shared_swap_failures
++;
836 vm_object_unlock(object
);
840 * ref_count was updated atomically !
842 vm_object_deallocate_shared_successes
++;
847 * Someone else updated the ref_count at the same
848 * time and we lost the race. Fall back to the usual
849 * slow but safe path...
851 vm_object_deallocate_shared_failures
++;
854 while (object
!= VM_OBJECT_NULL
) {
856 vm_object_lock(object
);
858 assert(object
->ref_count
> 0);
861 * If the object has a named reference, and only
862 * that reference would remain, inform the pager
863 * about the last "mapping" reference going away.
865 if ((object
->ref_count
== 2) && (object
->named
)) {
866 memory_object_t pager
= object
->pager
;
868 /* Notify the Pager that there are no */
869 /* more mappers for this object */
871 if (pager
!= MEMORY_OBJECT_NULL
) {
872 vm_object_mapping_wait(object
, THREAD_UNINT
);
873 vm_object_mapping_begin(object
);
874 vm_object_unlock(object
);
876 memory_object_last_unmap(pager
);
878 vm_object_lock(object
);
879 vm_object_mapping_end(object
);
881 assert(object
->ref_count
> 0);
885 * Lose the reference. If other references
886 * remain, then we are done, unless we need
887 * to retry a cache trim.
888 * If it is the last reference, then keep it
889 * until any pending initialization is completed.
892 /* if the object is terminating, it cannot go into */
893 /* the cache and we obviously should not call */
894 /* terminate again. */
896 if ((object
->ref_count
> 1) || object
->terminating
) {
897 vm_object_lock_assert_exclusive(object
);
899 vm_object_res_deallocate(object
);
901 if (object
->ref_count
== 1 &&
902 object
->shadow
!= VM_OBJECT_NULL
) {
904 * There's only one reference left on this
905 * VM object. We can't tell if it's a valid
906 * one (from a mapping for example) or if this
907 * object is just part of a possibly stale and
908 * useless shadow chain.
909 * We would like to try and collapse it into
910 * its parent, but we don't have any pointers
911 * back to this parent object.
912 * But we can try and collapse this object with
913 * its own shadows, in case these are useless
915 * We can't bypass this object though, since we
916 * don't know if this last reference on it is
919 vm_object_collapse(object
, 0, FALSE
);
921 vm_object_unlock(object
);
923 if (retry_cache_trim
&&
924 ((object
= vm_object_cache_trim(TRUE
)) !=
933 * We have to wait for initialization
934 * before destroying or caching the object.
937 if (object
->pager_created
&& ! object
->pager_initialized
) {
938 assert(! object
->can_persist
);
939 vm_object_assert_wait(object
,
940 VM_OBJECT_EVENT_INITIALIZED
,
942 vm_object_unlock(object
);
944 thread_block(THREAD_CONTINUE_NULL
);
950 * If this object can persist, then enter it in
951 * the cache. Otherwise, terminate it.
953 * NOTE: Only permanent objects are cached, and
954 * permanent objects cannot have shadows. This
955 * affects the residence counting logic in a minor
956 * way (can do it in-line, mostly).
959 if ((object
->can_persist
) && (object
->alive
)) {
961 * Now it is safe to decrement reference count,
962 * and to return if reference count is > 0.
965 vm_object_lock_assert_exclusive(object
);
966 if (--object
->ref_count
> 0) {
967 vm_object_res_deallocate(object
);
968 vm_object_unlock(object
);
970 if (retry_cache_trim
&&
971 ((object
= vm_object_cache_trim(TRUE
)) !=
978 #if MIGHT_NOT_CACHE_SHADOWS
980 * Remove shadow now if we don't
981 * want to cache shadows.
983 if (! cache_shadows
) {
984 shadow
= object
->shadow
;
985 object
->shadow
= VM_OBJECT_NULL
;
987 #endif /* MIGHT_NOT_CACHE_SHADOWS */
990 * Enter the object onto the queue of
991 * cached objects, and deactivate
994 assert(object
->shadow
== VM_OBJECT_NULL
);
995 VM_OBJ_RES_DECR(object
);
997 "vm_o_deallocate: adding %x to cache, queue = (%x, %x)\n",
999 vm_object_cached_list
.next
,
1000 vm_object_cached_list
.prev
,0,0);
1003 vm_object_unlock(object
);
1005 try_failed_count
= 0;
1007 vm_object_cache_lock();
1010 * if we try to take a regular lock here
1011 * we risk deadlocking against someone
1012 * holding a lock on this object while
1013 * trying to vm_object_deallocate a different
1016 if (vm_object_lock_try(object
))
1018 vm_object_cache_unlock();
1021 mutex_pause(try_failed_count
); /* wait a bit */
1023 vm_object_cached_count
++;
1024 if (vm_object_cached_count
> vm_object_cached_high
)
1025 vm_object_cached_high
= vm_object_cached_count
;
1026 queue_enter(&vm_object_cached_list
, object
,
1027 vm_object_t
, cached_list
);
1028 vm_object_cache_unlock();
1030 vm_object_deactivate_all_pages(object
);
1031 vm_object_unlock(object
);
1033 #if MIGHT_NOT_CACHE_SHADOWS
1035 * If we have a shadow that we need
1036 * to deallocate, do so now, remembering
1037 * to trim the cache later.
1039 if (! cache_shadows
&& shadow
!= VM_OBJECT_NULL
) {
1041 retry_cache_trim
= TRUE
;
1044 #endif /* MIGHT_NOT_CACHE_SHADOWS */
1047 * Trim the cache. If the cache trim
1048 * returns with a shadow for us to deallocate,
1049 * then remember to retry the cache trim
1050 * when we are done deallocating the shadow.
1051 * Otherwise, we are done.
1054 object
= vm_object_cache_trim(TRUE
);
1055 if (object
== VM_OBJECT_NULL
) {
1058 retry_cache_trim
= TRUE
;
1060 #endif /* VM_OBJECT_CACHE */
1063 * This object is not cachable; terminate it.
1066 "vm_o_deallocate: !cacheable 0x%X res %d paging_ops %d thread 0x%p ref %d\n",
1067 object
, object
->resident_page_count
,
1068 object
->paging_in_progress
,
1069 (void *)current_thread(),object
->ref_count
);
1071 VM_OBJ_RES_DECR(object
); /* XXX ? */
1073 * Terminate this object. If it had a shadow,
1074 * then deallocate it; otherwise, if we need
1075 * to retry a cache trim, do so now; otherwise,
1076 * we are done. "pageout" objects have a shadow,
1077 * but maintain a "paging reference" rather than
1078 * a normal reference.
1080 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
1082 if (vm_object_terminate(object
) != KERN_SUCCESS
) {
1085 if (shadow
!= VM_OBJECT_NULL
) {
1090 if (retry_cache_trim
&&
1091 ((object
= vm_object_cache_trim(TRUE
)) !=
1100 assert(! retry_cache_trim
);
1107 vm_object_page_grab(
1110 vm_page_t p
, next_p
;
1114 vm_object_lock_assert_exclusive(object
);
1116 next_p
= (vm_page_t
)queue_first(&object
->memq
);
1117 p_limit
= MIN(50, object
->resident_page_count
);
1119 while (!queue_end(&object
->memq
, (queue_entry_t
)next_p
) && --p_limit
> 0) {
1122 next_p
= (vm_page_t
)queue_next(&next_p
->listq
);
1124 if (VM_PAGE_WIRED(p
) || p
->busy
|| p
->cleaning
|| p
->laundry
|| p
->fictitious
)
1125 goto move_page_in_obj
;
1127 if (p
->pmapped
|| p
->dirty
|| p
->precious
) {
1128 vm_page_lockspin_queues();
1133 vm_object_page_grab_pmapped
++;
1135 if (p
->reference
== FALSE
|| p
->dirty
== FALSE
) {
1137 refmod_state
= pmap_get_refmod(p
->phys_page
);
1139 if (refmod_state
& VM_MEM_REFERENCED
)
1140 p
->reference
= TRUE
;
1141 if (refmod_state
& VM_MEM_MODIFIED
) {
1142 SET_PAGE_DIRTY(p
, FALSE
);
1145 if (p
->dirty
== FALSE
&& p
->precious
== FALSE
) {
1147 refmod_state
= pmap_disconnect(p
->phys_page
);
1149 if (refmod_state
& VM_MEM_REFERENCED
)
1150 p
->reference
= TRUE
;
1151 if (refmod_state
& VM_MEM_MODIFIED
) {
1152 SET_PAGE_DIRTY(p
, FALSE
);
1155 if (p
->dirty
== FALSE
)
1159 if (p
->inactive
&& p
->reference
== TRUE
) {
1160 vm_page_activate(p
);
1162 VM_STAT_INCR(reactivations
);
1163 vm_object_page_grab_reactivations
++;
1165 vm_page_unlock_queues();
1167 queue_remove(&object
->memq
, p
, vm_page_t
, listq
);
1168 queue_enter(&object
->memq
, p
, vm_page_t
, listq
);
1173 vm_page_lockspin_queues();
1175 vm_page_free_prepare_queues(p
);
1176 vm_object_page_grab_returned
++;
1177 vm_object_page_grab_skipped
+= p_skipped
;
1179 vm_page_unlock_queues();
1181 vm_page_free_prepare_object(p
, TRUE
);
1185 vm_object_page_grab_skipped
+= p_skipped
;
1186 vm_object_page_grab_failed
++;
1193 #define EVICT_PREPARE_LIMIT 64
1194 #define EVICT_AGE 10
1196 static clock_sec_t vm_object_cache_aging_ts
= 0;
1199 vm_object_cache_remove_locked(
1202 queue_remove(&vm_object_cached_list
, object
, vm_object_t
, objq
);
1203 object
->objq
.next
= NULL
;
1204 object
->objq
.prev
= NULL
;
1206 vm_object_cached_count
--;
1210 vm_object_cache_remove(
1213 vm_object_cache_lock_spin();
1215 if (object
->objq
.next
|| object
->objq
.prev
)
1216 vm_object_cache_remove_locked(object
);
1218 vm_object_cache_unlock();
1222 vm_object_cache_add(
1228 if (object
->resident_page_count
== 0)
1230 clock_get_system_nanotime(&sec
, &nsec
);
1232 vm_object_cache_lock_spin();
1234 if (object
->objq
.next
== NULL
&& object
->objq
.prev
== NULL
) {
1235 queue_enter(&vm_object_cached_list
, object
, vm_object_t
, objq
);
1236 object
->vo_cache_ts
= sec
+ EVICT_AGE
;
1237 object
->vo_cache_pages_to_scan
= object
->resident_page_count
;
1239 vm_object_cached_count
++;
1240 vm_object_cache_adds
++;
1242 vm_object_cache_unlock();
1246 vm_object_cache_evict(
1248 int max_objects_to_examine
)
1250 vm_object_t object
= VM_OBJECT_NULL
;
1251 vm_object_t next_obj
= VM_OBJECT_NULL
;
1252 vm_page_t local_free_q
= VM_PAGE_NULL
;
1256 vm_page_t ep_array
[EVICT_PREPARE_LIMIT
];
1262 uint32_t ep_skipped
= 0;
1266 KERNEL_DEBUG(0x13001ec | DBG_FUNC_START
, 0, 0, 0, 0, 0);
1268 * do a couple of quick checks to see if it's
1269 * worthwhile grabbing the lock
1271 if (queue_empty(&vm_object_cached_list
)) {
1272 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END
, 0, 0, 0, 0, 0);
1275 clock_get_system_nanotime(&sec
, &nsec
);
1278 * the object on the head of the queue has not
1279 * yet sufficiently aged
1281 if (sec
< vm_object_cache_aging_ts
) {
1282 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END
, 0, 0, 0, 0, 0);
1286 * don't need the queue lock to find
1287 * and lock an object on the cached list
1289 vm_page_unlock_queues();
1291 vm_object_cache_lock_spin();
1294 next_obj
= (vm_object_t
)queue_first(&vm_object_cached_list
);
1296 while (!queue_end(&vm_object_cached_list
, (queue_entry_t
)next_obj
) && object_cnt
++ < max_objects_to_examine
) {
1299 next_obj
= (vm_object_t
)queue_next(&next_obj
->objq
);
1301 if (sec
< object
->vo_cache_ts
) {
1302 KERNEL_DEBUG(0x130020c, object
, object
->resident_page_count
, object
->vo_cache_ts
, sec
, 0);
1304 vm_object_cache_aging_ts
= object
->vo_cache_ts
;
1305 object
= VM_OBJECT_NULL
;
1308 if (!vm_object_lock_try_scan(object
)) {
1310 * just skip over this guy for now... if we find
1311 * an object to steal pages from, we'll revist in a bit...
1312 * hopefully, the lock will have cleared
1314 KERNEL_DEBUG(0x13001f8, object
, object
->resident_page_count
, 0, 0, 0);
1316 object
= VM_OBJECT_NULL
;
1319 if (queue_empty(&object
->memq
) || object
->vo_cache_pages_to_scan
== 0) {
1321 * this case really shouldn't happen, but it's not fatal
1322 * so deal with it... if we don't remove the object from
1323 * the list, we'll never move past it.
1325 KERNEL_DEBUG(0x13001fc, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1327 vm_object_cache_remove_locked(object
);
1328 vm_object_unlock(object
);
1329 object
= VM_OBJECT_NULL
;
1333 * we have a locked object with pages...
1334 * time to start harvesting
1338 vm_object_cache_unlock();
1340 if (object
== VM_OBJECT_NULL
)
1344 * object is locked at this point and
1345 * has resident pages
1347 next_p
= (vm_page_t
)queue_first(&object
->memq
);
1350 * break the page scan into 2 pieces to minimize the time spent
1351 * behind the page queue lock...
1352 * the list of pages on these unused objects is likely to be cold
1353 * w/r to the cpu cache which increases the time to scan the list
1354 * tenfold... and we may have a 'run' of pages we can't utilize that
1355 * needs to be skipped over...
1357 if ((ep_limit
= num_to_evict
- (ep_freed
+ ep_moved
)) > EVICT_PREPARE_LIMIT
)
1358 ep_limit
= EVICT_PREPARE_LIMIT
;
1361 while (!queue_end(&object
->memq
, (queue_entry_t
)next_p
) && object
->vo_cache_pages_to_scan
&& ep_count
< ep_limit
) {
1364 next_p
= (vm_page_t
)queue_next(&next_p
->listq
);
1366 object
->vo_cache_pages_to_scan
--;
1368 if (VM_PAGE_WIRED(p
) || p
->busy
|| p
->cleaning
|| p
->laundry
) {
1369 queue_remove(&object
->memq
, p
, vm_page_t
, listq
);
1370 queue_enter(&object
->memq
, p
, vm_page_t
, listq
);
1375 if (p
->wpmapped
|| p
->dirty
|| p
->precious
) {
1376 queue_remove(&object
->memq
, p
, vm_page_t
, listq
);
1377 queue_enter(&object
->memq
, p
, vm_page_t
, listq
);
1379 pmap_clear_reference(p
->phys_page
);
1381 ep_array
[ep_count
++] = p
;
1383 KERNEL_DEBUG(0x13001f4 | DBG_FUNC_START
, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1385 vm_page_lockspin_queues();
1387 for (ep_index
= 0; ep_index
< ep_count
; ep_index
++) {
1389 p
= ep_array
[ep_index
];
1391 if (p
->wpmapped
|| p
->dirty
|| p
->precious
) {
1392 p
->reference
= FALSE
;
1393 p
->no_cache
= FALSE
;
1396 * we've already filtered out pages that are in the laundry
1397 * so if we get here, this page can't be on the pageout queue
1399 assert(!p
->pageout_queue
);
1401 VM_PAGE_QUEUES_REMOVE(p
);
1402 VM_PAGE_ENQUEUE_INACTIVE(p
, TRUE
);
1406 vm_page_free_prepare_queues(p
);
1408 assert(p
->pageq
.next
== NULL
&& p
->pageq
.prev
== NULL
);
1410 * Add this page to our list of reclaimed pages,
1411 * to be freed later.
1413 p
->pageq
.next
= (queue_entry_t
) local_free_q
;
1419 vm_page_unlock_queues();
1421 KERNEL_DEBUG(0x13001f4 | DBG_FUNC_END
, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1424 vm_page_free_list(local_free_q
, TRUE
);
1425 local_free_q
= VM_PAGE_NULL
;
1427 if (object
->vo_cache_pages_to_scan
== 0) {
1428 KERNEL_DEBUG(0x1300208, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1430 vm_object_cache_remove(object
);
1432 KERNEL_DEBUG(0x13001fc, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1435 * done with this object
1437 vm_object_unlock(object
);
1438 object
= VM_OBJECT_NULL
;
1441 * at this point, we are not holding any locks
1443 if ((ep_freed
+ ep_moved
) >= num_to_evict
) {
1445 * we've reached our target for the
1446 * number of pages to evict
1450 vm_object_cache_lock_spin();
1453 * put the page queues lock back to the caller's
1456 vm_page_lock_queues();
1458 vm_object_cache_pages_freed
+= ep_freed
;
1459 vm_object_cache_pages_moved
+= ep_moved
;
1460 vm_object_cache_pages_skipped
+= ep_skipped
;
1462 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END
, ep_freed
, 0, 0, 0, 0);
1469 * Check to see whether we really need to trim
1470 * down the cache. If so, remove an object from
1471 * the cache, terminate it, and repeat.
1473 * Called with, and returns with, cache lock unlocked.
1476 vm_object_cache_trim(
1477 boolean_t called_from_vm_object_deallocate
)
1479 register vm_object_t object
= VM_OBJECT_NULL
;
1485 * If we no longer need to trim the cache,
1488 if (vm_object_cached_count
<= vm_object_cached_max
)
1489 return VM_OBJECT_NULL
;
1491 vm_object_cache_lock();
1492 if (vm_object_cached_count
<= vm_object_cached_max
) {
1493 vm_object_cache_unlock();
1494 return VM_OBJECT_NULL
;
1498 * We must trim down the cache, so remove
1499 * the first object in the cache.
1502 "vm_object_cache_trim: removing from front of cache (%x, %x)\n",
1503 vm_object_cached_list
.next
,
1504 vm_object_cached_list
.prev
, 0, 0, 0);
1506 object
= (vm_object_t
) queue_first(&vm_object_cached_list
);
1507 if(object
== (vm_object_t
) &vm_object_cached_list
) {
1508 /* something's wrong with the calling parameter or */
1509 /* the value of vm_object_cached_count, just fix */
1511 if(vm_object_cached_max
< 0)
1512 vm_object_cached_max
= 0;
1513 vm_object_cached_count
= 0;
1514 vm_object_cache_unlock();
1515 return VM_OBJECT_NULL
;
1517 vm_object_lock(object
);
1518 queue_remove(&vm_object_cached_list
, object
, vm_object_t
,
1520 vm_object_cached_count
--;
1522 vm_object_cache_unlock();
1524 * Since this object is in the cache, we know
1525 * that it is initialized and has no references.
1526 * Take a reference to avoid recursive deallocations.
1529 assert(object
->pager_initialized
);
1530 assert(object
->ref_count
== 0);
1531 vm_object_lock_assert_exclusive(object
);
1532 object
->ref_count
++;
1535 * Terminate the object.
1536 * If the object had a shadow, we let vm_object_deallocate
1537 * deallocate it. "pageout" objects have a shadow, but
1538 * maintain a "paging reference" rather than a normal
1540 * (We are careful here to limit recursion.)
1542 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
1544 if(vm_object_terminate(object
) != KERN_SUCCESS
)
1547 if (shadow
!= VM_OBJECT_NULL
) {
1548 if (called_from_vm_object_deallocate
) {
1551 vm_object_deallocate(shadow
);
1560 * Routine: vm_object_terminate
1562 * Free all resources associated with a vm_object.
1563 * In/out conditions:
1564 * Upon entry, the object must be locked,
1565 * and the object must have exactly one reference.
1567 * The shadow object reference is left alone.
1569 * The object must be unlocked if its found that pages
1570 * must be flushed to a backing object. If someone
1571 * manages to map the object while it is being flushed
1572 * the object is returned unlocked and unchanged. Otherwise,
1573 * upon exit, the cache will be unlocked, and the
1574 * object will cease to exist.
1576 static kern_return_t
1577 vm_object_terminate(
1580 vm_object_t shadow_object
;
1582 XPR(XPR_VM_OBJECT
, "vm_object_terminate, object 0x%X ref %d\n",
1583 object
, object
->ref_count
, 0, 0, 0);
1585 if (!object
->pageout
&& (!object
->temporary
|| object
->can_persist
) &&
1586 (object
->pager
!= NULL
|| object
->shadow_severed
)) {
1588 * Clear pager_trusted bit so that the pages get yanked
1589 * out of the object instead of cleaned in place. This
1590 * prevents a deadlock in XMM and makes more sense anyway.
1592 object
->pager_trusted
= FALSE
;
1594 vm_object_reap_pages(object
, REAP_TERMINATE
);
1597 * Make sure the object isn't already being terminated
1599 if (object
->terminating
) {
1600 vm_object_lock_assert_exclusive(object
);
1601 object
->ref_count
--;
1602 assert(object
->ref_count
> 0);
1603 vm_object_unlock(object
);
1604 return KERN_FAILURE
;
1608 * Did somebody get a reference to the object while we were
1611 if (object
->ref_count
!= 1) {
1612 vm_object_lock_assert_exclusive(object
);
1613 object
->ref_count
--;
1614 assert(object
->ref_count
> 0);
1615 vm_object_res_deallocate(object
);
1616 vm_object_unlock(object
);
1617 return KERN_FAILURE
;
1621 * Make sure no one can look us up now.
1624 object
->terminating
= TRUE
;
1625 object
->alive
= FALSE
;
1627 if ( !object
->internal
&& (object
->objq
.next
|| object
->objq
.prev
))
1628 vm_object_cache_remove(object
);
1630 if (object
->hashed
) {
1633 lck
= vm_object_hash_lock_spin(object
->pager
);
1634 vm_object_remove(object
);
1635 vm_object_hash_unlock(lck
);
1638 * Detach the object from its shadow if we are the shadow's
1639 * copy. The reference we hold on the shadow must be dropped
1642 if (((shadow_object
= object
->shadow
) != VM_OBJECT_NULL
) &&
1643 !(object
->pageout
)) {
1644 vm_object_lock(shadow_object
);
1645 if (shadow_object
->copy
== object
)
1646 shadow_object
->copy
= VM_OBJECT_NULL
;
1647 vm_object_unlock(shadow_object
);
1650 if (object
->paging_in_progress
!= 0 ||
1651 object
->activity_in_progress
!= 0) {
1653 * There are still some paging_in_progress references
1654 * on this object, meaning that there are some paging
1655 * or other I/O operations in progress for this VM object.
1656 * Such operations take some paging_in_progress references
1657 * up front to ensure that the object doesn't go away, but
1658 * they may also need to acquire a reference on the VM object,
1659 * to map it in kernel space, for example. That means that
1660 * they may end up releasing the last reference on the VM
1661 * object, triggering its termination, while still holding
1662 * paging_in_progress references. Waiting for these
1663 * pending paging_in_progress references to go away here would
1666 * To avoid deadlocking, we'll let the vm_object_reaper_thread
1667 * complete the VM object termination if it still holds
1668 * paging_in_progress references at this point.
1670 * No new paging_in_progress should appear now that the
1671 * VM object is "terminating" and not "alive".
1673 vm_object_reap_async(object
);
1674 vm_object_unlock(object
);
1676 * Return KERN_FAILURE to let the caller know that we
1677 * haven't completed the termination and it can't drop this
1678 * object's reference on its shadow object yet.
1679 * The reaper thread will take care of that once it has
1680 * completed this object's termination.
1682 return KERN_FAILURE
;
1685 * complete the VM object termination
1687 vm_object_reap(object
);
1688 object
= VM_OBJECT_NULL
;
1691 * the object lock was released by vm_object_reap()
1693 * KERN_SUCCESS means that this object has been terminated
1694 * and no longer needs its shadow object but still holds a
1696 * The caller is responsible for dropping that reference.
1697 * We can't call vm_object_deallocate() here because that
1698 * would create a recursion.
1700 return KERN_SUCCESS
;
1707 * Complete the termination of a VM object after it's been marked
1708 * as "terminating" and "!alive" by vm_object_terminate().
1710 * The VM object must be locked by caller.
1711 * The lock will be released on return and the VM object is no longer valid.
1717 memory_object_t pager
;
1719 vm_object_lock_assert_exclusive(object
);
1720 assert(object
->paging_in_progress
== 0);
1721 assert(object
->activity_in_progress
== 0);
1723 vm_object_reap_count
++;
1725 pager
= object
->pager
;
1726 object
->pager
= MEMORY_OBJECT_NULL
;
1728 if (pager
!= MEMORY_OBJECT_NULL
)
1729 memory_object_control_disable(object
->pager_control
);
1731 object
->ref_count
--;
1733 assert(object
->res_count
== 0);
1734 #endif /* TASK_SWAPPER */
1736 assert (object
->ref_count
== 0);
1739 * remove from purgeable queue if it's on
1741 if (object
->internal
&& (object
->objq
.next
|| object
->objq
.prev
)) {
1742 purgeable_q_t queue
= vm_purgeable_object_remove(object
);
1745 if (object
->purgeable_when_ripe
) {
1747 * Must take page lock for this -
1748 * using it to protect token queue
1750 vm_page_lock_queues();
1751 vm_purgeable_token_delete_first(queue
);
1753 assert(queue
->debug_count_objects
>=0);
1754 vm_page_unlock_queues();
1759 * Clean or free the pages, as appropriate.
1760 * It is possible for us to find busy/absent pages,
1761 * if some faults on this object were aborted.
1763 if (object
->pageout
) {
1764 assert(object
->shadow
!= VM_OBJECT_NULL
);
1766 vm_pageout_object_terminate(object
);
1768 } else if (((object
->temporary
&& !object
->can_persist
) || (pager
== MEMORY_OBJECT_NULL
))) {
1770 vm_object_reap_pages(object
, REAP_REAP
);
1772 assert(queue_empty(&object
->memq
));
1773 assert(object
->paging_in_progress
== 0);
1774 assert(object
->activity_in_progress
== 0);
1775 assert(object
->ref_count
== 0);
1778 * If the pager has not already been released by
1779 * vm_object_destroy, we need to terminate it and
1780 * release our reference to it here.
1782 if (pager
!= MEMORY_OBJECT_NULL
) {
1783 vm_object_unlock(object
);
1784 vm_object_release_pager(pager
, object
->hashed
);
1785 vm_object_lock(object
);
1788 /* kick off anyone waiting on terminating */
1789 object
->terminating
= FALSE
;
1790 vm_object_paging_begin(object
);
1791 vm_object_paging_end(object
);
1792 vm_object_unlock(object
);
1795 vm_external_destroy(object
->existence_map
, object
->vo_size
);
1796 #endif /* MACH_PAGEMAP */
1798 object
->shadow
= VM_OBJECT_NULL
;
1800 vm_object_lock_destroy(object
);
1802 * Free the space for the object.
1804 zfree(vm_object_zone
, object
);
1805 object
= VM_OBJECT_NULL
;
1809 unsigned int vm_max_batch
= 256;
1811 #define V_O_R_MAX_BATCH 128
1813 #define BATCH_LIMIT(max) (vm_max_batch >= max ? max : vm_max_batch)
1816 #define VM_OBJ_REAP_FREELIST(_local_free_q, do_disconnect) \
1818 if (_local_free_q) { \
1819 if (do_disconnect) { \
1821 for (m = _local_free_q; \
1822 m != VM_PAGE_NULL; \
1823 m = (vm_page_t) m->pageq.next) { \
1825 pmap_disconnect(m->phys_page); \
1829 vm_page_free_list(_local_free_q, TRUE); \
1830 _local_free_q = VM_PAGE_NULL; \
1836 vm_object_reap_pages(
1842 vm_page_t local_free_q
= VM_PAGE_NULL
;
1844 boolean_t disconnect_on_release
;
1845 pmap_flush_context pmap_flush_context_storage
;
1847 if (reap_type
== REAP_DATA_FLUSH
) {
1849 * We need to disconnect pages from all pmaps before
1850 * releasing them to the free list
1852 disconnect_on_release
= TRUE
;
1855 * Either the caller has already disconnected the pages
1856 * from all pmaps, or we disconnect them here as we add
1857 * them to out local list of pages to be released.
1858 * No need to re-disconnect them when we release the pages
1861 disconnect_on_release
= FALSE
;
1864 restart_after_sleep
:
1865 if (queue_empty(&object
->memq
))
1867 loop_count
= BATCH_LIMIT(V_O_R_MAX_BATCH
);
1869 if (reap_type
== REAP_PURGEABLE
)
1870 pmap_flush_context_init(&pmap_flush_context_storage
);
1872 vm_page_lockspin_queues();
1874 next
= (vm_page_t
)queue_first(&object
->memq
);
1876 while (!queue_end(&object
->memq
, (queue_entry_t
)next
)) {
1879 next
= (vm_page_t
)queue_next(&next
->listq
);
1881 if (--loop_count
== 0) {
1883 vm_page_unlock_queues();
1887 if (reap_type
== REAP_PURGEABLE
) {
1888 pmap_flush(&pmap_flush_context_storage
);
1889 pmap_flush_context_init(&pmap_flush_context_storage
);
1892 * Free the pages we reclaimed so far
1893 * and take a little break to avoid
1894 * hogging the page queue lock too long
1896 VM_OBJ_REAP_FREELIST(local_free_q
,
1897 disconnect_on_release
);
1901 loop_count
= BATCH_LIMIT(V_O_R_MAX_BATCH
);
1903 vm_page_lockspin_queues();
1905 if (reap_type
== REAP_DATA_FLUSH
|| reap_type
== REAP_TERMINATE
) {
1907 if (p
->busy
|| p
->cleaning
) {
1909 vm_page_unlock_queues();
1911 * free the pages reclaimed so far
1913 VM_OBJ_REAP_FREELIST(local_free_q
,
1914 disconnect_on_release
);
1916 PAGE_SLEEP(object
, p
, THREAD_UNINT
);
1918 goto restart_after_sleep
;
1923 vm_pageout_steal_laundry(p
, TRUE
);
1926 switch (reap_type
) {
1928 case REAP_DATA_FLUSH
:
1929 if (VM_PAGE_WIRED(p
)) {
1931 * this is an odd case... perhaps we should
1932 * zero-fill this page since we're conceptually
1933 * tossing its data at this point, but leaving
1934 * it on the object to honor the 'wire' contract
1940 case REAP_PURGEABLE
:
1941 if (VM_PAGE_WIRED(p
)) {
1943 * can't purge a wired page
1945 vm_page_purged_wired
++;
1948 if (p
->laundry
&& !p
->busy
&& !p
->cleaning
) {
1951 vm_pageout_steal_laundry(p
, TRUE
);
1953 if (p
->cleaning
|| p
->laundry
) {
1955 * page is being acted upon,
1956 * so don't mess with it
1958 vm_page_purged_others
++;
1963 * We can't reclaim a busy page but we can
1964 * make it more likely to be paged (it's not wired) to make
1965 * sure that it gets considered by
1966 * vm_pageout_scan() later.
1968 vm_page_deactivate(p
);
1969 vm_page_purged_busy
++;
1973 assert(p
->object
!= kernel_object
);
1976 * we can discard this page...
1978 if (p
->pmapped
== TRUE
) {
1982 pmap_disconnect_options(p
->phys_page
, PMAP_OPTIONS_NOFLUSH
| PMAP_OPTIONS_NOREFMOD
, (void *)&pmap_flush_context_storage
);
1984 vm_page_purged_count
++;
1988 case REAP_TERMINATE
:
1989 if (p
->absent
|| p
->private) {
1991 * For private pages, VM_PAGE_FREE just
1992 * leaves the page structure around for
1993 * its owner to clean up. For absent
1994 * pages, the structure is returned to
1995 * the appropriate pool.
1999 if (p
->fictitious
) {
2000 assert (p
->phys_page
== vm_page_guard_addr
);
2003 if (!p
->dirty
&& p
->wpmapped
)
2004 p
->dirty
= pmap_is_modified(p
->phys_page
);
2006 if ((p
->dirty
|| p
->precious
) && !p
->error
&& object
->alive
) {
2009 VM_PAGE_QUEUES_REMOVE(p
);
2011 * flush page... page will be freed
2012 * upon completion of I/O
2014 vm_pageout_cluster(p
, TRUE
);
2016 vm_page_unlock_queues();
2018 * free the pages reclaimed so far
2020 VM_OBJ_REAP_FREELIST(local_free_q
,
2021 disconnect_on_release
);
2023 vm_object_paging_wait(object
, THREAD_UNINT
);
2025 goto restart_after_sleep
;
2032 vm_page_free_prepare_queues(p
);
2033 assert(p
->pageq
.next
== NULL
&& p
->pageq
.prev
== NULL
);
2035 * Add this page to our list of reclaimed pages,
2036 * to be freed later.
2038 p
->pageq
.next
= (queue_entry_t
) local_free_q
;
2041 vm_page_unlock_queues();
2044 * Free the remaining reclaimed pages
2046 if (reap_type
== REAP_PURGEABLE
)
2047 pmap_flush(&pmap_flush_context_storage
);
2049 VM_OBJ_REAP_FREELIST(local_free_q
,
2050 disconnect_on_release
);
2055 vm_object_reap_async(
2058 vm_object_lock_assert_exclusive(object
);
2060 vm_object_reaper_lock_spin();
2062 vm_object_reap_count_async
++;
2064 /* enqueue the VM object... */
2065 queue_enter(&vm_object_reaper_queue
, object
,
2066 vm_object_t
, cached_list
);
2068 vm_object_reaper_unlock();
2070 /* ... and wake up the reaper thread */
2071 thread_wakeup((event_t
) &vm_object_reaper_queue
);
2076 vm_object_reaper_thread(void)
2078 vm_object_t object
, shadow_object
;
2080 vm_object_reaper_lock_spin();
2082 while (!queue_empty(&vm_object_reaper_queue
)) {
2083 queue_remove_first(&vm_object_reaper_queue
,
2088 vm_object_reaper_unlock();
2089 vm_object_lock(object
);
2091 assert(object
->terminating
);
2092 assert(!object
->alive
);
2095 * The pageout daemon might be playing with our pages.
2096 * Now that the object is dead, it won't touch any more
2097 * pages, but some pages might already be on their way out.
2098 * Hence, we wait until the active paging activities have
2099 * ceased before we break the association with the pager
2102 while (object
->paging_in_progress
!= 0 ||
2103 object
->activity_in_progress
!= 0) {
2104 vm_object_wait(object
,
2105 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
2107 vm_object_lock(object
);
2111 object
->pageout
? VM_OBJECT_NULL
: object
->shadow
;
2113 vm_object_reap(object
);
2114 /* cache is unlocked and object is no longer valid */
2115 object
= VM_OBJECT_NULL
;
2117 if (shadow_object
!= VM_OBJECT_NULL
) {
2119 * Drop the reference "object" was holding on
2120 * its shadow object.
2122 vm_object_deallocate(shadow_object
);
2123 shadow_object
= VM_OBJECT_NULL
;
2125 vm_object_reaper_lock_spin();
2128 /* wait for more work... */
2129 assert_wait((event_t
) &vm_object_reaper_queue
, THREAD_UNINT
);
2131 vm_object_reaper_unlock();
2133 thread_block((thread_continue_t
) vm_object_reaper_thread
);
2138 * Routine: vm_object_pager_wakeup
2139 * Purpose: Wake up anyone waiting for termination of a pager.
2143 vm_object_pager_wakeup(
2144 memory_object_t pager
)
2146 vm_object_hash_entry_t entry
;
2147 boolean_t waiting
= FALSE
;
2151 * If anyone was waiting for the memory_object_terminate
2152 * to be queued, wake them up now.
2154 lck
= vm_object_hash_lock_spin(pager
);
2155 entry
= vm_object_hash_lookup(pager
, TRUE
);
2156 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
2157 waiting
= entry
->waiting
;
2158 vm_object_hash_unlock(lck
);
2160 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
) {
2162 thread_wakeup((event_t
) pager
);
2163 vm_object_hash_entry_free(entry
);
2168 * Routine: vm_object_release_pager
2169 * Purpose: Terminate the pager and, upon completion,
2170 * release our last reference to it.
2171 * just like memory_object_terminate, except
2172 * that we wake up anyone blocked in vm_object_enter
2173 * waiting for termination message to be queued
2174 * before calling memory_object_init.
2177 vm_object_release_pager(
2178 memory_object_t pager
,
2183 * Terminate the pager.
2186 (void) memory_object_terminate(pager
);
2188 if (hashed
== TRUE
) {
2190 * Wakeup anyone waiting for this terminate
2191 * and remove the entry from the hash
2193 vm_object_pager_wakeup(pager
);
2196 * Release reference to pager.
2198 memory_object_deallocate(pager
);
2202 * Routine: vm_object_destroy
2204 * Shut down a VM object, despite the
2205 * presence of address map (or other) references
2211 __unused kern_return_t reason
)
2213 memory_object_t old_pager
;
2215 if (object
== VM_OBJECT_NULL
)
2216 return(KERN_SUCCESS
);
2219 * Remove the pager association immediately.
2221 * This will prevent the memory manager from further
2222 * meddling. [If it wanted to flush data or make
2223 * other changes, it should have done so before performing
2224 * the destroy call.]
2227 vm_object_lock(object
);
2228 object
->can_persist
= FALSE
;
2229 object
->named
= FALSE
;
2230 object
->alive
= FALSE
;
2232 if (object
->hashed
) {
2235 * Rip out the pager from the vm_object now...
2237 lck
= vm_object_hash_lock_spin(object
->pager
);
2238 vm_object_remove(object
);
2239 vm_object_hash_unlock(lck
);
2241 old_pager
= object
->pager
;
2242 object
->pager
= MEMORY_OBJECT_NULL
;
2243 if (old_pager
!= MEMORY_OBJECT_NULL
)
2244 memory_object_control_disable(object
->pager_control
);
2247 * Wait for the existing paging activity (that got
2248 * through before we nulled out the pager) to subside.
2251 vm_object_paging_wait(object
, THREAD_UNINT
);
2252 vm_object_unlock(object
);
2255 * Terminate the object now.
2257 if (old_pager
!= MEMORY_OBJECT_NULL
) {
2258 vm_object_release_pager(old_pager
, object
->hashed
);
2261 * JMM - Release the caller's reference. This assumes the
2262 * caller had a reference to release, which is a big (but
2263 * currently valid) assumption if this is driven from the
2264 * vnode pager (it is holding a named reference when making
2267 vm_object_deallocate(object
);
2270 return(KERN_SUCCESS
);
2276 #define VM_OBJ_DEACT_ALL_STATS DEBUG
2277 #if VM_OBJ_DEACT_ALL_STATS
2278 uint32_t vm_object_deactivate_all_pages_batches
= 0;
2279 uint32_t vm_object_deactivate_all_pages_pages
= 0;
2280 #endif /* VM_OBJ_DEACT_ALL_STATS */
2282 * vm_object_deactivate_all_pages
2284 * Deactivate all pages in the specified object. (Keep its pages
2285 * in memory even though it is no longer referenced.)
2287 * The object must be locked.
2290 vm_object_deactivate_all_pages(
2291 register vm_object_t object
)
2293 register vm_page_t p
;
2295 #if VM_OBJ_DEACT_ALL_STATS
2297 #endif /* VM_OBJ_DEACT_ALL_STATS */
2298 #define V_O_D_A_P_MAX_BATCH 256
2300 loop_count
= BATCH_LIMIT(V_O_D_A_P_MAX_BATCH
);
2301 #if VM_OBJ_DEACT_ALL_STATS
2303 #endif /* VM_OBJ_DEACT_ALL_STATS */
2304 vm_page_lock_queues();
2305 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
2306 if (--loop_count
== 0) {
2307 #if VM_OBJ_DEACT_ALL_STATS
2308 hw_atomic_add(&vm_object_deactivate_all_pages_batches
,
2310 hw_atomic_add(&vm_object_deactivate_all_pages_pages
,
2313 #endif /* VM_OBJ_DEACT_ALL_STATS */
2314 lck_mtx_yield(&vm_page_queue_lock
);
2315 loop_count
= BATCH_LIMIT(V_O_D_A_P_MAX_BATCH
);
2317 if (!p
->busy
&& !p
->throttled
) {
2318 #if VM_OBJ_DEACT_ALL_STATS
2320 #endif /* VM_OBJ_DEACT_ALL_STATS */
2321 vm_page_deactivate(p
);
2324 #if VM_OBJ_DEACT_ALL_STATS
2326 hw_atomic_add(&vm_object_deactivate_all_pages_batches
, 1);
2327 hw_atomic_add(&vm_object_deactivate_all_pages_pages
,
2331 #endif /* VM_OBJ_DEACT_ALL_STATS */
2332 vm_page_unlock_queues();
2334 #endif /* VM_OBJECT_CACHE */
2339 * The "chunk" macros are used by routines below when looking for pages to deactivate. These
2340 * exist because of the need to handle shadow chains. When deactivating pages, we only
2341 * want to deactive the ones at the top most level in the object chain. In order to do
2342 * this efficiently, the specified address range is divided up into "chunks" and we use
2343 * a bit map to keep track of which pages have already been processed as we descend down
2344 * the shadow chain. These chunk macros hide the details of the bit map implementation
2345 * as much as we can.
2347 * For convenience, we use a 64-bit data type as the bit map, and therefore a chunk is
2348 * set to 64 pages. The bit map is indexed from the low-order end, so that the lowest
2349 * order bit represents page 0 in the current range and highest order bit represents
2352 * For further convenience, we also use negative logic for the page state in the bit map.
2353 * The bit is set to 1 to indicate it has not yet been seen, and to 0 to indicate it has
2354 * been processed. This way we can simply test the 64-bit long word to see if it's zero
2355 * to easily tell if the whole range has been processed. Therefore, the bit map starts
2356 * out with all the bits set. The macros below hide all these details from the caller.
2359 #define PAGES_IN_A_CHUNK 64 /* The number of pages in the chunk must */
2360 /* be the same as the number of bits in */
2361 /* the chunk_state_t type. We use 64 */
2362 /* just for convenience. */
2364 #define CHUNK_SIZE (PAGES_IN_A_CHUNK * PAGE_SIZE_64) /* Size of a chunk in bytes */
2366 typedef uint64_t chunk_state_t
;
2369 * The bit map uses negative logic, so we start out with all 64 bits set to indicate
2370 * that no pages have been processed yet. Also, if len is less than the full CHUNK_SIZE,
2371 * then we mark pages beyond the len as having been "processed" so that we don't waste time
2372 * looking at pages in that range. This can save us from unnecessarily chasing down the
2376 #define CHUNK_INIT(c, len) \
2380 (c) = 0xffffffffffffffffLL; \
2382 for (p = (len) / PAGE_SIZE_64; p < PAGES_IN_A_CHUNK; p++) \
2383 MARK_PAGE_HANDLED(c, p); \
2388 * Return true if all pages in the chunk have not yet been processed.
2391 #define CHUNK_NOT_COMPLETE(c) ((c) != 0)
2394 * Return true if the page at offset 'p' in the bit map has already been handled
2395 * while processing a higher level object in the shadow chain.
2398 #define PAGE_ALREADY_HANDLED(c, p) (((c) & (1LL << (p))) == 0)
2401 * Mark the page at offset 'p' in the bit map as having been processed.
2404 #define MARK_PAGE_HANDLED(c, p) \
2406 (c) = (c) & ~(1LL << (p)); \
2411 * Return true if the page at the given offset has been paged out. Object is
2412 * locked upon entry and returned locked.
2418 vm_object_offset_t offset
)
2421 memory_object_t pager
;
2424 * Check the existence map for the page if we have one, otherwise
2425 * ask the pager about this page.
2429 if (object
->existence_map
) {
2430 if (vm_external_state_get(object
->existence_map
, offset
)
2431 == VM_EXTERNAL_STATE_EXISTS
) {
2439 #endif /* MACH_PAGEMAP */
2440 if (object
->internal
&&
2442 !object
->terminating
&&
2443 object
->pager_ready
) {
2445 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
2446 if (VM_COMPRESSOR_PAGER_STATE_GET(object
, offset
)
2447 == VM_EXTERNAL_STATE_EXISTS
) {
2455 * We're already holding a "paging in progress" reference
2456 * so the object can't disappear when we release the lock.
2459 assert(object
->paging_in_progress
);
2460 pager
= object
->pager
;
2461 vm_object_unlock(object
);
2463 kr
= memory_object_data_request(
2465 offset
+ object
->paging_offset
,
2466 0, /* just poke the pager */
2470 vm_object_lock(object
);
2472 if (kr
== KERN_SUCCESS
) {
2488 * madvise_free_debug
2490 * To help debug madvise(MADV_FREE*) mis-usage, this triggers a
2491 * zero-fill as soon as a page is affected by a madvise(MADV_FREE*), to
2492 * simulate the loss of the page's contents as if the page had been
2493 * reclaimed and then re-faulted.
2495 #if DEVELOPMENT || DEBUG
2496 int madvise_free_debug
= 1;
2498 int madvise_free_debug
= 0;
2502 * Deactivate the pages in the specified object and range. If kill_page is set, also discard any
2503 * page modified state from the pmap. Update the chunk_state as we go along. The caller must specify
2504 * a size that is less than or equal to the CHUNK_SIZE.
2508 deactivate_pages_in_object(
2510 vm_object_offset_t offset
,
2511 vm_object_size_t size
,
2512 boolean_t kill_page
,
2513 boolean_t reusable_page
,
2514 boolean_t all_reusable
,
2515 chunk_state_t
*chunk_state
,
2516 pmap_flush_context
*pfc
)
2520 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
2521 struct vm_page_delayed_work
*dwp
;
2524 unsigned int reusable
= 0;
2527 * Examine each page in the chunk. The variable 'p' is the page number relative to the start of the
2528 * chunk. Since this routine is called once for each level in the shadow chain, the chunk_state may
2529 * have pages marked as having been processed already. We stop the loop early if we find we've handled
2530 * all the pages in the chunk.
2535 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
2537 for(p
= 0; size
&& CHUNK_NOT_COMPLETE(*chunk_state
); p
++, size
-= PAGE_SIZE_64
, offset
+= PAGE_SIZE_64
) {
2540 * If this offset has already been found and handled in a higher level object, then don't
2541 * do anything with it in the current shadow object.
2544 if (PAGE_ALREADY_HANDLED(*chunk_state
, p
))
2548 * See if the page at this offset is around. First check to see if the page is resident,
2549 * then if not, check the existence map or with the pager.
2552 if ((m
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
2555 * We found a page we were looking for. Mark it as "handled" now in the chunk_state
2556 * so that we won't bother looking for a page at this offset again if there are more
2557 * shadow objects. Then deactivate the page.
2560 MARK_PAGE_HANDLED(*chunk_state
, p
);
2562 if (( !VM_PAGE_WIRED(m
)) && (!m
->private) && (!m
->gobbled
) && (!m
->busy
) && (!m
->laundry
)) {
2567 clear_refmod
= VM_MEM_REFERENCED
;
2568 dwp
->dw_mask
|= DW_clear_reference
;
2570 if ((kill_page
) && (object
->internal
)) {
2571 if (madvise_free_debug
) {
2573 * zero-fill the page now
2574 * to simulate it being
2575 * reclaimed and re-faulted.
2577 pmap_zero_page(m
->phys_page
);
2579 m
->precious
= FALSE
;
2582 clear_refmod
|= VM_MEM_MODIFIED
;
2585 * This page is now clean and
2586 * reclaimable. Move it out
2587 * of the throttled queue, so
2588 * that vm_pageout_scan() can
2591 dwp
->dw_mask
|= DW_move_page
;
2594 vm_external_state_clr(object
->existence_map
, offset
);
2595 #endif /* MACH_PAGEMAP */
2596 VM_COMPRESSOR_PAGER_STATE_CLR(object
,
2599 if (reusable_page
&& !m
->reusable
) {
2600 assert(!all_reusable
);
2601 assert(!object
->all_reusable
);
2603 object
->reusable_page_count
++;
2604 assert(object
->resident_page_count
>= object
->reusable_page_count
);
2608 pmap_clear_refmod_options(m
->phys_page
, clear_refmod
, PMAP_OPTIONS_NOFLUSH
, (void *)pfc
);
2610 if (!m
->throttled
&& !(reusable_page
|| all_reusable
))
2611 dwp
->dw_mask
|= DW_move_page
;
2614 VM_PAGE_ADD_DELAYED_WORK(dwp
, m
,
2617 if (dw_count
>= dw_limit
) {
2619 OSAddAtomic(reusable
,
2620 &vm_page_stats_reusable
.reusable_count
);
2621 vm_page_stats_reusable
.reusable
+= reusable
;
2624 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
2634 * The page at this offset isn't memory resident, check to see if it's
2635 * been paged out. If so, mark it as handled so we don't bother looking
2636 * for it in the shadow chain.
2639 if (page_is_paged_out(object
, offset
)) {
2640 MARK_PAGE_HANDLED(*chunk_state
, p
);
2643 * If we're killing a non-resident page, then clear the page in the existence
2644 * map so we don't bother paging it back in if it's touched again in the future.
2647 if ((kill_page
) && (object
->internal
)) {
2649 vm_external_state_clr(object
->existence_map
, offset
);
2650 #endif /* MACH_PAGEMAP */
2651 VM_COMPRESSOR_PAGER_STATE_CLR(object
,
2659 OSAddAtomic(reusable
, &vm_page_stats_reusable
.reusable_count
);
2660 vm_page_stats_reusable
.reusable
+= reusable
;
2665 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
2670 * Deactive a "chunk" of the given range of the object starting at offset. A "chunk"
2671 * will always be less than or equal to the given size. The total range is divided up
2672 * into chunks for efficiency and performance related to the locks and handling the shadow
2673 * chain. This routine returns how much of the given "size" it actually processed. It's
2674 * up to the caler to loop and keep calling this routine until the entire range they want
2675 * to process has been done.
2678 static vm_object_size_t
2680 vm_object_t orig_object
,
2681 vm_object_offset_t offset
,
2682 vm_object_size_t size
,
2683 boolean_t kill_page
,
2684 boolean_t reusable_page
,
2685 boolean_t all_reusable
,
2686 pmap_flush_context
*pfc
)
2689 vm_object_t tmp_object
;
2690 vm_object_size_t length
;
2691 chunk_state_t chunk_state
;
2695 * Get set to do a chunk. We'll do up to CHUNK_SIZE, but no more than the
2696 * remaining size the caller asked for.
2699 length
= MIN(size
, CHUNK_SIZE
);
2702 * The chunk_state keeps track of which pages we've already processed if there's
2703 * a shadow chain on this object. At this point, we haven't done anything with this
2704 * range of pages yet, so initialize the state to indicate no pages processed yet.
2707 CHUNK_INIT(chunk_state
, length
);
2708 object
= orig_object
;
2711 * Start at the top level object and iterate around the loop once for each object
2712 * in the shadow chain. We stop processing early if we've already found all the pages
2713 * in the range. Otherwise we stop when we run out of shadow objects.
2716 while (object
&& CHUNK_NOT_COMPLETE(chunk_state
)) {
2717 vm_object_paging_begin(object
);
2719 deactivate_pages_in_object(object
, offset
, length
, kill_page
, reusable_page
, all_reusable
, &chunk_state
, pfc
);
2721 vm_object_paging_end(object
);
2724 * We've finished with this object, see if there's a shadow object. If
2725 * there is, update the offset and lock the new object. We also turn off
2726 * kill_page at this point since we only kill pages in the top most object.
2729 tmp_object
= object
->shadow
;
2733 reusable_page
= FALSE
;
2734 all_reusable
= FALSE
;
2735 offset
+= object
->vo_shadow_offset
;
2736 vm_object_lock(tmp_object
);
2739 if (object
!= orig_object
)
2740 vm_object_unlock(object
);
2742 object
= tmp_object
;
2745 if (object
&& object
!= orig_object
)
2746 vm_object_unlock(object
);
2754 * Move any resident pages in the specified range to the inactive queue. If kill_page is set,
2755 * we also clear the modified status of the page and "forget" any changes that have been made
2759 __private_extern__
void
2760 vm_object_deactivate_pages(
2762 vm_object_offset_t offset
,
2763 vm_object_size_t size
,
2764 boolean_t kill_page
,
2765 boolean_t reusable_page
)
2767 vm_object_size_t length
;
2768 boolean_t all_reusable
;
2769 pmap_flush_context pmap_flush_context_storage
;
2772 * We break the range up into chunks and do one chunk at a time. This is for
2773 * efficiency and performance while handling the shadow chains and the locks.
2774 * The deactivate_a_chunk() function returns how much of the range it processed.
2775 * We keep calling this routine until the given size is exhausted.
2779 all_reusable
= FALSE
;
2780 if (reusable_page
&&
2782 object
->vo_size
!= 0 &&
2783 object
->vo_size
== size
&&
2784 object
->reusable_page_count
== 0) {
2785 all_reusable
= TRUE
;
2786 reusable_page
= FALSE
;
2789 if ((reusable_page
|| all_reusable
) && object
->all_reusable
) {
2790 /* This means MADV_FREE_REUSABLE has been called twice, which
2791 * is probably illegal. */
2795 pmap_flush_context_init(&pmap_flush_context_storage
);
2798 length
= deactivate_a_chunk(object
, offset
, size
, kill_page
, reusable_page
, all_reusable
, &pmap_flush_context_storage
);
2803 pmap_flush(&pmap_flush_context_storage
);
2806 if (!object
->all_reusable
) {
2807 unsigned int reusable
;
2809 object
->all_reusable
= TRUE
;
2810 assert(object
->reusable_page_count
== 0);
2811 /* update global stats */
2812 reusable
= object
->resident_page_count
;
2813 OSAddAtomic(reusable
,
2814 &vm_page_stats_reusable
.reusable_count
);
2815 vm_page_stats_reusable
.reusable
+= reusable
;
2816 vm_page_stats_reusable
.all_reusable_calls
++;
2818 } else if (reusable_page
) {
2819 vm_page_stats_reusable
.partial_reusable_calls
++;
2824 vm_object_reuse_pages(
2826 vm_object_offset_t start_offset
,
2827 vm_object_offset_t end_offset
,
2828 boolean_t allow_partial_reuse
)
2830 vm_object_offset_t cur_offset
;
2832 unsigned int reused
, reusable
;
2834 #define VM_OBJECT_REUSE_PAGE(object, m, reused) \
2836 if ((m) != VM_PAGE_NULL && \
2838 assert((object)->reusable_page_count <= \
2839 (object)->resident_page_count); \
2840 assert((object)->reusable_page_count > 0); \
2841 (object)->reusable_page_count--; \
2842 (m)->reusable = FALSE; \
2850 vm_object_lock_assert_exclusive(object
);
2852 if (object
->all_reusable
) {
2853 assert(object
->reusable_page_count
== 0);
2854 object
->all_reusable
= FALSE
;
2855 if (end_offset
- start_offset
== object
->vo_size
||
2856 !allow_partial_reuse
) {
2857 vm_page_stats_reusable
.all_reuse_calls
++;
2858 reused
= object
->resident_page_count
;
2860 vm_page_stats_reusable
.partial_reuse_calls
++;
2861 queue_iterate(&object
->memq
, m
, vm_page_t
, listq
) {
2862 if (m
->offset
< start_offset
||
2863 m
->offset
>= end_offset
) {
2865 object
->reusable_page_count
++;
2866 assert(object
->resident_page_count
>= object
->reusable_page_count
);
2869 assert(!m
->reusable
);
2874 } else if (object
->resident_page_count
>
2875 ((end_offset
- start_offset
) >> PAGE_SHIFT
)) {
2876 vm_page_stats_reusable
.partial_reuse_calls
++;
2877 for (cur_offset
= start_offset
;
2878 cur_offset
< end_offset
;
2879 cur_offset
+= PAGE_SIZE_64
) {
2880 if (object
->reusable_page_count
== 0) {
2883 m
= vm_page_lookup(object
, cur_offset
);
2884 VM_OBJECT_REUSE_PAGE(object
, m
, reused
);
2887 vm_page_stats_reusable
.partial_reuse_calls
++;
2888 queue_iterate(&object
->memq
, m
, vm_page_t
, listq
) {
2889 if (object
->reusable_page_count
== 0) {
2892 if (m
->offset
< start_offset
||
2893 m
->offset
>= end_offset
) {
2896 VM_OBJECT_REUSE_PAGE(object
, m
, reused
);
2900 /* update global stats */
2901 OSAddAtomic(reusable
-reused
, &vm_page_stats_reusable
.reusable_count
);
2902 vm_page_stats_reusable
.reused
+= reused
;
2903 vm_page_stats_reusable
.reusable
+= reusable
;
2907 * Routine: vm_object_pmap_protect
2910 * Reduces the permission for all physical
2911 * pages in the specified object range.
2913 * If removing write permission only, it is
2914 * sufficient to protect only the pages in
2915 * the top-level object; only those pages may
2916 * have write permission.
2918 * If removing all access, we must follow the
2919 * shadow chain from the top-level object to
2920 * remove access to all pages in shadowed objects.
2922 * The object must *not* be locked. The object must
2923 * be temporary/internal.
2925 * If pmap is not NULL, this routine assumes that
2926 * the only mappings for the pages are in that
2930 __private_extern__
void
2931 vm_object_pmap_protect(
2932 register vm_object_t object
,
2933 register vm_object_offset_t offset
,
2934 vm_object_size_t size
,
2936 vm_map_offset_t pmap_start
,
2939 vm_object_pmap_protect_options(object
, offset
, size
,
2940 pmap
, pmap_start
, prot
, 0);
2943 __private_extern__
void
2944 vm_object_pmap_protect_options(
2945 register vm_object_t object
,
2946 register vm_object_offset_t offset
,
2947 vm_object_size_t size
,
2949 vm_map_offset_t pmap_start
,
2953 pmap_flush_context pmap_flush_context_storage
;
2954 boolean_t delayed_pmap_flush
= FALSE
;
2956 if (object
== VM_OBJECT_NULL
)
2958 size
= vm_object_round_page(size
);
2959 offset
= vm_object_trunc_page(offset
);
2961 vm_object_lock(object
);
2963 if (object
->phys_contiguous
) {
2965 vm_object_unlock(object
);
2966 pmap_protect_options(pmap
,
2970 options
& ~PMAP_OPTIONS_NOFLUSH
,
2973 vm_object_offset_t phys_start
, phys_end
, phys_addr
;
2975 phys_start
= object
->vo_shadow_offset
+ offset
;
2976 phys_end
= phys_start
+ size
;
2977 assert(phys_start
<= phys_end
);
2978 assert(phys_end
<= object
->vo_shadow_offset
+ object
->vo_size
);
2979 vm_object_unlock(object
);
2981 pmap_flush_context_init(&pmap_flush_context_storage
);
2982 delayed_pmap_flush
= FALSE
;
2984 for (phys_addr
= phys_start
;
2985 phys_addr
< phys_end
;
2986 phys_addr
+= PAGE_SIZE_64
) {
2987 pmap_page_protect_options(
2988 (ppnum_t
) (phys_addr
>> PAGE_SHIFT
),
2990 options
| PMAP_OPTIONS_NOFLUSH
,
2991 (void *)&pmap_flush_context_storage
);
2992 delayed_pmap_flush
= TRUE
;
2994 if (delayed_pmap_flush
== TRUE
)
2995 pmap_flush(&pmap_flush_context_storage
);
3000 assert(object
->internal
);
3003 if (ptoa_64(object
->resident_page_count
) > size
/2 && pmap
!= PMAP_NULL
) {
3004 vm_object_unlock(object
);
3005 pmap_protect_options(pmap
, pmap_start
, pmap_start
+ size
, prot
,
3006 options
& ~PMAP_OPTIONS_NOFLUSH
, NULL
);
3010 pmap_flush_context_init(&pmap_flush_context_storage
);
3011 delayed_pmap_flush
= FALSE
;
3014 * if we are doing large ranges with respect to resident
3015 * page count then we should interate over pages otherwise
3016 * inverse page look-up will be faster
3018 if (ptoa_64(object
->resident_page_count
/ 4) < size
) {
3020 vm_object_offset_t end
;
3022 end
= offset
+ size
;
3024 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
3025 if (!p
->fictitious
&& (offset
<= p
->offset
) && (p
->offset
< end
)) {
3026 vm_map_offset_t start
;
3028 start
= pmap_start
+ p
->offset
- offset
;
3030 if (pmap
!= PMAP_NULL
)
3031 pmap_protect_options(
3034 start
+ PAGE_SIZE_64
,
3036 options
| PMAP_OPTIONS_NOFLUSH
,
3037 &pmap_flush_context_storage
);
3039 pmap_page_protect_options(
3042 options
| PMAP_OPTIONS_NOFLUSH
,
3043 &pmap_flush_context_storage
);
3044 delayed_pmap_flush
= TRUE
;
3050 vm_object_offset_t end
;
3051 vm_object_offset_t target_off
;
3053 end
= offset
+ size
;
3055 for (target_off
= offset
;
3056 target_off
< end
; target_off
+= PAGE_SIZE
) {
3058 p
= vm_page_lookup(object
, target_off
);
3060 if (p
!= VM_PAGE_NULL
) {
3061 vm_object_offset_t start
;
3063 start
= pmap_start
+ (p
->offset
- offset
);
3065 if (pmap
!= PMAP_NULL
)
3066 pmap_protect_options(
3069 start
+ PAGE_SIZE_64
,
3071 options
| PMAP_OPTIONS_NOFLUSH
,
3072 &pmap_flush_context_storage
);
3074 pmap_page_protect_options(
3077 options
| PMAP_OPTIONS_NOFLUSH
,
3078 &pmap_flush_context_storage
);
3079 delayed_pmap_flush
= TRUE
;
3083 if (delayed_pmap_flush
== TRUE
)
3084 pmap_flush(&pmap_flush_context_storage
);
3086 if (prot
== VM_PROT_NONE
) {
3088 * Must follow shadow chain to remove access
3089 * to pages in shadowed objects.
3091 register vm_object_t next_object
;
3093 next_object
= object
->shadow
;
3094 if (next_object
!= VM_OBJECT_NULL
) {
3095 offset
+= object
->vo_shadow_offset
;
3096 vm_object_lock(next_object
);
3097 vm_object_unlock(object
);
3098 object
= next_object
;
3102 * End of chain - we are done.
3109 * Pages in shadowed objects may never have
3110 * write permission - we may stop here.
3116 vm_object_unlock(object
);
3120 * Routine: vm_object_copy_slowly
3123 * Copy the specified range of the source
3124 * virtual memory object without using
3125 * protection-based optimizations (such
3126 * as copy-on-write). The pages in the
3127 * region are actually copied.
3129 * In/out conditions:
3130 * The caller must hold a reference and a lock
3131 * for the source virtual memory object. The source
3132 * object will be returned *unlocked*.
3135 * If the copy is completed successfully, KERN_SUCCESS is
3136 * returned. If the caller asserted the interruptible
3137 * argument, and an interruption occurred while waiting
3138 * for a user-generated event, MACH_SEND_INTERRUPTED is
3139 * returned. Other values may be returned to indicate
3140 * hard errors during the copy operation.
3142 * A new virtual memory object is returned in a
3143 * parameter (_result_object). The contents of this
3144 * new object, starting at a zero offset, are a copy
3145 * of the source memory region. In the event of
3146 * an error, this parameter will contain the value
3149 __private_extern__ kern_return_t
3150 vm_object_copy_slowly(
3151 register vm_object_t src_object
,
3152 vm_object_offset_t src_offset
,
3153 vm_object_size_t size
,
3154 boolean_t interruptible
,
3155 vm_object_t
*_result_object
) /* OUT */
3157 vm_object_t new_object
;
3158 vm_object_offset_t new_offset
;
3160 struct vm_object_fault_info fault_info
;
3162 XPR(XPR_VM_OBJECT
, "v_o_c_slowly obj 0x%x off 0x%x size 0x%x\n",
3163 src_object
, src_offset
, size
, 0, 0);
3166 vm_object_unlock(src_object
);
3167 *_result_object
= VM_OBJECT_NULL
;
3168 return(KERN_INVALID_ARGUMENT
);
3172 * Prevent destruction of the source object while we copy.
3175 vm_object_reference_locked(src_object
);
3176 vm_object_unlock(src_object
);
3179 * Create a new object to hold the copied pages.
3181 * We fill the new object starting at offset 0,
3182 * regardless of the input offset.
3183 * We don't bother to lock the new object within
3184 * this routine, since we have the only reference.
3187 new_object
= vm_object_allocate(size
);
3190 assert(size
== trunc_page_64(size
)); /* Will the loop terminate? */
3192 fault_info
.interruptible
= interruptible
;
3193 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
3194 fault_info
.user_tag
= 0;
3195 fault_info
.lo_offset
= src_offset
;
3196 fault_info
.hi_offset
= src_offset
+ size
;
3197 fault_info
.no_cache
= FALSE
;
3198 fault_info
.stealth
= TRUE
;
3199 fault_info
.io_sync
= FALSE
;
3200 fault_info
.cs_bypass
= FALSE
;
3201 fault_info
.mark_zf_absent
= FALSE
;
3202 fault_info
.batch_pmap_op
= FALSE
;
3206 src_offset
+= PAGE_SIZE_64
,
3207 new_offset
+= PAGE_SIZE_64
, size
-= PAGE_SIZE_64
3210 vm_fault_return_t result
;
3212 vm_object_lock(new_object
);
3214 while ((new_page
= vm_page_alloc(new_object
, new_offset
))
3217 vm_object_unlock(new_object
);
3219 if (!vm_page_wait(interruptible
)) {
3220 vm_object_deallocate(new_object
);
3221 vm_object_deallocate(src_object
);
3222 *_result_object
= VM_OBJECT_NULL
;
3223 return(MACH_SEND_INTERRUPTED
);
3225 vm_object_lock(new_object
);
3227 vm_object_unlock(new_object
);
3230 vm_prot_t prot
= VM_PROT_READ
;
3231 vm_page_t _result_page
;
3234 vm_page_t result_page
;
3235 kern_return_t error_code
;
3237 vm_object_lock(src_object
);
3238 vm_object_paging_begin(src_object
);
3240 if (size
> (vm_size_t
) -1) {
3241 /* 32-bit overflow */
3242 fault_info
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
3244 fault_info
.cluster_size
= (vm_size_t
) size
;
3245 assert(fault_info
.cluster_size
== size
);
3248 XPR(XPR_VM_FAULT
,"vm_object_copy_slowly -> vm_fault_page",0,0,0,0,0);
3249 _result_page
= VM_PAGE_NULL
;
3250 result
= vm_fault_page(src_object
, src_offset
,
3251 VM_PROT_READ
, FALSE
,
3252 FALSE
, /* page not looked up */
3253 &prot
, &_result_page
, &top_page
,
3255 &error_code
, FALSE
, FALSE
, &fault_info
);
3258 case VM_FAULT_SUCCESS
:
3259 result_page
= _result_page
;
3262 * Copy the page to the new object.
3265 * If result_page is clean,
3266 * we could steal it instead
3270 vm_page_copy(result_page
, new_page
);
3271 vm_object_unlock(result_page
->object
);
3274 * Let go of both pages (make them
3275 * not busy, perform wakeup, activate).
3277 vm_object_lock(new_object
);
3278 SET_PAGE_DIRTY(new_page
, FALSE
);
3279 PAGE_WAKEUP_DONE(new_page
);
3280 vm_object_unlock(new_object
);
3282 vm_object_lock(result_page
->object
);
3283 PAGE_WAKEUP_DONE(result_page
);
3285 vm_page_lockspin_queues();
3286 if (!result_page
->active
&&
3287 !result_page
->inactive
&&
3288 !result_page
->throttled
)
3289 vm_page_activate(result_page
);
3290 vm_page_activate(new_page
);
3291 vm_page_unlock_queues();
3294 * Release paging references and
3295 * top-level placeholder page, if any.
3298 vm_fault_cleanup(result_page
->object
,
3303 case VM_FAULT_RETRY
:
3306 case VM_FAULT_MEMORY_SHORTAGE
:
3307 if (vm_page_wait(interruptible
))
3311 case VM_FAULT_INTERRUPTED
:
3312 vm_object_lock(new_object
);
3313 VM_PAGE_FREE(new_page
);
3314 vm_object_unlock(new_object
);
3316 vm_object_deallocate(new_object
);
3317 vm_object_deallocate(src_object
);
3318 *_result_object
= VM_OBJECT_NULL
;
3319 return(MACH_SEND_INTERRUPTED
);
3321 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
3322 /* success but no VM page: fail */
3323 vm_object_paging_end(src_object
);
3324 vm_object_unlock(src_object
);
3326 case VM_FAULT_MEMORY_ERROR
:
3329 * (a) ignore pages that we can't
3331 * (b) return the null object if
3332 * any page fails [chosen]
3335 vm_object_lock(new_object
);
3336 VM_PAGE_FREE(new_page
);
3337 vm_object_unlock(new_object
);
3339 vm_object_deallocate(new_object
);
3340 vm_object_deallocate(src_object
);
3341 *_result_object
= VM_OBJECT_NULL
;
3342 return(error_code
? error_code
:
3346 panic("vm_object_copy_slowly: unexpected error"
3347 " 0x%x from vm_fault_page()\n", result
);
3349 } while (result
!= VM_FAULT_SUCCESS
);
3353 * Lose the extra reference, and return our object.
3355 vm_object_deallocate(src_object
);
3356 *_result_object
= new_object
;
3357 return(KERN_SUCCESS
);
3361 * Routine: vm_object_copy_quickly
3364 * Copy the specified range of the source virtual
3365 * memory object, if it can be done without waiting
3366 * for user-generated events.
3369 * If the copy is successful, the copy is returned in
3370 * the arguments; otherwise, the arguments are not
3373 * In/out conditions:
3374 * The object should be unlocked on entry and exit.
3378 __private_extern__ boolean_t
3379 vm_object_copy_quickly(
3380 vm_object_t
*_object
, /* INOUT */
3381 __unused vm_object_offset_t offset
, /* IN */
3382 __unused vm_object_size_t size
, /* IN */
3383 boolean_t
*_src_needs_copy
, /* OUT */
3384 boolean_t
*_dst_needs_copy
) /* OUT */
3386 vm_object_t object
= *_object
;
3387 memory_object_copy_strategy_t copy_strategy
;
3389 XPR(XPR_VM_OBJECT
, "v_o_c_quickly obj 0x%x off 0x%x size 0x%x\n",
3390 *_object
, offset
, size
, 0, 0);
3391 if (object
== VM_OBJECT_NULL
) {
3392 *_src_needs_copy
= FALSE
;
3393 *_dst_needs_copy
= FALSE
;
3397 vm_object_lock(object
);
3399 copy_strategy
= object
->copy_strategy
;
3401 switch (copy_strategy
) {
3402 case MEMORY_OBJECT_COPY_SYMMETRIC
:
3405 * Symmetric copy strategy.
3406 * Make another reference to the object.
3407 * Leave object/offset unchanged.
3410 vm_object_reference_locked(object
);
3411 object
->shadowed
= TRUE
;
3412 vm_object_unlock(object
);
3415 * Both source and destination must make
3416 * shadows, and the source must be made
3417 * read-only if not already.
3420 *_src_needs_copy
= TRUE
;
3421 *_dst_needs_copy
= TRUE
;
3425 case MEMORY_OBJECT_COPY_DELAY
:
3426 vm_object_unlock(object
);
3430 vm_object_unlock(object
);
3436 static int copy_call_count
= 0;
3437 static int copy_call_sleep_count
= 0;
3438 static int copy_call_restart_count
= 0;
3441 * Routine: vm_object_copy_call [internal]
3444 * Copy the source object (src_object), using the
3445 * user-managed copy algorithm.
3447 * In/out conditions:
3448 * The source object must be locked on entry. It
3449 * will be *unlocked* on exit.
3452 * If the copy is successful, KERN_SUCCESS is returned.
3453 * A new object that represents the copied virtual
3454 * memory is returned in a parameter (*_result_object).
3455 * If the return value indicates an error, this parameter
3458 static kern_return_t
3459 vm_object_copy_call(
3460 vm_object_t src_object
,
3461 vm_object_offset_t src_offset
,
3462 vm_object_size_t size
,
3463 vm_object_t
*_result_object
) /* OUT */
3467 boolean_t check_ready
= FALSE
;
3468 uint32_t try_failed_count
= 0;
3471 * If a copy is already in progress, wait and retry.
3474 * Consider making this call interruptable, as Mike
3475 * intended it to be.
3478 * Need a counter or version or something to allow
3479 * us to use the copy that the currently requesting
3480 * thread is obtaining -- is it worth adding to the
3481 * vm object structure? Depends how common this case it.
3484 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
3485 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
3487 copy_call_restart_count
++;
3491 * Indicate (for the benefit of memory_object_create_copy)
3492 * that we want a copy for src_object. (Note that we cannot
3493 * do a real assert_wait before calling memory_object_copy,
3494 * so we simply set the flag.)
3497 vm_object_set_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
);
3498 vm_object_unlock(src_object
);
3501 * Ask the memory manager to give us a memory object
3502 * which represents a copy of the src object.
3503 * The memory manager may give us a memory object
3504 * which we already have, or it may give us a
3505 * new memory object. This memory object will arrive
3506 * via memory_object_create_copy.
3509 kr
= KERN_FAILURE
; /* XXX need to change memory_object.defs */
3510 if (kr
!= KERN_SUCCESS
) {
3515 * Wait for the copy to arrive.
3517 vm_object_lock(src_object
);
3518 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
3519 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
3521 copy_call_sleep_count
++;
3524 assert(src_object
->copy
!= VM_OBJECT_NULL
);
3525 copy
= src_object
->copy
;
3526 if (!vm_object_lock_try(copy
)) {
3527 vm_object_unlock(src_object
);
3530 mutex_pause(try_failed_count
); /* wait a bit */
3532 vm_object_lock(src_object
);
3535 if (copy
->vo_size
< src_offset
+size
)
3536 copy
->vo_size
= src_offset
+size
;
3538 if (!copy
->pager_ready
)
3544 *_result_object
= copy
;
3545 vm_object_unlock(copy
);
3546 vm_object_unlock(src_object
);
3548 /* Wait for the copy to be ready. */
3549 if (check_ready
== TRUE
) {
3550 vm_object_lock(copy
);
3551 while (!copy
->pager_ready
) {
3552 vm_object_sleep(copy
, VM_OBJECT_EVENT_PAGER_READY
, THREAD_UNINT
);
3554 vm_object_unlock(copy
);
3557 return KERN_SUCCESS
;
3560 static int copy_delayed_lock_collisions
= 0;
3561 static int copy_delayed_max_collisions
= 0;
3562 static int copy_delayed_lock_contention
= 0;
3563 static int copy_delayed_protect_iterate
= 0;
3566 * Routine: vm_object_copy_delayed [internal]
3569 * Copy the specified virtual memory object, using
3570 * the asymmetric copy-on-write algorithm.
3572 * In/out conditions:
3573 * The src_object must be locked on entry. It will be unlocked
3574 * on exit - so the caller must also hold a reference to it.
3576 * This routine will not block waiting for user-generated
3577 * events. It is not interruptible.
3579 __private_extern__ vm_object_t
3580 vm_object_copy_delayed(
3581 vm_object_t src_object
,
3582 vm_object_offset_t src_offset
,
3583 vm_object_size_t size
,
3584 boolean_t src_object_shared
)
3586 vm_object_t new_copy
= VM_OBJECT_NULL
;
3587 vm_object_t old_copy
;
3589 vm_object_size_t copy_size
= src_offset
+ size
;
3590 pmap_flush_context pmap_flush_context_storage
;
3591 boolean_t delayed_pmap_flush
= FALSE
;
3596 * The user-level memory manager wants to see all of the changes
3597 * to this object, but it has promised not to make any changes on
3600 * Perform an asymmetric copy-on-write, as follows:
3601 * Create a new object, called a "copy object" to hold
3602 * pages modified by the new mapping (i.e., the copy,
3603 * not the original mapping).
3604 * Record the original object as the backing object for
3605 * the copy object. If the original mapping does not
3606 * change a page, it may be used read-only by the copy.
3607 * Record the copy object in the original object.
3608 * When the original mapping causes a page to be modified,
3609 * it must be copied to a new page that is "pushed" to
3611 * Mark the new mapping (the copy object) copy-on-write.
3612 * This makes the copy object itself read-only, allowing
3613 * it to be reused if the original mapping makes no
3614 * changes, and simplifying the synchronization required
3615 * in the "push" operation described above.
3617 * The copy-on-write is said to be assymetric because the original
3618 * object is *not* marked copy-on-write. A copied page is pushed
3619 * to the copy object, regardless which party attempted to modify
3622 * Repeated asymmetric copy operations may be done. If the
3623 * original object has not been changed since the last copy, its
3624 * copy object can be reused. Otherwise, a new copy object can be
3625 * inserted between the original object and its previous copy
3626 * object. Since any copy object is read-only, this cannot affect
3627 * affect the contents of the previous copy object.
3629 * Note that a copy object is higher in the object tree than the
3630 * original object; therefore, use of the copy object recorded in
3631 * the original object must be done carefully, to avoid deadlock.
3637 * Wait for paging in progress.
3639 if (!src_object
->true_share
&&
3640 (src_object
->paging_in_progress
!= 0 ||
3641 src_object
->activity_in_progress
!= 0)) {
3642 if (src_object_shared
== TRUE
) {
3643 vm_object_unlock(src_object
);
3644 vm_object_lock(src_object
);
3645 src_object_shared
= FALSE
;
3648 vm_object_paging_wait(src_object
, THREAD_UNINT
);
3651 * See whether we can reuse the result of a previous
3655 old_copy
= src_object
->copy
;
3656 if (old_copy
!= VM_OBJECT_NULL
) {
3660 * Try to get the locks (out of order)
3662 if (src_object_shared
== TRUE
)
3663 lock_granted
= vm_object_lock_try_shared(old_copy
);
3665 lock_granted
= vm_object_lock_try(old_copy
);
3667 if (!lock_granted
) {
3668 vm_object_unlock(src_object
);
3670 if (collisions
++ == 0)
3671 copy_delayed_lock_contention
++;
3672 mutex_pause(collisions
);
3674 /* Heisenberg Rules */
3675 copy_delayed_lock_collisions
++;
3677 if (collisions
> copy_delayed_max_collisions
)
3678 copy_delayed_max_collisions
= collisions
;
3680 if (src_object_shared
== TRUE
)
3681 vm_object_lock_shared(src_object
);
3683 vm_object_lock(src_object
);
3689 * Determine whether the old copy object has
3693 if (old_copy
->resident_page_count
== 0 &&
3694 !old_copy
->pager_created
) {
3696 * It has not been modified.
3698 * Return another reference to
3699 * the existing copy-object if
3700 * we can safely grow it (if
3704 if (old_copy
->vo_size
< copy_size
) {
3705 if (src_object_shared
== TRUE
) {
3706 vm_object_unlock(old_copy
);
3707 vm_object_unlock(src_object
);
3709 vm_object_lock(src_object
);
3710 src_object_shared
= FALSE
;
3714 * We can't perform a delayed copy if any of the
3715 * pages in the extended range are wired (because
3716 * we can't safely take write permission away from
3717 * wired pages). If the pages aren't wired, then
3718 * go ahead and protect them.
3720 copy_delayed_protect_iterate
++;
3722 pmap_flush_context_init(&pmap_flush_context_storage
);
3723 delayed_pmap_flush
= FALSE
;
3725 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
3726 if (!p
->fictitious
&&
3727 p
->offset
>= old_copy
->vo_size
&&
3728 p
->offset
< copy_size
) {
3729 if (VM_PAGE_WIRED(p
)) {
3730 vm_object_unlock(old_copy
);
3731 vm_object_unlock(src_object
);
3733 if (new_copy
!= VM_OBJECT_NULL
) {
3734 vm_object_unlock(new_copy
);
3735 vm_object_deallocate(new_copy
);
3737 if (delayed_pmap_flush
== TRUE
)
3738 pmap_flush(&pmap_flush_context_storage
);
3740 return VM_OBJECT_NULL
;
3742 pmap_page_protect_options(p
->phys_page
, (VM_PROT_ALL
& ~VM_PROT_WRITE
),
3743 PMAP_OPTIONS_NOFLUSH
, (void *)&pmap_flush_context_storage
);
3744 delayed_pmap_flush
= TRUE
;
3748 if (delayed_pmap_flush
== TRUE
)
3749 pmap_flush(&pmap_flush_context_storage
);
3751 old_copy
->vo_size
= copy_size
;
3753 if (src_object_shared
== TRUE
)
3754 vm_object_reference_shared(old_copy
);
3756 vm_object_reference_locked(old_copy
);
3757 vm_object_unlock(old_copy
);
3758 vm_object_unlock(src_object
);
3760 if (new_copy
!= VM_OBJECT_NULL
) {
3761 vm_object_unlock(new_copy
);
3762 vm_object_deallocate(new_copy
);
3770 * Adjust the size argument so that the newly-created
3771 * copy object will be large enough to back either the
3772 * old copy object or the new mapping.
3774 if (old_copy
->vo_size
> copy_size
)
3775 copy_size
= old_copy
->vo_size
;
3777 if (new_copy
== VM_OBJECT_NULL
) {
3778 vm_object_unlock(old_copy
);
3779 vm_object_unlock(src_object
);
3780 new_copy
= vm_object_allocate(copy_size
);
3781 vm_object_lock(src_object
);
3782 vm_object_lock(new_copy
);
3784 src_object_shared
= FALSE
;
3787 new_copy
->vo_size
= copy_size
;
3790 * The copy-object is always made large enough to
3791 * completely shadow the original object, since
3792 * it may have several users who want to shadow
3793 * the original object at different points.
3796 assert((old_copy
->shadow
== src_object
) &&
3797 (old_copy
->vo_shadow_offset
== (vm_object_offset_t
) 0));
3799 } else if (new_copy
== VM_OBJECT_NULL
) {
3800 vm_object_unlock(src_object
);
3801 new_copy
= vm_object_allocate(copy_size
);
3802 vm_object_lock(src_object
);
3803 vm_object_lock(new_copy
);
3805 src_object_shared
= FALSE
;
3810 * We now have the src object locked, and the new copy object
3811 * allocated and locked (and potentially the old copy locked).
3812 * Before we go any further, make sure we can still perform
3813 * a delayed copy, as the situation may have changed.
3815 * Specifically, we can't perform a delayed copy if any of the
3816 * pages in the range are wired (because we can't safely take
3817 * write permission away from wired pages). If the pages aren't
3818 * wired, then go ahead and protect them.
3820 copy_delayed_protect_iterate
++;
3822 pmap_flush_context_init(&pmap_flush_context_storage
);
3823 delayed_pmap_flush
= FALSE
;
3825 queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
3826 if (!p
->fictitious
&& p
->offset
< copy_size
) {
3827 if (VM_PAGE_WIRED(p
)) {
3829 vm_object_unlock(old_copy
);
3830 vm_object_unlock(src_object
);
3831 vm_object_unlock(new_copy
);
3832 vm_object_deallocate(new_copy
);
3834 if (delayed_pmap_flush
== TRUE
)
3835 pmap_flush(&pmap_flush_context_storage
);
3837 return VM_OBJECT_NULL
;
3839 pmap_page_protect_options(p
->phys_page
, (VM_PROT_ALL
& ~VM_PROT_WRITE
),
3840 PMAP_OPTIONS_NOFLUSH
, (void *)&pmap_flush_context_storage
);
3841 delayed_pmap_flush
= TRUE
;
3845 if (delayed_pmap_flush
== TRUE
)
3846 pmap_flush(&pmap_flush_context_storage
);
3848 if (old_copy
!= VM_OBJECT_NULL
) {
3850 * Make the old copy-object shadow the new one.
3851 * It will receive no more pages from the original
3855 /* remove ref. from old_copy */
3856 vm_object_lock_assert_exclusive(src_object
);
3857 src_object
->ref_count
--;
3858 assert(src_object
->ref_count
> 0);
3859 vm_object_lock_assert_exclusive(old_copy
);
3860 old_copy
->shadow
= new_copy
;
3861 vm_object_lock_assert_exclusive(new_copy
);
3862 assert(new_copy
->ref_count
> 0);
3863 new_copy
->ref_count
++; /* for old_copy->shadow ref. */
3866 if (old_copy
->res_count
) {
3867 VM_OBJ_RES_INCR(new_copy
);
3868 VM_OBJ_RES_DECR(src_object
);
3872 vm_object_unlock(old_copy
); /* done with old_copy */
3876 * Point the new copy at the existing object.
3878 vm_object_lock_assert_exclusive(new_copy
);
3879 new_copy
->shadow
= src_object
;
3880 new_copy
->vo_shadow_offset
= 0;
3881 new_copy
->shadowed
= TRUE
; /* caller must set needs_copy */
3883 vm_object_lock_assert_exclusive(src_object
);
3884 vm_object_reference_locked(src_object
);
3885 src_object
->copy
= new_copy
;
3886 vm_object_unlock(src_object
);
3887 vm_object_unlock(new_copy
);
3890 "vm_object_copy_delayed: used copy object %X for source %X\n",
3891 new_copy
, src_object
, 0, 0, 0);
3897 * Routine: vm_object_copy_strategically
3900 * Perform a copy according to the source object's
3901 * declared strategy. This operation may block,
3902 * and may be interrupted.
3904 __private_extern__ kern_return_t
3905 vm_object_copy_strategically(
3906 register vm_object_t src_object
,
3907 vm_object_offset_t src_offset
,
3908 vm_object_size_t size
,
3909 vm_object_t
*dst_object
, /* OUT */
3910 vm_object_offset_t
*dst_offset
, /* OUT */
3911 boolean_t
*dst_needs_copy
) /* OUT */
3914 boolean_t interruptible
= THREAD_ABORTSAFE
; /* XXX */
3915 boolean_t object_lock_shared
= FALSE
;
3916 memory_object_copy_strategy_t copy_strategy
;
3918 assert(src_object
!= VM_OBJECT_NULL
);
3920 copy_strategy
= src_object
->copy_strategy
;
3922 if (copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
3923 vm_object_lock_shared(src_object
);
3924 object_lock_shared
= TRUE
;
3926 vm_object_lock(src_object
);
3929 * The copy strategy is only valid if the memory manager
3930 * is "ready". Internal objects are always ready.
3933 while (!src_object
->internal
&& !src_object
->pager_ready
) {
3934 wait_result_t wait_result
;
3936 if (object_lock_shared
== TRUE
) {
3937 vm_object_unlock(src_object
);
3938 vm_object_lock(src_object
);
3939 object_lock_shared
= FALSE
;
3942 wait_result
= vm_object_sleep( src_object
,
3943 VM_OBJECT_EVENT_PAGER_READY
,
3945 if (wait_result
!= THREAD_AWAKENED
) {
3946 vm_object_unlock(src_object
);
3947 *dst_object
= VM_OBJECT_NULL
;
3949 *dst_needs_copy
= FALSE
;
3950 return(MACH_SEND_INTERRUPTED
);
3955 * Use the appropriate copy strategy.
3958 switch (copy_strategy
) {
3959 case MEMORY_OBJECT_COPY_DELAY
:
3960 *dst_object
= vm_object_copy_delayed(src_object
,
3961 src_offset
, size
, object_lock_shared
);
3962 if (*dst_object
!= VM_OBJECT_NULL
) {
3963 *dst_offset
= src_offset
;
3964 *dst_needs_copy
= TRUE
;
3965 result
= KERN_SUCCESS
;
3968 vm_object_lock(src_object
);
3969 /* fall thru when delayed copy not allowed */
3971 case MEMORY_OBJECT_COPY_NONE
:
3972 result
= vm_object_copy_slowly(src_object
, src_offset
, size
,
3973 interruptible
, dst_object
);
3974 if (result
== KERN_SUCCESS
) {
3976 *dst_needs_copy
= FALSE
;
3980 case MEMORY_OBJECT_COPY_CALL
:
3981 result
= vm_object_copy_call(src_object
, src_offset
, size
,
3983 if (result
== KERN_SUCCESS
) {
3984 *dst_offset
= src_offset
;
3985 *dst_needs_copy
= TRUE
;
3989 case MEMORY_OBJECT_COPY_SYMMETRIC
:
3990 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);
3991 vm_object_unlock(src_object
);
3992 result
= KERN_MEMORY_RESTART_COPY
;
3996 panic("copy_strategically: bad strategy");
3997 result
= KERN_INVALID_ARGUMENT
;
4005 * Create a new object which is backed by the
4006 * specified existing object range. The source
4007 * object reference is deallocated.
4009 * The new object and offset into that object
4010 * are returned in the source parameters.
4012 boolean_t vm_object_shadow_check
= TRUE
;
4014 __private_extern__ boolean_t
4016 vm_object_t
*object
, /* IN/OUT */
4017 vm_object_offset_t
*offset
, /* IN/OUT */
4018 vm_object_size_t length
)
4020 register vm_object_t source
;
4021 register vm_object_t result
;
4024 assert(source
!= VM_OBJECT_NULL
);
4025 if (source
== VM_OBJECT_NULL
)
4031 * This assertion is valid but it gets triggered by Rosetta for example
4032 * due to a combination of vm_remap() that changes a VM object's
4033 * copy_strategy from SYMMETRIC to DELAY and vm_protect(VM_PROT_COPY)
4034 * that then sets "needs_copy" on its map entry. This creates a
4035 * mapping situation that VM should never see and doesn't know how to
4037 * It's not clear if this can create any real problem but we should
4038 * look into fixing this, probably by having vm_protect(VM_PROT_COPY)
4039 * do more than just set "needs_copy" to handle the copy-on-write...
4040 * In the meantime, let's disable the assertion.
4042 assert(source
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
);
4046 * Determine if we really need a shadow.
4048 * If the source object is larger than what we are trying
4049 * to create, then force the shadow creation even if the
4050 * ref count is 1. This will allow us to [potentially]
4051 * collapse the underlying object away in the future
4052 * (freeing up the extra data it might contain and that
4055 if (vm_object_shadow_check
&&
4056 source
->vo_size
== length
&&
4057 source
->ref_count
== 1 &&
4058 (source
->shadow
== VM_OBJECT_NULL
||
4059 source
->shadow
->copy
== VM_OBJECT_NULL
) )
4061 source
->shadowed
= FALSE
;
4066 * Allocate a new object with the given length
4069 if ((result
= vm_object_allocate(length
)) == VM_OBJECT_NULL
)
4070 panic("vm_object_shadow: no object for shadowing");
4073 * The new object shadows the source object, adding
4074 * a reference to it. Our caller changes his reference
4075 * to point to the new object, removing a reference to
4076 * the source object. Net result: no change of reference
4079 result
->shadow
= source
;
4082 * Store the offset into the source object,
4083 * and fix up the offset into the new object.
4086 result
->vo_shadow_offset
= *offset
;
4089 * Return the new things
4098 * The relationship between vm_object structures and
4099 * the memory_object requires careful synchronization.
4101 * All associations are created by memory_object_create_named
4102 * for external pagers and vm_object_pager_create for internal
4103 * objects as follows:
4105 * pager: the memory_object itself, supplied by
4106 * the user requesting a mapping (or the kernel,
4107 * when initializing internal objects); the
4108 * kernel simulates holding send rights by keeping
4112 * the memory object control port,
4113 * created by the kernel; the kernel holds
4114 * receive (and ownership) rights to this
4115 * port, but no other references.
4117 * When initialization is complete, the "initialized" field
4118 * is asserted. Other mappings using a particular memory object,
4119 * and any references to the vm_object gained through the
4120 * port association must wait for this initialization to occur.
4122 * In order to allow the memory manager to set attributes before
4123 * requests (notably virtual copy operations, but also data or
4124 * unlock requests) are made, a "ready" attribute is made available.
4125 * Only the memory manager may affect the value of this attribute.
4126 * Its value does not affect critical kernel functions, such as
4127 * internal object initialization or destruction. [Furthermore,
4128 * memory objects created by the kernel are assumed to be ready
4129 * immediately; the default memory manager need not explicitly
4130 * set the "ready" attribute.]
4132 * [Both the "initialized" and "ready" attribute wait conditions
4133 * use the "pager" field as the wait event.]
4135 * The port associations can be broken down by any of the
4136 * following routines:
4137 * vm_object_terminate:
4138 * No references to the vm_object remain, and
4139 * the object cannot (or will not) be cached.
4140 * This is the normal case, and is done even
4141 * though one of the other cases has already been
4143 * memory_object_destroy:
4144 * The memory manager has requested that the
4145 * kernel relinquish references to the memory
4146 * object. [The memory manager may not want to
4147 * destroy the memory object, but may wish to
4148 * refuse or tear down existing memory mappings.]
4150 * Each routine that breaks an association must break all of
4151 * them at once. At some later time, that routine must clear
4152 * the pager field and release the memory object references.
4153 * [Furthermore, each routine must cope with the simultaneous
4154 * or previous operations of the others.]
4156 * In addition to the lock on the object, the vm_object_hash_lock
4157 * governs the associations. References gained through the
4158 * association require use of the hash lock.
4160 * Because the pager field may be cleared spontaneously, it
4161 * cannot be used to determine whether a memory object has
4162 * ever been associated with a particular vm_object. [This
4163 * knowledge is important to the shadow object mechanism.]
4164 * For this reason, an additional "created" attribute is
4167 * During various paging operations, the pager reference found in the
4168 * vm_object must be valid. To prevent this from being released,
4169 * (other than being removed, i.e., made null), routines may use
4170 * the vm_object_paging_begin/end routines [actually, macros].
4171 * The implementation uses the "paging_in_progress" and "wanted" fields.
4172 * [Operations that alter the validity of the pager values include the
4173 * termination routines and vm_object_collapse.]
4178 * Routine: vm_object_enter
4180 * Find a VM object corresponding to the given
4181 * pager; if no such object exists, create one,
4182 * and initialize the pager.
4186 memory_object_t pager
,
4187 vm_object_size_t size
,
4192 register vm_object_t object
;
4193 vm_object_t new_object
;
4194 boolean_t must_init
;
4195 vm_object_hash_entry_t entry
, new_entry
;
4196 uint32_t try_failed_count
= 0;
4199 if (pager
== MEMORY_OBJECT_NULL
)
4200 return(vm_object_allocate(size
));
4202 new_object
= VM_OBJECT_NULL
;
4203 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
4207 * Look for an object associated with this port.
4210 lck
= vm_object_hash_lock_spin(pager
);
4212 entry
= vm_object_hash_lookup(pager
, FALSE
);
4214 if (entry
== VM_OBJECT_HASH_ENTRY_NULL
) {
4215 if (new_object
== VM_OBJECT_NULL
) {
4217 * We must unlock to create a new object;
4218 * if we do so, we must try the lookup again.
4220 vm_object_hash_unlock(lck
);
4221 assert(new_entry
== VM_OBJECT_HASH_ENTRY_NULL
);
4222 new_entry
= vm_object_hash_entry_alloc(pager
);
4223 new_object
= vm_object_allocate(size
);
4224 lck
= vm_object_hash_lock_spin(pager
);
4227 * Lookup failed twice, and we have something
4228 * to insert; set the object.
4230 vm_object_hash_insert(new_entry
, new_object
);
4232 new_entry
= VM_OBJECT_HASH_ENTRY_NULL
;
4233 new_object
= VM_OBJECT_NULL
;
4236 } else if (entry
->object
== VM_OBJECT_NULL
) {
4238 * If a previous object is being terminated,
4239 * we must wait for the termination message
4240 * to be queued (and lookup the entry again).
4242 entry
->waiting
= TRUE
;
4243 entry
= VM_OBJECT_HASH_ENTRY_NULL
;
4244 assert_wait((event_t
) pager
, THREAD_UNINT
);
4245 vm_object_hash_unlock(lck
);
4247 thread_block(THREAD_CONTINUE_NULL
);
4248 lck
= vm_object_hash_lock_spin(pager
);
4250 } while (entry
== VM_OBJECT_HASH_ENTRY_NULL
);
4252 object
= entry
->object
;
4253 assert(object
!= VM_OBJECT_NULL
);
4256 if ( !vm_object_lock_try(object
)) {
4258 vm_object_hash_unlock(lck
);
4261 mutex_pause(try_failed_count
); /* wait a bit */
4264 assert(!internal
|| object
->internal
);
4266 if (object
->ref_count
== 0) {
4267 if ( !vm_object_cache_lock_try()) {
4269 vm_object_hash_unlock(lck
);
4270 vm_object_unlock(object
);
4273 mutex_pause(try_failed_count
); /* wait a bit */
4276 XPR(XPR_VM_OBJECT_CACHE
,
4277 "vm_object_enter: removing %x from cache, head (%x, %x)\n",
4279 vm_object_cached_list
.next
,
4280 vm_object_cached_list
.prev
, 0,0);
4281 queue_remove(&vm_object_cached_list
, object
,
4282 vm_object_t
, cached_list
);
4283 vm_object_cached_count
--;
4285 vm_object_cache_unlock();
4289 assert(!object
->named
);
4290 object
->named
= TRUE
;
4292 vm_object_lock_assert_exclusive(object
);
4293 object
->ref_count
++;
4294 vm_object_res_reference(object
);
4296 vm_object_hash_unlock(lck
);
4297 vm_object_unlock(object
);
4301 vm_object_hash_unlock(lck
);
4303 assert(object
->ref_count
> 0);
4305 VM_STAT_INCR(lookups
);
4308 "vm_o_enter: pager 0x%x obj 0x%x must_init %d\n",
4309 pager
, object
, must_init
, 0, 0);
4312 * If we raced to create a vm_object but lost, let's
4316 if (new_object
!= VM_OBJECT_NULL
)
4317 vm_object_deallocate(new_object
);
4319 if (new_entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
4320 vm_object_hash_entry_free(new_entry
);
4323 memory_object_control_t control
;
4326 * Allocate request port.
4329 control
= memory_object_control_allocate(object
);
4330 assert (control
!= MEMORY_OBJECT_CONTROL_NULL
);
4332 vm_object_lock(object
);
4333 assert(object
!= kernel_object
);
4336 * Copy the reference we were given.
4339 memory_object_reference(pager
);
4340 object
->pager_created
= TRUE
;
4341 object
->pager
= pager
;
4342 object
->internal
= internal
;
4343 object
->pager_trusted
= internal
;
4345 /* copy strategy invalid until set by memory manager */
4346 object
->copy_strategy
= MEMORY_OBJECT_COPY_INVALID
;
4348 object
->pager_control
= control
;
4349 object
->pager_ready
= FALSE
;
4351 vm_object_unlock(object
);
4354 * Let the pager know we're using it.
4357 (void) memory_object_init(pager
,
4358 object
->pager_control
,
4361 vm_object_lock(object
);
4363 object
->named
= TRUE
;
4365 object
->pager_ready
= TRUE
;
4366 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
4369 object
->pager_initialized
= TRUE
;
4370 vm_object_wakeup(object
, VM_OBJECT_EVENT_INITIALIZED
);
4372 vm_object_lock(object
);
4376 * [At this point, the object must be locked]
4380 * Wait for the work above to be done by the first
4381 * thread to map this object.
4384 while (!object
->pager_initialized
) {
4385 vm_object_sleep(object
,
4386 VM_OBJECT_EVENT_INITIALIZED
,
4389 vm_object_unlock(object
);
4392 "vm_object_enter: vm_object %x, memory_object %x, internal %d\n",
4393 object
, object
->pager
, internal
, 0,0);
4398 * Routine: vm_object_pager_create
4400 * Create a memory object for an internal object.
4401 * In/out conditions:
4402 * The object is locked on entry and exit;
4403 * it may be unlocked within this call.
4405 * Only one thread may be performing a
4406 * vm_object_pager_create on an object at
4407 * a time. Presumably, only the pageout
4408 * daemon will be using this routine.
4412 vm_object_pager_create(
4413 register vm_object_t object
)
4415 memory_object_t pager
;
4416 vm_object_hash_entry_t entry
;
4419 vm_object_size_t size
;
4420 vm_external_map_t map
;
4421 #endif /* MACH_PAGEMAP */
4423 XPR(XPR_VM_OBJECT
, "vm_object_pager_create, object 0x%X\n",
4426 assert(object
!= kernel_object
);
4428 if (memory_manager_default_check() != KERN_SUCCESS
)
4432 * Prevent collapse or termination by holding a paging reference
4435 vm_object_paging_begin(object
);
4436 if (object
->pager_created
) {
4438 * Someone else got to it first...
4439 * wait for them to finish initializing the ports
4441 while (!object
->pager_initialized
) {
4442 vm_object_sleep(object
,
4443 VM_OBJECT_EVENT_INITIALIZED
,
4446 vm_object_paging_end(object
);
4451 * Indicate that a memory object has been assigned
4452 * before dropping the lock, to prevent a race.
4455 object
->pager_created
= TRUE
;
4456 object
->paging_offset
= 0;
4459 size
= object
->vo_size
;
4460 #endif /* MACH_PAGEMAP */
4461 vm_object_unlock(object
);
4464 if (DEFAULT_PAGER_IS_ACTIVE
) {
4465 map
= vm_external_create(size
);
4466 vm_object_lock(object
);
4467 assert(object
->vo_size
== size
);
4468 object
->existence_map
= map
;
4469 vm_object_unlock(object
);
4471 #endif /* MACH_PAGEMAP */
4473 if ((uint32_t) object
->vo_size
!= object
->vo_size
) {
4474 panic("vm_object_pager_create(): object size 0x%llx >= 4GB\n",
4475 (uint64_t) object
->vo_size
);
4479 * Create the [internal] pager, and associate it with this object.
4481 * We make the association here so that vm_object_enter()
4482 * can look up the object to complete initializing it. No
4483 * user will ever map this object.
4486 memory_object_default_t dmm
;
4488 /* acquire a reference for the default memory manager */
4489 dmm
= memory_manager_default_reference();
4491 assert(object
->temporary
);
4493 /* create our new memory object */
4494 assert((vm_size_t
) object
->vo_size
== object
->vo_size
);
4495 (void) memory_object_create(dmm
, (vm_size_t
) object
->vo_size
,
4498 memory_object_default_deallocate(dmm
);
4501 entry
= vm_object_hash_entry_alloc(pager
);
4503 lck
= vm_object_hash_lock_spin(pager
);
4504 vm_object_hash_insert(entry
, object
);
4505 vm_object_hash_unlock(lck
);
4508 * A reference was returned by
4509 * memory_object_create(), and it is
4510 * copied by vm_object_enter().
4513 if (vm_object_enter(pager
, object
->vo_size
, TRUE
, TRUE
, FALSE
) != object
)
4514 panic("vm_object_pager_create: mismatch");
4517 * Drop the reference we were passed.
4519 memory_object_deallocate(pager
);
4521 vm_object_lock(object
);
4524 * Release the paging reference
4526 vm_object_paging_end(object
);
4530 vm_object_compressor_pager_create(
4531 register vm_object_t object
)
4533 memory_object_t pager
;
4534 vm_object_hash_entry_t entry
;
4537 assert(object
!= kernel_object
);
4540 * Prevent collapse or termination by holding a paging reference
4543 vm_object_paging_begin(object
);
4544 if (object
->pager_created
) {
4546 * Someone else got to it first...
4547 * wait for them to finish initializing the ports
4549 while (!object
->pager_initialized
) {
4550 vm_object_sleep(object
,
4551 VM_OBJECT_EVENT_INITIALIZED
,
4554 vm_object_paging_end(object
);
4559 * Indicate that a memory object has been assigned
4560 * before dropping the lock, to prevent a race.
4563 object
->pager_created
= TRUE
;
4564 object
->paging_offset
= 0;
4566 vm_object_unlock(object
);
4568 if ((uint32_t) object
->vo_size
!= object
->vo_size
) {
4569 panic("vm_object_compressor_pager_create(): object size 0x%llx >= 4GB\n",
4570 (uint64_t) object
->vo_size
);
4574 * Create the [internal] pager, and associate it with this object.
4576 * We make the association here so that vm_object_enter()
4577 * can look up the object to complete initializing it. No
4578 * user will ever map this object.
4581 assert(object
->temporary
);
4583 /* create our new memory object */
4584 assert((vm_size_t
) object
->vo_size
== object
->vo_size
);
4585 (void) compressor_memory_object_create(
4586 (vm_size_t
) object
->vo_size
,
4590 entry
= vm_object_hash_entry_alloc(pager
);
4592 lck
= vm_object_hash_lock_spin(pager
);
4593 vm_object_hash_insert(entry
, object
);
4594 vm_object_hash_unlock(lck
);
4597 * A reference was returned by
4598 * memory_object_create(), and it is
4599 * copied by vm_object_enter().
4602 if (vm_object_enter(pager
, object
->vo_size
, TRUE
, TRUE
, FALSE
) != object
)
4603 panic("vm_object_compressor_pager_create: mismatch");
4606 * Drop the reference we were passed.
4608 memory_object_deallocate(pager
);
4610 vm_object_lock(object
);
4613 * Release the paging reference
4615 vm_object_paging_end(object
);
4619 * Routine: vm_object_remove
4621 * Eliminate the pager/object association
4624 * The object cache must be locked.
4626 __private_extern__
void
4630 memory_object_t pager
;
4632 if ((pager
= object
->pager
) != MEMORY_OBJECT_NULL
) {
4633 vm_object_hash_entry_t entry
;
4635 entry
= vm_object_hash_lookup(pager
, FALSE
);
4636 if (entry
!= VM_OBJECT_HASH_ENTRY_NULL
)
4637 entry
->object
= VM_OBJECT_NULL
;
4643 * Global variables for vm_object_collapse():
4645 * Counts for normal collapses and bypasses.
4646 * Debugging variables, to watch or disable collapse.
4648 static long object_collapses
= 0;
4649 static long object_bypasses
= 0;
4651 static boolean_t vm_object_collapse_allowed
= TRUE
;
4652 static boolean_t vm_object_bypass_allowed
= TRUE
;
4655 static int vm_external_discarded
;
4656 static int vm_external_collapsed
;
4659 unsigned long vm_object_collapse_encrypted
= 0;
4662 * Routine: vm_object_do_collapse
4664 * Collapse an object with the object backing it.
4665 * Pages in the backing object are moved into the
4666 * parent, and the backing object is deallocated.
4668 * Both objects and the cache are locked; the page
4669 * queues are unlocked.
4673 vm_object_do_collapse(
4675 vm_object_t backing_object
)
4678 vm_object_offset_t new_offset
, backing_offset
;
4679 vm_object_size_t size
;
4681 vm_object_lock_assert_exclusive(object
);
4682 vm_object_lock_assert_exclusive(backing_object
);
4684 backing_offset
= object
->vo_shadow_offset
;
4685 size
= object
->vo_size
;
4688 * Move all in-memory pages from backing_object
4689 * to the parent. Pages that have been paged out
4690 * will be overwritten by any of the parent's
4691 * pages that shadow them.
4694 while (!queue_empty(&backing_object
->memq
)) {
4696 p
= (vm_page_t
) queue_first(&backing_object
->memq
);
4698 new_offset
= (p
->offset
- backing_offset
);
4700 assert(!p
->busy
|| p
->absent
);
4703 * If the parent has a page here, or if
4704 * this page falls outside the parent,
4707 * Otherwise, move it as planned.
4710 if (p
->offset
< backing_offset
|| new_offset
>= size
) {
4715 * The encryption key includes the "pager" and the
4716 * "paging_offset". These will not change during the
4717 * object collapse, so we can just move an encrypted
4718 * page from one object to the other in this case.
4719 * We can't decrypt the page here, since we can't drop
4723 vm_object_collapse_encrypted
++;
4725 pp
= vm_page_lookup(object
, new_offset
);
4726 if (pp
== VM_PAGE_NULL
) {
4729 * Parent now has no page.
4730 * Move the backing object's page up.
4733 vm_page_rename(p
, object
, new_offset
, TRUE
);
4735 } else if (pp
->absent
) {
4738 * Parent has an absent page...
4739 * it's not being paged in, so
4740 * it must really be missing from
4743 * Throw out the absent page...
4744 * any faults looking for that
4745 * page will restart with the new
4750 vm_page_rename(p
, object
, new_offset
, TRUE
);
4751 #endif /* MACH_PAGEMAP */
4753 assert(! pp
->absent
);
4756 * Parent object has a real page.
4757 * Throw away the backing object's
4766 assert((!object
->pager_created
&& (object
->pager
== MEMORY_OBJECT_NULL
))
4767 || (!backing_object
->pager_created
4768 && (backing_object
->pager
== MEMORY_OBJECT_NULL
)));
4770 assert(!object
->pager_created
&& object
->pager
== MEMORY_OBJECT_NULL
);
4771 #endif /* !MACH_PAGEMAP */
4773 if (backing_object
->pager
!= MEMORY_OBJECT_NULL
) {
4774 vm_object_hash_entry_t entry
;
4777 if (COMPRESSED_PAGER_IS_ACTIVE
) {
4778 panic("vm_object_do_collapse(%p,%p): "
4779 "backing_object has a compressor pager",
4780 object
, backing_object
);
4785 * Move the pager from backing_object to object.
4787 * XXX We're only using part of the paging space
4788 * for keeps now... we ought to discard the
4792 assert(!object
->paging_in_progress
);
4793 assert(!object
->activity_in_progress
);
4794 object
->pager
= backing_object
->pager
;
4796 if (backing_object
->hashed
) {
4799 lck
= vm_object_hash_lock_spin(backing_object
->pager
);
4800 entry
= vm_object_hash_lookup(object
->pager
, FALSE
);
4801 assert(entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
4802 entry
->object
= object
;
4803 vm_object_hash_unlock(lck
);
4805 object
->hashed
= TRUE
;
4807 object
->pager_created
= backing_object
->pager_created
;
4808 object
->pager_control
= backing_object
->pager_control
;
4809 object
->pager_ready
= backing_object
->pager_ready
;
4810 object
->pager_initialized
= backing_object
->pager_initialized
;
4811 object
->paging_offset
=
4812 backing_object
->paging_offset
+ backing_offset
;
4813 if (object
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
4814 memory_object_control_collapse(object
->pager_control
,
4821 * If the shadow offset is 0, the use the existence map from
4822 * the backing object if there is one. If the shadow offset is
4823 * not zero, toss it.
4825 * XXX - If the shadow offset is not 0 then a bit copy is needed
4826 * if the map is to be salvaged. For now, we just just toss the
4827 * old map, giving the collapsed object no map. This means that
4828 * the pager is invoked for zero fill pages. If analysis shows
4829 * that this happens frequently and is a performance hit, then
4830 * this code should be fixed to salvage the map.
4832 assert(object
->existence_map
== VM_EXTERNAL_NULL
);
4833 if (backing_offset
|| (size
!= backing_object
->vo_size
)) {
4834 vm_external_discarded
++;
4835 vm_external_destroy(backing_object
->existence_map
,
4836 backing_object
->vo_size
);
4839 vm_external_collapsed
++;
4840 object
->existence_map
= backing_object
->existence_map
;
4842 backing_object
->existence_map
= VM_EXTERNAL_NULL
;
4843 #endif /* MACH_PAGEMAP */
4846 * Object now shadows whatever backing_object did.
4847 * Note that the reference to backing_object->shadow
4848 * moves from within backing_object to within object.
4851 assert(!object
->phys_contiguous
);
4852 assert(!backing_object
->phys_contiguous
);
4853 object
->shadow
= backing_object
->shadow
;
4854 if (object
->shadow
) {
4855 object
->vo_shadow_offset
+= backing_object
->vo_shadow_offset
;
4857 /* no shadow, therefore no shadow offset... */
4858 object
->vo_shadow_offset
= 0;
4860 assert((object
->shadow
== VM_OBJECT_NULL
) ||
4861 (object
->shadow
->copy
!= backing_object
));
4864 * Discard backing_object.
4866 * Since the backing object has no pages, no
4867 * pager left, and no object references within it,
4868 * all that is necessary is to dispose of it.
4871 assert((backing_object
->ref_count
== 1) &&
4872 (backing_object
->resident_page_count
== 0) &&
4873 (backing_object
->paging_in_progress
== 0) &&
4874 (backing_object
->activity_in_progress
== 0));
4876 backing_object
->alive
= FALSE
;
4877 vm_object_unlock(backing_object
);
4879 XPR(XPR_VM_OBJECT
, "vm_object_collapse, collapsed 0x%X\n",
4880 backing_object
, 0,0,0,0);
4882 vm_object_lock_destroy(backing_object
);
4884 zfree(vm_object_zone
, backing_object
);
4890 vm_object_do_bypass(
4892 vm_object_t backing_object
)
4895 * Make the parent shadow the next object
4899 vm_object_lock_assert_exclusive(object
);
4900 vm_object_lock_assert_exclusive(backing_object
);
4904 * Do object reference in-line to
4905 * conditionally increment shadow's
4906 * residence count. If object is not
4907 * resident, leave residence count
4910 if (backing_object
->shadow
!= VM_OBJECT_NULL
) {
4911 vm_object_lock(backing_object
->shadow
);
4912 vm_object_lock_assert_exclusive(backing_object
->shadow
);
4913 backing_object
->shadow
->ref_count
++;
4914 if (object
->res_count
!= 0)
4915 vm_object_res_reference(backing_object
->shadow
);
4916 vm_object_unlock(backing_object
->shadow
);
4918 #else /* TASK_SWAPPER */
4919 vm_object_reference(backing_object
->shadow
);
4920 #endif /* TASK_SWAPPER */
4922 assert(!object
->phys_contiguous
);
4923 assert(!backing_object
->phys_contiguous
);
4924 object
->shadow
= backing_object
->shadow
;
4925 if (object
->shadow
) {
4926 object
->vo_shadow_offset
+= backing_object
->vo_shadow_offset
;
4928 /* no shadow, therefore no shadow offset... */
4929 object
->vo_shadow_offset
= 0;
4933 * Backing object might have had a copy pointer
4934 * to us. If it did, clear it.
4936 if (backing_object
->copy
== object
) {
4937 backing_object
->copy
= VM_OBJECT_NULL
;
4941 * Drop the reference count on backing_object.
4943 * Since its ref_count was at least 2, it
4944 * will not vanish; so we don't need to call
4945 * vm_object_deallocate.
4946 * [with a caveat for "named" objects]
4948 * The res_count on the backing object is
4949 * conditionally decremented. It's possible
4950 * (via vm_pageout_scan) to get here with
4951 * a "swapped" object, which has a 0 res_count,
4952 * in which case, the backing object res_count
4953 * is already down by one.
4955 * Don't call vm_object_deallocate unless
4956 * ref_count drops to zero.
4958 * The ref_count can drop to zero here if the
4959 * backing object could be bypassed but not
4960 * collapsed, such as when the backing object
4961 * is temporary and cachable.
4964 if (backing_object
->ref_count
> 2 ||
4965 (!backing_object
->named
&& backing_object
->ref_count
> 1)) {
4966 vm_object_lock_assert_exclusive(backing_object
);
4967 backing_object
->ref_count
--;
4969 if (object
->res_count
!= 0)
4970 vm_object_res_deallocate(backing_object
);
4971 assert(backing_object
->ref_count
> 0);
4972 #endif /* TASK_SWAPPER */
4973 vm_object_unlock(backing_object
);
4977 * Drop locks so that we can deallocate
4978 * the backing object.
4982 if (object
->res_count
== 0) {
4983 /* XXX get a reference for the deallocate below */
4984 vm_object_res_reference(backing_object
);
4986 #endif /* TASK_SWAPPER */
4988 * vm_object_collapse (the caller of this function) is
4989 * now called from contexts that may not guarantee that a
4990 * valid reference is held on the object... w/o a valid
4991 * reference, it is unsafe and unwise (you will definitely
4992 * regret it) to unlock the object and then retake the lock
4993 * since the object may be terminated and recycled in between.
4994 * The "activity_in_progress" reference will keep the object
4997 vm_object_activity_begin(object
);
4998 vm_object_unlock(object
);
5000 vm_object_unlock(backing_object
);
5001 vm_object_deallocate(backing_object
);
5004 * Relock object. We don't have to reverify
5005 * its state since vm_object_collapse will
5006 * do that for us as it starts at the
5010 vm_object_lock(object
);
5011 vm_object_activity_end(object
);
5019 * vm_object_collapse:
5021 * Perform an object collapse or an object bypass if appropriate.
5022 * The real work of collapsing and bypassing is performed in
5023 * the routines vm_object_do_collapse and vm_object_do_bypass.
5025 * Requires that the object be locked and the page queues be unlocked.
5028 static unsigned long vm_object_collapse_calls
= 0;
5029 static unsigned long vm_object_collapse_objects
= 0;
5030 static unsigned long vm_object_collapse_do_collapse
= 0;
5031 static unsigned long vm_object_collapse_do_bypass
= 0;
5033 __private_extern__
void
5035 register vm_object_t object
,
5036 register vm_object_offset_t hint_offset
,
5037 boolean_t can_bypass
)
5039 register vm_object_t backing_object
;
5040 register unsigned int rcount
;
5041 register unsigned int size
;
5042 vm_object_t original_object
;
5043 int object_lock_type
;
5044 int backing_object_lock_type
;
5046 vm_object_collapse_calls
++;
5048 if (! vm_object_collapse_allowed
&&
5049 ! (can_bypass
&& vm_object_bypass_allowed
)) {
5053 XPR(XPR_VM_OBJECT
, "vm_object_collapse, obj 0x%X\n",
5056 if (object
== VM_OBJECT_NULL
)
5059 original_object
= object
;
5062 * The top object was locked "exclusive" by the caller.
5063 * In the first pass, to determine if we can collapse the shadow chain,
5064 * take a "shared" lock on the shadow objects. If we can collapse,
5065 * we'll have to go down the chain again with exclusive locks.
5067 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
5068 backing_object_lock_type
= OBJECT_LOCK_SHARED
;
5071 object
= original_object
;
5072 vm_object_lock_assert_exclusive(object
);
5075 vm_object_collapse_objects
++;
5077 * Verify that the conditions are right for either
5078 * collapse or bypass:
5082 * There is a backing object, and
5085 backing_object
= object
->shadow
;
5086 if (backing_object
== VM_OBJECT_NULL
) {
5087 if (object
!= original_object
) {
5088 vm_object_unlock(object
);
5092 if (backing_object_lock_type
== OBJECT_LOCK_SHARED
) {
5093 vm_object_lock_shared(backing_object
);
5095 vm_object_lock(backing_object
);
5099 * No pages in the object are currently
5100 * being paged out, and
5102 if (object
->paging_in_progress
!= 0 ||
5103 object
->activity_in_progress
!= 0) {
5104 /* try and collapse the rest of the shadow chain */
5105 if (object
!= original_object
) {
5106 vm_object_unlock(object
);
5108 object
= backing_object
;
5109 object_lock_type
= backing_object_lock_type
;
5115 * The backing object is not read_only,
5116 * and no pages in the backing object are
5117 * currently being paged out.
5118 * The backing object is internal.
5122 if (!backing_object
->internal
||
5123 backing_object
->paging_in_progress
!= 0 ||
5124 backing_object
->activity_in_progress
!= 0) {
5125 /* try and collapse the rest of the shadow chain */
5126 if (object
!= original_object
) {
5127 vm_object_unlock(object
);
5129 object
= backing_object
;
5130 object_lock_type
= backing_object_lock_type
;
5135 * The backing object can't be a copy-object:
5136 * the shadow_offset for the copy-object must stay
5137 * as 0. Furthermore (for the 'we have all the
5138 * pages' case), if we bypass backing_object and
5139 * just shadow the next object in the chain, old
5140 * pages from that object would then have to be copied
5141 * BOTH into the (former) backing_object and into the
5144 if (backing_object
->shadow
!= VM_OBJECT_NULL
&&
5145 backing_object
->shadow
->copy
== backing_object
) {
5146 /* try and collapse the rest of the shadow chain */
5147 if (object
!= original_object
) {
5148 vm_object_unlock(object
);
5150 object
= backing_object
;
5151 object_lock_type
= backing_object_lock_type
;
5156 * We can now try to either collapse the backing
5157 * object (if the parent is the only reference to
5158 * it) or (perhaps) remove the parent's reference
5161 * If there is exactly one reference to the backing
5162 * object, we may be able to collapse it into the
5165 * If MACH_PAGEMAP is defined:
5166 * The parent must not have a pager created for it,
5167 * since collapsing a backing_object dumps new pages
5168 * into the parent that its pager doesn't know about
5169 * (and the collapse code can't merge the existence
5172 * As long as one of the objects is still not known
5173 * to the pager, we can collapse them.
5175 if (backing_object
->ref_count
== 1 &&
5176 (!object
->pager_created
5178 || (!backing_object
->pager_created
)
5179 #endif /*!MACH_PAGEMAP */
5180 ) && vm_object_collapse_allowed
) {
5183 * We need the exclusive lock on the VM objects.
5185 if (backing_object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
5187 * We have an object and its shadow locked
5188 * "shared". We can't just upgrade the locks
5189 * to "exclusive", as some other thread might
5190 * also have these objects locked "shared" and
5191 * attempt to upgrade one or the other to
5192 * "exclusive". The upgrades would block
5193 * forever waiting for the other "shared" locks
5195 * So we have to release the locks and go
5196 * down the shadow chain again (since it could
5197 * have changed) with "exclusive" locking.
5199 vm_object_unlock(backing_object
);
5200 if (object
!= original_object
)
5201 vm_object_unlock(object
);
5202 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
5203 backing_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
5208 "vm_object_collapse: %x to %x, pager %x, pager_control %x\n",
5209 backing_object
, object
,
5210 backing_object
->pager
,
5211 backing_object
->pager_control
, 0);
5214 * Collapse the object with its backing
5215 * object, and try again with the object's
5216 * new backing object.
5219 vm_object_do_collapse(object
, backing_object
);
5220 vm_object_collapse_do_collapse
++;
5225 * Collapsing the backing object was not possible
5226 * or permitted, so let's try bypassing it.
5229 if (! (can_bypass
&& vm_object_bypass_allowed
)) {
5230 /* try and collapse the rest of the shadow chain */
5231 if (object
!= original_object
) {
5232 vm_object_unlock(object
);
5234 object
= backing_object
;
5235 object_lock_type
= backing_object_lock_type
;
5241 * If the object doesn't have all its pages present,
5242 * we have to make sure no pages in the backing object
5243 * "show through" before bypassing it.
5245 size
= (unsigned int)atop(object
->vo_size
);
5246 rcount
= object
->resident_page_count
;
5248 if (rcount
!= size
) {
5249 vm_object_offset_t offset
;
5250 vm_object_offset_t backing_offset
;
5251 unsigned int backing_rcount
;
5254 * If the backing object has a pager but no pagemap,
5255 * then we cannot bypass it, because we don't know
5256 * what pages it has.
5258 if (backing_object
->pager_created
5260 && (backing_object
->existence_map
== VM_EXTERNAL_NULL
)
5261 #endif /* MACH_PAGEMAP */
5263 /* try and collapse the rest of the shadow chain */
5264 if (object
!= original_object
) {
5265 vm_object_unlock(object
);
5267 object
= backing_object
;
5268 object_lock_type
= backing_object_lock_type
;
5273 * If the object has a pager but no pagemap,
5274 * then we cannot bypass it, because we don't know
5275 * what pages it has.
5277 if (object
->pager_created
5279 && (object
->existence_map
== VM_EXTERNAL_NULL
)
5280 #endif /* MACH_PAGEMAP */
5282 /* try and collapse the rest of the shadow chain */
5283 if (object
!= original_object
) {
5284 vm_object_unlock(object
);
5286 object
= backing_object
;
5287 object_lock_type
= backing_object_lock_type
;
5291 backing_offset
= object
->vo_shadow_offset
;
5292 backing_rcount
= backing_object
->resident_page_count
;
5294 if ( (int)backing_rcount
- (int)(atop(backing_object
->vo_size
) - size
) > (int)rcount
) {
5296 * we have enough pages in the backing object to guarantee that
5297 * at least 1 of them must be 'uncovered' by a resident page
5298 * in the object we're evaluating, so move on and
5299 * try to collapse the rest of the shadow chain
5301 if (object
!= original_object
) {
5302 vm_object_unlock(object
);
5304 object
= backing_object
;
5305 object_lock_type
= backing_object_lock_type
;
5310 * If all of the pages in the backing object are
5311 * shadowed by the parent object, the parent
5312 * object no longer has to shadow the backing
5313 * object; it can shadow the next one in the
5316 * If the backing object has existence info,
5317 * we must check examine its existence info
5323 #define EXISTS_IN_OBJECT(obj, off, rc) \
5324 ((vm_external_state_get((obj)->existence_map, \
5325 (vm_offset_t)(off)) \
5326 == VM_EXTERNAL_STATE_EXISTS) || \
5327 (VM_COMPRESSOR_PAGER_STATE_GET((obj), (off)) \
5328 == VM_EXTERNAL_STATE_EXISTS) || \
5329 ((rc) && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
5330 #else /* MACH_PAGEMAP */
5331 #define EXISTS_IN_OBJECT(obj, off, rc) \
5332 ((VM_COMPRESSOR_PAGER_STATE_GET((obj), (off)) \
5333 == VM_EXTERNAL_STATE_EXISTS) || \
5334 ((rc) && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
5335 #endif /* MACH_PAGEMAP */
5338 * Check the hint location first
5339 * (since it is often the quickest way out of here).
5341 if (object
->cow_hint
!= ~(vm_offset_t
)0)
5342 hint_offset
= (vm_object_offset_t
)object
->cow_hint
;
5344 hint_offset
= (hint_offset
> 8 * PAGE_SIZE_64
) ?
5345 (hint_offset
- 8 * PAGE_SIZE_64
) : 0;
5347 if (EXISTS_IN_OBJECT(backing_object
, hint_offset
+
5348 backing_offset
, backing_rcount
) &&
5349 !EXISTS_IN_OBJECT(object
, hint_offset
, rcount
)) {
5350 /* dependency right at the hint */
5351 object
->cow_hint
= (vm_offset_t
) hint_offset
; /* atomic */
5352 /* try and collapse the rest of the shadow chain */
5353 if (object
!= original_object
) {
5354 vm_object_unlock(object
);
5356 object
= backing_object
;
5357 object_lock_type
= backing_object_lock_type
;
5362 * If the object's window onto the backing_object
5363 * is large compared to the number of resident
5364 * pages in the backing object, it makes sense to
5365 * walk the backing_object's resident pages first.
5367 * NOTE: Pages may be in both the existence map and/or
5368 * resident, so if we don't find a dependency while
5369 * walking the backing object's resident page list
5370 * directly, and there is an existence map, we'll have
5371 * to run the offset based 2nd pass. Because we may
5372 * have to run both passes, we need to be careful
5373 * not to decrement 'rcount' in the 1st pass
5375 if (backing_rcount
&& backing_rcount
< (size
/ 8)) {
5376 unsigned int rc
= rcount
;
5379 backing_rcount
= backing_object
->resident_page_count
;
5380 p
= (vm_page_t
)queue_first(&backing_object
->memq
);
5382 offset
= (p
->offset
- backing_offset
);
5384 if (offset
< object
->vo_size
&&
5385 offset
!= hint_offset
&&
5386 !EXISTS_IN_OBJECT(object
, offset
, rc
)) {
5387 /* found a dependency */
5388 object
->cow_hint
= (vm_offset_t
) offset
; /* atomic */
5392 p
= (vm_page_t
) queue_next(&p
->listq
);
5394 } while (--backing_rcount
);
5395 if (backing_rcount
!= 0 ) {
5396 /* try and collapse the rest of the shadow chain */
5397 if (object
!= original_object
) {
5398 vm_object_unlock(object
);
5400 object
= backing_object
;
5401 object_lock_type
= backing_object_lock_type
;
5407 * Walk through the offsets looking for pages in the
5408 * backing object that show through to the object.
5412 || backing_object
->existence_map
5413 #endif /* MACH_PAGEMAP */
5415 offset
= hint_offset
;
5418 (offset
+ PAGE_SIZE_64
< object
->vo_size
) ?
5419 (offset
+ PAGE_SIZE_64
) : 0) != hint_offset
) {
5421 if (EXISTS_IN_OBJECT(backing_object
, offset
+
5422 backing_offset
, backing_rcount
) &&
5423 !EXISTS_IN_OBJECT(object
, offset
, rcount
)) {
5424 /* found a dependency */
5425 object
->cow_hint
= (vm_offset_t
) offset
; /* atomic */
5429 if (offset
!= hint_offset
) {
5430 /* try and collapse the rest of the shadow chain */
5431 if (object
!= original_object
) {
5432 vm_object_unlock(object
);
5434 object
= backing_object
;
5435 object_lock_type
= backing_object_lock_type
;
5442 * We need "exclusive" locks on the 2 VM objects.
5444 if (backing_object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
5445 vm_object_unlock(backing_object
);
5446 if (object
!= original_object
)
5447 vm_object_unlock(object
);
5448 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
5449 backing_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
5453 /* reset the offset hint for any objects deeper in the chain */
5454 object
->cow_hint
= (vm_offset_t
)0;
5457 * All interesting pages in the backing object
5458 * already live in the parent or its pager.
5459 * Thus we can bypass the backing object.
5462 vm_object_do_bypass(object
, backing_object
);
5463 vm_object_collapse_do_bypass
++;
5466 * Try again with this object's new backing object.
5472 if (object
!= original_object
) {
5473 vm_object_unlock(object
);
5478 * Routine: vm_object_page_remove: [internal]
5480 * Removes all physical pages in the specified
5481 * object range from the object's list of pages.
5483 * In/out conditions:
5484 * The object must be locked.
5485 * The object must not have paging_in_progress, usually
5486 * guaranteed by not having a pager.
5488 unsigned int vm_object_page_remove_lookup
= 0;
5489 unsigned int vm_object_page_remove_iterate
= 0;
5491 __private_extern__
void
5492 vm_object_page_remove(
5493 register vm_object_t object
,
5494 register vm_object_offset_t start
,
5495 register vm_object_offset_t end
)
5497 register vm_page_t p
, next
;
5500 * One and two page removals are most popular.
5501 * The factor of 16 here is somewhat arbitrary.
5502 * It balances vm_object_lookup vs iteration.
5505 if (atop_64(end
- start
) < (unsigned)object
->resident_page_count
/16) {
5506 vm_object_page_remove_lookup
++;
5508 for (; start
< end
; start
+= PAGE_SIZE_64
) {
5509 p
= vm_page_lookup(object
, start
);
5510 if (p
!= VM_PAGE_NULL
) {
5511 assert(!p
->cleaning
&& !p
->pageout
&& !p
->laundry
);
5512 if (!p
->fictitious
&& p
->pmapped
)
5513 pmap_disconnect(p
->phys_page
);
5518 vm_object_page_remove_iterate
++;
5520 p
= (vm_page_t
) queue_first(&object
->memq
);
5521 while (!queue_end(&object
->memq
, (queue_entry_t
) p
)) {
5522 next
= (vm_page_t
) queue_next(&p
->listq
);
5523 if ((start
<= p
->offset
) && (p
->offset
< end
)) {
5524 assert(!p
->cleaning
&& !p
->pageout
&& !p
->laundry
);
5525 if (!p
->fictitious
&& p
->pmapped
)
5526 pmap_disconnect(p
->phys_page
);
5536 * Routine: vm_object_coalesce
5537 * Function: Coalesces two objects backing up adjoining
5538 * regions of memory into a single object.
5540 * returns TRUE if objects were combined.
5542 * NOTE: Only works at the moment if the second object is NULL -
5543 * if it's not, which object do we lock first?
5546 * prev_object First object to coalesce
5547 * prev_offset Offset into prev_object
5548 * next_object Second object into coalesce
5549 * next_offset Offset into next_object
5551 * prev_size Size of reference to prev_object
5552 * next_size Size of reference to next_object
5555 * The object(s) must *not* be locked. The map must be locked
5556 * to preserve the reference to the object(s).
5558 static int vm_object_coalesce_count
= 0;
5560 __private_extern__ boolean_t
5562 register vm_object_t prev_object
,
5563 vm_object_t next_object
,
5564 vm_object_offset_t prev_offset
,
5565 __unused vm_object_offset_t next_offset
,
5566 vm_object_size_t prev_size
,
5567 vm_object_size_t next_size
)
5569 vm_object_size_t newsize
;
5575 if (next_object
!= VM_OBJECT_NULL
) {
5579 if (prev_object
== VM_OBJECT_NULL
) {
5584 "vm_object_coalesce: 0x%X prev_off 0x%X prev_size 0x%X next_size 0x%X\n",
5585 prev_object
, prev_offset
, prev_size
, next_size
, 0);
5587 vm_object_lock(prev_object
);
5590 * Try to collapse the object first
5592 vm_object_collapse(prev_object
, prev_offset
, TRUE
);
5595 * Can't coalesce if pages not mapped to
5596 * prev_entry may be in use any way:
5597 * . more than one reference
5599 * . shadows another object
5600 * . has a copy elsewhere
5602 * . paging references (pages might be in page-list)
5605 if ((prev_object
->ref_count
> 1) ||
5606 prev_object
->pager_created
||
5607 (prev_object
->shadow
!= VM_OBJECT_NULL
) ||
5608 (prev_object
->copy
!= VM_OBJECT_NULL
) ||
5609 (prev_object
->true_share
!= FALSE
) ||
5610 (prev_object
->purgable
!= VM_PURGABLE_DENY
) ||
5611 (prev_object
->paging_in_progress
!= 0) ||
5612 (prev_object
->activity_in_progress
!= 0)) {
5613 vm_object_unlock(prev_object
);
5617 vm_object_coalesce_count
++;
5620 * Remove any pages that may still be in the object from
5621 * a previous deallocation.
5623 vm_object_page_remove(prev_object
,
5624 prev_offset
+ prev_size
,
5625 prev_offset
+ prev_size
+ next_size
);
5628 * Extend the object if necessary.
5630 newsize
= prev_offset
+ prev_size
+ next_size
;
5631 if (newsize
> prev_object
->vo_size
) {
5634 * We cannot extend an object that has existence info,
5635 * since the existence info might then fail to cover
5636 * the entire object.
5638 * This assertion must be true because the object
5639 * has no pager, and we only create existence info
5640 * for objects with pagers.
5642 assert(prev_object
->existence_map
== VM_EXTERNAL_NULL
);
5643 #endif /* MACH_PAGEMAP */
5644 prev_object
->vo_size
= newsize
;
5647 vm_object_unlock(prev_object
);
5652 * Attach a set of physical pages to an object, so that they can
5653 * be mapped by mapping the object. Typically used to map IO memory.
5655 * The mapping function and its private data are used to obtain the
5656 * physical addresses for each page to be mapped.
5661 vm_object_offset_t offset
,
5662 vm_object_size_t size
,
5663 vm_object_offset_t (*map_fn
)(void *map_fn_data
,
5664 vm_object_offset_t offset
),
5665 void *map_fn_data
) /* private to map_fn */
5671 vm_object_offset_t addr
;
5673 num_pages
= atop_64(size
);
5675 for (i
= 0; i
< num_pages
; i
++, offset
+= PAGE_SIZE_64
) {
5677 addr
= (*map_fn
)(map_fn_data
, offset
);
5679 while ((m
= vm_page_grab_fictitious()) == VM_PAGE_NULL
)
5680 vm_page_more_fictitious();
5682 vm_object_lock(object
);
5683 if ((old_page
= vm_page_lookup(object
, offset
))
5686 VM_PAGE_FREE(old_page
);
5689 assert((ppnum_t
) addr
== addr
);
5690 vm_page_init(m
, (ppnum_t
) addr
, FALSE
);
5692 * private normally requires lock_queues but since we
5693 * are initializing the page, its not necessary here
5695 m
->private = TRUE
; /* don`t free page */
5697 vm_page_insert(m
, object
, offset
);
5699 PAGE_WAKEUP_DONE(m
);
5700 vm_object_unlock(object
);
5705 vm_object_populate_with_private(
5707 vm_object_offset_t offset
,
5712 vm_object_offset_t base_offset
;
5715 if (!object
->private)
5716 return KERN_FAILURE
;
5718 base_page
= phys_page
;
5720 vm_object_lock(object
);
5722 if (!object
->phys_contiguous
) {
5725 if ((base_offset
= trunc_page_64(offset
)) != offset
) {
5726 vm_object_unlock(object
);
5727 return KERN_FAILURE
;
5729 base_offset
+= object
->paging_offset
;
5732 m
= vm_page_lookup(object
, base_offset
);
5734 if (m
!= VM_PAGE_NULL
) {
5735 if (m
->fictitious
) {
5736 if (m
->phys_page
!= vm_page_guard_addr
) {
5738 vm_page_lockspin_queues();
5740 vm_page_unlock_queues();
5742 m
->fictitious
= FALSE
;
5743 m
->phys_page
= base_page
;
5745 } else if (m
->phys_page
!= base_page
) {
5749 * we'd leak a real page... that can't be right
5751 panic("vm_object_populate_with_private - %p not private", m
);
5755 * pmap call to clear old mapping
5757 pmap_disconnect(m
->phys_page
);
5759 m
->phys_page
= base_page
;
5763 * we should never see this on a ficticious or private page
5765 panic("vm_object_populate_with_private - %p encrypted", m
);
5769 while ((m
= vm_page_grab_fictitious()) == VM_PAGE_NULL
)
5770 vm_page_more_fictitious();
5773 * private normally requires lock_queues but since we
5774 * are initializing the page, its not necessary here
5777 m
->fictitious
= FALSE
;
5778 m
->phys_page
= base_page
;
5782 vm_page_insert(m
, object
, base_offset
);
5784 base_page
++; /* Go to the next physical page */
5785 base_offset
+= PAGE_SIZE
;
5789 /* NOTE: we should check the original settings here */
5790 /* if we have a size > zero a pmap call should be made */
5791 /* to disable the range */
5795 /* shadows on contiguous memory are not allowed */
5796 /* we therefore can use the offset field */
5797 object
->vo_shadow_offset
= (vm_object_offset_t
)phys_page
<< PAGE_SHIFT
;
5798 object
->vo_size
= size
;
5800 vm_object_unlock(object
);
5802 return KERN_SUCCESS
;
5806 * memory_object_free_from_cache:
5808 * Walk the vm_object cache list, removing and freeing vm_objects
5809 * which are backed by the pager identified by the caller, (pager_ops).
5810 * Remove up to "count" objects, if there are that may available
5813 * Walk the list at most once, return the number of vm_objects
5817 __private_extern__ kern_return_t
5818 memory_object_free_from_cache(
5819 __unused host_t host
,
5820 __unused memory_object_pager_ops_t pager_ops
,
5824 int object_released
= 0;
5826 register vm_object_t object
= VM_OBJECT_NULL
;
5830 if(host == HOST_NULL)
5831 return(KERN_INVALID_ARGUMENT);
5835 vm_object_cache_lock();
5837 queue_iterate(&vm_object_cached_list
, object
,
5838 vm_object_t
, cached_list
) {
5839 if (object
->pager
&&
5840 (pager_ops
== object
->pager
->mo_pager_ops
)) {
5841 vm_object_lock(object
);
5842 queue_remove(&vm_object_cached_list
, object
,
5843 vm_object_t
, cached_list
);
5844 vm_object_cached_count
--;
5846 vm_object_cache_unlock();
5848 * Since this object is in the cache, we know
5849 * that it is initialized and has only a pager's
5850 * (implicit) reference. Take a reference to avoid
5851 * recursive deallocations.
5854 assert(object
->pager_initialized
);
5855 assert(object
->ref_count
== 0);
5856 vm_object_lock_assert_exclusive(object
);
5857 object
->ref_count
++;
5860 * Terminate the object.
5861 * If the object had a shadow, we let
5862 * vm_object_deallocate deallocate it.
5863 * "pageout" objects have a shadow, but
5864 * maintain a "paging reference" rather
5865 * than a normal reference.
5866 * (We are careful here to limit recursion.)
5868 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
5870 if ((vm_object_terminate(object
) == KERN_SUCCESS
)
5871 && (shadow
!= VM_OBJECT_NULL
)) {
5872 vm_object_deallocate(shadow
);
5875 if(object_released
++ == *count
)
5876 return KERN_SUCCESS
;
5880 vm_object_cache_unlock();
5881 *count
= object_released
;
5885 return KERN_SUCCESS
;
5891 memory_object_create_named(
5892 memory_object_t pager
,
5893 memory_object_offset_t size
,
5894 memory_object_control_t
*control
)
5897 vm_object_hash_entry_t entry
;
5900 *control
= MEMORY_OBJECT_CONTROL_NULL
;
5901 if (pager
== MEMORY_OBJECT_NULL
)
5902 return KERN_INVALID_ARGUMENT
;
5904 lck
= vm_object_hash_lock_spin(pager
);
5905 entry
= vm_object_hash_lookup(pager
, FALSE
);
5907 if ((entry
!= VM_OBJECT_HASH_ENTRY_NULL
) &&
5908 (entry
->object
!= VM_OBJECT_NULL
)) {
5909 if (entry
->object
->named
== TRUE
)
5910 panic("memory_object_create_named: caller already holds the right"); }
5911 vm_object_hash_unlock(lck
);
5913 if ((object
= vm_object_enter(pager
, size
, FALSE
, FALSE
, TRUE
)) == VM_OBJECT_NULL
) {
5914 return(KERN_INVALID_OBJECT
);
5917 /* wait for object (if any) to be ready */
5918 if (object
!= VM_OBJECT_NULL
) {
5919 vm_object_lock(object
);
5920 object
->named
= TRUE
;
5921 while (!object
->pager_ready
) {
5922 vm_object_sleep(object
,
5923 VM_OBJECT_EVENT_PAGER_READY
,
5926 *control
= object
->pager_control
;
5927 vm_object_unlock(object
);
5929 return (KERN_SUCCESS
);
5934 * Routine: memory_object_recover_named [user interface]
5936 * Attempt to recover a named reference for a VM object.
5937 * VM will verify that the object has not already started
5938 * down the termination path, and if it has, will optionally
5939 * wait for that to finish.
5941 * KERN_SUCCESS - we recovered a named reference on the object
5942 * KERN_FAILURE - we could not recover a reference (object dead)
5943 * KERN_INVALID_ARGUMENT - bad memory object control
5946 memory_object_recover_named(
5947 memory_object_control_t control
,
5948 boolean_t wait_on_terminating
)
5952 object
= memory_object_control_to_vm_object(control
);
5953 if (object
== VM_OBJECT_NULL
) {
5954 return (KERN_INVALID_ARGUMENT
);
5957 vm_object_lock(object
);
5959 if (object
->terminating
&& wait_on_terminating
) {
5960 vm_object_wait(object
,
5961 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
5966 if (!object
->alive
) {
5967 vm_object_unlock(object
);
5968 return KERN_FAILURE
;
5971 if (object
->named
== TRUE
) {
5972 vm_object_unlock(object
);
5973 return KERN_SUCCESS
;
5976 if ((object
->ref_count
== 0) && (!object
->terminating
)) {
5977 if (!vm_object_cache_lock_try()) {
5978 vm_object_unlock(object
);
5981 queue_remove(&vm_object_cached_list
, object
,
5982 vm_object_t
, cached_list
);
5983 vm_object_cached_count
--;
5984 XPR(XPR_VM_OBJECT_CACHE
,
5985 "memory_object_recover_named: removing %X, head (%X, %X)\n",
5987 vm_object_cached_list
.next
,
5988 vm_object_cached_list
.prev
, 0,0);
5990 vm_object_cache_unlock();
5993 object
->named
= TRUE
;
5994 vm_object_lock_assert_exclusive(object
);
5995 object
->ref_count
++;
5996 vm_object_res_reference(object
);
5997 while (!object
->pager_ready
) {
5998 vm_object_sleep(object
,
5999 VM_OBJECT_EVENT_PAGER_READY
,
6002 vm_object_unlock(object
);
6003 return (KERN_SUCCESS
);
6008 * vm_object_release_name:
6010 * Enforces name semantic on memory_object reference count decrement
6011 * This routine should not be called unless the caller holds a name
6012 * reference gained through the memory_object_create_named.
6014 * If the TERMINATE_IDLE flag is set, the call will return if the
6015 * reference count is not 1. i.e. idle with the only remaining reference
6017 * If the decision is made to proceed the name field flag is set to
6018 * false and the reference count is decremented. If the RESPECT_CACHE
6019 * flag is set and the reference count has gone to zero, the
6020 * memory_object is checked to see if it is cacheable otherwise when
6021 * the reference count is zero, it is simply terminated.
6024 __private_extern__ kern_return_t
6025 vm_object_release_name(
6030 boolean_t original_object
= TRUE
;
6032 while (object
!= VM_OBJECT_NULL
) {
6034 vm_object_lock(object
);
6036 assert(object
->alive
);
6037 if (original_object
)
6038 assert(object
->named
);
6039 assert(object
->ref_count
> 0);
6042 * We have to wait for initialization before
6043 * destroying or caching the object.
6046 if (object
->pager_created
&& !object
->pager_initialized
) {
6047 assert(!object
->can_persist
);
6048 vm_object_assert_wait(object
,
6049 VM_OBJECT_EVENT_INITIALIZED
,
6051 vm_object_unlock(object
);
6052 thread_block(THREAD_CONTINUE_NULL
);
6056 if (((object
->ref_count
> 1)
6057 && (flags
& MEMORY_OBJECT_TERMINATE_IDLE
))
6058 || (object
->terminating
)) {
6059 vm_object_unlock(object
);
6060 return KERN_FAILURE
;
6062 if (flags
& MEMORY_OBJECT_RELEASE_NO_OP
) {
6063 vm_object_unlock(object
);
6064 return KERN_SUCCESS
;
6068 if ((flags
& MEMORY_OBJECT_RESPECT_CACHE
) &&
6069 (object
->ref_count
== 1)) {
6070 if (original_object
)
6071 object
->named
= FALSE
;
6072 vm_object_unlock(object
);
6073 /* let vm_object_deallocate push this thing into */
6074 /* the cache, if that it is where it is bound */
6075 vm_object_deallocate(object
);
6076 return KERN_SUCCESS
;
6078 VM_OBJ_RES_DECR(object
);
6079 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
6081 if (object
->ref_count
== 1) {
6082 if (vm_object_terminate(object
) != KERN_SUCCESS
) {
6083 if (original_object
) {
6084 return KERN_FAILURE
;
6086 return KERN_SUCCESS
;
6089 if (shadow
!= VM_OBJECT_NULL
) {
6090 original_object
= FALSE
;
6094 return KERN_SUCCESS
;
6096 vm_object_lock_assert_exclusive(object
);
6097 object
->ref_count
--;
6098 assert(object
->ref_count
> 0);
6100 object
->named
= FALSE
;
6101 vm_object_unlock(object
);
6102 return KERN_SUCCESS
;
6107 return KERN_FAILURE
;
6111 __private_extern__ kern_return_t
6112 vm_object_lock_request(
6114 vm_object_offset_t offset
,
6115 vm_object_size_t size
,
6116 memory_object_return_t should_return
,
6120 __unused boolean_t should_flush
;
6122 should_flush
= flags
& MEMORY_OBJECT_DATA_FLUSH
;
6124 XPR(XPR_MEMORY_OBJECT
,
6125 "vm_o_lock_request, obj 0x%X off 0x%X size 0x%X flags %X prot %X\n",
6126 object
, offset
, size
,
6127 (((should_return
&1)<<1)|should_flush
), prot
);
6130 * Check for bogus arguments.
6132 if (object
== VM_OBJECT_NULL
)
6133 return (KERN_INVALID_ARGUMENT
);
6135 if ((prot
& ~VM_PROT_ALL
) != 0 && prot
!= VM_PROT_NO_CHANGE
)
6136 return (KERN_INVALID_ARGUMENT
);
6138 size
= round_page_64(size
);
6141 * Lock the object, and acquire a paging reference to
6142 * prevent the memory_object reference from being released.
6144 vm_object_lock(object
);
6145 vm_object_paging_begin(object
);
6147 (void)vm_object_update(object
,
6148 offset
, size
, NULL
, NULL
, should_return
, flags
, prot
);
6150 vm_object_paging_end(object
);
6151 vm_object_unlock(object
);
6153 return (KERN_SUCCESS
);
6157 * Empty a purgeable object by grabbing the physical pages assigned to it and
6158 * putting them on the free queue without writing them to backing store, etc.
6159 * When the pages are next touched they will be demand zero-fill pages. We
6160 * skip pages which are busy, being paged in/out, wired, etc. We do _not_
6161 * skip referenced/dirty pages, pages on the active queue, etc. We're more
6162 * than happy to grab these since this is a purgeable object. We mark the
6163 * object as "empty" after reaping its pages.
6165 * On entry the object must be locked and it must be
6166 * purgeable with no delayed copies pending.
6169 vm_object_purge(vm_object_t object
)
6171 vm_object_lock_assert_exclusive(object
);
6173 if (object
->purgable
== VM_PURGABLE_DENY
)
6176 assert(object
->copy
== VM_OBJECT_NULL
);
6177 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
6179 if(object
->purgable
== VM_PURGABLE_VOLATILE
) {
6181 assert(object
->resident_page_count
>=
6182 object
->wired_page_count
);
6183 delta
= (object
->resident_page_count
-
6184 object
->wired_page_count
);
6186 assert(vm_page_purgeable_count
>=
6189 (SInt32
*)&vm_page_purgeable_count
);
6191 if (object
->wired_page_count
!= 0) {
6192 assert(vm_page_purgeable_wired_count
>=
6193 object
->wired_page_count
);
6194 OSAddAtomic(-object
->wired_page_count
,
6195 (SInt32
*)&vm_page_purgeable_wired_count
);
6198 object
->purgable
= VM_PURGABLE_EMPTY
;
6200 vm_object_reap_pages(object
, REAP_PURGEABLE
);
6205 * vm_object_purgeable_control() allows the caller to control and investigate the
6206 * state of a purgeable object. A purgeable object is created via a call to
6207 * vm_allocate() with VM_FLAGS_PURGABLE specified. A purgeable object will
6208 * never be coalesced with any other object -- even other purgeable objects --
6209 * and will thus always remain a distinct object. A purgeable object has
6210 * special semantics when its reference count is exactly 1. If its reference
6211 * count is greater than 1, then a purgeable object will behave like a normal
6212 * object and attempts to use this interface will result in an error return
6213 * of KERN_INVALID_ARGUMENT.
6215 * A purgeable object may be put into a "volatile" state which will make the
6216 * object's pages elligable for being reclaimed without paging to backing
6217 * store if the system runs low on memory. If the pages in a volatile
6218 * purgeable object are reclaimed, the purgeable object is said to have been
6219 * "emptied." When a purgeable object is emptied the system will reclaim as
6220 * many pages from the object as it can in a convenient manner (pages already
6221 * en route to backing store or busy for other reasons are left as is). When
6222 * a purgeable object is made volatile, its pages will generally be reclaimed
6223 * before other pages in the application's working set. This semantic is
6224 * generally used by applications which can recreate the data in the object
6225 * faster than it can be paged in. One such example might be media assets
6226 * which can be reread from a much faster RAID volume.
6228 * A purgeable object may be designated as "non-volatile" which means it will
6229 * behave like all other objects in the system with pages being written to and
6230 * read from backing store as needed to satisfy system memory needs. If the
6231 * object was emptied before the object was made non-volatile, that fact will
6232 * be returned as the old state of the purgeable object (see
6233 * VM_PURGABLE_SET_STATE below). In this case, any pages of the object which
6234 * were reclaimed as part of emptying the object will be refaulted in as
6235 * zero-fill on demand. It is up to the application to note that an object
6236 * was emptied and recreate the objects contents if necessary. When a
6237 * purgeable object is made non-volatile, its pages will generally not be paged
6238 * out to backing store in the immediate future. A purgeable object may also
6239 * be manually emptied.
6241 * Finally, the current state (non-volatile, volatile, volatile & empty) of a
6242 * volatile purgeable object may be queried at any time. This information may
6243 * be used as a control input to let the application know when the system is
6244 * experiencing memory pressure and is reclaiming memory.
6246 * The specified address may be any address within the purgeable object. If
6247 * the specified address does not represent any object in the target task's
6248 * virtual address space, then KERN_INVALID_ADDRESS will be returned. If the
6249 * object containing the specified address is not a purgeable object, then
6250 * KERN_INVALID_ARGUMENT will be returned. Otherwise, KERN_SUCCESS will be
6253 * The control parameter may be any one of VM_PURGABLE_SET_STATE or
6254 * VM_PURGABLE_GET_STATE. For VM_PURGABLE_SET_STATE, the in/out parameter
6255 * state is used to set the new state of the purgeable object and return its
6256 * old state. For VM_PURGABLE_GET_STATE, the current state of the purgeable
6257 * object is returned in the parameter state.
6259 * The in/out parameter state may be one of VM_PURGABLE_NONVOLATILE,
6260 * VM_PURGABLE_VOLATILE or VM_PURGABLE_EMPTY. These, respectively, represent
6261 * the non-volatile, volatile and volatile/empty states described above.
6262 * Setting the state of a purgeable object to VM_PURGABLE_EMPTY will
6263 * immediately reclaim as many pages in the object as can be conveniently
6264 * collected (some may have already been written to backing store or be
6267 * The process of making a purgeable object non-volatile and determining its
6268 * previous state is atomic. Thus, if a purgeable object is made
6269 * VM_PURGABLE_NONVOLATILE and the old state is returned as
6270 * VM_PURGABLE_VOLATILE, then the purgeable object's previous contents are
6271 * completely intact and will remain so until the object is made volatile
6272 * again. If the old state is returned as VM_PURGABLE_EMPTY then the object
6273 * was reclaimed while it was in a volatile state and its previous contents
6277 * The object must be locked.
6280 vm_object_purgable_control(
6282 vm_purgable_t control
,
6288 if (object
== VM_OBJECT_NULL
) {
6290 * Object must already be present or it can't be purgeable.
6292 return KERN_INVALID_ARGUMENT
;
6296 * Get current state of the purgeable object.
6298 old_state
= object
->purgable
;
6299 if (old_state
== VM_PURGABLE_DENY
)
6300 return KERN_INVALID_ARGUMENT
;
6302 /* purgeable cant have delayed copies - now or in the future */
6303 assert(object
->copy
== VM_OBJECT_NULL
);
6304 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
6307 * Execute the desired operation.
6309 if (control
== VM_PURGABLE_GET_STATE
) {
6311 return KERN_SUCCESS
;
6314 if ((*state
) & VM_PURGABLE_DEBUG_EMPTY
) {
6315 object
->volatile_empty
= TRUE
;
6317 if ((*state
) & VM_PURGABLE_DEBUG_FAULT
) {
6318 object
->volatile_fault
= TRUE
;
6321 new_state
= *state
& VM_PURGABLE_STATE_MASK
;
6322 if (new_state
== VM_PURGABLE_VOLATILE
&&
6323 object
->volatile_empty
) {
6324 new_state
= VM_PURGABLE_EMPTY
;
6327 switch (new_state
) {
6328 case VM_PURGABLE_DENY
:
6329 case VM_PURGABLE_NONVOLATILE
:
6330 object
->purgable
= new_state
;
6332 if (old_state
== VM_PURGABLE_VOLATILE
) {
6335 assert(object
->resident_page_count
>=
6336 object
->wired_page_count
);
6337 delta
= (object
->resident_page_count
-
6338 object
->wired_page_count
);
6340 assert(vm_page_purgeable_count
>= delta
);
6344 (SInt32
*)&vm_page_purgeable_count
);
6346 if (object
->wired_page_count
!= 0) {
6347 assert(vm_page_purgeable_wired_count
>=
6348 object
->wired_page_count
);
6349 OSAddAtomic(-object
->wired_page_count
,
6350 (SInt32
*)&vm_page_purgeable_wired_count
);
6353 vm_page_lock_queues();
6355 assert(object
->objq
.next
!= NULL
&& object
->objq
.prev
!= NULL
); /* object should be on a queue */
6356 purgeable_q_t queue
= vm_purgeable_object_remove(object
);
6359 if (object
->purgeable_when_ripe
) {
6360 vm_purgeable_token_delete_last(queue
);
6362 assert(queue
->debug_count_objects
>=0);
6364 vm_page_unlock_queues();
6368 case VM_PURGABLE_VOLATILE
:
6369 if (object
->volatile_fault
) {
6373 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
6379 refmod
= pmap_disconnect(p
->phys_page
);
6380 if ((refmod
& VM_MEM_MODIFIED
) &&
6382 SET_PAGE_DIRTY(p
, FALSE
);
6387 if (old_state
== VM_PURGABLE_EMPTY
&&
6388 object
->resident_page_count
== 0)
6391 purgeable_q_t queue
;
6393 /* find the correct queue */
6394 if ((*state
&VM_PURGABLE_ORDERING_MASK
) == VM_PURGABLE_ORDERING_OBSOLETE
)
6395 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_OBSOLETE
];
6397 if ((*state
&VM_PURGABLE_BEHAVIOR_MASK
) == VM_PURGABLE_BEHAVIOR_FIFO
)
6398 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_FIFO
];
6400 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_LIFO
];
6403 if (old_state
== VM_PURGABLE_NONVOLATILE
||
6404 old_state
== VM_PURGABLE_EMPTY
) {
6407 if ((*state
& VM_PURGABLE_NO_AGING_MASK
) ==
6408 VM_PURGABLE_NO_AGING
) {
6409 object
->purgeable_when_ripe
= FALSE
;
6411 object
->purgeable_when_ripe
= TRUE
;
6414 if (object
->purgeable_when_ripe
) {
6415 kern_return_t result
;
6417 /* try to add token... this can fail */
6418 vm_page_lock_queues();
6420 result
= vm_purgeable_token_add(queue
);
6421 if (result
!= KERN_SUCCESS
) {
6422 vm_page_unlock_queues();
6425 vm_page_unlock_queues();
6428 assert(object
->resident_page_count
>=
6429 object
->wired_page_count
);
6430 delta
= (object
->resident_page_count
-
6431 object
->wired_page_count
);
6435 &vm_page_purgeable_count
);
6437 if (object
->wired_page_count
!= 0) {
6438 OSAddAtomic(object
->wired_page_count
,
6439 &vm_page_purgeable_wired_count
);
6442 object
->purgable
= new_state
;
6444 /* object should not be on a queue */
6445 assert(object
->objq
.next
== NULL
&& object
->objq
.prev
== NULL
);
6447 else if (old_state
== VM_PURGABLE_VOLATILE
) {
6448 purgeable_q_t old_queue
;
6449 boolean_t purgeable_when_ripe
;
6452 * if reassigning priorities / purgeable groups, we don't change the
6453 * token queue. So moving priorities will not make pages stay around longer.
6454 * Reasoning is that the algorithm gives most priority to the most important
6455 * object. If a new token is added, the most important object' priority is boosted.
6456 * This biases the system already for purgeable queues that move a lot.
6457 * It doesn't seem more biasing is neccessary in this case, where no new object is added.
6459 assert(object
->objq
.next
!= NULL
&& object
->objq
.prev
!= NULL
); /* object should be on a queue */
6461 old_queue
= vm_purgeable_object_remove(object
);
6464 if ((*state
& VM_PURGABLE_NO_AGING_MASK
) ==
6465 VM_PURGABLE_NO_AGING
) {
6466 purgeable_when_ripe
= FALSE
;
6468 purgeable_when_ripe
= TRUE
;
6471 if (old_queue
!= queue
||
6472 (purgeable_when_ripe
!=
6473 object
->purgeable_when_ripe
)) {
6474 kern_return_t result
;
6476 /* Changing queue. Have to move token. */
6477 vm_page_lock_queues();
6478 if (object
->purgeable_when_ripe
) {
6479 vm_purgeable_token_delete_last(old_queue
);
6481 object
->purgeable_when_ripe
= purgeable_when_ripe
;
6482 if (object
->purgeable_when_ripe
) {
6483 result
= vm_purgeable_token_add(queue
);
6484 assert(result
==KERN_SUCCESS
); /* this should never fail since we just freed a token */
6486 vm_page_unlock_queues();
6490 vm_purgeable_object_add(object
, queue
, (*state
&VM_VOLATILE_GROUP_MASK
)>>VM_VOLATILE_GROUP_SHIFT
);
6492 assert(queue
->debug_count_objects
>=0);
6497 case VM_PURGABLE_EMPTY
:
6498 if (object
->volatile_fault
) {
6502 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
6508 refmod
= pmap_disconnect(p
->phys_page
);
6509 if ((refmod
& VM_MEM_MODIFIED
) &&
6511 SET_PAGE_DIRTY(p
, FALSE
);
6516 if (old_state
!= new_state
) {
6517 assert(old_state
== VM_PURGABLE_NONVOLATILE
||
6518 old_state
== VM_PURGABLE_VOLATILE
);
6519 if (old_state
== VM_PURGABLE_VOLATILE
) {
6520 purgeable_q_t old_queue
;
6522 /* object should be on a queue */
6523 assert(object
->objq
.next
!= NULL
&&
6524 object
->objq
.prev
!= NULL
);
6525 old_queue
= vm_purgeable_object_remove(object
);
6527 if (object
->purgeable_when_ripe
) {
6528 vm_page_lock_queues();
6529 vm_purgeable_token_delete_first(old_queue
);
6530 vm_page_unlock_queues();
6533 (void) vm_object_purge(object
);
6540 return KERN_SUCCESS
;
6544 vm_object_get_page_counts(
6546 vm_object_offset_t offset
,
6547 vm_object_size_t size
,
6548 unsigned int *resident_page_count
,
6549 unsigned int *dirty_page_count
)
6552 kern_return_t kr
= KERN_SUCCESS
;
6553 boolean_t count_dirty_pages
= FALSE
;
6554 vm_page_t p
= VM_PAGE_NULL
;
6555 unsigned int local_resident_count
= 0;
6556 unsigned int local_dirty_count
= 0;
6557 vm_object_offset_t cur_offset
= 0;
6558 vm_object_offset_t end_offset
= 0;
6560 if (object
== VM_OBJECT_NULL
)
6561 return KERN_INVALID_ARGUMENT
;
6564 cur_offset
= offset
;
6566 end_offset
= offset
+ size
;
6568 vm_object_lock_assert_exclusive(object
);
6570 if (dirty_page_count
!= NULL
) {
6572 count_dirty_pages
= TRUE
;
6575 if (resident_page_count
!= NULL
&& count_dirty_pages
== FALSE
) {
6578 * - we only want the resident page count, and,
6579 * - the entire object is exactly covered by the request.
6581 if (offset
== 0 && (object
->vo_size
== size
)) {
6583 *resident_page_count
= object
->resident_page_count
;
6588 if (object
->resident_page_count
<= (size
>> PAGE_SHIFT
)) {
6590 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
6592 if (p
->offset
>= cur_offset
&& p
->offset
< end_offset
) {
6594 local_resident_count
++;
6596 if (count_dirty_pages
) {
6598 if (p
->dirty
|| (p
->wpmapped
&& pmap_is_modified(p
->phys_page
))) {
6600 local_dirty_count
++;
6607 for (cur_offset
= offset
; cur_offset
< end_offset
; cur_offset
+= PAGE_SIZE_64
) {
6609 p
= vm_page_lookup(object
, cur_offset
);
6611 if (p
!= VM_PAGE_NULL
) {
6613 local_resident_count
++;
6615 if (count_dirty_pages
) {
6617 if (p
->dirty
|| (p
->wpmapped
&& pmap_is_modified(p
->phys_page
))) {
6619 local_dirty_count
++;
6627 if (resident_page_count
!= NULL
) {
6628 *resident_page_count
= local_resident_count
;
6631 if (dirty_page_count
!= NULL
) {
6632 *dirty_page_count
= local_dirty_count
;
6642 * vm_object_res_deallocate
6644 * (recursively) decrement residence counts on vm objects and their shadows.
6645 * Called from vm_object_deallocate and when swapping out an object.
6647 * The object is locked, and remains locked throughout the function,
6648 * even as we iterate down the shadow chain. Locks on intermediate objects
6649 * will be dropped, but not the original object.
6651 * NOTE: this function used to use recursion, rather than iteration.
6654 __private_extern__
void
6655 vm_object_res_deallocate(
6658 vm_object_t orig_object
= object
;
6660 * Object is locked so it can be called directly
6661 * from vm_object_deallocate. Original object is never
6664 assert(object
->res_count
> 0);
6665 while (--object
->res_count
== 0) {
6666 assert(object
->ref_count
>= object
->res_count
);
6667 vm_object_deactivate_all_pages(object
);
6668 /* iterate on shadow, if present */
6669 if (object
->shadow
!= VM_OBJECT_NULL
) {
6670 vm_object_t tmp_object
= object
->shadow
;
6671 vm_object_lock(tmp_object
);
6672 if (object
!= orig_object
)
6673 vm_object_unlock(object
);
6674 object
= tmp_object
;
6675 assert(object
->res_count
> 0);
6679 if (object
!= orig_object
)
6680 vm_object_unlock(object
);
6684 * vm_object_res_reference
6686 * Internal function to increment residence count on a vm object
6687 * and its shadows. It is called only from vm_object_reference, and
6688 * when swapping in a vm object, via vm_map_swap.
6690 * The object is locked, and remains locked throughout the function,
6691 * even as we iterate down the shadow chain. Locks on intermediate objects
6692 * will be dropped, but not the original object.
6694 * NOTE: this function used to use recursion, rather than iteration.
6697 __private_extern__
void
6698 vm_object_res_reference(
6701 vm_object_t orig_object
= object
;
6703 * Object is locked, so this can be called directly
6704 * from vm_object_reference. This lock is never released.
6706 while ((++object
->res_count
== 1) &&
6707 (object
->shadow
!= VM_OBJECT_NULL
)) {
6708 vm_object_t tmp_object
= object
->shadow
;
6710 assert(object
->ref_count
>= object
->res_count
);
6711 vm_object_lock(tmp_object
);
6712 if (object
!= orig_object
)
6713 vm_object_unlock(object
);
6714 object
= tmp_object
;
6716 if (object
!= orig_object
)
6717 vm_object_unlock(object
);
6718 assert(orig_object
->ref_count
>= orig_object
->res_count
);
6720 #endif /* TASK_SWAPPER */
6723 * vm_object_reference:
6725 * Gets another reference to the given object.
6727 #ifdef vm_object_reference
6728 #undef vm_object_reference
6730 __private_extern__
void
6731 vm_object_reference(
6732 register vm_object_t object
)
6734 if (object
== VM_OBJECT_NULL
)
6737 vm_object_lock(object
);
6738 assert(object
->ref_count
> 0);
6739 vm_object_reference_locked(object
);
6740 vm_object_unlock(object
);
6745 * Scale the vm_object_cache
6746 * This is required to make sure that the vm_object_cache is big
6747 * enough to effectively cache the mapped file.
6748 * This is really important with UBC as all the regular file vnodes
6749 * have memory object associated with them. Havving this cache too
6750 * small results in rapid reclaim of vnodes and hurts performance a LOT!
6752 * This is also needed as number of vnodes can be dynamically scaled.
6755 adjust_vm_object_cache(
6756 __unused vm_size_t oval
,
6757 __unused vm_size_t nval
)
6760 vm_object_cached_max
= nval
;
6761 vm_object_cache_trim(FALSE
);
6763 return (KERN_SUCCESS
);
6765 #endif /* MACH_BSD */
6769 * vm_object_transpose
6771 * This routine takes two VM objects of the same size and exchanges
6772 * their backing store.
6773 * The objects should be "quiesced" via a UPL operation with UPL_SET_IO_WIRE
6774 * and UPL_BLOCK_ACCESS if they are referenced anywhere.
6776 * The VM objects must not be locked by caller.
6778 unsigned int vm_object_transpose_count
= 0;
6780 vm_object_transpose(
6781 vm_object_t object1
,
6782 vm_object_t object2
,
6783 vm_object_size_t transpose_size
)
6785 vm_object_t tmp_object
;
6786 kern_return_t retval
;
6787 boolean_t object1_locked
, object2_locked
;
6789 vm_object_offset_t page_offset
;
6790 lck_mtx_t
*hash_lck
;
6791 vm_object_hash_entry_t hash_entry
;
6793 tmp_object
= VM_OBJECT_NULL
;
6794 object1_locked
= FALSE
; object2_locked
= FALSE
;
6796 if (object1
== object2
||
6797 object1
== VM_OBJECT_NULL
||
6798 object2
== VM_OBJECT_NULL
) {
6800 * If the 2 VM objects are the same, there's
6801 * no point in exchanging their backing store.
6803 retval
= KERN_INVALID_VALUE
;
6808 * Since we need to lock both objects at the same time,
6809 * make sure we always lock them in the same order to
6812 if (object1
> object2
) {
6813 tmp_object
= object1
;
6815 object2
= tmp_object
;
6819 * Allocate a temporary VM object to hold object1's contents
6820 * while we copy object2 to object1.
6822 tmp_object
= vm_object_allocate(transpose_size
);
6823 vm_object_lock(tmp_object
);
6824 tmp_object
->can_persist
= FALSE
;
6828 * Grab control of the 1st VM object.
6830 vm_object_lock(object1
);
6831 object1_locked
= TRUE
;
6832 if (!object1
->alive
|| object1
->terminating
||
6833 object1
->copy
|| object1
->shadow
|| object1
->shadowed
||
6834 object1
->purgable
!= VM_PURGABLE_DENY
) {
6836 * We don't deal with copy or shadow objects (yet).
6838 retval
= KERN_INVALID_VALUE
;
6842 * We're about to mess with the object's backing store and
6843 * taking a "paging_in_progress" reference wouldn't be enough
6844 * to prevent any paging activity on this object, so the caller should
6845 * have "quiesced" the objects beforehand, via a UPL operation with
6846 * UPL_SET_IO_WIRE (to make sure all the pages are there and wired)
6847 * and UPL_BLOCK_ACCESS (to mark the pages "busy").
6849 * Wait for any paging operation to complete (but only paging, not
6850 * other kind of activities not linked to the pager). After we're
6851 * statisfied that there's no more paging in progress, we keep the
6852 * object locked, to guarantee that no one tries to access its pager.
6854 vm_object_paging_only_wait(object1
, THREAD_UNINT
);
6857 * Same as above for the 2nd object...
6859 vm_object_lock(object2
);
6860 object2_locked
= TRUE
;
6861 if (! object2
->alive
|| object2
->terminating
||
6862 object2
->copy
|| object2
->shadow
|| object2
->shadowed
||
6863 object2
->purgable
!= VM_PURGABLE_DENY
) {
6864 retval
= KERN_INVALID_VALUE
;
6867 vm_object_paging_only_wait(object2
, THREAD_UNINT
);
6870 if (object1
->vo_size
!= object2
->vo_size
||
6871 object1
->vo_size
!= transpose_size
) {
6873 * If the 2 objects don't have the same size, we can't
6874 * exchange their backing stores or one would overflow.
6875 * If their size doesn't match the caller's
6876 * "transpose_size", we can't do it either because the
6877 * transpose operation will affect the entire span of
6880 retval
= KERN_INVALID_VALUE
;
6886 * Transpose the lists of resident pages.
6887 * This also updates the resident_page_count and the memq_hint.
6889 if (object1
->phys_contiguous
|| queue_empty(&object1
->memq
)) {
6891 * No pages in object1, just transfer pages
6892 * from object2 to object1. No need to go through
6893 * an intermediate object.
6895 while (!queue_empty(&object2
->memq
)) {
6896 page
= (vm_page_t
) queue_first(&object2
->memq
);
6897 vm_page_rename(page
, object1
, page
->offset
, FALSE
);
6899 assert(queue_empty(&object2
->memq
));
6900 } else if (object2
->phys_contiguous
|| queue_empty(&object2
->memq
)) {
6902 * No pages in object2, just transfer pages
6903 * from object1 to object2. No need to go through
6904 * an intermediate object.
6906 while (!queue_empty(&object1
->memq
)) {
6907 page
= (vm_page_t
) queue_first(&object1
->memq
);
6908 vm_page_rename(page
, object2
, page
->offset
, FALSE
);
6910 assert(queue_empty(&object1
->memq
));
6912 /* transfer object1's pages to tmp_object */
6913 while (!queue_empty(&object1
->memq
)) {
6914 page
= (vm_page_t
) queue_first(&object1
->memq
);
6915 page_offset
= page
->offset
;
6916 vm_page_remove(page
, TRUE
);
6917 page
->offset
= page_offset
;
6918 queue_enter(&tmp_object
->memq
, page
, vm_page_t
, listq
);
6920 assert(queue_empty(&object1
->memq
));
6921 /* transfer object2's pages to object1 */
6922 while (!queue_empty(&object2
->memq
)) {
6923 page
= (vm_page_t
) queue_first(&object2
->memq
);
6924 vm_page_rename(page
, object1
, page
->offset
, FALSE
);
6926 assert(queue_empty(&object2
->memq
));
6927 /* transfer tmp_object's pages to object1 */
6928 while (!queue_empty(&tmp_object
->memq
)) {
6929 page
= (vm_page_t
) queue_first(&tmp_object
->memq
);
6930 queue_remove(&tmp_object
->memq
, page
,
6932 vm_page_insert(page
, object2
, page
->offset
);
6934 assert(queue_empty(&tmp_object
->memq
));
6937 #define __TRANSPOSE_FIELD(field) \
6939 tmp_object->field = object1->field; \
6940 object1->field = object2->field; \
6941 object2->field = tmp_object->field; \
6944 /* "Lock" refers to the object not its contents */
6945 /* "size" should be identical */
6946 assert(object1
->vo_size
== object2
->vo_size
);
6947 /* "memq_hint" was updated above when transposing pages */
6948 /* "ref_count" refers to the object not its contents */
6950 /* "res_count" refers to the object not its contents */
6952 /* "resident_page_count" was updated above when transposing pages */
6953 /* "wired_page_count" was updated above when transposing pages */
6954 /* "reusable_page_count" was updated above when transposing pages */
6955 /* there should be no "copy" */
6956 assert(!object1
->copy
);
6957 assert(!object2
->copy
);
6958 /* there should be no "shadow" */
6959 assert(!object1
->shadow
);
6960 assert(!object2
->shadow
);
6961 __TRANSPOSE_FIELD(vo_shadow_offset
); /* used by phys_contiguous objects */
6962 __TRANSPOSE_FIELD(pager
);
6963 __TRANSPOSE_FIELD(paging_offset
);
6964 __TRANSPOSE_FIELD(pager_control
);
6965 /* update the memory_objects' pointers back to the VM objects */
6966 if (object1
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
6967 memory_object_control_collapse(object1
->pager_control
,
6970 if (object2
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
6971 memory_object_control_collapse(object2
->pager_control
,
6974 __TRANSPOSE_FIELD(copy_strategy
);
6975 /* "paging_in_progress" refers to the object not its contents */
6976 assert(!object1
->paging_in_progress
);
6977 assert(!object2
->paging_in_progress
);
6978 assert(object1
->activity_in_progress
);
6979 assert(object2
->activity_in_progress
);
6980 /* "all_wanted" refers to the object not its contents */
6981 __TRANSPOSE_FIELD(pager_created
);
6982 __TRANSPOSE_FIELD(pager_initialized
);
6983 __TRANSPOSE_FIELD(pager_ready
);
6984 __TRANSPOSE_FIELD(pager_trusted
);
6985 __TRANSPOSE_FIELD(can_persist
);
6986 __TRANSPOSE_FIELD(internal
);
6987 __TRANSPOSE_FIELD(temporary
);
6988 __TRANSPOSE_FIELD(private);
6989 __TRANSPOSE_FIELD(pageout
);
6990 /* "alive" should be set */
6991 assert(object1
->alive
);
6992 assert(object2
->alive
);
6993 /* "purgeable" should be non-purgeable */
6994 assert(object1
->purgable
== VM_PURGABLE_DENY
);
6995 assert(object2
->purgable
== VM_PURGABLE_DENY
);
6996 /* "shadowed" refers to the the object not its contents */
6997 __TRANSPOSE_FIELD(purgeable_when_ripe
);
6998 __TRANSPOSE_FIELD(advisory_pageout
);
6999 __TRANSPOSE_FIELD(true_share
);
7000 /* "terminating" should not be set */
7001 assert(!object1
->terminating
);
7002 assert(!object2
->terminating
);
7003 __TRANSPOSE_FIELD(named
);
7004 /* "shadow_severed" refers to the object not its contents */
7005 __TRANSPOSE_FIELD(phys_contiguous
);
7006 __TRANSPOSE_FIELD(nophyscache
);
7007 /* "cached_list.next" points to transposed object */
7008 object1
->cached_list
.next
= (queue_entry_t
) object2
;
7009 object2
->cached_list
.next
= (queue_entry_t
) object1
;
7010 /* "cached_list.prev" should be NULL */
7011 assert(object1
->cached_list
.prev
== NULL
);
7012 assert(object2
->cached_list
.prev
== NULL
);
7013 /* "msr_q" is linked to the object not its contents */
7014 assert(queue_empty(&object1
->msr_q
));
7015 assert(queue_empty(&object2
->msr_q
));
7016 __TRANSPOSE_FIELD(last_alloc
);
7017 __TRANSPOSE_FIELD(sequential
);
7018 __TRANSPOSE_FIELD(pages_created
);
7019 __TRANSPOSE_FIELD(pages_used
);
7020 __TRANSPOSE_FIELD(scan_collisions
);
7022 __TRANSPOSE_FIELD(existence_map
);
7024 __TRANSPOSE_FIELD(cow_hint
);
7026 __TRANSPOSE_FIELD(paging_object
);
7028 __TRANSPOSE_FIELD(wimg_bits
);
7029 __TRANSPOSE_FIELD(set_cache_attr
);
7030 __TRANSPOSE_FIELD(code_signed
);
7031 if (object1
->hashed
) {
7032 hash_lck
= vm_object_hash_lock_spin(object2
->pager
);
7033 hash_entry
= vm_object_hash_lookup(object2
->pager
, FALSE
);
7034 assert(hash_entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
7035 hash_entry
->object
= object2
;
7036 vm_object_hash_unlock(hash_lck
);
7038 if (object2
->hashed
) {
7039 hash_lck
= vm_object_hash_lock_spin(object1
->pager
);
7040 hash_entry
= vm_object_hash_lookup(object1
->pager
, FALSE
);
7041 assert(hash_entry
!= VM_OBJECT_HASH_ENTRY_NULL
);
7042 hash_entry
->object
= object1
;
7043 vm_object_hash_unlock(hash_lck
);
7045 __TRANSPOSE_FIELD(hashed
);
7046 object1
->transposed
= TRUE
;
7047 object2
->transposed
= TRUE
;
7048 __TRANSPOSE_FIELD(mapping_in_progress
);
7049 __TRANSPOSE_FIELD(volatile_empty
);
7050 __TRANSPOSE_FIELD(volatile_fault
);
7051 __TRANSPOSE_FIELD(all_reusable
);
7052 assert(object1
->blocked_access
);
7053 assert(object2
->blocked_access
);
7054 assert(object1
->__object2_unused_bits
== 0);
7055 assert(object2
->__object2_unused_bits
== 0);
7057 /* "uplq" refers to the object not its contents (see upl_transpose()) */
7059 assert(object1
->objq
.next
== NULL
);
7060 assert(object1
->objq
.prev
== NULL
);
7061 assert(object2
->objq
.next
== NULL
);
7062 assert(object2
->objq
.prev
== NULL
);
7064 #undef __TRANSPOSE_FIELD
7066 retval
= KERN_SUCCESS
;
7072 if (tmp_object
!= VM_OBJECT_NULL
) {
7073 vm_object_unlock(tmp_object
);
7075 * Re-initialize the temporary object to avoid
7076 * deallocating a real pager.
7078 _vm_object_allocate(transpose_size
, tmp_object
);
7079 vm_object_deallocate(tmp_object
);
7080 tmp_object
= VM_OBJECT_NULL
;
7083 if (object1_locked
) {
7084 vm_object_unlock(object1
);
7085 object1_locked
= FALSE
;
7087 if (object2_locked
) {
7088 vm_object_unlock(object2
);
7089 object2_locked
= FALSE
;
7092 vm_object_transpose_count
++;
7099 * vm_object_cluster_size
7101 * Determine how big a cluster we should issue an I/O for...
7103 * Inputs: *start == offset of page needed
7104 * *length == maximum cluster pager can handle
7105 * Outputs: *start == beginning offset of cluster
7106 * *length == length of cluster to try
7108 * The original *start will be encompassed by the cluster
7111 extern int speculative_reads_disabled
;
7112 extern int ignore_is_ssd
;
7114 unsigned int preheat_pages_max
= MAX_UPL_TRANSFER
;
7115 unsigned int preheat_pages_min
= 8;
7117 uint32_t pre_heat_scaling
[MAX_UPL_TRANSFER
+ 1];
7118 uint32_t pre_heat_cluster
[MAX_UPL_TRANSFER
+ 1];
7121 __private_extern__
void
7122 vm_object_cluster_size(vm_object_t object
, vm_object_offset_t
*start
,
7123 vm_size_t
*length
, vm_object_fault_info_t fault_info
, uint32_t *io_streaming
)
7125 vm_size_t pre_heat_size
;
7126 vm_size_t tail_size
;
7127 vm_size_t head_size
;
7128 vm_size_t max_length
;
7129 vm_size_t cluster_size
;
7130 vm_object_offset_t object_size
;
7131 vm_object_offset_t orig_start
;
7132 vm_object_offset_t target_start
;
7133 vm_object_offset_t offset
;
7134 vm_behavior_t behavior
;
7135 boolean_t look_behind
= TRUE
;
7136 boolean_t look_ahead
= TRUE
;
7137 boolean_t isSSD
= FALSE
;
7138 uint32_t throttle_limit
;
7140 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
7141 unsigned int max_ph_size
;
7142 unsigned int min_ph_size
;
7143 unsigned int min_ph_size_in_bytes
;
7145 assert( !(*length
& PAGE_MASK
));
7146 assert( !(*start
& PAGE_MASK_64
));
7149 * remember maxiumum length of run requested
7151 max_length
= *length
;
7153 * we'll always return a cluster size of at least
7154 * 1 page, since the original fault must always
7157 *length
= PAGE_SIZE
;
7160 if (speculative_reads_disabled
|| fault_info
== NULL
) {
7162 * no cluster... just fault the page in
7166 orig_start
= *start
;
7167 target_start
= orig_start
;
7168 cluster_size
= round_page(fault_info
->cluster_size
);
7169 behavior
= fault_info
->behavior
;
7171 vm_object_lock(object
);
7173 if (object
->pager
== MEMORY_OBJECT_NULL
)
7174 goto out
; /* pager is gone for this object, nothing more to do */
7177 vnode_pager_get_isSSD(object
->pager
, &isSSD
);
7179 min_ph_size
= preheat_pages_min
;
7180 max_ph_size
= preheat_pages_max
;
7186 if (min_ph_size
< 1)
7189 if (max_ph_size
< 1)
7191 else if (max_ph_size
> MAX_UPL_TRANSFER
)
7192 max_ph_size
= MAX_UPL_TRANSFER
;
7194 if (max_length
> (max_ph_size
* PAGE_SIZE
))
7195 max_length
= max_ph_size
* PAGE_SIZE
;
7197 if (max_length
<= PAGE_SIZE
)
7200 min_ph_size_in_bytes
= min_ph_size
* PAGE_SIZE
;
7202 if (object
->internal
)
7203 object_size
= object
->vo_size
;
7205 vnode_pager_get_object_size(object
->pager
, &object_size
);
7207 object_size
= round_page_64(object_size
);
7209 if (orig_start
>= object_size
) {
7211 * fault occurred beyond the EOF...
7212 * we need to punt w/o changing the
7217 if (object
->pages_used
> object
->pages_created
) {
7219 * must have wrapped our 32 bit counters
7222 object
->pages_used
= object
->pages_created
= 0;
7224 if ((sequential_run
= object
->sequential
)) {
7225 if (sequential_run
< 0) {
7226 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
7227 sequential_run
= 0 - sequential_run
;
7229 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
7236 behavior
= VM_BEHAVIOR_DEFAULT
;
7238 case VM_BEHAVIOR_DEFAULT
:
7239 if (object
->internal
&& fault_info
->user_tag
== VM_MEMORY_STACK
)
7242 if (sequential_run
>= (3 * PAGE_SIZE
)) {
7243 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
7245 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
)
7246 look_behind
= FALSE
;
7253 if (object
->pages_created
< (20 * min_ph_size
)) {
7257 pre_heat_size
= min_ph_size_in_bytes
;
7260 * Linear growth in PH size: The maximum size is max_length...
7261 * this cacluation will result in a size that is neither a
7262 * power of 2 nor a multiple of PAGE_SIZE... so round
7263 * it up to the nearest PAGE_SIZE boundary
7265 pre_heat_size
= (max_length
* object
->pages_used
) / object
->pages_created
;
7267 if (pre_heat_size
< min_ph_size_in_bytes
)
7268 pre_heat_size
= min_ph_size_in_bytes
;
7270 pre_heat_size
= round_page(pre_heat_size
);
7275 case VM_BEHAVIOR_RANDOM
:
7276 if ((pre_heat_size
= cluster_size
) <= PAGE_SIZE
)
7280 case VM_BEHAVIOR_SEQUENTIAL
:
7281 if ((pre_heat_size
= cluster_size
) == 0)
7282 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
7283 look_behind
= FALSE
;
7288 case VM_BEHAVIOR_RSEQNTL
:
7289 if ((pre_heat_size
= cluster_size
) == 0)
7290 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
7297 throttle_limit
= (uint32_t) max_length
;
7298 assert(throttle_limit
== max_length
);
7300 if (vnode_pager_get_throttle_io_limit(object
->pager
, &throttle_limit
) == KERN_SUCCESS
) {
7301 if (max_length
> throttle_limit
)
7302 max_length
= throttle_limit
;
7304 if (pre_heat_size
> max_length
)
7305 pre_heat_size
= max_length
;
7307 if (behavior
== VM_BEHAVIOR_DEFAULT
&& (pre_heat_size
> min_ph_size_in_bytes
)) {
7309 unsigned int consider_free
= vm_page_free_count
+ vm_page_cleaned_count
;
7311 if (consider_free
< vm_page_throttle_limit
) {
7312 pre_heat_size
= trunc_page(pre_heat_size
/ 16);
7313 } else if (consider_free
< vm_page_free_target
) {
7314 pre_heat_size
= trunc_page(pre_heat_size
/ 4);
7317 if (pre_heat_size
< min_ph_size_in_bytes
)
7318 pre_heat_size
= min_ph_size_in_bytes
;
7320 if (look_ahead
== TRUE
) {
7321 if (look_behind
== TRUE
) {
7323 * if we get here its due to a random access...
7324 * so we want to center the original fault address
7325 * within the cluster we will issue... make sure
7326 * to calculate 'head_size' as a multiple of PAGE_SIZE...
7327 * 'pre_heat_size' is a multiple of PAGE_SIZE but not
7328 * necessarily an even number of pages so we need to truncate
7329 * the result to a PAGE_SIZE boundary
7331 head_size
= trunc_page(pre_heat_size
/ 2);
7333 if (target_start
> head_size
)
7334 target_start
-= head_size
;
7339 * 'target_start' at this point represents the beginning offset
7340 * of the cluster we are considering... 'orig_start' will be in
7341 * the center of this cluster if we didn't have to clip the start
7342 * due to running into the start of the file
7345 if ((target_start
+ pre_heat_size
) > object_size
)
7346 pre_heat_size
= (vm_size_t
)(round_page_64(object_size
- target_start
));
7348 * at this point caclulate the number of pages beyond the original fault
7349 * address that we want to consider... this is guaranteed not to extend beyond
7350 * the current EOF...
7352 assert((vm_size_t
)(orig_start
- target_start
) == (orig_start
- target_start
));
7353 tail_size
= pre_heat_size
- (vm_size_t
)(orig_start
- target_start
) - PAGE_SIZE
;
7355 if (pre_heat_size
> target_start
) {
7357 * since pre_heat_size is always smaller then 2^32,
7358 * if it is larger then target_start (a 64 bit value)
7359 * it is safe to clip target_start to 32 bits
7361 pre_heat_size
= (vm_size_t
) target_start
;
7365 assert( !(target_start
& PAGE_MASK_64
));
7366 assert( !(pre_heat_size
& PAGE_MASK
));
7368 pre_heat_scaling
[pre_heat_size
/ PAGE_SIZE
]++;
7370 if (pre_heat_size
<= PAGE_SIZE
)
7373 if (look_behind
== TRUE
) {
7375 * take a look at the pages before the original
7376 * faulting offset... recalculate this in case
7377 * we had to clip 'pre_heat_size' above to keep
7378 * from running past the EOF.
7380 head_size
= pre_heat_size
- tail_size
- PAGE_SIZE
;
7382 for (offset
= orig_start
- PAGE_SIZE_64
; head_size
; offset
-= PAGE_SIZE_64
, head_size
-= PAGE_SIZE
) {
7384 * don't poke below the lowest offset
7386 if (offset
< fault_info
->lo_offset
)
7389 * for external objects and internal objects w/o an existence map
7390 * vm_externl_state_get will return VM_EXTERNAL_STATE_UNKNOWN
7393 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_ABSENT
) {
7395 * we know for a fact that the pager can't provide the page
7396 * so don't include it or any pages beyond it in this cluster
7400 #endif /* MACH_PAGEMAP */
7401 if (VM_COMPRESSOR_PAGER_STATE_GET(object
, offset
)
7402 == VM_EXTERNAL_STATE_ABSENT
) {
7405 if (vm_page_lookup(object
, offset
) != VM_PAGE_NULL
) {
7407 * don't bridge resident pages
7412 *length
+= PAGE_SIZE
;
7415 if (look_ahead
== TRUE
) {
7416 for (offset
= orig_start
+ PAGE_SIZE_64
; tail_size
; offset
+= PAGE_SIZE_64
, tail_size
-= PAGE_SIZE
) {
7418 * don't poke above the highest offset
7420 if (offset
>= fault_info
->hi_offset
)
7422 assert(offset
< object_size
);
7425 * for external objects and internal objects w/o an existence map
7426 * vm_externl_state_get will return VM_EXTERNAL_STATE_UNKNOWN
7429 if (vm_external_state_get(object
->existence_map
, offset
) == VM_EXTERNAL_STATE_ABSENT
) {
7431 * we know for a fact that the pager can't provide the page
7432 * so don't include it or any pages beyond it in this cluster
7436 #endif /* MACH_PAGEMAP */
7437 if (VM_COMPRESSOR_PAGER_STATE_GET(object
, offset
)
7438 == VM_EXTERNAL_STATE_ABSENT
) {
7441 if (vm_page_lookup(object
, offset
) != VM_PAGE_NULL
) {
7443 * don't bridge resident pages
7447 *length
+= PAGE_SIZE
;
7451 if (*length
> max_length
)
7452 *length
= max_length
;
7454 pre_heat_cluster
[*length
/ PAGE_SIZE
]++;
7456 vm_object_unlock(object
);
7458 DTRACE_VM1(clustersize
, vm_size_t
, *length
);
7463 * Allow manipulation of individual page state. This is actually part of
7464 * the UPL regimen but takes place on the VM object rather than on a UPL
7470 vm_object_offset_t offset
,
7472 ppnum_t
*phys_entry
,
7477 vm_object_lock(object
);
7479 if(ops
& UPL_POP_PHYSICAL
) {
7480 if(object
->phys_contiguous
) {
7482 *phys_entry
= (ppnum_t
)
7483 (object
->vo_shadow_offset
>> PAGE_SHIFT
);
7485 vm_object_unlock(object
);
7486 return KERN_SUCCESS
;
7488 vm_object_unlock(object
);
7489 return KERN_INVALID_OBJECT
;
7492 if(object
->phys_contiguous
) {
7493 vm_object_unlock(object
);
7494 return KERN_INVALID_OBJECT
;
7498 if((dst_page
= vm_page_lookup(object
,offset
)) == VM_PAGE_NULL
) {
7499 vm_object_unlock(object
);
7500 return KERN_FAILURE
;
7503 /* Sync up on getting the busy bit */
7504 if((dst_page
->busy
|| dst_page
->cleaning
) &&
7505 (((ops
& UPL_POP_SET
) &&
7506 (ops
& UPL_POP_BUSY
)) || (ops
& UPL_POP_DUMP
))) {
7507 /* someone else is playing with the page, we will */
7509 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
7513 if (ops
& UPL_POP_DUMP
) {
7514 if (dst_page
->pmapped
== TRUE
)
7515 pmap_disconnect(dst_page
->phys_page
);
7517 VM_PAGE_FREE(dst_page
);
7524 /* Get the condition of flags before requested ops */
7525 /* are undertaken */
7527 if(dst_page
->dirty
) *flags
|= UPL_POP_DIRTY
;
7528 if(dst_page
->pageout
) *flags
|= UPL_POP_PAGEOUT
;
7529 if(dst_page
->precious
) *flags
|= UPL_POP_PRECIOUS
;
7530 if(dst_page
->absent
) *flags
|= UPL_POP_ABSENT
;
7531 if(dst_page
->busy
) *flags
|= UPL_POP_BUSY
;
7534 /* The caller should have made a call either contingent with */
7535 /* or prior to this call to set UPL_POP_BUSY */
7536 if(ops
& UPL_POP_SET
) {
7537 /* The protection granted with this assert will */
7538 /* not be complete. If the caller violates the */
7539 /* convention and attempts to change page state */
7540 /* without first setting busy we may not see it */
7541 /* because the page may already be busy. However */
7542 /* if such violations occur we will assert sooner */
7544 assert(dst_page
->busy
|| (ops
& UPL_POP_BUSY
));
7545 if (ops
& UPL_POP_DIRTY
) {
7546 SET_PAGE_DIRTY(dst_page
, FALSE
);
7548 if (ops
& UPL_POP_PAGEOUT
) dst_page
->pageout
= TRUE
;
7549 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= TRUE
;
7550 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= TRUE
;
7551 if (ops
& UPL_POP_BUSY
) dst_page
->busy
= TRUE
;
7554 if(ops
& UPL_POP_CLR
) {
7555 assert(dst_page
->busy
);
7556 if (ops
& UPL_POP_DIRTY
) dst_page
->dirty
= FALSE
;
7557 if (ops
& UPL_POP_PAGEOUT
) dst_page
->pageout
= FALSE
;
7558 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= FALSE
;
7559 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= FALSE
;
7560 if (ops
& UPL_POP_BUSY
) {
7561 dst_page
->busy
= FALSE
;
7562 PAGE_WAKEUP(dst_page
);
7566 if (dst_page
->encrypted
) {
7569 * We need to decrypt this encrypted page before the
7570 * caller can access its contents.
7571 * But if the caller really wants to access the page's
7572 * contents, they have to keep the page "busy".
7573 * Otherwise, the page could get recycled or re-encrypted
7576 if ((ops
& UPL_POP_SET
) && (ops
& UPL_POP_BUSY
) &&
7579 * The page is stable enough to be accessed by
7580 * the caller, so make sure its contents are
7583 vm_page_decrypt(dst_page
, 0);
7586 * The page is not busy, so don't bother
7587 * decrypting it, since anything could
7588 * happen to it between now and when the
7589 * caller wants to access it.
7590 * We should not give the caller access
7593 assert(!phys_entry
);
7599 * The physical page number will remain valid
7600 * only if the page is kept busy.
7601 * ENCRYPTED SWAP: make sure we don't let the
7602 * caller access an encrypted page.
7604 assert(dst_page
->busy
);
7605 assert(!dst_page
->encrypted
);
7606 *phys_entry
= dst_page
->phys_page
;
7612 vm_object_unlock(object
);
7613 return KERN_SUCCESS
;
7618 * vm_object_range_op offers performance enhancement over
7619 * vm_object_page_op for page_op functions which do not require page
7620 * level state to be returned from the call. Page_op was created to provide
7621 * a low-cost alternative to page manipulation via UPLs when only a single
7622 * page was involved. The range_op call establishes the ability in the _op
7623 * family of functions to work on multiple pages where the lack of page level
7624 * state handling allows the caller to avoid the overhead of the upl structures.
7630 vm_object_offset_t offset_beg
,
7631 vm_object_offset_t offset_end
,
7635 vm_object_offset_t offset
;
7638 if (offset_end
- offset_beg
> (uint32_t) -1) {
7639 /* range is too big and would overflow "*range" */
7640 return KERN_INVALID_ARGUMENT
;
7642 if (object
->resident_page_count
== 0) {
7644 if (ops
& UPL_ROP_PRESENT
) {
7647 *range
= (uint32_t) (offset_end
- offset_beg
);
7648 assert(*range
== (offset_end
- offset_beg
));
7651 return KERN_SUCCESS
;
7653 vm_object_lock(object
);
7655 if (object
->phys_contiguous
) {
7656 vm_object_unlock(object
);
7657 return KERN_INVALID_OBJECT
;
7660 offset
= offset_beg
& ~PAGE_MASK_64
;
7662 while (offset
< offset_end
) {
7663 dst_page
= vm_page_lookup(object
, offset
);
7664 if (dst_page
!= VM_PAGE_NULL
) {
7665 if (ops
& UPL_ROP_DUMP
) {
7666 if (dst_page
->busy
|| dst_page
->cleaning
) {
7668 * someone else is playing with the
7669 * page, we will have to wait
7671 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
7673 * need to relook the page up since it's
7674 * state may have changed while we slept
7675 * it might even belong to a different object
7680 if (dst_page
->laundry
) {
7681 dst_page
->pageout
= FALSE
;
7683 vm_pageout_steal_laundry(dst_page
, FALSE
);
7685 if (dst_page
->pmapped
== TRUE
)
7686 pmap_disconnect(dst_page
->phys_page
);
7688 VM_PAGE_FREE(dst_page
);
7690 } else if ((ops
& UPL_ROP_ABSENT
) && !dst_page
->absent
)
7692 } else if (ops
& UPL_ROP_PRESENT
)
7695 offset
+= PAGE_SIZE
;
7697 vm_object_unlock(object
);
7700 if (offset
> offset_end
)
7701 offset
= offset_end
;
7702 if(offset
> offset_beg
) {
7703 *range
= (uint32_t) (offset
- offset_beg
);
7704 assert(*range
== (offset
- offset_beg
));
7709 return KERN_SUCCESS
;
7713 * Used to point a pager directly to a range of memory (when the pager may be associated
7714 * with a non-device vnode). Takes a virtual address, an offset, and a size. We currently
7715 * expect that the virtual address will denote the start of a range that is physically contiguous.
7717 kern_return_t
pager_map_to_phys_contiguous(
7718 memory_object_control_t object
,
7719 memory_object_offset_t offset
,
7720 addr64_t base_vaddr
,
7724 boolean_t clobbered_private
;
7725 kern_return_t retval
;
7726 vm_object_t pager_object
;
7728 page_num
= pmap_find_phys(kernel_pmap
, base_vaddr
);
7731 retval
= KERN_FAILURE
;
7735 pager_object
= memory_object_control_to_vm_object(object
);
7737 if (!pager_object
) {
7738 retval
= KERN_FAILURE
;
7742 clobbered_private
= pager_object
->private;
7743 pager_object
->private = TRUE
;
7744 retval
= vm_object_populate_with_private(pager_object
, offset
, page_num
, size
);
7746 if (retval
!= KERN_SUCCESS
)
7747 pager_object
->private = clobbered_private
;
7753 uint32_t scan_object_collision
= 0;
7756 vm_object_lock(vm_object_t object
)
7758 if (object
== vm_pageout_scan_wants_object
) {
7759 scan_object_collision
++;
7762 lck_rw_lock_exclusive(&object
->Lock
);
7766 vm_object_lock_avoid(vm_object_t object
)
7768 if (object
== vm_pageout_scan_wants_object
) {
7769 scan_object_collision
++;
7776 _vm_object_lock_try(vm_object_t object
)
7778 return (lck_rw_try_lock_exclusive(&object
->Lock
));
7782 vm_object_lock_try(vm_object_t object
)
7785 * Called from hibernate path so check before blocking.
7787 if (vm_object_lock_avoid(object
) && ml_get_interrupts_enabled() && get_preemption_level()==0) {
7790 return _vm_object_lock_try(object
);
7794 vm_object_lock_shared(vm_object_t object
)
7796 if (vm_object_lock_avoid(object
)) {
7799 lck_rw_lock_shared(&object
->Lock
);
7803 vm_object_lock_try_shared(vm_object_t object
)
7805 if (vm_object_lock_avoid(object
)) {
7808 return (lck_rw_try_lock_shared(&object
->Lock
));
7812 unsigned int vm_object_change_wimg_mode_count
= 0;
7815 * The object must be locked
7818 vm_object_change_wimg_mode(vm_object_t object
, unsigned int wimg_mode
)
7822 vm_object_lock_assert_exclusive(object
);
7824 vm_object_paging_wait(object
, THREAD_UNINT
);
7826 queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
7829 pmap_set_cache_attributes(p
->phys_page
, wimg_mode
);
7831 if (wimg_mode
== VM_WIMG_USE_DEFAULT
)
7832 object
->set_cache_attr
= FALSE
;
7834 object
->set_cache_attr
= TRUE
;
7836 object
->wimg_bits
= wimg_mode
;
7838 vm_object_change_wimg_mode_count
++;
7843 kern_return_t
vm_object_pack(
7844 unsigned int *purgeable_count
,
7845 unsigned int *wired_count
,
7846 unsigned int *clean_count
,
7847 unsigned int *dirty_count
,
7848 unsigned int dirty_budget
,
7850 vm_object_t src_object
,
7851 struct default_freezer_handle
*df_handle
)
7853 kern_return_t kr
= KERN_SUCCESS
;
7855 vm_object_lock(src_object
);
7857 *purgeable_count
= *wired_count
= *clean_count
= *dirty_count
= 0;
7860 if (!src_object
->alive
|| src_object
->terminating
){
7865 if (src_object
->purgable
== VM_PURGABLE_VOLATILE
) {
7866 *purgeable_count
= src_object
->resident_page_count
;
7868 /* If the default freezer handle is null, we're just walking the pages to discover how many can be hibernated */
7869 if (df_handle
!= NULL
) {
7870 purgeable_q_t queue
;
7871 /* object should be on a queue */
7872 assert(src_object
->objq
.next
!= NULL
&&
7873 src_object
->objq
.prev
!= NULL
);
7874 queue
= vm_purgeable_object_remove(src_object
);
7876 if (src_object
->purgeable_when_ripe
) {
7877 vm_page_lock_queues();
7878 vm_purgeable_token_delete_first(queue
);
7879 vm_page_unlock_queues();
7881 vm_object_purge(src_object
);
7886 if (src_object
->ref_count
== 1) {
7887 vm_object_pack_pages(wired_count
, clean_count
, dirty_count
, dirty_budget
, src_object
, df_handle
);
7889 if (src_object
->internal
) {
7894 vm_object_unlock(src_object
);
7901 vm_object_pack_pages(
7902 unsigned int *wired_count
,
7903 unsigned int *clean_count
,
7904 unsigned int *dirty_count
,
7905 unsigned int dirty_budget
,
7906 vm_object_t src_object
,
7907 struct default_freezer_handle
*df_handle
)
7911 next
= (vm_page_t
)queue_first(&src_object
->memq
);
7913 while (!queue_end(&src_object
->memq
, (queue_entry_t
)next
)) {
7915 next
= (vm_page_t
)queue_next(&next
->listq
);
7917 /* Finish up if we've hit our pageout limit */
7918 if (dirty_budget
&& (dirty_budget
== *dirty_count
)) {
7921 assert(!p
->laundry
);
7923 if (p
->fictitious
|| p
->busy
)
7926 if (p
->absent
|| p
->unusual
|| p
->error
)
7929 if (VM_PAGE_WIRED(p
)) {
7934 if (df_handle
== NULL
) {
7935 if (p
->dirty
|| pmap_is_modified(p
->phys_page
)) {
7948 if (p
->pmapped
== TRUE
) {
7950 refmod_state
= pmap_disconnect(p
->phys_page
);
7951 if (refmod_state
& VM_MEM_MODIFIED
) {
7952 SET_PAGE_DIRTY(p
, FALSE
);
7957 default_freezer_pack_page(p
, df_handle
);
7972 struct vm_pageout_queue
*iq
;
7974 iq
= &vm_pageout_queue_internal
;
7976 assert(object
!= VM_OBJECT_NULL
);
7978 vm_object_lock(object
);
7980 if (DEFAULT_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_IS_ACTIVE
) {
7981 if (!object
->pager_initialized
) {
7983 * If there is no memory object for the page, create
7984 * one and hand it to the default pager.
7986 vm_object_pager_create(object
);
7991 next
= (vm_page_t
)queue_first(&object
->memq
);
7993 while (!queue_end(&object
->memq
, (queue_entry_t
)next
)) {
7995 next
= (vm_page_t
)queue_next(&next
->listq
);
7997 /* Throw to the pageout queue */
7998 vm_page_lockspin_queues();
8001 * see if page is already in the process of
8002 * being cleaned... if so, leave it alone
8006 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
8008 if (VM_PAGE_Q_THROTTLED(iq
)) {
8010 iq
->pgo_draining
= TRUE
;
8012 assert_wait((event_t
) (&iq
->pgo_laundry
+ 1), THREAD_INTERRUPTIBLE
);
8013 vm_page_unlock_queues();
8014 vm_object_unlock(object
);
8016 thread_block(THREAD_CONTINUE_NULL
);
8018 vm_object_lock(object
);
8022 if (p
->fictitious
|| p
->busy
) {
8023 vm_page_unlock_queues();
8027 if (p
->absent
|| p
->unusual
|| p
->error
|| VM_PAGE_WIRED(p
)) {
8028 vm_page_unlock_queues();
8034 vm_page_unlock_queues();
8038 if (p
->pmapped
== TRUE
) {
8040 refmod_state
= pmap_disconnect_options(p
->phys_page
, PMAP_OPTIONS_COMPRESSOR
, NULL
);
8041 if (refmod_state
& VM_MEM_MODIFIED
) {
8042 SET_PAGE_DIRTY(p
, FALSE
);
8046 if (p
->dirty
== FALSE
) {
8047 vm_page_unlock_queues();
8053 VM_PAGE_QUEUES_REMOVE(p
);
8054 vm_pageout_cluster(p
, TRUE
);
8056 vm_page_unlock_queues();
8059 vm_object_unlock(object
);
8066 memory_object_t pager
;
8069 vm_object_lock(object
);
8071 pager
= object
->pager
;
8073 if (!object
->pager_ready
|| pager
== MEMORY_OBJECT_NULL
) {
8074 vm_object_unlock(object
);
8075 return KERN_FAILURE
;
8078 vm_object_paging_wait(object
, THREAD_UNINT
);
8079 vm_object_paging_begin(object
);
8081 object
->blocked_access
= TRUE
;
8082 vm_object_unlock(object
);
8084 kr
= memory_object_data_reclaim(pager
, TRUE
);
8086 vm_object_lock(object
);
8088 object
->blocked_access
= FALSE
;
8089 vm_object_paging_end(object
);
8091 vm_object_unlock(object
);
8095 #endif /* CONFIG_FREEZE */