2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * File: vm/vm_object.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
62 * Virtual memory object module.
66 #include <mach_pagemap.h>
67 #include <task_swapper.h>
69 #include <mach/mach_types.h>
70 #include <mach/memory_object.h>
71 #include <mach/memory_object_default.h>
72 #include <mach/memory_object_control_server.h>
73 #include <mach/vm_param.h>
77 #include <ipc/ipc_types.h>
78 #include <ipc/ipc_port.h>
80 #include <kern/kern_types.h>
81 #include <kern/assert.h>
82 #include <kern/queue.h>
84 #include <kern/kalloc.h>
85 #include <kern/zalloc.h>
86 #include <kern/host.h>
87 #include <kern/host_statistics.h>
88 #include <kern/processor.h>
89 #include <kern/misc_protos.h>
90 #include <kern/policy_internal.h>
92 #include <vm/memory_object.h>
93 #include <vm/vm_compressor_pager.h>
94 #include <vm/vm_fault.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_pageout.h>
99 #include <vm/vm_protos.h>
100 #include <vm/vm_purgeable_internal.h>
102 #include <vm/vm_compressor.h>
104 #if CONFIG_PHANTOM_CACHE
105 #include <vm/vm_phantom_cache.h>
108 boolean_t vm_object_collapse_compressor_allowed
= TRUE
;
110 struct vm_counters vm_counters
;
112 #if VM_OBJECT_TRACKING
113 boolean_t vm_object_tracking_inited
= FALSE
;
114 btlog_t
*vm_object_tracking_btlog
;
117 vm_object_tracking_init(void)
119 int vm_object_tracking
;
121 vm_object_tracking
= 1;
122 PE_parse_boot_argn("vm_object_tracking", &vm_object_tracking
,
123 sizeof (vm_object_tracking
));
125 if (vm_object_tracking
) {
126 vm_object_tracking_btlog
= btlog_create(
127 VM_OBJECT_TRACKING_NUM_RECORDS
,
128 VM_OBJECT_TRACKING_BTDEPTH
,
129 TRUE
/* caller_will_remove_entries_for_element? */);
130 assert(vm_object_tracking_btlog
);
131 vm_object_tracking_inited
= TRUE
;
134 #endif /* VM_OBJECT_TRACKING */
137 * Virtual memory objects maintain the actual data
138 * associated with allocated virtual memory. A given
139 * page of memory exists within exactly one object.
141 * An object is only deallocated when all "references"
144 * Associated with each object is a list of all resident
145 * memory pages belonging to that object; this list is
146 * maintained by the "vm_page" module, but locked by the object's
149 * Each object also records the memory object reference
150 * that is used by the kernel to request and write
151 * back data (the memory object, field "pager"), etc...
153 * Virtual memory objects are allocated to provide
154 * zero-filled memory (vm_allocate) or map a user-defined
155 * memory object into a virtual address space (vm_map).
157 * Virtual memory objects that refer to a user-defined
158 * memory object are called "permanent", because all changes
159 * made in virtual memory are reflected back to the
160 * memory manager, which may then store it permanently.
161 * Other virtual memory objects are called "temporary",
162 * meaning that changes need be written back only when
163 * necessary to reclaim pages, and that storage associated
164 * with the object can be discarded once it is no longer
167 * A permanent memory object may be mapped into more
168 * than one virtual address space. Moreover, two threads
169 * may attempt to make the first mapping of a memory
170 * object concurrently. Only one thread is allowed to
171 * complete this mapping; all others wait for the
172 * "pager_initialized" field is asserted, indicating
173 * that the first thread has initialized all of the
174 * necessary fields in the virtual memory object structure.
176 * The kernel relies on a *default memory manager* to
177 * provide backing storage for the zero-filled virtual
178 * memory objects. The pager memory objects associated
179 * with these temporary virtual memory objects are only
180 * requested from the default memory manager when it
181 * becomes necessary. Virtual memory objects
182 * that depend on the default memory manager are called
183 * "internal". The "pager_created" field is provided to
184 * indicate whether these ports have ever been allocated.
186 * The kernel may also create virtual memory objects to
187 * hold changed pages after a copy-on-write operation.
188 * In this case, the virtual memory object (and its
189 * backing storage -- its memory object) only contain
190 * those pages that have been changed. The "shadow"
191 * field refers to the virtual memory object that contains
192 * the remainder of the contents. The "shadow_offset"
193 * field indicates where in the "shadow" these contents begin.
194 * The "copy" field refers to a virtual memory object
195 * to which changed pages must be copied before changing
196 * this object, in order to implement another form
197 * of copy-on-write optimization.
199 * The virtual memory object structure also records
200 * the attributes associated with its memory object.
201 * The "pager_ready", "can_persist" and "copy_strategy"
202 * fields represent those attributes. The "cached_list"
203 * field is used in the implementation of the persistence
206 * ZZZ Continue this comment.
209 /* Forward declarations for internal functions. */
210 static kern_return_t
vm_object_terminate(
213 static kern_return_t
vm_object_copy_call(
214 vm_object_t src_object
,
215 vm_object_offset_t src_offset
,
216 vm_object_size_t size
,
217 vm_object_t
*_result_object
);
219 static void vm_object_do_collapse(
221 vm_object_t backing_object
);
223 static void vm_object_do_bypass(
225 vm_object_t backing_object
);
227 static void vm_object_release_pager(
228 memory_object_t pager
);
230 zone_t vm_object_zone
; /* vm backing store zone */
233 * All wired-down kernel memory belongs to a single virtual
234 * memory object (kernel_object) to avoid wasting data structures.
236 static struct vm_object kernel_object_store
__attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT
)));
237 vm_object_t kernel_object
;
239 static struct vm_object compressor_object_store
__attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT
)));
240 vm_object_t compressor_object
= &compressor_object_store
;
243 * The submap object is used as a placeholder for vm_map_submap
244 * operations. The object is declared in vm_map.c because it
245 * is exported by the vm_map module. The storage is declared
246 * here because it must be initialized here.
248 static struct vm_object vm_submap_object_store
__attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT
)));
251 * Virtual memory objects are initialized from
252 * a template (see vm_object_allocate).
254 * When adding a new field to the virtual memory
255 * object structure, be sure to add initialization
256 * (see _vm_object_allocate()).
258 static struct vm_object vm_object_template
;
260 unsigned int vm_page_purged_wired
= 0;
261 unsigned int vm_page_purged_busy
= 0;
262 unsigned int vm_page_purged_others
= 0;
264 static queue_head_t vm_object_cached_list
;
265 static uint32_t vm_object_cache_pages_freed
= 0;
266 static uint32_t vm_object_cache_pages_moved
= 0;
267 static uint32_t vm_object_cache_pages_skipped
= 0;
268 static uint32_t vm_object_cache_adds
= 0;
269 static uint32_t vm_object_cached_count
= 0;
270 static lck_mtx_t vm_object_cached_lock_data
;
271 static lck_mtx_ext_t vm_object_cached_lock_data_ext
;
273 static uint32_t vm_object_page_grab_failed
= 0;
274 static uint32_t vm_object_page_grab_skipped
= 0;
275 static uint32_t vm_object_page_grab_returned
= 0;
276 static uint32_t vm_object_page_grab_pmapped
= 0;
277 static uint32_t vm_object_page_grab_reactivations
= 0;
279 #define vm_object_cache_lock_spin() \
280 lck_mtx_lock_spin(&vm_object_cached_lock_data)
281 #define vm_object_cache_unlock() \
282 lck_mtx_unlock(&vm_object_cached_lock_data)
284 static void vm_object_cache_remove_locked(vm_object_t
);
287 static void vm_object_reap(vm_object_t object
);
288 static void vm_object_reap_async(vm_object_t object
);
289 static void vm_object_reaper_thread(void);
291 static lck_mtx_t vm_object_reaper_lock_data
;
292 static lck_mtx_ext_t vm_object_reaper_lock_data_ext
;
294 static queue_head_t vm_object_reaper_queue
; /* protected by vm_object_reaper_lock() */
295 unsigned int vm_object_reap_count
= 0;
296 unsigned int vm_object_reap_count_async
= 0;
298 #define vm_object_reaper_lock() \
299 lck_mtx_lock(&vm_object_reaper_lock_data)
300 #define vm_object_reaper_lock_spin() \
301 lck_mtx_lock_spin(&vm_object_reaper_lock_data)
302 #define vm_object_reaper_unlock() \
303 lck_mtx_unlock(&vm_object_reaper_lock_data)
306 /* I/O Re-prioritization request list */
307 queue_head_t io_reprioritize_list
;
308 lck_spin_t io_reprioritize_list_lock
;
310 #define IO_REPRIORITIZE_LIST_LOCK() \
311 lck_spin_lock(&io_reprioritize_list_lock)
312 #define IO_REPRIORITIZE_LIST_UNLOCK() \
313 lck_spin_unlock(&io_reprioritize_list_lock)
315 #define MAX_IO_REPRIORITIZE_REQS 8192
316 zone_t io_reprioritize_req_zone
;
318 /* I/O Re-prioritization thread */
319 int io_reprioritize_wakeup
= 0;
320 static void io_reprioritize_thread(void *param __unused
, wait_result_t wr __unused
);
322 #define IO_REPRIO_THREAD_WAKEUP() thread_wakeup((event_t)&io_reprioritize_wakeup)
323 #define IO_REPRIO_THREAD_CONTINUATION() \
325 assert_wait(&io_reprioritize_wakeup, THREAD_UNINT); \
326 thread_block(io_reprioritize_thread); \
329 void vm_page_request_reprioritize(vm_object_t
, uint64_t, uint32_t, int);
330 void vm_page_handle_prio_inversion(vm_object_t
, vm_page_t
);
331 void vm_decmp_upl_reprioritize(upl_t
, int);
336 #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
341 * vm_object_allocate:
343 * Returns a new object with the given size.
346 __private_extern__
void
348 vm_object_size_t size
,
352 "vm_object_allocate, object 0x%X size 0x%X\n",
353 object
, size
, 0,0,0);
355 *object
= vm_object_template
;
356 vm_page_queue_init(&object
->memq
);
357 #if UPL_DEBUG || CONFIG_IOSCHED
358 queue_init(&object
->uplq
);
360 vm_object_lock_init(object
);
361 object
->vo_size
= size
;
363 #if VM_OBJECT_TRACKING_OP_CREATED
364 if (vm_object_tracking_inited
) {
365 void *bt
[VM_OBJECT_TRACKING_BTDEPTH
];
368 numsaved
= OSBacktrace(bt
, VM_OBJECT_TRACKING_BTDEPTH
);
369 btlog_add_entry(vm_object_tracking_btlog
,
371 VM_OBJECT_TRACKING_OP_CREATED
,
375 #endif /* VM_OBJECT_TRACKING_OP_CREATED */
378 __private_extern__ vm_object_t
380 vm_object_size_t size
)
384 object
= (vm_object_t
) zalloc(vm_object_zone
);
386 // dbgLog(object, size, 0, 2); /* (TEST/DEBUG) */
388 if (object
!= VM_OBJECT_NULL
)
389 _vm_object_allocate(size
, object
);
395 lck_grp_t vm_object_lck_grp
;
396 lck_grp_t vm_object_cache_lck_grp
;
397 lck_grp_attr_t vm_object_lck_grp_attr
;
398 lck_attr_t vm_object_lck_attr
;
399 lck_attr_t kernel_object_lck_attr
;
400 lck_attr_t compressor_object_lck_attr
;
403 * vm_object_bootstrap:
405 * Initialize the VM objects module.
407 __private_extern__
void
408 vm_object_bootstrap(void)
410 vm_size_t vm_object_size
;
412 assert(sizeof (mo_ipc_object_bits_t
) == sizeof (ipc_object_bits_t
));
414 vm_object_size
= (sizeof(struct vm_object
) + (VM_PACKED_POINTER_ALIGNMENT
-1)) & ~(VM_PACKED_POINTER_ALIGNMENT
- 1);
416 vm_object_zone
= zinit(vm_object_size
,
417 round_page(512*1024),
420 zone_change(vm_object_zone
, Z_CALLERACCT
, FALSE
); /* don't charge caller */
421 zone_change(vm_object_zone
, Z_NOENCRYPT
, TRUE
);
422 zone_change(vm_object_zone
, Z_ALIGNMENT_REQUIRED
, TRUE
);
424 vm_object_init_lck_grp();
426 queue_init(&vm_object_cached_list
);
428 lck_mtx_init_ext(&vm_object_cached_lock_data
,
429 &vm_object_cached_lock_data_ext
,
430 &vm_object_cache_lck_grp
,
431 &vm_object_lck_attr
);
433 queue_init(&vm_object_reaper_queue
);
435 lck_mtx_init_ext(&vm_object_reaper_lock_data
,
436 &vm_object_reaper_lock_data_ext
,
438 &vm_object_lck_attr
);
442 * Fill in a template object, for quick initialization
445 /* memq; Lock; init after allocation */
447 vm_object_template
.memq
.prev
= 0;
448 vm_object_template
.memq
.next
= 0;
451 * We can't call vm_object_lock_init() here because that will
452 * allocate some memory and VM is not fully initialized yet.
453 * The lock will be initialized for each allocated object in
454 * _vm_object_allocate(), so we don't need to initialize it in
455 * the vm_object_template.
457 vm_object_lock_init(&vm_object_template
);
459 #if DEVELOPMENT || DEBUG
460 vm_object_template
.Lock_owner
= 0;
462 vm_object_template
.vo_size
= 0;
463 vm_object_template
.memq_hint
= VM_PAGE_NULL
;
464 vm_object_template
.ref_count
= 1;
466 vm_object_template
.res_count
= 1;
467 #endif /* TASK_SWAPPER */
468 vm_object_template
.resident_page_count
= 0;
469 // static vm_object_template is zeroed
470 // vm_object_template.wired_page_count = 0;
471 vm_object_template
.reusable_page_count
= 0;
472 vm_object_template
.copy
= VM_OBJECT_NULL
;
473 vm_object_template
.shadow
= VM_OBJECT_NULL
;
474 vm_object_template
.vo_shadow_offset
= (vm_object_offset_t
) 0;
475 vm_object_template
.pager
= MEMORY_OBJECT_NULL
;
476 vm_object_template
.paging_offset
= 0;
477 vm_object_template
.pager_control
= MEMORY_OBJECT_CONTROL_NULL
;
478 vm_object_template
.copy_strategy
= MEMORY_OBJECT_COPY_SYMMETRIC
;
479 vm_object_template
.paging_in_progress
= 0;
481 vm_object_template
.__object1_unused_bits
= 0;
482 #endif /* __LP64__ */
483 vm_object_template
.activity_in_progress
= 0;
485 /* Begin bitfields */
486 vm_object_template
.all_wanted
= 0; /* all bits FALSE */
487 vm_object_template
.pager_created
= FALSE
;
488 vm_object_template
.pager_initialized
= FALSE
;
489 vm_object_template
.pager_ready
= FALSE
;
490 vm_object_template
.pager_trusted
= FALSE
;
491 vm_object_template
.can_persist
= FALSE
;
492 vm_object_template
.internal
= TRUE
;
493 vm_object_template
.private = FALSE
;
494 vm_object_template
.pageout
= FALSE
;
495 vm_object_template
.alive
= TRUE
;
496 vm_object_template
.purgable
= VM_PURGABLE_DENY
;
497 vm_object_template
.purgeable_when_ripe
= FALSE
;
498 vm_object_template
.purgeable_only_by_kernel
= FALSE
;
499 vm_object_template
.shadowed
= FALSE
;
500 vm_object_template
.true_share
= FALSE
;
501 vm_object_template
.terminating
= FALSE
;
502 vm_object_template
.named
= FALSE
;
503 vm_object_template
.shadow_severed
= FALSE
;
504 vm_object_template
.phys_contiguous
= FALSE
;
505 vm_object_template
.nophyscache
= FALSE
;
508 vm_object_template
.cached_list
.prev
= NULL
;
509 vm_object_template
.cached_list
.next
= NULL
;
511 vm_object_template
.last_alloc
= (vm_object_offset_t
) 0;
512 vm_object_template
.sequential
= (vm_object_offset_t
) 0;
513 vm_object_template
.pages_created
= 0;
514 vm_object_template
.pages_used
= 0;
515 vm_object_template
.scan_collisions
= 0;
516 #if CONFIG_PHANTOM_CACHE
517 vm_object_template
.phantom_object_id
= 0;
519 vm_object_template
.cow_hint
= ~(vm_offset_t
)0;
521 /* cache bitfields */
522 vm_object_template
.wimg_bits
= VM_WIMG_USE_DEFAULT
;
523 vm_object_template
.set_cache_attr
= FALSE
;
524 vm_object_template
.object_slid
= FALSE
;
525 vm_object_template
.code_signed
= FALSE
;
526 vm_object_template
.transposed
= FALSE
;
527 vm_object_template
.mapping_in_progress
= FALSE
;
528 vm_object_template
.phantom_isssd
= FALSE
;
529 vm_object_template
.volatile_empty
= FALSE
;
530 vm_object_template
.volatile_fault
= FALSE
;
531 vm_object_template
.all_reusable
= FALSE
;
532 vm_object_template
.blocked_access
= FALSE
;
533 vm_object_template
.__object2_unused_bits
= 0;
534 #if CONFIG_IOSCHED || UPL_DEBUG
535 vm_object_template
.uplq
.prev
= NULL
;
536 vm_object_template
.uplq
.next
= NULL
;
537 #endif /* UPL_DEBUG */
539 bzero(&vm_object_template
.pip_holders
,
540 sizeof (vm_object_template
.pip_holders
));
541 #endif /* VM_PIP_DEBUG */
543 vm_object_template
.objq
.next
= NULL
;
544 vm_object_template
.objq
.prev
= NULL
;
546 vm_object_template
.purgeable_queue_type
= PURGEABLE_Q_TYPE_MAX
;
547 vm_object_template
.purgeable_queue_group
= 0;
549 vm_object_template
.vo_cache_ts
= 0;
551 vm_object_template
.wire_tag
= VM_KERN_MEMORY_NONE
;
553 vm_object_template
.io_tracking
= FALSE
;
555 #if CONFIG_SECLUDED_MEMORY
556 vm_object_template
.eligible_for_secluded
= FALSE
;
557 vm_object_template
.can_grab_secluded
= FALSE
;
558 #else /* CONFIG_SECLUDED_MEMORY */
559 vm_object_template
.__object3_unused_bits
= 0;
560 #endif /* CONFIG_SECLUDED_MEMORY */
563 bzero(&vm_object_template
.purgeable_owner_bt
[0],
564 sizeof (vm_object_template
.purgeable_owner_bt
));
565 vm_object_template
.vo_purgeable_volatilizer
= NULL
;
566 bzero(&vm_object_template
.purgeable_volatilizer_bt
[0],
567 sizeof (vm_object_template
.purgeable_volatilizer_bt
));
571 * Initialize the "kernel object"
574 kernel_object
= &kernel_object_store
;
577 * Note that in the following size specifications, we need to add 1 because
578 * VM_MAX_KERNEL_ADDRESS (vm_last_addr) is a maximum address, not a size.
581 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
584 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
586 kernel_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
587 compressor_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
588 kernel_object
->no_tag_update
= TRUE
;
591 * Initialize the "submap object". Make it as large as the
592 * kernel object so that no limit is imposed on submap sizes.
595 vm_submap_object
= &vm_submap_object_store
;
596 _vm_object_allocate(VM_MAX_KERNEL_ADDRESS
+ 1,
598 vm_submap_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
601 * Create an "extra" reference to this object so that we never
602 * try to deallocate it; zfree doesn't like to be called with
605 vm_object_reference(vm_submap_object
);
610 vm_io_reprioritize_init(void)
612 kern_return_t result
;
613 thread_t thread
= THREAD_NULL
;
615 /* Initialze the I/O reprioritization subsystem */
616 lck_spin_init(&io_reprioritize_list_lock
, &vm_object_lck_grp
, &vm_object_lck_attr
);
617 queue_init(&io_reprioritize_list
);
619 io_reprioritize_req_zone
= zinit(sizeof(struct io_reprioritize_req
),
620 MAX_IO_REPRIORITIZE_REQS
* sizeof(struct io_reprioritize_req
),
621 4096, "io_reprioritize_req");
622 zone_change(io_reprioritize_req_zone
, Z_COLLECT
, FALSE
);
624 result
= kernel_thread_start_priority(io_reprioritize_thread
, NULL
, 95 /* MAXPRI_KERNEL */, &thread
);
625 if (result
== KERN_SUCCESS
) {
626 thread_deallocate(thread
);
628 panic("Could not create io_reprioritize_thread");
634 vm_object_reaper_init(void)
639 kr
= kernel_thread_start_priority(
640 (thread_continue_t
) vm_object_reaper_thread
,
644 if (kr
!= KERN_SUCCESS
) {
645 panic("failed to launch vm_object_reaper_thread kr=0x%x", kr
);
647 thread_deallocate(thread
);
650 __private_extern__
void
654 * Finish initializing the kernel object.
659 __private_extern__
void
660 vm_object_init_lck_grp(void)
663 * initialze the vm_object lock world
665 lck_grp_attr_setdefault(&vm_object_lck_grp_attr
);
666 lck_grp_init(&vm_object_lck_grp
, "vm_object", &vm_object_lck_grp_attr
);
667 lck_grp_init(&vm_object_cache_lck_grp
, "vm_object_cache", &vm_object_lck_grp_attr
);
668 lck_attr_setdefault(&vm_object_lck_attr
);
669 lck_attr_setdefault(&kernel_object_lck_attr
);
670 lck_attr_cleardebug(&kernel_object_lck_attr
);
671 lck_attr_setdefault(&compressor_object_lck_attr
);
672 lck_attr_cleardebug(&compressor_object_lck_attr
);
677 * vm_object_deallocate:
679 * Release a reference to the specified object,
680 * gained either through a vm_object_allocate
681 * or a vm_object_reference call. When all references
682 * are gone, storage associated with this object
683 * may be relinquished.
685 * No object may be locked.
687 unsigned long vm_object_deallocate_shared_successes
= 0;
688 unsigned long vm_object_deallocate_shared_failures
= 0;
689 unsigned long vm_object_deallocate_shared_swap_failures
= 0;
691 __private_extern__
void
692 vm_object_deallocate(
695 vm_object_t shadow
= VM_OBJECT_NULL
;
697 // if(object)dbgLog(object, object->ref_count, object->can_persist, 3); /* (TEST/DEBUG) */
698 // else dbgLog(object, 0, 0, 3); /* (TEST/DEBUG) */
700 if (object
== VM_OBJECT_NULL
)
703 if (object
== kernel_object
|| object
== compressor_object
) {
704 vm_object_lock_shared(object
);
706 OSAddAtomic(-1, &object
->ref_count
);
708 if (object
->ref_count
== 0) {
709 if (object
== kernel_object
)
710 panic("vm_object_deallocate: losing kernel_object\n");
712 panic("vm_object_deallocate: losing compressor_object\n");
714 vm_object_unlock(object
);
718 if (object
->ref_count
== 2 &&
721 * This "named" object's reference count is about to
723 * we'll need to call memory_object_last_unmap().
725 } else if (object
->ref_count
== 2 &&
727 object
->shadow
!= VM_OBJECT_NULL
) {
729 * This internal object's reference count is about to
730 * drop from 2 to 1 and it has a shadow object:
731 * we'll want to try and collapse this object with its
734 } else if (object
->ref_count
>= 2) {
735 UInt32 original_ref_count
;
736 volatile UInt32
*ref_count_p
;
740 * The object currently looks like it is not being
741 * kept alive solely by the reference we're about to release.
742 * Let's try and release our reference without taking
743 * all the locks we would need if we had to terminate the
744 * object (cache lock + exclusive object lock).
745 * Lock the object "shared" to make sure we don't race with
746 * anyone holding it "exclusive".
748 vm_object_lock_shared(object
);
749 ref_count_p
= (volatile UInt32
*) &object
->ref_count
;
750 original_ref_count
= object
->ref_count
;
752 * Test again as "ref_count" could have changed.
753 * "named" shouldn't change.
755 if (original_ref_count
== 2 &&
757 /* need to take slow path for m_o_last_unmap() */
759 } else if (original_ref_count
== 2 &&
761 object
->shadow
!= VM_OBJECT_NULL
) {
762 /* need to take slow path for vm_object_collapse() */
764 } else if (original_ref_count
< 2) {
765 /* need to take slow path for vm_object_terminate() */
768 /* try an atomic update with the shared lock */
769 atomic_swap
= OSCompareAndSwap(
771 original_ref_count
- 1,
772 (UInt32
*) &object
->ref_count
);
773 if (atomic_swap
== FALSE
) {
774 vm_object_deallocate_shared_swap_failures
++;
775 /* fall back to the slow path... */
779 vm_object_unlock(object
);
783 * ref_count was updated atomically !
785 vm_object_deallocate_shared_successes
++;
790 * Someone else updated the ref_count at the same
791 * time and we lost the race. Fall back to the usual
792 * slow but safe path...
794 vm_object_deallocate_shared_failures
++;
797 while (object
!= VM_OBJECT_NULL
) {
799 vm_object_lock(object
);
801 assert(object
->ref_count
> 0);
804 * If the object has a named reference, and only
805 * that reference would remain, inform the pager
806 * about the last "mapping" reference going away.
808 if ((object
->ref_count
== 2) && (object
->named
)) {
809 memory_object_t pager
= object
->pager
;
811 /* Notify the Pager that there are no */
812 /* more mappers for this object */
814 if (pager
!= MEMORY_OBJECT_NULL
) {
815 vm_object_mapping_wait(object
, THREAD_UNINT
);
816 vm_object_mapping_begin(object
);
817 vm_object_unlock(object
);
819 memory_object_last_unmap(pager
);
821 vm_object_lock(object
);
822 vm_object_mapping_end(object
);
824 assert(object
->ref_count
> 0);
828 * Lose the reference. If other references
829 * remain, then we are done, unless we need
830 * to retry a cache trim.
831 * If it is the last reference, then keep it
832 * until any pending initialization is completed.
835 /* if the object is terminating, it cannot go into */
836 /* the cache and we obviously should not call */
837 /* terminate again. */
839 if ((object
->ref_count
> 1) || object
->terminating
) {
840 vm_object_lock_assert_exclusive(object
);
842 vm_object_res_deallocate(object
);
844 if (object
->ref_count
== 1 &&
845 object
->shadow
!= VM_OBJECT_NULL
) {
847 * There's only one reference left on this
848 * VM object. We can't tell if it's a valid
849 * one (from a mapping for example) or if this
850 * object is just part of a possibly stale and
851 * useless shadow chain.
852 * We would like to try and collapse it into
853 * its parent, but we don't have any pointers
854 * back to this parent object.
855 * But we can try and collapse this object with
856 * its own shadows, in case these are useless
858 * We can't bypass this object though, since we
859 * don't know if this last reference on it is
862 vm_object_collapse(object
, 0, FALSE
);
864 vm_object_unlock(object
);
869 * We have to wait for initialization
870 * before destroying or caching the object.
873 if (object
->pager_created
&& ! object
->pager_initialized
) {
874 assert(! object
->can_persist
);
875 vm_object_assert_wait(object
,
876 VM_OBJECT_EVENT_INITIALIZED
,
878 vm_object_unlock(object
);
880 thread_block(THREAD_CONTINUE_NULL
);
885 "vm_o_deallocate: 0x%X res %d paging_ops %d thread 0x%p ref %d\n",
886 object
, object
->resident_page_count
,
887 object
->paging_in_progress
,
888 (void *)current_thread(),object
->ref_count
);
890 VM_OBJ_RES_DECR(object
); /* XXX ? */
892 * Terminate this object. If it had a shadow,
893 * then deallocate it; otherwise, if we need
894 * to retry a cache trim, do so now; otherwise,
895 * we are done. "pageout" objects have a shadow,
896 * but maintain a "paging reference" rather than
897 * a normal reference.
899 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
901 if (vm_object_terminate(object
) != KERN_SUCCESS
) {
904 if (shadow
!= VM_OBJECT_NULL
) {
922 vm_object_lock_assert_exclusive(object
);
924 next_p
= (vm_page_t
)vm_page_queue_first(&object
->memq
);
925 p_limit
= MIN(50, object
->resident_page_count
);
927 while (!vm_page_queue_end(&object
->memq
, (vm_page_queue_entry_t
)next_p
) && --p_limit
> 0) {
930 next_p
= (vm_page_t
)vm_page_queue_next(&next_p
->listq
);
932 if (VM_PAGE_WIRED(p
) || p
->busy
|| p
->cleaning
|| p
->laundry
|| p
->fictitious
)
933 goto move_page_in_obj
;
935 if (p
->pmapped
|| p
->dirty
|| p
->precious
) {
936 vm_page_lockspin_queues();
941 vm_object_page_grab_pmapped
++;
943 if (p
->reference
== FALSE
|| p
->dirty
== FALSE
) {
945 refmod_state
= pmap_get_refmod(VM_PAGE_GET_PHYS_PAGE(p
));
947 if (refmod_state
& VM_MEM_REFERENCED
)
949 if (refmod_state
& VM_MEM_MODIFIED
) {
950 SET_PAGE_DIRTY(p
, FALSE
);
953 if (p
->dirty
== FALSE
&& p
->precious
== FALSE
) {
955 refmod_state
= pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(p
));
957 if (refmod_state
& VM_MEM_REFERENCED
)
959 if (refmod_state
& VM_MEM_MODIFIED
) {
960 SET_PAGE_DIRTY(p
, FALSE
);
963 if (p
->dirty
== FALSE
)
967 if ((p
->vm_page_q_state
!= VM_PAGE_ON_ACTIVE_Q
) && p
->reference
== TRUE
) {
970 VM_STAT_INCR(reactivations
);
971 vm_object_page_grab_reactivations
++;
973 vm_page_unlock_queues();
975 vm_page_queue_remove(&object
->memq
, p
, vm_page_t
, listq
);
976 vm_page_queue_enter(&object
->memq
, p
, vm_page_t
, listq
);
981 vm_page_lockspin_queues();
983 vm_page_free_prepare_queues(p
);
984 vm_object_page_grab_returned
++;
985 vm_object_page_grab_skipped
+= p_skipped
;
987 vm_page_unlock_queues();
989 vm_page_free_prepare_object(p
, TRUE
);
993 vm_object_page_grab_skipped
+= p_skipped
;
994 vm_object_page_grab_failed
++;
1001 #define EVICT_PREPARE_LIMIT 64
1002 #define EVICT_AGE 10
1004 static clock_sec_t vm_object_cache_aging_ts
= 0;
1007 vm_object_cache_remove_locked(
1010 assert(object
->purgable
== VM_PURGABLE_DENY
);
1011 assert(object
->wired_page_count
== 0);
1013 queue_remove(&vm_object_cached_list
, object
, vm_object_t
, objq
);
1014 object
->objq
.next
= NULL
;
1015 object
->objq
.prev
= NULL
;
1017 vm_object_cached_count
--;
1021 vm_object_cache_remove(
1024 vm_object_cache_lock_spin();
1026 if (object
->objq
.next
|| object
->objq
.prev
)
1027 vm_object_cache_remove_locked(object
);
1029 vm_object_cache_unlock();
1033 vm_object_cache_add(
1039 assert(object
->purgable
== VM_PURGABLE_DENY
);
1040 assert(object
->wired_page_count
== 0);
1042 if (object
->resident_page_count
== 0)
1044 clock_get_system_nanotime(&sec
, &nsec
);
1046 vm_object_cache_lock_spin();
1048 if (object
->objq
.next
== NULL
&& object
->objq
.prev
== NULL
) {
1049 queue_enter(&vm_object_cached_list
, object
, vm_object_t
, objq
);
1050 object
->vo_cache_ts
= sec
+ EVICT_AGE
;
1051 object
->vo_cache_pages_to_scan
= object
->resident_page_count
;
1053 vm_object_cached_count
++;
1054 vm_object_cache_adds
++;
1056 vm_object_cache_unlock();
1060 vm_object_cache_evict(
1062 int max_objects_to_examine
)
1064 vm_object_t object
= VM_OBJECT_NULL
;
1065 vm_object_t next_obj
= VM_OBJECT_NULL
;
1066 vm_page_t local_free_q
= VM_PAGE_NULL
;
1070 vm_page_t ep_array
[EVICT_PREPARE_LIMIT
];
1076 uint32_t ep_skipped
= 0;
1080 KERNEL_DEBUG(0x13001ec | DBG_FUNC_START
, 0, 0, 0, 0, 0);
1082 * do a couple of quick checks to see if it's
1083 * worthwhile grabbing the lock
1085 if (queue_empty(&vm_object_cached_list
)) {
1086 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END
, 0, 0, 0, 0, 0);
1089 clock_get_system_nanotime(&sec
, &nsec
);
1092 * the object on the head of the queue has not
1093 * yet sufficiently aged
1095 if (sec
< vm_object_cache_aging_ts
) {
1096 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END
, 0, 0, 0, 0, 0);
1100 * don't need the queue lock to find
1101 * and lock an object on the cached list
1103 vm_page_unlock_queues();
1105 vm_object_cache_lock_spin();
1108 next_obj
= (vm_object_t
)queue_first(&vm_object_cached_list
);
1110 while (!queue_end(&vm_object_cached_list
, (queue_entry_t
)next_obj
) && object_cnt
++ < max_objects_to_examine
) {
1113 next_obj
= (vm_object_t
)queue_next(&next_obj
->objq
);
1115 assert(object
->purgable
== VM_PURGABLE_DENY
);
1116 assert(object
->wired_page_count
== 0);
1118 if (sec
< object
->vo_cache_ts
) {
1119 KERNEL_DEBUG(0x130020c, object
, object
->resident_page_count
, object
->vo_cache_ts
, sec
, 0);
1121 vm_object_cache_aging_ts
= object
->vo_cache_ts
;
1122 object
= VM_OBJECT_NULL
;
1125 if (!vm_object_lock_try_scan(object
)) {
1127 * just skip over this guy for now... if we find
1128 * an object to steal pages from, we'll revist in a bit...
1129 * hopefully, the lock will have cleared
1131 KERNEL_DEBUG(0x13001f8, object
, object
->resident_page_count
, 0, 0, 0);
1133 object
= VM_OBJECT_NULL
;
1136 if (vm_page_queue_empty(&object
->memq
) || object
->vo_cache_pages_to_scan
== 0) {
1138 * this case really shouldn't happen, but it's not fatal
1139 * so deal with it... if we don't remove the object from
1140 * the list, we'll never move past it.
1142 KERNEL_DEBUG(0x13001fc, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1144 vm_object_cache_remove_locked(object
);
1145 vm_object_unlock(object
);
1146 object
= VM_OBJECT_NULL
;
1150 * we have a locked object with pages...
1151 * time to start harvesting
1155 vm_object_cache_unlock();
1157 if (object
== VM_OBJECT_NULL
)
1161 * object is locked at this point and
1162 * has resident pages
1164 next_p
= (vm_page_t
)vm_page_queue_first(&object
->memq
);
1167 * break the page scan into 2 pieces to minimize the time spent
1168 * behind the page queue lock...
1169 * the list of pages on these unused objects is likely to be cold
1170 * w/r to the cpu cache which increases the time to scan the list
1171 * tenfold... and we may have a 'run' of pages we can't utilize that
1172 * needs to be skipped over...
1174 if ((ep_limit
= num_to_evict
- (ep_freed
+ ep_moved
)) > EVICT_PREPARE_LIMIT
)
1175 ep_limit
= EVICT_PREPARE_LIMIT
;
1178 while (!vm_page_queue_end(&object
->memq
, (vm_page_queue_entry_t
)next_p
) && object
->vo_cache_pages_to_scan
&& ep_count
< ep_limit
) {
1181 next_p
= (vm_page_t
)vm_page_queue_next(&next_p
->listq
);
1183 object
->vo_cache_pages_to_scan
--;
1185 if (VM_PAGE_WIRED(p
) || p
->busy
|| p
->cleaning
|| p
->laundry
) {
1186 vm_page_queue_remove(&object
->memq
, p
, vm_page_t
, listq
);
1187 vm_page_queue_enter(&object
->memq
, p
, vm_page_t
, listq
);
1192 if (p
->wpmapped
|| p
->dirty
|| p
->precious
) {
1193 vm_page_queue_remove(&object
->memq
, p
, vm_page_t
, listq
);
1194 vm_page_queue_enter(&object
->memq
, p
, vm_page_t
, listq
);
1196 pmap_clear_reference(VM_PAGE_GET_PHYS_PAGE(p
));
1198 ep_array
[ep_count
++] = p
;
1200 KERNEL_DEBUG(0x13001f4 | DBG_FUNC_START
, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1202 vm_page_lockspin_queues();
1204 for (ep_index
= 0; ep_index
< ep_count
; ep_index
++) {
1206 p
= ep_array
[ep_index
];
1208 if (p
->wpmapped
|| p
->dirty
|| p
->precious
) {
1209 p
->reference
= FALSE
;
1210 p
->no_cache
= FALSE
;
1213 * we've already filtered out pages that are in the laundry
1214 * so if we get here, this page can't be on the pageout queue
1216 vm_page_queues_remove(p
, FALSE
);
1217 vm_page_enqueue_inactive(p
, TRUE
);
1221 #if CONFIG_PHANTOM_CACHE
1222 vm_phantom_cache_add_ghost(p
);
1224 vm_page_free_prepare_queues(p
);
1226 assert(p
->pageq
.next
== 0 && p
->pageq
.prev
== 0);
1228 * Add this page to our list of reclaimed pages,
1229 * to be freed later.
1231 p
->snext
= local_free_q
;
1237 vm_page_unlock_queues();
1239 KERNEL_DEBUG(0x13001f4 | DBG_FUNC_END
, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1242 vm_page_free_list(local_free_q
, TRUE
);
1243 local_free_q
= VM_PAGE_NULL
;
1245 if (object
->vo_cache_pages_to_scan
== 0) {
1246 KERNEL_DEBUG(0x1300208, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1248 vm_object_cache_remove(object
);
1250 KERNEL_DEBUG(0x13001fc, object
, object
->resident_page_count
, ep_freed
, ep_moved
, 0);
1253 * done with this object
1255 vm_object_unlock(object
);
1256 object
= VM_OBJECT_NULL
;
1259 * at this point, we are not holding any locks
1261 if ((ep_freed
+ ep_moved
) >= num_to_evict
) {
1263 * we've reached our target for the
1264 * number of pages to evict
1268 vm_object_cache_lock_spin();
1271 * put the page queues lock back to the caller's
1274 vm_page_lock_queues();
1276 vm_object_cache_pages_freed
+= ep_freed
;
1277 vm_object_cache_pages_moved
+= ep_moved
;
1278 vm_object_cache_pages_skipped
+= ep_skipped
;
1280 KERNEL_DEBUG(0x13001ec | DBG_FUNC_END
, ep_freed
, 0, 0, 0, 0);
1285 * Routine: vm_object_terminate
1287 * Free all resources associated with a vm_object.
1288 * In/out conditions:
1289 * Upon entry, the object must be locked,
1290 * and the object must have exactly one reference.
1292 * The shadow object reference is left alone.
1294 * The object must be unlocked if its found that pages
1295 * must be flushed to a backing object. If someone
1296 * manages to map the object while it is being flushed
1297 * the object is returned unlocked and unchanged. Otherwise,
1298 * upon exit, the cache will be unlocked, and the
1299 * object will cease to exist.
1301 static kern_return_t
1302 vm_object_terminate(
1305 vm_object_t shadow_object
;
1307 XPR(XPR_VM_OBJECT
, "vm_object_terminate, object 0x%X ref %d\n",
1308 object
, object
->ref_count
, 0, 0, 0);
1310 vm_object_lock_assert_exclusive(object
);
1312 if (!object
->pageout
&& (!object
->internal
&& object
->can_persist
) &&
1313 (object
->pager
!= NULL
|| object
->shadow_severed
)) {
1315 * Clear pager_trusted bit so that the pages get yanked
1316 * out of the object instead of cleaned in place. This
1317 * prevents a deadlock in XMM and makes more sense anyway.
1319 object
->pager_trusted
= FALSE
;
1321 vm_object_reap_pages(object
, REAP_TERMINATE
);
1324 * Make sure the object isn't already being terminated
1326 if (object
->terminating
) {
1327 vm_object_lock_assert_exclusive(object
);
1328 object
->ref_count
--;
1329 assert(object
->ref_count
> 0);
1330 vm_object_unlock(object
);
1331 return KERN_FAILURE
;
1335 * Did somebody get a reference to the object while we were
1338 if (object
->ref_count
!= 1) {
1339 vm_object_lock_assert_exclusive(object
);
1340 object
->ref_count
--;
1341 assert(object
->ref_count
> 0);
1342 vm_object_res_deallocate(object
);
1343 vm_object_unlock(object
);
1344 return KERN_FAILURE
;
1348 * Make sure no one can look us up now.
1351 object
->terminating
= TRUE
;
1352 object
->alive
= FALSE
;
1354 if ( !object
->internal
&& (object
->objq
.next
|| object
->objq
.prev
))
1355 vm_object_cache_remove(object
);
1358 * Detach the object from its shadow if we are the shadow's
1359 * copy. The reference we hold on the shadow must be dropped
1362 if (((shadow_object
= object
->shadow
) != VM_OBJECT_NULL
) &&
1363 !(object
->pageout
)) {
1364 vm_object_lock(shadow_object
);
1365 if (shadow_object
->copy
== object
)
1366 shadow_object
->copy
= VM_OBJECT_NULL
;
1367 vm_object_unlock(shadow_object
);
1370 if (object
->paging_in_progress
!= 0 ||
1371 object
->activity_in_progress
!= 0) {
1373 * There are still some paging_in_progress references
1374 * on this object, meaning that there are some paging
1375 * or other I/O operations in progress for this VM object.
1376 * Such operations take some paging_in_progress references
1377 * up front to ensure that the object doesn't go away, but
1378 * they may also need to acquire a reference on the VM object,
1379 * to map it in kernel space, for example. That means that
1380 * they may end up releasing the last reference on the VM
1381 * object, triggering its termination, while still holding
1382 * paging_in_progress references. Waiting for these
1383 * pending paging_in_progress references to go away here would
1386 * To avoid deadlocking, we'll let the vm_object_reaper_thread
1387 * complete the VM object termination if it still holds
1388 * paging_in_progress references at this point.
1390 * No new paging_in_progress should appear now that the
1391 * VM object is "terminating" and not "alive".
1393 vm_object_reap_async(object
);
1394 vm_object_unlock(object
);
1396 * Return KERN_FAILURE to let the caller know that we
1397 * haven't completed the termination and it can't drop this
1398 * object's reference on its shadow object yet.
1399 * The reaper thread will take care of that once it has
1400 * completed this object's termination.
1402 return KERN_FAILURE
;
1405 * complete the VM object termination
1407 vm_object_reap(object
);
1408 object
= VM_OBJECT_NULL
;
1411 * the object lock was released by vm_object_reap()
1413 * KERN_SUCCESS means that this object has been terminated
1414 * and no longer needs its shadow object but still holds a
1416 * The caller is responsible for dropping that reference.
1417 * We can't call vm_object_deallocate() here because that
1418 * would create a recursion.
1420 return KERN_SUCCESS
;
1427 * Complete the termination of a VM object after it's been marked
1428 * as "terminating" and "!alive" by vm_object_terminate().
1430 * The VM object must be locked by caller.
1431 * The lock will be released on return and the VM object is no longer valid.
1438 memory_object_t pager
;
1440 vm_object_lock_assert_exclusive(object
);
1441 assert(object
->paging_in_progress
== 0);
1442 assert(object
->activity_in_progress
== 0);
1444 vm_object_reap_count
++;
1447 * Disown this purgeable object to cleanup its owner's purgeable
1448 * ledgers. We need to do this before disconnecting the object
1449 * from its pager, to properly account for compressed pages.
1451 if (object
->internal
&&
1452 object
->purgable
!= VM_PURGABLE_DENY
) {
1453 vm_purgeable_accounting(object
,
1458 pager
= object
->pager
;
1459 object
->pager
= MEMORY_OBJECT_NULL
;
1461 if (pager
!= MEMORY_OBJECT_NULL
)
1462 memory_object_control_disable(object
->pager_control
);
1464 object
->ref_count
--;
1466 assert(object
->res_count
== 0);
1467 #endif /* TASK_SWAPPER */
1469 assert (object
->ref_count
== 0);
1472 * remove from purgeable queue if it's on
1474 if (object
->internal
) {
1477 owner
= object
->vo_purgeable_owner
;
1479 VM_OBJECT_UNWIRED(object
);
1481 if (object
->purgable
== VM_PURGABLE_DENY
) {
1482 /* not purgeable: nothing to do */
1483 } else if (object
->purgable
== VM_PURGABLE_VOLATILE
) {
1484 purgeable_q_t queue
;
1486 assert(object
->vo_purgeable_owner
== NULL
);
1488 queue
= vm_purgeable_object_remove(object
);
1491 if (object
->purgeable_when_ripe
) {
1493 * Must take page lock for this -
1494 * using it to protect token queue
1496 vm_page_lock_queues();
1497 vm_purgeable_token_delete_first(queue
);
1499 assert(queue
->debug_count_objects
>=0);
1500 vm_page_unlock_queues();
1504 * Update "vm_page_purgeable_count" in bulk and mark
1505 * object as VM_PURGABLE_EMPTY to avoid updating
1506 * "vm_page_purgeable_count" again in vm_page_remove()
1507 * when reaping the pages.
1510 assert(object
->resident_page_count
>=
1511 object
->wired_page_count
);
1512 delta
= (object
->resident_page_count
-
1513 object
->wired_page_count
);
1515 assert(vm_page_purgeable_count
>= delta
);
1517 (SInt32
*)&vm_page_purgeable_count
);
1519 if (object
->wired_page_count
!= 0) {
1520 assert(vm_page_purgeable_wired_count
>=
1521 object
->wired_page_count
);
1522 OSAddAtomic(-object
->wired_page_count
,
1523 (SInt32
*)&vm_page_purgeable_wired_count
);
1525 object
->purgable
= VM_PURGABLE_EMPTY
;
1527 else if (object
->purgable
== VM_PURGABLE_NONVOLATILE
||
1528 object
->purgable
== VM_PURGABLE_EMPTY
) {
1529 /* remove from nonvolatile queue */
1530 assert(object
->vo_purgeable_owner
== TASK_NULL
);
1531 vm_purgeable_nonvolatile_dequeue(object
);
1533 panic("object %p in unexpected purgeable state 0x%x\n",
1534 object
, object
->purgable
);
1536 assert(object
->objq
.next
== NULL
);
1537 assert(object
->objq
.prev
== NULL
);
1540 if (object
->pageout
) {
1542 * free all remaining pages tabled on
1544 * clean up it's shadow
1546 assert(object
->shadow
!= VM_OBJECT_NULL
);
1548 vm_pageout_object_terminate(object
);
1550 } else if (object
->resident_page_count
) {
1552 * free all remaining pages tabled on
1555 vm_object_reap_pages(object
, REAP_REAP
);
1557 assert(vm_page_queue_empty(&object
->memq
));
1558 assert(object
->paging_in_progress
== 0);
1559 assert(object
->activity_in_progress
== 0);
1560 assert(object
->ref_count
== 0);
1563 * If the pager has not already been released by
1564 * vm_object_destroy, we need to terminate it and
1565 * release our reference to it here.
1567 if (pager
!= MEMORY_OBJECT_NULL
) {
1568 vm_object_unlock(object
);
1569 vm_object_release_pager(pager
);
1570 vm_object_lock(object
);
1573 /* kick off anyone waiting on terminating */
1574 object
->terminating
= FALSE
;
1575 vm_object_paging_begin(object
);
1576 vm_object_paging_end(object
);
1577 vm_object_unlock(object
);
1579 object
->shadow
= VM_OBJECT_NULL
;
1581 #if VM_OBJECT_TRACKING
1582 if (vm_object_tracking_inited
) {
1583 btlog_remove_entries_for_element(vm_object_tracking_btlog
,
1586 #endif /* VM_OBJECT_TRACKING */
1588 vm_object_lock_destroy(object
);
1590 * Free the space for the object.
1592 zfree(vm_object_zone
, object
);
1593 object
= VM_OBJECT_NULL
;
1597 unsigned int vm_max_batch
= 256;
1599 #define V_O_R_MAX_BATCH 128
1601 #define BATCH_LIMIT(max) (vm_max_batch >= max ? max : vm_max_batch)
1604 #define VM_OBJ_REAP_FREELIST(_local_free_q, do_disconnect) \
1606 if (_local_free_q) { \
1607 if (do_disconnect) { \
1609 for (m = _local_free_q; \
1610 m != VM_PAGE_NULL; \
1613 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m)); \
1617 vm_page_free_list(_local_free_q, TRUE); \
1618 _local_free_q = VM_PAGE_NULL; \
1624 vm_object_reap_pages(
1630 vm_page_t local_free_q
= VM_PAGE_NULL
;
1632 boolean_t disconnect_on_release
;
1633 pmap_flush_context pmap_flush_context_storage
;
1635 if (reap_type
== REAP_DATA_FLUSH
) {
1637 * We need to disconnect pages from all pmaps before
1638 * releasing them to the free list
1640 disconnect_on_release
= TRUE
;
1643 * Either the caller has already disconnected the pages
1644 * from all pmaps, or we disconnect them here as we add
1645 * them to out local list of pages to be released.
1646 * No need to re-disconnect them when we release the pages
1649 disconnect_on_release
= FALSE
;
1652 restart_after_sleep
:
1653 if (vm_page_queue_empty(&object
->memq
))
1655 loop_count
= BATCH_LIMIT(V_O_R_MAX_BATCH
);
1657 if (reap_type
== REAP_PURGEABLE
)
1658 pmap_flush_context_init(&pmap_flush_context_storage
);
1660 vm_page_lockspin_queues();
1662 next
= (vm_page_t
)vm_page_queue_first(&object
->memq
);
1664 while (!vm_page_queue_end(&object
->memq
, (vm_page_queue_entry_t
)next
)) {
1667 next
= (vm_page_t
)vm_page_queue_next(&next
->listq
);
1669 if (--loop_count
== 0) {
1671 vm_page_unlock_queues();
1675 if (reap_type
== REAP_PURGEABLE
) {
1676 pmap_flush(&pmap_flush_context_storage
);
1677 pmap_flush_context_init(&pmap_flush_context_storage
);
1680 * Free the pages we reclaimed so far
1681 * and take a little break to avoid
1682 * hogging the page queue lock too long
1684 VM_OBJ_REAP_FREELIST(local_free_q
,
1685 disconnect_on_release
);
1689 loop_count
= BATCH_LIMIT(V_O_R_MAX_BATCH
);
1691 vm_page_lockspin_queues();
1693 if (reap_type
== REAP_DATA_FLUSH
|| reap_type
== REAP_TERMINATE
) {
1695 if (p
->busy
|| p
->cleaning
) {
1697 vm_page_unlock_queues();
1699 * free the pages reclaimed so far
1701 VM_OBJ_REAP_FREELIST(local_free_q
,
1702 disconnect_on_release
);
1704 PAGE_SLEEP(object
, p
, THREAD_UNINT
);
1706 goto restart_after_sleep
;
1709 vm_pageout_steal_laundry(p
, TRUE
);
1711 switch (reap_type
) {
1713 case REAP_DATA_FLUSH
:
1714 if (VM_PAGE_WIRED(p
)) {
1716 * this is an odd case... perhaps we should
1717 * zero-fill this page since we're conceptually
1718 * tossing its data at this point, but leaving
1719 * it on the object to honor the 'wire' contract
1725 case REAP_PURGEABLE
:
1726 if (VM_PAGE_WIRED(p
)) {
1728 * can't purge a wired page
1730 vm_page_purged_wired
++;
1733 if (p
->laundry
&& !p
->busy
&& !p
->cleaning
)
1734 vm_pageout_steal_laundry(p
, TRUE
);
1736 if (p
->cleaning
|| p
->laundry
|| p
->absent
) {
1738 * page is being acted upon,
1739 * so don't mess with it
1741 vm_page_purged_others
++;
1746 * We can't reclaim a busy page but we can
1747 * make it more likely to be paged (it's not wired) to make
1748 * sure that it gets considered by
1749 * vm_pageout_scan() later.
1751 if (VM_PAGE_PAGEABLE(p
))
1752 vm_page_deactivate(p
);
1753 vm_page_purged_busy
++;
1757 assert(VM_PAGE_OBJECT(p
) != kernel_object
);
1760 * we can discard this page...
1762 if (p
->pmapped
== TRUE
) {
1766 pmap_disconnect_options(VM_PAGE_GET_PHYS_PAGE(p
), PMAP_OPTIONS_NOFLUSH
| PMAP_OPTIONS_NOREFMOD
, (void *)&pmap_flush_context_storage
);
1768 vm_page_purged_count
++;
1772 case REAP_TERMINATE
:
1773 if (p
->absent
|| p
->private) {
1775 * For private pages, VM_PAGE_FREE just
1776 * leaves the page structure around for
1777 * its owner to clean up. For absent
1778 * pages, the structure is returned to
1779 * the appropriate pool.
1783 if (p
->fictitious
) {
1784 assert (VM_PAGE_GET_PHYS_PAGE(p
) == vm_page_guard_addr
);
1787 if (!p
->dirty
&& p
->wpmapped
)
1788 p
->dirty
= pmap_is_modified(VM_PAGE_GET_PHYS_PAGE(p
));
1790 if ((p
->dirty
|| p
->precious
) && !p
->error
&& object
->alive
) {
1792 assert(!object
->internal
);
1794 p
->free_when_done
= TRUE
;
1797 vm_page_queues_remove(p
, TRUE
);
1799 * flush page... page will be freed
1800 * upon completion of I/O
1802 vm_pageout_cluster(p
);
1804 vm_page_unlock_queues();
1806 * free the pages reclaimed so far
1808 VM_OBJ_REAP_FREELIST(local_free_q
,
1809 disconnect_on_release
);
1811 vm_object_paging_wait(object
, THREAD_UNINT
);
1813 goto restart_after_sleep
;
1820 vm_page_free_prepare_queues(p
);
1821 assert(p
->pageq
.next
== 0 && p
->pageq
.prev
== 0);
1823 * Add this page to our list of reclaimed pages,
1824 * to be freed later.
1826 p
->snext
= local_free_q
;
1829 vm_page_unlock_queues();
1832 * Free the remaining reclaimed pages
1834 if (reap_type
== REAP_PURGEABLE
)
1835 pmap_flush(&pmap_flush_context_storage
);
1837 VM_OBJ_REAP_FREELIST(local_free_q
,
1838 disconnect_on_release
);
1843 vm_object_reap_async(
1846 vm_object_lock_assert_exclusive(object
);
1848 vm_object_reaper_lock_spin();
1850 vm_object_reap_count_async
++;
1852 /* enqueue the VM object... */
1853 queue_enter(&vm_object_reaper_queue
, object
,
1854 vm_object_t
, cached_list
);
1856 vm_object_reaper_unlock();
1858 /* ... and wake up the reaper thread */
1859 thread_wakeup((event_t
) &vm_object_reaper_queue
);
1864 vm_object_reaper_thread(void)
1866 vm_object_t object
, shadow_object
;
1868 vm_object_reaper_lock_spin();
1870 while (!queue_empty(&vm_object_reaper_queue
)) {
1871 queue_remove_first(&vm_object_reaper_queue
,
1876 vm_object_reaper_unlock();
1877 vm_object_lock(object
);
1879 assert(object
->terminating
);
1880 assert(!object
->alive
);
1883 * The pageout daemon might be playing with our pages.
1884 * Now that the object is dead, it won't touch any more
1885 * pages, but some pages might already be on their way out.
1886 * Hence, we wait until the active paging activities have
1887 * ceased before we break the association with the pager
1890 while (object
->paging_in_progress
!= 0 ||
1891 object
->activity_in_progress
!= 0) {
1892 vm_object_wait(object
,
1893 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
1895 vm_object_lock(object
);
1899 object
->pageout
? VM_OBJECT_NULL
: object
->shadow
;
1901 vm_object_reap(object
);
1902 /* cache is unlocked and object is no longer valid */
1903 object
= VM_OBJECT_NULL
;
1905 if (shadow_object
!= VM_OBJECT_NULL
) {
1907 * Drop the reference "object" was holding on
1908 * its shadow object.
1910 vm_object_deallocate(shadow_object
);
1911 shadow_object
= VM_OBJECT_NULL
;
1913 vm_object_reaper_lock_spin();
1916 /* wait for more work... */
1917 assert_wait((event_t
) &vm_object_reaper_queue
, THREAD_UNINT
);
1919 vm_object_reaper_unlock();
1921 thread_block((thread_continue_t
) vm_object_reaper_thread
);
1926 * Routine: vm_object_release_pager
1927 * Purpose: Terminate the pager and, upon completion,
1928 * release our last reference to it.
1931 vm_object_release_pager(
1932 memory_object_t pager
)
1936 * Terminate the pager.
1939 (void) memory_object_terminate(pager
);
1942 * Release reference to pager.
1944 memory_object_deallocate(pager
);
1948 * Routine: vm_object_destroy
1950 * Shut down a VM object, despite the
1951 * presence of address map (or other) references
1957 __unused kern_return_t reason
)
1959 memory_object_t old_pager
;
1961 if (object
== VM_OBJECT_NULL
)
1962 return(KERN_SUCCESS
);
1965 * Remove the pager association immediately.
1967 * This will prevent the memory manager from further
1968 * meddling. [If it wanted to flush data or make
1969 * other changes, it should have done so before performing
1970 * the destroy call.]
1973 vm_object_lock(object
);
1974 object
->can_persist
= FALSE
;
1975 object
->named
= FALSE
;
1976 object
->alive
= FALSE
;
1978 old_pager
= object
->pager
;
1979 object
->pager
= MEMORY_OBJECT_NULL
;
1980 if (old_pager
!= MEMORY_OBJECT_NULL
)
1981 memory_object_control_disable(object
->pager_control
);
1984 * Wait for the existing paging activity (that got
1985 * through before we nulled out the pager) to subside.
1988 vm_object_paging_wait(object
, THREAD_UNINT
);
1989 vm_object_unlock(object
);
1992 * Terminate the object now.
1994 if (old_pager
!= MEMORY_OBJECT_NULL
) {
1995 vm_object_release_pager(old_pager
);
1998 * JMM - Release the caller's reference. This assumes the
1999 * caller had a reference to release, which is a big (but
2000 * currently valid) assumption if this is driven from the
2001 * vnode pager (it is holding a named reference when making
2004 vm_object_deallocate(object
);
2007 return(KERN_SUCCESS
);
2011 * The "chunk" macros are used by routines below when looking for pages to deactivate. These
2012 * exist because of the need to handle shadow chains. When deactivating pages, we only
2013 * want to deactive the ones at the top most level in the object chain. In order to do
2014 * this efficiently, the specified address range is divided up into "chunks" and we use
2015 * a bit map to keep track of which pages have already been processed as we descend down
2016 * the shadow chain. These chunk macros hide the details of the bit map implementation
2017 * as much as we can.
2019 * For convenience, we use a 64-bit data type as the bit map, and therefore a chunk is
2020 * set to 64 pages. The bit map is indexed from the low-order end, so that the lowest
2021 * order bit represents page 0 in the current range and highest order bit represents
2024 * For further convenience, we also use negative logic for the page state in the bit map.
2025 * The bit is set to 1 to indicate it has not yet been seen, and to 0 to indicate it has
2026 * been processed. This way we can simply test the 64-bit long word to see if it's zero
2027 * to easily tell if the whole range has been processed. Therefore, the bit map starts
2028 * out with all the bits set. The macros below hide all these details from the caller.
2031 #define PAGES_IN_A_CHUNK 64 /* The number of pages in the chunk must */
2032 /* be the same as the number of bits in */
2033 /* the chunk_state_t type. We use 64 */
2034 /* just for convenience. */
2036 #define CHUNK_SIZE (PAGES_IN_A_CHUNK * PAGE_SIZE_64) /* Size of a chunk in bytes */
2038 typedef uint64_t chunk_state_t
;
2041 * The bit map uses negative logic, so we start out with all 64 bits set to indicate
2042 * that no pages have been processed yet. Also, if len is less than the full CHUNK_SIZE,
2043 * then we mark pages beyond the len as having been "processed" so that we don't waste time
2044 * looking at pages in that range. This can save us from unnecessarily chasing down the
2048 #define CHUNK_INIT(c, len) \
2052 (c) = 0xffffffffffffffffLL; \
2054 for (p = (len) / PAGE_SIZE_64; p < PAGES_IN_A_CHUNK; p++) \
2055 MARK_PAGE_HANDLED(c, p); \
2060 * Return true if all pages in the chunk have not yet been processed.
2063 #define CHUNK_NOT_COMPLETE(c) ((c) != 0)
2066 * Return true if the page at offset 'p' in the bit map has already been handled
2067 * while processing a higher level object in the shadow chain.
2070 #define PAGE_ALREADY_HANDLED(c, p) (((c) & (1LL << (p))) == 0)
2073 * Mark the page at offset 'p' in the bit map as having been processed.
2076 #define MARK_PAGE_HANDLED(c, p) \
2078 (c) = (c) & ~(1LL << (p)); \
2083 * Return true if the page at the given offset has been paged out. Object is
2084 * locked upon entry and returned locked.
2090 vm_object_offset_t offset
)
2092 if (object
->internal
&&
2094 !object
->terminating
&&
2095 object
->pager_ready
) {
2097 if (VM_COMPRESSOR_PAGER_STATE_GET(object
, offset
)
2098 == VM_EXTERNAL_STATE_EXISTS
) {
2108 * madvise_free_debug
2110 * To help debug madvise(MADV_FREE*) mis-usage, this triggers a
2111 * zero-fill as soon as a page is affected by a madvise(MADV_FREE*), to
2112 * simulate the loss of the page's contents as if the page had been
2113 * reclaimed and then re-faulted.
2115 #if DEVELOPMENT || DEBUG
2116 int madvise_free_debug
= 1;
2118 int madvise_free_debug
= 0;
2122 * Deactivate the pages in the specified object and range. If kill_page is set, also discard any
2123 * page modified state from the pmap. Update the chunk_state as we go along. The caller must specify
2124 * a size that is less than or equal to the CHUNK_SIZE.
2128 deactivate_pages_in_object(
2130 vm_object_offset_t offset
,
2131 vm_object_size_t size
,
2132 boolean_t kill_page
,
2133 boolean_t reusable_page
,
2134 boolean_t all_reusable
,
2135 chunk_state_t
*chunk_state
,
2136 pmap_flush_context
*pfc
,
2138 vm_map_offset_t pmap_offset
)
2142 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
2143 struct vm_page_delayed_work
*dwp
;
2146 unsigned int reusable
= 0;
2149 * Examine each page in the chunk. The variable 'p' is the page number relative to the start of the
2150 * chunk. Since this routine is called once for each level in the shadow chain, the chunk_state may
2151 * have pages marked as having been processed already. We stop the loop early if we find we've handled
2152 * all the pages in the chunk.
2157 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
2159 for(p
= 0; size
&& CHUNK_NOT_COMPLETE(*chunk_state
); p
++, size
-= PAGE_SIZE_64
, offset
+= PAGE_SIZE_64
, pmap_offset
+= PAGE_SIZE_64
) {
2162 * If this offset has already been found and handled in a higher level object, then don't
2163 * do anything with it in the current shadow object.
2166 if (PAGE_ALREADY_HANDLED(*chunk_state
, p
))
2170 * See if the page at this offset is around. First check to see if the page is resident,
2171 * then if not, check the existence map or with the pager.
2174 if ((m
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
2177 * We found a page we were looking for. Mark it as "handled" now in the chunk_state
2178 * so that we won't bother looking for a page at this offset again if there are more
2179 * shadow objects. Then deactivate the page.
2182 MARK_PAGE_HANDLED(*chunk_state
, p
);
2184 if (( !VM_PAGE_WIRED(m
)) && (!m
->private) && (!m
->gobbled
) && (!m
->busy
) &&
2185 (!m
->laundry
) && (!m
->cleaning
) && !(m
->free_when_done
)) {
2192 clear_refmod
= VM_MEM_REFERENCED
;
2193 dwp
->dw_mask
|= DW_clear_reference
;
2195 if ((kill_page
) && (object
->internal
)) {
2196 if (madvise_free_debug
) {
2198 * zero-fill the page now
2199 * to simulate it being
2200 * reclaimed and re-faulted.
2202 pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(m
));
2204 m
->precious
= FALSE
;
2207 clear_refmod
|= VM_MEM_MODIFIED
;
2208 if (m
->vm_page_q_state
== VM_PAGE_ON_THROTTLED_Q
) {
2210 * This page is now clean and
2211 * reclaimable. Move it out
2212 * of the throttled queue, so
2213 * that vm_pageout_scan() can
2216 dwp
->dw_mask
|= DW_move_page
;
2219 VM_COMPRESSOR_PAGER_STATE_CLR(object
, offset
);
2221 if (reusable_page
&& !m
->reusable
) {
2222 assert(!all_reusable
);
2223 assert(!object
->all_reusable
);
2225 object
->reusable_page_count
++;
2226 assert(object
->resident_page_count
>= object
->reusable_page_count
);
2229 * Tell pmap this page is now
2230 * "reusable" (to update pmap
2231 * stats for all mappings).
2233 pmap_options
|= PMAP_OPTIONS_SET_REUSABLE
;
2236 pmap_options
|= PMAP_OPTIONS_NOFLUSH
;
2237 pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE(m
),
2242 if ((m
->vm_page_q_state
!= VM_PAGE_ON_THROTTLED_Q
) && !(reusable_page
|| all_reusable
))
2243 dwp
->dw_mask
|= DW_move_page
;
2246 VM_PAGE_ADD_DELAYED_WORK(dwp
, m
,
2249 if (dw_count
>= dw_limit
) {
2251 OSAddAtomic(reusable
,
2252 &vm_page_stats_reusable
.reusable_count
);
2253 vm_page_stats_reusable
.reusable
+= reusable
;
2256 vm_page_do_delayed_work(object
, VM_KERN_MEMORY_NONE
, &dw_array
[0], dw_count
);
2266 * The page at this offset isn't memory resident, check to see if it's
2267 * been paged out. If so, mark it as handled so we don't bother looking
2268 * for it in the shadow chain.
2271 if (page_is_paged_out(object
, offset
)) {
2272 MARK_PAGE_HANDLED(*chunk_state
, p
);
2275 * If we're killing a non-resident page, then clear the page in the existence
2276 * map so we don't bother paging it back in if it's touched again in the future.
2279 if ((kill_page
) && (object
->internal
)) {
2281 VM_COMPRESSOR_PAGER_STATE_CLR(object
, offset
);
2283 if (pmap
!= PMAP_NULL
) {
2285 * Tell pmap that this page
2286 * is no longer mapped, to
2287 * adjust the footprint ledger
2288 * because this page is no
2289 * longer compressed.
2291 pmap_remove_options(
2296 PMAP_OPTIONS_REMOVE
);
2304 OSAddAtomic(reusable
, &vm_page_stats_reusable
.reusable_count
);
2305 vm_page_stats_reusable
.reusable
+= reusable
;
2310 vm_page_do_delayed_work(object
, VM_KERN_MEMORY_NONE
, &dw_array
[0], dw_count
);
2315 * Deactive a "chunk" of the given range of the object starting at offset. A "chunk"
2316 * will always be less than or equal to the given size. The total range is divided up
2317 * into chunks for efficiency and performance related to the locks and handling the shadow
2318 * chain. This routine returns how much of the given "size" it actually processed. It's
2319 * up to the caler to loop and keep calling this routine until the entire range they want
2320 * to process has been done.
2323 static vm_object_size_t
2325 vm_object_t orig_object
,
2326 vm_object_offset_t offset
,
2327 vm_object_size_t size
,
2328 boolean_t kill_page
,
2329 boolean_t reusable_page
,
2330 boolean_t all_reusable
,
2331 pmap_flush_context
*pfc
,
2333 vm_map_offset_t pmap_offset
)
2336 vm_object_t tmp_object
;
2337 vm_object_size_t length
;
2338 chunk_state_t chunk_state
;
2342 * Get set to do a chunk. We'll do up to CHUNK_SIZE, but no more than the
2343 * remaining size the caller asked for.
2346 length
= MIN(size
, CHUNK_SIZE
);
2349 * The chunk_state keeps track of which pages we've already processed if there's
2350 * a shadow chain on this object. At this point, we haven't done anything with this
2351 * range of pages yet, so initialize the state to indicate no pages processed yet.
2354 CHUNK_INIT(chunk_state
, length
);
2355 object
= orig_object
;
2358 * Start at the top level object and iterate around the loop once for each object
2359 * in the shadow chain. We stop processing early if we've already found all the pages
2360 * in the range. Otherwise we stop when we run out of shadow objects.
2363 while (object
&& CHUNK_NOT_COMPLETE(chunk_state
)) {
2364 vm_object_paging_begin(object
);
2366 deactivate_pages_in_object(object
, offset
, length
, kill_page
, reusable_page
, all_reusable
, &chunk_state
, pfc
, pmap
, pmap_offset
);
2368 vm_object_paging_end(object
);
2371 * We've finished with this object, see if there's a shadow object. If
2372 * there is, update the offset and lock the new object. We also turn off
2373 * kill_page at this point since we only kill pages in the top most object.
2376 tmp_object
= object
->shadow
;
2380 reusable_page
= FALSE
;
2381 all_reusable
= FALSE
;
2382 offset
+= object
->vo_shadow_offset
;
2383 vm_object_lock(tmp_object
);
2386 if (object
!= orig_object
)
2387 vm_object_unlock(object
);
2389 object
= tmp_object
;
2392 if (object
&& object
!= orig_object
)
2393 vm_object_unlock(object
);
2401 * Move any resident pages in the specified range to the inactive queue. If kill_page is set,
2402 * we also clear the modified status of the page and "forget" any changes that have been made
2406 __private_extern__
void
2407 vm_object_deactivate_pages(
2409 vm_object_offset_t offset
,
2410 vm_object_size_t size
,
2411 boolean_t kill_page
,
2412 boolean_t reusable_page
,
2414 vm_map_offset_t pmap_offset
)
2416 vm_object_size_t length
;
2417 boolean_t all_reusable
;
2418 pmap_flush_context pmap_flush_context_storage
;
2421 * We break the range up into chunks and do one chunk at a time. This is for
2422 * efficiency and performance while handling the shadow chains and the locks.
2423 * The deactivate_a_chunk() function returns how much of the range it processed.
2424 * We keep calling this routine until the given size is exhausted.
2428 all_reusable
= FALSE
;
2431 * For the sake of accurate "reusable" pmap stats, we need
2432 * to tell pmap about each page that is no longer "reusable",
2433 * so we can't do the "all_reusable" optimization.
2436 if (reusable_page
&&
2438 object
->vo_size
!= 0 &&
2439 object
->vo_size
== size
&&
2440 object
->reusable_page_count
== 0) {
2441 all_reusable
= TRUE
;
2442 reusable_page
= FALSE
;
2446 if ((reusable_page
|| all_reusable
) && object
->all_reusable
) {
2447 /* This means MADV_FREE_REUSABLE has been called twice, which
2448 * is probably illegal. */
2452 pmap_flush_context_init(&pmap_flush_context_storage
);
2455 length
= deactivate_a_chunk(object
, offset
, size
, kill_page
, reusable_page
, all_reusable
, &pmap_flush_context_storage
, pmap
, pmap_offset
);
2459 pmap_offset
+= length
;
2461 pmap_flush(&pmap_flush_context_storage
);
2464 if (!object
->all_reusable
) {
2465 unsigned int reusable
;
2467 object
->all_reusable
= TRUE
;
2468 assert(object
->reusable_page_count
== 0);
2469 /* update global stats */
2470 reusable
= object
->resident_page_count
;
2471 OSAddAtomic(reusable
,
2472 &vm_page_stats_reusable
.reusable_count
);
2473 vm_page_stats_reusable
.reusable
+= reusable
;
2474 vm_page_stats_reusable
.all_reusable_calls
++;
2476 } else if (reusable_page
) {
2477 vm_page_stats_reusable
.partial_reusable_calls
++;
2482 vm_object_reuse_pages(
2484 vm_object_offset_t start_offset
,
2485 vm_object_offset_t end_offset
,
2486 boolean_t allow_partial_reuse
)
2488 vm_object_offset_t cur_offset
;
2490 unsigned int reused
, reusable
;
2492 #define VM_OBJECT_REUSE_PAGE(object, m, reused) \
2494 if ((m) != VM_PAGE_NULL && \
2496 assert((object)->reusable_page_count <= \
2497 (object)->resident_page_count); \
2498 assert((object)->reusable_page_count > 0); \
2499 (object)->reusable_page_count--; \
2500 (m)->reusable = FALSE; \
2503 * Tell pmap that this page is no longer \
2504 * "reusable", to update the "reusable" stats \
2505 * for all the pmaps that have mapped this \
2508 pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE((m)), \
2510 (PMAP_OPTIONS_CLEAR_REUSABLE \
2511 | PMAP_OPTIONS_NOFLUSH), \
2519 vm_object_lock_assert_exclusive(object
);
2521 if (object
->all_reusable
) {
2522 panic("object %p all_reusable: can't update pmap stats\n",
2524 assert(object
->reusable_page_count
== 0);
2525 object
->all_reusable
= FALSE
;
2526 if (end_offset
- start_offset
== object
->vo_size
||
2527 !allow_partial_reuse
) {
2528 vm_page_stats_reusable
.all_reuse_calls
++;
2529 reused
= object
->resident_page_count
;
2531 vm_page_stats_reusable
.partial_reuse_calls
++;
2532 vm_page_queue_iterate(&object
->memq
, m
, vm_page_t
, listq
) {
2533 if (m
->offset
< start_offset
||
2534 m
->offset
>= end_offset
) {
2536 object
->reusable_page_count
++;
2537 assert(object
->resident_page_count
>= object
->reusable_page_count
);
2540 assert(!m
->reusable
);
2545 } else if (object
->resident_page_count
>
2546 ((end_offset
- start_offset
) >> PAGE_SHIFT
)) {
2547 vm_page_stats_reusable
.partial_reuse_calls
++;
2548 for (cur_offset
= start_offset
;
2549 cur_offset
< end_offset
;
2550 cur_offset
+= PAGE_SIZE_64
) {
2551 if (object
->reusable_page_count
== 0) {
2554 m
= vm_page_lookup(object
, cur_offset
);
2555 VM_OBJECT_REUSE_PAGE(object
, m
, reused
);
2558 vm_page_stats_reusable
.partial_reuse_calls
++;
2559 vm_page_queue_iterate(&object
->memq
, m
, vm_page_t
, listq
) {
2560 if (object
->reusable_page_count
== 0) {
2563 if (m
->offset
< start_offset
||
2564 m
->offset
>= end_offset
) {
2567 VM_OBJECT_REUSE_PAGE(object
, m
, reused
);
2571 /* update global stats */
2572 OSAddAtomic(reusable
-reused
, &vm_page_stats_reusable
.reusable_count
);
2573 vm_page_stats_reusable
.reused
+= reused
;
2574 vm_page_stats_reusable
.reusable
+= reusable
;
2578 * Routine: vm_object_pmap_protect
2581 * Reduces the permission for all physical
2582 * pages in the specified object range.
2584 * If removing write permission only, it is
2585 * sufficient to protect only the pages in
2586 * the top-level object; only those pages may
2587 * have write permission.
2589 * If removing all access, we must follow the
2590 * shadow chain from the top-level object to
2591 * remove access to all pages in shadowed objects.
2593 * The object must *not* be locked. The object must
2596 * If pmap is not NULL, this routine assumes that
2597 * the only mappings for the pages are in that
2601 __private_extern__
void
2602 vm_object_pmap_protect(
2604 vm_object_offset_t offset
,
2605 vm_object_size_t size
,
2607 vm_map_offset_t pmap_start
,
2610 vm_object_pmap_protect_options(object
, offset
, size
,
2611 pmap
, pmap_start
, prot
, 0);
2614 __private_extern__
void
2615 vm_object_pmap_protect_options(
2617 vm_object_offset_t offset
,
2618 vm_object_size_t size
,
2620 vm_map_offset_t pmap_start
,
2624 pmap_flush_context pmap_flush_context_storage
;
2625 boolean_t delayed_pmap_flush
= FALSE
;
2627 if (object
== VM_OBJECT_NULL
)
2629 size
= vm_object_round_page(size
);
2630 offset
= vm_object_trunc_page(offset
);
2632 vm_object_lock(object
);
2634 if (object
->phys_contiguous
) {
2636 vm_object_unlock(object
);
2637 pmap_protect_options(pmap
,
2641 options
& ~PMAP_OPTIONS_NOFLUSH
,
2644 vm_object_offset_t phys_start
, phys_end
, phys_addr
;
2646 phys_start
= object
->vo_shadow_offset
+ offset
;
2647 phys_end
= phys_start
+ size
;
2648 assert(phys_start
<= phys_end
);
2649 assert(phys_end
<= object
->vo_shadow_offset
+ object
->vo_size
);
2650 vm_object_unlock(object
);
2652 pmap_flush_context_init(&pmap_flush_context_storage
);
2653 delayed_pmap_flush
= FALSE
;
2655 for (phys_addr
= phys_start
;
2656 phys_addr
< phys_end
;
2657 phys_addr
+= PAGE_SIZE_64
) {
2658 pmap_page_protect_options(
2659 (ppnum_t
) (phys_addr
>> PAGE_SHIFT
),
2661 options
| PMAP_OPTIONS_NOFLUSH
,
2662 (void *)&pmap_flush_context_storage
);
2663 delayed_pmap_flush
= TRUE
;
2665 if (delayed_pmap_flush
== TRUE
)
2666 pmap_flush(&pmap_flush_context_storage
);
2671 assert(object
->internal
);
2674 if (ptoa_64(object
->resident_page_count
) > size
/2 && pmap
!= PMAP_NULL
) {
2675 vm_object_unlock(object
);
2676 pmap_protect_options(pmap
, pmap_start
, pmap_start
+ size
, prot
,
2677 options
& ~PMAP_OPTIONS_NOFLUSH
, NULL
);
2681 pmap_flush_context_init(&pmap_flush_context_storage
);
2682 delayed_pmap_flush
= FALSE
;
2685 * if we are doing large ranges with respect to resident
2686 * page count then we should interate over pages otherwise
2687 * inverse page look-up will be faster
2689 if (ptoa_64(object
->resident_page_count
/ 4) < size
) {
2691 vm_object_offset_t end
;
2693 end
= offset
+ size
;
2695 vm_page_queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
2696 if (!p
->fictitious
&& (offset
<= p
->offset
) && (p
->offset
< end
)) {
2697 vm_map_offset_t start
;
2699 start
= pmap_start
+ p
->offset
- offset
;
2701 if (pmap
!= PMAP_NULL
)
2702 pmap_protect_options(
2705 start
+ PAGE_SIZE_64
,
2707 options
| PMAP_OPTIONS_NOFLUSH
,
2708 &pmap_flush_context_storage
);
2710 pmap_page_protect_options(
2711 VM_PAGE_GET_PHYS_PAGE(p
),
2713 options
| PMAP_OPTIONS_NOFLUSH
,
2714 &pmap_flush_context_storage
);
2715 delayed_pmap_flush
= TRUE
;
2721 vm_object_offset_t end
;
2722 vm_object_offset_t target_off
;
2724 end
= offset
+ size
;
2726 for (target_off
= offset
;
2727 target_off
< end
; target_off
+= PAGE_SIZE
) {
2729 p
= vm_page_lookup(object
, target_off
);
2731 if (p
!= VM_PAGE_NULL
) {
2732 vm_object_offset_t start
;
2734 start
= pmap_start
+ (p
->offset
- offset
);
2736 if (pmap
!= PMAP_NULL
)
2737 pmap_protect_options(
2740 start
+ PAGE_SIZE_64
,
2742 options
| PMAP_OPTIONS_NOFLUSH
,
2743 &pmap_flush_context_storage
);
2745 pmap_page_protect_options(
2746 VM_PAGE_GET_PHYS_PAGE(p
),
2748 options
| PMAP_OPTIONS_NOFLUSH
,
2749 &pmap_flush_context_storage
);
2750 delayed_pmap_flush
= TRUE
;
2754 if (delayed_pmap_flush
== TRUE
)
2755 pmap_flush(&pmap_flush_context_storage
);
2757 if (prot
== VM_PROT_NONE
) {
2759 * Must follow shadow chain to remove access
2760 * to pages in shadowed objects.
2762 vm_object_t next_object
;
2764 next_object
= object
->shadow
;
2765 if (next_object
!= VM_OBJECT_NULL
) {
2766 offset
+= object
->vo_shadow_offset
;
2767 vm_object_lock(next_object
);
2768 vm_object_unlock(object
);
2769 object
= next_object
;
2773 * End of chain - we are done.
2780 * Pages in shadowed objects may never have
2781 * write permission - we may stop here.
2787 vm_object_unlock(object
);
2791 * Routine: vm_object_copy_slowly
2794 * Copy the specified range of the source
2795 * virtual memory object without using
2796 * protection-based optimizations (such
2797 * as copy-on-write). The pages in the
2798 * region are actually copied.
2800 * In/out conditions:
2801 * The caller must hold a reference and a lock
2802 * for the source virtual memory object. The source
2803 * object will be returned *unlocked*.
2806 * If the copy is completed successfully, KERN_SUCCESS is
2807 * returned. If the caller asserted the interruptible
2808 * argument, and an interruption occurred while waiting
2809 * for a user-generated event, MACH_SEND_INTERRUPTED is
2810 * returned. Other values may be returned to indicate
2811 * hard errors during the copy operation.
2813 * A new virtual memory object is returned in a
2814 * parameter (_result_object). The contents of this
2815 * new object, starting at a zero offset, are a copy
2816 * of the source memory region. In the event of
2817 * an error, this parameter will contain the value
2820 __private_extern__ kern_return_t
2821 vm_object_copy_slowly(
2822 vm_object_t src_object
,
2823 vm_object_offset_t src_offset
,
2824 vm_object_size_t size
,
2825 boolean_t interruptible
,
2826 vm_object_t
*_result_object
) /* OUT */
2828 vm_object_t new_object
;
2829 vm_object_offset_t new_offset
;
2831 struct vm_object_fault_info fault_info
;
2833 XPR(XPR_VM_OBJECT
, "v_o_c_slowly obj 0x%x off 0x%x size 0x%x\n",
2834 src_object
, src_offset
, size
, 0, 0);
2837 vm_object_unlock(src_object
);
2838 *_result_object
= VM_OBJECT_NULL
;
2839 return(KERN_INVALID_ARGUMENT
);
2843 * Prevent destruction of the source object while we copy.
2846 vm_object_reference_locked(src_object
);
2847 vm_object_unlock(src_object
);
2850 * Create a new object to hold the copied pages.
2852 * We fill the new object starting at offset 0,
2853 * regardless of the input offset.
2854 * We don't bother to lock the new object within
2855 * this routine, since we have the only reference.
2858 new_object
= vm_object_allocate(size
);
2861 assert(size
== trunc_page_64(size
)); /* Will the loop terminate? */
2863 fault_info
.interruptible
= interruptible
;
2864 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
2865 fault_info
.user_tag
= 0;
2866 fault_info
.pmap_options
= 0;
2867 fault_info
.lo_offset
= src_offset
;
2868 fault_info
.hi_offset
= src_offset
+ size
;
2869 fault_info
.no_cache
= FALSE
;
2870 fault_info
.stealth
= TRUE
;
2871 fault_info
.io_sync
= FALSE
;
2872 fault_info
.cs_bypass
= FALSE
;
2873 fault_info
.mark_zf_absent
= FALSE
;
2874 fault_info
.batch_pmap_op
= FALSE
;
2878 src_offset
+= PAGE_SIZE_64
,
2879 new_offset
+= PAGE_SIZE_64
, size
-= PAGE_SIZE_64
2882 vm_fault_return_t result
;
2884 vm_object_lock(new_object
);
2886 while ((new_page
= vm_page_alloc(new_object
, new_offset
))
2889 vm_object_unlock(new_object
);
2891 if (!vm_page_wait(interruptible
)) {
2892 vm_object_deallocate(new_object
);
2893 vm_object_deallocate(src_object
);
2894 *_result_object
= VM_OBJECT_NULL
;
2895 return(MACH_SEND_INTERRUPTED
);
2897 vm_object_lock(new_object
);
2899 vm_object_unlock(new_object
);
2902 vm_prot_t prot
= VM_PROT_READ
;
2903 vm_page_t _result_page
;
2905 vm_page_t result_page
;
2906 kern_return_t error_code
;
2907 vm_object_t result_page_object
;
2910 vm_object_lock(src_object
);
2912 if (src_object
->internal
&&
2913 src_object
->shadow
== VM_OBJECT_NULL
&&
2914 (vm_page_lookup(src_object
,
2915 src_offset
) == VM_PAGE_NULL
) &&
2916 (src_object
->pager
== NULL
||
2917 (VM_COMPRESSOR_PAGER_STATE_GET(src_object
,
2919 VM_EXTERNAL_STATE_ABSENT
))) {
2921 * This page is neither resident nor compressed
2922 * and there's no shadow object below
2923 * "src_object", so this page is really missing.
2924 * There's no need to zero-fill it just to copy
2925 * it: let's leave it missing in "new_object"
2926 * and get zero-filled on demand.
2928 vm_object_unlock(src_object
);
2929 /* free the unused "new_page"... */
2930 vm_object_lock(new_object
);
2931 VM_PAGE_FREE(new_page
);
2932 new_page
= VM_PAGE_NULL
;
2933 vm_object_unlock(new_object
);
2934 /* ...and go to next page in "src_object" */
2935 result
= VM_FAULT_SUCCESS
;
2939 vm_object_paging_begin(src_object
);
2941 if (size
> (vm_size_t
) -1) {
2942 /* 32-bit overflow */
2943 fault_info
.cluster_size
= (vm_size_t
) (0 - PAGE_SIZE
);
2945 fault_info
.cluster_size
= (vm_size_t
) size
;
2946 assert(fault_info
.cluster_size
== size
);
2949 XPR(XPR_VM_FAULT
,"vm_object_copy_slowly -> vm_fault_page",0,0,0,0,0);
2950 _result_page
= VM_PAGE_NULL
;
2951 result
= vm_fault_page(src_object
, src_offset
,
2952 VM_PROT_READ
, FALSE
,
2953 FALSE
, /* page not looked up */
2954 &prot
, &_result_page
, &top_page
,
2956 &error_code
, FALSE
, FALSE
, &fault_info
);
2959 case VM_FAULT_SUCCESS
:
2960 result_page
= _result_page
;
2961 result_page_object
= VM_PAGE_OBJECT(result_page
);
2964 * Copy the page to the new object.
2967 * If result_page is clean,
2968 * we could steal it instead
2972 vm_page_copy(result_page
, new_page
);
2973 vm_object_unlock(result_page_object
);
2976 * Let go of both pages (make them
2977 * not busy, perform wakeup, activate).
2979 vm_object_lock(new_object
);
2980 SET_PAGE_DIRTY(new_page
, FALSE
);
2981 PAGE_WAKEUP_DONE(new_page
);
2982 vm_object_unlock(new_object
);
2984 vm_object_lock(result_page_object
);
2985 PAGE_WAKEUP_DONE(result_page
);
2987 vm_page_lockspin_queues();
2988 if ((result_page
->vm_page_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) ||
2989 (result_page
->vm_page_q_state
== VM_PAGE_NOT_ON_Q
)) {
2990 vm_page_activate(result_page
);
2992 vm_page_activate(new_page
);
2993 vm_page_unlock_queues();
2996 * Release paging references and
2997 * top-level placeholder page, if any.
3000 vm_fault_cleanup(result_page_object
,
3005 case VM_FAULT_RETRY
:
3008 case VM_FAULT_MEMORY_SHORTAGE
:
3009 if (vm_page_wait(interruptible
))
3013 case VM_FAULT_INTERRUPTED
:
3014 vm_object_lock(new_object
);
3015 VM_PAGE_FREE(new_page
);
3016 vm_object_unlock(new_object
);
3018 vm_object_deallocate(new_object
);
3019 vm_object_deallocate(src_object
);
3020 *_result_object
= VM_OBJECT_NULL
;
3021 return(MACH_SEND_INTERRUPTED
);
3023 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
3024 /* success but no VM page: fail */
3025 vm_object_paging_end(src_object
);
3026 vm_object_unlock(src_object
);
3028 case VM_FAULT_MEMORY_ERROR
:
3031 * (a) ignore pages that we can't
3033 * (b) return the null object if
3034 * any page fails [chosen]
3037 vm_object_lock(new_object
);
3038 VM_PAGE_FREE(new_page
);
3039 vm_object_unlock(new_object
);
3041 vm_object_deallocate(new_object
);
3042 vm_object_deallocate(src_object
);
3043 *_result_object
= VM_OBJECT_NULL
;
3044 return(error_code
? error_code
:
3048 panic("vm_object_copy_slowly: unexpected error"
3049 " 0x%x from vm_fault_page()\n", result
);
3051 } while (result
!= VM_FAULT_SUCCESS
);
3055 * Lose the extra reference, and return our object.
3057 vm_object_deallocate(src_object
);
3058 *_result_object
= new_object
;
3059 return(KERN_SUCCESS
);
3063 * Routine: vm_object_copy_quickly
3066 * Copy the specified range of the source virtual
3067 * memory object, if it can be done without waiting
3068 * for user-generated events.
3071 * If the copy is successful, the copy is returned in
3072 * the arguments; otherwise, the arguments are not
3075 * In/out conditions:
3076 * The object should be unlocked on entry and exit.
3080 __private_extern__ boolean_t
3081 vm_object_copy_quickly(
3082 vm_object_t
*_object
, /* INOUT */
3083 __unused vm_object_offset_t offset
, /* IN */
3084 __unused vm_object_size_t size
, /* IN */
3085 boolean_t
*_src_needs_copy
, /* OUT */
3086 boolean_t
*_dst_needs_copy
) /* OUT */
3088 vm_object_t object
= *_object
;
3089 memory_object_copy_strategy_t copy_strategy
;
3091 XPR(XPR_VM_OBJECT
, "v_o_c_quickly obj 0x%x off 0x%x size 0x%x\n",
3092 *_object
, offset
, size
, 0, 0);
3093 if (object
== VM_OBJECT_NULL
) {
3094 *_src_needs_copy
= FALSE
;
3095 *_dst_needs_copy
= FALSE
;
3099 vm_object_lock(object
);
3101 copy_strategy
= object
->copy_strategy
;
3103 switch (copy_strategy
) {
3104 case MEMORY_OBJECT_COPY_SYMMETRIC
:
3107 * Symmetric copy strategy.
3108 * Make another reference to the object.
3109 * Leave object/offset unchanged.
3112 vm_object_reference_locked(object
);
3113 object
->shadowed
= TRUE
;
3114 vm_object_unlock(object
);
3117 * Both source and destination must make
3118 * shadows, and the source must be made
3119 * read-only if not already.
3122 *_src_needs_copy
= TRUE
;
3123 *_dst_needs_copy
= TRUE
;
3127 case MEMORY_OBJECT_COPY_DELAY
:
3128 vm_object_unlock(object
);
3132 vm_object_unlock(object
);
3138 static int copy_call_count
= 0;
3139 static int copy_call_sleep_count
= 0;
3140 static int copy_call_restart_count
= 0;
3143 * Routine: vm_object_copy_call [internal]
3146 * Copy the source object (src_object), using the
3147 * user-managed copy algorithm.
3149 * In/out conditions:
3150 * The source object must be locked on entry. It
3151 * will be *unlocked* on exit.
3154 * If the copy is successful, KERN_SUCCESS is returned.
3155 * A new object that represents the copied virtual
3156 * memory is returned in a parameter (*_result_object).
3157 * If the return value indicates an error, this parameter
3160 static kern_return_t
3161 vm_object_copy_call(
3162 vm_object_t src_object
,
3163 vm_object_offset_t src_offset
,
3164 vm_object_size_t size
,
3165 vm_object_t
*_result_object
) /* OUT */
3169 boolean_t check_ready
= FALSE
;
3170 uint32_t try_failed_count
= 0;
3173 * If a copy is already in progress, wait and retry.
3176 * Consider making this call interruptable, as Mike
3177 * intended it to be.
3180 * Need a counter or version or something to allow
3181 * us to use the copy that the currently requesting
3182 * thread is obtaining -- is it worth adding to the
3183 * vm object structure? Depends how common this case it.
3186 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
3187 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
3189 copy_call_restart_count
++;
3193 * Indicate (for the benefit of memory_object_create_copy)
3194 * that we want a copy for src_object. (Note that we cannot
3195 * do a real assert_wait before calling memory_object_copy,
3196 * so we simply set the flag.)
3199 vm_object_set_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
);
3200 vm_object_unlock(src_object
);
3203 * Ask the memory manager to give us a memory object
3204 * which represents a copy of the src object.
3205 * The memory manager may give us a memory object
3206 * which we already have, or it may give us a
3207 * new memory object. This memory object will arrive
3208 * via memory_object_create_copy.
3211 kr
= KERN_FAILURE
; /* XXX need to change memory_object.defs */
3212 if (kr
!= KERN_SUCCESS
) {
3217 * Wait for the copy to arrive.
3219 vm_object_lock(src_object
);
3220 while (vm_object_wanted(src_object
, VM_OBJECT_EVENT_COPY_CALL
)) {
3221 vm_object_sleep(src_object
, VM_OBJECT_EVENT_COPY_CALL
,
3223 copy_call_sleep_count
++;
3226 assert(src_object
->copy
!= VM_OBJECT_NULL
);
3227 copy
= src_object
->copy
;
3228 if (!vm_object_lock_try(copy
)) {
3229 vm_object_unlock(src_object
);
3232 mutex_pause(try_failed_count
); /* wait a bit */
3234 vm_object_lock(src_object
);
3237 if (copy
->vo_size
< src_offset
+size
)
3238 copy
->vo_size
= src_offset
+size
;
3240 if (!copy
->pager_ready
)
3246 *_result_object
= copy
;
3247 vm_object_unlock(copy
);
3248 vm_object_unlock(src_object
);
3250 /* Wait for the copy to be ready. */
3251 if (check_ready
== TRUE
) {
3252 vm_object_lock(copy
);
3253 while (!copy
->pager_ready
) {
3254 vm_object_sleep(copy
, VM_OBJECT_EVENT_PAGER_READY
, THREAD_UNINT
);
3256 vm_object_unlock(copy
);
3259 return KERN_SUCCESS
;
3262 static int copy_delayed_lock_collisions
= 0;
3263 static int copy_delayed_max_collisions
= 0;
3264 static int copy_delayed_lock_contention
= 0;
3265 static int copy_delayed_protect_iterate
= 0;
3268 * Routine: vm_object_copy_delayed [internal]
3271 * Copy the specified virtual memory object, using
3272 * the asymmetric copy-on-write algorithm.
3274 * In/out conditions:
3275 * The src_object must be locked on entry. It will be unlocked
3276 * on exit - so the caller must also hold a reference to it.
3278 * This routine will not block waiting for user-generated
3279 * events. It is not interruptible.
3281 __private_extern__ vm_object_t
3282 vm_object_copy_delayed(
3283 vm_object_t src_object
,
3284 vm_object_offset_t src_offset
,
3285 vm_object_size_t size
,
3286 boolean_t src_object_shared
)
3288 vm_object_t new_copy
= VM_OBJECT_NULL
;
3289 vm_object_t old_copy
;
3291 vm_object_size_t copy_size
= src_offset
+ size
;
3292 pmap_flush_context pmap_flush_context_storage
;
3293 boolean_t delayed_pmap_flush
= FALSE
;
3298 * The user-level memory manager wants to see all of the changes
3299 * to this object, but it has promised not to make any changes on
3302 * Perform an asymmetric copy-on-write, as follows:
3303 * Create a new object, called a "copy object" to hold
3304 * pages modified by the new mapping (i.e., the copy,
3305 * not the original mapping).
3306 * Record the original object as the backing object for
3307 * the copy object. If the original mapping does not
3308 * change a page, it may be used read-only by the copy.
3309 * Record the copy object in the original object.
3310 * When the original mapping causes a page to be modified,
3311 * it must be copied to a new page that is "pushed" to
3313 * Mark the new mapping (the copy object) copy-on-write.
3314 * This makes the copy object itself read-only, allowing
3315 * it to be reused if the original mapping makes no
3316 * changes, and simplifying the synchronization required
3317 * in the "push" operation described above.
3319 * The copy-on-write is said to be assymetric because the original
3320 * object is *not* marked copy-on-write. A copied page is pushed
3321 * to the copy object, regardless which party attempted to modify
3324 * Repeated asymmetric copy operations may be done. If the
3325 * original object has not been changed since the last copy, its
3326 * copy object can be reused. Otherwise, a new copy object can be
3327 * inserted between the original object and its previous copy
3328 * object. Since any copy object is read-only, this cannot affect
3329 * affect the contents of the previous copy object.
3331 * Note that a copy object is higher in the object tree than the
3332 * original object; therefore, use of the copy object recorded in
3333 * the original object must be done carefully, to avoid deadlock.
3336 copy_size
= vm_object_round_page(copy_size
);
3340 * Wait for paging in progress.
3342 if (!src_object
->true_share
&&
3343 (src_object
->paging_in_progress
!= 0 ||
3344 src_object
->activity_in_progress
!= 0)) {
3345 if (src_object_shared
== TRUE
) {
3346 vm_object_unlock(src_object
);
3347 vm_object_lock(src_object
);
3348 src_object_shared
= FALSE
;
3351 vm_object_paging_wait(src_object
, THREAD_UNINT
);
3354 * See whether we can reuse the result of a previous
3358 old_copy
= src_object
->copy
;
3359 if (old_copy
!= VM_OBJECT_NULL
) {
3363 * Try to get the locks (out of order)
3365 if (src_object_shared
== TRUE
)
3366 lock_granted
= vm_object_lock_try_shared(old_copy
);
3368 lock_granted
= vm_object_lock_try(old_copy
);
3370 if (!lock_granted
) {
3371 vm_object_unlock(src_object
);
3373 if (collisions
++ == 0)
3374 copy_delayed_lock_contention
++;
3375 mutex_pause(collisions
);
3377 /* Heisenberg Rules */
3378 copy_delayed_lock_collisions
++;
3380 if (collisions
> copy_delayed_max_collisions
)
3381 copy_delayed_max_collisions
= collisions
;
3383 if (src_object_shared
== TRUE
)
3384 vm_object_lock_shared(src_object
);
3386 vm_object_lock(src_object
);
3392 * Determine whether the old copy object has
3396 if (old_copy
->resident_page_count
== 0 &&
3397 !old_copy
->pager_created
) {
3399 * It has not been modified.
3401 * Return another reference to
3402 * the existing copy-object if
3403 * we can safely grow it (if
3407 if (old_copy
->vo_size
< copy_size
) {
3408 if (src_object_shared
== TRUE
) {
3409 vm_object_unlock(old_copy
);
3410 vm_object_unlock(src_object
);
3412 vm_object_lock(src_object
);
3413 src_object_shared
= FALSE
;
3417 * We can't perform a delayed copy if any of the
3418 * pages in the extended range are wired (because
3419 * we can't safely take write permission away from
3420 * wired pages). If the pages aren't wired, then
3421 * go ahead and protect them.
3423 copy_delayed_protect_iterate
++;
3425 pmap_flush_context_init(&pmap_flush_context_storage
);
3426 delayed_pmap_flush
= FALSE
;
3428 vm_page_queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
3429 if (!p
->fictitious
&&
3430 p
->offset
>= old_copy
->vo_size
&&
3431 p
->offset
< copy_size
) {
3432 if (VM_PAGE_WIRED(p
)) {
3433 vm_object_unlock(old_copy
);
3434 vm_object_unlock(src_object
);
3436 if (new_copy
!= VM_OBJECT_NULL
) {
3437 vm_object_unlock(new_copy
);
3438 vm_object_deallocate(new_copy
);
3440 if (delayed_pmap_flush
== TRUE
)
3441 pmap_flush(&pmap_flush_context_storage
);
3443 return VM_OBJECT_NULL
;
3445 pmap_page_protect_options(VM_PAGE_GET_PHYS_PAGE(p
), (VM_PROT_ALL
& ~VM_PROT_WRITE
),
3446 PMAP_OPTIONS_NOFLUSH
, (void *)&pmap_flush_context_storage
);
3447 delayed_pmap_flush
= TRUE
;
3451 if (delayed_pmap_flush
== TRUE
)
3452 pmap_flush(&pmap_flush_context_storage
);
3454 old_copy
->vo_size
= copy_size
;
3456 if (src_object_shared
== TRUE
)
3457 vm_object_reference_shared(old_copy
);
3459 vm_object_reference_locked(old_copy
);
3460 vm_object_unlock(old_copy
);
3461 vm_object_unlock(src_object
);
3463 if (new_copy
!= VM_OBJECT_NULL
) {
3464 vm_object_unlock(new_copy
);
3465 vm_object_deallocate(new_copy
);
3473 * Adjust the size argument so that the newly-created
3474 * copy object will be large enough to back either the
3475 * old copy object or the new mapping.
3477 if (old_copy
->vo_size
> copy_size
)
3478 copy_size
= old_copy
->vo_size
;
3480 if (new_copy
== VM_OBJECT_NULL
) {
3481 vm_object_unlock(old_copy
);
3482 vm_object_unlock(src_object
);
3483 new_copy
= vm_object_allocate(copy_size
);
3484 vm_object_lock(src_object
);
3485 vm_object_lock(new_copy
);
3487 src_object_shared
= FALSE
;
3490 new_copy
->vo_size
= copy_size
;
3493 * The copy-object is always made large enough to
3494 * completely shadow the original object, since
3495 * it may have several users who want to shadow
3496 * the original object at different points.
3499 assert((old_copy
->shadow
== src_object
) &&
3500 (old_copy
->vo_shadow_offset
== (vm_object_offset_t
) 0));
3502 } else if (new_copy
== VM_OBJECT_NULL
) {
3503 vm_object_unlock(src_object
);
3504 new_copy
= vm_object_allocate(copy_size
);
3505 vm_object_lock(src_object
);
3506 vm_object_lock(new_copy
);
3508 src_object_shared
= FALSE
;
3513 * We now have the src object locked, and the new copy object
3514 * allocated and locked (and potentially the old copy locked).
3515 * Before we go any further, make sure we can still perform
3516 * a delayed copy, as the situation may have changed.
3518 * Specifically, we can't perform a delayed copy if any of the
3519 * pages in the range are wired (because we can't safely take
3520 * write permission away from wired pages). If the pages aren't
3521 * wired, then go ahead and protect them.
3523 copy_delayed_protect_iterate
++;
3525 pmap_flush_context_init(&pmap_flush_context_storage
);
3526 delayed_pmap_flush
= FALSE
;
3528 vm_page_queue_iterate(&src_object
->memq
, p
, vm_page_t
, listq
) {
3529 if (!p
->fictitious
&& p
->offset
< copy_size
) {
3530 if (VM_PAGE_WIRED(p
)) {
3532 vm_object_unlock(old_copy
);
3533 vm_object_unlock(src_object
);
3534 vm_object_unlock(new_copy
);
3535 vm_object_deallocate(new_copy
);
3537 if (delayed_pmap_flush
== TRUE
)
3538 pmap_flush(&pmap_flush_context_storage
);
3540 return VM_OBJECT_NULL
;
3542 pmap_page_protect_options(VM_PAGE_GET_PHYS_PAGE(p
), (VM_PROT_ALL
& ~VM_PROT_WRITE
),
3543 PMAP_OPTIONS_NOFLUSH
, (void *)&pmap_flush_context_storage
);
3544 delayed_pmap_flush
= TRUE
;
3548 if (delayed_pmap_flush
== TRUE
)
3549 pmap_flush(&pmap_flush_context_storage
);
3551 if (old_copy
!= VM_OBJECT_NULL
) {
3553 * Make the old copy-object shadow the new one.
3554 * It will receive no more pages from the original
3558 /* remove ref. from old_copy */
3559 vm_object_lock_assert_exclusive(src_object
);
3560 src_object
->ref_count
--;
3561 assert(src_object
->ref_count
> 0);
3562 vm_object_lock_assert_exclusive(old_copy
);
3563 old_copy
->shadow
= new_copy
;
3564 vm_object_lock_assert_exclusive(new_copy
);
3565 assert(new_copy
->ref_count
> 0);
3566 new_copy
->ref_count
++; /* for old_copy->shadow ref. */
3569 if (old_copy
->res_count
) {
3570 VM_OBJ_RES_INCR(new_copy
);
3571 VM_OBJ_RES_DECR(src_object
);
3575 vm_object_unlock(old_copy
); /* done with old_copy */
3579 * Point the new copy at the existing object.
3581 vm_object_lock_assert_exclusive(new_copy
);
3582 new_copy
->shadow
= src_object
;
3583 new_copy
->vo_shadow_offset
= 0;
3584 new_copy
->shadowed
= TRUE
; /* caller must set needs_copy */
3586 vm_object_lock_assert_exclusive(src_object
);
3587 vm_object_reference_locked(src_object
);
3588 src_object
->copy
= new_copy
;
3589 vm_object_unlock(src_object
);
3590 vm_object_unlock(new_copy
);
3593 "vm_object_copy_delayed: used copy object %X for source %X\n",
3594 new_copy
, src_object
, 0, 0, 0);
3600 * Routine: vm_object_copy_strategically
3603 * Perform a copy according to the source object's
3604 * declared strategy. This operation may block,
3605 * and may be interrupted.
3607 __private_extern__ kern_return_t
3608 vm_object_copy_strategically(
3609 vm_object_t src_object
,
3610 vm_object_offset_t src_offset
,
3611 vm_object_size_t size
,
3612 vm_object_t
*dst_object
, /* OUT */
3613 vm_object_offset_t
*dst_offset
, /* OUT */
3614 boolean_t
*dst_needs_copy
) /* OUT */
3617 boolean_t interruptible
= THREAD_ABORTSAFE
; /* XXX */
3618 boolean_t object_lock_shared
= FALSE
;
3619 memory_object_copy_strategy_t copy_strategy
;
3621 assert(src_object
!= VM_OBJECT_NULL
);
3623 copy_strategy
= src_object
->copy_strategy
;
3625 if (copy_strategy
== MEMORY_OBJECT_COPY_DELAY
) {
3626 vm_object_lock_shared(src_object
);
3627 object_lock_shared
= TRUE
;
3629 vm_object_lock(src_object
);
3632 * The copy strategy is only valid if the memory manager
3633 * is "ready". Internal objects are always ready.
3636 while (!src_object
->internal
&& !src_object
->pager_ready
) {
3637 wait_result_t wait_result
;
3639 if (object_lock_shared
== TRUE
) {
3640 vm_object_unlock(src_object
);
3641 vm_object_lock(src_object
);
3642 object_lock_shared
= FALSE
;
3645 wait_result
= vm_object_sleep( src_object
,
3646 VM_OBJECT_EVENT_PAGER_READY
,
3648 if (wait_result
!= THREAD_AWAKENED
) {
3649 vm_object_unlock(src_object
);
3650 *dst_object
= VM_OBJECT_NULL
;
3652 *dst_needs_copy
= FALSE
;
3653 return(MACH_SEND_INTERRUPTED
);
3658 * Use the appropriate copy strategy.
3661 switch (copy_strategy
) {
3662 case MEMORY_OBJECT_COPY_DELAY
:
3663 *dst_object
= vm_object_copy_delayed(src_object
,
3664 src_offset
, size
, object_lock_shared
);
3665 if (*dst_object
!= VM_OBJECT_NULL
) {
3666 *dst_offset
= src_offset
;
3667 *dst_needs_copy
= TRUE
;
3668 result
= KERN_SUCCESS
;
3671 vm_object_lock(src_object
);
3672 /* fall thru when delayed copy not allowed */
3674 case MEMORY_OBJECT_COPY_NONE
:
3675 result
= vm_object_copy_slowly(src_object
, src_offset
, size
,
3676 interruptible
, dst_object
);
3677 if (result
== KERN_SUCCESS
) {
3679 *dst_needs_copy
= FALSE
;
3683 case MEMORY_OBJECT_COPY_CALL
:
3684 result
= vm_object_copy_call(src_object
, src_offset
, size
,
3686 if (result
== KERN_SUCCESS
) {
3687 *dst_offset
= src_offset
;
3688 *dst_needs_copy
= TRUE
;
3692 case MEMORY_OBJECT_COPY_SYMMETRIC
:
3693 XPR(XPR_VM_OBJECT
, "v_o_c_strategically obj 0x%x off 0x%x size 0x%x\n", src_object
, src_offset
, size
, 0, 0);
3694 vm_object_unlock(src_object
);
3695 result
= KERN_MEMORY_RESTART_COPY
;
3699 panic("copy_strategically: bad strategy");
3700 result
= KERN_INVALID_ARGUMENT
;
3708 * Create a new object which is backed by the
3709 * specified existing object range. The source
3710 * object reference is deallocated.
3712 * The new object and offset into that object
3713 * are returned in the source parameters.
3715 boolean_t vm_object_shadow_check
= TRUE
;
3717 __private_extern__ boolean_t
3719 vm_object_t
*object
, /* IN/OUT */
3720 vm_object_offset_t
*offset
, /* IN/OUT */
3721 vm_object_size_t length
)
3727 assert(source
!= VM_OBJECT_NULL
);
3728 if (source
== VM_OBJECT_NULL
)
3734 * This assertion is valid but it gets triggered by Rosetta for example
3735 * due to a combination of vm_remap() that changes a VM object's
3736 * copy_strategy from SYMMETRIC to DELAY and vm_protect(VM_PROT_COPY)
3737 * that then sets "needs_copy" on its map entry. This creates a
3738 * mapping situation that VM should never see and doesn't know how to
3740 * It's not clear if this can create any real problem but we should
3741 * look into fixing this, probably by having vm_protect(VM_PROT_COPY)
3742 * do more than just set "needs_copy" to handle the copy-on-write...
3743 * In the meantime, let's disable the assertion.
3745 assert(source
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
);
3749 * Determine if we really need a shadow.
3751 * If the source object is larger than what we are trying
3752 * to create, then force the shadow creation even if the
3753 * ref count is 1. This will allow us to [potentially]
3754 * collapse the underlying object away in the future
3755 * (freeing up the extra data it might contain and that
3759 assert(source
->copy_strategy
!= MEMORY_OBJECT_COPY_NONE
); /* Purgeable objects shouldn't have shadow objects. */
3761 if (vm_object_shadow_check
&&
3762 source
->vo_size
== length
&&
3763 source
->ref_count
== 1 &&
3764 (source
->shadow
== VM_OBJECT_NULL
||
3765 source
->shadow
->copy
== VM_OBJECT_NULL
) )
3767 /* lock the object and check again */
3768 vm_object_lock(source
);
3769 if (source
->vo_size
== length
&&
3770 source
->ref_count
== 1 &&
3771 (source
->shadow
== VM_OBJECT_NULL
||
3772 source
->shadow
->copy
== VM_OBJECT_NULL
))
3774 source
->shadowed
= FALSE
;
3775 vm_object_unlock(source
);
3778 /* things changed while we were locking "source"... */
3779 vm_object_unlock(source
);
3783 * Allocate a new object with the given length
3786 if ((result
= vm_object_allocate(length
)) == VM_OBJECT_NULL
)
3787 panic("vm_object_shadow: no object for shadowing");
3790 * The new object shadows the source object, adding
3791 * a reference to it. Our caller changes his reference
3792 * to point to the new object, removing a reference to
3793 * the source object. Net result: no change of reference
3796 result
->shadow
= source
;
3799 * Store the offset into the source object,
3800 * and fix up the offset into the new object.
3803 result
->vo_shadow_offset
= *offset
;
3806 * Return the new things
3815 * The relationship between vm_object structures and
3816 * the memory_object requires careful synchronization.
3818 * All associations are created by memory_object_create_named
3819 * for external pagers and vm_object_compressor_pager_create for internal
3820 * objects as follows:
3822 * pager: the memory_object itself, supplied by
3823 * the user requesting a mapping (or the kernel,
3824 * when initializing internal objects); the
3825 * kernel simulates holding send rights by keeping
3829 * the memory object control port,
3830 * created by the kernel; the kernel holds
3831 * receive (and ownership) rights to this
3832 * port, but no other references.
3834 * When initialization is complete, the "initialized" field
3835 * is asserted. Other mappings using a particular memory object,
3836 * and any references to the vm_object gained through the
3837 * port association must wait for this initialization to occur.
3839 * In order to allow the memory manager to set attributes before
3840 * requests (notably virtual copy operations, but also data or
3841 * unlock requests) are made, a "ready" attribute is made available.
3842 * Only the memory manager may affect the value of this attribute.
3843 * Its value does not affect critical kernel functions, such as
3844 * internal object initialization or destruction. [Furthermore,
3845 * memory objects created by the kernel are assumed to be ready
3846 * immediately; the default memory manager need not explicitly
3847 * set the "ready" attribute.]
3849 * [Both the "initialized" and "ready" attribute wait conditions
3850 * use the "pager" field as the wait event.]
3852 * The port associations can be broken down by any of the
3853 * following routines:
3854 * vm_object_terminate:
3855 * No references to the vm_object remain, and
3856 * the object cannot (or will not) be cached.
3857 * This is the normal case, and is done even
3858 * though one of the other cases has already been
3860 * memory_object_destroy:
3861 * The memory manager has requested that the
3862 * kernel relinquish references to the memory
3863 * object. [The memory manager may not want to
3864 * destroy the memory object, but may wish to
3865 * refuse or tear down existing memory mappings.]
3867 * Each routine that breaks an association must break all of
3868 * them at once. At some later time, that routine must clear
3869 * the pager field and release the memory object references.
3870 * [Furthermore, each routine must cope with the simultaneous
3871 * or previous operations of the others.]
3873 * Because the pager field may be cleared spontaneously, it
3874 * cannot be used to determine whether a memory object has
3875 * ever been associated with a particular vm_object. [This
3876 * knowledge is important to the shadow object mechanism.]
3877 * For this reason, an additional "created" attribute is
3880 * During various paging operations, the pager reference found in the
3881 * vm_object must be valid. To prevent this from being released,
3882 * (other than being removed, i.e., made null), routines may use
3883 * the vm_object_paging_begin/end routines [actually, macros].
3884 * The implementation uses the "paging_in_progress" and "wanted" fields.
3885 * [Operations that alter the validity of the pager values include the
3886 * termination routines and vm_object_collapse.]
3891 * Routine: vm_object_memory_object_associate
3893 * Associate a VM object to the given pager.
3894 * If a VM object is not provided, create one.
3895 * Initialize the pager.
3898 vm_object_memory_object_associate(
3899 memory_object_t pager
,
3901 vm_object_size_t size
,
3904 memory_object_control_t control
;
3906 assert(pager
!= MEMORY_OBJECT_NULL
);
3908 if (object
!= VM_OBJECT_NULL
) {
3909 assert(object
->internal
);
3910 assert(object
->pager_created
);
3911 assert(!object
->pager_initialized
);
3912 assert(!object
->pager_ready
);
3914 object
= vm_object_allocate(size
);
3915 assert(object
!= VM_OBJECT_NULL
);
3916 object
->internal
= FALSE
;
3917 object
->pager_trusted
= FALSE
;
3918 /* copy strategy invalid until set by memory manager */
3919 object
->copy_strategy
= MEMORY_OBJECT_COPY_INVALID
;
3923 * Allocate request port.
3926 control
= memory_object_control_allocate(object
);
3927 assert (control
!= MEMORY_OBJECT_CONTROL_NULL
);
3929 vm_object_lock(object
);
3931 assert(!object
->pager_ready
);
3932 assert(!object
->pager_initialized
);
3933 assert(object
->pager
== NULL
);
3934 assert(object
->pager_control
== NULL
);
3937 * Copy the reference we were given.
3940 memory_object_reference(pager
);
3941 object
->pager_created
= TRUE
;
3942 object
->pager
= pager
;
3943 object
->pager_control
= control
;
3944 object
->pager_ready
= FALSE
;
3946 vm_object_unlock(object
);
3949 * Let the pager know we're using it.
3952 (void) memory_object_init(pager
,
3953 object
->pager_control
,
3956 vm_object_lock(object
);
3958 object
->named
= TRUE
;
3959 if (object
->internal
) {
3960 object
->pager_ready
= TRUE
;
3961 vm_object_wakeup(object
, VM_OBJECT_EVENT_PAGER_READY
);
3964 object
->pager_initialized
= TRUE
;
3965 vm_object_wakeup(object
, VM_OBJECT_EVENT_INITIALIZED
);
3967 vm_object_unlock(object
);
3973 * Routine: vm_object_compressor_pager_create
3975 * Create a memory object for an internal object.
3976 * In/out conditions:
3977 * The object is locked on entry and exit;
3978 * it may be unlocked within this call.
3980 * Only one thread may be performing a
3981 * vm_object_compressor_pager_create on an object at
3982 * a time. Presumably, only the pageout
3983 * daemon will be using this routine.
3987 vm_object_compressor_pager_create(
3990 memory_object_t pager
;
3991 vm_object_t pager_object
= VM_OBJECT_NULL
;
3993 assert(object
!= kernel_object
);
3996 * Prevent collapse or termination by holding a paging reference
3999 vm_object_paging_begin(object
);
4000 if (object
->pager_created
) {
4002 * Someone else got to it first...
4003 * wait for them to finish initializing the ports
4005 while (!object
->pager_initialized
) {
4006 vm_object_sleep(object
,
4007 VM_OBJECT_EVENT_INITIALIZED
,
4010 vm_object_paging_end(object
);
4014 if ((uint32_t) (object
->vo_size
/PAGE_SIZE
) !=
4015 (object
->vo_size
/PAGE_SIZE
)) {
4016 #if DEVELOPMENT || DEBUG
4017 printf("vm_object_compressor_pager_create(%p): "
4018 "object size 0x%llx >= 0x%llx\n",
4020 (uint64_t) object
->vo_size
,
4021 0x0FFFFFFFFULL
*PAGE_SIZE
);
4022 #endif /* DEVELOPMENT || DEBUG */
4023 vm_object_paging_end(object
);
4028 * Indicate that a memory object has been assigned
4029 * before dropping the lock, to prevent a race.
4032 object
->pager_created
= TRUE
;
4033 object
->paging_offset
= 0;
4035 vm_object_unlock(object
);
4038 * Create the [internal] pager, and associate it with this object.
4040 * We make the association here so that vm_object_enter()
4041 * can look up the object to complete initializing it. No
4042 * user will ever map this object.
4045 /* create our new memory object */
4046 assert((uint32_t) (object
->vo_size
/PAGE_SIZE
) ==
4047 (object
->vo_size
/PAGE_SIZE
));
4048 (void) compressor_memory_object_create(
4049 (memory_object_size_t
) object
->vo_size
,
4051 if (pager
== NULL
) {
4052 panic("vm_object_compressor_pager_create(): "
4053 "no pager for object %p size 0x%llx\n",
4054 object
, (uint64_t) object
->vo_size
);
4059 * A reference was returned by
4060 * memory_object_create(), and it is
4061 * copied by vm_object_memory_object_associate().
4064 pager_object
= vm_object_memory_object_associate(pager
,
4068 if (pager_object
!= object
) {
4069 panic("vm_object_compressor_pager_create: mismatch (pager: %p, pager_object: %p, orig_object: %p, orig_object size: 0x%llx)\n", pager
, pager_object
, object
, (uint64_t) object
->vo_size
);
4073 * Drop the reference we were passed.
4075 memory_object_deallocate(pager
);
4077 vm_object_lock(object
);
4080 * Release the paging reference
4082 vm_object_paging_end(object
);
4086 * Global variables for vm_object_collapse():
4088 * Counts for normal collapses and bypasses.
4089 * Debugging variables, to watch or disable collapse.
4091 static long object_collapses
= 0;
4092 static long object_bypasses
= 0;
4094 static boolean_t vm_object_collapse_allowed
= TRUE
;
4095 static boolean_t vm_object_bypass_allowed
= TRUE
;
4097 void vm_object_do_collapse_compressor(vm_object_t object
,
4098 vm_object_t backing_object
);
4100 vm_object_do_collapse_compressor(
4102 vm_object_t backing_object
)
4104 vm_object_offset_t new_offset
, backing_offset
;
4105 vm_object_size_t size
;
4107 vm_counters
.do_collapse_compressor
++;
4109 vm_object_lock_assert_exclusive(object
);
4110 vm_object_lock_assert_exclusive(backing_object
);
4112 size
= object
->vo_size
;
4115 * Move all compressed pages from backing_object
4119 for (backing_offset
= object
->vo_shadow_offset
;
4120 backing_offset
< object
->vo_shadow_offset
+ object
->vo_size
;
4121 backing_offset
+= PAGE_SIZE
) {
4122 memory_object_offset_t backing_pager_offset
;
4124 /* find the next compressed page at or after this offset */
4125 backing_pager_offset
= (backing_offset
+
4126 backing_object
->paging_offset
);
4127 backing_pager_offset
= vm_compressor_pager_next_compressed(
4128 backing_object
->pager
,
4129 backing_pager_offset
);
4130 if (backing_pager_offset
== (memory_object_offset_t
) -1) {
4131 /* no more compressed pages */
4134 backing_offset
= (backing_pager_offset
-
4135 backing_object
->paging_offset
);
4137 new_offset
= backing_offset
- object
->vo_shadow_offset
;
4139 if (new_offset
>= object
->vo_size
) {
4140 /* we're out of the scope of "object": done */
4144 if ((vm_page_lookup(object
, new_offset
) != VM_PAGE_NULL
) ||
4145 (vm_compressor_pager_state_get(object
->pager
,
4147 object
->paging_offset
)) ==
4148 VM_EXTERNAL_STATE_EXISTS
)) {
4150 * This page already exists in object, resident or
4152 * We don't need this compressed page in backing_object
4153 * and it will be reclaimed when we release
4160 * backing_object has this page in the VM compressor and
4161 * we need to transfer it to object.
4163 vm_counters
.do_collapse_compressor_pages
++;
4164 vm_compressor_pager_transfer(
4167 (new_offset
+ object
->paging_offset
),
4169 backing_object
->pager
,
4170 (backing_offset
+ backing_object
->paging_offset
));
4175 * Routine: vm_object_do_collapse
4177 * Collapse an object with the object backing it.
4178 * Pages in the backing object are moved into the
4179 * parent, and the backing object is deallocated.
4181 * Both objects and the cache are locked; the page
4182 * queues are unlocked.
4186 vm_object_do_collapse(
4188 vm_object_t backing_object
)
4191 vm_object_offset_t new_offset
, backing_offset
;
4192 vm_object_size_t size
;
4194 vm_object_lock_assert_exclusive(object
);
4195 vm_object_lock_assert_exclusive(backing_object
);
4197 assert(object
->purgable
== VM_PURGABLE_DENY
);
4198 assert(backing_object
->purgable
== VM_PURGABLE_DENY
);
4200 backing_offset
= object
->vo_shadow_offset
;
4201 size
= object
->vo_size
;
4204 * Move all in-memory pages from backing_object
4205 * to the parent. Pages that have been paged out
4206 * will be overwritten by any of the parent's
4207 * pages that shadow them.
4210 while (!vm_page_queue_empty(&backing_object
->memq
)) {
4212 p
= (vm_page_t
) vm_page_queue_first(&backing_object
->memq
);
4214 new_offset
= (p
->offset
- backing_offset
);
4216 assert(!p
->busy
|| p
->absent
);
4219 * If the parent has a page here, or if
4220 * this page falls outside the parent,
4223 * Otherwise, move it as planned.
4226 if (p
->offset
< backing_offset
|| new_offset
>= size
) {
4229 pp
= vm_page_lookup(object
, new_offset
);
4230 if (pp
== VM_PAGE_NULL
) {
4232 if (VM_COMPRESSOR_PAGER_STATE_GET(object
,
4234 == VM_EXTERNAL_STATE_EXISTS
) {
4236 * Parent object has this page
4237 * in the VM compressor.
4238 * Throw away the backing
4244 * Parent now has no page.
4245 * Move the backing object's page
4248 vm_page_rename(p
, object
, new_offset
);
4251 assert(! pp
->absent
);
4254 * Parent object has a real page.
4255 * Throw away the backing object's
4263 if (vm_object_collapse_compressor_allowed
&&
4264 object
->pager
!= MEMORY_OBJECT_NULL
&&
4265 backing_object
->pager
!= MEMORY_OBJECT_NULL
) {
4267 /* move compressed pages from backing_object to object */
4268 vm_object_do_collapse_compressor(object
, backing_object
);
4270 } else if (backing_object
->pager
!= MEMORY_OBJECT_NULL
) {
4272 assert((!object
->pager_created
&&
4273 (object
->pager
== MEMORY_OBJECT_NULL
)) ||
4274 (!backing_object
->pager_created
&&
4275 (backing_object
->pager
== MEMORY_OBJECT_NULL
)));
4277 * Move the pager from backing_object to object.
4279 * XXX We're only using part of the paging space
4280 * for keeps now... we ought to discard the
4284 assert(!object
->paging_in_progress
);
4285 assert(!object
->activity_in_progress
);
4286 assert(!object
->pager_created
);
4287 assert(object
->pager
== NULL
);
4288 object
->pager
= backing_object
->pager
;
4290 object
->pager_created
= backing_object
->pager_created
;
4291 object
->pager_control
= backing_object
->pager_control
;
4292 object
->pager_ready
= backing_object
->pager_ready
;
4293 object
->pager_initialized
= backing_object
->pager_initialized
;
4294 object
->paging_offset
=
4295 backing_object
->paging_offset
+ backing_offset
;
4296 if (object
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
4297 memory_object_control_collapse(object
->pager_control
,
4300 /* the backing_object has lost its pager: reset all fields */
4301 backing_object
->pager_created
= FALSE
;
4302 backing_object
->pager_control
= NULL
;
4303 backing_object
->pager_ready
= FALSE
;
4304 backing_object
->paging_offset
= 0;
4305 backing_object
->pager
= NULL
;
4308 * Object now shadows whatever backing_object did.
4309 * Note that the reference to backing_object->shadow
4310 * moves from within backing_object to within object.
4313 assert(!object
->phys_contiguous
);
4314 assert(!backing_object
->phys_contiguous
);
4315 object
->shadow
= backing_object
->shadow
;
4316 if (object
->shadow
) {
4317 object
->vo_shadow_offset
+= backing_object
->vo_shadow_offset
;
4318 /* "backing_object" gave its shadow to "object" */
4319 backing_object
->shadow
= VM_OBJECT_NULL
;
4320 backing_object
->vo_shadow_offset
= 0;
4322 /* no shadow, therefore no shadow offset... */
4323 object
->vo_shadow_offset
= 0;
4325 assert((object
->shadow
== VM_OBJECT_NULL
) ||
4326 (object
->shadow
->copy
!= backing_object
));
4329 * Discard backing_object.
4331 * Since the backing object has no pages, no
4332 * pager left, and no object references within it,
4333 * all that is necessary is to dispose of it.
4337 assert(backing_object
->ref_count
== 1);
4338 assert(backing_object
->resident_page_count
== 0);
4339 assert(backing_object
->paging_in_progress
== 0);
4340 assert(backing_object
->activity_in_progress
== 0);
4341 assert(backing_object
->shadow
== VM_OBJECT_NULL
);
4342 assert(backing_object
->vo_shadow_offset
== 0);
4344 if (backing_object
->pager
!= MEMORY_OBJECT_NULL
) {
4345 /* ... unless it has a pager; need to terminate pager too */
4346 vm_counters
.do_collapse_terminate
++;
4347 if (vm_object_terminate(backing_object
) != KERN_SUCCESS
) {
4348 vm_counters
.do_collapse_terminate_failure
++;
4353 assert(backing_object
->pager
== NULL
);
4355 backing_object
->alive
= FALSE
;
4356 vm_object_unlock(backing_object
);
4358 XPR(XPR_VM_OBJECT
, "vm_object_collapse, collapsed 0x%X\n",
4359 backing_object
, 0,0,0,0);
4361 #if VM_OBJECT_TRACKING
4362 if (vm_object_tracking_inited
) {
4363 btlog_remove_entries_for_element(vm_object_tracking_btlog
,
4366 #endif /* VM_OBJECT_TRACKING */
4368 vm_object_lock_destroy(backing_object
);
4370 zfree(vm_object_zone
, backing_object
);
4375 vm_object_do_bypass(
4377 vm_object_t backing_object
)
4380 * Make the parent shadow the next object
4384 vm_object_lock_assert_exclusive(object
);
4385 vm_object_lock_assert_exclusive(backing_object
);
4389 * Do object reference in-line to
4390 * conditionally increment shadow's
4391 * residence count. If object is not
4392 * resident, leave residence count
4395 if (backing_object
->shadow
!= VM_OBJECT_NULL
) {
4396 vm_object_lock(backing_object
->shadow
);
4397 vm_object_lock_assert_exclusive(backing_object
->shadow
);
4398 backing_object
->shadow
->ref_count
++;
4399 if (object
->res_count
!= 0)
4400 vm_object_res_reference(backing_object
->shadow
);
4401 vm_object_unlock(backing_object
->shadow
);
4403 #else /* TASK_SWAPPER */
4404 vm_object_reference(backing_object
->shadow
);
4405 #endif /* TASK_SWAPPER */
4407 assert(!object
->phys_contiguous
);
4408 assert(!backing_object
->phys_contiguous
);
4409 object
->shadow
= backing_object
->shadow
;
4410 if (object
->shadow
) {
4411 object
->vo_shadow_offset
+= backing_object
->vo_shadow_offset
;
4413 /* no shadow, therefore no shadow offset... */
4414 object
->vo_shadow_offset
= 0;
4418 * Backing object might have had a copy pointer
4419 * to us. If it did, clear it.
4421 if (backing_object
->copy
== object
) {
4422 backing_object
->copy
= VM_OBJECT_NULL
;
4426 * Drop the reference count on backing_object.
4428 * Since its ref_count was at least 2, it
4429 * will not vanish; so we don't need to call
4430 * vm_object_deallocate.
4431 * [with a caveat for "named" objects]
4433 * The res_count on the backing object is
4434 * conditionally decremented. It's possible
4435 * (via vm_pageout_scan) to get here with
4436 * a "swapped" object, which has a 0 res_count,
4437 * in which case, the backing object res_count
4438 * is already down by one.
4440 * Don't call vm_object_deallocate unless
4441 * ref_count drops to zero.
4443 * The ref_count can drop to zero here if the
4444 * backing object could be bypassed but not
4445 * collapsed, such as when the backing object
4446 * is temporary and cachable.
4449 if (backing_object
->ref_count
> 2 ||
4450 (!backing_object
->named
&& backing_object
->ref_count
> 1)) {
4451 vm_object_lock_assert_exclusive(backing_object
);
4452 backing_object
->ref_count
--;
4454 if (object
->res_count
!= 0)
4455 vm_object_res_deallocate(backing_object
);
4456 assert(backing_object
->ref_count
> 0);
4457 #endif /* TASK_SWAPPER */
4458 vm_object_unlock(backing_object
);
4462 * Drop locks so that we can deallocate
4463 * the backing object.
4467 if (object
->res_count
== 0) {
4468 /* XXX get a reference for the deallocate below */
4469 vm_object_res_reference(backing_object
);
4471 #endif /* TASK_SWAPPER */
4473 * vm_object_collapse (the caller of this function) is
4474 * now called from contexts that may not guarantee that a
4475 * valid reference is held on the object... w/o a valid
4476 * reference, it is unsafe and unwise (you will definitely
4477 * regret it) to unlock the object and then retake the lock
4478 * since the object may be terminated and recycled in between.
4479 * The "activity_in_progress" reference will keep the object
4482 vm_object_activity_begin(object
);
4483 vm_object_unlock(object
);
4485 vm_object_unlock(backing_object
);
4486 vm_object_deallocate(backing_object
);
4489 * Relock object. We don't have to reverify
4490 * its state since vm_object_collapse will
4491 * do that for us as it starts at the
4495 vm_object_lock(object
);
4496 vm_object_activity_end(object
);
4504 * vm_object_collapse:
4506 * Perform an object collapse or an object bypass if appropriate.
4507 * The real work of collapsing and bypassing is performed in
4508 * the routines vm_object_do_collapse and vm_object_do_bypass.
4510 * Requires that the object be locked and the page queues be unlocked.
4513 static unsigned long vm_object_collapse_calls
= 0;
4514 static unsigned long vm_object_collapse_objects
= 0;
4515 static unsigned long vm_object_collapse_do_collapse
= 0;
4516 static unsigned long vm_object_collapse_do_bypass
= 0;
4518 __private_extern__
void
4521 vm_object_offset_t hint_offset
,
4522 boolean_t can_bypass
)
4524 vm_object_t backing_object
;
4525 unsigned int rcount
;
4527 vm_object_t original_object
;
4528 int object_lock_type
;
4529 int backing_object_lock_type
;
4531 vm_object_collapse_calls
++;
4533 if (! vm_object_collapse_allowed
&&
4534 ! (can_bypass
&& vm_object_bypass_allowed
)) {
4538 XPR(XPR_VM_OBJECT
, "vm_object_collapse, obj 0x%X\n",
4541 if (object
== VM_OBJECT_NULL
)
4544 original_object
= object
;
4547 * The top object was locked "exclusive" by the caller.
4548 * In the first pass, to determine if we can collapse the shadow chain,
4549 * take a "shared" lock on the shadow objects. If we can collapse,
4550 * we'll have to go down the chain again with exclusive locks.
4552 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4553 backing_object_lock_type
= OBJECT_LOCK_SHARED
;
4556 object
= original_object
;
4557 vm_object_lock_assert_exclusive(object
);
4560 vm_object_collapse_objects
++;
4562 * Verify that the conditions are right for either
4563 * collapse or bypass:
4567 * There is a backing object, and
4570 backing_object
= object
->shadow
;
4571 if (backing_object
== VM_OBJECT_NULL
) {
4572 if (object
!= original_object
) {
4573 vm_object_unlock(object
);
4577 if (backing_object_lock_type
== OBJECT_LOCK_SHARED
) {
4578 vm_object_lock_shared(backing_object
);
4580 vm_object_lock(backing_object
);
4584 * No pages in the object are currently
4585 * being paged out, and
4587 if (object
->paging_in_progress
!= 0 ||
4588 object
->activity_in_progress
!= 0) {
4589 /* try and collapse the rest of the shadow chain */
4590 if (object
!= original_object
) {
4591 vm_object_unlock(object
);
4593 object
= backing_object
;
4594 object_lock_type
= backing_object_lock_type
;
4600 * The backing object is not read_only,
4601 * and no pages in the backing object are
4602 * currently being paged out.
4603 * The backing object is internal.
4607 if (!backing_object
->internal
||
4608 backing_object
->paging_in_progress
!= 0 ||
4609 backing_object
->activity_in_progress
!= 0) {
4610 /* try and collapse the rest of the shadow chain */
4611 if (object
!= original_object
) {
4612 vm_object_unlock(object
);
4614 object
= backing_object
;
4615 object_lock_type
= backing_object_lock_type
;
4620 * Purgeable objects are not supposed to engage in
4621 * copy-on-write activities, so should not have
4622 * any shadow objects or be a shadow object to another
4624 * Collapsing a purgeable object would require some
4625 * updates to the purgeable compressed ledgers.
4627 if (object
->purgable
!= VM_PURGABLE_DENY
||
4628 backing_object
->purgable
!= VM_PURGABLE_DENY
) {
4629 panic("vm_object_collapse() attempting to collapse "
4630 "purgeable object: %p(%d) %p(%d)\n",
4631 object
, object
->purgable
,
4632 backing_object
, backing_object
->purgable
);
4633 /* try and collapse the rest of the shadow chain */
4634 if (object
!= original_object
) {
4635 vm_object_unlock(object
);
4637 object
= backing_object
;
4638 object_lock_type
= backing_object_lock_type
;
4643 * The backing object can't be a copy-object:
4644 * the shadow_offset for the copy-object must stay
4645 * as 0. Furthermore (for the 'we have all the
4646 * pages' case), if we bypass backing_object and
4647 * just shadow the next object in the chain, old
4648 * pages from that object would then have to be copied
4649 * BOTH into the (former) backing_object and into the
4652 if (backing_object
->shadow
!= VM_OBJECT_NULL
&&
4653 backing_object
->shadow
->copy
== backing_object
) {
4654 /* try and collapse the rest of the shadow chain */
4655 if (object
!= original_object
) {
4656 vm_object_unlock(object
);
4658 object
= backing_object
;
4659 object_lock_type
= backing_object_lock_type
;
4664 * We can now try to either collapse the backing
4665 * object (if the parent is the only reference to
4666 * it) or (perhaps) remove the parent's reference
4669 * If there is exactly one reference to the backing
4670 * object, we may be able to collapse it into the
4673 * As long as one of the objects is still not known
4674 * to the pager, we can collapse them.
4676 if (backing_object
->ref_count
== 1 &&
4677 (vm_object_collapse_compressor_allowed
||
4678 !object
->pager_created
4679 || (!backing_object
->pager_created
)
4680 ) && vm_object_collapse_allowed
) {
4683 * We need the exclusive lock on the VM objects.
4685 if (backing_object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
4687 * We have an object and its shadow locked
4688 * "shared". We can't just upgrade the locks
4689 * to "exclusive", as some other thread might
4690 * also have these objects locked "shared" and
4691 * attempt to upgrade one or the other to
4692 * "exclusive". The upgrades would block
4693 * forever waiting for the other "shared" locks
4695 * So we have to release the locks and go
4696 * down the shadow chain again (since it could
4697 * have changed) with "exclusive" locking.
4699 vm_object_unlock(backing_object
);
4700 if (object
!= original_object
)
4701 vm_object_unlock(object
);
4702 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4703 backing_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4708 "vm_object_collapse: %x to %x, pager %x, pager_control %x\n",
4709 backing_object
, object
,
4710 backing_object
->pager
,
4711 backing_object
->pager_control
, 0);
4714 * Collapse the object with its backing
4715 * object, and try again with the object's
4716 * new backing object.
4719 vm_object_do_collapse(object
, backing_object
);
4720 vm_object_collapse_do_collapse
++;
4725 * Collapsing the backing object was not possible
4726 * or permitted, so let's try bypassing it.
4729 if (! (can_bypass
&& vm_object_bypass_allowed
)) {
4730 /* try and collapse the rest of the shadow chain */
4731 if (object
!= original_object
) {
4732 vm_object_unlock(object
);
4734 object
= backing_object
;
4735 object_lock_type
= backing_object_lock_type
;
4741 * If the object doesn't have all its pages present,
4742 * we have to make sure no pages in the backing object
4743 * "show through" before bypassing it.
4745 size
= (unsigned int)atop(object
->vo_size
);
4746 rcount
= object
->resident_page_count
;
4748 if (rcount
!= size
) {
4749 vm_object_offset_t offset
;
4750 vm_object_offset_t backing_offset
;
4751 unsigned int backing_rcount
;
4754 * If the backing object has a pager but no pagemap,
4755 * then we cannot bypass it, because we don't know
4756 * what pages it has.
4758 if (backing_object
->pager_created
) {
4759 /* try and collapse the rest of the shadow chain */
4760 if (object
!= original_object
) {
4761 vm_object_unlock(object
);
4763 object
= backing_object
;
4764 object_lock_type
= backing_object_lock_type
;
4769 * If the object has a pager but no pagemap,
4770 * then we cannot bypass it, because we don't know
4771 * what pages it has.
4773 if (object
->pager_created
) {
4774 /* try and collapse the rest of the shadow chain */
4775 if (object
!= original_object
) {
4776 vm_object_unlock(object
);
4778 object
= backing_object
;
4779 object_lock_type
= backing_object_lock_type
;
4783 backing_offset
= object
->vo_shadow_offset
;
4784 backing_rcount
= backing_object
->resident_page_count
;
4786 if ( (int)backing_rcount
- (int)(atop(backing_object
->vo_size
) - size
) > (int)rcount
) {
4788 * we have enough pages in the backing object to guarantee that
4789 * at least 1 of them must be 'uncovered' by a resident page
4790 * in the object we're evaluating, so move on and
4791 * try to collapse the rest of the shadow chain
4793 if (object
!= original_object
) {
4794 vm_object_unlock(object
);
4796 object
= backing_object
;
4797 object_lock_type
= backing_object_lock_type
;
4802 * If all of the pages in the backing object are
4803 * shadowed by the parent object, the parent
4804 * object no longer has to shadow the backing
4805 * object; it can shadow the next one in the
4808 * If the backing object has existence info,
4809 * we must check examine its existence info
4814 #define EXISTS_IN_OBJECT(obj, off, rc) \
4815 ((VM_COMPRESSOR_PAGER_STATE_GET((obj), (off)) \
4816 == VM_EXTERNAL_STATE_EXISTS) || \
4817 ((rc) && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--))
4820 * Check the hint location first
4821 * (since it is often the quickest way out of here).
4823 if (object
->cow_hint
!= ~(vm_offset_t
)0)
4824 hint_offset
= (vm_object_offset_t
)object
->cow_hint
;
4826 hint_offset
= (hint_offset
> 8 * PAGE_SIZE_64
) ?
4827 (hint_offset
- 8 * PAGE_SIZE_64
) : 0;
4829 if (EXISTS_IN_OBJECT(backing_object
, hint_offset
+
4830 backing_offset
, backing_rcount
) &&
4831 !EXISTS_IN_OBJECT(object
, hint_offset
, rcount
)) {
4832 /* dependency right at the hint */
4833 object
->cow_hint
= (vm_offset_t
) hint_offset
; /* atomic */
4834 /* try and collapse the rest of the shadow chain */
4835 if (object
!= original_object
) {
4836 vm_object_unlock(object
);
4838 object
= backing_object
;
4839 object_lock_type
= backing_object_lock_type
;
4844 * If the object's window onto the backing_object
4845 * is large compared to the number of resident
4846 * pages in the backing object, it makes sense to
4847 * walk the backing_object's resident pages first.
4849 * NOTE: Pages may be in both the existence map and/or
4850 * resident, so if we don't find a dependency while
4851 * walking the backing object's resident page list
4852 * directly, and there is an existence map, we'll have
4853 * to run the offset based 2nd pass. Because we may
4854 * have to run both passes, we need to be careful
4855 * not to decrement 'rcount' in the 1st pass
4857 if (backing_rcount
&& backing_rcount
< (size
/ 8)) {
4858 unsigned int rc
= rcount
;
4861 backing_rcount
= backing_object
->resident_page_count
;
4862 p
= (vm_page_t
)vm_page_queue_first(&backing_object
->memq
);
4864 offset
= (p
->offset
- backing_offset
);
4866 if (offset
< object
->vo_size
&&
4867 offset
!= hint_offset
&&
4868 !EXISTS_IN_OBJECT(object
, offset
, rc
)) {
4869 /* found a dependency */
4870 object
->cow_hint
= (vm_offset_t
) offset
; /* atomic */
4874 p
= (vm_page_t
) vm_page_queue_next(&p
->listq
);
4876 } while (--backing_rcount
);
4877 if (backing_rcount
!= 0 ) {
4878 /* try and collapse the rest of the shadow chain */
4879 if (object
!= original_object
) {
4880 vm_object_unlock(object
);
4882 object
= backing_object
;
4883 object_lock_type
= backing_object_lock_type
;
4889 * Walk through the offsets looking for pages in the
4890 * backing object that show through to the object.
4892 if (backing_rcount
) {
4893 offset
= hint_offset
;
4896 (offset
+ PAGE_SIZE_64
< object
->vo_size
) ?
4897 (offset
+ PAGE_SIZE_64
) : 0) != hint_offset
) {
4899 if (EXISTS_IN_OBJECT(backing_object
, offset
+
4900 backing_offset
, backing_rcount
) &&
4901 !EXISTS_IN_OBJECT(object
, offset
, rcount
)) {
4902 /* found a dependency */
4903 object
->cow_hint
= (vm_offset_t
) offset
; /* atomic */
4907 if (offset
!= hint_offset
) {
4908 /* try and collapse the rest of the shadow chain */
4909 if (object
!= original_object
) {
4910 vm_object_unlock(object
);
4912 object
= backing_object
;
4913 object_lock_type
= backing_object_lock_type
;
4920 * We need "exclusive" locks on the 2 VM objects.
4922 if (backing_object_lock_type
!= OBJECT_LOCK_EXCLUSIVE
) {
4923 vm_object_unlock(backing_object
);
4924 if (object
!= original_object
)
4925 vm_object_unlock(object
);
4926 object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4927 backing_object_lock_type
= OBJECT_LOCK_EXCLUSIVE
;
4931 /* reset the offset hint for any objects deeper in the chain */
4932 object
->cow_hint
= (vm_offset_t
)0;
4935 * All interesting pages in the backing object
4936 * already live in the parent or its pager.
4937 * Thus we can bypass the backing object.
4940 vm_object_do_bypass(object
, backing_object
);
4941 vm_object_collapse_do_bypass
++;
4944 * Try again with this object's new backing object.
4952 if (object != original_object) {
4953 vm_object_unlock(object);
4959 * Routine: vm_object_page_remove: [internal]
4961 * Removes all physical pages in the specified
4962 * object range from the object's list of pages.
4964 * In/out conditions:
4965 * The object must be locked.
4966 * The object must not have paging_in_progress, usually
4967 * guaranteed by not having a pager.
4969 unsigned int vm_object_page_remove_lookup
= 0;
4970 unsigned int vm_object_page_remove_iterate
= 0;
4972 __private_extern__
void
4973 vm_object_page_remove(
4975 vm_object_offset_t start
,
4976 vm_object_offset_t end
)
4981 * One and two page removals are most popular.
4982 * The factor of 16 here is somewhat arbitrary.
4983 * It balances vm_object_lookup vs iteration.
4986 if (atop_64(end
- start
) < (unsigned)object
->resident_page_count
/16) {
4987 vm_object_page_remove_lookup
++;
4989 for (; start
< end
; start
+= PAGE_SIZE_64
) {
4990 p
= vm_page_lookup(object
, start
);
4991 if (p
!= VM_PAGE_NULL
) {
4992 assert(!p
->cleaning
&& !p
->laundry
);
4993 if (!p
->fictitious
&& p
->pmapped
)
4994 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(p
));
4999 vm_object_page_remove_iterate
++;
5001 p
= (vm_page_t
) vm_page_queue_first(&object
->memq
);
5002 while (!vm_page_queue_end(&object
->memq
, (vm_page_queue_entry_t
) p
)) {
5003 next
= (vm_page_t
) vm_page_queue_next(&p
->listq
);
5004 if ((start
<= p
->offset
) && (p
->offset
< end
)) {
5005 assert(!p
->cleaning
&& !p
->laundry
);
5006 if (!p
->fictitious
&& p
->pmapped
)
5007 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(p
));
5017 * Routine: vm_object_coalesce
5018 * Function: Coalesces two objects backing up adjoining
5019 * regions of memory into a single object.
5021 * returns TRUE if objects were combined.
5023 * NOTE: Only works at the moment if the second object is NULL -
5024 * if it's not, which object do we lock first?
5027 * prev_object First object to coalesce
5028 * prev_offset Offset into prev_object
5029 * next_object Second object into coalesce
5030 * next_offset Offset into next_object
5032 * prev_size Size of reference to prev_object
5033 * next_size Size of reference to next_object
5036 * The object(s) must *not* be locked. The map must be locked
5037 * to preserve the reference to the object(s).
5039 static int vm_object_coalesce_count
= 0;
5041 __private_extern__ boolean_t
5043 vm_object_t prev_object
,
5044 vm_object_t next_object
,
5045 vm_object_offset_t prev_offset
,
5046 __unused vm_object_offset_t next_offset
,
5047 vm_object_size_t prev_size
,
5048 vm_object_size_t next_size
)
5050 vm_object_size_t newsize
;
5056 if (next_object
!= VM_OBJECT_NULL
) {
5060 if (prev_object
== VM_OBJECT_NULL
) {
5065 "vm_object_coalesce: 0x%X prev_off 0x%X prev_size 0x%X next_size 0x%X\n",
5066 prev_object
, prev_offset
, prev_size
, next_size
, 0);
5068 vm_object_lock(prev_object
);
5071 * Try to collapse the object first
5073 vm_object_collapse(prev_object
, prev_offset
, TRUE
);
5076 * Can't coalesce if pages not mapped to
5077 * prev_entry may be in use any way:
5078 * . more than one reference
5080 * . shadows another object
5081 * . has a copy elsewhere
5083 * . paging references (pages might be in page-list)
5086 if ((prev_object
->ref_count
> 1) ||
5087 prev_object
->pager_created
||
5088 (prev_object
->shadow
!= VM_OBJECT_NULL
) ||
5089 (prev_object
->copy
!= VM_OBJECT_NULL
) ||
5090 (prev_object
->true_share
!= FALSE
) ||
5091 (prev_object
->purgable
!= VM_PURGABLE_DENY
) ||
5092 (prev_object
->paging_in_progress
!= 0) ||
5093 (prev_object
->activity_in_progress
!= 0)) {
5094 vm_object_unlock(prev_object
);
5098 vm_object_coalesce_count
++;
5101 * Remove any pages that may still be in the object from
5102 * a previous deallocation.
5104 vm_object_page_remove(prev_object
,
5105 prev_offset
+ prev_size
,
5106 prev_offset
+ prev_size
+ next_size
);
5109 * Extend the object if necessary.
5111 newsize
= prev_offset
+ prev_size
+ next_size
;
5112 if (newsize
> prev_object
->vo_size
) {
5113 prev_object
->vo_size
= newsize
;
5116 vm_object_unlock(prev_object
);
5121 vm_object_populate_with_private(
5123 vm_object_offset_t offset
,
5128 vm_object_offset_t base_offset
;
5131 if (!object
->private)
5132 return KERN_FAILURE
;
5134 base_page
= phys_page
;
5136 vm_object_lock(object
);
5138 if (!object
->phys_contiguous
) {
5141 if ((base_offset
= trunc_page_64(offset
)) != offset
) {
5142 vm_object_unlock(object
);
5143 return KERN_FAILURE
;
5145 base_offset
+= object
->paging_offset
;
5148 m
= vm_page_lookup(object
, base_offset
);
5150 if (m
!= VM_PAGE_NULL
) {
5151 if (m
->fictitious
) {
5152 if (VM_PAGE_GET_PHYS_PAGE(m
) != vm_page_guard_addr
) {
5154 vm_page_lockspin_queues();
5156 vm_page_unlock_queues();
5158 m
->fictitious
= FALSE
;
5159 VM_PAGE_SET_PHYS_PAGE(m
, base_page
);
5161 } else if (VM_PAGE_GET_PHYS_PAGE(m
) != base_page
) {
5165 * we'd leak a real page... that can't be right
5167 panic("vm_object_populate_with_private - %p not private", m
);
5171 * pmap call to clear old mapping
5173 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
5175 VM_PAGE_SET_PHYS_PAGE(m
, base_page
);
5179 while ((m
= vm_page_grab_fictitious()) == VM_PAGE_NULL
)
5180 vm_page_more_fictitious();
5183 * private normally requires lock_queues but since we
5184 * are initializing the page, its not necessary here
5187 m
->fictitious
= FALSE
;
5188 VM_PAGE_SET_PHYS_PAGE(m
, base_page
);
5192 vm_page_insert(m
, object
, base_offset
);
5194 base_page
++; /* Go to the next physical page */
5195 base_offset
+= PAGE_SIZE
;
5199 /* NOTE: we should check the original settings here */
5200 /* if we have a size > zero a pmap call should be made */
5201 /* to disable the range */
5205 /* shadows on contiguous memory are not allowed */
5206 /* we therefore can use the offset field */
5207 object
->vo_shadow_offset
= (vm_object_offset_t
)phys_page
<< PAGE_SHIFT
;
5208 object
->vo_size
= size
;
5210 vm_object_unlock(object
);
5212 return KERN_SUCCESS
;
5217 memory_object_create_named(
5218 memory_object_t pager
,
5219 memory_object_offset_t size
,
5220 memory_object_control_t
*control
)
5224 *control
= MEMORY_OBJECT_CONTROL_NULL
;
5225 if (pager
== MEMORY_OBJECT_NULL
)
5226 return KERN_INVALID_ARGUMENT
;
5228 object
= vm_object_memory_object_associate(pager
,
5232 if (object
== VM_OBJECT_NULL
) {
5233 return KERN_INVALID_OBJECT
;
5236 /* wait for object (if any) to be ready */
5237 if (object
!= VM_OBJECT_NULL
) {
5238 vm_object_lock(object
);
5239 object
->named
= TRUE
;
5240 while (!object
->pager_ready
) {
5241 vm_object_sleep(object
,
5242 VM_OBJECT_EVENT_PAGER_READY
,
5245 *control
= object
->pager_control
;
5246 vm_object_unlock(object
);
5248 return (KERN_SUCCESS
);
5253 * Routine: memory_object_recover_named [user interface]
5255 * Attempt to recover a named reference for a VM object.
5256 * VM will verify that the object has not already started
5257 * down the termination path, and if it has, will optionally
5258 * wait for that to finish.
5260 * KERN_SUCCESS - we recovered a named reference on the object
5261 * KERN_FAILURE - we could not recover a reference (object dead)
5262 * KERN_INVALID_ARGUMENT - bad memory object control
5265 memory_object_recover_named(
5266 memory_object_control_t control
,
5267 boolean_t wait_on_terminating
)
5271 object
= memory_object_control_to_vm_object(control
);
5272 if (object
== VM_OBJECT_NULL
) {
5273 return (KERN_INVALID_ARGUMENT
);
5276 vm_object_lock(object
);
5278 if (object
->terminating
&& wait_on_terminating
) {
5279 vm_object_wait(object
,
5280 VM_OBJECT_EVENT_PAGING_IN_PROGRESS
,
5285 if (!object
->alive
) {
5286 vm_object_unlock(object
);
5287 return KERN_FAILURE
;
5290 if (object
->named
== TRUE
) {
5291 vm_object_unlock(object
);
5292 return KERN_SUCCESS
;
5294 object
->named
= TRUE
;
5295 vm_object_lock_assert_exclusive(object
);
5296 object
->ref_count
++;
5297 vm_object_res_reference(object
);
5298 while (!object
->pager_ready
) {
5299 vm_object_sleep(object
,
5300 VM_OBJECT_EVENT_PAGER_READY
,
5303 vm_object_unlock(object
);
5304 return (KERN_SUCCESS
);
5309 * vm_object_release_name:
5311 * Enforces name semantic on memory_object reference count decrement
5312 * This routine should not be called unless the caller holds a name
5313 * reference gained through the memory_object_create_named.
5315 * If the TERMINATE_IDLE flag is set, the call will return if the
5316 * reference count is not 1. i.e. idle with the only remaining reference
5318 * If the decision is made to proceed the name field flag is set to
5319 * false and the reference count is decremented. If the RESPECT_CACHE
5320 * flag is set and the reference count has gone to zero, the
5321 * memory_object is checked to see if it is cacheable otherwise when
5322 * the reference count is zero, it is simply terminated.
5325 __private_extern__ kern_return_t
5326 vm_object_release_name(
5331 boolean_t original_object
= TRUE
;
5333 while (object
!= VM_OBJECT_NULL
) {
5335 vm_object_lock(object
);
5337 assert(object
->alive
);
5338 if (original_object
)
5339 assert(object
->named
);
5340 assert(object
->ref_count
> 0);
5343 * We have to wait for initialization before
5344 * destroying or caching the object.
5347 if (object
->pager_created
&& !object
->pager_initialized
) {
5348 assert(!object
->can_persist
);
5349 vm_object_assert_wait(object
,
5350 VM_OBJECT_EVENT_INITIALIZED
,
5352 vm_object_unlock(object
);
5353 thread_block(THREAD_CONTINUE_NULL
);
5357 if (((object
->ref_count
> 1)
5358 && (flags
& MEMORY_OBJECT_TERMINATE_IDLE
))
5359 || (object
->terminating
)) {
5360 vm_object_unlock(object
);
5361 return KERN_FAILURE
;
5363 if (flags
& MEMORY_OBJECT_RELEASE_NO_OP
) {
5364 vm_object_unlock(object
);
5365 return KERN_SUCCESS
;
5369 if ((flags
& MEMORY_OBJECT_RESPECT_CACHE
) &&
5370 (object
->ref_count
== 1)) {
5371 if (original_object
)
5372 object
->named
= FALSE
;
5373 vm_object_unlock(object
);
5374 /* let vm_object_deallocate push this thing into */
5375 /* the cache, if that it is where it is bound */
5376 vm_object_deallocate(object
);
5377 return KERN_SUCCESS
;
5379 VM_OBJ_RES_DECR(object
);
5380 shadow
= object
->pageout
?VM_OBJECT_NULL
:object
->shadow
;
5382 if (object
->ref_count
== 1) {
5383 if (vm_object_terminate(object
) != KERN_SUCCESS
) {
5384 if (original_object
) {
5385 return KERN_FAILURE
;
5387 return KERN_SUCCESS
;
5390 if (shadow
!= VM_OBJECT_NULL
) {
5391 original_object
= FALSE
;
5395 return KERN_SUCCESS
;
5397 vm_object_lock_assert_exclusive(object
);
5398 object
->ref_count
--;
5399 assert(object
->ref_count
> 0);
5401 object
->named
= FALSE
;
5402 vm_object_unlock(object
);
5403 return KERN_SUCCESS
;
5408 return KERN_FAILURE
;
5412 __private_extern__ kern_return_t
5413 vm_object_lock_request(
5415 vm_object_offset_t offset
,
5416 vm_object_size_t size
,
5417 memory_object_return_t should_return
,
5421 __unused boolean_t should_flush
;
5423 should_flush
= flags
& MEMORY_OBJECT_DATA_FLUSH
;
5425 XPR(XPR_MEMORY_OBJECT
,
5426 "vm_o_lock_request, obj 0x%X off 0x%X size 0x%X flags %X prot %X\n",
5427 object
, offset
, size
,
5428 (((should_return
&1)<<1)|should_flush
), prot
);
5431 * Check for bogus arguments.
5433 if (object
== VM_OBJECT_NULL
)
5434 return (KERN_INVALID_ARGUMENT
);
5436 if ((prot
& ~VM_PROT_ALL
) != 0 && prot
!= VM_PROT_NO_CHANGE
)
5437 return (KERN_INVALID_ARGUMENT
);
5439 size
= round_page_64(size
);
5442 * Lock the object, and acquire a paging reference to
5443 * prevent the memory_object reference from being released.
5445 vm_object_lock(object
);
5446 vm_object_paging_begin(object
);
5448 (void)vm_object_update(object
,
5449 offset
, size
, NULL
, NULL
, should_return
, flags
, prot
);
5451 vm_object_paging_end(object
);
5452 vm_object_unlock(object
);
5454 return (KERN_SUCCESS
);
5458 * Empty a purgeable object by grabbing the physical pages assigned to it and
5459 * putting them on the free queue without writing them to backing store, etc.
5460 * When the pages are next touched they will be demand zero-fill pages. We
5461 * skip pages which are busy, being paged in/out, wired, etc. We do _not_
5462 * skip referenced/dirty pages, pages on the active queue, etc. We're more
5463 * than happy to grab these since this is a purgeable object. We mark the
5464 * object as "empty" after reaping its pages.
5466 * On entry the object must be locked and it must be
5467 * purgeable with no delayed copies pending.
5470 vm_object_purge(vm_object_t object
, int flags
)
5472 unsigned int object_page_count
= 0;
5473 unsigned int pgcount
= 0;
5474 boolean_t skipped_object
= FALSE
;
5476 vm_object_lock_assert_exclusive(object
);
5478 if (object
->purgable
== VM_PURGABLE_DENY
)
5481 assert(object
->copy
== VM_OBJECT_NULL
);
5482 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
5485 * We need to set the object's state to VM_PURGABLE_EMPTY *before*
5486 * reaping its pages. We update vm_page_purgeable_count in bulk
5487 * and we don't want vm_page_remove() to update it again for each
5488 * page we reap later.
5490 * For the purgeable ledgers, pages from VOLATILE and EMPTY objects
5491 * are all accounted for in the "volatile" ledgers, so this does not
5492 * make any difference.
5493 * If we transitioned directly from NONVOLATILE to EMPTY,
5494 * vm_page_purgeable_count must have been updated when the object
5495 * was dequeued from its volatile queue and the purgeable ledgers
5496 * must have also been updated accordingly at that time (in
5497 * vm_object_purgable_control()).
5499 if (object
->purgable
== VM_PURGABLE_VOLATILE
) {
5501 assert(object
->resident_page_count
>=
5502 object
->wired_page_count
);
5503 delta
= (object
->resident_page_count
-
5504 object
->wired_page_count
);
5506 assert(vm_page_purgeable_count
>=
5509 (SInt32
*)&vm_page_purgeable_count
);
5511 if (object
->wired_page_count
!= 0) {
5512 assert(vm_page_purgeable_wired_count
>=
5513 object
->wired_page_count
);
5514 OSAddAtomic(-object
->wired_page_count
,
5515 (SInt32
*)&vm_page_purgeable_wired_count
);
5517 object
->purgable
= VM_PURGABLE_EMPTY
;
5519 assert(object
->purgable
== VM_PURGABLE_EMPTY
);
5521 object_page_count
= object
->resident_page_count
;
5523 vm_object_reap_pages(object
, REAP_PURGEABLE
);
5525 if (object
->pager
!= NULL
) {
5527 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
5529 if (object
->activity_in_progress
== 0 &&
5530 object
->paging_in_progress
== 0) {
5532 * Also reap any memory coming from this object
5533 * in the VM compressor.
5535 * There are no operations in progress on the VM object
5536 * and no operation can start while we're holding the
5537 * VM object lock, so it's safe to reap the compressed
5538 * pages and update the page counts.
5540 pgcount
= vm_compressor_pager_get_count(object
->pager
);
5542 pgcount
= vm_compressor_pager_reap_pages(object
->pager
, flags
);
5543 vm_compressor_pager_count(object
->pager
,
5547 vm_purgeable_compressed_update(object
,
5550 if ( !(flags
& C_DONT_BLOCK
)) {
5551 assert(vm_compressor_pager_get_count(object
->pager
)
5556 * There's some kind of paging activity in progress
5557 * for this object, which could result in a page
5558 * being compressed or decompressed, possibly while
5559 * the VM object is not locked, so it could race
5562 * We can't really synchronize this without possibly
5563 * causing a deadlock when the compressor needs to
5564 * allocate or free memory while compressing or
5565 * decompressing a page from a purgeable object
5566 * mapped in the kernel_map...
5568 * So let's not attempt to purge the compressor
5569 * pager if there's any kind of operation in
5570 * progress on the VM object.
5572 skipped_object
= TRUE
;
5576 vm_object_lock_assert_exclusive(object
);
5578 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_VM
, OBJECT_PURGE_ONE
)),
5579 VM_KERNEL_UNSLIDE_OR_PERM(object
), /* purged object */
5589 * vm_object_purgeable_control() allows the caller to control and investigate the
5590 * state of a purgeable object. A purgeable object is created via a call to
5591 * vm_allocate() with VM_FLAGS_PURGABLE specified. A purgeable object will
5592 * never be coalesced with any other object -- even other purgeable objects --
5593 * and will thus always remain a distinct object. A purgeable object has
5594 * special semantics when its reference count is exactly 1. If its reference
5595 * count is greater than 1, then a purgeable object will behave like a normal
5596 * object and attempts to use this interface will result in an error return
5597 * of KERN_INVALID_ARGUMENT.
5599 * A purgeable object may be put into a "volatile" state which will make the
5600 * object's pages elligable for being reclaimed without paging to backing
5601 * store if the system runs low on memory. If the pages in a volatile
5602 * purgeable object are reclaimed, the purgeable object is said to have been
5603 * "emptied." When a purgeable object is emptied the system will reclaim as
5604 * many pages from the object as it can in a convenient manner (pages already
5605 * en route to backing store or busy for other reasons are left as is). When
5606 * a purgeable object is made volatile, its pages will generally be reclaimed
5607 * before other pages in the application's working set. This semantic is
5608 * generally used by applications which can recreate the data in the object
5609 * faster than it can be paged in. One such example might be media assets
5610 * which can be reread from a much faster RAID volume.
5612 * A purgeable object may be designated as "non-volatile" which means it will
5613 * behave like all other objects in the system with pages being written to and
5614 * read from backing store as needed to satisfy system memory needs. If the
5615 * object was emptied before the object was made non-volatile, that fact will
5616 * be returned as the old state of the purgeable object (see
5617 * VM_PURGABLE_SET_STATE below). In this case, any pages of the object which
5618 * were reclaimed as part of emptying the object will be refaulted in as
5619 * zero-fill on demand. It is up to the application to note that an object
5620 * was emptied and recreate the objects contents if necessary. When a
5621 * purgeable object is made non-volatile, its pages will generally not be paged
5622 * out to backing store in the immediate future. A purgeable object may also
5623 * be manually emptied.
5625 * Finally, the current state (non-volatile, volatile, volatile & empty) of a
5626 * volatile purgeable object may be queried at any time. This information may
5627 * be used as a control input to let the application know when the system is
5628 * experiencing memory pressure and is reclaiming memory.
5630 * The specified address may be any address within the purgeable object. If
5631 * the specified address does not represent any object in the target task's
5632 * virtual address space, then KERN_INVALID_ADDRESS will be returned. If the
5633 * object containing the specified address is not a purgeable object, then
5634 * KERN_INVALID_ARGUMENT will be returned. Otherwise, KERN_SUCCESS will be
5637 * The control parameter may be any one of VM_PURGABLE_SET_STATE or
5638 * VM_PURGABLE_GET_STATE. For VM_PURGABLE_SET_STATE, the in/out parameter
5639 * state is used to set the new state of the purgeable object and return its
5640 * old state. For VM_PURGABLE_GET_STATE, the current state of the purgeable
5641 * object is returned in the parameter state.
5643 * The in/out parameter state may be one of VM_PURGABLE_NONVOLATILE,
5644 * VM_PURGABLE_VOLATILE or VM_PURGABLE_EMPTY. These, respectively, represent
5645 * the non-volatile, volatile and volatile/empty states described above.
5646 * Setting the state of a purgeable object to VM_PURGABLE_EMPTY will
5647 * immediately reclaim as many pages in the object as can be conveniently
5648 * collected (some may have already been written to backing store or be
5651 * The process of making a purgeable object non-volatile and determining its
5652 * previous state is atomic. Thus, if a purgeable object is made
5653 * VM_PURGABLE_NONVOLATILE and the old state is returned as
5654 * VM_PURGABLE_VOLATILE, then the purgeable object's previous contents are
5655 * completely intact and will remain so until the object is made volatile
5656 * again. If the old state is returned as VM_PURGABLE_EMPTY then the object
5657 * was reclaimed while it was in a volatile state and its previous contents
5661 * The object must be locked.
5664 vm_object_purgable_control(
5666 vm_purgable_t control
,
5672 if (object
== VM_OBJECT_NULL
) {
5674 * Object must already be present or it can't be purgeable.
5676 return KERN_INVALID_ARGUMENT
;
5679 vm_object_lock_assert_exclusive(object
);
5682 * Get current state of the purgeable object.
5684 old_state
= object
->purgable
;
5685 if (old_state
== VM_PURGABLE_DENY
)
5686 return KERN_INVALID_ARGUMENT
;
5688 /* purgeable cant have delayed copies - now or in the future */
5689 assert(object
->copy
== VM_OBJECT_NULL
);
5690 assert(object
->copy_strategy
== MEMORY_OBJECT_COPY_NONE
);
5693 * Execute the desired operation.
5695 if (control
== VM_PURGABLE_GET_STATE
) {
5697 return KERN_SUCCESS
;
5700 if (control
== VM_PURGABLE_SET_STATE
&&
5701 object
->purgeable_only_by_kernel
) {
5702 return KERN_PROTECTION_FAILURE
;
5705 if (control
!= VM_PURGABLE_SET_STATE
&&
5706 control
!= VM_PURGABLE_SET_STATE_FROM_KERNEL
) {
5707 return KERN_INVALID_ARGUMENT
;
5710 if ((*state
) & VM_PURGABLE_DEBUG_EMPTY
) {
5711 object
->volatile_empty
= TRUE
;
5713 if ((*state
) & VM_PURGABLE_DEBUG_FAULT
) {
5714 object
->volatile_fault
= TRUE
;
5717 new_state
= *state
& VM_PURGABLE_STATE_MASK
;
5718 if (new_state
== VM_PURGABLE_VOLATILE
) {
5719 if (old_state
== VM_PURGABLE_EMPTY
) {
5720 /* what's been emptied must stay empty */
5721 new_state
= VM_PURGABLE_EMPTY
;
5723 if (object
->volatile_empty
) {
5724 /* debugging mode: go straight to empty */
5725 new_state
= VM_PURGABLE_EMPTY
;
5729 switch (new_state
) {
5730 case VM_PURGABLE_DENY
:
5732 * Attempting to convert purgeable memory to non-purgeable:
5735 return KERN_INVALID_ARGUMENT
;
5736 case VM_PURGABLE_NONVOLATILE
:
5737 object
->purgable
= new_state
;
5739 if (old_state
== VM_PURGABLE_VOLATILE
) {
5742 assert(object
->resident_page_count
>=
5743 object
->wired_page_count
);
5744 delta
= (object
->resident_page_count
-
5745 object
->wired_page_count
);
5747 assert(vm_page_purgeable_count
>= delta
);
5751 (SInt32
*)&vm_page_purgeable_count
);
5753 if (object
->wired_page_count
!= 0) {
5754 assert(vm_page_purgeable_wired_count
>=
5755 object
->wired_page_count
);
5756 OSAddAtomic(-object
->wired_page_count
,
5757 (SInt32
*)&vm_page_purgeable_wired_count
);
5760 vm_page_lock_queues();
5762 /* object should be on a queue */
5763 assert(object
->objq
.next
!= NULL
&&
5764 object
->objq
.prev
!= NULL
);
5765 purgeable_q_t queue
;
5768 * Move object from its volatile queue to the
5769 * non-volatile queue...
5771 queue
= vm_purgeable_object_remove(object
);
5774 if (object
->purgeable_when_ripe
) {
5775 vm_purgeable_token_delete_last(queue
);
5777 assert(queue
->debug_count_objects
>=0);
5779 vm_page_unlock_queues();
5781 if (old_state
== VM_PURGABLE_VOLATILE
||
5782 old_state
== VM_PURGABLE_EMPTY
) {
5784 * Transfer the object's pages from the volatile to
5785 * non-volatile ledgers.
5787 vm_purgeable_accounting(object
, VM_PURGABLE_VOLATILE
,
5793 case VM_PURGABLE_VOLATILE
:
5794 if (object
->volatile_fault
) {
5798 vm_page_queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
5804 refmod
= pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(p
));
5805 if ((refmod
& VM_MEM_MODIFIED
) &&
5807 SET_PAGE_DIRTY(p
, FALSE
);
5812 assert(old_state
!= VM_PURGABLE_EMPTY
);
5814 purgeable_q_t queue
;
5816 /* find the correct queue */
5817 if ((*state
&VM_PURGABLE_ORDERING_MASK
) == VM_PURGABLE_ORDERING_OBSOLETE
)
5818 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_OBSOLETE
];
5820 if ((*state
&VM_PURGABLE_BEHAVIOR_MASK
) == VM_PURGABLE_BEHAVIOR_FIFO
)
5821 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_FIFO
];
5823 queue
= &purgeable_queues
[PURGEABLE_Q_TYPE_LIFO
];
5826 if (old_state
== VM_PURGABLE_NONVOLATILE
||
5827 old_state
== VM_PURGABLE_EMPTY
) {
5830 if ((*state
& VM_PURGABLE_NO_AGING_MASK
) ==
5831 VM_PURGABLE_NO_AGING
) {
5832 object
->purgeable_when_ripe
= FALSE
;
5834 object
->purgeable_when_ripe
= TRUE
;
5837 if (object
->purgeable_when_ripe
) {
5838 kern_return_t result
;
5840 /* try to add token... this can fail */
5841 vm_page_lock_queues();
5843 result
= vm_purgeable_token_add(queue
);
5844 if (result
!= KERN_SUCCESS
) {
5845 vm_page_unlock_queues();
5848 vm_page_unlock_queues();
5851 assert(object
->resident_page_count
>=
5852 object
->wired_page_count
);
5853 delta
= (object
->resident_page_count
-
5854 object
->wired_page_count
);
5858 &vm_page_purgeable_count
);
5860 if (object
->wired_page_count
!= 0) {
5861 OSAddAtomic(object
->wired_page_count
,
5862 &vm_page_purgeable_wired_count
);
5865 object
->purgable
= new_state
;
5867 /* object should be on "non-volatile" queue */
5868 assert(object
->objq
.next
!= NULL
);
5869 assert(object
->objq
.prev
!= NULL
);
5871 else if (old_state
== VM_PURGABLE_VOLATILE
) {
5872 purgeable_q_t old_queue
;
5873 boolean_t purgeable_when_ripe
;
5876 * if reassigning priorities / purgeable groups, we don't change the
5877 * token queue. So moving priorities will not make pages stay around longer.
5878 * Reasoning is that the algorithm gives most priority to the most important
5879 * object. If a new token is added, the most important object' priority is boosted.
5880 * This biases the system already for purgeable queues that move a lot.
5881 * It doesn't seem more biasing is neccessary in this case, where no new object is added.
5883 assert(object
->objq
.next
!= NULL
&& object
->objq
.prev
!= NULL
); /* object should be on a queue */
5885 old_queue
= vm_purgeable_object_remove(object
);
5888 if ((*state
& VM_PURGABLE_NO_AGING_MASK
) ==
5889 VM_PURGABLE_NO_AGING
) {
5890 purgeable_when_ripe
= FALSE
;
5892 purgeable_when_ripe
= TRUE
;
5895 if (old_queue
!= queue
||
5896 (purgeable_when_ripe
!=
5897 object
->purgeable_when_ripe
)) {
5898 kern_return_t result
;
5900 /* Changing queue. Have to move token. */
5901 vm_page_lock_queues();
5902 if (object
->purgeable_when_ripe
) {
5903 vm_purgeable_token_delete_last(old_queue
);
5905 object
->purgeable_when_ripe
= purgeable_when_ripe
;
5906 if (object
->purgeable_when_ripe
) {
5907 result
= vm_purgeable_token_add(queue
);
5908 assert(result
==KERN_SUCCESS
); /* this should never fail since we just freed a token */
5910 vm_page_unlock_queues();
5914 vm_purgeable_object_add(object
, queue
, (*state
&VM_VOLATILE_GROUP_MASK
)>>VM_VOLATILE_GROUP_SHIFT
);
5915 if (old_state
== VM_PURGABLE_NONVOLATILE
) {
5916 vm_purgeable_accounting(object
, VM_PURGABLE_NONVOLATILE
,
5920 assert(queue
->debug_count_objects
>=0);
5925 case VM_PURGABLE_EMPTY
:
5926 if (object
->volatile_fault
) {
5930 vm_page_queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
5936 refmod
= pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(p
));
5937 if ((refmod
& VM_MEM_MODIFIED
) &&
5939 SET_PAGE_DIRTY(p
, FALSE
);
5944 if (old_state
== VM_PURGABLE_VOLATILE
) {
5945 purgeable_q_t old_queue
;
5947 /* object should be on a queue */
5948 assert(object
->objq
.next
!= NULL
&&
5949 object
->objq
.prev
!= NULL
);
5951 old_queue
= vm_purgeable_object_remove(object
);
5953 if (object
->purgeable_when_ripe
) {
5954 vm_page_lock_queues();
5955 vm_purgeable_token_delete_first(old_queue
);
5956 vm_page_unlock_queues();
5960 if (old_state
== VM_PURGABLE_NONVOLATILE
) {
5962 * This object's pages were previously accounted as
5963 * "non-volatile" and now need to be accounted as
5966 vm_purgeable_accounting(object
, VM_PURGABLE_NONVOLATILE
,
5969 * Set to VM_PURGABLE_EMPTY because the pages are no
5970 * longer accounted in the "non-volatile" ledger
5971 * and are also not accounted for in
5972 * "vm_page_purgeable_count".
5974 object
->purgable
= VM_PURGABLE_EMPTY
;
5977 (void) vm_object_purge(object
, 0);
5978 assert(object
->purgable
== VM_PURGABLE_EMPTY
);
5985 vm_object_lock_assert_exclusive(object
);
5987 return KERN_SUCCESS
;
5991 vm_object_get_page_counts(
5993 vm_object_offset_t offset
,
5994 vm_object_size_t size
,
5995 unsigned int *resident_page_count
,
5996 unsigned int *dirty_page_count
)
5999 kern_return_t kr
= KERN_SUCCESS
;
6000 boolean_t count_dirty_pages
= FALSE
;
6001 vm_page_t p
= VM_PAGE_NULL
;
6002 unsigned int local_resident_count
= 0;
6003 unsigned int local_dirty_count
= 0;
6004 vm_object_offset_t cur_offset
= 0;
6005 vm_object_offset_t end_offset
= 0;
6007 if (object
== VM_OBJECT_NULL
)
6008 return KERN_INVALID_ARGUMENT
;
6011 cur_offset
= offset
;
6013 end_offset
= offset
+ size
;
6015 vm_object_lock_assert_exclusive(object
);
6017 if (dirty_page_count
!= NULL
) {
6019 count_dirty_pages
= TRUE
;
6022 if (resident_page_count
!= NULL
&& count_dirty_pages
== FALSE
) {
6025 * - we only want the resident page count, and,
6026 * - the entire object is exactly covered by the request.
6028 if (offset
== 0 && (object
->vo_size
== size
)) {
6030 *resident_page_count
= object
->resident_page_count
;
6035 if (object
->resident_page_count
<= (size
>> PAGE_SHIFT
)) {
6037 vm_page_queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
6039 if (p
->offset
>= cur_offset
&& p
->offset
< end_offset
) {
6041 local_resident_count
++;
6043 if (count_dirty_pages
) {
6045 if (p
->dirty
|| (p
->wpmapped
&& pmap_is_modified(VM_PAGE_GET_PHYS_PAGE(p
)))) {
6047 local_dirty_count
++;
6054 for (cur_offset
= offset
; cur_offset
< end_offset
; cur_offset
+= PAGE_SIZE_64
) {
6056 p
= vm_page_lookup(object
, cur_offset
);
6058 if (p
!= VM_PAGE_NULL
) {
6060 local_resident_count
++;
6062 if (count_dirty_pages
) {
6064 if (p
->dirty
|| (p
->wpmapped
&& pmap_is_modified(VM_PAGE_GET_PHYS_PAGE(p
)))) {
6066 local_dirty_count
++;
6074 if (resident_page_count
!= NULL
) {
6075 *resident_page_count
= local_resident_count
;
6078 if (dirty_page_count
!= NULL
) {
6079 *dirty_page_count
= local_dirty_count
;
6089 * vm_object_res_deallocate
6091 * (recursively) decrement residence counts on vm objects and their shadows.
6092 * Called from vm_object_deallocate and when swapping out an object.
6094 * The object is locked, and remains locked throughout the function,
6095 * even as we iterate down the shadow chain. Locks on intermediate objects
6096 * will be dropped, but not the original object.
6098 * NOTE: this function used to use recursion, rather than iteration.
6101 __private_extern__
void
6102 vm_object_res_deallocate(
6105 vm_object_t orig_object
= object
;
6107 * Object is locked so it can be called directly
6108 * from vm_object_deallocate. Original object is never
6111 assert(object
->res_count
> 0);
6112 while (--object
->res_count
== 0) {
6113 assert(object
->ref_count
>= object
->res_count
);
6114 vm_object_deactivate_all_pages(object
);
6115 /* iterate on shadow, if present */
6116 if (object
->shadow
!= VM_OBJECT_NULL
) {
6117 vm_object_t tmp_object
= object
->shadow
;
6118 vm_object_lock(tmp_object
);
6119 if (object
!= orig_object
)
6120 vm_object_unlock(object
);
6121 object
= tmp_object
;
6122 assert(object
->res_count
> 0);
6126 if (object
!= orig_object
)
6127 vm_object_unlock(object
);
6131 * vm_object_res_reference
6133 * Internal function to increment residence count on a vm object
6134 * and its shadows. It is called only from vm_object_reference, and
6135 * when swapping in a vm object, via vm_map_swap.
6137 * The object is locked, and remains locked throughout the function,
6138 * even as we iterate down the shadow chain. Locks on intermediate objects
6139 * will be dropped, but not the original object.
6141 * NOTE: this function used to use recursion, rather than iteration.
6144 __private_extern__
void
6145 vm_object_res_reference(
6148 vm_object_t orig_object
= object
;
6150 * Object is locked, so this can be called directly
6151 * from vm_object_reference. This lock is never released.
6153 while ((++object
->res_count
== 1) &&
6154 (object
->shadow
!= VM_OBJECT_NULL
)) {
6155 vm_object_t tmp_object
= object
->shadow
;
6157 assert(object
->ref_count
>= object
->res_count
);
6158 vm_object_lock(tmp_object
);
6159 if (object
!= orig_object
)
6160 vm_object_unlock(object
);
6161 object
= tmp_object
;
6163 if (object
!= orig_object
)
6164 vm_object_unlock(object
);
6165 assert(orig_object
->ref_count
>= orig_object
->res_count
);
6167 #endif /* TASK_SWAPPER */
6170 * vm_object_reference:
6172 * Gets another reference to the given object.
6174 #ifdef vm_object_reference
6175 #undef vm_object_reference
6177 __private_extern__
void
6178 vm_object_reference(
6181 if (object
== VM_OBJECT_NULL
)
6184 vm_object_lock(object
);
6185 assert(object
->ref_count
> 0);
6186 vm_object_reference_locked(object
);
6187 vm_object_unlock(object
);
6191 * vm_object_transpose
6193 * This routine takes two VM objects of the same size and exchanges
6194 * their backing store.
6195 * The objects should be "quiesced" via a UPL operation with UPL_SET_IO_WIRE
6196 * and UPL_BLOCK_ACCESS if they are referenced anywhere.
6198 * The VM objects must not be locked by caller.
6200 unsigned int vm_object_transpose_count
= 0;
6202 vm_object_transpose(
6203 vm_object_t object1
,
6204 vm_object_t object2
,
6205 vm_object_size_t transpose_size
)
6207 vm_object_t tmp_object
;
6208 kern_return_t retval
;
6209 boolean_t object1_locked
, object2_locked
;
6211 vm_object_offset_t page_offset
;
6213 tmp_object
= VM_OBJECT_NULL
;
6214 object1_locked
= FALSE
; object2_locked
= FALSE
;
6216 if (object1
== object2
||
6217 object1
== VM_OBJECT_NULL
||
6218 object2
== VM_OBJECT_NULL
) {
6220 * If the 2 VM objects are the same, there's
6221 * no point in exchanging their backing store.
6223 retval
= KERN_INVALID_VALUE
;
6228 * Since we need to lock both objects at the same time,
6229 * make sure we always lock them in the same order to
6232 if (object1
> object2
) {
6233 tmp_object
= object1
;
6235 object2
= tmp_object
;
6239 * Allocate a temporary VM object to hold object1's contents
6240 * while we copy object2 to object1.
6242 tmp_object
= vm_object_allocate(transpose_size
);
6243 vm_object_lock(tmp_object
);
6244 tmp_object
->can_persist
= FALSE
;
6248 * Grab control of the 1st VM object.
6250 vm_object_lock(object1
);
6251 object1_locked
= TRUE
;
6252 if (!object1
->alive
|| object1
->terminating
||
6253 object1
->copy
|| object1
->shadow
|| object1
->shadowed
||
6254 object1
->purgable
!= VM_PURGABLE_DENY
) {
6256 * We don't deal with copy or shadow objects (yet).
6258 retval
= KERN_INVALID_VALUE
;
6262 * We're about to mess with the object's backing store and
6263 * taking a "paging_in_progress" reference wouldn't be enough
6264 * to prevent any paging activity on this object, so the caller should
6265 * have "quiesced" the objects beforehand, via a UPL operation with
6266 * UPL_SET_IO_WIRE (to make sure all the pages are there and wired)
6267 * and UPL_BLOCK_ACCESS (to mark the pages "busy").
6269 * Wait for any paging operation to complete (but only paging, not
6270 * other kind of activities not linked to the pager). After we're
6271 * statisfied that there's no more paging in progress, we keep the
6272 * object locked, to guarantee that no one tries to access its pager.
6274 vm_object_paging_only_wait(object1
, THREAD_UNINT
);
6277 * Same as above for the 2nd object...
6279 vm_object_lock(object2
);
6280 object2_locked
= TRUE
;
6281 if (! object2
->alive
|| object2
->terminating
||
6282 object2
->copy
|| object2
->shadow
|| object2
->shadowed
||
6283 object2
->purgable
!= VM_PURGABLE_DENY
) {
6284 retval
= KERN_INVALID_VALUE
;
6287 vm_object_paging_only_wait(object2
, THREAD_UNINT
);
6290 if (object1
->vo_size
!= object2
->vo_size
||
6291 object1
->vo_size
!= transpose_size
) {
6293 * If the 2 objects don't have the same size, we can't
6294 * exchange their backing stores or one would overflow.
6295 * If their size doesn't match the caller's
6296 * "transpose_size", we can't do it either because the
6297 * transpose operation will affect the entire span of
6300 retval
= KERN_INVALID_VALUE
;
6306 * Transpose the lists of resident pages.
6307 * This also updates the resident_page_count and the memq_hint.
6309 if (object1
->phys_contiguous
|| vm_page_queue_empty(&object1
->memq
)) {
6311 * No pages in object1, just transfer pages
6312 * from object2 to object1. No need to go through
6313 * an intermediate object.
6315 while (!vm_page_queue_empty(&object2
->memq
)) {
6316 page
= (vm_page_t
) vm_page_queue_first(&object2
->memq
);
6317 vm_page_rename(page
, object1
, page
->offset
);
6319 assert(vm_page_queue_empty(&object2
->memq
));
6320 } else if (object2
->phys_contiguous
|| vm_page_queue_empty(&object2
->memq
)) {
6322 * No pages in object2, just transfer pages
6323 * from object1 to object2. No need to go through
6324 * an intermediate object.
6326 while (!vm_page_queue_empty(&object1
->memq
)) {
6327 page
= (vm_page_t
) vm_page_queue_first(&object1
->memq
);
6328 vm_page_rename(page
, object2
, page
->offset
);
6330 assert(vm_page_queue_empty(&object1
->memq
));
6332 /* transfer object1's pages to tmp_object */
6333 while (!vm_page_queue_empty(&object1
->memq
)) {
6334 page
= (vm_page_t
) vm_page_queue_first(&object1
->memq
);
6335 page_offset
= page
->offset
;
6336 vm_page_remove(page
, TRUE
);
6337 page
->offset
= page_offset
;
6338 vm_page_queue_enter(&tmp_object
->memq
, page
, vm_page_t
, listq
);
6340 assert(vm_page_queue_empty(&object1
->memq
));
6341 /* transfer object2's pages to object1 */
6342 while (!vm_page_queue_empty(&object2
->memq
)) {
6343 page
= (vm_page_t
) vm_page_queue_first(&object2
->memq
);
6344 vm_page_rename(page
, object1
, page
->offset
);
6346 assert(vm_page_queue_empty(&object2
->memq
));
6347 /* transfer tmp_object's pages to object2 */
6348 while (!vm_page_queue_empty(&tmp_object
->memq
)) {
6349 page
= (vm_page_t
) vm_page_queue_first(&tmp_object
->memq
);
6350 vm_page_queue_remove(&tmp_object
->memq
, page
,
6352 vm_page_insert(page
, object2
, page
->offset
);
6354 assert(vm_page_queue_empty(&tmp_object
->memq
));
6357 #define __TRANSPOSE_FIELD(field) \
6359 tmp_object->field = object1->field; \
6360 object1->field = object2->field; \
6361 object2->field = tmp_object->field; \
6364 /* "Lock" refers to the object not its contents */
6365 /* "size" should be identical */
6366 assert(object1
->vo_size
== object2
->vo_size
);
6367 /* "memq_hint" was updated above when transposing pages */
6368 /* "ref_count" refers to the object not its contents */
6369 assert(object1
->ref_count
>= 1);
6370 assert(object2
->ref_count
>= 1);
6372 /* "res_count" refers to the object not its contents */
6374 /* "resident_page_count" was updated above when transposing pages */
6375 /* "wired_page_count" was updated above when transposing pages */
6376 /* "reusable_page_count" was updated above when transposing pages */
6377 /* there should be no "copy" */
6378 assert(!object1
->copy
);
6379 assert(!object2
->copy
);
6380 /* there should be no "shadow" */
6381 assert(!object1
->shadow
);
6382 assert(!object2
->shadow
);
6383 __TRANSPOSE_FIELD(vo_shadow_offset
); /* used by phys_contiguous objects */
6384 __TRANSPOSE_FIELD(pager
);
6385 __TRANSPOSE_FIELD(paging_offset
);
6386 __TRANSPOSE_FIELD(pager_control
);
6387 /* update the memory_objects' pointers back to the VM objects */
6388 if (object1
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
6389 memory_object_control_collapse(object1
->pager_control
,
6392 if (object2
->pager_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
6393 memory_object_control_collapse(object2
->pager_control
,
6396 __TRANSPOSE_FIELD(copy_strategy
);
6397 /* "paging_in_progress" refers to the object not its contents */
6398 assert(!object1
->paging_in_progress
);
6399 assert(!object2
->paging_in_progress
);
6400 assert(object1
->activity_in_progress
);
6401 assert(object2
->activity_in_progress
);
6402 /* "all_wanted" refers to the object not its contents */
6403 __TRANSPOSE_FIELD(pager_created
);
6404 __TRANSPOSE_FIELD(pager_initialized
);
6405 __TRANSPOSE_FIELD(pager_ready
);
6406 __TRANSPOSE_FIELD(pager_trusted
);
6407 __TRANSPOSE_FIELD(can_persist
);
6408 __TRANSPOSE_FIELD(internal
);
6409 __TRANSPOSE_FIELD(private);
6410 __TRANSPOSE_FIELD(pageout
);
6411 /* "alive" should be set */
6412 assert(object1
->alive
);
6413 assert(object2
->alive
);
6414 /* "purgeable" should be non-purgeable */
6415 assert(object1
->purgable
== VM_PURGABLE_DENY
);
6416 assert(object2
->purgable
== VM_PURGABLE_DENY
);
6417 /* "shadowed" refers to the the object not its contents */
6418 __TRANSPOSE_FIELD(purgeable_when_ripe
);
6419 __TRANSPOSE_FIELD(true_share
);
6420 /* "terminating" should not be set */
6421 assert(!object1
->terminating
);
6422 assert(!object2
->terminating
);
6423 /* transfer "named" reference if needed */
6424 if (object1
->named
&& !object2
->named
) {
6425 assert(object1
->ref_count
>= 2);
6426 assert(object2
->ref_count
>= 1);
6427 object1
->ref_count
--;
6428 object2
->ref_count
++;
6429 } else if (!object1
->named
&& object2
->named
) {
6430 assert(object1
->ref_count
>= 1);
6431 assert(object2
->ref_count
>= 2);
6432 object1
->ref_count
++;
6433 object2
->ref_count
--;
6435 __TRANSPOSE_FIELD(named
);
6436 /* "shadow_severed" refers to the object not its contents */
6437 __TRANSPOSE_FIELD(phys_contiguous
);
6438 __TRANSPOSE_FIELD(nophyscache
);
6439 /* "cached_list.next" points to transposed object */
6440 object1
->cached_list
.next
= (queue_entry_t
) object2
;
6441 object2
->cached_list
.next
= (queue_entry_t
) object1
;
6442 /* "cached_list.prev" should be NULL */
6443 assert(object1
->cached_list
.prev
== NULL
);
6444 assert(object2
->cached_list
.prev
== NULL
);
6445 __TRANSPOSE_FIELD(last_alloc
);
6446 __TRANSPOSE_FIELD(sequential
);
6447 __TRANSPOSE_FIELD(pages_created
);
6448 __TRANSPOSE_FIELD(pages_used
);
6449 __TRANSPOSE_FIELD(scan_collisions
);
6450 __TRANSPOSE_FIELD(cow_hint
);
6451 __TRANSPOSE_FIELD(wimg_bits
);
6452 __TRANSPOSE_FIELD(set_cache_attr
);
6453 __TRANSPOSE_FIELD(code_signed
);
6454 object1
->transposed
= TRUE
;
6455 object2
->transposed
= TRUE
;
6456 __TRANSPOSE_FIELD(mapping_in_progress
);
6457 __TRANSPOSE_FIELD(volatile_empty
);
6458 __TRANSPOSE_FIELD(volatile_fault
);
6459 __TRANSPOSE_FIELD(all_reusable
);
6460 assert(object1
->blocked_access
);
6461 assert(object2
->blocked_access
);
6462 assert(object1
->__object2_unused_bits
== 0);
6463 assert(object2
->__object2_unused_bits
== 0);
6465 /* "uplq" refers to the object not its contents (see upl_transpose()) */
6467 assert((object1
->purgable
== VM_PURGABLE_DENY
) || (object1
->objq
.next
== NULL
));
6468 assert((object1
->purgable
== VM_PURGABLE_DENY
) || (object1
->objq
.prev
== NULL
));
6469 assert((object2
->purgable
== VM_PURGABLE_DENY
) || (object2
->objq
.next
== NULL
));
6470 assert((object2
->purgable
== VM_PURGABLE_DENY
) || (object2
->objq
.prev
== NULL
));
6472 #undef __TRANSPOSE_FIELD
6474 retval
= KERN_SUCCESS
;
6480 if (tmp_object
!= VM_OBJECT_NULL
) {
6481 vm_object_unlock(tmp_object
);
6483 * Re-initialize the temporary object to avoid
6484 * deallocating a real pager.
6486 _vm_object_allocate(transpose_size
, tmp_object
);
6487 vm_object_deallocate(tmp_object
);
6488 tmp_object
= VM_OBJECT_NULL
;
6491 if (object1_locked
) {
6492 vm_object_unlock(object1
);
6493 object1_locked
= FALSE
;
6495 if (object2_locked
) {
6496 vm_object_unlock(object2
);
6497 object2_locked
= FALSE
;
6500 vm_object_transpose_count
++;
6507 * vm_object_cluster_size
6509 * Determine how big a cluster we should issue an I/O for...
6511 * Inputs: *start == offset of page needed
6512 * *length == maximum cluster pager can handle
6513 * Outputs: *start == beginning offset of cluster
6514 * *length == length of cluster to try
6516 * The original *start will be encompassed by the cluster
6519 extern int speculative_reads_disabled
;
6522 * Try to always keep these values an even multiple of PAGE_SIZE. We use these values
6523 * to derive min_ph_bytes and max_ph_bytes (IMP: bytes not # of pages) and expect those values to
6524 * always be page-aligned. The derivation could involve operations (e.g. division)
6525 * that could give us non-page-size aligned values if we start out with values that
6526 * are odd multiples of PAGE_SIZE.
6529 unsigned int preheat_max_bytes
= (1024 * 512);
6530 #else /* CONFIG_EMBEDDED */
6531 unsigned int preheat_max_bytes
= MAX_UPL_TRANSFER_BYTES
;
6532 #endif /* CONFIG_EMBEDDED */
6533 unsigned int preheat_min_bytes
= (1024 * 32);
6536 __private_extern__
void
6537 vm_object_cluster_size(vm_object_t object
, vm_object_offset_t
*start
,
6538 vm_size_t
*length
, vm_object_fault_info_t fault_info
, uint32_t *io_streaming
)
6540 vm_size_t pre_heat_size
;
6541 vm_size_t tail_size
;
6542 vm_size_t head_size
;
6543 vm_size_t max_length
;
6544 vm_size_t cluster_size
;
6545 vm_object_offset_t object_size
;
6546 vm_object_offset_t orig_start
;
6547 vm_object_offset_t target_start
;
6548 vm_object_offset_t offset
;
6549 vm_behavior_t behavior
;
6550 boolean_t look_behind
= TRUE
;
6551 boolean_t look_ahead
= TRUE
;
6552 boolean_t isSSD
= FALSE
;
6553 uint32_t throttle_limit
;
6555 int sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
6556 vm_size_t max_ph_size
;
6557 vm_size_t min_ph_size
;
6559 assert( !(*length
& PAGE_MASK
));
6560 assert( !(*start
& PAGE_MASK_64
));
6563 * remember maxiumum length of run requested
6565 max_length
= *length
;
6567 * we'll always return a cluster size of at least
6568 * 1 page, since the original fault must always
6571 *length
= PAGE_SIZE
;
6574 if (speculative_reads_disabled
|| fault_info
== NULL
) {
6576 * no cluster... just fault the page in
6580 orig_start
= *start
;
6581 target_start
= orig_start
;
6582 cluster_size
= round_page(fault_info
->cluster_size
);
6583 behavior
= fault_info
->behavior
;
6585 vm_object_lock(object
);
6587 if (object
->pager
== MEMORY_OBJECT_NULL
)
6588 goto out
; /* pager is gone for this object, nothing more to do */
6590 vnode_pager_get_isSSD(object
->pager
, &isSSD
);
6592 min_ph_size
= round_page(preheat_min_bytes
);
6593 max_ph_size
= round_page(preheat_max_bytes
);
6595 #if !CONFIG_EMBEDDED
6600 if (min_ph_size
& PAGE_MASK_64
) {
6601 min_ph_size
= trunc_page(min_ph_size
);
6604 if (max_ph_size
& PAGE_MASK_64
) {
6605 max_ph_size
= trunc_page(max_ph_size
);
6608 #endif /* !CONFIG_EMBEDDED */
6610 if (min_ph_size
< PAGE_SIZE
)
6611 min_ph_size
= PAGE_SIZE
;
6613 if (max_ph_size
< PAGE_SIZE
)
6614 max_ph_size
= PAGE_SIZE
;
6615 else if (max_ph_size
> MAX_UPL_TRANSFER_BYTES
)
6616 max_ph_size
= MAX_UPL_TRANSFER_BYTES
;
6618 if (max_length
> max_ph_size
)
6619 max_length
= max_ph_size
;
6621 if (max_length
<= PAGE_SIZE
)
6624 if (object
->internal
)
6625 object_size
= object
->vo_size
;
6627 vnode_pager_get_object_size(object
->pager
, &object_size
);
6629 object_size
= round_page_64(object_size
);
6631 if (orig_start
>= object_size
) {
6633 * fault occurred beyond the EOF...
6634 * we need to punt w/o changing the
6639 if (object
->pages_used
> object
->pages_created
) {
6641 * must have wrapped our 32 bit counters
6644 object
->pages_used
= object
->pages_created
= 0;
6646 if ((sequential_run
= object
->sequential
)) {
6647 if (sequential_run
< 0) {
6648 sequential_behavior
= VM_BEHAVIOR_RSEQNTL
;
6649 sequential_run
= 0 - sequential_run
;
6651 sequential_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
6658 behavior
= VM_BEHAVIOR_DEFAULT
;
6660 case VM_BEHAVIOR_DEFAULT
:
6661 if (object
->internal
&& fault_info
->user_tag
== VM_MEMORY_STACK
)
6664 if (sequential_run
>= (3 * PAGE_SIZE
)) {
6665 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6667 if (sequential_behavior
== VM_BEHAVIOR_SEQUENTIAL
)
6668 look_behind
= FALSE
;
6675 if (object
->pages_created
< (20 * (min_ph_size
>> PAGE_SHIFT
))) {
6679 pre_heat_size
= min_ph_size
;
6682 * Linear growth in PH size: The maximum size is max_length...
6683 * this cacluation will result in a size that is neither a
6684 * power of 2 nor a multiple of PAGE_SIZE... so round
6685 * it up to the nearest PAGE_SIZE boundary
6687 pre_heat_size
= (max_length
* (uint64_t)object
->pages_used
) / object
->pages_created
;
6689 if (pre_heat_size
< min_ph_size
)
6690 pre_heat_size
= min_ph_size
;
6692 pre_heat_size
= round_page(pre_heat_size
);
6697 case VM_BEHAVIOR_RANDOM
:
6698 if ((pre_heat_size
= cluster_size
) <= PAGE_SIZE
)
6702 case VM_BEHAVIOR_SEQUENTIAL
:
6703 if ((pre_heat_size
= cluster_size
) == 0)
6704 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6705 look_behind
= FALSE
;
6710 case VM_BEHAVIOR_RSEQNTL
:
6711 if ((pre_heat_size
= cluster_size
) == 0)
6712 pre_heat_size
= sequential_run
+ PAGE_SIZE
;
6719 throttle_limit
= (uint32_t) max_length
;
6720 assert(throttle_limit
== max_length
);
6722 if (vnode_pager_get_throttle_io_limit(object
->pager
, &throttle_limit
) == KERN_SUCCESS
) {
6723 if (max_length
> throttle_limit
)
6724 max_length
= throttle_limit
;
6726 if (pre_heat_size
> max_length
)
6727 pre_heat_size
= max_length
;
6729 if (behavior
== VM_BEHAVIOR_DEFAULT
&& (pre_heat_size
> min_ph_size
)) {
6731 unsigned int consider_free
= vm_page_free_count
+ vm_page_cleaned_count
;
6733 if (consider_free
< vm_page_throttle_limit
) {
6734 pre_heat_size
= trunc_page(pre_heat_size
/ 16);
6735 } else if (consider_free
< vm_page_free_target
) {
6736 pre_heat_size
= trunc_page(pre_heat_size
/ 4);
6739 if (pre_heat_size
< min_ph_size
)
6740 pre_heat_size
= min_ph_size
;
6742 if (look_ahead
== TRUE
) {
6743 if (look_behind
== TRUE
) {
6745 * if we get here its due to a random access...
6746 * so we want to center the original fault address
6747 * within the cluster we will issue... make sure
6748 * to calculate 'head_size' as a multiple of PAGE_SIZE...
6749 * 'pre_heat_size' is a multiple of PAGE_SIZE but not
6750 * necessarily an even number of pages so we need to truncate
6751 * the result to a PAGE_SIZE boundary
6753 head_size
= trunc_page(pre_heat_size
/ 2);
6755 if (target_start
> head_size
)
6756 target_start
-= head_size
;
6761 * 'target_start' at this point represents the beginning offset
6762 * of the cluster we are considering... 'orig_start' will be in
6763 * the center of this cluster if we didn't have to clip the start
6764 * due to running into the start of the file
6767 if ((target_start
+ pre_heat_size
) > object_size
)
6768 pre_heat_size
= (vm_size_t
)(round_page_64(object_size
- target_start
));
6770 * at this point caclulate the number of pages beyond the original fault
6771 * address that we want to consider... this is guaranteed not to extend beyond
6772 * the current EOF...
6774 assert((vm_size_t
)(orig_start
- target_start
) == (orig_start
- target_start
));
6775 tail_size
= pre_heat_size
- (vm_size_t
)(orig_start
- target_start
) - PAGE_SIZE
;
6777 if (pre_heat_size
> target_start
) {
6779 * since pre_heat_size is always smaller then 2^32,
6780 * if it is larger then target_start (a 64 bit value)
6781 * it is safe to clip target_start to 32 bits
6783 pre_heat_size
= (vm_size_t
) target_start
;
6787 assert( !(target_start
& PAGE_MASK_64
));
6788 assert( !(pre_heat_size
& PAGE_MASK_64
));
6790 if (pre_heat_size
<= PAGE_SIZE
)
6793 if (look_behind
== TRUE
) {
6795 * take a look at the pages before the original
6796 * faulting offset... recalculate this in case
6797 * we had to clip 'pre_heat_size' above to keep
6798 * from running past the EOF.
6800 head_size
= pre_heat_size
- tail_size
- PAGE_SIZE
;
6802 for (offset
= orig_start
- PAGE_SIZE_64
; head_size
; offset
-= PAGE_SIZE_64
, head_size
-= PAGE_SIZE
) {
6804 * don't poke below the lowest offset
6806 if (offset
< fault_info
->lo_offset
)
6809 * for external objects or internal objects w/o a pager,
6810 * VM_COMPRESSOR_PAGER_STATE_GET will return VM_EXTERNAL_STATE_UNKNOWN
6812 if (VM_COMPRESSOR_PAGER_STATE_GET(object
, offset
) == VM_EXTERNAL_STATE_ABSENT
) {
6815 if (vm_page_lookup(object
, offset
) != VM_PAGE_NULL
) {
6817 * don't bridge resident pages
6822 *length
+= PAGE_SIZE
;
6825 if (look_ahead
== TRUE
) {
6826 for (offset
= orig_start
+ PAGE_SIZE_64
; tail_size
; offset
+= PAGE_SIZE_64
, tail_size
-= PAGE_SIZE
) {
6828 * don't poke above the highest offset
6830 if (offset
>= fault_info
->hi_offset
)
6832 assert(offset
< object_size
);
6835 * for external objects or internal objects w/o a pager,
6836 * VM_COMPRESSOR_PAGER_STATE_GET will return VM_EXTERNAL_STATE_UNKNOWN
6838 if (VM_COMPRESSOR_PAGER_STATE_GET(object
, offset
) == VM_EXTERNAL_STATE_ABSENT
) {
6841 if (vm_page_lookup(object
, offset
) != VM_PAGE_NULL
) {
6843 * don't bridge resident pages
6847 *length
+= PAGE_SIZE
;
6851 if (*length
> max_length
)
6852 *length
= max_length
;
6854 vm_object_unlock(object
);
6856 DTRACE_VM1(clustersize
, vm_size_t
, *length
);
6861 * Allow manipulation of individual page state. This is actually part of
6862 * the UPL regimen but takes place on the VM object rather than on a UPL
6868 vm_object_offset_t offset
,
6870 ppnum_t
*phys_entry
,
6875 vm_object_lock(object
);
6877 if(ops
& UPL_POP_PHYSICAL
) {
6878 if(object
->phys_contiguous
) {
6880 *phys_entry
= (ppnum_t
)
6881 (object
->vo_shadow_offset
>> PAGE_SHIFT
);
6883 vm_object_unlock(object
);
6884 return KERN_SUCCESS
;
6886 vm_object_unlock(object
);
6887 return KERN_INVALID_OBJECT
;
6890 if(object
->phys_contiguous
) {
6891 vm_object_unlock(object
);
6892 return KERN_INVALID_OBJECT
;
6896 if((dst_page
= vm_page_lookup(object
,offset
)) == VM_PAGE_NULL
) {
6897 vm_object_unlock(object
);
6898 return KERN_FAILURE
;
6901 /* Sync up on getting the busy bit */
6902 if((dst_page
->busy
|| dst_page
->cleaning
) &&
6903 (((ops
& UPL_POP_SET
) &&
6904 (ops
& UPL_POP_BUSY
)) || (ops
& UPL_POP_DUMP
))) {
6905 /* someone else is playing with the page, we will */
6907 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
6911 if (ops
& UPL_POP_DUMP
) {
6912 if (dst_page
->pmapped
== TRUE
)
6913 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(dst_page
));
6915 VM_PAGE_FREE(dst_page
);
6922 /* Get the condition of flags before requested ops */
6923 /* are undertaken */
6925 if(dst_page
->dirty
) *flags
|= UPL_POP_DIRTY
;
6926 if(dst_page
->free_when_done
) *flags
|= UPL_POP_PAGEOUT
;
6927 if(dst_page
->precious
) *flags
|= UPL_POP_PRECIOUS
;
6928 if(dst_page
->absent
) *flags
|= UPL_POP_ABSENT
;
6929 if(dst_page
->busy
) *flags
|= UPL_POP_BUSY
;
6932 /* The caller should have made a call either contingent with */
6933 /* or prior to this call to set UPL_POP_BUSY */
6934 if(ops
& UPL_POP_SET
) {
6935 /* The protection granted with this assert will */
6936 /* not be complete. If the caller violates the */
6937 /* convention and attempts to change page state */
6938 /* without first setting busy we may not see it */
6939 /* because the page may already be busy. However */
6940 /* if such violations occur we will assert sooner */
6942 assert(dst_page
->busy
|| (ops
& UPL_POP_BUSY
));
6943 if (ops
& UPL_POP_DIRTY
) {
6944 SET_PAGE_DIRTY(dst_page
, FALSE
);
6946 if (ops
& UPL_POP_PAGEOUT
) dst_page
->free_when_done
= TRUE
;
6947 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= TRUE
;
6948 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= TRUE
;
6949 if (ops
& UPL_POP_BUSY
) dst_page
->busy
= TRUE
;
6952 if(ops
& UPL_POP_CLR
) {
6953 assert(dst_page
->busy
);
6954 if (ops
& UPL_POP_DIRTY
) dst_page
->dirty
= FALSE
;
6955 if (ops
& UPL_POP_PAGEOUT
) dst_page
->free_when_done
= FALSE
;
6956 if (ops
& UPL_POP_PRECIOUS
) dst_page
->precious
= FALSE
;
6957 if (ops
& UPL_POP_ABSENT
) dst_page
->absent
= FALSE
;
6958 if (ops
& UPL_POP_BUSY
) {
6959 dst_page
->busy
= FALSE
;
6960 PAGE_WAKEUP(dst_page
);
6965 * The physical page number will remain valid
6966 * only if the page is kept busy.
6968 assert(dst_page
->busy
);
6969 *phys_entry
= VM_PAGE_GET_PHYS_PAGE(dst_page
);
6975 vm_object_unlock(object
);
6976 return KERN_SUCCESS
;
6981 * vm_object_range_op offers performance enhancement over
6982 * vm_object_page_op for page_op functions which do not require page
6983 * level state to be returned from the call. Page_op was created to provide
6984 * a low-cost alternative to page manipulation via UPLs when only a single
6985 * page was involved. The range_op call establishes the ability in the _op
6986 * family of functions to work on multiple pages where the lack of page level
6987 * state handling allows the caller to avoid the overhead of the upl structures.
6993 vm_object_offset_t offset_beg
,
6994 vm_object_offset_t offset_end
,
6998 vm_object_offset_t offset
;
7001 if (offset_end
- offset_beg
> (uint32_t) -1) {
7002 /* range is too big and would overflow "*range" */
7003 return KERN_INVALID_ARGUMENT
;
7005 if (object
->resident_page_count
== 0) {
7007 if (ops
& UPL_ROP_PRESENT
) {
7010 *range
= (uint32_t) (offset_end
- offset_beg
);
7011 assert(*range
== (offset_end
- offset_beg
));
7014 return KERN_SUCCESS
;
7016 vm_object_lock(object
);
7018 if (object
->phys_contiguous
) {
7019 vm_object_unlock(object
);
7020 return KERN_INVALID_OBJECT
;
7023 offset
= offset_beg
& ~PAGE_MASK_64
;
7025 while (offset
< offset_end
) {
7026 dst_page
= vm_page_lookup(object
, offset
);
7027 if (dst_page
!= VM_PAGE_NULL
) {
7028 if (ops
& UPL_ROP_DUMP
) {
7029 if (dst_page
->busy
|| dst_page
->cleaning
) {
7031 * someone else is playing with the
7032 * page, we will have to wait
7034 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
7036 * need to relook the page up since it's
7037 * state may have changed while we slept
7038 * it might even belong to a different object
7043 if (dst_page
->laundry
)
7044 vm_pageout_steal_laundry(dst_page
, FALSE
);
7046 if (dst_page
->pmapped
== TRUE
)
7047 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(dst_page
));
7049 VM_PAGE_FREE(dst_page
);
7051 } else if ((ops
& UPL_ROP_ABSENT
)
7052 && (!dst_page
->absent
|| dst_page
->busy
)) {
7055 } else if (ops
& UPL_ROP_PRESENT
)
7058 offset
+= PAGE_SIZE
;
7060 vm_object_unlock(object
);
7063 if (offset
> offset_end
)
7064 offset
= offset_end
;
7065 if(offset
> offset_beg
) {
7066 *range
= (uint32_t) (offset
- offset_beg
);
7067 assert(*range
== (offset
- offset_beg
));
7072 return KERN_SUCCESS
;
7076 * Used to point a pager directly to a range of memory (when the pager may be associated
7077 * with a non-device vnode). Takes a virtual address, an offset, and a size. We currently
7078 * expect that the virtual address will denote the start of a range that is physically contiguous.
7080 kern_return_t
pager_map_to_phys_contiguous(
7081 memory_object_control_t object
,
7082 memory_object_offset_t offset
,
7083 addr64_t base_vaddr
,
7087 boolean_t clobbered_private
;
7088 kern_return_t retval
;
7089 vm_object_t pager_object
;
7091 page_num
= pmap_find_phys(kernel_pmap
, base_vaddr
);
7094 retval
= KERN_FAILURE
;
7098 pager_object
= memory_object_control_to_vm_object(object
);
7100 if (!pager_object
) {
7101 retval
= KERN_FAILURE
;
7105 clobbered_private
= pager_object
->private;
7106 if (pager_object
->private != TRUE
) {
7107 vm_object_lock(pager_object
);
7108 pager_object
->private = TRUE
;
7109 vm_object_unlock(pager_object
);
7111 retval
= vm_object_populate_with_private(pager_object
, offset
, page_num
, size
);
7113 if (retval
!= KERN_SUCCESS
) {
7114 if (pager_object
->private != clobbered_private
) {
7115 vm_object_lock(pager_object
);
7116 pager_object
->private = clobbered_private
;
7117 vm_object_unlock(pager_object
);
7125 uint32_t scan_object_collision
= 0;
7128 vm_object_lock(vm_object_t object
)
7130 if (object
== vm_pageout_scan_wants_object
) {
7131 scan_object_collision
++;
7134 lck_rw_lock_exclusive(&object
->Lock
);
7135 #if DEVELOPMENT || DEBUG
7136 object
->Lock_owner
= current_thread();
7141 vm_object_lock_avoid(vm_object_t object
)
7143 if (object
== vm_pageout_scan_wants_object
) {
7144 scan_object_collision
++;
7151 _vm_object_lock_try(vm_object_t object
)
7155 retval
= lck_rw_try_lock_exclusive(&object
->Lock
);
7156 #if DEVELOPMENT || DEBUG
7158 object
->Lock_owner
= current_thread();
7164 vm_object_lock_try(vm_object_t object
)
7167 * Called from hibernate path so check before blocking.
7169 if (vm_object_lock_avoid(object
) && ml_get_interrupts_enabled() && get_preemption_level()==0) {
7172 return _vm_object_lock_try(object
);
7176 vm_object_lock_shared(vm_object_t object
)
7178 if (vm_object_lock_avoid(object
)) {
7181 lck_rw_lock_shared(&object
->Lock
);
7185 vm_object_lock_yield_shared(vm_object_t object
)
7187 boolean_t retval
= FALSE
, force_yield
= FALSE
;;
7189 vm_object_lock_assert_shared(object
);
7191 force_yield
= vm_object_lock_avoid(object
);
7193 retval
= lck_rw_lock_yield_shared(&object
->Lock
, force_yield
);
7199 vm_object_lock_try_shared(vm_object_t object
)
7201 if (vm_object_lock_avoid(object
)) {
7204 return (lck_rw_try_lock_shared(&object
->Lock
));
7208 vm_object_lock_upgrade(vm_object_t object
)
7211 retval
= lck_rw_lock_shared_to_exclusive(&object
->Lock
);
7212 #if DEVELOPMENT || DEBUG
7214 object
->Lock_owner
= current_thread();
7220 vm_object_unlock(vm_object_t object
)
7222 #if DEVELOPMENT || DEBUG
7223 if (object
->Lock_owner
) {
7224 if (object
->Lock_owner
!= current_thread())
7225 panic("vm_object_unlock: not owner - %p\n", object
);
7226 object
->Lock_owner
= 0;
7229 lck_rw_done(&object
->Lock
);
7233 unsigned int vm_object_change_wimg_mode_count
= 0;
7236 * The object must be locked
7239 vm_object_change_wimg_mode(vm_object_t object
, unsigned int wimg_mode
)
7243 vm_object_lock_assert_exclusive(object
);
7245 vm_object_paging_wait(object
, THREAD_UNINT
);
7247 vm_page_queue_iterate(&object
->memq
, p
, vm_page_t
, listq
) {
7250 pmap_set_cache_attributes(VM_PAGE_GET_PHYS_PAGE(p
), wimg_mode
);
7252 if (wimg_mode
== VM_WIMG_USE_DEFAULT
)
7253 object
->set_cache_attr
= FALSE
;
7255 object
->set_cache_attr
= TRUE
;
7257 object
->wimg_bits
= wimg_mode
;
7259 vm_object_change_wimg_mode_count
++;
7265 * This routine does the "relocation" of previously
7266 * compressed pages belonging to this object that are
7267 * residing in a number of compressed segments into
7268 * a set of compressed segments dedicated to hold
7269 * compressed pages belonging to this object.
7272 extern void *freezer_chead
;
7273 extern char *freezer_compressor_scratch_buf
;
7274 extern int c_freezer_compression_count
;
7275 extern AbsoluteTime c_freezer_last_yield_ts
;
7277 #define MAX_FREE_BATCH 32
7278 #define FREEZER_DUTY_CYCLE_ON_MS 5
7279 #define FREEZER_DUTY_CYCLE_OFF_MS 5
7281 static int c_freezer_should_yield(void);
7285 c_freezer_should_yield()
7287 AbsoluteTime cur_time
;
7290 assert(c_freezer_last_yield_ts
);
7291 clock_get_uptime(&cur_time
);
7293 SUB_ABSOLUTETIME(&cur_time
, &c_freezer_last_yield_ts
);
7294 absolutetime_to_nanoseconds(cur_time
, &nsecs
);
7296 if (nsecs
> 1000 * 1000 * FREEZER_DUTY_CYCLE_ON_MS
)
7303 vm_object_compressed_freezer_done()
7305 vm_compressor_finished_filling(&freezer_chead
);
7310 vm_object_compressed_freezer_pageout(
7314 vm_page_t local_freeq
= NULL
;
7315 int local_freed
= 0;
7316 kern_return_t retval
= KERN_SUCCESS
;
7317 int obj_resident_page_count_snapshot
= 0;
7319 assert(object
!= VM_OBJECT_NULL
);
7320 assert(object
->internal
);
7322 vm_object_lock(object
);
7324 if (!object
->pager_initialized
|| object
->pager
== MEMORY_OBJECT_NULL
) {
7326 if (!object
->pager_initialized
) {
7328 vm_object_collapse(object
, (vm_object_offset_t
) 0, TRUE
);
7330 if (!object
->pager_initialized
)
7331 vm_object_compressor_pager_create(object
);
7334 if (!object
->pager_initialized
|| object
->pager
== MEMORY_OBJECT_NULL
) {
7335 vm_object_unlock(object
);
7340 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
) {
7341 vm_object_offset_t curr_offset
= 0;
7344 * Go through the object and make sure that any
7345 * previously compressed pages are relocated into
7346 * a compressed segment associated with our "freezer_chead".
7348 while (curr_offset
< object
->vo_size
) {
7350 curr_offset
= vm_compressor_pager_next_compressed(object
->pager
, curr_offset
);
7352 if (curr_offset
== (vm_object_offset_t
) -1)
7355 retval
= vm_compressor_pager_relocate(object
->pager
, curr_offset
, &freezer_chead
);
7357 if (retval
!= KERN_SUCCESS
)
7360 curr_offset
+= PAGE_SIZE_64
;
7365 * We can't hold the object lock while heading down into the compressed pager
7366 * layer because we might need the kernel map lock down there to allocate new
7367 * compressor data structures. And if this same object is mapped in the kernel
7368 * and there's a fault on it, then that thread will want the object lock while
7369 * holding the kernel map lock.
7371 * Since we are going to drop/grab the object lock repeatedly, we must make sure
7372 * we won't be stuck in an infinite loop if the same page(s) keep getting
7373 * decompressed. So we grab a snapshot of the number of pages in the object and
7374 * we won't process any more than that number of pages.
7377 obj_resident_page_count_snapshot
= object
->resident_page_count
;
7379 vm_object_activity_begin(object
);
7381 while ((obj_resident_page_count_snapshot
--) && !vm_page_queue_empty(&object
->memq
)) {
7383 p
= (vm_page_t
)vm_page_queue_first(&object
->memq
);
7385 KERNEL_DEBUG(0xe0430004 | DBG_FUNC_START
, object
, local_freed
, 0, 0, 0);
7387 vm_page_lockspin_queues();
7389 if (p
->cleaning
|| p
->fictitious
|| p
->busy
|| p
->absent
|| p
->unusual
|| p
->error
|| VM_PAGE_WIRED(p
)) {
7391 vm_page_unlock_queues();
7393 KERNEL_DEBUG(0xe0430004 | DBG_FUNC_END
, object
, local_freed
, 1, 0, 0);
7395 vm_page_queue_remove(&object
->memq
, p
, vm_page_t
, listq
);
7396 vm_page_queue_enter(&object
->memq
, p
, vm_page_t
, listq
);
7401 if (p
->pmapped
== TRUE
) {
7402 int refmod_state
, pmap_flags
;
7404 if (p
->dirty
|| p
->precious
) {
7405 pmap_flags
= PMAP_OPTIONS_COMPRESSOR
;
7407 pmap_flags
= PMAP_OPTIONS_COMPRESSOR_IFF_MODIFIED
;
7410 refmod_state
= pmap_disconnect_options(VM_PAGE_GET_PHYS_PAGE(p
), pmap_flags
, NULL
);
7411 if (refmod_state
& VM_MEM_MODIFIED
) {
7412 SET_PAGE_DIRTY(p
, FALSE
);
7416 if (p
->dirty
== FALSE
&& p
->precious
== FALSE
) {
7418 * Clean and non-precious page.
7420 vm_page_unlock_queues();
7423 KERNEL_DEBUG(0xe0430004 | DBG_FUNC_END
, object
, local_freed
, 2, 0, 0);
7428 vm_pageout_steal_laundry(p
, TRUE
);
7430 vm_page_queues_remove(p
, TRUE
);
7432 vm_page_unlock_queues();
7436 * In case the compressor fails to compress this page, we need it at
7437 * the back of the object memq so that we don't keep trying to process it.
7438 * Make the move here while we have the object lock held.
7441 vm_page_queue_remove(&object
->memq
, p
, vm_page_t
, listq
);
7442 vm_page_queue_enter(&object
->memq
, p
, vm_page_t
, listq
);
7445 * Grab an activity_in_progress here for vm_pageout_compress_page() to consume.
7447 * Mark the page busy so no one messes with it while we have the object lock dropped.
7452 vm_object_activity_begin(object
);
7454 vm_object_unlock(object
);
7457 * arg3 == FALSE tells vm_pageout_compress_page that we don't hold the object lock and the pager may not be initialized.
7459 if (vm_pageout_compress_page(&freezer_chead
, freezer_compressor_scratch_buf
, p
, FALSE
) == KERN_SUCCESS
) {
7461 * page has already been un-tabled from the object via 'vm_page_remove'
7463 p
->snext
= local_freeq
;
7467 if (local_freed
>= MAX_FREE_BATCH
) {
7469 vm_page_free_list(local_freeq
, TRUE
);
7474 c_freezer_compression_count
++;
7476 KERNEL_DEBUG(0xe0430004 | DBG_FUNC_END
, object
, local_freed
, 0, 0, 0);
7478 if (local_freed
== 0 && c_freezer_should_yield()) {
7480 thread_yield_internal(FREEZER_DUTY_CYCLE_OFF_MS
);
7481 clock_get_uptime(&c_freezer_last_yield_ts
);
7484 vm_object_lock(object
);
7488 vm_page_free_list(local_freeq
, TRUE
);
7494 vm_object_activity_end(object
);
7496 vm_object_unlock(object
);
7498 if (c_freezer_should_yield()) {
7500 thread_yield_internal(FREEZER_DUTY_CYCLE_OFF_MS
);
7501 clock_get_uptime(&c_freezer_last_yield_ts
);
7505 #endif /* CONFIG_FREEZE */
7513 struct vm_pageout_queue
*iq
;
7515 if (!VM_CONFIG_COMPRESSOR_IS_PRESENT
)
7518 iq
= &vm_pageout_queue_internal
;
7520 assert(object
!= VM_OBJECT_NULL
);
7522 vm_object_lock(object
);
7524 if (!object
->internal
||
7525 object
->terminating
||
7527 vm_object_unlock(object
);
7531 if (!object
->pager_initialized
|| object
->pager
== MEMORY_OBJECT_NULL
) {
7533 if (!object
->pager_initialized
) {
7535 vm_object_collapse(object
, (vm_object_offset_t
) 0, TRUE
);
7537 if (!object
->pager_initialized
)
7538 vm_object_compressor_pager_create(object
);
7541 if (!object
->pager_initialized
|| object
->pager
== MEMORY_OBJECT_NULL
) {
7542 vm_object_unlock(object
);
7548 next
= (vm_page_t
)vm_page_queue_first(&object
->memq
);
7550 while (!vm_page_queue_end(&object
->memq
, (vm_page_queue_entry_t
)next
)) {
7552 next
= (vm_page_t
)vm_page_queue_next(&next
->listq
);
7554 assert(p
->vm_page_q_state
!= VM_PAGE_ON_FREE_Q
);
7556 if ((p
->vm_page_q_state
== VM_PAGE_ON_THROTTLED_Q
) ||
7565 * Page is already being cleaned or can't be cleaned.
7570 /* Throw to the pageout queue */
7572 vm_page_lockspin_queues();
7574 if (vm_compressor_low_on_space()) {
7575 vm_page_unlock_queues();
7579 if (VM_PAGE_Q_THROTTLED(iq
)) {
7581 iq
->pgo_draining
= TRUE
;
7583 assert_wait((event_t
) (&iq
->pgo_laundry
+ 1),
7584 THREAD_INTERRUPTIBLE
);
7585 vm_page_unlock_queues();
7586 vm_object_unlock(object
);
7588 thread_block(THREAD_CONTINUE_NULL
);
7590 vm_object_lock(object
);
7594 assert(!p
->fictitious
);
7597 assert(!p
->unusual
);
7599 assert(!VM_PAGE_WIRED(p
));
7600 assert(!p
->cleaning
);
7602 if (p
->pmapped
== TRUE
) {
7607 * Tell pmap the page should be accounted
7608 * for as "compressed" if it's been modified.
7611 PMAP_OPTIONS_COMPRESSOR_IFF_MODIFIED
;
7612 if (p
->dirty
|| p
->precious
) {
7614 * We already know it's been modified,
7615 * so tell pmap to account for it
7618 pmap_options
= PMAP_OPTIONS_COMPRESSOR
;
7620 refmod_state
= pmap_disconnect_options(VM_PAGE_GET_PHYS_PAGE(p
),
7623 if (refmod_state
& VM_MEM_MODIFIED
) {
7624 SET_PAGE_DIRTY(p
, FALSE
);
7628 if (!p
->dirty
&& !p
->precious
) {
7629 vm_page_unlock_queues();
7633 vm_page_queues_remove(p
, TRUE
);
7635 vm_pageout_cluster(p
);
7637 vm_page_unlock_queues();
7639 vm_object_unlock(object
);
7645 vm_page_request_reprioritize(vm_object_t o
, uint64_t blkno
, uint32_t len
, int prio
)
7647 io_reprioritize_req_t req
;
7648 struct vnode
*devvp
= NULL
;
7650 if(vnode_pager_get_object_devvp(o
->pager
, (uintptr_t *)&devvp
) != KERN_SUCCESS
)
7654 * Create the request for I/O reprioritization.
7655 * We use the noblock variant of zalloc because we're holding the object
7656 * lock here and we could cause a deadlock in low memory conditions.
7658 req
= (io_reprioritize_req_t
)zalloc_noblock(io_reprioritize_req_zone
);
7663 req
->priority
= prio
;
7666 /* Insert request into the reprioritization list */
7667 IO_REPRIORITIZE_LIST_LOCK();
7668 queue_enter(&io_reprioritize_list
, req
, io_reprioritize_req_t
, io_reprioritize_list
);
7669 IO_REPRIORITIZE_LIST_UNLOCK();
7671 /* Wakeup reprioritize thread */
7672 IO_REPRIO_THREAD_WAKEUP();
7678 vm_decmp_upl_reprioritize(upl_t upl
, int prio
)
7682 io_reprioritize_req_t req
;
7683 struct vnode
*devvp
= NULL
;
7687 uint64_t *io_upl_reprio_info
;
7690 if ((upl
->flags
& UPL_TRACKED_BY_OBJECT
) == 0 || (upl
->flags
& UPL_EXPEDITE_SUPPORTED
) == 0)
7694 * We dont want to perform any allocations with the upl lock held since that might
7695 * result in a deadlock. If the system is low on memory, the pageout thread would
7696 * try to pageout stuff and might wait on this lock. If we are waiting for the memory to
7697 * be freed up by the pageout thread, it would be a deadlock.
7701 /* First step is just to get the size of the upl to find out how big the reprio info is */
7702 if(!upl_try_lock(upl
))
7705 if (upl
->decmp_io_upl
== NULL
) {
7706 /* The real I/O upl was destroyed by the time we came in here. Nothing to do. */
7711 io_upl
= upl
->decmp_io_upl
;
7712 assert((io_upl
->flags
& UPL_DECMP_REAL_IO
) != 0);
7713 io_upl_size
= io_upl
->size
;
7716 /* Now perform the allocation */
7717 io_upl_reprio_info
= (uint64_t *)kalloc(sizeof(uint64_t) * (io_upl_size
/ PAGE_SIZE
));
7718 if (io_upl_reprio_info
== NULL
)
7721 /* Now again take the lock, recheck the state and grab out the required info */
7722 if(!upl_try_lock(upl
))
7725 if (upl
->decmp_io_upl
== NULL
|| upl
->decmp_io_upl
!= io_upl
) {
7726 /* The real I/O upl was destroyed by the time we came in here. Nothing to do. */
7730 memcpy(io_upl_reprio_info
, io_upl
->upl_reprio_info
, sizeof(uint64_t) * (io_upl_size
/ PAGE_SIZE
));
7732 /* Get the VM object for this UPL */
7733 if (io_upl
->flags
& UPL_SHADOWED
) {
7734 object
= io_upl
->map_object
->shadow
;
7736 object
= io_upl
->map_object
;
7739 /* Get the dev vnode ptr for this object */
7740 if(!object
|| !object
->pager
||
7741 vnode_pager_get_object_devvp(object
->pager
, (uintptr_t *)&devvp
) != KERN_SUCCESS
) {
7748 /* Now we have all the information needed to do the expedite */
7751 while (offset
< io_upl_size
) {
7752 blkno
= io_upl_reprio_info
[(offset
/ PAGE_SIZE
)] & UPL_REPRIO_INFO_MASK
;
7753 len
= (io_upl_reprio_info
[(offset
/ PAGE_SIZE
)] >> UPL_REPRIO_INFO_SHIFT
) & UPL_REPRIO_INFO_MASK
;
7756 * This implementation may cause some spurious expedites due to the
7757 * fact that we dont cleanup the blkno & len from the upl_reprio_info
7758 * even after the I/O is complete.
7761 if (blkno
!= 0 && len
!= 0) {
7762 /* Create the request for I/O reprioritization */
7763 req
= (io_reprioritize_req_t
)zalloc(io_reprioritize_req_zone
);
7764 assert(req
!= NULL
);
7767 req
->priority
= prio
;
7770 /* Insert request into the reprioritization list */
7771 IO_REPRIORITIZE_LIST_LOCK();
7772 queue_enter(&io_reprioritize_list
, req
, io_reprioritize_req_t
, io_reprioritize_list
);
7773 IO_REPRIORITIZE_LIST_UNLOCK();
7777 offset
+= PAGE_SIZE
;
7781 /* Wakeup reprioritize thread */
7782 IO_REPRIO_THREAD_WAKEUP();
7785 kfree(io_upl_reprio_info
, sizeof(uint64_t) * (io_upl_size
/ PAGE_SIZE
));
7790 vm_page_handle_prio_inversion(vm_object_t o
, vm_page_t m
)
7793 upl_page_info_t
*pl
;
7794 unsigned int i
, num_pages
;
7797 cur_tier
= proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO
);
7800 Scan through all UPLs associated with the object to find the
7801 UPL containing the contended page.
7803 queue_iterate(&o
->uplq
, upl
, upl_t
, uplq
) {
7804 if (((upl
->flags
& UPL_EXPEDITE_SUPPORTED
) == 0) || upl
->upl_priority
<= cur_tier
)
7806 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
7807 num_pages
= (upl
->size
/ PAGE_SIZE
);
7810 For each page in the UPL page list, see if it matches the contended
7811 page and was issued as a low prio I/O.
7813 for(i
=0; i
< num_pages
; i
++) {
7814 if(UPL_PAGE_PRESENT(pl
,i
) && VM_PAGE_GET_PHYS_PAGE(m
) == pl
[i
].phys_addr
) {
7815 if ((upl
->flags
& UPL_DECMP_REQ
) && upl
->decmp_io_upl
) {
7816 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_PAGE_EXPEDITE
)) | DBG_FUNC_NONE
, VM_KERNEL_UNSLIDE_OR_PERM(upl
->upl_creator
), VM_KERNEL_UNSLIDE_OR_PERM(m
),
7817 VM_KERNEL_UNSLIDE_OR_PERM(upl
), upl
->upl_priority
, 0);
7818 vm_decmp_upl_reprioritize(upl
, cur_tier
);
7821 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_PAGE_EXPEDITE
)) | DBG_FUNC_NONE
, VM_KERNEL_UNSLIDE_OR_PERM(upl
->upl_creator
), VM_KERNEL_UNSLIDE_OR_PERM(m
),
7822 upl
->upl_reprio_info
[i
], upl
->upl_priority
, 0);
7823 if (UPL_REPRIO_INFO_BLKNO(upl
, i
) != 0 && UPL_REPRIO_INFO_LEN(upl
, i
) != 0)
7824 vm_page_request_reprioritize(o
, UPL_REPRIO_INFO_BLKNO(upl
, i
), UPL_REPRIO_INFO_LEN(upl
, i
), cur_tier
);
7828 /* Check if we found any hits */
7837 vm_page_sleep(vm_object_t o
, vm_page_t m
, int interruptible
)
7841 KERNEL_DEBUG((MACHDBG_CODE(DBG_MACH_VM
, VM_PAGE_SLEEP
)) | DBG_FUNC_START
, o
, m
, 0, 0, 0);
7843 if (o
->io_tracking
&& ((m
->busy
== TRUE
) || (m
->cleaning
== TRUE
) || VM_PAGE_WIRED(m
))) {
7845 Indicates page is busy due to an I/O. Issue a reprioritize request if necessary.
7847 vm_page_handle_prio_inversion(o
,m
);
7850 ret
= thread_sleep_vm_object(o
, m
, interruptible
);
7851 KERNEL_DEBUG((MACHDBG_CODE(DBG_MACH_VM
, VM_PAGE_SLEEP
)) | DBG_FUNC_END
, o
, m
, 0, 0, 0);
7856 io_reprioritize_thread(void *param __unused
, wait_result_t wr __unused
)
7858 io_reprioritize_req_t req
= NULL
;
7862 IO_REPRIORITIZE_LIST_LOCK();
7863 if (queue_empty(&io_reprioritize_list
)) {
7864 IO_REPRIORITIZE_LIST_UNLOCK();
7868 queue_remove_first(&io_reprioritize_list
, req
, io_reprioritize_req_t
, io_reprioritize_list
);
7869 IO_REPRIORITIZE_LIST_UNLOCK();
7871 vnode_pager_issue_reprioritize_io(req
->devvp
, req
->blkno
, req
->len
, req
->priority
);
7872 zfree(io_reprioritize_req_zone
, req
);
7875 IO_REPRIO_THREAD_CONTINUATION();