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