2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * File: vm/vm_pageout.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
63 * The proverbial page-out daemon.
69 #include <mach_pagemap.h>
70 #include <mach_cluster_stats.h>
72 #include <mach/mach_types.h>
73 #include <mach/memory_object.h>
74 #include <mach/memory_object_default.h>
75 #include <mach/memory_object_control_server.h>
76 #include <mach/mach_host_server.h>
78 #include <mach/vm_map.h>
79 #include <mach/vm_param.h>
80 #include <mach/vm_statistics.h>
83 #include <kern/kern_types.h>
84 #include <kern/counters.h>
85 #include <kern/host_statistics.h>
86 #include <kern/machine.h>
87 #include <kern/misc_protos.h>
88 #include <kern/sched.h>
89 #include <kern/thread.h>
91 #include <kern/kalloc.h>
93 #include <machine/vm_tuning.h>
94 #include <machine/commpage.h>
97 #include <vm/vm_compressor_pager.h>
98 #include <vm/vm_fault.h>
99 #include <vm/vm_map.h>
100 #include <vm/vm_object.h>
101 #include <vm/vm_page.h>
102 #include <vm/vm_pageout.h>
103 #include <vm/vm_protos.h> /* must be last */
104 #include <vm/memory_object.h>
105 #include <vm/vm_purgeable_internal.h>
106 #include <vm/vm_shared_region.h>
107 #include <vm/vm_compressor.h>
109 #if CONFIG_PHANTOM_CACHE
110 #include <vm/vm_phantom_cache.h>
115 #include <libkern/crypto/aes.h>
116 extern u_int32_t
random(void); /* from <libkern/libkern.h> */
121 #include <libkern/OSDebug.h>
124 extern void m_drain(void);
126 #if VM_PRESSURE_EVENTS
127 extern unsigned int memorystatus_available_pages
;
128 extern unsigned int memorystatus_available_pages_pressure
;
129 extern unsigned int memorystatus_available_pages_critical
;
130 extern unsigned int memorystatus_frozen_count
;
131 extern unsigned int memorystatus_suspended_count
;
133 extern vm_pressure_level_t memorystatus_vm_pressure_level
;
134 int memorystatus_purge_on_warning
= 2;
135 int memorystatus_purge_on_urgent
= 5;
136 int memorystatus_purge_on_critical
= 8;
138 void vm_pressure_response(void);
139 boolean_t vm_pressure_thread_running
= FALSE
;
140 extern void consider_vm_pressure_events(void);
142 #define MEMORYSTATUS_SUSPENDED_THRESHOLD 4
143 #endif /* VM_PRESSURE_EVENTS */
145 boolean_t vm_pressure_changed
= FALSE
;
147 #ifndef VM_PAGEOUT_BURST_ACTIVE_THROTTLE /* maximum iterations of the active queue to move pages to inactive */
148 #define VM_PAGEOUT_BURST_ACTIVE_THROTTLE 100
151 #ifndef VM_PAGEOUT_BURST_INACTIVE_THROTTLE /* maximum iterations of the inactive queue w/o stealing/cleaning a page */
152 #define VM_PAGEOUT_BURST_INACTIVE_THROTTLE 4096
155 #ifndef VM_PAGEOUT_DEADLOCK_RELIEF
156 #define VM_PAGEOUT_DEADLOCK_RELIEF 100 /* number of pages to move to break deadlock */
159 #ifndef VM_PAGEOUT_INACTIVE_RELIEF
160 #define VM_PAGEOUT_INACTIVE_RELIEF 50 /* minimum number of pages to move to the inactive q */
163 #ifndef VM_PAGE_LAUNDRY_MAX
164 #define VM_PAGE_LAUNDRY_MAX 128UL /* maximum pageouts on a given pageout queue */
165 #endif /* VM_PAGEOUT_LAUNDRY_MAX */
167 #ifndef VM_PAGEOUT_BURST_WAIT
168 #define VM_PAGEOUT_BURST_WAIT 10 /* milliseconds */
169 #endif /* VM_PAGEOUT_BURST_WAIT */
171 #ifndef VM_PAGEOUT_EMPTY_WAIT
172 #define VM_PAGEOUT_EMPTY_WAIT 200 /* milliseconds */
173 #endif /* VM_PAGEOUT_EMPTY_WAIT */
175 #ifndef VM_PAGEOUT_DEADLOCK_WAIT
176 #define VM_PAGEOUT_DEADLOCK_WAIT 300 /* milliseconds */
177 #endif /* VM_PAGEOUT_DEADLOCK_WAIT */
179 #ifndef VM_PAGEOUT_IDLE_WAIT
180 #define VM_PAGEOUT_IDLE_WAIT 10 /* milliseconds */
181 #endif /* VM_PAGEOUT_IDLE_WAIT */
183 #ifndef VM_PAGEOUT_SWAP_WAIT
184 #define VM_PAGEOUT_SWAP_WAIT 50 /* milliseconds */
185 #endif /* VM_PAGEOUT_SWAP_WAIT */
187 #ifndef VM_PAGEOUT_PRESSURE_PAGES_CONSIDERED
188 #define VM_PAGEOUT_PRESSURE_PAGES_CONSIDERED 1000 /* maximum pages considered before we issue a pressure event */
189 #endif /* VM_PAGEOUT_PRESSURE_PAGES_CONSIDERED */
191 #ifndef VM_PAGEOUT_PRESSURE_EVENT_MONITOR_SECS
192 #define VM_PAGEOUT_PRESSURE_EVENT_MONITOR_SECS 5 /* seconds */
193 #endif /* VM_PAGEOUT_PRESSURE_EVENT_MONITOR_SECS */
195 unsigned int vm_page_speculative_q_age_ms
= VM_PAGE_SPECULATIVE_Q_AGE_MS
;
196 unsigned int vm_page_speculative_percentage
= 5;
198 #ifndef VM_PAGE_SPECULATIVE_TARGET
199 #define VM_PAGE_SPECULATIVE_TARGET(total) ((total) * 1 / (100 / vm_page_speculative_percentage))
200 #endif /* VM_PAGE_SPECULATIVE_TARGET */
203 #ifndef VM_PAGE_INACTIVE_HEALTHY_LIMIT
204 #define VM_PAGE_INACTIVE_HEALTHY_LIMIT(total) ((total) * 1 / 200)
205 #endif /* VM_PAGE_INACTIVE_HEALTHY_LIMIT */
209 * To obtain a reasonable LRU approximation, the inactive queue
210 * needs to be large enough to give pages on it a chance to be
211 * referenced a second time. This macro defines the fraction
212 * of active+inactive pages that should be inactive.
213 * The pageout daemon uses it to update vm_page_inactive_target.
215 * If vm_page_free_count falls below vm_page_free_target and
216 * vm_page_inactive_count is below vm_page_inactive_target,
217 * then the pageout daemon starts running.
220 #ifndef VM_PAGE_INACTIVE_TARGET
221 #define VM_PAGE_INACTIVE_TARGET(avail) ((avail) * 1 / 2)
222 #endif /* VM_PAGE_INACTIVE_TARGET */
225 * Once the pageout daemon starts running, it keeps going
226 * until vm_page_free_count meets or exceeds vm_page_free_target.
229 #ifndef VM_PAGE_FREE_TARGET
230 #define VM_PAGE_FREE_TARGET(free) (15 + (free) / 80)
231 #endif /* VM_PAGE_FREE_TARGET */
235 * The pageout daemon always starts running once vm_page_free_count
236 * falls below vm_page_free_min.
239 #ifndef VM_PAGE_FREE_MIN
240 #define VM_PAGE_FREE_MIN(free) (10 + (free) / 100)
241 #endif /* VM_PAGE_FREE_MIN */
243 #define VM_PAGE_FREE_RESERVED_LIMIT 1700
244 #define VM_PAGE_FREE_MIN_LIMIT 3500
245 #define VM_PAGE_FREE_TARGET_LIMIT 4000
248 * When vm_page_free_count falls below vm_page_free_reserved,
249 * only vm-privileged threads can allocate pages. vm-privilege
250 * allows the pageout daemon and default pager (and any other
251 * associated threads needed for default pageout) to continue
252 * operation by dipping into the reserved pool of pages.
255 #ifndef VM_PAGE_FREE_RESERVED
256 #define VM_PAGE_FREE_RESERVED(n) \
257 ((unsigned) (6 * VM_PAGE_LAUNDRY_MAX) + (n))
258 #endif /* VM_PAGE_FREE_RESERVED */
261 * When we dequeue pages from the inactive list, they are
262 * reactivated (ie, put back on the active queue) if referenced.
263 * However, it is possible to starve the free list if other
264 * processors are referencing pages faster than we can turn off
265 * the referenced bit. So we limit the number of reactivations
266 * we will make per call of vm_pageout_scan().
268 #define VM_PAGE_REACTIVATE_LIMIT_MAX 20000
269 #ifndef VM_PAGE_REACTIVATE_LIMIT
270 #define VM_PAGE_REACTIVATE_LIMIT(avail) (MAX((avail) * 1 / 20,VM_PAGE_REACTIVATE_LIMIT_MAX))
271 #endif /* VM_PAGE_REACTIVATE_LIMIT */
272 #define VM_PAGEOUT_INACTIVE_FORCE_RECLAIM 100
275 extern boolean_t hibernate_cleaning_in_progress
;
278 * Exported variable used to broadcast the activation of the pageout scan
279 * Working Set uses this to throttle its use of pmap removes. In this
280 * way, code which runs within memory in an uncontested context does
281 * not keep encountering soft faults.
284 unsigned int vm_pageout_scan_event_counter
= 0;
287 * Forward declarations for internal routines.
290 struct vm_pageout_queue
*q
;
296 #if VM_PRESSURE_EVENTS
297 void vm_pressure_thread(void);
299 boolean_t
VM_PRESSURE_NORMAL_TO_WARNING(void);
300 boolean_t
VM_PRESSURE_WARNING_TO_CRITICAL(void);
302 boolean_t
VM_PRESSURE_WARNING_TO_NORMAL(void);
303 boolean_t
VM_PRESSURE_CRITICAL_TO_WARNING(void);
305 static void vm_pageout_garbage_collect(int);
306 static void vm_pageout_iothread_continue(struct vm_pageout_queue
*);
307 static void vm_pageout_iothread_external(void);
308 static void vm_pageout_iothread_internal(struct cq
*cq
);
309 static void vm_pageout_adjust_io_throttles(struct vm_pageout_queue
*, struct vm_pageout_queue
*, boolean_t
);
311 extern void vm_pageout_continue(void);
312 extern void vm_pageout_scan(void);
314 static thread_t vm_pageout_external_iothread
= THREAD_NULL
;
315 static thread_t vm_pageout_internal_iothread
= THREAD_NULL
;
317 unsigned int vm_pageout_reserved_internal
= 0;
318 unsigned int vm_pageout_reserved_really
= 0;
320 unsigned int vm_pageout_swap_wait
= 0;
321 unsigned int vm_pageout_idle_wait
= 0; /* milliseconds */
322 unsigned int vm_pageout_empty_wait
= 0; /* milliseconds */
323 unsigned int vm_pageout_burst_wait
= 0; /* milliseconds */
324 unsigned int vm_pageout_deadlock_wait
= 0; /* milliseconds */
325 unsigned int vm_pageout_deadlock_relief
= 0;
326 unsigned int vm_pageout_inactive_relief
= 0;
327 unsigned int vm_pageout_burst_active_throttle
= 0;
328 unsigned int vm_pageout_burst_inactive_throttle
= 0;
330 int vm_upl_wait_for_pages
= 0;
334 * These variables record the pageout daemon's actions:
335 * how many pages it looks at and what happens to those pages.
336 * No locking needed because only one thread modifies the variables.
339 unsigned int vm_pageout_active
= 0; /* debugging */
340 unsigned int vm_pageout_active_busy
= 0; /* debugging */
341 unsigned int vm_pageout_inactive
= 0; /* debugging */
342 unsigned int vm_pageout_inactive_throttled
= 0; /* debugging */
343 unsigned int vm_pageout_inactive_forced
= 0; /* debugging */
344 unsigned int vm_pageout_inactive_nolock
= 0; /* debugging */
345 unsigned int vm_pageout_inactive_avoid
= 0; /* debugging */
346 unsigned int vm_pageout_inactive_busy
= 0; /* debugging */
347 unsigned int vm_pageout_inactive_error
= 0; /* debugging */
348 unsigned int vm_pageout_inactive_absent
= 0; /* debugging */
349 unsigned int vm_pageout_inactive_notalive
= 0; /* debugging */
350 unsigned int vm_pageout_inactive_used
= 0; /* debugging */
351 unsigned int vm_pageout_cache_evicted
= 0; /* debugging */
352 unsigned int vm_pageout_inactive_clean
= 0; /* debugging */
353 unsigned int vm_pageout_speculative_clean
= 0; /* debugging */
355 unsigned int vm_pageout_freed_from_cleaned
= 0;
356 unsigned int vm_pageout_freed_from_speculative
= 0;
357 unsigned int vm_pageout_freed_from_inactive_clean
= 0;
359 unsigned int vm_pageout_enqueued_cleaned_from_inactive_clean
= 0;
360 unsigned int vm_pageout_enqueued_cleaned_from_inactive_dirty
= 0;
362 unsigned int vm_pageout_cleaned_reclaimed
= 0; /* debugging; how many cleaned pages are reclaimed by the pageout scan */
363 unsigned int vm_pageout_cleaned_reactivated
= 0; /* debugging; how many cleaned pages are found to be referenced on pageout (and are therefore reactivated) */
364 unsigned int vm_pageout_cleaned_reference_reactivated
= 0;
365 unsigned int vm_pageout_cleaned_volatile_reactivated
= 0;
366 unsigned int vm_pageout_cleaned_fault_reactivated
= 0;
367 unsigned int vm_pageout_cleaned_commit_reactivated
= 0; /* debugging; how many cleaned pages are found to be referenced on commit (and are therefore reactivated) */
368 unsigned int vm_pageout_cleaned_busy
= 0;
369 unsigned int vm_pageout_cleaned_nolock
= 0;
371 unsigned int vm_pageout_inactive_dirty_internal
= 0; /* debugging */
372 unsigned int vm_pageout_inactive_dirty_external
= 0; /* debugging */
373 unsigned int vm_pageout_inactive_deactivated
= 0; /* debugging */
374 unsigned int vm_pageout_inactive_anonymous
= 0; /* debugging */
375 unsigned int vm_pageout_dirty_no_pager
= 0; /* debugging */
376 unsigned int vm_pageout_purged_objects
= 0; /* debugging */
377 unsigned int vm_stat_discard
= 0; /* debugging */
378 unsigned int vm_stat_discard_sent
= 0; /* debugging */
379 unsigned int vm_stat_discard_failure
= 0; /* debugging */
380 unsigned int vm_stat_discard_throttle
= 0; /* debugging */
381 unsigned int vm_pageout_reactivation_limit_exceeded
= 0; /* debugging */
382 unsigned int vm_pageout_catch_ups
= 0; /* debugging */
383 unsigned int vm_pageout_inactive_force_reclaim
= 0; /* debugging */
385 unsigned int vm_pageout_scan_reclaimed_throttled
= 0;
386 unsigned int vm_pageout_scan_active_throttled
= 0;
387 unsigned int vm_pageout_scan_inactive_throttled_internal
= 0;
388 unsigned int vm_pageout_scan_inactive_throttled_external
= 0;
389 unsigned int vm_pageout_scan_throttle
= 0; /* debugging */
390 unsigned int vm_pageout_scan_burst_throttle
= 0; /* debugging */
391 unsigned int vm_pageout_scan_empty_throttle
= 0; /* debugging */
392 unsigned int vm_pageout_scan_swap_throttle
= 0; /* debugging */
393 unsigned int vm_pageout_scan_deadlock_detected
= 0; /* debugging */
394 unsigned int vm_pageout_scan_active_throttle_success
= 0; /* debugging */
395 unsigned int vm_pageout_scan_inactive_throttle_success
= 0; /* debugging */
396 unsigned int vm_pageout_inactive_external_forced_jetsam_count
= 0; /* debugging */
397 unsigned int vm_page_speculative_count_drifts
= 0;
398 unsigned int vm_page_speculative_count_drift_max
= 0;
402 * Backing store throttle when BS is exhausted
404 unsigned int vm_backing_store_low
= 0;
406 unsigned int vm_pageout_out_of_line
= 0;
407 unsigned int vm_pageout_in_place
= 0;
409 unsigned int vm_page_steal_pageout_page
= 0;
413 * counters and statistics...
415 unsigned long vm_page_decrypt_counter
= 0;
416 unsigned long vm_page_decrypt_for_upl_counter
= 0;
417 unsigned long vm_page_encrypt_counter
= 0;
418 unsigned long vm_page_encrypt_abort_counter
= 0;
419 unsigned long vm_page_encrypt_already_encrypted_counter
= 0;
420 boolean_t vm_pages_encrypted
= FALSE
; /* are there encrypted pages ? */
422 struct vm_pageout_queue vm_pageout_queue_internal
;
423 struct vm_pageout_queue vm_pageout_queue_external
;
425 unsigned int vm_page_speculative_target
= 0;
427 vm_object_t vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
429 boolean_t (* volatile consider_buffer_cache_collect
)(int) = NULL
;
431 #if DEVELOPMENT || DEBUG
432 unsigned long vm_cs_validated_resets
= 0;
435 int vm_debug_events
= 0;
437 #if CONFIG_MEMORYSTATUS
439 extern boolean_t
memorystatus_idle_exit_from_VM(void);
441 extern boolean_t
memorystatus_kill_on_VM_page_shortage(boolean_t async
);
442 extern void memorystatus_on_pageout_scan_end(void);
445 boolean_t vm_page_compressions_failing
= FALSE
;
448 * Routine: vm_backing_store_disable
450 * Suspend non-privileged threads wishing to extend
451 * backing store when we are low on backing store
452 * (Synchronized by caller)
455 vm_backing_store_disable(
459 vm_backing_store_low
= 1;
461 if(vm_backing_store_low
) {
462 vm_backing_store_low
= 0;
463 thread_wakeup((event_t
) &vm_backing_store_low
);
469 #if MACH_CLUSTER_STATS
470 unsigned long vm_pageout_cluster_dirtied
= 0;
471 unsigned long vm_pageout_cluster_cleaned
= 0;
472 unsigned long vm_pageout_cluster_collisions
= 0;
473 unsigned long vm_pageout_cluster_clusters
= 0;
474 unsigned long vm_pageout_cluster_conversions
= 0;
475 unsigned long vm_pageout_target_collisions
= 0;
476 unsigned long vm_pageout_target_page_dirtied
= 0;
477 unsigned long vm_pageout_target_page_freed
= 0;
478 #define CLUSTER_STAT(clause) clause
479 #else /* MACH_CLUSTER_STATS */
480 #define CLUSTER_STAT(clause)
481 #endif /* MACH_CLUSTER_STATS */
484 * Routine: vm_pageout_object_terminate
486 * Destroy the pageout_object, and perform all of the
487 * required cleanup actions.
490 * The object must be locked, and will be returned locked.
493 vm_pageout_object_terminate(
496 vm_object_t shadow_object
;
499 * Deal with the deallocation (last reference) of a pageout object
500 * (used for cleaning-in-place) by dropping the paging references/
501 * freeing pages in the original object.
504 assert(object
->pageout
);
505 shadow_object
= object
->shadow
;
506 vm_object_lock(shadow_object
);
508 while (!queue_empty(&object
->memq
)) {
510 vm_object_offset_t offset
;
512 p
= (vm_page_t
) queue_first(&object
->memq
);
517 assert(!p
->cleaning
);
524 m
= vm_page_lookup(shadow_object
,
525 offset
+ object
->vo_shadow_offset
);
527 if(m
== VM_PAGE_NULL
)
530 assert((m
->dirty
) || (m
->precious
) ||
531 (m
->busy
&& m
->cleaning
));
534 * Handle the trusted pager throttle.
535 * Also decrement the burst throttle (if external).
537 vm_page_lock_queues();
538 if (m
->pageout_queue
)
539 vm_pageout_throttle_up(m
);
542 * Handle the "target" page(s). These pages are to be freed if
543 * successfully cleaned. Target pages are always busy, and are
544 * wired exactly once. The initial target pages are not mapped,
545 * (so cannot be referenced or modified) but converted target
546 * pages may have been modified between the selection as an
547 * adjacent page and conversion to a target.
551 assert(m
->wire_count
== 1);
553 m
->encrypted_cleaning
= FALSE
;
555 #if MACH_CLUSTER_STATS
556 if (m
->wanted
) vm_pageout_target_collisions
++;
559 * Revoke all access to the page. Since the object is
560 * locked, and the page is busy, this prevents the page
561 * from being dirtied after the pmap_disconnect() call
564 * Since the page is left "dirty" but "not modifed", we
565 * can detect whether the page was redirtied during
566 * pageout by checking the modify state.
568 if (pmap_disconnect(m
->phys_page
) & VM_MEM_MODIFIED
) {
569 SET_PAGE_DIRTY(m
, FALSE
);
575 CLUSTER_STAT(vm_pageout_target_page_dirtied
++;)
576 vm_page_unwire(m
, TRUE
); /* reactivates */
577 VM_STAT_INCR(reactivations
);
580 CLUSTER_STAT(vm_pageout_target_page_freed
++;)
581 vm_page_free(m
);/* clears busy, etc. */
583 vm_page_unlock_queues();
587 * Handle the "adjacent" pages. These pages were cleaned in
588 * place, and should be left alone.
589 * If prep_pin_count is nonzero, then someone is using the
590 * page, so make it active.
592 if (!m
->active
&& !m
->inactive
&& !m
->throttled
&& !m
->private) {
596 vm_page_deactivate(m
);
598 if (m
->overwriting
) {
600 * the (COPY_OUT_FROM == FALSE) request_page_list case
604 * We do not re-set m->dirty !
605 * The page was busy so no extraneous activity
606 * could have occurred. COPY_INTO is a read into the
607 * new pages. CLEAN_IN_PLACE does actually write
608 * out the pages but handling outside of this code
609 * will take care of resetting dirty. We clear the
610 * modify however for the Programmed I/O case.
612 pmap_clear_modify(m
->phys_page
);
618 * alternate (COPY_OUT_FROM == FALSE) request_page_list case
619 * Occurs when the original page was wired
620 * at the time of the list request
622 assert(VM_PAGE_WIRED(m
));
623 vm_page_unwire(m
, TRUE
); /* reactivates */
625 m
->overwriting
= FALSE
;
628 * Set the dirty state according to whether or not the page was
629 * modified during the pageout. Note that we purposefully do
630 * NOT call pmap_clear_modify since the page is still mapped.
631 * If the page were to be dirtied between the 2 calls, this
632 * this fact would be lost. This code is only necessary to
633 * maintain statistics, since the pmap module is always
634 * consulted if m->dirty is false.
636 #if MACH_CLUSTER_STATS
637 m
->dirty
= pmap_is_modified(m
->phys_page
);
639 if (m
->dirty
) vm_pageout_cluster_dirtied
++;
640 else vm_pageout_cluster_cleaned
++;
641 if (m
->wanted
) vm_pageout_cluster_collisions
++;
646 if (m
->encrypted_cleaning
== TRUE
) {
647 m
->encrypted_cleaning
= FALSE
;
653 * Wakeup any thread waiting for the page to be un-cleaning.
656 vm_page_unlock_queues();
659 * Account for the paging reference taken in vm_paging_object_allocate.
661 vm_object_activity_end(shadow_object
);
662 vm_object_unlock(shadow_object
);
664 assert(object
->ref_count
== 0);
665 assert(object
->paging_in_progress
== 0);
666 assert(object
->activity_in_progress
== 0);
667 assert(object
->resident_page_count
== 0);
672 * Routine: vm_pageclean_setup
674 * Purpose: setup a page to be cleaned (made non-dirty), but not
675 * necessarily flushed from the VM page cache.
676 * This is accomplished by cleaning in place.
678 * The page must not be busy, and new_object
686 vm_object_t new_object
,
687 vm_object_offset_t new_offset
)
691 assert(!m
->cleaning
);
695 "vm_pageclean_setup, obj 0x%X off 0x%X page 0x%X new 0x%X new_off 0x%X\n",
696 m
->object
, m
->offset
, m
,
699 pmap_clear_modify(m
->phys_page
);
702 * Mark original page as cleaning in place.
705 SET_PAGE_DIRTY(m
, FALSE
);
709 * Convert the fictitious page to a private shadow of
712 assert(new_m
->fictitious
);
713 assert(new_m
->phys_page
== vm_page_fictitious_addr
);
714 new_m
->fictitious
= FALSE
;
715 new_m
->private = TRUE
;
716 new_m
->pageout
= TRUE
;
717 new_m
->phys_page
= m
->phys_page
;
719 vm_page_lockspin_queues();
721 vm_page_unlock_queues();
723 vm_page_insert(new_m
, new_object
, new_offset
);
724 assert(!new_m
->wanted
);
729 * Routine: vm_pageout_initialize_page
731 * Causes the specified page to be initialized in
732 * the appropriate memory object. This routine is used to push
733 * pages into a copy-object when they are modified in the
736 * The page is moved to a temporary object and paged out.
739 * The page in question must not be on any pageout queues.
740 * The object to which it belongs must be locked.
741 * The page must be busy, but not hold a paging reference.
744 * Move this page to a completely new object.
747 vm_pageout_initialize_page(
751 vm_object_offset_t paging_offset
;
752 memory_object_t pager
;
755 "vm_pageout_initialize_page, page 0x%X\n",
760 * Verify that we really want to clean this page
767 * Create a paging reference to let us play with the object.
770 paging_offset
= m
->offset
+ object
->paging_offset
;
772 if (m
->absent
|| m
->error
|| m
->restart
|| (!m
->dirty
&& !m
->precious
)) {
774 panic("reservation without pageout?"); /* alan */
775 vm_object_unlock(object
);
781 * If there's no pager, then we can't clean the page. This should
782 * never happen since this should be a copy object and therefore not
783 * an external object, so the pager should always be there.
786 pager
= object
->pager
;
788 if (pager
== MEMORY_OBJECT_NULL
) {
790 panic("missing pager for copy object");
795 * set the page for future call to vm_fault_list_request
797 pmap_clear_modify(m
->phys_page
);
798 SET_PAGE_DIRTY(m
, FALSE
);
802 * keep the object from collapsing or terminating
804 vm_object_paging_begin(object
);
805 vm_object_unlock(object
);
808 * Write the data to its pager.
809 * Note that the data is passed by naming the new object,
810 * not a virtual address; the pager interface has been
811 * manipulated to use the "internal memory" data type.
812 * [The object reference from its allocation is donated
813 * to the eventual recipient.]
815 memory_object_data_initialize(pager
, paging_offset
, PAGE_SIZE
);
817 vm_object_lock(object
);
818 vm_object_paging_end(object
);
821 #if MACH_CLUSTER_STATS
822 #define MAXCLUSTERPAGES 16
824 unsigned long pages_in_cluster
;
825 unsigned long pages_at_higher_offsets
;
826 unsigned long pages_at_lower_offsets
;
827 } cluster_stats
[MAXCLUSTERPAGES
];
828 #endif /* MACH_CLUSTER_STATS */
832 * vm_pageout_cluster:
834 * Given a page, queue it to the appropriate I/O thread,
835 * which will page it out and attempt to clean adjacent pages
836 * in the same operation.
838 * The object and queues must be locked. We will take a
839 * paging reference to prevent deallocation or collapse when we
840 * release the object lock back at the call site. The I/O thread
841 * is responsible for consuming this reference
843 * The page must not be on any pageout queue.
847 vm_pageout_cluster(vm_page_t m
, boolean_t pageout
)
849 vm_object_t object
= m
->object
;
850 struct vm_pageout_queue
*q
;
854 "vm_pageout_cluster, object 0x%X offset 0x%X page 0x%X\n",
855 object
, m
->offset
, m
, 0, 0);
859 lck_mtx_assert(&vm_page_queue_lock
, LCK_MTX_ASSERT_OWNED
);
861 vm_object_lock_assert_exclusive(object
);
864 * Only a certain kind of page is appreciated here.
866 assert((m
->dirty
|| m
->precious
) && (!VM_PAGE_WIRED(m
)));
867 assert(!m
->cleaning
&& !m
->pageout
&& !m
->laundry
);
868 #ifndef CONFIG_FREEZE
869 assert(!m
->inactive
&& !m
->active
);
870 assert(!m
->throttled
);
874 * protect the object from collapse or termination
876 vm_object_activity_begin(object
);
878 m
->pageout
= pageout
;
880 if (object
->internal
== TRUE
) {
881 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
)
884 q
= &vm_pageout_queue_internal
;
886 q
= &vm_pageout_queue_external
;
889 * pgo_laundry count is tied to the laundry bit
894 m
->pageout_queue
= TRUE
;
895 queue_enter(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
897 if (q
->pgo_idle
== TRUE
) {
899 thread_wakeup((event_t
) &q
->pgo_pending
);
905 unsigned long vm_pageout_throttle_up_count
= 0;
908 * A page is back from laundry or we are stealing it back from
909 * the laundering state. See if there are some pages waiting to
910 * go to laundry and if we can let some of them go now.
912 * Object and page queues must be locked.
915 vm_pageout_throttle_up(
918 struct vm_pageout_queue
*q
;
920 assert(m
->object
!= VM_OBJECT_NULL
);
921 assert(m
->object
!= kernel_object
);
924 lck_mtx_assert(&vm_page_queue_lock
, LCK_MTX_ASSERT_OWNED
);
925 vm_object_lock_assert_exclusive(m
->object
);
928 vm_pageout_throttle_up_count
++;
930 if (m
->object
->internal
== TRUE
)
931 q
= &vm_pageout_queue_internal
;
933 q
= &vm_pageout_queue_external
;
935 if (m
->pageout_queue
== TRUE
) {
937 queue_remove(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
938 m
->pageout_queue
= FALSE
;
940 m
->pageq
.next
= NULL
;
941 m
->pageq
.prev
= NULL
;
943 vm_object_activity_end(m
->object
);
945 if (m
->laundry
== TRUE
) {
950 if (q
->pgo_throttled
== TRUE
) {
951 q
->pgo_throttled
= FALSE
;
952 thread_wakeup((event_t
) &q
->pgo_laundry
);
954 if (q
->pgo_draining
== TRUE
&& q
->pgo_laundry
== 0) {
955 q
->pgo_draining
= FALSE
;
956 thread_wakeup((event_t
) (&q
->pgo_laundry
+1));
963 vm_pageout_throttle_up_batch(
964 struct vm_pageout_queue
*q
,
968 lck_mtx_assert(&vm_page_queue_lock
, LCK_MTX_ASSERT_OWNED
);
971 vm_pageout_throttle_up_count
+= batch_cnt
;
973 q
->pgo_laundry
-= batch_cnt
;
975 if (q
->pgo_throttled
== TRUE
) {
976 q
->pgo_throttled
= FALSE
;
977 thread_wakeup((event_t
) &q
->pgo_laundry
);
979 if (q
->pgo_draining
== TRUE
&& q
->pgo_laundry
== 0) {
980 q
->pgo_draining
= FALSE
;
981 thread_wakeup((event_t
) (&q
->pgo_laundry
+1));
988 * VM memory pressure monitoring.
990 * vm_pageout_scan() keeps track of the number of pages it considers and
991 * reclaims, in the currently active vm_pageout_stat[vm_pageout_stat_now].
993 * compute_memory_pressure() is called every second from compute_averages()
994 * and moves "vm_pageout_stat_now" forward, to start accumulating the number
995 * of recalimed pages in a new vm_pageout_stat[] bucket.
997 * mach_vm_pressure_monitor() collects past statistics about memory pressure.
998 * The caller provides the number of seconds ("nsecs") worth of statistics
999 * it wants, up to 30 seconds.
1000 * It computes the number of pages reclaimed in the past "nsecs" seconds and
1001 * also returns the number of pages the system still needs to reclaim at this
1004 #define VM_PAGEOUT_STAT_SIZE 31
1005 struct vm_pageout_stat
{
1006 unsigned int considered
;
1007 unsigned int reclaimed
;
1008 } vm_pageout_stats
[VM_PAGEOUT_STAT_SIZE
] = {{0,0}, };
1009 unsigned int vm_pageout_stat_now
= 0;
1010 unsigned int vm_memory_pressure
= 0;
1012 #define VM_PAGEOUT_STAT_BEFORE(i) \
1013 (((i) == 0) ? VM_PAGEOUT_STAT_SIZE - 1 : (i) - 1)
1014 #define VM_PAGEOUT_STAT_AFTER(i) \
1015 (((i) == VM_PAGEOUT_STAT_SIZE - 1) ? 0 : (i) + 1)
1017 #if VM_PAGE_BUCKETS_CHECK
1018 int vm_page_buckets_check_interval
= 10; /* in seconds */
1019 #endif /* VM_PAGE_BUCKETS_CHECK */
1022 * Called from compute_averages().
1025 compute_memory_pressure(
1028 unsigned int vm_pageout_next
;
1030 #if VM_PAGE_BUCKETS_CHECK
1031 /* check the consistency of VM page buckets at regular interval */
1032 static int counter
= 0;
1033 if ((++counter
% vm_page_buckets_check_interval
) == 0) {
1034 vm_page_buckets_check();
1036 #endif /* VM_PAGE_BUCKETS_CHECK */
1038 vm_memory_pressure
=
1039 vm_pageout_stats
[VM_PAGEOUT_STAT_BEFORE(vm_pageout_stat_now
)].reclaimed
;
1041 commpage_set_memory_pressure( vm_memory_pressure
);
1043 /* move "now" forward */
1044 vm_pageout_next
= VM_PAGEOUT_STAT_AFTER(vm_pageout_stat_now
);
1045 vm_pageout_stats
[vm_pageout_next
].considered
= 0;
1046 vm_pageout_stats
[vm_pageout_next
].reclaimed
= 0;
1047 vm_pageout_stat_now
= vm_pageout_next
;
1053 * mach_vm_ctl_page_free_wanted() is called indirectly, via
1054 * mach_vm_pressure_monitor(), when taking a stackshot. Therefore,
1055 * it must be safe in the restricted stackshot context. Locks and/or
1056 * blocking are not allowable.
1059 mach_vm_ctl_page_free_wanted(void)
1061 unsigned int page_free_target
, page_free_count
, page_free_wanted
;
1063 page_free_target
= vm_page_free_target
;
1064 page_free_count
= vm_page_free_count
;
1065 if (page_free_target
> page_free_count
) {
1066 page_free_wanted
= page_free_target
- page_free_count
;
1068 page_free_wanted
= 0;
1071 return page_free_wanted
;
1077 * mach_vm_pressure_monitor() is called when taking a stackshot, with
1078 * wait_for_pressure FALSE, so that code path must remain safe in the
1079 * restricted stackshot context. No blocking or locks are allowable.
1080 * on that code path.
1084 mach_vm_pressure_monitor(
1085 boolean_t wait_for_pressure
,
1086 unsigned int nsecs_monitored
,
1087 unsigned int *pages_reclaimed_p
,
1088 unsigned int *pages_wanted_p
)
1091 unsigned int vm_pageout_then
, vm_pageout_now
;
1092 unsigned int pages_reclaimed
;
1095 * We don't take the vm_page_queue_lock here because we don't want
1096 * vm_pressure_monitor() to get in the way of the vm_pageout_scan()
1097 * thread when it's trying to reclaim memory. We don't need fully
1098 * accurate monitoring anyway...
1101 if (wait_for_pressure
) {
1102 /* wait until there's memory pressure */
1103 while (vm_page_free_count
>= vm_page_free_target
) {
1104 wr
= assert_wait((event_t
) &vm_page_free_wanted
,
1105 THREAD_INTERRUPTIBLE
);
1106 if (wr
== THREAD_WAITING
) {
1107 wr
= thread_block(THREAD_CONTINUE_NULL
);
1109 if (wr
== THREAD_INTERRUPTED
) {
1110 return KERN_ABORTED
;
1112 if (wr
== THREAD_AWAKENED
) {
1114 * The memory pressure might have already
1115 * been relieved but let's not block again
1116 * and let's report that there was memory
1117 * pressure at some point.
1124 /* provide the number of pages the system wants to reclaim */
1125 if (pages_wanted_p
!= NULL
) {
1126 *pages_wanted_p
= mach_vm_ctl_page_free_wanted();
1129 if (pages_reclaimed_p
== NULL
) {
1130 return KERN_SUCCESS
;
1133 /* provide number of pages reclaimed in the last "nsecs_monitored" */
1135 vm_pageout_now
= vm_pageout_stat_now
;
1136 pages_reclaimed
= 0;
1137 for (vm_pageout_then
=
1138 VM_PAGEOUT_STAT_BEFORE(vm_pageout_now
);
1139 vm_pageout_then
!= vm_pageout_now
&&
1140 nsecs_monitored
-- != 0;
1142 VM_PAGEOUT_STAT_BEFORE(vm_pageout_then
)) {
1143 pages_reclaimed
+= vm_pageout_stats
[vm_pageout_then
].reclaimed
;
1145 } while (vm_pageout_now
!= vm_pageout_stat_now
);
1146 *pages_reclaimed_p
= pages_reclaimed
;
1148 return KERN_SUCCESS
;
1154 * function in BSD to apply I/O throttle to the pageout thread
1156 extern void vm_pageout_io_throttle(void);
1159 * Page States: Used below to maintain the page state
1160 * before it's removed from it's Q. This saved state
1161 * helps us do the right accounting in certain cases
1163 #define PAGE_STATE_SPECULATIVE 1
1164 #define PAGE_STATE_ANONYMOUS 2
1165 #define PAGE_STATE_INACTIVE 3
1166 #define PAGE_STATE_INACTIVE_FIRST 4
1167 #define PAGE_STATE_CLEAN 5
1170 #define VM_PAGEOUT_SCAN_HANDLE_REUSABLE_PAGE(m) \
1173 * If a "reusable" page somehow made it back into \
1174 * the active queue, it's been re-used and is not \
1175 * quite re-usable. \
1176 * If the VM object was "all_reusable", consider it \
1177 * as "all re-used" instead of converting it to \
1178 * "partially re-used", which could be expensive. \
1180 if ((m)->reusable || \
1181 (m)->object->all_reusable) { \
1182 vm_object_reuse_pages((m)->object, \
1184 (m)->offset + PAGE_SIZE_64, \
1190 #define VM_PAGEOUT_DELAYED_UNLOCK_LIMIT 64
1191 #define VM_PAGEOUT_DELAYED_UNLOCK_LIMIT_MAX 1024
1194 #define FCS_DELAYED 1
1195 #define FCS_DEADLOCK_DETECTED 2
1197 struct flow_control
{
1202 uint32_t vm_pageout_considered_page
= 0;
1203 uint32_t vm_page_filecache_min
= 0;
1205 #define VM_PAGE_FILECACHE_MIN 50000
1206 #define ANONS_GRABBED_LIMIT 2
1209 * vm_pageout_scan does the dirty work for the pageout daemon.
1210 * It returns with both vm_page_queue_free_lock and vm_page_queue_lock
1211 * held and vm_page_free_wanted == 0.
1214 vm_pageout_scan(void)
1216 unsigned int loop_count
= 0;
1217 unsigned int inactive_burst_count
= 0;
1218 unsigned int active_burst_count
= 0;
1219 unsigned int reactivated_this_call
;
1220 unsigned int reactivate_limit
;
1221 vm_page_t local_freeq
= NULL
;
1222 int local_freed
= 0;
1224 int delayed_unlock_limit
= 0;
1225 int refmod_state
= 0;
1226 int vm_pageout_deadlock_target
= 0;
1227 struct vm_pageout_queue
*iq
;
1228 struct vm_pageout_queue
*eq
;
1229 struct vm_speculative_age_q
*sq
;
1230 struct flow_control flow_control
= { 0, { 0, 0 } };
1231 boolean_t inactive_throttled
= FALSE
;
1232 boolean_t try_failed
;
1234 unsigned int msecs
= 0;
1236 vm_object_t last_object_tried
;
1237 uint32_t catch_up_count
= 0;
1238 uint32_t inactive_reclaim_run
;
1239 boolean_t forced_reclaim
;
1240 boolean_t exceeded_burst_throttle
;
1241 boolean_t grab_anonymous
= FALSE
;
1242 boolean_t force_anonymous
= FALSE
;
1243 int anons_grabbed
= 0;
1244 int page_prev_state
= 0;
1245 int cache_evict_throttle
= 0;
1246 uint32_t vm_pageout_inactive_external_forced_reactivate_limit
= 0;
1247 int force_purge
= 0;
1249 #if VM_PRESSURE_EVENTS
1250 vm_pressure_level_t pressure_level
;
1251 #endif /* VM_PRESSURE_EVENTS */
1253 VM_DEBUG_EVENT(vm_pageout_scan
, VM_PAGEOUT_SCAN
, DBG_FUNC_START
,
1254 vm_pageout_speculative_clean
, vm_pageout_inactive_clean
,
1255 vm_pageout_inactive_dirty_internal
, vm_pageout_inactive_dirty_external
);
1257 flow_control
.state
= FCS_IDLE
;
1258 iq
= &vm_pageout_queue_internal
;
1259 eq
= &vm_pageout_queue_external
;
1260 sq
= &vm_page_queue_speculative
[VM_PAGE_SPECULATIVE_AGED_Q
];
1263 XPR(XPR_VM_PAGEOUT
, "vm_pageout_scan\n", 0, 0, 0, 0, 0);
1266 vm_page_lock_queues();
1267 delayed_unlock
= 1; /* must be nonzero if Qs are locked, 0 if unlocked */
1270 * Calculate the max number of referenced pages on the inactive
1271 * queue that we will reactivate.
1273 reactivated_this_call
= 0;
1274 reactivate_limit
= VM_PAGE_REACTIVATE_LIMIT(vm_page_active_count
+
1275 vm_page_inactive_count
);
1276 inactive_reclaim_run
= 0;
1278 vm_pageout_inactive_external_forced_reactivate_limit
= vm_page_active_count
+ vm_page_inactive_count
;
1281 * We want to gradually dribble pages from the active queue
1282 * to the inactive queue. If we let the inactive queue get
1283 * very small, and then suddenly dump many pages into it,
1284 * those pages won't get a sufficient chance to be referenced
1285 * before we start taking them from the inactive queue.
1287 * We must limit the rate at which we send pages to the pagers
1288 * so that we don't tie up too many pages in the I/O queues.
1289 * We implement a throttling mechanism using the laundry count
1290 * to limit the number of pages outstanding to the default
1291 * and external pagers. We can bypass the throttles and look
1292 * for clean pages if the pageout queues don't drain in a timely
1293 * fashion since this may indicate that the pageout paths are
1294 * stalled waiting for memory, which only we can provide.
1299 assert(delayed_unlock
!=0);
1302 * Recalculate vm_page_inactivate_target.
1304 vm_page_inactive_target
= VM_PAGE_INACTIVE_TARGET(vm_page_active_count
+
1305 vm_page_inactive_count
+
1306 vm_page_speculative_count
);
1308 vm_page_anonymous_min
= vm_page_inactive_target
/ 20;
1312 * don't want to wake the pageout_scan thread up everytime we fall below
1313 * the targets... set a low water mark at 0.25% below the target
1315 vm_page_inactive_min
= vm_page_inactive_target
- (vm_page_inactive_target
/ 400);
1317 if (vm_page_speculative_percentage
> 50)
1318 vm_page_speculative_percentage
= 50;
1319 else if (vm_page_speculative_percentage
<= 0)
1320 vm_page_speculative_percentage
= 1;
1322 vm_page_speculative_target
= VM_PAGE_SPECULATIVE_TARGET(vm_page_active_count
+
1323 vm_page_inactive_count
);
1326 last_object_tried
= NULL
;
1329 if ((vm_page_inactive_count
+ vm_page_speculative_count
) < VM_PAGE_INACTIVE_HEALTHY_LIMIT(vm_page_active_count
))
1330 catch_up_count
= vm_page_inactive_count
+ vm_page_speculative_count
;
1337 DTRACE_VM2(rev
, int, 1, (uint64_t *), NULL
);
1339 if (delayed_unlock
== 0) {
1340 vm_page_lock_queues();
1343 if (vm_upl_wait_for_pages
< 0)
1344 vm_upl_wait_for_pages
= 0;
1346 delayed_unlock_limit
= VM_PAGEOUT_DELAYED_UNLOCK_LIMIT
+ vm_upl_wait_for_pages
;
1348 if (delayed_unlock_limit
> VM_PAGEOUT_DELAYED_UNLOCK_LIMIT_MAX
)
1349 delayed_unlock_limit
= VM_PAGEOUT_DELAYED_UNLOCK_LIMIT_MAX
;
1352 * Move pages from active to inactive if we're below the target
1354 /* if we are trying to make clean, we need to make sure we actually have inactive - mj */
1355 if ((vm_page_inactive_count
+ vm_page_speculative_count
) >= vm_page_inactive_target
)
1356 goto done_moving_active_pages
;
1358 if (object
!= NULL
) {
1359 vm_object_unlock(object
);
1361 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1364 * Don't sweep through active queue more than the throttle
1365 * which should be kept relatively low
1367 active_burst_count
= MIN(vm_pageout_burst_active_throttle
, vm_page_active_count
);
1369 VM_DEBUG_EVENT(vm_pageout_balance
, VM_PAGEOUT_BALANCE
, DBG_FUNC_START
,
1370 vm_pageout_inactive
, vm_pageout_inactive_used
, vm_page_free_count
, local_freed
);
1372 VM_DEBUG_EVENT(vm_pageout_balance
, VM_PAGEOUT_BALANCE
, DBG_FUNC_NONE
,
1373 vm_pageout_speculative_clean
, vm_pageout_inactive_clean
,
1374 vm_pageout_inactive_dirty_internal
, vm_pageout_inactive_dirty_external
);
1375 memoryshot(VM_PAGEOUT_BALANCE
, DBG_FUNC_START
);
1378 while (!queue_empty(&vm_page_queue_active
) && active_burst_count
--) {
1380 vm_pageout_active
++;
1382 m
= (vm_page_t
) queue_first(&vm_page_queue_active
);
1384 assert(m
->active
&& !m
->inactive
);
1385 assert(!m
->laundry
);
1386 assert(m
->object
!= kernel_object
);
1387 assert(m
->phys_page
!= vm_page_guard_addr
);
1389 DTRACE_VM2(scan
, int, 1, (uint64_t *), NULL
);
1392 * by not passing in a pmap_flush_context we will forgo any TLB flushing, local or otherwise...
1394 * a TLB flush isn't really needed here since at worst we'll miss the reference bit being
1395 * updated in the PTE if a remote processor still has this mapping cached in its TLB when the
1396 * new reference happens. If no futher references happen on the page after that remote TLB flushes
1397 * we'll see a clean, non-referenced page when it eventually gets pulled out of the inactive queue
1398 * by pageout_scan, which is just fine since the last reference would have happened quite far
1399 * in the past (TLB caches don't hang around for very long), and of course could just as easily
1400 * have happened before we moved the page
1402 pmap_clear_refmod_options(m
->phys_page
, VM_MEM_REFERENCED
, PMAP_OPTIONS_NOFLUSH
, (void *)NULL
);
1405 * The page might be absent or busy,
1406 * but vm_page_deactivate can handle that.
1407 * FALSE indicates that we don't want a H/W clear reference
1409 vm_page_deactivate_internal(m
, FALSE
);
1411 if (delayed_unlock
++ > delayed_unlock_limit
) {
1414 vm_page_unlock_queues();
1416 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_START
,
1417 vm_page_free_count
, local_freed
, delayed_unlock_limit
, 1);
1419 vm_page_free_list(local_freeq
, TRUE
);
1421 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_END
,
1422 vm_page_free_count
, 0, 0, 1);
1426 vm_page_lock_queues();
1428 lck_mtx_yield(&vm_page_queue_lock
);
1434 * continue the while loop processing
1435 * the active queue... need to hold
1436 * the page queues lock
1441 VM_DEBUG_EVENT(vm_pageout_balance
, VM_PAGEOUT_BALANCE
, DBG_FUNC_END
,
1442 vm_page_active_count
, vm_page_inactive_count
, vm_page_speculative_count
, vm_page_inactive_target
);
1443 memoryshot(VM_PAGEOUT_BALANCE
, DBG_FUNC_END
);
1445 /**********************************************************************
1446 * above this point we're playing with the active queue
1447 * below this point we're playing with the throttling mechanisms
1448 * and the inactive queue
1449 **********************************************************************/
1451 done_moving_active_pages
:
1453 if (vm_page_free_count
+ local_freed
>= vm_page_free_target
) {
1454 if (object
!= NULL
) {
1455 vm_object_unlock(object
);
1458 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1461 vm_page_unlock_queues();
1463 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_START
,
1464 vm_page_free_count
, local_freed
, delayed_unlock_limit
, 2);
1466 vm_page_free_list(local_freeq
, TRUE
);
1468 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_END
,
1469 vm_page_free_count
, local_freed
, 0, 2);
1473 vm_page_lock_queues();
1476 * make sure the pageout I/O threads are running
1477 * throttled in case there are still requests
1478 * in the laundry... since we have met our targets
1479 * we don't need the laundry to be cleaned in a timely
1480 * fashion... so let's avoid interfering with foreground
1483 vm_pageout_adjust_io_throttles(iq
, eq
, TRUE
);
1486 * recalculate vm_page_inactivate_target
1488 vm_page_inactive_target
= VM_PAGE_INACTIVE_TARGET(vm_page_active_count
+
1489 vm_page_inactive_count
+
1490 vm_page_speculative_count
);
1491 if (((vm_page_inactive_count
+ vm_page_speculative_count
) < vm_page_inactive_target
) &&
1492 !queue_empty(&vm_page_queue_active
)) {
1494 * inactive target still not met... keep going
1495 * until we get the queues balanced...
1499 lck_mtx_lock(&vm_page_queue_free_lock
);
1501 if ((vm_page_free_count
>= vm_page_free_target
) &&
1502 (vm_page_free_wanted
== 0) && (vm_page_free_wanted_privileged
== 0)) {
1504 * done - we have met our target *and*
1505 * there is no one waiting for a page.
1508 assert(vm_pageout_scan_wants_object
== VM_OBJECT_NULL
);
1510 VM_DEBUG_EVENT(vm_pageout_scan
, VM_PAGEOUT_SCAN
, DBG_FUNC_NONE
,
1511 vm_pageout_inactive
, vm_pageout_inactive_used
, 0, 0);
1512 VM_DEBUG_EVENT(vm_pageout_scan
, VM_PAGEOUT_SCAN
, DBG_FUNC_END
,
1513 vm_pageout_speculative_clean
, vm_pageout_inactive_clean
,
1514 vm_pageout_inactive_dirty_internal
, vm_pageout_inactive_dirty_external
);
1518 lck_mtx_unlock(&vm_page_queue_free_lock
);
1522 * Before anything, we check if we have any ripe volatile
1523 * objects around. If so, try to purge the first object.
1524 * If the purge fails, fall through to reclaim a page instead.
1525 * If the purge succeeds, go back to the top and reevalute
1526 * the new memory situation.
1529 assert (available_for_purge
>=0);
1530 force_purge
= 0; /* no force-purging */
1532 #if VM_PRESSURE_EVENTS
1533 pressure_level
= memorystatus_vm_pressure_level
;
1535 if (pressure_level
> kVMPressureNormal
) {
1537 if (pressure_level
>= kVMPressureCritical
) {
1538 force_purge
= memorystatus_purge_on_critical
;
1539 } else if (pressure_level
>= kVMPressureUrgent
) {
1540 force_purge
= memorystatus_purge_on_urgent
;
1541 } else if (pressure_level
>= kVMPressureWarning
) {
1542 force_purge
= memorystatus_purge_on_warning
;
1545 #endif /* VM_PRESSURE_EVENTS */
1547 if (available_for_purge
|| force_purge
) {
1549 if (object
!= NULL
) {
1550 vm_object_unlock(object
);
1554 memoryshot(VM_PAGEOUT_PURGEONE
, DBG_FUNC_START
);
1556 VM_DEBUG_EVENT(vm_pageout_purgeone
, VM_PAGEOUT_PURGEONE
, DBG_FUNC_START
, vm_page_free_count
, 0, 0, 0);
1557 if (vm_purgeable_object_purge_one(force_purge
, C_DONT_BLOCK
)) {
1559 VM_DEBUG_EVENT(vm_pageout_purgeone
, VM_PAGEOUT_PURGEONE
, DBG_FUNC_END
, vm_page_free_count
, 0, 0, 0);
1560 memoryshot(VM_PAGEOUT_PURGEONE
, DBG_FUNC_END
);
1563 VM_DEBUG_EVENT(vm_pageout_purgeone
, VM_PAGEOUT_PURGEONE
, DBG_FUNC_END
, 0, 0, 0, -1);
1564 memoryshot(VM_PAGEOUT_PURGEONE
, DBG_FUNC_END
);
1567 if (queue_empty(&sq
->age_q
) && vm_page_speculative_count
) {
1569 * try to pull pages from the aging bins...
1570 * see vm_page.h for an explanation of how
1571 * this mechanism works
1573 struct vm_speculative_age_q
*aq
;
1574 mach_timespec_t ts_fully_aged
;
1575 boolean_t can_steal
= FALSE
;
1576 int num_scanned_queues
;
1578 aq
= &vm_page_queue_speculative
[speculative_steal_index
];
1580 num_scanned_queues
= 0;
1581 while (queue_empty(&aq
->age_q
) &&
1582 num_scanned_queues
++ != VM_PAGE_MAX_SPECULATIVE_AGE_Q
) {
1584 speculative_steal_index
++;
1586 if (speculative_steal_index
> VM_PAGE_MAX_SPECULATIVE_AGE_Q
)
1587 speculative_steal_index
= VM_PAGE_MIN_SPECULATIVE_AGE_Q
;
1589 aq
= &vm_page_queue_speculative
[speculative_steal_index
];
1592 if (num_scanned_queues
== VM_PAGE_MAX_SPECULATIVE_AGE_Q
+ 1) {
1594 * XXX We've scanned all the speculative
1595 * queues but still haven't found one
1596 * that is not empty, even though
1597 * vm_page_speculative_count is not 0.
1599 * report the anomaly...
1601 printf("vm_pageout_scan: "
1602 "all speculative queues empty "
1603 "but count=%d. Re-adjusting.\n",
1604 vm_page_speculative_count
);
1605 if (vm_page_speculative_count
> vm_page_speculative_count_drift_max
)
1606 vm_page_speculative_count_drift_max
= vm_page_speculative_count
;
1607 vm_page_speculative_count_drifts
++;
1609 Debugger("vm_pageout_scan: no speculative pages");
1612 vm_page_speculative_count
= 0;
1613 /* ... and continue */
1617 if (vm_page_speculative_count
> vm_page_speculative_target
)
1620 ts_fully_aged
.tv_sec
= (VM_PAGE_MAX_SPECULATIVE_AGE_Q
* vm_page_speculative_q_age_ms
) / 1000;
1621 ts_fully_aged
.tv_nsec
= ((VM_PAGE_MAX_SPECULATIVE_AGE_Q
* vm_page_speculative_q_age_ms
) % 1000)
1622 * 1000 * NSEC_PER_USEC
;
1624 ADD_MACH_TIMESPEC(&ts_fully_aged
, &aq
->age_ts
);
1628 clock_get_system_nanotime(&sec
, &nsec
);
1629 ts
.tv_sec
= (unsigned int) sec
;
1632 if (CMP_MACH_TIMESPEC(&ts
, &ts_fully_aged
) >= 0)
1635 if (can_steal
== TRUE
)
1636 vm_page_speculate_ageit(aq
);
1638 if (queue_empty(&sq
->age_q
) && cache_evict_throttle
== 0) {
1641 if (object
!= NULL
) {
1642 vm_object_unlock(object
);
1645 pages_evicted
= vm_object_cache_evict(100, 10);
1647 if (pages_evicted
) {
1649 vm_pageout_cache_evicted
+= pages_evicted
;
1651 VM_DEBUG_EVENT(vm_pageout_cache_evict
, VM_PAGEOUT_CACHE_EVICT
, DBG_FUNC_NONE
,
1652 vm_page_free_count
, pages_evicted
, vm_pageout_cache_evicted
, 0);
1653 memoryshot(VM_PAGEOUT_CACHE_EVICT
, DBG_FUNC_NONE
);
1656 * we just freed up to 100 pages,
1657 * so go back to the top of the main loop
1658 * and re-evaulate the memory situation
1662 cache_evict_throttle
= 100;
1664 if (cache_evict_throttle
)
1665 cache_evict_throttle
--;
1668 exceeded_burst_throttle
= FALSE
;
1670 * Sometimes we have to pause:
1671 * 1) No inactive pages - nothing to do.
1672 * 2) Loop control - no acceptable pages found on the inactive queue
1673 * within the last vm_pageout_burst_inactive_throttle iterations
1674 * 3) Flow control - default pageout queue is full
1676 if (queue_empty(&vm_page_queue_inactive
) && queue_empty(&vm_page_queue_anonymous
) && queue_empty(&sq
->age_q
)) {
1677 vm_pageout_scan_empty_throttle
++;
1678 msecs
= vm_pageout_empty_wait
;
1679 goto vm_pageout_scan_delay
;
1681 } else if (inactive_burst_count
>=
1682 MIN(vm_pageout_burst_inactive_throttle
,
1683 (vm_page_inactive_count
+
1684 vm_page_speculative_count
))) {
1685 vm_pageout_scan_burst_throttle
++;
1686 msecs
= vm_pageout_burst_wait
;
1688 exceeded_burst_throttle
= TRUE
;
1689 goto vm_pageout_scan_delay
;
1691 } else if (vm_page_free_count
> (vm_page_free_reserved
/ 4) &&
1692 VM_PAGEOUT_SCAN_NEEDS_TO_THROTTLE()) {
1693 vm_pageout_scan_swap_throttle
++;
1694 msecs
= vm_pageout_swap_wait
;
1695 goto vm_pageout_scan_delay
;
1697 } else if (VM_PAGE_Q_THROTTLED(iq
) &&
1698 VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
)) {
1702 switch (flow_control
.state
) {
1705 if ((vm_page_free_count
+ local_freed
) < vm_page_free_target
) {
1707 if (vm_page_pageable_external_count
> vm_page_filecache_min
&& !queue_empty(&vm_page_queue_inactive
)) {
1708 anons_grabbed
= ANONS_GRABBED_LIMIT
;
1709 goto consider_inactive
;
1711 if (((vm_page_inactive_count
+ vm_page_speculative_count
) < vm_page_inactive_target
) && vm_page_active_count
)
1714 reset_deadlock_timer
:
1715 ts
.tv_sec
= vm_pageout_deadlock_wait
/ 1000;
1716 ts
.tv_nsec
= (vm_pageout_deadlock_wait
% 1000) * 1000 * NSEC_PER_USEC
;
1717 clock_get_system_nanotime(&sec
, &nsec
);
1718 flow_control
.ts
.tv_sec
= (unsigned int) sec
;
1719 flow_control
.ts
.tv_nsec
= nsec
;
1720 ADD_MACH_TIMESPEC(&flow_control
.ts
, &ts
);
1722 flow_control
.state
= FCS_DELAYED
;
1723 msecs
= vm_pageout_deadlock_wait
;
1728 clock_get_system_nanotime(&sec
, &nsec
);
1729 ts
.tv_sec
= (unsigned int) sec
;
1732 if (CMP_MACH_TIMESPEC(&ts
, &flow_control
.ts
) >= 0) {
1734 * the pageout thread for the default pager is potentially
1735 * deadlocked since the
1736 * default pager queue has been throttled for more than the
1737 * allowable time... we need to move some clean pages or dirty
1738 * pages belonging to the external pagers if they aren't throttled
1739 * vm_page_free_wanted represents the number of threads currently
1740 * blocked waiting for pages... we'll move one page for each of
1741 * these plus a fixed amount to break the logjam... once we're done
1742 * moving this number of pages, we'll re-enter the FSC_DELAYED state
1743 * with a new timeout target since we have no way of knowing
1744 * whether we've broken the deadlock except through observation
1745 * of the queue associated with the default pager... we need to
1746 * stop moving pages and allow the system to run to see what
1747 * state it settles into.
1749 vm_pageout_deadlock_target
= vm_pageout_deadlock_relief
+ vm_page_free_wanted
+ vm_page_free_wanted_privileged
;
1750 vm_pageout_scan_deadlock_detected
++;
1751 flow_control
.state
= FCS_DEADLOCK_DETECTED
;
1752 thread_wakeup((event_t
) &vm_pageout_garbage_collect
);
1753 goto consider_inactive
;
1756 * just resniff instead of trying
1757 * to compute a new delay time... we're going to be
1758 * awakened immediately upon a laundry completion,
1759 * so we won't wait any longer than necessary
1761 msecs
= vm_pageout_idle_wait
;
1764 case FCS_DEADLOCK_DETECTED
:
1765 if (vm_pageout_deadlock_target
)
1766 goto consider_inactive
;
1767 goto reset_deadlock_timer
;
1770 vm_pageout_scan_delay
:
1771 if (object
!= NULL
) {
1772 vm_object_unlock(object
);
1775 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1777 vm_page_unlock_queues();
1781 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_START
,
1782 vm_page_free_count
, local_freed
, delayed_unlock_limit
, 3);
1784 vm_page_free_list(local_freeq
, TRUE
);
1786 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_END
,
1787 vm_page_free_count
, local_freed
, 0, 3);
1792 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
)
1793 vm_consider_waking_compactor_swapper();
1795 vm_page_lock_queues();
1797 if (flow_control
.state
== FCS_DELAYED
&&
1798 !VM_PAGE_Q_THROTTLED(iq
)) {
1799 flow_control
.state
= FCS_IDLE
;
1800 goto consider_inactive
;
1803 if (vm_page_free_count
>= vm_page_free_target
) {
1805 * we're here because
1806 * 1) someone else freed up some pages while we had
1807 * the queues unlocked above
1808 * and we've hit one of the 3 conditions that
1809 * cause us to pause the pageout scan thread
1811 * since we already have enough free pages,
1812 * let's avoid stalling and return normally
1814 * before we return, make sure the pageout I/O threads
1815 * are running throttled in case there are still requests
1816 * in the laundry... since we have enough free pages
1817 * we don't need the laundry to be cleaned in a timely
1818 * fashion... so let's avoid interfering with foreground
1821 * we don't want to hold vm_page_queue_free_lock when
1822 * calling vm_pageout_adjust_io_throttles (since it
1823 * may cause other locks to be taken), we do the intitial
1824 * check outside of the lock. Once we take the lock,
1825 * we recheck the condition since it may have changed.
1826 * if it has, no problem, we will make the threads
1827 * non-throttled before actually blocking
1829 vm_pageout_adjust_io_throttles(iq
, eq
, TRUE
);
1831 lck_mtx_lock(&vm_page_queue_free_lock
);
1833 if (vm_page_free_count
>= vm_page_free_target
&&
1834 (vm_page_free_wanted
== 0) && (vm_page_free_wanted_privileged
== 0)) {
1835 goto return_from_scan
;
1837 lck_mtx_unlock(&vm_page_queue_free_lock
);
1839 if ((vm_page_free_count
+ vm_page_cleaned_count
) < vm_page_free_target
) {
1841 * we're most likely about to block due to one of
1842 * the 3 conditions that cause vm_pageout_scan to
1843 * not be able to make forward progress w/r
1844 * to providing new pages to the free queue,
1845 * so unthrottle the I/O threads in case we
1846 * have laundry to be cleaned... it needs
1847 * to be completed ASAP.
1849 * even if we don't block, we want the io threads
1850 * running unthrottled since the sum of free +
1851 * clean pages is still under our free target
1853 vm_pageout_adjust_io_throttles(iq
, eq
, FALSE
);
1855 if (vm_page_cleaned_count
> 0 && exceeded_burst_throttle
== FALSE
) {
1857 * if we get here we're below our free target and
1858 * we're stalling due to a full laundry queue or
1859 * we don't have any inactive pages other then
1860 * those in the clean queue...
1861 * however, we have pages on the clean queue that
1862 * can be moved to the free queue, so let's not
1863 * stall the pageout scan
1865 flow_control
.state
= FCS_IDLE
;
1866 goto consider_inactive
;
1868 VM_CHECK_MEMORYSTATUS
;
1870 if (flow_control
.state
!= FCS_IDLE
)
1871 vm_pageout_scan_throttle
++;
1872 iq
->pgo_throttled
= TRUE
;
1874 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
)
1875 vm_consider_waking_compactor_swapper();
1877 assert_wait_timeout((event_t
) &iq
->pgo_laundry
, THREAD_INTERRUPTIBLE
, msecs
, 1000*NSEC_PER_USEC
);
1878 counter(c_vm_pageout_scan_block
++);
1880 vm_page_unlock_queues();
1882 assert(vm_pageout_scan_wants_object
== VM_OBJECT_NULL
);
1884 VM_DEBUG_EVENT(vm_pageout_thread_block
, VM_PAGEOUT_THREAD_BLOCK
, DBG_FUNC_START
,
1885 iq
->pgo_laundry
, iq
->pgo_maxlaundry
, msecs
, 0);
1886 memoryshot(VM_PAGEOUT_THREAD_BLOCK
, DBG_FUNC_START
);
1888 thread_block(THREAD_CONTINUE_NULL
);
1890 VM_DEBUG_EVENT(vm_pageout_thread_block
, VM_PAGEOUT_THREAD_BLOCK
, DBG_FUNC_END
,
1891 iq
->pgo_laundry
, iq
->pgo_maxlaundry
, msecs
, 0);
1892 memoryshot(VM_PAGEOUT_THREAD_BLOCK
, DBG_FUNC_END
);
1894 vm_page_lock_queues();
1897 iq
->pgo_throttled
= FALSE
;
1899 if (loop_count
>= vm_page_inactive_count
)
1901 inactive_burst_count
= 0;
1908 flow_control
.state
= FCS_IDLE
;
1910 vm_pageout_inactive_external_forced_reactivate_limit
= MIN((vm_page_active_count
+ vm_page_inactive_count
),
1911 vm_pageout_inactive_external_forced_reactivate_limit
);
1913 inactive_burst_count
++;
1914 vm_pageout_inactive
++;
1923 if (VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
)) {
1924 assert(vm_page_throttled_count
== 0);
1925 assert(queue_empty(&vm_page_queue_throttled
));
1928 * The most eligible pages are ones we paged in speculatively,
1929 * but which have not yet been touched.
1931 if (!queue_empty(&sq
->age_q
) && force_anonymous
== FALSE
) {
1932 m
= (vm_page_t
) queue_first(&sq
->age_q
);
1934 page_prev_state
= PAGE_STATE_SPECULATIVE
;
1939 * Try a clean-queue inactive page.
1941 if (!queue_empty(&vm_page_queue_cleaned
)) {
1942 m
= (vm_page_t
) queue_first(&vm_page_queue_cleaned
);
1944 page_prev_state
= PAGE_STATE_CLEAN
;
1949 grab_anonymous
= (vm_page_anonymous_count
> vm_page_anonymous_min
);
1951 if (vm_page_pageable_external_count
< vm_page_filecache_min
|| force_anonymous
== TRUE
) {
1952 grab_anonymous
= TRUE
;
1956 if (grab_anonymous
== FALSE
|| anons_grabbed
>= ANONS_GRABBED_LIMIT
|| queue_empty(&vm_page_queue_anonymous
)) {
1958 if ( !queue_empty(&vm_page_queue_inactive
) ) {
1959 m
= (vm_page_t
) queue_first(&vm_page_queue_inactive
);
1961 page_prev_state
= PAGE_STATE_INACTIVE
;
1967 if ( !queue_empty(&vm_page_queue_anonymous
) ) {
1968 m
= (vm_page_t
) queue_first(&vm_page_queue_anonymous
);
1970 page_prev_state
= PAGE_STATE_ANONYMOUS
;
1977 * if we've gotten here, we have no victim page.
1978 * if making clean, free the local freed list and return.
1979 * if making free, check to see if we've finished balancing the queues
1980 * yet, if we haven't just continue, else panic
1982 vm_page_unlock_queues();
1984 if (object
!= NULL
) {
1985 vm_object_unlock(object
);
1988 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1991 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_START
,
1992 vm_page_free_count
, local_freed
, delayed_unlock_limit
, 5);
1994 vm_page_free_list(local_freeq
, TRUE
);
1996 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_END
,
1997 vm_page_free_count
, local_freed
, 0, 5);
2002 vm_page_lock_queues();
2005 force_anonymous
= FALSE
;
2007 if ((vm_page_inactive_count
+ vm_page_speculative_count
) < vm_page_inactive_target
)
2010 if (!queue_empty(&sq
->age_q
))
2013 panic("vm_pageout: no victim");
2017 force_anonymous
= FALSE
;
2020 * we just found this page on one of our queues...
2021 * it can't also be on the pageout queue, so safe
2022 * to call VM_PAGE_QUEUES_REMOVE
2024 assert(!m
->pageout_queue
);
2026 VM_PAGE_QUEUES_REMOVE(m
);
2028 assert(!m
->laundry
);
2029 assert(!m
->private);
2030 assert(!m
->fictitious
);
2031 assert(m
->object
!= kernel_object
);
2032 assert(m
->phys_page
!= vm_page_guard_addr
);
2035 if (page_prev_state
!= PAGE_STATE_SPECULATIVE
)
2036 vm_pageout_stats
[vm_pageout_stat_now
].considered
++;
2038 DTRACE_VM2(scan
, int, 1, (uint64_t *), NULL
);
2041 * check to see if we currently are working
2042 * with the same object... if so, we've
2043 * already got the lock
2045 if (m
->object
!= object
) {
2047 * the object associated with candidate page is
2048 * different from the one we were just working
2049 * with... dump the lock if we still own it
2051 if (object
!= NULL
) {
2052 vm_object_unlock(object
);
2054 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
2057 * Try to lock object; since we've alread got the
2058 * page queues lock, we can only 'try' for this one.
2059 * if the 'try' fails, we need to do a mutex_pause
2060 * to allow the owner of the object lock a chance to
2061 * run... otherwise, we're likely to trip over this
2062 * object in the same state as we work our way through
2063 * the queue... clumps of pages associated with the same
2064 * object are fairly typical on the inactive and active queues
2066 if (!vm_object_lock_try_scan(m
->object
)) {
2067 vm_page_t m_want
= NULL
;
2069 vm_pageout_inactive_nolock
++;
2071 if (page_prev_state
== PAGE_STATE_CLEAN
)
2072 vm_pageout_cleaned_nolock
++;
2074 if (page_prev_state
== PAGE_STATE_SPECULATIVE
)
2075 page_prev_state
= PAGE_STATE_INACTIVE_FIRST
;
2077 pmap_clear_reference(m
->phys_page
);
2078 m
->reference
= FALSE
;
2081 * m->object must be stable since we hold the page queues lock...
2082 * we can update the scan_collisions field sans the object lock
2083 * since it is a separate field and this is the only spot that does
2084 * a read-modify-write operation and it is never executed concurrently...
2085 * we can asynchronously set this field to 0 when creating a UPL, so it
2086 * is possible for the value to be a bit non-determistic, but that's ok
2087 * since it's only used as a hint
2089 m
->object
->scan_collisions
++;
2091 if ( !queue_empty(&sq
->age_q
) )
2092 m_want
= (vm_page_t
) queue_first(&sq
->age_q
);
2093 else if ( !queue_empty(&vm_page_queue_cleaned
))
2094 m_want
= (vm_page_t
) queue_first(&vm_page_queue_cleaned
);
2095 else if (anons_grabbed
>= ANONS_GRABBED_LIMIT
|| queue_empty(&vm_page_queue_anonymous
))
2096 m_want
= (vm_page_t
) queue_first(&vm_page_queue_inactive
);
2097 else if ( !queue_empty(&vm_page_queue_anonymous
))
2098 m_want
= (vm_page_t
) queue_first(&vm_page_queue_anonymous
);
2101 * this is the next object we're going to be interested in
2102 * try to make sure its available after the mutex_yield
2106 vm_pageout_scan_wants_object
= m_want
->object
;
2109 * force us to dump any collected free pages
2110 * and to pause before moving on
2117 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
2125 if (m
->encrypted_cleaning
) {
2128 * if this page has already been picked up as
2129 * part of a page-out cluster, it will be busy
2130 * because it is being encrypted (see
2131 * vm_object_upl_request()). But we still
2132 * want to demote it from "clean-in-place"
2133 * (aka "adjacent") to "clean-and-free" (aka
2134 * "target"), so let's ignore its "busy" bit
2135 * here and proceed to check for "cleaning" a
2136 * little bit below...
2139 * A "busy" page should still be left alone for
2140 * most purposes, so we have to be very careful
2141 * not to process that page too much.
2143 assert(m
->cleaning
);
2144 goto consider_inactive_page
;
2148 * Somebody is already playing with this page.
2149 * Put it back on the appropriate queue
2152 vm_pageout_inactive_busy
++;
2154 if (page_prev_state
== PAGE_STATE_CLEAN
)
2155 vm_pageout_cleaned_busy
++;
2158 switch (page_prev_state
) {
2160 case PAGE_STATE_SPECULATIVE
:
2161 vm_page_speculate(m
, FALSE
);
2164 case PAGE_STATE_ANONYMOUS
:
2165 case PAGE_STATE_CLEAN
:
2166 case PAGE_STATE_INACTIVE
:
2167 VM_PAGE_ENQUEUE_INACTIVE(m
, FALSE
);
2170 case PAGE_STATE_INACTIVE_FIRST
:
2171 VM_PAGE_ENQUEUE_INACTIVE(m
, TRUE
);
2174 goto done_with_inactivepage
;
2179 * If it's absent, in error or the object is no longer alive,
2180 * we can reclaim the page... in the no longer alive case,
2181 * there are 2 states the page can be in that preclude us
2182 * from reclaiming it - busy or cleaning - that we've already
2185 if (m
->absent
|| m
->error
|| !object
->alive
) {
2188 vm_pageout_inactive_absent
++;
2189 else if (!object
->alive
)
2190 vm_pageout_inactive_notalive
++;
2192 vm_pageout_inactive_error
++;
2194 if (vm_pageout_deadlock_target
) {
2195 vm_pageout_scan_inactive_throttle_success
++;
2196 vm_pageout_deadlock_target
--;
2199 DTRACE_VM2(dfree
, int, 1, (uint64_t *), NULL
);
2201 if (object
->internal
) {
2202 DTRACE_VM2(anonfree
, int, 1, (uint64_t *), NULL
);
2204 DTRACE_VM2(fsfree
, int, 1, (uint64_t *), NULL
);
2206 assert(!m
->cleaning
);
2207 assert(!m
->laundry
);
2212 * remove page from object here since we're already
2213 * behind the object lock... defer the rest of the work
2214 * we'd normally do in vm_page_free_prepare_object
2215 * until 'vm_page_free_list' is called
2218 vm_page_remove(m
, TRUE
);
2220 assert(m
->pageq
.next
== NULL
&&
2221 m
->pageq
.prev
== NULL
);
2222 m
->pageq
.next
= (queue_entry_t
)local_freeq
;
2226 if (page_prev_state
== PAGE_STATE_SPECULATIVE
)
2227 vm_pageout_freed_from_speculative
++;
2228 else if (page_prev_state
== PAGE_STATE_CLEAN
)
2229 vm_pageout_freed_from_cleaned
++;
2231 vm_pageout_freed_from_inactive_clean
++;
2233 if (page_prev_state
!= PAGE_STATE_SPECULATIVE
)
2234 vm_pageout_stats
[vm_pageout_stat_now
].reclaimed
++;
2236 inactive_burst_count
= 0;
2237 goto done_with_inactivepage
;
2240 * If the object is empty, the page must be reclaimed even
2242 * If the page belongs to a volatile object, we stick it back
2245 if (object
->copy
== VM_OBJECT_NULL
) {
2246 if (object
->purgable
== VM_PURGABLE_EMPTY
) {
2247 if (m
->pmapped
== TRUE
) {
2248 /* unmap the page */
2249 refmod_state
= pmap_disconnect(m
->phys_page
);
2250 if (refmod_state
& VM_MEM_MODIFIED
) {
2251 SET_PAGE_DIRTY(m
, FALSE
);
2254 if (m
->dirty
|| m
->precious
) {
2255 /* we saved the cost of cleaning this page ! */
2256 vm_page_purged_count
++;
2261 if (COMPRESSED_PAGER_IS_ACTIVE
) {
2263 * With the VM compressor, the cost of
2264 * reclaiming a page is much lower (no I/O),
2265 * so if we find a "volatile" page, it's better
2266 * to let it get compressed rather than letting
2267 * it occupy a full page until it gets purged.
2268 * So no need to check for "volatile" here.
2270 } else if (object
->purgable
== VM_PURGABLE_VOLATILE
) {
2272 * Avoid cleaning a "volatile" page which might
2276 /* if it's wired, we can't put it on our queue */
2277 assert(!VM_PAGE_WIRED(m
));
2279 /* just stick it back on! */
2280 reactivated_this_call
++;
2282 if (page_prev_state
== PAGE_STATE_CLEAN
)
2283 vm_pageout_cleaned_volatile_reactivated
++;
2285 goto reactivate_page
;
2289 consider_inactive_page
:
2293 * A "busy" page should always be left alone, except...
2295 if (m
->cleaning
&& m
->encrypted_cleaning
) {
2298 * We could get here with a "busy" page
2299 * if it's being encrypted during a
2300 * "clean-in-place" operation. We'll deal
2301 * with it right away by testing if it has been
2302 * referenced and either reactivating it or
2303 * promoting it from "clean-in-place" to
2307 panic("\"busy\" page considered for pageout\n");
2312 * If it's being used, reactivate.
2313 * (Fictitious pages are either busy or absent.)
2314 * First, update the reference and dirty bits
2315 * to make sure the page is unreferenced.
2319 if (m
->reference
== FALSE
&& m
->pmapped
== TRUE
) {
2320 refmod_state
= pmap_get_refmod(m
->phys_page
);
2322 if (refmod_state
& VM_MEM_REFERENCED
)
2323 m
->reference
= TRUE
;
2324 if (refmod_state
& VM_MEM_MODIFIED
) {
2325 SET_PAGE_DIRTY(m
, FALSE
);
2330 * if (m->cleaning && !m->pageout)
2331 * If already cleaning this page in place and it hasn't
2332 * been recently referenced, just pull off the queue.
2333 * We can leave the page mapped, and upl_commit_range
2334 * will put it on the clean queue.
2336 * note: if m->encrypted_cleaning == TRUE, then
2337 * m->cleaning == TRUE
2338 * and we'll handle it here
2340 * if (m->pageout && !m->cleaning)
2341 * an msync INVALIDATE is in progress...
2342 * this page has been marked for destruction
2343 * after it has been cleaned,
2344 * but not yet gathered into a UPL
2345 * where 'cleaning' will be set...
2346 * just leave it off the paging queues
2348 * if (m->pageout && m->clenaing)
2349 * an msync INVALIDATE is in progress
2350 * and the UPL has already gathered this page...
2351 * just leave it off the paging queues
2355 * page with m->pageout and still on the queues means that an
2356 * MS_INVALIDATE is in progress on this page... leave it alone
2359 goto done_with_inactivepage
;
2362 /* if cleaning, reactivate if referenced. otherwise, just pull off queue */
2364 if (m
->reference
== TRUE
) {
2365 reactivated_this_call
++;
2366 goto reactivate_page
;
2368 goto done_with_inactivepage
;
2372 if (m
->reference
|| m
->dirty
) {
2373 /* deal with a rogue "reusable" page */
2374 VM_PAGEOUT_SCAN_HANDLE_REUSABLE_PAGE(m
);
2379 (m
->xpmapped
&& !object
->internal
&& (vm_page_xpmapped_external_count
< (vm_page_external_count
/ 4))))) {
2381 * The page we pulled off the inactive list has
2382 * been referenced. It is possible for other
2383 * processors to be touching pages faster than we
2384 * can clear the referenced bit and traverse the
2385 * inactive queue, so we limit the number of
2388 if (++reactivated_this_call
>= reactivate_limit
) {
2389 vm_pageout_reactivation_limit_exceeded
++;
2390 } else if (catch_up_count
) {
2391 vm_pageout_catch_ups
++;
2392 } else if (++inactive_reclaim_run
>= VM_PAGEOUT_INACTIVE_FORCE_RECLAIM
) {
2393 vm_pageout_inactive_force_reclaim
++;
2397 if (page_prev_state
== PAGE_STATE_CLEAN
)
2398 vm_pageout_cleaned_reference_reactivated
++;
2401 if ( !object
->internal
&& object
->pager
!= MEMORY_OBJECT_NULL
&&
2402 vnode_pager_get_isinuse(object
->pager
, &isinuse
) == KERN_SUCCESS
&& !isinuse
) {
2404 * no explict mappings of this object exist
2405 * and it's not open via the filesystem
2407 vm_page_deactivate(m
);
2408 vm_pageout_inactive_deactivated
++;
2411 * The page was/is being used, so put back on active list.
2413 vm_page_activate(m
);
2414 VM_STAT_INCR(reactivations
);
2415 inactive_burst_count
= 0;
2418 if (page_prev_state
== PAGE_STATE_CLEAN
)
2419 vm_pageout_cleaned_reactivated
++;
2421 vm_pageout_inactive_used
++;
2423 goto done_with_inactivepage
;
2426 * Make sure we call pmap_get_refmod() if it
2427 * wasn't already called just above, to update
2430 if ((refmod_state
== -1) && !m
->dirty
&& m
->pmapped
) {
2431 refmod_state
= pmap_get_refmod(m
->phys_page
);
2432 if (refmod_state
& VM_MEM_MODIFIED
) {
2433 SET_PAGE_DIRTY(m
, FALSE
);
2436 forced_reclaim
= TRUE
;
2438 forced_reclaim
= FALSE
;
2442 "vm_pageout_scan, replace object 0x%X offset 0x%X page 0x%X\n",
2443 object
, m
->offset
, m
, 0,0);
2446 * we've got a candidate page to steal...
2448 * m->dirty is up to date courtesy of the
2449 * preceding check for m->reference... if
2450 * we get here, then m->reference had to be
2451 * FALSE (or possibly "reactivate_limit" was
2452 * exceeded), but in either case we called
2453 * pmap_get_refmod() and updated both
2454 * m->reference and m->dirty
2456 * if it's dirty or precious we need to
2457 * see if the target queue is throtttled
2458 * it if is, we need to skip over it by moving it back
2459 * to the end of the inactive queue
2462 inactive_throttled
= FALSE
;
2464 if (m
->dirty
|| m
->precious
) {
2465 if (object
->internal
) {
2466 if (VM_PAGE_Q_THROTTLED(iq
))
2467 inactive_throttled
= TRUE
;
2468 } else if (VM_PAGE_Q_THROTTLED(eq
)) {
2469 inactive_throttled
= TRUE
;
2473 if (!VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
) &&
2474 object
->internal
&& m
->dirty
&&
2475 (object
->purgable
== VM_PURGABLE_DENY
||
2476 object
->purgable
== VM_PURGABLE_NONVOLATILE
||
2477 object
->purgable
== VM_PURGABLE_VOLATILE
)) {
2478 queue_enter(&vm_page_queue_throttled
, m
,
2480 m
->throttled
= TRUE
;
2481 vm_page_throttled_count
++;
2483 vm_pageout_scan_reclaimed_throttled
++;
2485 inactive_burst_count
= 0;
2486 goto done_with_inactivepage
;
2488 if (inactive_throttled
== TRUE
) {
2490 if (object
->internal
== FALSE
) {
2492 * we need to break up the following potential deadlock case...
2493 * a) The external pageout thread is stuck on the truncate lock for a file that is being extended i.e. written.
2494 * b) The thread doing the writing is waiting for pages while holding the truncate lock
2495 * c) Most of the pages in the inactive queue belong to this file.
2497 * we are potentially in this deadlock because...
2498 * a) the external pageout queue is throttled
2499 * b) we're done with the active queue and moved on to the inactive queue
2500 * c) we've got a dirty external page
2502 * since we don't know the reason for the external pageout queue being throttled we
2503 * must suspect that we are deadlocked, so move the current page onto the active queue
2504 * in an effort to cause a page from the active queue to 'age' to the inactive queue
2506 * if we don't have jetsam configured (i.e. we have a dynamic pager), set
2507 * 'force_anonymous' to TRUE to cause us to grab a page from the cleaned/anonymous
2508 * pool the next time we select a victim page... if we can make enough new free pages,
2509 * the deadlock will break, the external pageout queue will empty and it will no longer
2512 * if we have jestam configured, keep a count of the pages reactivated this way so
2513 * that we can try to find clean pages in the active/inactive queues before
2514 * deciding to jetsam a process
2516 vm_pageout_scan_inactive_throttled_external
++;
2518 queue_enter(&vm_page_queue_active
, m
, vm_page_t
, pageq
);
2520 vm_page_active_count
++;
2521 vm_page_pageable_external_count
++;
2523 vm_pageout_adjust_io_throttles(iq
, eq
, FALSE
);
2525 #if CONFIG_MEMORYSTATUS && CONFIG_JETSAM
2526 vm_pageout_inactive_external_forced_reactivate_limit
--;
2528 if (vm_pageout_inactive_external_forced_reactivate_limit
<= 0) {
2529 vm_pageout_inactive_external_forced_reactivate_limit
= vm_page_active_count
+ vm_page_inactive_count
;
2531 * Possible deadlock scenario so request jetsam action
2534 vm_object_unlock(object
);
2535 object
= VM_OBJECT_NULL
;
2536 vm_page_unlock_queues();
2538 VM_DEBUG_EVENT(vm_pageout_jetsam
, VM_PAGEOUT_JETSAM
, DBG_FUNC_START
,
2539 vm_page_active_count
, vm_page_inactive_count
, vm_page_free_count
, vm_page_free_count
);
2541 /* Kill first suitable process */
2542 if (memorystatus_kill_on_VM_page_shortage(FALSE
) == FALSE
) {
2543 panic("vm_pageout_scan: Jetsam request failed\n");
2546 VM_DEBUG_EVENT(vm_pageout_jetsam
, VM_PAGEOUT_JETSAM
, DBG_FUNC_END
, 0, 0, 0, 0);
2548 vm_pageout_inactive_external_forced_jetsam_count
++;
2549 vm_page_lock_queues();
2552 #else /* CONFIG_MEMORYSTATUS && CONFIG_JETSAM */
2553 force_anonymous
= TRUE
;
2555 inactive_burst_count
= 0;
2556 goto done_with_inactivepage
;
2558 if (page_prev_state
== PAGE_STATE_SPECULATIVE
)
2559 page_prev_state
= PAGE_STATE_INACTIVE
;
2561 vm_pageout_scan_inactive_throttled_internal
++;
2568 * we've got a page that we can steal...
2569 * eliminate all mappings and make sure
2570 * we have the up-to-date modified state
2572 * if we need to do a pmap_disconnect then we
2573 * need to re-evaluate m->dirty since the pmap_disconnect
2574 * provides the true state atomically... the
2575 * page was still mapped up to the pmap_disconnect
2576 * and may have been dirtied at the last microsecond
2578 * Note that if 'pmapped' is FALSE then the page is not
2579 * and has not been in any map, so there is no point calling
2580 * pmap_disconnect(). m->dirty could have been set in anticipation
2581 * of likely usage of the page.
2583 if (m
->pmapped
== TRUE
) {
2585 if (DEFAULT_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
|| object
->internal
== FALSE
) {
2587 * Don't count this page as going into the compressor if any of these are true:
2588 * 1) We have the dynamic pager i.e. no compressed pager
2589 * 2) Freezer enabled device with a freezer file to hold the app data i.e. no compressed pager
2590 * 3) Freezer enabled device with compressed pager backend (exclusive use) i.e. most of the VM system
2591 (including vm_pageout_scan) has no knowledge of the compressor
2592 * 4) This page belongs to a file and hence will not be sent into the compressor
2595 refmod_state
= pmap_disconnect_options(m
->phys_page
, 0, NULL
);
2597 refmod_state
= pmap_disconnect_options(m
->phys_page
, PMAP_OPTIONS_COMPRESSOR
, NULL
);
2600 if (refmod_state
& VM_MEM_MODIFIED
) {
2601 SET_PAGE_DIRTY(m
, FALSE
);
2605 * reset our count of pages that have been reclaimed
2606 * since the last page was 'stolen'
2608 inactive_reclaim_run
= 0;
2611 * If it's clean and not precious, we can free the page.
2613 if (!m
->dirty
&& !m
->precious
) {
2615 if (page_prev_state
== PAGE_STATE_SPECULATIVE
)
2616 vm_pageout_speculative_clean
++;
2618 if (page_prev_state
== PAGE_STATE_ANONYMOUS
)
2619 vm_pageout_inactive_anonymous
++;
2620 else if (page_prev_state
== PAGE_STATE_CLEAN
)
2621 vm_pageout_cleaned_reclaimed
++;
2623 vm_pageout_inactive_clean
++;
2627 * OK, at this point we have found a page we are going to free.
2629 #if CONFIG_PHANTOM_CACHE
2630 if (!object
->internal
)
2631 vm_phantom_cache_add_ghost(m
);
2637 * The page may have been dirtied since the last check
2638 * for a throttled target queue (which may have been skipped
2639 * if the page was clean then). With the dirty page
2640 * disconnected here, we can make one final check.
2642 if (object
->internal
) {
2643 if (VM_PAGE_Q_THROTTLED(iq
))
2644 inactive_throttled
= TRUE
;
2645 } else if (VM_PAGE_Q_THROTTLED(eq
)) {
2646 inactive_throttled
= TRUE
;
2649 if (inactive_throttled
== TRUE
)
2650 goto throttle_inactive
;
2652 #if VM_PRESSURE_EVENTS
2656 * If Jetsam is enabled, then the sending
2657 * of memory pressure notifications is handled
2658 * from the same thread that takes care of high-water
2659 * and other jetsams i.e. the memorystatus_thread.
2662 #else /* CONFIG_JETSAM */
2664 vm_pressure_response();
2666 #endif /* CONFIG_JETSAM */
2667 #endif /* VM_PRESSURE_EVENTS */
2670 * do NOT set the pageout bit!
2671 * sure, we might need free pages, but this page is going to take time to become free
2672 * anyway, so we may as well put it on the clean queue first and take it from there later
2673 * if necessary. that way, we'll ensure we don't free up too much. -mj
2675 vm_pageout_cluster(m
, FALSE
);
2677 if (page_prev_state
== PAGE_STATE_ANONYMOUS
)
2678 vm_pageout_inactive_anonymous
++;
2679 if (object
->internal
)
2680 vm_pageout_inactive_dirty_internal
++;
2682 vm_pageout_inactive_dirty_external
++;
2685 done_with_inactivepage
:
2687 if (delayed_unlock
++ > delayed_unlock_limit
|| try_failed
== TRUE
) {
2688 boolean_t need_delay
= TRUE
;
2690 if (object
!= NULL
) {
2691 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
2692 vm_object_unlock(object
);
2695 vm_page_unlock_queues();
2699 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_START
,
2700 vm_page_free_count
, local_freed
, delayed_unlock_limit
, 4);
2702 vm_page_free_list(local_freeq
, TRUE
);
2704 VM_DEBUG_EVENT(vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_END
,
2705 vm_page_free_count
, local_freed
, 0, 4);
2711 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
2712 vm_consider_waking_compactor_swapper();
2715 vm_page_lock_queues();
2717 if (need_delay
== TRUE
)
2718 lck_mtx_yield(&vm_page_queue_lock
);
2722 vm_pageout_considered_page
++;
2725 * back to top of pageout scan loop
2731 int vm_page_free_count_init
;
2734 vm_page_free_reserve(
2737 int free_after_reserve
;
2739 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
2741 if ((vm_page_free_reserved
+ pages
+ COMPRESSOR_FREE_RESERVED_LIMIT
) >= (VM_PAGE_FREE_RESERVED_LIMIT
+ COMPRESSOR_FREE_RESERVED_LIMIT
))
2742 vm_page_free_reserved
= VM_PAGE_FREE_RESERVED_LIMIT
+ COMPRESSOR_FREE_RESERVED_LIMIT
;
2744 vm_page_free_reserved
+= (pages
+ COMPRESSOR_FREE_RESERVED_LIMIT
);
2747 if ((vm_page_free_reserved
+ pages
) >= VM_PAGE_FREE_RESERVED_LIMIT
)
2748 vm_page_free_reserved
= VM_PAGE_FREE_RESERVED_LIMIT
;
2750 vm_page_free_reserved
+= pages
;
2752 free_after_reserve
= vm_page_free_count_init
- vm_page_free_reserved
;
2754 vm_page_free_min
= vm_page_free_reserved
+
2755 VM_PAGE_FREE_MIN(free_after_reserve
);
2757 if (vm_page_free_min
> VM_PAGE_FREE_MIN_LIMIT
)
2758 vm_page_free_min
= VM_PAGE_FREE_MIN_LIMIT
;
2760 vm_page_free_target
= vm_page_free_reserved
+
2761 VM_PAGE_FREE_TARGET(free_after_reserve
);
2763 if (vm_page_free_target
> VM_PAGE_FREE_TARGET_LIMIT
)
2764 vm_page_free_target
= VM_PAGE_FREE_TARGET_LIMIT
;
2766 if (vm_page_free_target
< vm_page_free_min
+ 5)
2767 vm_page_free_target
= vm_page_free_min
+ 5;
2769 vm_page_throttle_limit
= vm_page_free_target
- (vm_page_free_target
/ 3);
2770 vm_page_creation_throttle
= vm_page_free_target
* 3;
2774 * vm_pageout is the high level pageout daemon.
2778 vm_pageout_continue(void)
2780 DTRACE_VM2(pgrrun
, int, 1, (uint64_t *), NULL
);
2781 vm_pageout_scan_event_counter
++;
2785 * we hold both the vm_page_queue_free_lock
2786 * and the vm_page_queues_lock at this point
2788 assert(vm_page_free_wanted
== 0);
2789 assert(vm_page_free_wanted_privileged
== 0);
2790 assert_wait((event_t
) &vm_page_free_wanted
, THREAD_UNINT
);
2792 lck_mtx_unlock(&vm_page_queue_free_lock
);
2793 vm_page_unlock_queues();
2795 counter(c_vm_pageout_block
++);
2796 thread_block((thread_continue_t
)vm_pageout_continue
);
2801 #ifdef FAKE_DEADLOCK
2803 #define FAKE_COUNT 5000
2805 int internal_count
= 0;
2806 int fake_deadlock
= 0;
2811 vm_pageout_iothread_continue(struct vm_pageout_queue
*q
)
2815 vm_object_offset_t offset
;
2816 memory_object_t pager
;
2817 thread_t self
= current_thread();
2819 if ((vm_pageout_internal_iothread
!= THREAD_NULL
)
2820 && (self
== vm_pageout_external_iothread
)
2821 && (self
->options
& TH_OPT_VMPRIV
))
2822 self
->options
&= ~TH_OPT_VMPRIV
;
2824 vm_page_lockspin_queues();
2826 while ( !queue_empty(&q
->pgo_pending
) ) {
2829 queue_remove_first(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
2830 if (m
->object
->object_slid
) {
2831 panic("slid page %p not allowed on this path\n", m
);
2834 m
->pageout_queue
= FALSE
;
2835 m
->pageq
.next
= NULL
;
2836 m
->pageq
.prev
= NULL
;
2839 * grab a snapshot of the object and offset this
2840 * page is tabled in so that we can relookup this
2841 * page after we've taken the object lock - these
2842 * fields are stable while we hold the page queues lock
2843 * but as soon as we drop it, there is nothing to keep
2844 * this page in this object... we hold an activity_in_progress
2845 * on this object which will keep it from terminating
2850 vm_page_unlock_queues();
2852 #ifdef FAKE_DEADLOCK
2853 if (q
== &vm_pageout_queue_internal
) {
2859 if ((internal_count
== FAKE_COUNT
)) {
2861 pg_count
= vm_page_free_count
+ vm_page_free_reserved
;
2863 if (kmem_alloc(kernel_map
, &addr
, PAGE_SIZE
* pg_count
) == KERN_SUCCESS
) {
2864 kmem_free(kernel_map
, addr
, PAGE_SIZE
* pg_count
);
2871 vm_object_lock(object
);
2873 m
= vm_page_lookup(object
, offset
);
2876 m
->busy
|| m
->cleaning
|| m
->pageout_queue
|| !m
->laundry
) {
2878 * it's either the same page that someone else has
2879 * started cleaning (or it's finished cleaning or
2880 * been put back on the pageout queue), or
2881 * the page has been freed or we have found a
2882 * new page at this offset... in all of these cases
2883 * we merely need to release the activity_in_progress
2884 * we took when we put the page on the pageout queue
2886 vm_object_activity_end(object
);
2887 vm_object_unlock(object
);
2889 vm_page_lockspin_queues();
2892 if (!object
->pager_initialized
) {
2895 * If there is no memory object for the page, create
2896 * one and hand it to the default pager.
2899 if (!object
->pager_initialized
)
2900 vm_object_collapse(object
,
2901 (vm_object_offset_t
) 0,
2903 if (!object
->pager_initialized
)
2904 vm_object_pager_create(object
);
2905 if (!object
->pager_initialized
) {
2907 * Still no pager for the object.
2908 * Reactivate the page.
2910 * Should only happen if there is no
2915 vm_page_lockspin_queues();
2917 vm_pageout_throttle_up(m
);
2918 vm_page_activate(m
);
2919 vm_pageout_dirty_no_pager
++;
2921 vm_page_unlock_queues();
2924 * And we are done with it.
2926 vm_object_activity_end(object
);
2927 vm_object_unlock(object
);
2929 vm_page_lockspin_queues();
2933 pager
= object
->pager
;
2935 if (pager
== MEMORY_OBJECT_NULL
) {
2937 * This pager has been destroyed by either
2938 * memory_object_destroy or vm_object_destroy, and
2939 * so there is nowhere for the page to go.
2943 * Just free the page... VM_PAGE_FREE takes
2944 * care of cleaning up all the state...
2945 * including doing the vm_pageout_throttle_up
2949 vm_page_lockspin_queues();
2951 vm_pageout_throttle_up(m
);
2952 vm_page_activate(m
);
2954 vm_page_unlock_queues();
2957 * And we are done with it.
2960 vm_object_activity_end(object
);
2961 vm_object_unlock(object
);
2963 vm_page_lockspin_queues();
2968 * we don't hold the page queue lock
2969 * so this check isn't safe to make
2974 * give back the activity_in_progress reference we
2975 * took when we queued up this page and replace it
2976 * it with a paging_in_progress reference that will
2977 * also hold the paging offset from changing and
2978 * prevent the object from terminating
2980 vm_object_activity_end(object
);
2981 vm_object_paging_begin(object
);
2982 vm_object_unlock(object
);
2985 * Send the data to the pager.
2986 * any pageout clustering happens there
2988 memory_object_data_return(pager
,
2989 m
->offset
+ object
->paging_offset
,
2997 vm_object_lock(object
);
2998 vm_object_paging_end(object
);
2999 vm_object_unlock(object
);
3001 vm_pageout_io_throttle();
3003 vm_page_lockspin_queues();
3005 q
->pgo_busy
= FALSE
;
3008 assert_wait((event_t
) &q
->pgo_pending
, THREAD_UNINT
);
3009 vm_page_unlock_queues();
3011 thread_block_parameter((thread_continue_t
)vm_pageout_iothread_continue
, (void *) q
);
3017 vm_pageout_iothread_external_continue(struct vm_pageout_queue
*q
)
3021 vm_object_offset_t offset
;
3022 memory_object_t pager
;
3025 if (vm_pageout_internal_iothread
!= THREAD_NULL
)
3026 current_thread()->options
&= ~TH_OPT_VMPRIV
;
3028 vm_page_lockspin_queues();
3030 while ( !queue_empty(&q
->pgo_pending
) ) {
3033 queue_remove_first(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
3034 if (m
->object
->object_slid
) {
3035 panic("slid page %p not allowed on this path\n", m
);
3038 m
->pageout_queue
= FALSE
;
3039 m
->pageq
.next
= NULL
;
3040 m
->pageq
.prev
= NULL
;
3043 * grab a snapshot of the object and offset this
3044 * page is tabled in so that we can relookup this
3045 * page after we've taken the object lock - these
3046 * fields are stable while we hold the page queues lock
3047 * but as soon as we drop it, there is nothing to keep
3048 * this page in this object... we hold an activity_in_progress
3049 * on this object which will keep it from terminating
3054 vm_page_unlock_queues();
3056 vm_object_lock(object
);
3058 m
= vm_page_lookup(object
, offset
);
3061 m
->busy
|| m
->cleaning
|| m
->pageout_queue
|| !m
->laundry
) {
3063 * it's either the same page that someone else has
3064 * started cleaning (or it's finished cleaning or
3065 * been put back on the pageout queue), or
3066 * the page has been freed or we have found a
3067 * new page at this offset... in all of these cases
3068 * we merely need to release the activity_in_progress
3069 * we took when we put the page on the pageout queue
3071 vm_object_activity_end(object
);
3072 vm_object_unlock(object
);
3074 vm_page_lockspin_queues();
3077 pager
= object
->pager
;
3079 if (pager
== MEMORY_OBJECT_NULL
) {
3081 * This pager has been destroyed by either
3082 * memory_object_destroy or vm_object_destroy, and
3083 * so there is nowhere for the page to go.
3087 * Just free the page... VM_PAGE_FREE takes
3088 * care of cleaning up all the state...
3089 * including doing the vm_pageout_throttle_up
3093 vm_page_lockspin_queues();
3095 vm_pageout_throttle_up(m
);
3096 vm_page_activate(m
);
3098 vm_page_unlock_queues();
3101 * And we are done with it.
3104 vm_object_activity_end(object
);
3105 vm_object_unlock(object
);
3107 vm_page_lockspin_queues();
3112 * we don't hold the page queue lock
3113 * so this check isn't safe to make
3118 * give back the activity_in_progress reference we
3119 * took when we queued up this page and replace it
3120 * it with a paging_in_progress reference that will
3121 * also hold the paging offset from changing and
3122 * prevent the object from terminating
3124 vm_object_activity_end(object
);
3125 vm_object_paging_begin(object
);
3126 vm_object_unlock(object
);
3129 * Send the data to the pager.
3130 * any pageout clustering happens there
3132 memory_object_data_return(pager
,
3133 m
->offset
+ object
->paging_offset
,
3141 vm_object_lock(object
);
3142 vm_object_paging_end(object
);
3143 vm_object_unlock(object
);
3145 vm_pageout_io_throttle();
3147 vm_page_lockspin_queues();
3149 q
->pgo_busy
= FALSE
;
3152 assert_wait((event_t
) &q
->pgo_pending
, THREAD_UNINT
);
3153 vm_page_unlock_queues();
3155 thread_block_parameter((thread_continue_t
)vm_pageout_iothread_external_continue
, (void *) q
);
3160 uint32_t vm_compressor_failed
;
3163 vm_pageout_iothread_internal_continue(struct cq
*cq
)
3165 struct vm_pageout_queue
*q
;
3168 memory_object_t pager
;
3169 boolean_t pgo_draining
;
3172 vm_page_t local_freeq
= NULL
;
3173 int local_freed
= 0;
3174 int local_batch_size
;
3175 kern_return_t retval
;
3176 int compressed_count_delta
;
3179 KERNEL_DEBUG(0xe040000c | DBG_FUNC_END
, 0, 0, 0, 0, 0);
3182 local_batch_size
= q
->pgo_maxlaundry
/ (vm_compressor_thread_count
* 4);
3189 KERNEL_DEBUG(0xe0400014 | DBG_FUNC_START
, 0, 0, 0, 0, 0);
3191 vm_page_lock_queues();
3193 KERNEL_DEBUG(0xe0400014 | DBG_FUNC_END
, 0, 0, 0, 0, 0);
3195 KERNEL_DEBUG(0xe0400018 | DBG_FUNC_START
, 0, 0, 0, 0, 0);
3197 while ( !queue_empty(&q
->pgo_pending
) && local_cnt
< local_batch_size
) {
3199 queue_remove_first(&q
->pgo_pending
, m
, vm_page_t
, pageq
);
3203 m
->pageout_queue
= FALSE
;
3204 m
->pageq
.prev
= NULL
;
3206 m
->pageq
.next
= (queue_entry_t
)local_q
;
3210 if (local_q
== NULL
)
3215 if ((pgo_draining
= q
->pgo_draining
) == FALSE
)
3216 vm_pageout_throttle_up_batch(q
, local_cnt
);
3218 vm_page_unlock_queues();
3220 KERNEL_DEBUG(0xe0400018 | DBG_FUNC_END
, 0, 0, 0, 0, 0);
3225 local_q
= (vm_page_t
)m
->pageq
.next
;
3226 m
->pageq
.next
= NULL
;
3228 if (m
->object
->object_slid
) {
3229 panic("slid page %p not allowed on this path\n", m
);
3233 pager
= object
->pager
;
3235 if (!object
->pager_initialized
|| pager
== MEMORY_OBJECT_NULL
) {
3237 KERNEL_DEBUG(0xe0400010 | DBG_FUNC_START
, object
, pager
, 0, 0, 0);
3239 vm_object_lock(object
);
3242 * If there is no memory object for the page, create
3243 * one and hand it to the compression pager.
3246 if (!object
->pager_initialized
)
3247 vm_object_collapse(object
, (vm_object_offset_t
) 0, TRUE
);
3248 if (!object
->pager_initialized
)
3249 vm_object_compressor_pager_create(object
);
3251 if (!object
->pager_initialized
) {
3253 * Still no pager for the object.
3254 * Reactivate the page.
3256 * Should only happen if there is no
3261 PAGE_WAKEUP_DONE(m
);
3263 vm_page_lockspin_queues();
3264 vm_page_activate(m
);
3265 vm_pageout_dirty_no_pager
++;
3266 vm_page_unlock_queues();
3269 * And we are done with it.
3271 vm_object_activity_end(object
);
3272 vm_object_unlock(object
);
3276 pager
= object
->pager
;
3278 if (pager
== MEMORY_OBJECT_NULL
) {
3280 * This pager has been destroyed by either
3281 * memory_object_destroy or vm_object_destroy, and
3282 * so there is nowhere for the page to go.
3286 * Just free the page... VM_PAGE_FREE takes
3287 * care of cleaning up all the state...
3288 * including doing the vm_pageout_throttle_up
3293 PAGE_WAKEUP_DONE(m
);
3295 vm_page_lockspin_queues();
3296 vm_page_activate(m
);
3297 vm_page_unlock_queues();
3300 * And we are done with it.
3303 vm_object_activity_end(object
);
3304 vm_object_unlock(object
);
3308 vm_object_unlock(object
);
3310 KERNEL_DEBUG(0xe0400010 | DBG_FUNC_END
, object
, pager
, 0, 0, 0);
3312 while (vm_page_free_count
< (vm_page_free_reserved
- COMPRESSOR_FREE_RESERVED_LIMIT
)) {
3313 kern_return_t wait_result
;
3314 int need_wakeup
= 0;
3317 vm_page_free_list(local_freeq
, TRUE
);
3324 lck_mtx_lock_spin(&vm_page_queue_free_lock
);
3326 if (vm_page_free_count
< (vm_page_free_reserved
- COMPRESSOR_FREE_RESERVED_LIMIT
)) {
3328 if (vm_page_free_wanted_privileged
++ == 0)
3330 wait_result
= assert_wait((event_t
)&vm_page_free_wanted_privileged
, THREAD_UNINT
);
3332 lck_mtx_unlock(&vm_page_queue_free_lock
);
3335 thread_wakeup((event_t
)&vm_page_free_wanted
);
3337 if (wait_result
== THREAD_WAITING
)
3338 thread_block(THREAD_CONTINUE_NULL
);
3340 lck_mtx_unlock(&vm_page_queue_free_lock
);
3343 assert(object
->activity_in_progress
> 0);
3345 retval
= vm_compressor_pager_put(
3347 m
->offset
+ object
->paging_offset
,
3351 &compressed_count_delta
);
3353 vm_object_lock(object
);
3354 assert(object
->activity_in_progress
> 0);
3356 assert(m
->object
== object
);
3358 vm_compressor_pager_count(pager
,
3359 compressed_count_delta
,
3360 FALSE
, /* shared_lock */
3366 if (retval
== KERN_SUCCESS
) {
3368 * If the object is purgeable, its owner's
3369 * purgeable ledgers will be updated in
3370 * vm_page_remove() but the page still
3371 * contributes to the owner's memory footprint,
3372 * so account for it as such.
3374 if (object
->purgable
!= VM_PURGABLE_DENY
&&
3375 object
->vo_purgeable_owner
!= NULL
) {
3376 /* one more compressed purgeable page */
3377 vm_purgeable_compressed_update(object
,
3381 vm_page_compressions_failing
= FALSE
;
3383 VM_STAT_INCR(compressions
);
3386 vm_page_remove(m
, TRUE
);
3387 vm_object_activity_end(object
);
3388 vm_object_unlock(object
);
3390 m
->pageq
.next
= (queue_entry_t
)local_freeq
;
3395 PAGE_WAKEUP_DONE(m
);
3397 vm_page_lockspin_queues();
3399 vm_page_activate(m
);
3400 vm_compressor_failed
++;
3402 vm_page_compressions_failing
= TRUE
;
3404 vm_page_unlock_queues();
3406 vm_object_activity_end(object
);
3407 vm_object_unlock(object
);
3411 vm_page_free_list(local_freeq
, TRUE
);
3416 if (pgo_draining
== TRUE
) {
3417 vm_page_lockspin_queues();
3418 vm_pageout_throttle_up_batch(q
, local_cnt
);
3419 vm_page_unlock_queues();
3422 KERNEL_DEBUG(0xe040000c | DBG_FUNC_START
, 0, 0, 0, 0, 0);
3425 * queue lock is held and our q is empty
3427 q
->pgo_busy
= FALSE
;
3430 assert_wait((event_t
) &q
->pgo_pending
, THREAD_UNINT
);
3431 vm_page_unlock_queues();
3433 KERNEL_DEBUG(0xe0400018 | DBG_FUNC_END
, 0, 0, 0, 0, 0);
3435 thread_block_parameter((thread_continue_t
)vm_pageout_iothread_internal_continue
, (void *) cq
);
3442 vm_pageout_adjust_io_throttles(struct vm_pageout_queue
*iq
, struct vm_pageout_queue
*eq
, boolean_t req_lowpriority
)
3445 boolean_t set_iq
= FALSE
;
3446 boolean_t set_eq
= FALSE
;
3448 if (hibernate_cleaning_in_progress
== TRUE
)
3449 req_lowpriority
= FALSE
;
3451 if ((DEFAULT_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_IS_ACTIVE
) && iq
->pgo_inited
== TRUE
&& iq
->pgo_lowpriority
!= req_lowpriority
)
3454 if (eq
->pgo_inited
== TRUE
&& eq
->pgo_lowpriority
!= req_lowpriority
)
3457 if (set_iq
== TRUE
|| set_eq
== TRUE
) {
3459 vm_page_unlock_queues();
3461 if (req_lowpriority
== TRUE
) {
3462 policy
= THROTTLE_LEVEL_PAGEOUT_THROTTLED
;
3463 DTRACE_VM(laundrythrottle
);
3465 policy
= THROTTLE_LEVEL_PAGEOUT_UNTHROTTLED
;
3466 DTRACE_VM(laundryunthrottle
);
3468 if (set_iq
== TRUE
) {
3469 proc_set_task_policy_thread(kernel_task
, iq
->pgo_tid
, TASK_POLICY_EXTERNAL
, TASK_POLICY_IO
, policy
);
3471 iq
->pgo_lowpriority
= req_lowpriority
;
3473 if (set_eq
== TRUE
) {
3474 proc_set_task_policy_thread(kernel_task
, eq
->pgo_tid
, TASK_POLICY_EXTERNAL
, TASK_POLICY_IO
, policy
);
3476 eq
->pgo_lowpriority
= req_lowpriority
;
3478 vm_page_lock_queues();
3484 vm_pageout_iothread_external(void)
3486 thread_t self
= current_thread();
3488 self
->options
|= TH_OPT_VMPRIV
;
3490 DTRACE_VM2(laundrythrottle
, int, 1, (uint64_t *), NULL
);
3492 proc_set_task_policy_thread(kernel_task
, self
->thread_id
, TASK_POLICY_EXTERNAL
,
3493 TASK_POLICY_IO
, THROTTLE_LEVEL_PAGEOUT_THROTTLED
);
3495 vm_page_lock_queues();
3497 vm_pageout_queue_external
.pgo_tid
= self
->thread_id
;
3498 vm_pageout_queue_external
.pgo_lowpriority
= TRUE
;
3499 vm_pageout_queue_external
.pgo_inited
= TRUE
;
3501 vm_page_unlock_queues();
3503 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
)
3504 vm_pageout_iothread_external_continue(&vm_pageout_queue_external
);
3506 vm_pageout_iothread_continue(&vm_pageout_queue_external
);
3513 vm_pageout_iothread_internal(struct cq
*cq
)
3515 thread_t self
= current_thread();
3517 self
->options
|= TH_OPT_VMPRIV
;
3519 if (DEFAULT_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_IS_ACTIVE
) {
3520 DTRACE_VM2(laundrythrottle
, int, 1, (uint64_t *), NULL
);
3522 proc_set_task_policy_thread(kernel_task
, self
->thread_id
, TASK_POLICY_EXTERNAL
,
3523 TASK_POLICY_IO
, THROTTLE_LEVEL_PAGEOUT_THROTTLED
);
3525 vm_page_lock_queues();
3527 vm_pageout_queue_internal
.pgo_tid
= self
->thread_id
;
3528 vm_pageout_queue_internal
.pgo_lowpriority
= TRUE
;
3529 vm_pageout_queue_internal
.pgo_inited
= TRUE
;
3531 vm_page_unlock_queues();
3533 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
3534 cq
->q
= &vm_pageout_queue_internal
;
3535 cq
->current_chead
= NULL
;
3536 cq
->scratch_buf
= kalloc(COMPRESSOR_SCRATCH_BUF_SIZE
);
3538 vm_pageout_iothread_internal_continue(cq
);
3540 vm_pageout_iothread_continue(&vm_pageout_queue_internal
);
3546 vm_set_buffer_cleanup_callout(boolean_t (*func
)(int))
3548 if (OSCompareAndSwapPtr(NULL
, func
, (void * volatile *) &consider_buffer_cache_collect
)) {
3549 return KERN_SUCCESS
;
3551 return KERN_FAILURE
; /* Already set */
3555 extern boolean_t memorystatus_manual_testing_on
;
3556 extern unsigned int memorystatus_level
;
3559 #if VM_PRESSURE_EVENTS
3561 boolean_t vm_pressure_events_enabled
= FALSE
;
3564 vm_pressure_response(void)
3567 vm_pressure_level_t old_level
= kVMPressureNormal
;
3570 uint64_t available_memory
= 0;
3572 if (vm_pressure_events_enabled
== FALSE
)
3576 available_memory
= (((uint64_t) AVAILABLE_NON_COMPRESSED_MEMORY
) * 100);
3579 memorystatus_level
= (unsigned int) (available_memory
/ atop_64(max_mem
));
3581 if (memorystatus_manual_testing_on
) {
3585 old_level
= memorystatus_vm_pressure_level
;
3587 switch (memorystatus_vm_pressure_level
) {
3589 case kVMPressureNormal
:
3591 if (VM_PRESSURE_WARNING_TO_CRITICAL()) {
3592 new_level
= kVMPressureCritical
;
3593 } else if (VM_PRESSURE_NORMAL_TO_WARNING()) {
3594 new_level
= kVMPressureWarning
;
3599 case kVMPressureWarning
:
3600 case kVMPressureUrgent
:
3602 if (VM_PRESSURE_WARNING_TO_NORMAL()) {
3603 new_level
= kVMPressureNormal
;
3604 } else if (VM_PRESSURE_WARNING_TO_CRITICAL()) {
3605 new_level
= kVMPressureCritical
;
3610 case kVMPressureCritical
:
3612 if (VM_PRESSURE_WARNING_TO_NORMAL()) {
3613 new_level
= kVMPressureNormal
;
3614 } else if (VM_PRESSURE_CRITICAL_TO_WARNING()) {
3615 new_level
= kVMPressureWarning
;
3624 if (new_level
!= -1) {
3625 memorystatus_vm_pressure_level
= (vm_pressure_level_t
) new_level
;
3627 if ((memorystatus_vm_pressure_level
!= kVMPressureNormal
) || (old_level
!= new_level
)) {
3628 if (vm_pressure_thread_running
== FALSE
) {
3629 thread_wakeup(&vm_pressure_thread
);
3632 if (old_level
!= new_level
) {
3633 thread_wakeup(&vm_pressure_changed
);
3639 #endif /* VM_PRESSURE_EVENTS */
3642 mach_vm_pressure_level_monitor(__unused boolean_t wait_for_pressure
, __unused
unsigned int *pressure_level
) {
3644 #if !VM_PRESSURE_EVENTS
3646 return KERN_FAILURE
;
3648 #else /* VM_PRESSURE_EVENTS */
3650 kern_return_t kr
= KERN_SUCCESS
;
3652 if (pressure_level
!= NULL
) {
3654 vm_pressure_level_t old_level
= memorystatus_vm_pressure_level
;
3656 if (wait_for_pressure
== TRUE
) {
3657 wait_result_t wr
= 0;
3659 while (old_level
== *pressure_level
) {
3660 wr
= assert_wait((event_t
) &vm_pressure_changed
,
3661 THREAD_INTERRUPTIBLE
);
3662 if (wr
== THREAD_WAITING
) {
3663 wr
= thread_block(THREAD_CONTINUE_NULL
);
3665 if (wr
== THREAD_INTERRUPTED
) {
3666 return KERN_ABORTED
;
3668 if (wr
== THREAD_AWAKENED
) {
3670 old_level
= memorystatus_vm_pressure_level
;
3672 if (old_level
!= *pressure_level
) {
3679 *pressure_level
= old_level
;
3682 kr
= KERN_INVALID_ARGUMENT
;
3686 #endif /* VM_PRESSURE_EVENTS */
3689 #if VM_PRESSURE_EVENTS
3691 vm_pressure_thread(void) {
3692 static boolean_t thread_initialized
= FALSE
;
3694 if (thread_initialized
== TRUE
) {
3695 vm_pressure_thread_running
= TRUE
;
3696 consider_vm_pressure_events();
3697 vm_pressure_thread_running
= FALSE
;
3700 thread_initialized
= TRUE
;
3701 assert_wait((event_t
) &vm_pressure_thread
, THREAD_UNINT
);
3702 thread_block((thread_continue_t
)vm_pressure_thread
);
3704 #endif /* VM_PRESSURE_EVENTS */
3707 uint32_t vm_pageout_considered_page_last
= 0;
3710 * called once per-second via "compute_averages"
3713 compute_pageout_gc_throttle()
3715 if (vm_pageout_considered_page
!= vm_pageout_considered_page_last
) {
3717 vm_pageout_considered_page_last
= vm_pageout_considered_page
;
3719 thread_wakeup((event_t
) &vm_pageout_garbage_collect
);
3725 vm_pageout_garbage_collect(int collect
)
3729 boolean_t buf_large_zfree
= FALSE
;
3730 boolean_t first_try
= TRUE
;
3734 consider_machine_collect();
3738 if (consider_buffer_cache_collect
!= NULL
) {
3739 buf_large_zfree
= (*consider_buffer_cache_collect
)(0);
3741 if (first_try
== TRUE
|| buf_large_zfree
== TRUE
) {
3743 * consider_zone_gc should be last, because the other operations
3744 * might return memory to zones.
3746 consider_zone_gc(buf_large_zfree
);
3750 } while (buf_large_zfree
== TRUE
&& vm_page_free_count
< vm_page_free_target
);
3752 consider_machine_adjust();
3754 assert_wait((event_t
) &vm_pageout_garbage_collect
, THREAD_UNINT
);
3756 thread_block_parameter((thread_continue_t
) vm_pageout_garbage_collect
, (void *)1);
3761 void vm_pageout_reinit_tuneables(void);
3764 vm_pageout_reinit_tuneables(void)
3766 vm_page_filecache_min
= (uint32_t) (max_mem
/ PAGE_SIZE
) / 15;
3768 if (vm_page_filecache_min
< VM_PAGE_FILECACHE_MIN
)
3769 vm_page_filecache_min
= VM_PAGE_FILECACHE_MIN
;
3771 vm_compressor_minorcompact_threshold_divisor
= 18;
3772 vm_compressor_majorcompact_threshold_divisor
= 22;
3773 vm_compressor_unthrottle_threshold_divisor
= 32;
3777 #if VM_PAGE_BUCKETS_CHECK
3778 #if VM_PAGE_FAKE_BUCKETS
3779 extern vm_map_offset_t vm_page_fake_buckets_start
, vm_page_fake_buckets_end
;
3780 #endif /* VM_PAGE_FAKE_BUCKETS */
3781 #endif /* VM_PAGE_BUCKETS_CHECK */
3783 #define FBDP_TEST_COLLAPSE_COMPRESSOR 0
3784 #if FBDP_TEST_COLLAPSE_COMPRESSOR
3785 extern boolean_t vm_object_collapse_compressor_allowed
;
3786 #include <IOKit/IOLib.h>
3787 #endif /* FBDP_TEST_COLLAPSE_COMPRESSOR */
3789 #define FBDP_TEST_WIRE_AND_EXTRACT 0
3790 #if FBDP_TEST_WIRE_AND_EXTRACT
3791 extern ledger_template_t task_ledger_template
;
3792 #include <mach/mach_vm.h>
3793 extern ppnum_t
vm_map_get_phys_page(vm_map_t map
,
3794 vm_offset_t offset
);
3795 #endif /* FBDP_TEST_WIRE_AND_EXTRACT */
3800 thread_t self
= current_thread();
3802 kern_return_t result
;
3806 * Set thread privileges.
3810 self
->priority
= BASEPRI_PREEMPT
- 1;
3811 set_sched_pri(self
, self
->priority
);
3812 thread_unlock(self
);
3814 if (!self
->reserved_stack
)
3815 self
->reserved_stack
= self
->kernel_stack
;
3820 * Initialize some paging parameters.
3823 if (vm_pageout_swap_wait
== 0)
3824 vm_pageout_swap_wait
= VM_PAGEOUT_SWAP_WAIT
;
3826 if (vm_pageout_idle_wait
== 0)
3827 vm_pageout_idle_wait
= VM_PAGEOUT_IDLE_WAIT
;
3829 if (vm_pageout_burst_wait
== 0)
3830 vm_pageout_burst_wait
= VM_PAGEOUT_BURST_WAIT
;
3832 if (vm_pageout_empty_wait
== 0)
3833 vm_pageout_empty_wait
= VM_PAGEOUT_EMPTY_WAIT
;
3835 if (vm_pageout_deadlock_wait
== 0)
3836 vm_pageout_deadlock_wait
= VM_PAGEOUT_DEADLOCK_WAIT
;
3838 if (vm_pageout_deadlock_relief
== 0)
3839 vm_pageout_deadlock_relief
= VM_PAGEOUT_DEADLOCK_RELIEF
;
3841 if (vm_pageout_inactive_relief
== 0)
3842 vm_pageout_inactive_relief
= VM_PAGEOUT_INACTIVE_RELIEF
;
3844 if (vm_pageout_burst_active_throttle
== 0)
3845 vm_pageout_burst_active_throttle
= VM_PAGEOUT_BURST_ACTIVE_THROTTLE
;
3847 if (vm_pageout_burst_inactive_throttle
== 0)
3848 vm_pageout_burst_inactive_throttle
= VM_PAGEOUT_BURST_INACTIVE_THROTTLE
;
3851 vm_page_filecache_min
= (uint32_t) (max_mem
/ PAGE_SIZE
) / 20;
3852 if (vm_page_filecache_min
< VM_PAGE_FILECACHE_MIN
)
3853 vm_page_filecache_min
= VM_PAGE_FILECACHE_MIN
;
3857 * Set kernel task to low backing store privileged
3860 task_lock(kernel_task
);
3861 kernel_task
->priv_flags
|= VM_BACKING_STORE_PRIV
;
3862 task_unlock(kernel_task
);
3864 vm_page_free_count_init
= vm_page_free_count
;
3867 * even if we've already called vm_page_free_reserve
3868 * call it again here to insure that the targets are
3869 * accurately calculated (it uses vm_page_free_count_init)
3870 * calling it with an arg of 0 will not change the reserve
3871 * but will re-calculate free_min and free_target
3873 if (vm_page_free_reserved
< VM_PAGE_FREE_RESERVED(processor_count
)) {
3874 vm_page_free_reserve((VM_PAGE_FREE_RESERVED(processor_count
)) - vm_page_free_reserved
);
3876 vm_page_free_reserve(0);
3879 queue_init(&vm_pageout_queue_external
.pgo_pending
);
3880 vm_pageout_queue_external
.pgo_maxlaundry
= VM_PAGE_LAUNDRY_MAX
;
3881 vm_pageout_queue_external
.pgo_laundry
= 0;
3882 vm_pageout_queue_external
.pgo_idle
= FALSE
;
3883 vm_pageout_queue_external
.pgo_busy
= FALSE
;
3884 vm_pageout_queue_external
.pgo_throttled
= FALSE
;
3885 vm_pageout_queue_external
.pgo_draining
= FALSE
;
3886 vm_pageout_queue_external
.pgo_lowpriority
= FALSE
;
3887 vm_pageout_queue_external
.pgo_tid
= -1;
3888 vm_pageout_queue_external
.pgo_inited
= FALSE
;
3891 queue_init(&vm_pageout_queue_internal
.pgo_pending
);
3892 vm_pageout_queue_internal
.pgo_maxlaundry
= 0;
3893 vm_pageout_queue_internal
.pgo_laundry
= 0;
3894 vm_pageout_queue_internal
.pgo_idle
= FALSE
;
3895 vm_pageout_queue_internal
.pgo_busy
= FALSE
;
3896 vm_pageout_queue_internal
.pgo_throttled
= FALSE
;
3897 vm_pageout_queue_internal
.pgo_draining
= FALSE
;
3898 vm_pageout_queue_internal
.pgo_lowpriority
= FALSE
;
3899 vm_pageout_queue_internal
.pgo_tid
= -1;
3900 vm_pageout_queue_internal
.pgo_inited
= FALSE
;
3902 /* internal pageout thread started when default pager registered first time */
3903 /* external pageout and garbage collection threads started here */
3905 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_iothread_external
, NULL
,
3906 BASEPRI_PREEMPT
- 1,
3907 &vm_pageout_external_iothread
);
3908 if (result
!= KERN_SUCCESS
)
3909 panic("vm_pageout_iothread_external: create failed");
3911 thread_deallocate(vm_pageout_external_iothread
);
3913 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_garbage_collect
, NULL
,
3916 if (result
!= KERN_SUCCESS
)
3917 panic("vm_pageout_garbage_collect: create failed");
3919 thread_deallocate(thread
);
3921 #if VM_PRESSURE_EVENTS
3922 result
= kernel_thread_start_priority((thread_continue_t
)vm_pressure_thread
, NULL
,
3926 if (result
!= KERN_SUCCESS
)
3927 panic("vm_pressure_thread: create failed");
3929 thread_deallocate(thread
);
3932 vm_object_reaper_init();
3934 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
)
3935 vm_compressor_pager_init();
3937 #if VM_PRESSURE_EVENTS
3938 vm_pressure_events_enabled
= TRUE
;
3939 #endif /* VM_PRESSURE_EVENTS */
3941 #if CONFIG_PHANTOM_CACHE
3942 vm_phantom_cache_init();
3944 #if VM_PAGE_BUCKETS_CHECK
3945 #if VM_PAGE_FAKE_BUCKETS
3946 printf("**** DEBUG: protecting fake buckets [0x%llx:0x%llx]\n",
3947 (uint64_t) vm_page_fake_buckets_start
,
3948 (uint64_t) vm_page_fake_buckets_end
);
3949 pmap_protect(kernel_pmap
,
3950 vm_page_fake_buckets_start
,
3951 vm_page_fake_buckets_end
,
3953 // *(char *) vm_page_fake_buckets_start = 'x'; /* panic! */
3954 #endif /* VM_PAGE_FAKE_BUCKETS */
3955 #endif /* VM_PAGE_BUCKETS_CHECK */
3957 #if VM_OBJECT_TRACKING
3958 vm_object_tracking_init();
3959 #endif /* VM_OBJECT_TRACKING */
3962 #if FBDP_TEST_COLLAPSE_COMPRESSOR
3963 vm_object_size_t backing_size
, top_size
;
3964 vm_object_t backing_object
, top_object
;
3965 vm_map_offset_t backing_offset
, top_offset
;
3966 unsigned char *backing_address
, *top_address
;
3969 printf("FBDP_TEST_COLLAPSE_COMPRESSOR:\n");
3971 /* create backing object */
3972 backing_size
= 15 * PAGE_SIZE
;
3973 backing_object
= vm_object_allocate(backing_size
);
3974 assert(backing_object
!= VM_OBJECT_NULL
);
3975 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: created backing object %p\n",
3977 /* map backing object */
3979 kr
= vm_map_enter(kernel_map
, &backing_offset
, backing_size
, 0,
3980 VM_FLAGS_ANYWHERE
, backing_object
, 0, FALSE
,
3981 VM_PROT_DEFAULT
, VM_PROT_DEFAULT
, VM_INHERIT_DEFAULT
);
3982 assert(kr
== KERN_SUCCESS
);
3983 backing_address
= (unsigned char *) backing_offset
;
3984 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
3985 "mapped backing object %p at 0x%llx\n",
3986 backing_object
, (uint64_t) backing_offset
);
3987 /* populate with pages to be compressed in backing object */
3988 backing_address
[0x1*PAGE_SIZE
] = 0xB1;
3989 backing_address
[0x4*PAGE_SIZE
] = 0xB4;
3990 backing_address
[0x7*PAGE_SIZE
] = 0xB7;
3991 backing_address
[0xa*PAGE_SIZE
] = 0xBA;
3992 backing_address
[0xd*PAGE_SIZE
] = 0xBD;
3993 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
3994 "populated pages to be compressed in "
3995 "backing_object %p\n", backing_object
);
3996 /* compress backing object */
3997 vm_object_pageout(backing_object
);
3998 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: compressing backing_object %p\n",
4000 /* wait for all the pages to be gone */
4001 while (*(volatile int *)&backing_object
->resident_page_count
!= 0)
4003 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: backing_object %p compressed\n",
4005 /* populate with pages to be resident in backing object */
4006 backing_address
[0x0*PAGE_SIZE
] = 0xB0;
4007 backing_address
[0x3*PAGE_SIZE
] = 0xB3;
4008 backing_address
[0x6*PAGE_SIZE
] = 0xB6;
4009 backing_address
[0x9*PAGE_SIZE
] = 0xB9;
4010 backing_address
[0xc*PAGE_SIZE
] = 0xBC;
4011 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
4012 "populated pages to be resident in "
4013 "backing_object %p\n", backing_object
);
4014 /* leave the other pages absent */
4015 /* mess with the paging_offset of the backing_object */
4016 assert(backing_object
->paging_offset
== 0);
4017 backing_object
->paging_offset
= 0x3000;
4019 /* create top object */
4020 top_size
= 9 * PAGE_SIZE
;
4021 top_object
= vm_object_allocate(top_size
);
4022 assert(top_object
!= VM_OBJECT_NULL
);
4023 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: created top object %p\n",
4025 /* map top object */
4027 kr
= vm_map_enter(kernel_map
, &top_offset
, top_size
, 0,
4028 VM_FLAGS_ANYWHERE
, top_object
, 0, FALSE
,
4029 VM_PROT_DEFAULT
, VM_PROT_DEFAULT
, VM_INHERIT_DEFAULT
);
4030 assert(kr
== KERN_SUCCESS
);
4031 top_address
= (unsigned char *) top_offset
;
4032 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
4033 "mapped top object %p at 0x%llx\n",
4034 top_object
, (uint64_t) top_offset
);
4035 /* populate with pages to be compressed in top object */
4036 top_address
[0x3*PAGE_SIZE
] = 0xA3;
4037 top_address
[0x4*PAGE_SIZE
] = 0xA4;
4038 top_address
[0x5*PAGE_SIZE
] = 0xA5;
4039 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
4040 "populated pages to be compressed in "
4041 "top_object %p\n", top_object
);
4042 /* compress top object */
4043 vm_object_pageout(top_object
);
4044 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: compressing top_object %p\n",
4046 /* wait for all the pages to be gone */
4047 while (top_object
->resident_page_count
!= 0);
4048 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: top_object %p compressed\n",
4050 /* populate with pages to be resident in top object */
4051 top_address
[0x0*PAGE_SIZE
] = 0xA0;
4052 top_address
[0x1*PAGE_SIZE
] = 0xA1;
4053 top_address
[0x2*PAGE_SIZE
] = 0xA2;
4054 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
4055 "populated pages to be resident in "
4056 "top_object %p\n", top_object
);
4057 /* leave the other pages absent */
4059 /* link the 2 objects */
4060 vm_object_reference(backing_object
);
4061 top_object
->shadow
= backing_object
;
4062 top_object
->vo_shadow_offset
= 0x3000;
4063 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: linked %p and %p\n",
4064 top_object
, backing_object
);
4066 /* unmap backing object */
4067 vm_map_remove(kernel_map
,
4069 backing_offset
+ backing_size
,
4071 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
4072 "unmapped backing_object %p [0x%llx:0x%llx]\n",
4074 (uint64_t) backing_offset
,
4075 (uint64_t) (backing_offset
+ backing_size
));
4078 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: collapsing %p\n", top_object
);
4079 vm_object_lock(top_object
);
4080 vm_object_collapse(top_object
, 0, FALSE
);
4081 vm_object_unlock(top_object
);
4082 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: collapsed %p\n", top_object
);
4085 if (top_object
->shadow
!= VM_OBJECT_NULL
) {
4086 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: not collapsed\n");
4087 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: FAIL\n");
4088 if (vm_object_collapse_compressor_allowed
) {
4089 panic("FBDP_TEST_COLLAPSE_COMPRESSOR: FAIL\n");
4092 /* check the contents of the mapping */
4093 unsigned char expect
[9] =
4094 { 0xA0, 0xA1, 0xA2, /* resident in top */
4095 0xA3, 0xA4, 0xA5, /* compressed in top */
4096 0xB9, /* resident in backing + shadow_offset */
4097 0xBD, /* compressed in backing + shadow_offset + paging_offset */
4098 0x00 }; /* absent in both */
4099 unsigned char actual
[9];
4100 unsigned int i
, errors
;
4103 for (i
= 0; i
< sizeof (actual
); i
++) {
4104 actual
[i
] = (unsigned char) top_address
[i
*PAGE_SIZE
];
4105 if (actual
[i
] != expect
[i
]) {
4109 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
4110 "actual [%x %x %x %x %x %x %x %x %x] "
4111 "expect [%x %x %x %x %x %x %x %x %x] "
4113 actual
[0], actual
[1], actual
[2], actual
[3],
4114 actual
[4], actual
[5], actual
[6], actual
[7],
4116 expect
[0], expect
[1], expect
[2], expect
[3],
4117 expect
[4], expect
[5], expect
[6], expect
[7],
4121 panic("FBDP_TEST_COLLAPSE_COMPRESSOR: FAIL\n");
4123 printf("FBDP_TEST_COLLAPSE_COMPRESSOR: PASS\n");
4126 #endif /* FBDP_TEST_COLLAPSE_COMPRESSOR */
4128 #if FBDP_TEST_WIRE_AND_EXTRACT
4130 vm_map_t user_map
, wire_map
;
4131 mach_vm_address_t user_addr
, wire_addr
;
4132 mach_vm_size_t user_size
, wire_size
;
4133 mach_vm_offset_t cur_offset
;
4134 vm_prot_t cur_prot
, max_prot
;
4135 ppnum_t user_ppnum
, wire_ppnum
;
4138 ledger
= ledger_instantiate(task_ledger_template
,
4139 LEDGER_CREATE_ACTIVE_ENTRIES
);
4140 user_map
= vm_map_create(pmap_create(ledger
, 0, TRUE
),
4144 wire_map
= vm_map_create(NULL
,
4149 user_size
= 0x10000;
4150 kr
= mach_vm_allocate(user_map
,
4154 assert(kr
== KERN_SUCCESS
);
4156 wire_size
= user_size
;
4157 kr
= mach_vm_remap(wire_map
,
4168 assert(kr
== KERN_SUCCESS
);
4169 for (cur_offset
= 0;
4170 cur_offset
< wire_size
;
4171 cur_offset
+= PAGE_SIZE
) {
4172 kr
= vm_map_wire_and_extract(wire_map
,
4173 wire_addr
+ cur_offset
,
4177 assert(kr
== KERN_SUCCESS
);
4178 user_ppnum
= vm_map_get_phys_page(user_map
,
4179 user_addr
+ cur_offset
);
4180 printf("FBDP_TEST_WIRE_AND_EXTRACT: kr=0x%x "
4181 "user[%p:0x%llx:0x%x] wire[%p:0x%llx:0x%x]\n",
4183 user_map
, user_addr
+ cur_offset
, user_ppnum
,
4184 wire_map
, wire_addr
+ cur_offset
, wire_ppnum
);
4185 if (kr
!= KERN_SUCCESS
||
4187 wire_ppnum
!= user_ppnum
) {
4188 panic("FBDP_TEST_WIRE_AND_EXTRACT: FAIL\n");
4191 cur_offset
-= PAGE_SIZE
;
4192 kr
= vm_map_wire_and_extract(wire_map
,
4193 wire_addr
+ cur_offset
,
4197 assert(kr
== KERN_SUCCESS
);
4198 printf("FBDP_TEST_WIRE_AND_EXTRACT: re-wire kr=0x%x "
4199 "user[%p:0x%llx:0x%x] wire[%p:0x%llx:0x%x]\n",
4201 user_map
, user_addr
+ cur_offset
, user_ppnum
,
4202 wire_map
, wire_addr
+ cur_offset
, wire_ppnum
);
4203 if (kr
!= KERN_SUCCESS
||
4205 wire_ppnum
!= user_ppnum
) {
4206 panic("FBDP_TEST_WIRE_AND_EXTRACT: FAIL\n");
4209 printf("FBDP_TEST_WIRE_AND_EXTRACT: PASS\n");
4210 #endif /* FBDP_TEST_WIRE_AND_EXTRACT */
4213 vm_pageout_continue();
4218 * The vm_pageout_continue() call above never returns, so the code below is never
4219 * executed. We take advantage of this to declare several DTrace VM related probe
4220 * points that our kernel doesn't have an analog for. These are probe points that
4221 * exist in Solaris and are in the DTrace documentation, so people may have written
4222 * scripts that use them. Declaring the probe points here means their scripts will
4223 * compile and execute which we want for portability of the scripts, but since this
4224 * section of code is never reached, the probe points will simply never fire. Yes,
4225 * this is basically a hack. The problem is the DTrace probe points were chosen with
4226 * Solaris specific VM events in mind, not portability to different VM implementations.
4229 DTRACE_VM2(execfree
, int, 1, (uint64_t *), NULL
);
4230 DTRACE_VM2(execpgin
, int, 1, (uint64_t *), NULL
);
4231 DTRACE_VM2(execpgout
, int, 1, (uint64_t *), NULL
);
4232 DTRACE_VM2(pgswapin
, int, 1, (uint64_t *), NULL
);
4233 DTRACE_VM2(pgswapout
, int, 1, (uint64_t *), NULL
);
4234 DTRACE_VM2(swapin
, int, 1, (uint64_t *), NULL
);
4235 DTRACE_VM2(swapout
, int, 1, (uint64_t *), NULL
);
4241 #define MAX_COMRPESSOR_THREAD_COUNT 8
4243 struct cq ciq
[MAX_COMRPESSOR_THREAD_COUNT
];
4245 int vm_compressor_thread_count
= 2;
4248 vm_pageout_internal_start(void)
4250 kern_return_t result
;
4252 host_basic_info_data_t hinfo
;
4254 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
4255 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
4257 host_info((host_t
)BSD_HOST
, HOST_BASIC_INFO
, (host_info_t
)&hinfo
, &count
);
4259 assert(hinfo
.max_cpus
> 0);
4261 if (vm_compressor_thread_count
>= hinfo
.max_cpus
)
4262 vm_compressor_thread_count
= hinfo
.max_cpus
- 1;
4263 if (vm_compressor_thread_count
<= 0)
4264 vm_compressor_thread_count
= 1;
4265 else if (vm_compressor_thread_count
> MAX_COMRPESSOR_THREAD_COUNT
)
4266 vm_compressor_thread_count
= MAX_COMRPESSOR_THREAD_COUNT
;
4268 vm_pageout_queue_internal
.pgo_maxlaundry
= (vm_compressor_thread_count
* 4) * VM_PAGE_LAUNDRY_MAX
;
4270 vm_compressor_thread_count
= 1;
4271 vm_pageout_queue_internal
.pgo_maxlaundry
= VM_PAGE_LAUNDRY_MAX
;
4274 for (i
= 0; i
< vm_compressor_thread_count
; i
++) {
4276 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_iothread_internal
, (void *)&ciq
[i
], BASEPRI_PREEMPT
- 1, &vm_pageout_internal_iothread
);
4277 if (result
== KERN_SUCCESS
)
4278 thread_deallocate(vm_pageout_internal_iothread
);
4287 * To support I/O Expedite for compressed files we mark the upls with special flags.
4288 * The way decmpfs works is that we create a big upl which marks all the pages needed to
4289 * represent the compressed file as busy. We tag this upl with the flag UPL_DECMP_REQ. Decmpfs
4290 * then issues smaller I/Os for compressed I/Os, deflates them and puts the data into the pages
4291 * being held in the big original UPL. We mark each of these smaller UPLs with the flag
4292 * UPL_DECMP_REAL_IO. Any outstanding real I/O UPL is tracked by the big req upl using the
4293 * decmp_io_upl field (in the upl structure). This link is protected in the forward direction
4294 * by the req upl lock (the reverse link doesnt need synch. since we never inspect this link
4295 * unless the real I/O upl is being destroyed).
4300 upl_set_decmp_info(upl_t upl
, upl_t src_upl
)
4302 assert((src_upl
->flags
& UPL_DECMP_REQ
) != 0);
4305 if (src_upl
->decmp_io_upl
) {
4307 * If there is already an alive real I/O UPL, ignore this new UPL.
4308 * This case should rarely happen and even if it does, it just means
4309 * that we might issue a spurious expedite which the driver is expected
4312 upl_unlock(src_upl
);
4315 src_upl
->decmp_io_upl
= (void *)upl
;
4316 src_upl
->ref_count
++;
4317 upl_unlock(src_upl
);
4319 upl
->flags
|= UPL_DECMP_REAL_IO
;
4320 upl
->decmp_io_upl
= (void *)src_upl
;
4323 #endif /* CONFIG_IOSCHED */
4326 int upl_debug_enabled
= 1;
4328 int upl_debug_enabled
= 0;
4332 upl_create(int type
, int flags
, upl_size_t size
)
4335 vm_size_t page_field_size
= 0;
4337 vm_size_t upl_size
= sizeof(struct upl
);
4339 size
= round_page_32(size
);
4341 if (type
& UPL_CREATE_LITE
) {
4342 page_field_size
= (atop(size
) + 7) >> 3;
4343 page_field_size
= (page_field_size
+ 3) & 0xFFFFFFFC;
4345 upl_flags
|= UPL_LITE
;
4347 if (type
& UPL_CREATE_INTERNAL
) {
4348 upl_size
+= sizeof(struct upl_page_info
) * atop(size
);
4350 upl_flags
|= UPL_INTERNAL
;
4352 upl
= (upl_t
)kalloc(upl_size
+ page_field_size
);
4354 if (page_field_size
)
4355 bzero((char *)upl
+ upl_size
, page_field_size
);
4357 upl
->flags
= upl_flags
| flags
;
4358 upl
->src_object
= NULL
;
4359 upl
->kaddr
= (vm_offset_t
)0;
4361 upl
->map_object
= NULL
;
4363 upl
->ext_ref_count
= 0;
4364 upl
->highest_page
= 0;
4366 upl
->vector_upl
= NULL
;
4368 if (type
& UPL_CREATE_IO_TRACKING
) {
4369 upl
->upl_priority
= proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO
);
4372 upl
->upl_reprio_info
= 0;
4373 upl
->decmp_io_upl
= 0;
4374 if ((type
& UPL_CREATE_INTERNAL
) && (type
& UPL_CREATE_EXPEDITE_SUP
)) {
4375 /* Only support expedite on internal UPLs */
4376 thread_t curthread
= current_thread();
4377 upl
->upl_reprio_info
= (uint64_t *)kalloc(sizeof(uint64_t) * atop(size
));
4378 bzero(upl
->upl_reprio_info
, (sizeof(uint64_t) * atop(size
)));
4379 upl
->flags
|= UPL_EXPEDITE_SUPPORTED
;
4380 if (curthread
->decmp_upl
!= NULL
)
4381 upl_set_decmp_info(upl
, curthread
->decmp_upl
);
4384 #if CONFIG_IOSCHED || UPL_DEBUG
4385 if ((type
& UPL_CREATE_IO_TRACKING
) || upl_debug_enabled
) {
4386 upl
->upl_creator
= current_thread();
4389 upl
->flags
|= UPL_TRACKED_BY_OBJECT
;
4394 upl
->ubc_alias1
= 0;
4395 upl
->ubc_alias2
= 0;
4398 upl
->upl_commit_index
= 0;
4399 bzero(&upl
->upl_commit_records
[0], sizeof(upl
->upl_commit_records
));
4401 (void) OSBacktrace(&upl
->upl_create_retaddr
[0], UPL_DEBUG_STACK_FRAMES
);
4402 #endif /* UPL_DEBUG */
4408 upl_destroy(upl_t upl
)
4410 int page_field_size
; /* bit field in word size buf */
4413 if (upl
->ext_ref_count
) {
4414 panic("upl(%p) ext_ref_count", upl
);
4418 if ((upl
->flags
& UPL_DECMP_REAL_IO
) && upl
->decmp_io_upl
) {
4420 src_upl
= upl
->decmp_io_upl
;
4421 assert((src_upl
->flags
& UPL_DECMP_REQ
) != 0);
4423 src_upl
->decmp_io_upl
= NULL
;
4424 upl_unlock(src_upl
);
4425 upl_deallocate(src_upl
);
4427 #endif /* CONFIG_IOSCHED */
4429 #if CONFIG_IOSCHED || UPL_DEBUG
4430 if ((upl
->flags
& UPL_TRACKED_BY_OBJECT
) && !(upl
->flags
& UPL_VECTOR
)) {
4433 if (upl
->flags
& UPL_SHADOWED
) {
4434 object
= upl
->map_object
->shadow
;
4436 object
= upl
->map_object
;
4439 vm_object_lock(object
);
4440 queue_remove(&object
->uplq
, upl
, upl_t
, uplq
);
4441 vm_object_activity_end(object
);
4442 vm_object_collapse(object
, 0, TRUE
);
4443 vm_object_unlock(object
);
4447 * drop a reference on the map_object whether or
4448 * not a pageout object is inserted
4450 if (upl
->flags
& UPL_SHADOWED
)
4451 vm_object_deallocate(upl
->map_object
);
4453 if (upl
->flags
& UPL_DEVICE_MEMORY
)
4457 page_field_size
= 0;
4459 if (upl
->flags
& UPL_LITE
) {
4460 page_field_size
= ((size
/PAGE_SIZE
) + 7) >> 3;
4461 page_field_size
= (page_field_size
+ 3) & 0xFFFFFFFC;
4463 upl_lock_destroy(upl
);
4464 upl
->vector_upl
= (vector_upl_t
) 0xfeedbeef;
4467 if (upl
->flags
& UPL_EXPEDITE_SUPPORTED
)
4468 kfree(upl
->upl_reprio_info
, sizeof(uint64_t) * (size
/PAGE_SIZE
));
4471 if (upl
->flags
& UPL_INTERNAL
) {
4473 sizeof(struct upl
) +
4474 (sizeof(struct upl_page_info
) * (size
/PAGE_SIZE
))
4477 kfree(upl
, sizeof(struct upl
) + page_field_size
);
4482 upl_deallocate(upl_t upl
)
4485 if (--upl
->ref_count
== 0) {
4486 if(vector_upl_is_valid(upl
))
4487 vector_upl_deallocate(upl
);
4497 upl_mark_decmp(upl_t upl
)
4499 if (upl
->flags
& UPL_TRACKED_BY_OBJECT
) {
4500 upl
->flags
|= UPL_DECMP_REQ
;
4501 upl
->upl_creator
->decmp_upl
= (void *)upl
;
4506 upl_unmark_decmp(upl_t upl
)
4508 if(upl
&& (upl
->flags
& UPL_DECMP_REQ
)) {
4509 upl
->upl_creator
->decmp_upl
= NULL
;
4513 #endif /* CONFIG_IOSCHED */
4515 #define VM_PAGE_Q_BACKING_UP(q) \
4516 ((q)->pgo_laundry >= (((q)->pgo_maxlaundry * 8) / 10))
4518 boolean_t
must_throttle_writes(void);
4521 must_throttle_writes()
4523 if (VM_PAGE_Q_BACKING_UP(&vm_pageout_queue_external
) &&
4524 vm_page_pageable_external_count
> (AVAILABLE_NON_COMPRESSED_MEMORY
* 6) / 10)
4531 #if DEVELOPMENT || DEBUG
4533 * Statistics about UPL enforcement of copy-on-write obligations.
4535 unsigned long upl_cow
= 0;
4536 unsigned long upl_cow_again
= 0;
4537 unsigned long upl_cow_pages
= 0;
4538 unsigned long upl_cow_again_pages
= 0;
4540 unsigned long iopl_cow
= 0;
4541 unsigned long iopl_cow_pages
= 0;
4545 * Routine: vm_object_upl_request
4547 * Cause the population of a portion of a vm_object.
4548 * Depending on the nature of the request, the pages
4549 * returned may be contain valid data or be uninitialized.
4550 * A page list structure, listing the physical pages
4551 * will be returned upon request.
4552 * This function is called by the file system or any other
4553 * supplier of backing store to a pager.
4554 * IMPORTANT NOTE: The caller must still respect the relationship
4555 * between the vm_object and its backing memory object. The
4556 * caller MUST NOT substitute changes in the backing file
4557 * without first doing a memory_object_lock_request on the
4558 * target range unless it is know that the pages are not
4559 * shared with another entity at the pager level.
4561 * if a page list structure is present
4562 * return the mapped physical pages, where a
4563 * page is not present, return a non-initialized
4564 * one. If the no_sync bit is turned on, don't
4565 * call the pager unlock to synchronize with other
4566 * possible copies of the page. Leave pages busy
4567 * in the original object, if a page list structure
4568 * was specified. When a commit of the page list
4569 * pages is done, the dirty bit will be set for each one.
4571 * If a page list structure is present, return
4572 * all mapped pages. Where a page does not exist
4573 * map a zero filled one. Leave pages busy in
4574 * the original object. If a page list structure
4575 * is not specified, this call is a no-op.
4577 * Note: access of default pager objects has a rather interesting
4578 * twist. The caller of this routine, presumably the file system
4579 * page cache handling code, will never actually make a request
4580 * against a default pager backed object. Only the default
4581 * pager will make requests on backing store related vm_objects
4582 * In this way the default pager can maintain the relationship
4583 * between backing store files (abstract memory objects) and
4584 * the vm_objects (cache objects), they support.
4588 __private_extern__ kern_return_t
4589 vm_object_upl_request(
4591 vm_object_offset_t offset
,
4594 upl_page_info_array_t user_page_list
,
4595 unsigned int *page_list_count
,
4598 vm_page_t dst_page
= VM_PAGE_NULL
;
4599 vm_object_offset_t dst_offset
;
4600 upl_size_t xfer_size
;
4601 unsigned int size_in_pages
;
4606 #if MACH_CLUSTER_STATS
4607 boolean_t encountered_lrp
= FALSE
;
4609 vm_page_t alias_page
= NULL
;
4610 int refmod_state
= 0;
4611 wpl_array_t lite_list
= NULL
;
4612 vm_object_t last_copy_object
;
4613 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
4614 struct vm_page_delayed_work
*dwp
;
4617 int io_tracking_flag
= 0;
4619 if (cntrl_flags
& ~UPL_VALID_FLAGS
) {
4621 * For forward compatibility's sake,
4622 * reject any unknown flag.
4624 return KERN_INVALID_VALUE
;
4626 if ( (!object
->internal
) && (object
->paging_offset
!= 0) )
4627 panic("vm_object_upl_request: external object with non-zero paging offset\n");
4628 if (object
->phys_contiguous
)
4629 panic("vm_object_upl_request: contiguous object specified\n");
4632 if (size
> MAX_UPL_SIZE_BYTES
)
4633 size
= MAX_UPL_SIZE_BYTES
;
4635 if ( (cntrl_flags
& UPL_SET_INTERNAL
) && page_list_count
!= NULL
)
4636 *page_list_count
= MAX_UPL_SIZE_BYTES
>> PAGE_SHIFT
;
4638 #if CONFIG_IOSCHED || UPL_DEBUG
4639 if (object
->io_tracking
|| upl_debug_enabled
)
4640 io_tracking_flag
|= UPL_CREATE_IO_TRACKING
;
4643 if (object
->io_tracking
)
4644 io_tracking_flag
|= UPL_CREATE_EXPEDITE_SUP
;
4647 if (cntrl_flags
& UPL_SET_INTERNAL
) {
4648 if (cntrl_flags
& UPL_SET_LITE
) {
4650 upl
= upl_create(UPL_CREATE_INTERNAL
| UPL_CREATE_LITE
| io_tracking_flag
, 0, size
);
4652 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
4653 lite_list
= (wpl_array_t
)
4654 (((uintptr_t)user_page_list
) +
4655 ((size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
4657 user_page_list
= NULL
;
4661 upl
= upl_create(UPL_CREATE_INTERNAL
| io_tracking_flag
, 0, size
);
4663 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
4665 user_page_list
= NULL
;
4669 if (cntrl_flags
& UPL_SET_LITE
) {
4671 upl
= upl_create(UPL_CREATE_EXTERNAL
| UPL_CREATE_LITE
| io_tracking_flag
, 0, size
);
4673 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
4678 upl
= upl_create(UPL_CREATE_EXTERNAL
| io_tracking_flag
, 0, size
);
4684 user_page_list
[0].device
= FALSE
;
4686 if (cntrl_flags
& UPL_SET_LITE
) {
4687 upl
->map_object
= object
;
4689 upl
->map_object
= vm_object_allocate(size
);
4691 * No neeed to lock the new object: nobody else knows
4692 * about it yet, so it's all ours so far.
4694 upl
->map_object
->shadow
= object
;
4695 upl
->map_object
->pageout
= TRUE
;
4696 upl
->map_object
->can_persist
= FALSE
;
4697 upl
->map_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
4698 upl
->map_object
->vo_shadow_offset
= offset
;
4699 upl
->map_object
->wimg_bits
= object
->wimg_bits
;
4701 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
4703 upl
->flags
|= UPL_SHADOWED
;
4707 * Just mark the UPL as "encrypted" here.
4708 * We'll actually encrypt the pages later,
4709 * in upl_encrypt(), when the caller has
4710 * selected which pages need to go to swap.
4712 if (cntrl_flags
& UPL_ENCRYPT
)
4713 upl
->flags
|= UPL_ENCRYPTED
;
4715 if (cntrl_flags
& UPL_FOR_PAGEOUT
)
4716 upl
->flags
|= UPL_PAGEOUT
;
4718 vm_object_lock(object
);
4719 vm_object_activity_begin(object
);
4722 * we can lock in the paging_offset once paging_in_progress is set
4725 upl
->offset
= offset
+ object
->paging_offset
;
4727 #if CONFIG_IOSCHED || UPL_DEBUG
4728 if (object
->io_tracking
|| upl_debug_enabled
) {
4729 vm_object_activity_begin(object
);
4730 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
4733 if ((cntrl_flags
& UPL_WILL_MODIFY
) && object
->copy
!= VM_OBJECT_NULL
) {
4735 * Honor copy-on-write obligations
4737 * The caller is gathering these pages and
4738 * might modify their contents. We need to
4739 * make sure that the copy object has its own
4740 * private copies of these pages before we let
4741 * the caller modify them.
4743 vm_object_update(object
,
4748 FALSE
, /* should_return */
4749 MEMORY_OBJECT_COPY_SYNC
,
4751 #if DEVELOPMENT || DEBUG
4753 upl_cow_pages
+= size
>> PAGE_SHIFT
;
4757 * remember which copy object we synchronized with
4759 last_copy_object
= object
->copy
;
4763 dst_offset
= offset
;
4764 size_in_pages
= size
/ PAGE_SIZE
;
4768 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
4770 if (vm_page_free_count
> (vm_page_free_target
+ size_in_pages
) ||
4771 object
->resident_page_count
< ((MAX_UPL_SIZE_BYTES
* 2) >> PAGE_SHIFT
))
4772 object
->scan_collisions
= 0;
4774 if ((cntrl_flags
& UPL_WILL_MODIFY
) && must_throttle_writes() == TRUE
) {
4775 boolean_t isSSD
= FALSE
;
4777 vnode_pager_get_isSSD(object
->pager
, &isSSD
);
4778 vm_object_unlock(object
);
4780 OSAddAtomic(size_in_pages
, &vm_upl_wait_for_pages
);
4783 delay(1000 * size_in_pages
);
4785 delay(5000 * size_in_pages
);
4786 OSAddAtomic(-size_in_pages
, &vm_upl_wait_for_pages
);
4788 vm_object_lock(object
);
4795 if ((alias_page
== NULL
) && !(cntrl_flags
& UPL_SET_LITE
)) {
4796 vm_object_unlock(object
);
4797 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
4798 vm_object_lock(object
);
4800 if (cntrl_flags
& UPL_COPYOUT_FROM
) {
4801 upl
->flags
|= UPL_PAGE_SYNC_DONE
;
4803 if ( ((dst_page
= vm_page_lookup(object
, dst_offset
)) == VM_PAGE_NULL
) ||
4804 dst_page
->fictitious
||
4807 dst_page
->cleaning
||
4808 (VM_PAGE_WIRED(dst_page
))) {
4811 user_page_list
[entry
].phys_addr
= 0;
4816 * grab this up front...
4817 * a high percentange of the time we're going to
4818 * need the hardware modification state a bit later
4819 * anyway... so we can eliminate an extra call into
4820 * the pmap layer by grabbing it here and recording it
4822 if (dst_page
->pmapped
)
4823 refmod_state
= pmap_get_refmod(dst_page
->phys_page
);
4827 if ( (refmod_state
& VM_MEM_REFERENCED
) && dst_page
->inactive
) {
4829 * page is on inactive list and referenced...
4830 * reactivate it now... this gets it out of the
4831 * way of vm_pageout_scan which would have to
4832 * reactivate it upon tripping over it
4834 dwp
->dw_mask
|= DW_vm_page_activate
;
4836 if (cntrl_flags
& UPL_RET_ONLY_DIRTY
) {
4838 * we're only asking for DIRTY pages to be returned
4840 if (dst_page
->laundry
|| !(cntrl_flags
& UPL_FOR_PAGEOUT
)) {
4842 * if we were the page stolen by vm_pageout_scan to be
4843 * cleaned (as opposed to a buddy being clustered in
4844 * or this request is not being driven by a PAGEOUT cluster
4845 * then we only need to check for the page being dirty or
4846 * precious to decide whether to return it
4848 if (dst_page
->dirty
|| dst_page
->precious
|| (refmod_state
& VM_MEM_MODIFIED
))
4853 * this is a request for a PAGEOUT cluster and this page
4854 * is merely along for the ride as a 'buddy'... not only
4855 * does it have to be dirty to be returned, but it also
4856 * can't have been referenced recently...
4858 if ( (hibernate_cleaning_in_progress
== TRUE
||
4859 (!((refmod_state
& VM_MEM_REFERENCED
) || dst_page
->reference
) || dst_page
->throttled
)) &&
4860 ((refmod_state
& VM_MEM_MODIFIED
) || dst_page
->dirty
|| dst_page
->precious
) ) {
4865 * if we reach here, we're not to return
4866 * the page... go on to the next one
4868 if (dst_page
->laundry
== TRUE
) {
4870 * if we get here, the page is not 'cleaning' (filtered out above).
4871 * since it has been referenced, remove it from the laundry
4872 * so we don't pay the cost of an I/O to clean a page
4873 * we're just going to take back
4875 vm_page_lockspin_queues();
4877 vm_pageout_steal_laundry(dst_page
, TRUE
);
4878 vm_page_activate(dst_page
);
4880 vm_page_unlock_queues();
4883 user_page_list
[entry
].phys_addr
= 0;
4888 if (dst_page
->busy
) {
4889 if (cntrl_flags
& UPL_NOBLOCK
) {
4891 user_page_list
[entry
].phys_addr
= 0;
4896 * someone else is playing with the
4897 * page. We will have to wait.
4899 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
4905 * The caller is gathering this page and might
4906 * access its contents later on. Decrypt the
4907 * page before adding it to the UPL, so that
4908 * the caller never sees encrypted data.
4910 if (! (cntrl_flags
& UPL_ENCRYPT
) && dst_page
->encrypted
) {
4914 * save the current state of busy
4915 * mark page as busy while decrypt
4916 * is in progress since it will drop
4917 * the object lock...
4919 was_busy
= dst_page
->busy
;
4920 dst_page
->busy
= TRUE
;
4922 vm_page_decrypt(dst_page
, 0);
4923 vm_page_decrypt_for_upl_counter
++;
4925 * restore to original busy state
4927 dst_page
->busy
= was_busy
;
4929 if (dst_page
->pageout_queue
== TRUE
) {
4931 vm_page_lockspin_queues();
4933 if (dst_page
->pageout_queue
== TRUE
) {
4935 * we've buddied up a page for a clustered pageout
4936 * that has already been moved to the pageout
4937 * queue by pageout_scan... we need to remove
4938 * it from the queue and drop the laundry count
4941 vm_pageout_throttle_up(dst_page
);
4943 vm_page_unlock_queues();
4945 #if MACH_CLUSTER_STATS
4947 * pageout statistics gathering. count
4948 * all the pages we will page out that
4949 * were not counted in the initial
4950 * vm_pageout_scan work
4952 if (dst_page
->pageout
)
4953 encountered_lrp
= TRUE
;
4954 if ((dst_page
->dirty
|| (dst_page
->object
->internal
&& dst_page
->precious
))) {
4955 if (encountered_lrp
)
4956 CLUSTER_STAT(pages_at_higher_offsets
++;)
4958 CLUSTER_STAT(pages_at_lower_offsets
++;)
4961 hw_dirty
= refmod_state
& VM_MEM_MODIFIED
;
4962 dirty
= hw_dirty
? TRUE
: dst_page
->dirty
;
4964 if (dst_page
->phys_page
> upl
->highest_page
)
4965 upl
->highest_page
= dst_page
->phys_page
;
4967 if (cntrl_flags
& UPL_SET_LITE
) {
4968 unsigned int pg_num
;
4970 pg_num
= (unsigned int) ((dst_offset
-offset
)/PAGE_SIZE
);
4971 assert(pg_num
== (dst_offset
-offset
)/PAGE_SIZE
);
4972 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
4975 pmap_clear_modify(dst_page
->phys_page
);
4978 * Mark original page as cleaning
4981 dst_page
->cleaning
= TRUE
;
4982 dst_page
->precious
= FALSE
;
4985 * use pageclean setup, it is more
4986 * convenient even for the pageout
4989 vm_object_lock(upl
->map_object
);
4990 vm_pageclean_setup(dst_page
, alias_page
, upl
->map_object
, size
- xfer_size
);
4991 vm_object_unlock(upl
->map_object
);
4993 alias_page
->absent
= FALSE
;
4998 * Record that this page has been
5001 vm_external_state_set(object
->existence_map
, dst_page
->offset
);
5002 #endif /*MACH_PAGEMAP*/
5004 SET_PAGE_DIRTY(dst_page
, FALSE
);
5006 dst_page
->dirty
= FALSE
;
5010 dst_page
->precious
= TRUE
;
5012 if ( (cntrl_flags
& UPL_ENCRYPT
) ) {
5015 * We want to deny access to the target page
5016 * because its contents are about to be
5017 * encrypted and the user would be very
5018 * confused to see encrypted data instead
5020 * We also set "encrypted_cleaning" to allow
5021 * vm_pageout_scan() to demote that page
5022 * from "adjacent/clean-in-place" to
5023 * "target/clean-and-free" if it bumps into
5024 * this page during its scanning while we're
5025 * still processing this cluster.
5027 dst_page
->busy
= TRUE
;
5028 dst_page
->encrypted_cleaning
= TRUE
;
5030 if ( !(cntrl_flags
& UPL_CLEAN_IN_PLACE
) ) {
5031 if ( !VM_PAGE_WIRED(dst_page
))
5032 dst_page
->pageout
= TRUE
;
5035 if ((cntrl_flags
& UPL_WILL_MODIFY
) && object
->copy
!= last_copy_object
) {
5037 * Honor copy-on-write obligations
5039 * The copy object has changed since we
5040 * last synchronized for copy-on-write.
5041 * Another copy object might have been
5042 * inserted while we released the object's
5043 * lock. Since someone could have seen the
5044 * original contents of the remaining pages
5045 * through that new object, we have to
5046 * synchronize with it again for the remaining
5047 * pages only. The previous pages are "busy"
5048 * so they can not be seen through the new
5049 * mapping. The new mapping will see our
5050 * upcoming changes for those previous pages,
5051 * but that's OK since they couldn't see what
5052 * was there before. It's just a race anyway
5053 * and there's no guarantee of consistency or
5054 * atomicity. We just don't want new mappings
5055 * to see both the *before* and *after* pages.
5057 if (object
->copy
!= VM_OBJECT_NULL
) {
5060 dst_offset
,/* current offset */
5061 xfer_size
, /* remaining size */
5064 FALSE
, /* should_return */
5065 MEMORY_OBJECT_COPY_SYNC
,
5068 #if DEVELOPMENT || DEBUG
5070 upl_cow_again_pages
+= xfer_size
>> PAGE_SHIFT
;
5074 * remember the copy object we synced with
5076 last_copy_object
= object
->copy
;
5078 dst_page
= vm_page_lookup(object
, dst_offset
);
5080 if (dst_page
!= VM_PAGE_NULL
) {
5082 if ((cntrl_flags
& UPL_RET_ONLY_ABSENT
)) {
5084 * skip over pages already present in the cache
5087 user_page_list
[entry
].phys_addr
= 0;
5091 if (dst_page
->fictitious
) {
5092 panic("need corner case for fictitious page");
5095 if (dst_page
->busy
|| dst_page
->cleaning
) {
5097 * someone else is playing with the
5098 * page. We will have to wait.
5100 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
5104 if (dst_page
->laundry
) {
5105 dst_page
->pageout
= FALSE
;
5107 vm_pageout_steal_laundry(dst_page
, FALSE
);
5110 if (object
->private) {
5112 * This is a nasty wrinkle for users
5113 * of upl who encounter device or
5114 * private memory however, it is
5115 * unavoidable, only a fault can
5116 * resolve the actual backing
5117 * physical page by asking the
5121 user_page_list
[entry
].phys_addr
= 0;
5125 if (object
->scan_collisions
) {
5127 * the pageout_scan thread is trying to steal
5128 * pages from this object, but has run into our
5129 * lock... grab 2 pages from the head of the object...
5130 * the first is freed on behalf of pageout_scan, the
5131 * 2nd is for our own use... we use vm_object_page_grab
5132 * in both cases to avoid taking pages from the free
5133 * list since we are under memory pressure and our
5134 * lock on this object is getting in the way of
5137 dst_page
= vm_object_page_grab(object
);
5139 if (dst_page
!= VM_PAGE_NULL
)
5140 vm_page_release(dst_page
);
5142 dst_page
= vm_object_page_grab(object
);
5144 if (dst_page
== VM_PAGE_NULL
) {
5146 * need to allocate a page
5148 dst_page
= vm_page_grab();
5150 if (dst_page
== VM_PAGE_NULL
) {
5151 if ( (cntrl_flags
& (UPL_RET_ONLY_ABSENT
| UPL_NOBLOCK
)) == (UPL_RET_ONLY_ABSENT
| UPL_NOBLOCK
)) {
5153 * we don't want to stall waiting for pages to come onto the free list
5154 * while we're already holding absent pages in this UPL
5155 * the caller will deal with the empty slots
5158 user_page_list
[entry
].phys_addr
= 0;
5163 * no pages available... wait
5164 * then try again for the same
5167 vm_object_unlock(object
);
5169 OSAddAtomic(size_in_pages
, &vm_upl_wait_for_pages
);
5171 VM_DEBUG_EVENT(vm_upl_page_wait
, VM_UPL_PAGE_WAIT
, DBG_FUNC_START
, vm_upl_wait_for_pages
, 0, 0, 0);
5174 OSAddAtomic(-size_in_pages
, &vm_upl_wait_for_pages
);
5176 VM_DEBUG_EVENT(vm_upl_page_wait
, VM_UPL_PAGE_WAIT
, DBG_FUNC_END
, vm_upl_wait_for_pages
, 0, 0, 0);
5178 vm_object_lock(object
);
5182 vm_page_insert(dst_page
, object
, dst_offset
);
5184 dst_page
->absent
= TRUE
;
5185 dst_page
->busy
= FALSE
;
5187 if (cntrl_flags
& UPL_RET_ONLY_ABSENT
) {
5189 * if UPL_RET_ONLY_ABSENT was specified,
5190 * than we're definitely setting up a
5191 * upl for a clustered read/pagein
5192 * operation... mark the pages as clustered
5193 * so upl_commit_range can put them on the
5196 dst_page
->clustered
= TRUE
;
5198 if ( !(cntrl_flags
& UPL_FILE_IO
))
5199 VM_STAT_INCR(pageins
);
5205 if (cntrl_flags
& UPL_ENCRYPT
) {
5207 * The page is going to be encrypted when we
5208 * get it from the pager, so mark it so.
5210 dst_page
->encrypted
= TRUE
;
5213 * Otherwise, the page will not contain
5216 dst_page
->encrypted
= FALSE
;
5218 dst_page
->overwriting
= TRUE
;
5220 if (dst_page
->pmapped
) {
5221 if ( !(cntrl_flags
& UPL_FILE_IO
))
5223 * eliminate all mappings from the
5224 * original object and its prodigy
5226 refmod_state
= pmap_disconnect(dst_page
->phys_page
);
5228 refmod_state
= pmap_get_refmod(dst_page
->phys_page
);
5232 hw_dirty
= refmod_state
& VM_MEM_MODIFIED
;
5233 dirty
= hw_dirty
? TRUE
: dst_page
->dirty
;
5235 if (cntrl_flags
& UPL_SET_LITE
) {
5236 unsigned int pg_num
;
5238 pg_num
= (unsigned int) ((dst_offset
-offset
)/PAGE_SIZE
);
5239 assert(pg_num
== (dst_offset
-offset
)/PAGE_SIZE
);
5240 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
5243 pmap_clear_modify(dst_page
->phys_page
);
5246 * Mark original page as cleaning
5249 dst_page
->cleaning
= TRUE
;
5250 dst_page
->precious
= FALSE
;
5253 * use pageclean setup, it is more
5254 * convenient even for the pageout
5257 vm_object_lock(upl
->map_object
);
5258 vm_pageclean_setup(dst_page
, alias_page
, upl
->map_object
, size
- xfer_size
);
5259 vm_object_unlock(upl
->map_object
);
5261 alias_page
->absent
= FALSE
;
5265 if (cntrl_flags
& UPL_REQUEST_SET_DIRTY
) {
5266 upl
->flags
&= ~UPL_CLEAR_DIRTY
;
5267 upl
->flags
|= UPL_SET_DIRTY
;
5269 upl
->flags
|= UPL_SET_DIRTY
;
5270 } else if (cntrl_flags
& UPL_CLEAN_IN_PLACE
) {
5272 * clean in place for read implies
5273 * that a write will be done on all
5274 * the pages that are dirty before
5275 * a upl commit is done. The caller
5276 * is obligated to preserve the
5277 * contents of all pages marked dirty
5279 upl
->flags
|= UPL_CLEAR_DIRTY
;
5281 dst_page
->dirty
= dirty
;
5284 dst_page
->precious
= TRUE
;
5286 if ( !VM_PAGE_WIRED(dst_page
)) {
5288 * deny access to the target page while
5289 * it is being worked on
5291 dst_page
->busy
= TRUE
;
5293 dwp
->dw_mask
|= DW_vm_page_wire
;
5296 * We might be about to satisfy a fault which has been
5297 * requested. So no need for the "restart" bit.
5299 dst_page
->restart
= FALSE
;
5300 if (!dst_page
->absent
&& !(cntrl_flags
& UPL_WILL_MODIFY
)) {
5302 * expect the page to be used
5304 dwp
->dw_mask
|= DW_set_reference
;
5306 if (cntrl_flags
& UPL_PRECIOUS
) {
5307 if (dst_page
->object
->internal
) {
5308 SET_PAGE_DIRTY(dst_page
, FALSE
);
5309 dst_page
->precious
= FALSE
;
5311 dst_page
->precious
= TRUE
;
5314 dst_page
->precious
= FALSE
;
5318 upl
->flags
|= UPL_HAS_BUSY
;
5320 if (dst_page
->phys_page
> upl
->highest_page
)
5321 upl
->highest_page
= dst_page
->phys_page
;
5322 if (user_page_list
) {
5323 user_page_list
[entry
].phys_addr
= dst_page
->phys_page
;
5324 user_page_list
[entry
].pageout
= dst_page
->pageout
;
5325 user_page_list
[entry
].absent
= dst_page
->absent
;
5326 user_page_list
[entry
].dirty
= dst_page
->dirty
;
5327 user_page_list
[entry
].precious
= dst_page
->precious
;
5328 user_page_list
[entry
].device
= FALSE
;
5329 user_page_list
[entry
].needed
= FALSE
;
5330 if (dst_page
->clustered
== TRUE
)
5331 user_page_list
[entry
].speculative
= dst_page
->speculative
;
5333 user_page_list
[entry
].speculative
= FALSE
;
5334 user_page_list
[entry
].cs_validated
= dst_page
->cs_validated
;
5335 user_page_list
[entry
].cs_tainted
= dst_page
->cs_tainted
;
5338 * if UPL_RET_ONLY_ABSENT is set, then
5339 * we are working with a fresh page and we've
5340 * just set the clustered flag on it to
5341 * indicate that it was drug in as part of a
5342 * speculative cluster... so leave it alone
5344 if ( !(cntrl_flags
& UPL_RET_ONLY_ABSENT
)) {
5346 * someone is explicitly grabbing this page...
5347 * update clustered and speculative state
5350 if (dst_page
->clustered
)
5351 VM_PAGE_CONSUME_CLUSTERED(dst_page
);
5355 if (dwp
->dw_mask
& DW_vm_page_activate
)
5356 VM_STAT_INCR(reactivations
);
5358 VM_PAGE_ADD_DELAYED_WORK(dwp
, dst_page
, dw_count
);
5360 if (dw_count
>= dw_limit
) {
5361 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
5368 dst_offset
+= PAGE_SIZE_64
;
5369 xfer_size
-= PAGE_SIZE
;
5372 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
5374 if (alias_page
!= NULL
) {
5375 VM_PAGE_FREE(alias_page
);
5378 if (page_list_count
!= NULL
) {
5379 if (upl
->flags
& UPL_INTERNAL
)
5380 *page_list_count
= 0;
5381 else if (*page_list_count
> entry
)
5382 *page_list_count
= entry
;
5387 vm_object_unlock(object
);
5389 return KERN_SUCCESS
;
5392 /* JMM - Backward compatability for now */
5394 vm_fault_list_request( /* forward */
5395 memory_object_control_t control
,
5396 vm_object_offset_t offset
,
5399 upl_page_info_t
**user_page_list_ptr
,
5400 unsigned int page_list_count
,
5403 vm_fault_list_request(
5404 memory_object_control_t control
,
5405 vm_object_offset_t offset
,
5408 upl_page_info_t
**user_page_list_ptr
,
5409 unsigned int page_list_count
,
5412 unsigned int local_list_count
;
5413 upl_page_info_t
*user_page_list
;
5416 if((cntrl_flags
& UPL_VECTOR
)==UPL_VECTOR
)
5417 return KERN_INVALID_ARGUMENT
;
5419 if (user_page_list_ptr
!= NULL
) {
5420 local_list_count
= page_list_count
;
5421 user_page_list
= *user_page_list_ptr
;
5423 local_list_count
= 0;
5424 user_page_list
= NULL
;
5426 kr
= memory_object_upl_request(control
,
5434 if(kr
!= KERN_SUCCESS
)
5437 if ((user_page_list_ptr
!= NULL
) && (cntrl_flags
& UPL_INTERNAL
)) {
5438 *user_page_list_ptr
= UPL_GET_INTERNAL_PAGE_LIST(*upl_ptr
);
5441 return KERN_SUCCESS
;
5447 * Routine: vm_object_super_upl_request
5449 * Cause the population of a portion of a vm_object
5450 * in much the same way as memory_object_upl_request.
5451 * Depending on the nature of the request, the pages
5452 * returned may be contain valid data or be uninitialized.
5453 * However, the region may be expanded up to the super
5454 * cluster size provided.
5457 __private_extern__ kern_return_t
5458 vm_object_super_upl_request(
5460 vm_object_offset_t offset
,
5462 upl_size_t super_cluster
,
5464 upl_page_info_t
*user_page_list
,
5465 unsigned int *page_list_count
,
5468 if (object
->paging_offset
> offset
|| ((cntrl_flags
& UPL_VECTOR
)==UPL_VECTOR
))
5469 return KERN_FAILURE
;
5471 assert(object
->paging_in_progress
);
5472 offset
= offset
- object
->paging_offset
;
5474 if (super_cluster
> size
) {
5476 vm_object_offset_t base_offset
;
5477 upl_size_t super_size
;
5478 vm_object_size_t super_size_64
;
5480 base_offset
= (offset
& ~((vm_object_offset_t
) super_cluster
- 1));
5481 super_size
= (offset
+ size
) > (base_offset
+ super_cluster
) ? super_cluster
<<1 : super_cluster
;
5482 super_size_64
= ((base_offset
+ super_size
) > object
->vo_size
) ? (object
->vo_size
- base_offset
) : super_size
;
5483 super_size
= (upl_size_t
) super_size_64
;
5484 assert(super_size
== super_size_64
);
5486 if (offset
> (base_offset
+ super_size
)) {
5487 panic("vm_object_super_upl_request: Missed target pageout"
5488 " %#llx,%#llx, %#x, %#x, %#x, %#llx\n",
5489 offset
, base_offset
, super_size
, super_cluster
,
5490 size
, object
->paging_offset
);
5493 * apparently there is a case where the vm requests a
5494 * page to be written out who's offset is beyond the
5497 if ((offset
+ size
) > (base_offset
+ super_size
)) {
5498 super_size_64
= (offset
+ size
) - base_offset
;
5499 super_size
= (upl_size_t
) super_size_64
;
5500 assert(super_size
== super_size_64
);
5503 offset
= base_offset
;
5506 return vm_object_upl_request(object
, offset
, size
, upl
, user_page_list
, page_list_count
, cntrl_flags
);
5513 vm_map_address_t offset
,
5514 upl_size_t
*upl_size
,
5516 upl_page_info_array_t page_list
,
5517 unsigned int *count
,
5520 vm_map_entry_t entry
;
5522 int force_data_sync
;
5524 vm_object_t local_object
;
5525 vm_map_offset_t local_offset
;
5526 vm_map_offset_t local_start
;
5529 caller_flags
= *flags
;
5531 if (caller_flags
& ~UPL_VALID_FLAGS
) {
5533 * For forward compatibility's sake,
5534 * reject any unknown flag.
5536 return KERN_INVALID_VALUE
;
5538 force_data_sync
= (caller_flags
& UPL_FORCE_DATA_SYNC
);
5539 sync_cow_data
= !(caller_flags
& UPL_COPYOUT_FROM
);
5542 return KERN_INVALID_ARGUMENT
;
5545 vm_map_lock_read(map
);
5547 if (vm_map_lookup_entry(map
, offset
, &entry
)) {
5549 if ((entry
->vme_end
- offset
) < *upl_size
) {
5550 *upl_size
= (upl_size_t
) (entry
->vme_end
- offset
);
5551 assert(*upl_size
== entry
->vme_end
- offset
);
5554 if (caller_flags
& UPL_QUERY_OBJECT_TYPE
) {
5557 if ( !entry
->is_sub_map
&& entry
->object
.vm_object
!= VM_OBJECT_NULL
) {
5558 if (entry
->object
.vm_object
->private)
5559 *flags
= UPL_DEV_MEMORY
;
5561 if (entry
->object
.vm_object
->phys_contiguous
)
5562 *flags
|= UPL_PHYS_CONTIG
;
5564 vm_map_unlock_read(map
);
5566 return KERN_SUCCESS
;
5569 if (entry
->is_sub_map
) {
5572 submap
= entry
->object
.sub_map
;
5573 local_start
= entry
->vme_start
;
5574 local_offset
= entry
->offset
;
5576 vm_map_reference(submap
);
5577 vm_map_unlock_read(map
);
5579 ret
= vm_map_create_upl(submap
,
5580 local_offset
+ (offset
- local_start
),
5581 upl_size
, upl
, page_list
, count
, flags
);
5582 vm_map_deallocate(submap
);
5587 if (entry
->object
.vm_object
== VM_OBJECT_NULL
|| !entry
->object
.vm_object
->phys_contiguous
) {
5588 if (*upl_size
> MAX_UPL_SIZE_BYTES
)
5589 *upl_size
= MAX_UPL_SIZE_BYTES
;
5592 * Create an object if necessary.
5594 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
5596 if (vm_map_lock_read_to_write(map
))
5597 goto REDISCOVER_ENTRY
;
5599 entry
->object
.vm_object
= vm_object_allocate((vm_size_t
)(entry
->vme_end
- entry
->vme_start
));
5602 vm_map_lock_write_to_read(map
);
5604 if (!(caller_flags
& UPL_COPYOUT_FROM
)) {
5605 if (!(entry
->protection
& VM_PROT_WRITE
)) {
5606 vm_map_unlock_read(map
);
5607 return KERN_PROTECTION_FAILURE
;
5611 local_object
= entry
->object
.vm_object
;
5612 if (vm_map_entry_should_cow_for_true_share(entry
) &&
5613 local_object
->vo_size
> *upl_size
&&
5618 * Set up the targeted range for copy-on-write to avoid
5619 * applying true_share/copy_delay to the entire object.
5622 if (vm_map_lock_read_to_write(map
)) {
5623 goto REDISCOVER_ENTRY
;
5626 vm_map_clip_start(map
,
5628 vm_map_trunc_page(offset
,
5629 VM_MAP_PAGE_MASK(map
)));
5630 vm_map_clip_end(map
,
5632 vm_map_round_page(offset
+ *upl_size
,
5633 VM_MAP_PAGE_MASK(map
)));
5634 if ((entry
->vme_end
- offset
) < *upl_size
) {
5635 *upl_size
= (upl_size_t
) (entry
->vme_end
- offset
);
5636 assert(*upl_size
== entry
->vme_end
- offset
);
5639 prot
= entry
->protection
& ~VM_PROT_WRITE
;
5640 if (override_nx(map
, entry
->alias
) && prot
)
5641 prot
|= VM_PROT_EXECUTE
;
5642 vm_object_pmap_protect(local_object
,
5644 entry
->vme_end
- entry
->vme_start
,
5645 ((entry
->is_shared
|| map
->mapped_in_other_pmaps
)
5650 entry
->needs_copy
= TRUE
;
5652 vm_map_lock_write_to_read(map
);
5655 if (entry
->needs_copy
) {
5657 * Honor copy-on-write for COPY_SYMMETRIC
5662 vm_object_offset_t new_offset
;
5665 vm_map_version_t version
;
5667 vm_prot_t fault_type
;
5671 if (caller_flags
& UPL_COPYOUT_FROM
) {
5672 fault_type
= VM_PROT_READ
| VM_PROT_COPY
;
5673 vm_counters
.create_upl_extra_cow
++;
5674 vm_counters
.create_upl_extra_cow_pages
+= (entry
->vme_end
- entry
->vme_start
) / PAGE_SIZE
;
5676 fault_type
= VM_PROT_WRITE
;
5678 if (vm_map_lookup_locked(&local_map
,
5680 OBJECT_LOCK_EXCLUSIVE
,
5682 &new_offset
, &prot
, &wired
,
5684 &real_map
) != KERN_SUCCESS
) {
5685 if (fault_type
== VM_PROT_WRITE
) {
5686 vm_counters
.create_upl_lookup_failure_write
++;
5688 vm_counters
.create_upl_lookup_failure_copy
++;
5690 vm_map_unlock_read(local_map
);
5691 return KERN_FAILURE
;
5693 if (real_map
!= map
)
5694 vm_map_unlock(real_map
);
5695 vm_map_unlock_read(local_map
);
5697 vm_object_unlock(object
);
5699 goto REDISCOVER_ENTRY
;
5702 if (sync_cow_data
) {
5703 if (entry
->object
.vm_object
->shadow
|| entry
->object
.vm_object
->copy
) {
5704 local_object
= entry
->object
.vm_object
;
5705 local_start
= entry
->vme_start
;
5706 local_offset
= entry
->offset
;
5708 vm_object_reference(local_object
);
5709 vm_map_unlock_read(map
);
5711 if (local_object
->shadow
&& local_object
->copy
) {
5712 vm_object_lock_request(
5713 local_object
->shadow
,
5714 (vm_object_offset_t
)
5715 ((offset
- local_start
) +
5717 local_object
->vo_shadow_offset
,
5719 MEMORY_OBJECT_DATA_SYNC
,
5722 sync_cow_data
= FALSE
;
5723 vm_object_deallocate(local_object
);
5725 goto REDISCOVER_ENTRY
;
5728 if (force_data_sync
) {
5729 local_object
= entry
->object
.vm_object
;
5730 local_start
= entry
->vme_start
;
5731 local_offset
= entry
->offset
;
5733 vm_object_reference(local_object
);
5734 vm_map_unlock_read(map
);
5736 vm_object_lock_request(
5738 (vm_object_offset_t
)
5739 ((offset
- local_start
) + local_offset
),
5740 (vm_object_size_t
)*upl_size
, FALSE
,
5741 MEMORY_OBJECT_DATA_SYNC
,
5744 force_data_sync
= FALSE
;
5745 vm_object_deallocate(local_object
);
5747 goto REDISCOVER_ENTRY
;
5749 if (entry
->object
.vm_object
->private)
5750 *flags
= UPL_DEV_MEMORY
;
5754 if (entry
->object
.vm_object
->phys_contiguous
)
5755 *flags
|= UPL_PHYS_CONTIG
;
5757 local_object
= entry
->object
.vm_object
;
5758 local_offset
= entry
->offset
;
5759 local_start
= entry
->vme_start
;
5761 vm_object_reference(local_object
);
5762 vm_map_unlock_read(map
);
5764 ret
= vm_object_iopl_request(local_object
,
5765 (vm_object_offset_t
) ((offset
- local_start
) + local_offset
),
5771 vm_object_deallocate(local_object
);
5775 vm_map_unlock_read(map
);
5777 return(KERN_FAILURE
);
5781 * Internal routine to enter a UPL into a VM map.
5783 * JMM - This should just be doable through the standard
5784 * vm_map_enter() API.
5790 vm_map_offset_t
*dst_addr
)
5793 vm_object_offset_t offset
;
5794 vm_map_offset_t addr
;
5797 int isVectorUPL
= 0, curr_upl
=0;
5798 upl_t vector_upl
= NULL
;
5799 vm_offset_t vector_upl_dst_addr
= 0;
5800 vm_map_t vector_upl_submap
= NULL
;
5801 upl_offset_t subupl_offset
= 0;
5802 upl_size_t subupl_size
= 0;
5804 if (upl
== UPL_NULL
)
5805 return KERN_INVALID_ARGUMENT
;
5807 if((isVectorUPL
= vector_upl_is_valid(upl
))) {
5808 int mapped
=0,valid_upls
=0;
5811 upl_lock(vector_upl
);
5812 for(curr_upl
=0; curr_upl
< MAX_VECTOR_UPL_ELEMENTS
; curr_upl
++) {
5813 upl
= vector_upl_subupl_byindex(vector_upl
, curr_upl
);
5817 if (UPL_PAGE_LIST_MAPPED
& upl
->flags
)
5822 if(mapped
!= valid_upls
)
5823 panic("Only %d of the %d sub-upls within the Vector UPL are alread mapped\n", mapped
, valid_upls
);
5825 upl_unlock(vector_upl
);
5826 return KERN_FAILURE
;
5830 kr
= kmem_suballoc(map
, &vector_upl_dst_addr
, vector_upl
->size
, FALSE
, VM_FLAGS_ANYWHERE
, &vector_upl_submap
);
5831 if( kr
!= KERN_SUCCESS
)
5832 panic("Vector UPL submap allocation failed\n");
5833 map
= vector_upl_submap
;
5834 vector_upl_set_submap(vector_upl
, vector_upl_submap
, vector_upl_dst_addr
);
5840 process_upl_to_enter
:
5842 if(curr_upl
== MAX_VECTOR_UPL_ELEMENTS
) {
5843 *dst_addr
= vector_upl_dst_addr
;
5844 upl_unlock(vector_upl
);
5845 return KERN_SUCCESS
;
5847 upl
= vector_upl_subupl_byindex(vector_upl
, curr_upl
++ );
5849 goto process_upl_to_enter
;
5851 vector_upl_get_iostate(vector_upl
, upl
, &subupl_offset
, &subupl_size
);
5852 *dst_addr
= (vm_map_offset_t
)(vector_upl_dst_addr
+ (vm_map_offset_t
)subupl_offset
);
5855 * check to see if already mapped
5857 if (UPL_PAGE_LIST_MAPPED
& upl
->flags
) {
5859 return KERN_FAILURE
;
5862 if ((!(upl
->flags
& UPL_SHADOWED
)) &&
5863 ((upl
->flags
& UPL_HAS_BUSY
) ||
5864 !((upl
->flags
& (UPL_DEVICE_MEMORY
| UPL_IO_WIRE
)) || (upl
->map_object
->phys_contiguous
)))) {
5867 vm_page_t alias_page
;
5868 vm_object_offset_t new_offset
;
5869 unsigned int pg_num
;
5870 wpl_array_t lite_list
;
5872 if (upl
->flags
& UPL_INTERNAL
) {
5873 lite_list
= (wpl_array_t
)
5874 ((((uintptr_t)upl
) + sizeof(struct upl
))
5875 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
5877 lite_list
= (wpl_array_t
)(((uintptr_t)upl
) + sizeof(struct upl
));
5879 object
= upl
->map_object
;
5880 upl
->map_object
= vm_object_allocate(upl
->size
);
5882 vm_object_lock(upl
->map_object
);
5884 upl
->map_object
->shadow
= object
;
5885 upl
->map_object
->pageout
= TRUE
;
5886 upl
->map_object
->can_persist
= FALSE
;
5887 upl
->map_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
5888 upl
->map_object
->vo_shadow_offset
= upl
->offset
- object
->paging_offset
;
5889 upl
->map_object
->wimg_bits
= object
->wimg_bits
;
5890 offset
= upl
->map_object
->vo_shadow_offset
;
5894 upl
->flags
|= UPL_SHADOWED
;
5897 pg_num
= (unsigned int) (new_offset
/ PAGE_SIZE
);
5898 assert(pg_num
== new_offset
/ PAGE_SIZE
);
5900 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
5902 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
5904 vm_object_lock(object
);
5906 m
= vm_page_lookup(object
, offset
);
5907 if (m
== VM_PAGE_NULL
) {
5908 panic("vm_upl_map: page missing\n");
5912 * Convert the fictitious page to a private
5913 * shadow of the real page.
5915 assert(alias_page
->fictitious
);
5916 alias_page
->fictitious
= FALSE
;
5917 alias_page
->private = TRUE
;
5918 alias_page
->pageout
= TRUE
;
5920 * since m is a page in the upl it must
5921 * already be wired or BUSY, so it's
5922 * safe to assign the underlying physical
5925 alias_page
->phys_page
= m
->phys_page
;
5927 vm_object_unlock(object
);
5929 vm_page_lockspin_queues();
5930 vm_page_wire(alias_page
);
5931 vm_page_unlock_queues();
5935 * The virtual page ("m") has to be wired in some way
5936 * here or its physical page ("m->phys_page") could
5937 * be recycled at any time.
5938 * Assuming this is enforced by the caller, we can't
5939 * get an encrypted page here. Since the encryption
5940 * key depends on the VM page's "pager" object and
5941 * the "paging_offset", we couldn't handle 2 pageable
5942 * VM pages (with different pagers and paging_offsets)
5943 * sharing the same physical page: we could end up
5944 * encrypting with one key (via one VM page) and
5945 * decrypting with another key (via the alias VM page).
5947 ASSERT_PAGE_DECRYPTED(m
);
5949 vm_page_insert(alias_page
, upl
->map_object
, new_offset
);
5951 assert(!alias_page
->wanted
);
5952 alias_page
->busy
= FALSE
;
5953 alias_page
->absent
= FALSE
;
5956 offset
+= PAGE_SIZE_64
;
5957 new_offset
+= PAGE_SIZE_64
;
5959 vm_object_unlock(upl
->map_object
);
5961 if (upl
->flags
& UPL_SHADOWED
)
5964 offset
= upl
->offset
- upl
->map_object
->paging_offset
;
5968 vm_object_reference(upl
->map_object
);
5973 * NEED A UPL_MAP ALIAS
5975 kr
= vm_map_enter(map
, dst_addr
, (vm_map_size_t
)size
, (vm_map_offset_t
) 0,
5976 VM_FLAGS_ANYWHERE
, upl
->map_object
, offset
, FALSE
,
5977 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
5979 if (kr
!= KERN_SUCCESS
) {
5985 kr
= vm_map_enter(map
, dst_addr
, (vm_map_size_t
)size
, (vm_map_offset_t
) 0,
5986 VM_FLAGS_FIXED
, upl
->map_object
, offset
, FALSE
,
5987 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
5989 panic("vm_map_enter failed for a Vector UPL\n");
5991 vm_object_lock(upl
->map_object
);
5993 for (addr
= *dst_addr
; size
> 0; size
-= PAGE_SIZE
, addr
+= PAGE_SIZE
) {
5994 m
= vm_page_lookup(upl
->map_object
, offset
);
5999 /* CODE SIGNING ENFORCEMENT: page has been wpmapped,
6000 * but only in kernel space. If this was on a user map,
6001 * we'd have to set the wpmapped bit. */
6002 /* m->wpmapped = TRUE; */
6003 assert(map
->pmap
== kernel_pmap
);
6005 PMAP_ENTER(map
->pmap
, addr
, m
, VM_PROT_DEFAULT
, VM_PROT_NONE
, 0, TRUE
);
6007 offset
+= PAGE_SIZE_64
;
6009 vm_object_unlock(upl
->map_object
);
6012 * hold a reference for the mapping
6015 upl
->flags
|= UPL_PAGE_LIST_MAPPED
;
6016 upl
->kaddr
= (vm_offset_t
) *dst_addr
;
6017 assert(upl
->kaddr
== *dst_addr
);
6020 goto process_upl_to_enter
;
6024 return KERN_SUCCESS
;
6028 * Internal routine to remove a UPL mapping from a VM map.
6030 * XXX - This should just be doable through a standard
6031 * vm_map_remove() operation. Otherwise, implicit clean-up
6032 * of the target map won't be able to correctly remove
6033 * these (and release the reference on the UPL). Having
6034 * to do this means we can't map these into user-space
6044 int isVectorUPL
= 0, curr_upl
= 0;
6045 upl_t vector_upl
= NULL
;
6047 if (upl
== UPL_NULL
)
6048 return KERN_INVALID_ARGUMENT
;
6050 if((isVectorUPL
= vector_upl_is_valid(upl
))) {
6051 int unmapped
=0, valid_upls
=0;
6053 upl_lock(vector_upl
);
6054 for(curr_upl
=0; curr_upl
< MAX_VECTOR_UPL_ELEMENTS
; curr_upl
++) {
6055 upl
= vector_upl_subupl_byindex(vector_upl
, curr_upl
);
6059 if (!(UPL_PAGE_LIST_MAPPED
& upl
->flags
))
6064 if(unmapped
!= valid_upls
)
6065 panic("%d of the %d sub-upls within the Vector UPL is/are not mapped\n", unmapped
, valid_upls
);
6067 upl_unlock(vector_upl
);
6068 return KERN_FAILURE
;
6076 process_upl_to_remove
:
6078 if(curr_upl
== MAX_VECTOR_UPL_ELEMENTS
) {
6079 vm_map_t v_upl_submap
;
6080 vm_offset_t v_upl_submap_dst_addr
;
6081 vector_upl_get_submap(vector_upl
, &v_upl_submap
, &v_upl_submap_dst_addr
);
6083 vm_map_remove(map
, v_upl_submap_dst_addr
, v_upl_submap_dst_addr
+ vector_upl
->size
, VM_MAP_NO_FLAGS
);
6084 vm_map_deallocate(v_upl_submap
);
6085 upl_unlock(vector_upl
);
6086 return KERN_SUCCESS
;
6089 upl
= vector_upl_subupl_byindex(vector_upl
, curr_upl
++ );
6091 goto process_upl_to_remove
;
6094 if (upl
->flags
& UPL_PAGE_LIST_MAPPED
) {
6098 assert(upl
->ref_count
> 1);
6099 upl
->ref_count
--; /* removing mapping ref */
6101 upl
->flags
&= ~UPL_PAGE_LIST_MAPPED
;
6102 upl
->kaddr
= (vm_offset_t
) 0;
6109 vm_map_trunc_page(addr
,
6110 VM_MAP_PAGE_MASK(map
)),
6111 vm_map_round_page(addr
+ size
,
6112 VM_MAP_PAGE_MASK(map
)),
6115 return KERN_SUCCESS
;
6119 * If it's a Vectored UPL, we'll be removing the entire
6120 * submap anyways, so no need to remove individual UPL
6121 * element mappings from within the submap
6123 goto process_upl_to_remove
;
6128 return KERN_FAILURE
;
6134 upl_offset_t offset
,
6137 upl_page_info_t
*page_list
,
6138 mach_msg_type_number_t count
,
6141 upl_size_t xfer_size
, subupl_size
= size
;
6142 vm_object_t shadow_object
;
6144 vm_object_offset_t target_offset
;
6145 upl_offset_t subupl_offset
= offset
;
6147 wpl_array_t lite_list
;
6149 int clear_refmod
= 0;
6150 int pgpgout_count
= 0;
6151 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
6152 struct vm_page_delayed_work
*dwp
;
6155 int isVectorUPL
= 0;
6156 upl_t vector_upl
= NULL
;
6157 boolean_t should_be_throttled
= FALSE
;
6159 vm_page_t nxt_page
= VM_PAGE_NULL
;
6160 int fast_path_possible
= 0;
6161 int fast_path_full_commit
= 0;
6162 int throttle_page
= 0;
6163 int unwired_count
= 0;
6164 int local_queue_count
= 0;
6165 queue_head_t local_queue
;
6169 if (upl
== UPL_NULL
)
6170 return KERN_INVALID_ARGUMENT
;
6175 if((isVectorUPL
= vector_upl_is_valid(upl
))) {
6177 upl_lock(vector_upl
);
6182 process_upl_to_commit
:
6186 offset
= subupl_offset
;
6188 upl_unlock(vector_upl
);
6189 return KERN_SUCCESS
;
6191 upl
= vector_upl_subupl_byoffset(vector_upl
, &offset
, &size
);
6193 upl_unlock(vector_upl
);
6194 return KERN_FAILURE
;
6196 page_list
= UPL_GET_INTERNAL_PAGE_LIST_SIMPLE(upl
);
6197 subupl_size
-= size
;
6198 subupl_offset
+= size
;
6202 if (upl
->upl_commit_index
< UPL_DEBUG_COMMIT_RECORDS
) {
6203 (void) OSBacktrace(&upl
->upl_commit_records
[upl
->upl_commit_index
].c_retaddr
[0], UPL_DEBUG_STACK_FRAMES
);
6205 upl
->upl_commit_records
[upl
->upl_commit_index
].c_beg
= offset
;
6206 upl
->upl_commit_records
[upl
->upl_commit_index
].c_end
= (offset
+ size
);
6208 upl
->upl_commit_index
++;
6211 if (upl
->flags
& UPL_DEVICE_MEMORY
)
6213 else if ((offset
+ size
) <= upl
->size
)
6219 upl_unlock(vector_upl
);
6221 return KERN_FAILURE
;
6223 if (upl
->flags
& UPL_SET_DIRTY
)
6224 flags
|= UPL_COMMIT_SET_DIRTY
;
6225 if (upl
->flags
& UPL_CLEAR_DIRTY
)
6226 flags
|= UPL_COMMIT_CLEAR_DIRTY
;
6228 if (upl
->flags
& UPL_INTERNAL
)
6229 lite_list
= (wpl_array_t
) ((((uintptr_t)upl
) + sizeof(struct upl
))
6230 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
6232 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
6234 object
= upl
->map_object
;
6236 if (upl
->flags
& UPL_SHADOWED
) {
6237 vm_object_lock(object
);
6238 shadow_object
= object
->shadow
;
6240 shadow_object
= object
;
6242 entry
= offset
/PAGE_SIZE
;
6243 target_offset
= (vm_object_offset_t
)offset
;
6245 if (upl
->flags
& UPL_KERNEL_OBJECT
)
6246 vm_object_lock_shared(shadow_object
);
6248 vm_object_lock(shadow_object
);
6250 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
6251 assert(shadow_object
->blocked_access
);
6252 shadow_object
->blocked_access
= FALSE
;
6253 vm_object_wakeup(object
, VM_OBJECT_EVENT_UNBLOCKED
);
6256 if (shadow_object
->code_signed
) {
6259 * If the object is code-signed, do not let this UPL tell
6260 * us if the pages are valid or not. Let the pages be
6261 * validated by VM the normal way (when they get mapped or
6264 flags
&= ~UPL_COMMIT_CS_VALIDATED
;
6268 * No page list to get the code-signing info from !?
6270 flags
&= ~UPL_COMMIT_CS_VALIDATED
;
6272 if (!VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
) && shadow_object
->internal
)
6273 should_be_throttled
= TRUE
;
6277 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
6279 if ((upl
->flags
& UPL_IO_WIRE
) &&
6280 !(flags
& UPL_COMMIT_FREE_ABSENT
) &&
6282 shadow_object
->purgable
!= VM_PURGABLE_VOLATILE
&&
6283 shadow_object
->purgable
!= VM_PURGABLE_EMPTY
) {
6285 if (!queue_empty(&shadow_object
->memq
)) {
6286 queue_init(&local_queue
);
6287 if (size
== shadow_object
->vo_size
) {
6288 nxt_page
= (vm_page_t
)queue_first(&shadow_object
->memq
);
6289 fast_path_full_commit
= 1;
6291 fast_path_possible
= 1;
6293 if (!VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
) && shadow_object
->internal
&&
6294 (shadow_object
->purgable
== VM_PURGABLE_DENY
||
6295 shadow_object
->purgable
== VM_PURGABLE_NONVOLATILE
||
6296 shadow_object
->purgable
== VM_PURGABLE_VOLATILE
)) {
6310 if (upl
->flags
& UPL_LITE
) {
6311 unsigned int pg_num
;
6313 if (nxt_page
!= VM_PAGE_NULL
) {
6315 nxt_page
= (vm_page_t
)queue_next(&nxt_page
->listq
);
6316 target_offset
= m
->offset
;
6318 pg_num
= (unsigned int) (target_offset
/PAGE_SIZE
);
6319 assert(pg_num
== target_offset
/PAGE_SIZE
);
6321 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
6322 lite_list
[pg_num
>>5] &= ~(1 << (pg_num
& 31));
6324 if (!(upl
->flags
& UPL_KERNEL_OBJECT
) && m
== VM_PAGE_NULL
)
6325 m
= vm_page_lookup(shadow_object
, target_offset
+ (upl
->offset
- shadow_object
->paging_offset
));
6329 if (upl
->flags
& UPL_SHADOWED
) {
6330 if ((t
= vm_page_lookup(object
, target_offset
)) != VM_PAGE_NULL
) {
6336 if (!(upl
->flags
& UPL_KERNEL_OBJECT
) && m
== VM_PAGE_NULL
)
6337 m
= vm_page_lookup(shadow_object
, target_offset
+ object
->vo_shadow_offset
);
6340 if (m
== VM_PAGE_NULL
)
6341 goto commit_next_page
;
6343 if (m
->compressor
) {
6346 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
6347 goto commit_next_page
;
6350 if (flags
& UPL_COMMIT_CS_VALIDATED
) {
6353 * Set the code signing bits according to
6354 * what the UPL says they should be.
6356 m
->cs_validated
= page_list
[entry
].cs_validated
;
6357 m
->cs_tainted
= page_list
[entry
].cs_tainted
;
6359 if (flags
& UPL_COMMIT_WRITTEN_BY_KERNEL
)
6360 m
->written_by_kernel
= TRUE
;
6362 if (upl
->flags
& UPL_IO_WIRE
) {
6365 page_list
[entry
].phys_addr
= 0;
6367 if (flags
& UPL_COMMIT_SET_DIRTY
) {
6368 SET_PAGE_DIRTY(m
, FALSE
);
6369 } else if (flags
& UPL_COMMIT_CLEAR_DIRTY
) {
6372 if (! (flags
& UPL_COMMIT_CS_VALIDATED
) &&
6373 m
->cs_validated
&& !m
->cs_tainted
) {
6376 * This page is no longer dirty
6377 * but could have been modified,
6378 * so it will need to be
6382 panic("upl_commit_range(%p): page %p was slid\n",
6386 m
->cs_validated
= FALSE
;
6387 #if DEVELOPMENT || DEBUG
6388 vm_cs_validated_resets
++;
6390 pmap_disconnect(m
->phys_page
);
6392 clear_refmod
|= VM_MEM_MODIFIED
;
6394 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
6396 * We blocked access to the pages in this UPL.
6397 * Clear the "busy" bit and wake up any waiter
6400 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
6402 if (fast_path_possible
) {
6403 assert(m
->object
->purgable
!= VM_PURGABLE_EMPTY
);
6404 assert(m
->object
->purgable
!= VM_PURGABLE_VOLATILE
);
6406 assert(m
->wire_count
== 0);
6410 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
6412 if (m
->wire_count
== 0)
6413 panic("wire_count == 0, m = %p, obj = %p\n", m
, shadow_object
);
6416 * XXX FBDP need to update some other
6417 * counters here (purgeable_wired_count)
6420 assert(m
->wire_count
);
6423 if (m
->wire_count
== 0)
6426 if (m
->wire_count
== 0) {
6427 queue_enter(&local_queue
, m
, vm_page_t
, pageq
);
6428 local_queue_count
++;
6430 if (throttle_page
) {
6431 m
->throttled
= TRUE
;
6433 if (flags
& UPL_COMMIT_INACTIVATE
)
6440 if (flags
& UPL_COMMIT_INACTIVATE
) {
6441 dwp
->dw_mask
|= DW_vm_page_deactivate_internal
;
6442 clear_refmod
|= VM_MEM_REFERENCED
;
6445 if (flags
& UPL_COMMIT_FREE_ABSENT
)
6446 dwp
->dw_mask
|= DW_vm_page_free
;
6449 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
6451 if ( !(dwp
->dw_mask
& DW_vm_page_deactivate_internal
))
6452 dwp
->dw_mask
|= DW_vm_page_activate
;
6455 dwp
->dw_mask
|= DW_vm_page_unwire
;
6457 goto commit_next_page
;
6459 assert(!m
->compressor
);
6462 page_list
[entry
].phys_addr
= 0;
6465 * make sure to clear the hardware
6466 * modify or reference bits before
6467 * releasing the BUSY bit on this page
6468 * otherwise we risk losing a legitimate
6471 if (flags
& UPL_COMMIT_CLEAR_DIRTY
) {
6474 clear_refmod
|= VM_MEM_MODIFIED
;
6477 dwp
->dw_mask
|= DW_vm_pageout_throttle_up
;
6479 if (VM_PAGE_WIRED(m
))
6482 if (! (flags
& UPL_COMMIT_CS_VALIDATED
) &&
6483 m
->cs_validated
&& !m
->cs_tainted
) {
6486 * This page is no longer dirty
6487 * but could have been modified,
6488 * so it will need to be
6492 panic("upl_commit_range(%p): page %p was slid\n",
6496 m
->cs_validated
= FALSE
;
6497 #if DEVELOPMENT || DEBUG
6498 vm_cs_validated_resets
++;
6500 pmap_disconnect(m
->phys_page
);
6502 if (m
->overwriting
) {
6504 * the (COPY_OUT_FROM == FALSE) request_page_list case
6507 #if CONFIG_PHANTOM_CACHE
6508 if (m
->absent
&& !m
->object
->internal
)
6509 dwp
->dw_mask
|= DW_vm_phantom_cache_update
;
6513 dwp
->dw_mask
|= DW_clear_busy
;
6516 * alternate (COPY_OUT_FROM == FALSE) page_list case
6517 * Occurs when the original page was wired
6518 * at the time of the list request
6520 assert(VM_PAGE_WIRED(m
));
6522 dwp
->dw_mask
|= DW_vm_page_unwire
; /* reactivates */
6524 m
->overwriting
= FALSE
;
6526 if (m
->encrypted_cleaning
== TRUE
) {
6527 m
->encrypted_cleaning
= FALSE
;
6529 dwp
->dw_mask
|= DW_clear_busy
| DW_PAGE_WAKEUP
;
6531 m
->cleaning
= FALSE
;
6535 * With the clean queue enabled, UPL_PAGEOUT should
6536 * no longer set the pageout bit. It's pages now go
6537 * to the clean queue.
6539 assert(!(flags
& UPL_PAGEOUT
));
6542 #if MACH_CLUSTER_STATS
6543 if (m
->wanted
) vm_pageout_target_collisions
++;
6545 if ((flags
& UPL_COMMIT_SET_DIRTY
) ||
6546 (m
->pmapped
&& (pmap_disconnect(m
->phys_page
) & VM_MEM_MODIFIED
))) {
6548 * page was re-dirtied after we started
6549 * the pageout... reactivate it since
6550 * we don't know whether the on-disk
6551 * copy matches what is now in memory
6553 SET_PAGE_DIRTY(m
, FALSE
);
6555 dwp
->dw_mask
|= DW_vm_page_activate
| DW_PAGE_WAKEUP
;
6557 if (upl
->flags
& UPL_PAGEOUT
) {
6558 CLUSTER_STAT(vm_pageout_target_page_dirtied
++;)
6559 VM_STAT_INCR(reactivations
);
6560 DTRACE_VM2(pgrec
, int, 1, (uint64_t *), NULL
);
6564 * page has been successfully cleaned
6565 * go ahead and free it for other use
6567 if (m
->object
->internal
) {
6568 DTRACE_VM2(anonpgout
, int, 1, (uint64_t *), NULL
);
6570 DTRACE_VM2(fspgout
, int, 1, (uint64_t *), NULL
);
6575 dwp
->dw_mask
|= DW_vm_page_free
;
6577 goto commit_next_page
;
6579 #if MACH_CLUSTER_STATS
6581 m
->dirty
= pmap_is_modified(m
->phys_page
);
6583 if (m
->dirty
) vm_pageout_cluster_dirtied
++;
6584 else vm_pageout_cluster_cleaned
++;
6585 if (m
->wanted
) vm_pageout_cluster_collisions
++;
6588 * It is a part of the semantic of COPYOUT_FROM
6589 * UPLs that a commit implies cache sync
6590 * between the vm page and the backing store
6591 * this can be used to strip the precious bit
6594 if ((upl
->flags
& UPL_PAGE_SYNC_DONE
) || (flags
& UPL_COMMIT_CLEAR_PRECIOUS
))
6595 m
->precious
= FALSE
;
6597 if (flags
& UPL_COMMIT_SET_DIRTY
) {
6598 SET_PAGE_DIRTY(m
, FALSE
);
6603 /* with the clean queue on, move *all* cleaned pages to the clean queue */
6604 if (hibernate_cleaning_in_progress
== FALSE
&& !m
->dirty
&& (upl
->flags
& UPL_PAGEOUT
)) {
6607 VM_STAT_INCR(pageouts
);
6608 DTRACE_VM2(pgout
, int, 1, (uint64_t *), NULL
);
6610 dwp
->dw_mask
|= DW_enqueue_cleaned
;
6611 vm_pageout_enqueued_cleaned_from_inactive_dirty
++;
6612 } else if (should_be_throttled
== TRUE
&& !m
->active
&& !m
->inactive
&& !m
->speculative
&& !m
->throttled
) {
6614 * page coming back in from being 'frozen'...
6615 * it was dirty before it was frozen, so keep it so
6616 * the vm_page_activate will notice that it really belongs
6617 * on the throttle queue and put it there
6619 SET_PAGE_DIRTY(m
, FALSE
);
6620 dwp
->dw_mask
|= DW_vm_page_activate
;
6623 if ((flags
& UPL_COMMIT_INACTIVATE
) && !m
->clustered
&& !m
->speculative
) {
6624 dwp
->dw_mask
|= DW_vm_page_deactivate_internal
;
6625 clear_refmod
|= VM_MEM_REFERENCED
;
6626 } else if (!m
->active
&& !m
->inactive
&& !m
->speculative
) {
6628 if (m
->clustered
|| (flags
& UPL_COMMIT_SPECULATE
))
6629 dwp
->dw_mask
|= DW_vm_page_speculate
;
6630 else if (m
->reference
)
6631 dwp
->dw_mask
|= DW_vm_page_activate
;
6633 dwp
->dw_mask
|= DW_vm_page_deactivate_internal
;
6634 clear_refmod
|= VM_MEM_REFERENCED
;
6638 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
6640 * We blocked access to the pages in this URL.
6641 * Clear the "busy" bit on this page before we
6642 * wake up any waiter.
6644 dwp
->dw_mask
|= DW_clear_busy
;
6647 * Wakeup any thread waiting for the page to be un-cleaning.
6649 dwp
->dw_mask
|= DW_PAGE_WAKEUP
;
6653 pmap_clear_refmod(m
->phys_page
, clear_refmod
);
6655 target_offset
+= PAGE_SIZE_64
;
6656 xfer_size
-= PAGE_SIZE
;
6660 if (dwp
->dw_mask
& ~(DW_clear_busy
| DW_PAGE_WAKEUP
)) {
6661 VM_PAGE_ADD_DELAYED_WORK(dwp
, m
, dw_count
);
6663 if (dw_count
>= dw_limit
) {
6664 vm_page_do_delayed_work(shadow_object
, &dw_array
[0], dw_count
);
6670 if (dwp
->dw_mask
& DW_clear_busy
)
6673 if (dwp
->dw_mask
& DW_PAGE_WAKEUP
)
6679 vm_page_do_delayed_work(shadow_object
, &dw_array
[0], dw_count
);
6681 if (fast_path_possible
) {
6683 assert(shadow_object
->purgable
!= VM_PURGABLE_VOLATILE
);
6684 assert(shadow_object
->purgable
!= VM_PURGABLE_EMPTY
);
6686 if (local_queue_count
|| unwired_count
) {
6688 if (local_queue_count
) {
6689 vm_page_t first_local
, last_local
;
6690 vm_page_t first_target
;
6691 queue_head_t
*target_queue
;
6694 target_queue
= &vm_page_queue_throttled
;
6696 if (flags
& UPL_COMMIT_INACTIVATE
) {
6697 if (shadow_object
->internal
)
6698 target_queue
= &vm_page_queue_anonymous
;
6700 target_queue
= &vm_page_queue_inactive
;
6702 target_queue
= &vm_page_queue_active
;
6705 * Transfer the entire local queue to a regular LRU page queues.
6707 first_local
= (vm_page_t
) queue_first(&local_queue
);
6708 last_local
= (vm_page_t
) queue_last(&local_queue
);
6710 vm_page_lockspin_queues();
6712 first_target
= (vm_page_t
) queue_first(target_queue
);
6714 if (queue_empty(target_queue
))
6715 queue_last(target_queue
) = (queue_entry_t
) last_local
;
6717 queue_prev(&first_target
->pageq
) = (queue_entry_t
) last_local
;
6719 queue_first(target_queue
) = (queue_entry_t
) first_local
;
6720 queue_prev(&first_local
->pageq
) = (queue_entry_t
) target_queue
;
6721 queue_next(&last_local
->pageq
) = (queue_entry_t
) first_target
;
6724 * Adjust the global page counts.
6726 if (throttle_page
) {
6727 vm_page_throttled_count
+= local_queue_count
;
6729 if (flags
& UPL_COMMIT_INACTIVATE
) {
6730 if (shadow_object
->internal
)
6731 vm_page_anonymous_count
+= local_queue_count
;
6732 vm_page_inactive_count
+= local_queue_count
;
6734 token_new_pagecount
+= local_queue_count
;
6736 vm_page_active_count
+= local_queue_count
;
6738 if (shadow_object
->internal
)
6739 vm_page_pageable_internal_count
+= local_queue_count
;
6741 vm_page_pageable_external_count
+= local_queue_count
;
6744 vm_page_lockspin_queues();
6746 if (unwired_count
) {
6747 vm_page_wire_count
-= unwired_count
;
6748 VM_CHECK_MEMORYSTATUS
;
6750 vm_page_unlock_queues();
6752 shadow_object
->wired_page_count
-= unwired_count
;
6757 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
6759 } else if (upl
->flags
& UPL_LITE
) {
6765 if (!fast_path_full_commit
) {
6766 pg_num
= upl
->size
/PAGE_SIZE
;
6767 pg_num
= (pg_num
+ 31) >> 5;
6769 for (i
= 0; i
< pg_num
; i
++) {
6770 if (lite_list
[i
] != 0) {
6777 if (queue_empty(&upl
->map_object
->memq
))
6780 if (occupied
== 0) {
6782 * If this UPL element belongs to a Vector UPL and is
6783 * empty, then this is the right function to deallocate
6784 * it. So go ahead set the *empty variable. The flag
6785 * UPL_COMMIT_NOTIFY_EMPTY, from the caller's point of view
6786 * should be considered relevant for the Vector UPL and not
6787 * the internal UPLs.
6789 if ((upl
->flags
& UPL_COMMIT_NOTIFY_EMPTY
) || isVectorUPL
)
6792 if (object
== shadow_object
&& !(upl
->flags
& UPL_KERNEL_OBJECT
)) {
6794 * this is not a paging object
6795 * so we need to drop the paging reference
6796 * that was taken when we created the UPL
6797 * against this object
6799 vm_object_activity_end(shadow_object
);
6800 vm_object_collapse(shadow_object
, 0, TRUE
);
6803 * we dontated the paging reference to
6804 * the map object... vm_pageout_object_terminate
6805 * will drop this reference
6809 vm_object_unlock(shadow_object
);
6810 if (object
!= shadow_object
)
6811 vm_object_unlock(object
);
6817 * If we completed our operations on an UPL that is
6818 * part of a Vectored UPL and if empty is TRUE, then
6819 * we should go ahead and deallocate this UPL element.
6820 * Then we check if this was the last of the UPL elements
6821 * within that Vectored UPL. If so, set empty to TRUE
6822 * so that in ubc_upl_commit_range or ubc_upl_commit, we
6823 * can go ahead and deallocate the Vector UPL too.
6826 *empty
= vector_upl_set_subupl(vector_upl
, upl
, 0);
6827 upl_deallocate(upl
);
6829 goto process_upl_to_commit
;
6832 if (pgpgout_count
) {
6833 DTRACE_VM2(pgpgout
, int, pgpgout_count
, (uint64_t *), NULL
);
6836 return KERN_SUCCESS
;
6842 upl_offset_t offset
,
6847 upl_page_info_t
*user_page_list
= NULL
;
6848 upl_size_t xfer_size
, subupl_size
= size
;
6849 vm_object_t shadow_object
;
6851 vm_object_offset_t target_offset
;
6852 upl_offset_t subupl_offset
= offset
;
6854 wpl_array_t lite_list
;
6856 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
6857 struct vm_page_delayed_work
*dwp
;
6860 int isVectorUPL
= 0;
6861 upl_t vector_upl
= NULL
;
6865 if (upl
== UPL_NULL
)
6866 return KERN_INVALID_ARGUMENT
;
6868 if ( (upl
->flags
& UPL_IO_WIRE
) && !(error
& UPL_ABORT_DUMP_PAGES
) )
6869 return upl_commit_range(upl
, offset
, size
, UPL_COMMIT_FREE_ABSENT
, NULL
, 0, empty
);
6871 if((isVectorUPL
= vector_upl_is_valid(upl
))) {
6873 upl_lock(vector_upl
);
6878 process_upl_to_abort
:
6881 offset
= subupl_offset
;
6883 upl_unlock(vector_upl
);
6884 return KERN_SUCCESS
;
6886 upl
= vector_upl_subupl_byoffset(vector_upl
, &offset
, &size
);
6888 upl_unlock(vector_upl
);
6889 return KERN_FAILURE
;
6891 subupl_size
-= size
;
6892 subupl_offset
+= size
;
6898 if (upl
->upl_commit_index
< UPL_DEBUG_COMMIT_RECORDS
) {
6899 (void) OSBacktrace(&upl
->upl_commit_records
[upl
->upl_commit_index
].c_retaddr
[0], UPL_DEBUG_STACK_FRAMES
);
6901 upl
->upl_commit_records
[upl
->upl_commit_index
].c_beg
= offset
;
6902 upl
->upl_commit_records
[upl
->upl_commit_index
].c_end
= (offset
+ size
);
6903 upl
->upl_commit_records
[upl
->upl_commit_index
].c_aborted
= 1;
6905 upl
->upl_commit_index
++;
6908 if (upl
->flags
& UPL_DEVICE_MEMORY
)
6910 else if ((offset
+ size
) <= upl
->size
)
6916 upl_unlock(vector_upl
);
6919 return KERN_FAILURE
;
6921 if (upl
->flags
& UPL_INTERNAL
) {
6922 lite_list
= (wpl_array_t
)
6923 ((((uintptr_t)upl
) + sizeof(struct upl
))
6924 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
6926 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
6928 lite_list
= (wpl_array_t
)
6929 (((uintptr_t)upl
) + sizeof(struct upl
));
6931 object
= upl
->map_object
;
6933 if (upl
->flags
& UPL_SHADOWED
) {
6934 vm_object_lock(object
);
6935 shadow_object
= object
->shadow
;
6937 shadow_object
= object
;
6939 entry
= offset
/PAGE_SIZE
;
6940 target_offset
= (vm_object_offset_t
)offset
;
6942 if (upl
->flags
& UPL_KERNEL_OBJECT
)
6943 vm_object_lock_shared(shadow_object
);
6945 vm_object_lock(shadow_object
);
6947 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
6948 assert(shadow_object
->blocked_access
);
6949 shadow_object
->blocked_access
= FALSE
;
6950 vm_object_wakeup(object
, VM_OBJECT_EVENT_UNBLOCKED
);
6955 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
6957 if ((error
& UPL_ABORT_DUMP_PAGES
) && (upl
->flags
& UPL_KERNEL_OBJECT
))
6958 panic("upl_abort_range: kernel_object being DUMPED");
6962 unsigned int pg_num
;
6965 pg_num
= (unsigned int) (target_offset
/PAGE_SIZE
);
6966 assert(pg_num
== target_offset
/PAGE_SIZE
);
6971 needed
= user_page_list
[pg_num
].needed
;
6976 if (upl
->flags
& UPL_LITE
) {
6978 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
6979 lite_list
[pg_num
>>5] &= ~(1 << (pg_num
& 31));
6981 if ( !(upl
->flags
& UPL_KERNEL_OBJECT
))
6982 m
= vm_page_lookup(shadow_object
, target_offset
+
6983 (upl
->offset
- shadow_object
->paging_offset
));
6986 if (upl
->flags
& UPL_SHADOWED
) {
6987 if ((t
= vm_page_lookup(object
, target_offset
)) != VM_PAGE_NULL
) {
6992 if (m
== VM_PAGE_NULL
)
6993 m
= vm_page_lookup(shadow_object
, target_offset
+ object
->vo_shadow_offset
);
6996 if ((upl
->flags
& UPL_KERNEL_OBJECT
))
6997 goto abort_next_page
;
6999 if (m
!= VM_PAGE_NULL
) {
7001 assert(!m
->compressor
);
7004 boolean_t must_free
= TRUE
;
7007 * COPYOUT = FALSE case
7008 * check for error conditions which must
7009 * be passed back to the pages customer
7011 if (error
& UPL_ABORT_RESTART
) {
7016 } else if (error
& UPL_ABORT_UNAVAILABLE
) {
7020 } else if (error
& UPL_ABORT_ERROR
) {
7027 if (m
->clustered
&& needed
== FALSE
) {
7029 * This page was a part of a speculative
7030 * read-ahead initiated by the kernel
7031 * itself. No one is expecting this
7032 * page and no one will clean up its
7033 * error state if it ever becomes valid
7035 * We have to free it here.
7042 * If the page was already encrypted,
7043 * we don't really need to decrypt it
7044 * now. It will get decrypted later,
7045 * on demand, as soon as someone needs
7046 * to access its contents.
7049 m
->cleaning
= FALSE
;
7050 m
->encrypted_cleaning
= FALSE
;
7052 if (m
->overwriting
&& !m
->busy
) {
7054 * this shouldn't happen since
7055 * this is an 'absent' page, but
7056 * it doesn't hurt to check for
7057 * the 'alternate' method of
7058 * stabilizing the page...
7059 * we will mark 'busy' to be cleared
7060 * in the following code which will
7061 * take care of the primary stabilzation
7062 * method (i.e. setting 'busy' to TRUE)
7064 dwp
->dw_mask
|= DW_vm_page_unwire
;
7066 m
->overwriting
= FALSE
;
7068 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
7070 if (must_free
== TRUE
)
7071 dwp
->dw_mask
|= DW_vm_page_free
;
7073 dwp
->dw_mask
|= DW_vm_page_activate
;
7076 * Handle the trusted pager throttle.
7079 dwp
->dw_mask
|= DW_vm_pageout_throttle_up
;
7081 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
7083 * We blocked access to the pages in this UPL.
7084 * Clear the "busy" bit and wake up any waiter
7087 dwp
->dw_mask
|= DW_clear_busy
;
7089 if (m
->overwriting
) {
7091 dwp
->dw_mask
|= DW_clear_busy
;
7094 * deal with the 'alternate' method
7095 * of stabilizing the page...
7096 * we will either free the page
7097 * or mark 'busy' to be cleared
7098 * in the following code which will
7099 * take care of the primary stabilzation
7100 * method (i.e. setting 'busy' to TRUE)
7102 dwp
->dw_mask
|= DW_vm_page_unwire
;
7104 m
->overwriting
= FALSE
;
7106 if (m
->encrypted_cleaning
== TRUE
) {
7107 m
->encrypted_cleaning
= FALSE
;
7109 dwp
->dw_mask
|= DW_clear_busy
;
7112 m
->cleaning
= FALSE
;
7114 vm_external_state_clr(m
->object
->existence_map
, m
->offset
);
7115 #endif /* MACH_PAGEMAP */
7116 if (error
& UPL_ABORT_DUMP_PAGES
) {
7117 pmap_disconnect(m
->phys_page
);
7119 dwp
->dw_mask
|= DW_vm_page_free
;
7121 if (!(dwp
->dw_mask
& DW_vm_page_unwire
)) {
7122 if (error
& UPL_ABORT_REFERENCE
) {
7124 * we've been told to explictly
7125 * reference this page... for
7126 * file I/O, this is done by
7127 * implementing an LRU on the inactive q
7129 dwp
->dw_mask
|= DW_vm_page_lru
;
7131 } else if (!m
->active
&& !m
->inactive
&& !m
->speculative
)
7132 dwp
->dw_mask
|= DW_vm_page_deactivate_internal
;
7134 dwp
->dw_mask
|= DW_PAGE_WAKEUP
;
7139 target_offset
+= PAGE_SIZE_64
;
7140 xfer_size
-= PAGE_SIZE
;
7144 if (dwp
->dw_mask
& ~(DW_clear_busy
| DW_PAGE_WAKEUP
)) {
7145 VM_PAGE_ADD_DELAYED_WORK(dwp
, m
, dw_count
);
7147 if (dw_count
>= dw_limit
) {
7148 vm_page_do_delayed_work(shadow_object
, &dw_array
[0], dw_count
);
7154 if (dwp
->dw_mask
& DW_clear_busy
)
7157 if (dwp
->dw_mask
& DW_PAGE_WAKEUP
)
7163 vm_page_do_delayed_work(shadow_object
, &dw_array
[0], dw_count
);
7167 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
7169 } else if (upl
->flags
& UPL_LITE
) {
7173 pg_num
= upl
->size
/PAGE_SIZE
;
7174 pg_num
= (pg_num
+ 31) >> 5;
7177 for (i
= 0; i
< pg_num
; i
++) {
7178 if (lite_list
[i
] != 0) {
7184 if (queue_empty(&upl
->map_object
->memq
))
7187 if (occupied
== 0) {
7189 * If this UPL element belongs to a Vector UPL and is
7190 * empty, then this is the right function to deallocate
7191 * it. So go ahead set the *empty variable. The flag
7192 * UPL_COMMIT_NOTIFY_EMPTY, from the caller's point of view
7193 * should be considered relevant for the Vector UPL and
7194 * not the internal UPLs.
7196 if ((upl
->flags
& UPL_COMMIT_NOTIFY_EMPTY
) || isVectorUPL
)
7199 if (object
== shadow_object
&& !(upl
->flags
& UPL_KERNEL_OBJECT
)) {
7201 * this is not a paging object
7202 * so we need to drop the paging reference
7203 * that was taken when we created the UPL
7204 * against this object
7206 vm_object_activity_end(shadow_object
);
7207 vm_object_collapse(shadow_object
, 0, TRUE
);
7210 * we dontated the paging reference to
7211 * the map object... vm_pageout_object_terminate
7212 * will drop this reference
7216 vm_object_unlock(shadow_object
);
7217 if (object
!= shadow_object
)
7218 vm_object_unlock(object
);
7224 * If we completed our operations on an UPL that is
7225 * part of a Vectored UPL and if empty is TRUE, then
7226 * we should go ahead and deallocate this UPL element.
7227 * Then we check if this was the last of the UPL elements
7228 * within that Vectored UPL. If so, set empty to TRUE
7229 * so that in ubc_upl_abort_range or ubc_upl_abort, we
7230 * can go ahead and deallocate the Vector UPL too.
7232 if(*empty
== TRUE
) {
7233 *empty
= vector_upl_set_subupl(vector_upl
, upl
,0);
7234 upl_deallocate(upl
);
7236 goto process_upl_to_abort
;
7239 return KERN_SUCCESS
;
7250 return upl_abort_range(upl
, 0, upl
->size
, error
, &empty
);
7254 /* an option on commit should be wire */
7258 upl_page_info_t
*page_list
,
7259 mach_msg_type_number_t count
)
7263 return upl_commit_range(upl
, 0, upl
->size
, 0, page_list
, count
, &empty
);
7273 vm_page_t m
, nxt_page
= VM_PAGE_NULL
;
7275 int wired_count
= 0;
7278 panic("iopl_valid_data: NULL upl");
7279 if (vector_upl_is_valid(upl
))
7280 panic("iopl_valid_data: vector upl");
7281 if ((upl
->flags
& (UPL_DEVICE_MEMORY
|UPL_SHADOWED
|UPL_ACCESS_BLOCKED
|UPL_IO_WIRE
|UPL_INTERNAL
)) != UPL_IO_WIRE
)
7282 panic("iopl_valid_data: unsupported upl, flags = %x", upl
->flags
);
7284 object
= upl
->map_object
;
7286 if (object
== kernel_object
|| object
== compressor_object
)
7287 panic("iopl_valid_data: object == kernel or compressor");
7289 if (object
->purgable
== VM_PURGABLE_VOLATILE
)
7290 panic("iopl_valid_data: object == VM_PURGABLE_VOLATILE");
7294 vm_object_lock(object
);
7296 if (object
->vo_size
== size
&& object
->resident_page_count
== (size
/ PAGE_SIZE
))
7297 nxt_page
= (vm_page_t
)queue_first(&object
->memq
);
7299 offset
= 0 + upl
->offset
- object
->paging_offset
;
7303 if (nxt_page
!= VM_PAGE_NULL
) {
7305 nxt_page
= (vm_page_t
)queue_next(&nxt_page
->listq
);
7307 m
= vm_page_lookup(object
, offset
);
7308 offset
+= PAGE_SIZE
;
7310 if (m
== VM_PAGE_NULL
)
7311 panic("iopl_valid_data: missing expected page at offset %lx", (long)offset
);
7315 panic("iopl_valid_data: busy page w/o absent");
7317 if (m
->pageq
.next
|| m
->pageq
.prev
)
7318 panic("iopl_valid_data: busy+absent page on page queue");
7325 PAGE_WAKEUP_DONE(m
);
7330 object
->wired_page_count
+= wired_count
;
7332 vm_page_lockspin_queues();
7333 vm_page_wire_count
+= wired_count
;
7334 vm_page_unlock_queues();
7336 vm_object_unlock(object
);
7343 vm_object_set_pmap_cache_attr(
7345 upl_page_info_array_t user_page_list
,
7346 unsigned int num_pages
,
7347 boolean_t batch_pmap_op
)
7349 unsigned int cache_attr
= 0;
7351 cache_attr
= object
->wimg_bits
& VM_WIMG_MASK
;
7352 assert(user_page_list
);
7353 if (cache_attr
!= VM_WIMG_USE_DEFAULT
) {
7354 PMAP_BATCH_SET_CACHE_ATTR(object
, user_page_list
, cache_attr
, num_pages
, batch_pmap_op
);
7358 unsigned int vm_object_iopl_request_sleep_for_cleaning
= 0;
7361 vm_object_iopl_request(
7363 vm_object_offset_t offset
,
7366 upl_page_info_array_t user_page_list
,
7367 unsigned int *page_list_count
,
7371 vm_object_offset_t dst_offset
;
7372 upl_size_t xfer_size
;
7375 wpl_array_t lite_list
= NULL
;
7376 int no_zero_fill
= FALSE
;
7377 unsigned int size_in_pages
;
7381 struct vm_object_fault_info fault_info
;
7382 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
7383 struct vm_page_delayed_work
*dwp
;
7387 boolean_t caller_lookup
;
7388 int io_tracking_flag
= 0;
7391 boolean_t set_cache_attr_needed
= FALSE
;
7392 boolean_t free_wired_pages
= FALSE
;
7393 int fast_path_possible
= 0;
7396 if (cntrl_flags
& ~UPL_VALID_FLAGS
) {
7398 * For forward compatibility's sake,
7399 * reject any unknown flag.
7401 return KERN_INVALID_VALUE
;
7403 if (vm_lopage_needed
== FALSE
)
7404 cntrl_flags
&= ~UPL_NEED_32BIT_ADDR
;
7406 if (cntrl_flags
& UPL_NEED_32BIT_ADDR
) {
7407 if ( (cntrl_flags
& (UPL_SET_IO_WIRE
| UPL_SET_LITE
)) != (UPL_SET_IO_WIRE
| UPL_SET_LITE
))
7408 return KERN_INVALID_VALUE
;
7410 if (object
->phys_contiguous
) {
7411 if ((offset
+ object
->vo_shadow_offset
) >= (vm_object_offset_t
)max_valid_dma_address
)
7412 return KERN_INVALID_ADDRESS
;
7414 if (((offset
+ object
->vo_shadow_offset
) + size
) >= (vm_object_offset_t
)max_valid_dma_address
)
7415 return KERN_INVALID_ADDRESS
;
7419 if (cntrl_flags
& UPL_ENCRYPT
) {
7422 * The paging path doesn't use this interface,
7423 * so we don't support the UPL_ENCRYPT flag
7424 * here. We won't encrypt the pages.
7426 assert(! (cntrl_flags
& UPL_ENCRYPT
));
7428 if (cntrl_flags
& (UPL_NOZEROFILL
| UPL_NOZEROFILLIO
))
7429 no_zero_fill
= TRUE
;
7431 if (cntrl_flags
& UPL_COPYOUT_FROM
)
7432 prot
= VM_PROT_READ
;
7434 prot
= VM_PROT_READ
| VM_PROT_WRITE
;
7436 if ((!object
->internal
) && (object
->paging_offset
!= 0))
7437 panic("vm_object_iopl_request: external object with non-zero paging offset\n");
7439 #if CONFIG_IOSCHED || UPL_DEBUG
7440 if ((object
->io_tracking
&& object
!= kernel_object
) || upl_debug_enabled
)
7441 io_tracking_flag
|= UPL_CREATE_IO_TRACKING
;
7445 if (object
->io_tracking
) {
7446 /* Check if we're dealing with the kernel object. We do not support expedite on kernel object UPLs */
7447 if (object
!= kernel_object
)
7448 io_tracking_flag
|= UPL_CREATE_EXPEDITE_SUP
;
7452 if (object
->phys_contiguous
)
7457 if (cntrl_flags
& UPL_SET_INTERNAL
) {
7458 upl
= upl_create(UPL_CREATE_INTERNAL
| UPL_CREATE_LITE
| io_tracking_flag
, UPL_IO_WIRE
, psize
);
7460 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
7461 lite_list
= (wpl_array_t
) (((uintptr_t)user_page_list
) +
7462 ((psize
/ PAGE_SIZE
) * sizeof(upl_page_info_t
)));
7464 user_page_list
= NULL
;
7468 upl
= upl_create(UPL_CREATE_LITE
| io_tracking_flag
, UPL_IO_WIRE
, psize
);
7470 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
7476 user_page_list
[0].device
= FALSE
;
7479 upl
->map_object
= object
;
7482 size_in_pages
= size
/ PAGE_SIZE
;
7484 if (object
== kernel_object
&&
7485 !(cntrl_flags
& (UPL_NEED_32BIT_ADDR
| UPL_BLOCK_ACCESS
))) {
7486 upl
->flags
|= UPL_KERNEL_OBJECT
;
7488 vm_object_lock(object
);
7490 vm_object_lock_shared(object
);
7493 vm_object_lock(object
);
7494 vm_object_activity_begin(object
);
7497 * paging in progress also protects the paging_offset
7499 upl
->offset
= offset
+ object
->paging_offset
;
7501 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
7503 * The user requested that access to the pages in this UPL
7504 * be blocked until the UPL is commited or aborted.
7506 upl
->flags
|= UPL_ACCESS_BLOCKED
;
7509 if (!(cntrl_flags
& (UPL_NEED_32BIT_ADDR
| UPL_BLOCK_ACCESS
)) &&
7510 object
->purgable
!= VM_PURGABLE_VOLATILE
&&
7511 object
->purgable
!= VM_PURGABLE_EMPTY
&&
7512 object
->copy
== NULL
&&
7513 size
== object
->vo_size
&&
7515 object
->resident_page_count
== 0 &&
7516 object
->shadow
== NULL
&&
7517 object
->pager
== NULL
)
7519 fast_path_possible
= 1;
7520 set_cache_attr_needed
= TRUE
;
7523 #if CONFIG_IOSCHED || UPL_DEBUG
7524 if (upl
->flags
& UPL_TRACKED_BY_OBJECT
) {
7525 vm_object_activity_begin(object
);
7526 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
7530 if (object
->phys_contiguous
) {
7532 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
7533 assert(!object
->blocked_access
);
7534 object
->blocked_access
= TRUE
;
7537 vm_object_unlock(object
);
7540 * don't need any shadow mappings for this one
7541 * since it is already I/O memory
7543 upl
->flags
|= UPL_DEVICE_MEMORY
;
7545 upl
->highest_page
= (ppnum_t
) ((offset
+ object
->vo_shadow_offset
+ size
- 1)>>PAGE_SHIFT
);
7547 if (user_page_list
) {
7548 user_page_list
[0].phys_addr
= (ppnum_t
) ((offset
+ object
->vo_shadow_offset
)>>PAGE_SHIFT
);
7549 user_page_list
[0].device
= TRUE
;
7551 if (page_list_count
!= NULL
) {
7552 if (upl
->flags
& UPL_INTERNAL
)
7553 *page_list_count
= 0;
7555 *page_list_count
= 1;
7557 return KERN_SUCCESS
;
7559 if (object
!= kernel_object
&& object
!= compressor_object
) {
7561 * Protect user space from future COW operations
7563 #if VM_OBJECT_TRACKING_OP_TRUESHARE
7564 if (!object
->true_share
&&
7565 vm_object_tracking_inited
) {
7566 void *bt
[VM_OBJECT_TRACKING_BTDEPTH
];
7569 num
= OSBacktrace(bt
,
7570 VM_OBJECT_TRACKING_BTDEPTH
);
7571 btlog_add_entry(vm_object_tracking_btlog
,
7573 VM_OBJECT_TRACKING_OP_TRUESHARE
,
7577 #endif /* VM_OBJECT_TRACKING_OP_TRUESHARE */
7579 object
->true_share
= TRUE
;
7581 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
)
7582 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
7585 if (!(cntrl_flags
& UPL_COPYOUT_FROM
) &&
7586 object
->copy
!= VM_OBJECT_NULL
) {
7588 * Honor copy-on-write obligations
7590 * The caller is gathering these pages and
7591 * might modify their contents. We need to
7592 * make sure that the copy object has its own
7593 * private copies of these pages before we let
7594 * the caller modify them.
7596 * NOTE: someone else could map the original object
7597 * after we've done this copy-on-write here, and they
7598 * could then see an inconsistent picture of the memory
7599 * while it's being modified via the UPL. To prevent this,
7600 * we would have to block access to these pages until the
7601 * UPL is released. We could use the UPL_BLOCK_ACCESS
7602 * code path for that...
7604 vm_object_update(object
,
7609 FALSE
, /* should_return */
7610 MEMORY_OBJECT_COPY_SYNC
,
7612 #if DEVELOPMENT || DEBUG
7614 iopl_cow_pages
+= size
>> PAGE_SHIFT
;
7617 if (cntrl_flags
& UPL_SET_INTERRUPTIBLE
)
7618 interruptible
= THREAD_ABORTSAFE
;
7620 interruptible
= THREAD_UNINT
;
7625 dst_offset
= offset
;
7628 if (fast_path_possible
) {
7629 int wired_count
= 0;
7633 while ( (dst_page
= vm_page_grab()) == VM_PAGE_NULL
) {
7634 OSAddAtomic(size_in_pages
, &vm_upl_wait_for_pages
);
7636 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_START
, vm_upl_wait_for_pages
, 0, 0, 0);
7638 if (vm_page_wait(interruptible
) == FALSE
) {
7642 OSAddAtomic(-size_in_pages
, &vm_upl_wait_for_pages
);
7644 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_END
, vm_upl_wait_for_pages
, 0, 0, -1);
7647 vm_page_lockspin_queues();
7648 vm_page_wire_count
+= wired_count
;
7649 vm_page_unlock_queues();
7651 free_wired_pages
= TRUE
;
7653 ret
= MACH_SEND_INTERRUPTED
;
7657 OSAddAtomic(-size_in_pages
, &vm_upl_wait_for_pages
);
7659 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_END
, vm_upl_wait_for_pages
, 0, 0, 0);
7661 if (no_zero_fill
== FALSE
)
7662 vm_page_zero_fill(dst_page
);
7664 dst_page
->absent
= TRUE
;
7666 dst_page
->reference
= TRUE
;
7668 if (!(cntrl_flags
& UPL_COPYOUT_FROM
)) {
7669 SET_PAGE_DIRTY(dst_page
, FALSE
);
7671 if (dst_page
->absent
== FALSE
) {
7672 assert(object
->purgable
!= VM_PURGABLE_VOLATILE
);
7673 assert(object
->purgable
!= VM_PURGABLE_EMPTY
);
7674 dst_page
->wire_count
++;
7677 PAGE_WAKEUP_DONE(dst_page
);
7679 vm_page_insert_internal(dst_page
, object
, dst_offset
, FALSE
, TRUE
, TRUE
);
7681 lite_list
[entry
>>5] |= 1 << (entry
& 31);
7683 if (dst_page
->phys_page
> upl
->highest_page
)
7684 upl
->highest_page
= dst_page
->phys_page
;
7686 if (user_page_list
) {
7687 user_page_list
[entry
].phys_addr
= dst_page
->phys_page
;
7688 user_page_list
[entry
].absent
= dst_page
->absent
;
7689 user_page_list
[entry
].dirty
= dst_page
->dirty
;
7690 user_page_list
[entry
].precious
= FALSE
;
7691 user_page_list
[entry
].pageout
= FALSE
;
7692 user_page_list
[entry
].device
= FALSE
;
7693 user_page_list
[entry
].needed
= FALSE
;
7694 user_page_list
[entry
].speculative
= FALSE
;
7695 user_page_list
[entry
].cs_validated
= FALSE
;
7696 user_page_list
[entry
].cs_tainted
= FALSE
;
7699 dst_offset
+= PAGE_SIZE_64
;
7700 xfer_size
-= PAGE_SIZE
;
7704 vm_page_lockspin_queues();
7705 vm_page_wire_count
+= wired_count
;
7706 vm_page_unlock_queues();
7711 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
7712 fault_info
.user_tag
= 0;
7713 fault_info
.lo_offset
= offset
;
7714 fault_info
.hi_offset
= offset
+ xfer_size
;
7715 fault_info
.no_cache
= FALSE
;
7716 fault_info
.stealth
= FALSE
;
7717 fault_info
.io_sync
= FALSE
;
7718 fault_info
.cs_bypass
= FALSE
;
7719 fault_info
.mark_zf_absent
= TRUE
;
7720 fault_info
.interruptible
= interruptible
;
7721 fault_info
.batch_pmap_op
= TRUE
;
7724 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
7727 vm_fault_return_t result
;
7728 unsigned int pg_num
;
7732 dst_page
= vm_page_lookup(object
, dst_offset
);
7736 * If the page is encrypted, we need to decrypt it,
7737 * so force a soft page fault.
7739 if (dst_page
== VM_PAGE_NULL
||
7741 dst_page
->encrypted
||
7743 dst_page
->restart
||
7745 dst_page
->fictitious
) {
7747 if (object
== kernel_object
)
7748 panic("vm_object_iopl_request: missing/bad page in kernel object\n");
7749 if (object
== compressor_object
)
7750 panic("vm_object_iopl_request: missing/bad page in compressor object\n");
7752 if (cntrl_flags
& UPL_REQUEST_NO_FAULT
) {
7753 ret
= KERN_MEMORY_ERROR
;
7756 set_cache_attr_needed
= TRUE
;
7759 * We just looked up the page and the result remains valid
7760 * until the object lock is release, so send it to
7761 * vm_fault_page() (as "dst_page"), to avoid having to
7762 * look it up again there.
7764 caller_lookup
= TRUE
;
7768 kern_return_t error_code
;
7770 fault_info
.cluster_size
= xfer_size
;
7772 vm_object_paging_begin(object
);
7774 result
= vm_fault_page(object
, dst_offset
,
7775 prot
| VM_PROT_WRITE
, FALSE
,
7777 &prot
, &dst_page
, &top_page
,
7779 &error_code
, no_zero_fill
,
7780 FALSE
, &fault_info
);
7782 /* our lookup is no longer valid at this point */
7783 caller_lookup
= FALSE
;
7787 case VM_FAULT_SUCCESS
:
7789 if ( !dst_page
->absent
) {
7790 PAGE_WAKEUP_DONE(dst_page
);
7793 * we only get back an absent page if we
7794 * requested that it not be zero-filled
7795 * because we are about to fill it via I/O
7797 * absent pages should be left BUSY
7798 * to prevent them from being faulted
7799 * into an address space before we've
7800 * had a chance to complete the I/O on
7801 * them since they may contain info that
7802 * shouldn't be seen by the faulting task
7806 * Release paging references and
7807 * top-level placeholder page, if any.
7809 if (top_page
!= VM_PAGE_NULL
) {
7810 vm_object_t local_object
;
7812 local_object
= top_page
->object
;
7814 if (top_page
->object
!= dst_page
->object
) {
7815 vm_object_lock(local_object
);
7816 VM_PAGE_FREE(top_page
);
7817 vm_object_paging_end(local_object
);
7818 vm_object_unlock(local_object
);
7820 VM_PAGE_FREE(top_page
);
7821 vm_object_paging_end(local_object
);
7824 vm_object_paging_end(object
);
7827 case VM_FAULT_RETRY
:
7828 vm_object_lock(object
);
7831 case VM_FAULT_MEMORY_SHORTAGE
:
7832 OSAddAtomic(size_in_pages
, &vm_upl_wait_for_pages
);
7834 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_START
, vm_upl_wait_for_pages
, 0, 0, 0);
7836 if (vm_page_wait(interruptible
)) {
7837 OSAddAtomic(-size_in_pages
, &vm_upl_wait_for_pages
);
7839 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_END
, vm_upl_wait_for_pages
, 0, 0, 0);
7840 vm_object_lock(object
);
7844 OSAddAtomic(-size_in_pages
, &vm_upl_wait_for_pages
);
7846 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_END
, vm_upl_wait_for_pages
, 0, 0, -1);
7850 case VM_FAULT_INTERRUPTED
:
7851 error_code
= MACH_SEND_INTERRUPTED
;
7852 case VM_FAULT_MEMORY_ERROR
:
7854 ret
= (error_code
? error_code
: KERN_MEMORY_ERROR
);
7856 vm_object_lock(object
);
7859 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
7860 /* success but no page: fail */
7861 vm_object_paging_end(object
);
7862 vm_object_unlock(object
);
7866 panic("vm_object_iopl_request: unexpected error"
7867 " 0x%x from vm_fault_page()\n", result
);
7869 } while (result
!= VM_FAULT_SUCCESS
);
7872 if (upl
->flags
& UPL_KERNEL_OBJECT
)
7873 goto record_phys_addr
;
7875 if (dst_page
->compressor
) {
7876 dst_page
->busy
= TRUE
;
7877 goto record_phys_addr
;
7880 if (dst_page
->cleaning
) {
7882 * Someone else is cleaning this page in place.
7883 * In theory, we should be able to proceed and use this
7884 * page but they'll probably end up clearing the "busy"
7885 * bit on it in upl_commit_range() but they didn't set
7886 * it, so they would clear our "busy" bit and open
7887 * us to race conditions.
7888 * We'd better wait for the cleaning to complete and
7891 vm_object_iopl_request_sleep_for_cleaning
++;
7892 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
7895 if (dst_page
->laundry
) {
7896 dst_page
->pageout
= FALSE
;
7898 vm_pageout_steal_laundry(dst_page
, FALSE
);
7900 if ( (cntrl_flags
& UPL_NEED_32BIT_ADDR
) &&
7901 dst_page
->phys_page
>= (max_valid_dma_address
>> PAGE_SHIFT
) ) {
7906 * support devices that can't DMA above 32 bits
7907 * by substituting pages from a pool of low address
7908 * memory for any pages we find above the 4G mark
7909 * can't substitute if the page is already wired because
7910 * we don't know whether that physical address has been
7911 * handed out to some other 64 bit capable DMA device to use
7913 if (VM_PAGE_WIRED(dst_page
)) {
7914 ret
= KERN_PROTECTION_FAILURE
;
7917 low_page
= vm_page_grablo();
7919 if (low_page
== VM_PAGE_NULL
) {
7920 ret
= KERN_RESOURCE_SHORTAGE
;
7924 * from here until the vm_page_replace completes
7925 * we musn't drop the object lock... we don't
7926 * want anyone refaulting this page in and using
7927 * it after we disconnect it... we want the fault
7928 * to find the new page being substituted.
7930 if (dst_page
->pmapped
)
7931 refmod
= pmap_disconnect(dst_page
->phys_page
);
7935 if (!dst_page
->absent
)
7936 vm_page_copy(dst_page
, low_page
);
7938 low_page
->reference
= dst_page
->reference
;
7939 low_page
->dirty
= dst_page
->dirty
;
7940 low_page
->absent
= dst_page
->absent
;
7942 if (refmod
& VM_MEM_REFERENCED
)
7943 low_page
->reference
= TRUE
;
7944 if (refmod
& VM_MEM_MODIFIED
) {
7945 SET_PAGE_DIRTY(low_page
, FALSE
);
7948 vm_page_replace(low_page
, object
, dst_offset
);
7950 dst_page
= low_page
;
7952 * vm_page_grablo returned the page marked
7953 * BUSY... we don't need a PAGE_WAKEUP_DONE
7954 * here, because we've never dropped the object lock
7956 if ( !dst_page
->absent
)
7957 dst_page
->busy
= FALSE
;
7959 if ( !dst_page
->busy
)
7960 dwp
->dw_mask
|= DW_vm_page_wire
;
7962 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
7964 * Mark the page "busy" to block any future page fault
7965 * on this page in addition to wiring it.
7966 * We'll also remove the mapping
7967 * of all these pages before leaving this routine.
7969 assert(!dst_page
->fictitious
);
7970 dst_page
->busy
= TRUE
;
7973 * expect the page to be used
7974 * page queues lock must be held to set 'reference'
7976 dwp
->dw_mask
|= DW_set_reference
;
7978 if (!(cntrl_flags
& UPL_COPYOUT_FROM
)) {
7979 SET_PAGE_DIRTY(dst_page
, TRUE
);
7981 if ((cntrl_flags
& UPL_REQUEST_FORCE_COHERENCY
) && dst_page
->written_by_kernel
== TRUE
) {
7982 pmap_sync_page_attributes_phys(dst_page
->phys_page
);
7983 dst_page
->written_by_kernel
= FALSE
;
7988 upl
->flags
|= UPL_HAS_BUSY
;
7990 pg_num
= (unsigned int) ((dst_offset
-offset
)/PAGE_SIZE
);
7991 assert(pg_num
== (dst_offset
-offset
)/PAGE_SIZE
);
7992 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
7994 if (dst_page
->phys_page
> upl
->highest_page
)
7995 upl
->highest_page
= dst_page
->phys_page
;
7997 if (user_page_list
) {
7998 user_page_list
[entry
].phys_addr
= dst_page
->phys_page
;
7999 user_page_list
[entry
].pageout
= dst_page
->pageout
;
8000 user_page_list
[entry
].absent
= dst_page
->absent
;
8001 user_page_list
[entry
].dirty
= dst_page
->dirty
;
8002 user_page_list
[entry
].precious
= dst_page
->precious
;
8003 user_page_list
[entry
].device
= FALSE
;
8004 user_page_list
[entry
].needed
= FALSE
;
8005 if (dst_page
->clustered
== TRUE
)
8006 user_page_list
[entry
].speculative
= dst_page
->speculative
;
8008 user_page_list
[entry
].speculative
= FALSE
;
8009 user_page_list
[entry
].cs_validated
= dst_page
->cs_validated
;
8010 user_page_list
[entry
].cs_tainted
= dst_page
->cs_tainted
;
8012 if (object
!= kernel_object
&& object
!= compressor_object
) {
8014 * someone is explicitly grabbing this page...
8015 * update clustered and speculative state
8018 if (dst_page
->clustered
)
8019 VM_PAGE_CONSUME_CLUSTERED(dst_page
);
8022 dst_offset
+= PAGE_SIZE_64
;
8023 xfer_size
-= PAGE_SIZE
;
8027 VM_PAGE_ADD_DELAYED_WORK(dwp
, dst_page
, dw_count
);
8029 if (dw_count
>= dw_limit
) {
8030 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
8038 vm_page_do_delayed_work(object
, &dw_array
[0], dw_count
);
8041 if (user_page_list
&& set_cache_attr_needed
== TRUE
)
8042 vm_object_set_pmap_cache_attr(object
, user_page_list
, entry
, TRUE
);
8044 if (page_list_count
!= NULL
) {
8045 if (upl
->flags
& UPL_INTERNAL
)
8046 *page_list_count
= 0;
8047 else if (*page_list_count
> entry
)
8048 *page_list_count
= entry
;
8050 vm_object_unlock(object
);
8052 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
8054 * We've marked all the pages "busy" so that future
8055 * page faults will block.
8056 * Now remove the mapping for these pages, so that they
8057 * can't be accessed without causing a page fault.
8059 vm_object_pmap_protect(object
, offset
, (vm_object_size_t
)size
,
8060 PMAP_NULL
, 0, VM_PROT_NONE
);
8061 assert(!object
->blocked_access
);
8062 object
->blocked_access
= TRUE
;
8064 return KERN_SUCCESS
;
8069 for (; offset
< dst_offset
; offset
+= PAGE_SIZE
) {
8070 boolean_t need_unwire
;
8072 dst_page
= vm_page_lookup(object
, offset
);
8074 if (dst_page
== VM_PAGE_NULL
)
8075 panic("vm_object_iopl_request: Wired page missing. \n");
8078 * if we've already processed this page in an earlier
8079 * dw_do_work, we need to undo the wiring... we will
8080 * leave the dirty and reference bits on if they
8081 * were set, since we don't have a good way of knowing
8082 * what the previous state was and we won't get here
8083 * under any normal circumstances... we will always
8084 * clear BUSY and wakeup any waiters via vm_page_free
8085 * or PAGE_WAKEUP_DONE
8090 if (dw_array
[dw_index
].dw_m
== dst_page
) {
8092 * still in the deferred work list
8093 * which means we haven't yet called
8094 * vm_page_wire on this page
8096 need_unwire
= FALSE
;
8102 vm_page_lock_queues();
8104 if (dst_page
->absent
|| free_wired_pages
== TRUE
) {
8105 vm_page_free(dst_page
);
8107 need_unwire
= FALSE
;
8109 if (need_unwire
== TRUE
)
8110 vm_page_unwire(dst_page
, TRUE
);
8112 PAGE_WAKEUP_DONE(dst_page
);
8114 vm_page_unlock_queues();
8116 if (need_unwire
== TRUE
)
8117 VM_STAT_INCR(reactivations
);
8122 if (! (upl
->flags
& UPL_KERNEL_OBJECT
)) {
8123 vm_object_activity_end(object
);
8124 vm_object_collapse(object
, 0, TRUE
);
8126 vm_object_unlock(object
);
8137 kern_return_t retval
;
8138 boolean_t upls_locked
;
8139 vm_object_t object1
, object2
;
8141 if (upl1
== UPL_NULL
|| upl2
== UPL_NULL
|| upl1
== upl2
|| ((upl1
->flags
& UPL_VECTOR
)==UPL_VECTOR
) || ((upl2
->flags
& UPL_VECTOR
)==UPL_VECTOR
)) {
8142 return KERN_INVALID_ARGUMENT
;
8145 upls_locked
= FALSE
;
8148 * Since we need to lock both UPLs at the same time,
8149 * avoid deadlocks by always taking locks in the same order.
8158 upls_locked
= TRUE
; /* the UPLs will need to be unlocked */
8160 object1
= upl1
->map_object
;
8161 object2
= upl2
->map_object
;
8163 if (upl1
->offset
!= 0 || upl2
->offset
!= 0 ||
8164 upl1
->size
!= upl2
->size
) {
8166 * We deal only with full objects, not subsets.
8167 * That's because we exchange the entire backing store info
8168 * for the objects: pager, resident pages, etc... We can't do
8171 retval
= KERN_INVALID_VALUE
;
8176 * Tranpose the VM objects' backing store.
8178 retval
= vm_object_transpose(object1
, object2
,
8179 (vm_object_size_t
) upl1
->size
);
8181 if (retval
== KERN_SUCCESS
) {
8183 * Make each UPL point to the correct VM object, i.e. the
8184 * object holding the pages that the UPL refers to...
8186 #if CONFIG_IOSCHED || UPL_DEBUG
8187 if ((upl1
->flags
& UPL_TRACKED_BY_OBJECT
) || (upl2
->flags
& UPL_TRACKED_BY_OBJECT
)) {
8188 vm_object_lock(object1
);
8189 vm_object_lock(object2
);
8191 if (upl1
->flags
& UPL_TRACKED_BY_OBJECT
)
8192 queue_remove(&object1
->uplq
, upl1
, upl_t
, uplq
);
8193 if (upl2
->flags
& UPL_TRACKED_BY_OBJECT
)
8194 queue_remove(&object2
->uplq
, upl2
, upl_t
, uplq
);
8196 upl1
->map_object
= object2
;
8197 upl2
->map_object
= object1
;
8199 #if CONFIG_IOSCHED || UPL_DEBUG
8200 if (upl1
->flags
& UPL_TRACKED_BY_OBJECT
)
8201 queue_enter(&object2
->uplq
, upl1
, upl_t
, uplq
);
8202 if (upl2
->flags
& UPL_TRACKED_BY_OBJECT
)
8203 queue_enter(&object1
->uplq
, upl2
, upl_t
, uplq
);
8204 if ((upl1
->flags
& UPL_TRACKED_BY_OBJECT
) || (upl2
->flags
& UPL_TRACKED_BY_OBJECT
)) {
8205 vm_object_unlock(object2
);
8206 vm_object_unlock(object1
);
8218 upls_locked
= FALSE
;
8230 upl_page_info_t
*user_page_list
;
8233 if ( !(upl
->flags
& UPL_INTERNAL
) || count
<= 0)
8236 size_in_pages
= upl
->size
/ PAGE_SIZE
;
8238 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
8240 while (count
-- && index
< size_in_pages
)
8241 user_page_list
[index
++].needed
= TRUE
;
8248 * Rationale: the user might have some encrypted data on disk (via
8249 * FileVault or any other mechanism). That data is then decrypted in
8250 * memory, which is safe as long as the machine is secure. But that
8251 * decrypted data in memory could be paged out to disk by the default
8252 * pager. The data would then be stored on disk in clear (not encrypted)
8253 * and it could be accessed by anyone who gets physical access to the
8254 * disk (if the laptop or the disk gets stolen for example). This weakens
8255 * the security offered by FileVault.
8257 * Solution: the default pager will optionally request that all the
8258 * pages it gathers for pageout be encrypted, via the UPL interfaces,
8259 * before it sends this UPL to disk via the vnode_pageout() path.
8263 * To avoid disrupting the VM LRU algorithms, we want to keep the
8264 * clean-in-place mechanisms, which allow us to send some extra pages to
8265 * swap (clustering) without actually removing them from the user's
8266 * address space. We don't want the user to unknowingly access encrypted
8267 * data, so we have to actually remove the encrypted pages from the page
8268 * table. When the user accesses the data, the hardware will fail to
8269 * locate the virtual page in its page table and will trigger a page
8270 * fault. We can then decrypt the page and enter it in the page table
8271 * again. Whenever we allow the user to access the contents of a page,
8272 * we have to make sure it's not encrypted.
8278 * Reserve of virtual addresses in the kernel address space.
8279 * We need to map the physical pages in the kernel, so that we
8280 * can call the encryption/decryption routines with a kernel
8281 * virtual address. We keep this pool of pre-allocated kernel
8282 * virtual addresses so that we don't have to scan the kernel's
8283 * virtaul address space each time we need to encrypt or decrypt
8285 * It would be nice to be able to encrypt and decrypt in physical
8286 * mode but that might not always be more efficient...
8288 decl_simple_lock_data(,vm_paging_lock
)
8289 #define VM_PAGING_NUM_PAGES 64
8290 vm_map_offset_t vm_paging_base_address
= 0;
8291 boolean_t vm_paging_page_inuse
[VM_PAGING_NUM_PAGES
] = { FALSE
, };
8292 int vm_paging_max_index
= 0;
8293 int vm_paging_page_waiter
= 0;
8294 int vm_paging_page_waiter_total
= 0;
8295 unsigned long vm_paging_no_kernel_page
= 0;
8296 unsigned long vm_paging_objects_mapped
= 0;
8297 unsigned long vm_paging_pages_mapped
= 0;
8298 unsigned long vm_paging_objects_mapped_slow
= 0;
8299 unsigned long vm_paging_pages_mapped_slow
= 0;
8302 vm_paging_map_init(void)
8305 vm_map_offset_t page_map_offset
;
8306 vm_map_entry_t map_entry
;
8308 assert(vm_paging_base_address
== 0);
8311 * Initialize our pool of pre-allocated kernel
8312 * virtual addresses.
8314 page_map_offset
= 0;
8315 kr
= vm_map_find_space(kernel_map
,
8317 VM_PAGING_NUM_PAGES
* PAGE_SIZE
,
8321 if (kr
!= KERN_SUCCESS
) {
8322 panic("vm_paging_map_init: kernel_map full\n");
8324 map_entry
->object
.vm_object
= kernel_object
;
8325 map_entry
->offset
= page_map_offset
;
8326 map_entry
->protection
= VM_PROT_NONE
;
8327 map_entry
->max_protection
= VM_PROT_NONE
;
8328 map_entry
->permanent
= TRUE
;
8329 vm_object_reference(kernel_object
);
8330 vm_map_unlock(kernel_map
);
8332 assert(vm_paging_base_address
== 0);
8333 vm_paging_base_address
= page_map_offset
;
8338 * vm_paging_map_object:
8339 * Maps part of a VM object's pages in the kernel
8340 * virtual address space, using the pre-allocated
8341 * kernel virtual addresses, if possible.
8343 * The VM object is locked. This lock will get
8344 * dropped and re-acquired though, so the caller
8345 * must make sure the VM object is kept alive
8346 * (by holding a VM map that has a reference
8347 * on it, for example, or taking an extra reference).
8348 * The page should also be kept busy to prevent
8349 * it from being reclaimed.
8352 vm_paging_map_object(
8355 vm_object_offset_t offset
,
8356 vm_prot_t protection
,
8357 boolean_t can_unlock_object
,
8358 vm_map_size_t
*size
, /* IN/OUT */
8359 vm_map_offset_t
*address
, /* OUT */
8360 boolean_t
*need_unmap
) /* OUT */
8363 vm_map_offset_t page_map_offset
;
8364 vm_map_size_t map_size
;
8365 vm_object_offset_t object_offset
;
8368 if (page
!= VM_PAGE_NULL
&& *size
== PAGE_SIZE
) {
8369 /* use permanent 1-to-1 kernel mapping of physical memory ? */
8371 *address
= (vm_map_offset_t
)
8372 PHYSMAP_PTOV((pmap_paddr_t
)page
->phys_page
<<
8374 *need_unmap
= FALSE
;
8375 return KERN_SUCCESS
;
8377 #warn "vm_paging_map_object: no 1-to-1 kernel mapping of physical memory..."
8382 * Use one of the pre-allocated kernel virtual addresses
8383 * and just enter the VM page in the kernel address space
8384 * at that virtual address.
8386 simple_lock(&vm_paging_lock
);
8389 * Try and find an available kernel virtual address
8390 * from our pre-allocated pool.
8392 page_map_offset
= 0;
8394 for (i
= 0; i
< VM_PAGING_NUM_PAGES
; i
++) {
8395 if (vm_paging_page_inuse
[i
] == FALSE
) {
8397 vm_paging_base_address
+
8402 if (page_map_offset
!= 0) {
8403 /* found a space to map our page ! */
8407 if (can_unlock_object
) {
8409 * If we can afford to unlock the VM object,
8410 * let's take the slow path now...
8415 * We can't afford to unlock the VM object, so
8416 * let's wait for a space to become available...
8418 vm_paging_page_waiter_total
++;
8419 vm_paging_page_waiter
++;
8420 kr
= assert_wait((event_t
)&vm_paging_page_waiter
, THREAD_UNINT
);
8421 if (kr
== THREAD_WAITING
) {
8422 simple_unlock(&vm_paging_lock
);
8423 kr
= thread_block(THREAD_CONTINUE_NULL
);
8424 simple_lock(&vm_paging_lock
);
8426 vm_paging_page_waiter
--;
8427 /* ... and try again */
8430 if (page_map_offset
!= 0) {
8432 * We found a kernel virtual address;
8433 * map the physical page to that virtual address.
8435 if (i
> vm_paging_max_index
) {
8436 vm_paging_max_index
= i
;
8438 vm_paging_page_inuse
[i
] = TRUE
;
8439 simple_unlock(&vm_paging_lock
);
8441 page
->pmapped
= TRUE
;
8444 * Keep the VM object locked over the PMAP_ENTER
8445 * and the actual use of the page by the kernel,
8446 * or this pmap mapping might get undone by a
8447 * vm_object_pmap_protect() call...
8449 PMAP_ENTER(kernel_pmap
,
8456 vm_paging_objects_mapped
++;
8457 vm_paging_pages_mapped
++;
8458 *address
= page_map_offset
;
8461 /* all done and mapped, ready to use ! */
8462 return KERN_SUCCESS
;
8466 * We ran out of pre-allocated kernel virtual
8467 * addresses. Just map the page in the kernel
8468 * the slow and regular way.
8470 vm_paging_no_kernel_page
++;
8471 simple_unlock(&vm_paging_lock
);
8474 if (! can_unlock_object
) {
8477 *need_unmap
= FALSE
;
8478 return KERN_NOT_SUPPORTED
;
8481 object_offset
= vm_object_trunc_page(offset
);
8482 map_size
= vm_map_round_page(*size
,
8483 VM_MAP_PAGE_MASK(kernel_map
));
8486 * Try and map the required range of the object
8490 vm_object_reference_locked(object
); /* for the map entry */
8491 vm_object_unlock(object
);
8493 kr
= vm_map_enter(kernel_map
,
8504 if (kr
!= KERN_SUCCESS
) {
8507 *need_unmap
= FALSE
;
8508 vm_object_deallocate(object
); /* for the map entry */
8509 vm_object_lock(object
);
8516 * Enter the mapped pages in the page table now.
8518 vm_object_lock(object
);
8520 * VM object must be kept locked from before PMAP_ENTER()
8521 * until after the kernel is done accessing the page(s).
8522 * Otherwise, the pmap mappings in the kernel could be
8523 * undone by a call to vm_object_pmap_protect().
8526 for (page_map_offset
= 0;
8528 map_size
-= PAGE_SIZE_64
, page_map_offset
+= PAGE_SIZE_64
) {
8530 page
= vm_page_lookup(object
, offset
+ page_map_offset
);
8531 if (page
== VM_PAGE_NULL
) {
8532 printf("vm_paging_map_object: no page !?");
8533 vm_object_unlock(object
);
8534 kr
= vm_map_remove(kernel_map
, *address
, *size
,
8536 assert(kr
== KERN_SUCCESS
);
8539 *need_unmap
= FALSE
;
8540 vm_object_lock(object
);
8541 return KERN_MEMORY_ERROR
;
8543 page
->pmapped
= TRUE
;
8545 //assert(pmap_verify_free(page->phys_page));
8546 PMAP_ENTER(kernel_pmap
,
8547 *address
+ page_map_offset
,
8555 vm_paging_objects_mapped_slow
++;
8556 vm_paging_pages_mapped_slow
+= (unsigned long) (map_size
/ PAGE_SIZE_64
);
8560 return KERN_SUCCESS
;
8565 * vm_paging_unmap_object:
8566 * Unmaps part of a VM object's pages from the kernel
8567 * virtual address space.
8569 * The VM object is locked. This lock will get
8570 * dropped and re-acquired though.
8573 vm_paging_unmap_object(
8575 vm_map_offset_t start
,
8576 vm_map_offset_t end
)
8581 if ((vm_paging_base_address
== 0) ||
8582 (start
< vm_paging_base_address
) ||
8583 (end
> (vm_paging_base_address
8584 + (VM_PAGING_NUM_PAGES
* PAGE_SIZE
)))) {
8586 * We didn't use our pre-allocated pool of
8587 * kernel virtual address. Deallocate the
8590 if (object
!= VM_OBJECT_NULL
) {
8591 vm_object_unlock(object
);
8593 kr
= vm_map_remove(kernel_map
, start
, end
, VM_MAP_NO_FLAGS
);
8594 if (object
!= VM_OBJECT_NULL
) {
8595 vm_object_lock(object
);
8597 assert(kr
== KERN_SUCCESS
);
8600 * We used a kernel virtual address from our
8601 * pre-allocated pool. Put it back in the pool
8604 assert(end
- start
== PAGE_SIZE
);
8605 i
= (int) ((start
- vm_paging_base_address
) >> PAGE_SHIFT
);
8606 assert(i
>= 0 && i
< VM_PAGING_NUM_PAGES
);
8608 /* undo the pmap mapping */
8609 pmap_remove(kernel_pmap
, start
, end
);
8611 simple_lock(&vm_paging_lock
);
8612 vm_paging_page_inuse
[i
] = FALSE
;
8613 if (vm_paging_page_waiter
) {
8614 thread_wakeup(&vm_paging_page_waiter
);
8616 simple_unlock(&vm_paging_lock
);
8623 * "iv" is the "initial vector". Ideally, we want to
8624 * have a different one for each page we encrypt, so that
8625 * crackers can't find encryption patterns too easily.
8627 #define SWAP_CRYPT_AES_KEY_SIZE 128 /* XXX 192 and 256 don't work ! */
8628 boolean_t swap_crypt_ctx_initialized
= FALSE
;
8629 uint32_t swap_crypt_key
[8]; /* big enough for a 256 key */
8630 aes_ctx swap_crypt_ctx
;
8631 const unsigned char swap_crypt_null_iv
[AES_BLOCK_SIZE
] = {0xa, };
8634 boolean_t swap_crypt_ctx_tested
= FALSE
;
8635 unsigned char swap_crypt_test_page_ref
[4096] __attribute__((aligned(4096)));
8636 unsigned char swap_crypt_test_page_encrypt
[4096] __attribute__((aligned(4096)));
8637 unsigned char swap_crypt_test_page_decrypt
[4096] __attribute__((aligned(4096)));
8641 * Initialize the encryption context: key and key size.
8643 void swap_crypt_ctx_initialize(void); /* forward */
8645 swap_crypt_ctx_initialize(void)
8650 * No need for locking to protect swap_crypt_ctx_initialized
8651 * because the first use of encryption will come from the
8652 * pageout thread (we won't pagein before there's been a pageout)
8653 * and there's only one pageout thread.
8655 if (swap_crypt_ctx_initialized
== FALSE
) {
8657 i
< (sizeof (swap_crypt_key
) /
8658 sizeof (swap_crypt_key
[0]));
8660 swap_crypt_key
[i
] = random();
8662 aes_encrypt_key((const unsigned char *) swap_crypt_key
,
8663 SWAP_CRYPT_AES_KEY_SIZE
,
8664 &swap_crypt_ctx
.encrypt
);
8665 aes_decrypt_key((const unsigned char *) swap_crypt_key
,
8666 SWAP_CRYPT_AES_KEY_SIZE
,
8667 &swap_crypt_ctx
.decrypt
);
8668 swap_crypt_ctx_initialized
= TRUE
;
8673 * Validate the encryption algorithms.
8675 if (swap_crypt_ctx_tested
== FALSE
) {
8677 for (i
= 0; i
< 4096; i
++) {
8678 swap_crypt_test_page_ref
[i
] = (char) i
;
8681 aes_encrypt_cbc(swap_crypt_test_page_ref
,
8683 PAGE_SIZE
/ AES_BLOCK_SIZE
,
8684 swap_crypt_test_page_encrypt
,
8685 &swap_crypt_ctx
.encrypt
);
8687 aes_decrypt_cbc(swap_crypt_test_page_encrypt
,
8689 PAGE_SIZE
/ AES_BLOCK_SIZE
,
8690 swap_crypt_test_page_decrypt
,
8691 &swap_crypt_ctx
.decrypt
);
8692 /* compare result with original */
8693 for (i
= 0; i
< 4096; i
++) {
8694 if (swap_crypt_test_page_decrypt
[i
] !=
8695 swap_crypt_test_page_ref
[i
]) {
8696 panic("encryption test failed");
8701 aes_encrypt_cbc(swap_crypt_test_page_decrypt
,
8703 PAGE_SIZE
/ AES_BLOCK_SIZE
,
8704 swap_crypt_test_page_decrypt
,
8705 &swap_crypt_ctx
.encrypt
);
8706 /* decrypt in place */
8707 aes_decrypt_cbc(swap_crypt_test_page_decrypt
,
8709 PAGE_SIZE
/ AES_BLOCK_SIZE
,
8710 swap_crypt_test_page_decrypt
,
8711 &swap_crypt_ctx
.decrypt
);
8712 for (i
= 0; i
< 4096; i
++) {
8713 if (swap_crypt_test_page_decrypt
[i
] !=
8714 swap_crypt_test_page_ref
[i
]) {
8715 panic("in place encryption test failed");
8719 swap_crypt_ctx_tested
= TRUE
;
8727 * Encrypt the given page, for secure paging.
8728 * The page might already be mapped at kernel virtual
8729 * address "kernel_mapping_offset". Otherwise, we need
8733 * The page's object is locked, but this lock will be released
8735 * The page is busy and not accessible by users (not entered in any pmap).
8740 vm_map_offset_t kernel_mapping_offset
)
8743 vm_map_size_t kernel_mapping_size
;
8744 boolean_t kernel_mapping_needs_unmap
;
8745 vm_offset_t kernel_vaddr
;
8747 unsigned char aes_iv
[AES_BLOCK_SIZE
];
8749 memory_object_t pager_object
;
8750 vm_object_offset_t paging_offset
;
8754 if (! vm_pages_encrypted
) {
8755 vm_pages_encrypted
= TRUE
;
8760 if (page
->encrypted
) {
8762 * Already encrypted: no need to do it again.
8764 vm_page_encrypt_already_encrypted_counter
++;
8767 assert(page
->dirty
|| page
->precious
);
8769 ASSERT_PAGE_DECRYPTED(page
);
8772 * Take a paging-in-progress reference to keep the object
8773 * alive even if we have to unlock it (in vm_paging_map_object()
8776 vm_object_paging_begin(page
->object
);
8778 if (kernel_mapping_offset
== 0) {
8780 * The page hasn't already been mapped in kernel space
8781 * by the caller. Map it now, so that we can access
8782 * its contents and encrypt them.
8784 kernel_mapping_size
= PAGE_SIZE
;
8785 kernel_mapping_needs_unmap
= FALSE
;
8786 kr
= vm_paging_map_object(page
,
8789 VM_PROT_READ
| VM_PROT_WRITE
,
8791 &kernel_mapping_size
,
8792 &kernel_mapping_offset
,
8793 &kernel_mapping_needs_unmap
);
8794 if (kr
!= KERN_SUCCESS
) {
8795 panic("vm_page_encrypt: "
8796 "could not map page in kernel: 0x%x\n",
8800 kernel_mapping_size
= 0;
8801 kernel_mapping_needs_unmap
= FALSE
;
8803 kernel_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping_offset
);
8805 if (swap_crypt_ctx_initialized
== FALSE
) {
8806 swap_crypt_ctx_initialize();
8808 assert(swap_crypt_ctx_initialized
);
8811 * Prepare an "initial vector" for the encryption.
8812 * We use the "pager" and the "paging_offset" for that
8813 * page to obfuscate the encrypted data a bit more and
8814 * prevent crackers from finding patterns that they could
8815 * use to break the key.
8817 bzero(&encrypt_iv
.aes_iv
[0], sizeof (encrypt_iv
.aes_iv
));
8818 encrypt_iv
.vm
.pager_object
= page
->object
->pager
;
8819 encrypt_iv
.vm
.paging_offset
=
8820 page
->object
->paging_offset
+ page
->offset
;
8822 /* encrypt the "initial vector" */
8823 aes_encrypt_cbc((const unsigned char *) &encrypt_iv
.aes_iv
[0],
8826 &encrypt_iv
.aes_iv
[0],
8827 &swap_crypt_ctx
.encrypt
);
8832 aes_encrypt_cbc((const unsigned char *) kernel_vaddr
,
8833 &encrypt_iv
.aes_iv
[0],
8834 PAGE_SIZE
/ AES_BLOCK_SIZE
,
8835 (unsigned char *) kernel_vaddr
,
8836 &swap_crypt_ctx
.encrypt
);
8838 vm_page_encrypt_counter
++;
8841 * Unmap the page from the kernel's address space,
8842 * if we had to map it ourselves. Otherwise, let
8843 * the caller undo the mapping if needed.
8845 if (kernel_mapping_needs_unmap
) {
8846 vm_paging_unmap_object(page
->object
,
8847 kernel_mapping_offset
,
8848 kernel_mapping_offset
+ kernel_mapping_size
);
8852 * Clear the "reference" and "modified" bits.
8853 * This should clean up any impact the encryption had
8855 * The page was kept busy and disconnected from all pmaps,
8856 * so it can't have been referenced or modified from user
8858 * The software bits will be reset later after the I/O
8859 * has completed (in upl_commit_range()).
8861 pmap_clear_refmod(page
->phys_page
, VM_MEM_REFERENCED
| VM_MEM_MODIFIED
);
8863 page
->encrypted
= TRUE
;
8865 vm_object_paging_end(page
->object
);
8871 * Decrypt the given page.
8872 * The page might already be mapped at kernel virtual
8873 * address "kernel_mapping_offset". Otherwise, we need
8877 * The page's VM object is locked but will be unlocked and relocked.
8878 * The page is busy and not accessible by users (not entered in any pmap).
8883 vm_map_offset_t kernel_mapping_offset
)
8886 vm_map_size_t kernel_mapping_size
;
8887 vm_offset_t kernel_vaddr
;
8888 boolean_t kernel_mapping_needs_unmap
;
8890 unsigned char aes_iv
[AES_BLOCK_SIZE
];
8892 memory_object_t pager_object
;
8893 vm_object_offset_t paging_offset
;
8896 boolean_t was_dirty
;
8899 assert(page
->encrypted
);
8901 was_dirty
= page
->dirty
;
8904 * Take a paging-in-progress reference to keep the object
8905 * alive even if we have to unlock it (in vm_paging_map_object()
8908 vm_object_paging_begin(page
->object
);
8910 if (kernel_mapping_offset
== 0) {
8912 * The page hasn't already been mapped in kernel space
8913 * by the caller. Map it now, so that we can access
8914 * its contents and decrypt them.
8916 kernel_mapping_size
= PAGE_SIZE
;
8917 kernel_mapping_needs_unmap
= FALSE
;
8918 kr
= vm_paging_map_object(page
,
8921 VM_PROT_READ
| VM_PROT_WRITE
,
8923 &kernel_mapping_size
,
8924 &kernel_mapping_offset
,
8925 &kernel_mapping_needs_unmap
);
8926 if (kr
!= KERN_SUCCESS
) {
8927 panic("vm_page_decrypt: "
8928 "could not map page in kernel: 0x%x\n",
8932 kernel_mapping_size
= 0;
8933 kernel_mapping_needs_unmap
= FALSE
;
8935 kernel_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping_offset
);
8937 assert(swap_crypt_ctx_initialized
);
8940 * Prepare an "initial vector" for the decryption.
8941 * It has to be the same as the "initial vector" we
8942 * used to encrypt that page.
8944 bzero(&decrypt_iv
.aes_iv
[0], sizeof (decrypt_iv
.aes_iv
));
8945 decrypt_iv
.vm
.pager_object
= page
->object
->pager
;
8946 decrypt_iv
.vm
.paging_offset
=
8947 page
->object
->paging_offset
+ page
->offset
;
8949 /* encrypt the "initial vector" */
8950 aes_encrypt_cbc((const unsigned char *) &decrypt_iv
.aes_iv
[0],
8953 &decrypt_iv
.aes_iv
[0],
8954 &swap_crypt_ctx
.encrypt
);
8959 aes_decrypt_cbc((const unsigned char *) kernel_vaddr
,
8960 &decrypt_iv
.aes_iv
[0],
8961 PAGE_SIZE
/ AES_BLOCK_SIZE
,
8962 (unsigned char *) kernel_vaddr
,
8963 &swap_crypt_ctx
.decrypt
);
8964 vm_page_decrypt_counter
++;
8967 * Unmap the page from the kernel's address space,
8968 * if we had to map it ourselves. Otherwise, let
8969 * the caller undo the mapping if needed.
8971 if (kernel_mapping_needs_unmap
) {
8972 vm_paging_unmap_object(page
->object
,
8974 kernel_vaddr
+ PAGE_SIZE
);
8979 * The pager did not specify that the page would be
8980 * clean when it got paged in, so let's not clean it here
8985 * After decryption, the page is actually still clean.
8986 * It was encrypted as part of paging, which "cleans"
8987 * the "dirty" pages.
8988 * Noone could access it after it was encrypted
8989 * and the decryption doesn't count.
8991 page
->dirty
= FALSE
;
8992 assert (page
->cs_validated
== FALSE
);
8993 pmap_clear_refmod(page
->phys_page
, VM_MEM_MODIFIED
| VM_MEM_REFERENCED
);
8995 page
->encrypted
= FALSE
;
8998 * We've just modified the page's contents via the data cache and part
8999 * of the new contents might still be in the cache and not yet in RAM.
9000 * Since the page is now available and might get gathered in a UPL to
9001 * be part of a DMA transfer from a driver that expects the memory to
9002 * be coherent at this point, we have to flush the data cache.
9004 pmap_sync_page_attributes_phys(page
->phys_page
);
9006 * Since the page is not mapped yet, some code might assume that it
9007 * doesn't need to invalidate the instruction cache when writing to
9008 * that page. That code relies on "pmapped" being FALSE, so that the
9009 * caches get synchronized when the page is first mapped.
9011 assert(pmap_verify_free(page
->phys_page
));
9012 page
->pmapped
= FALSE
;
9013 page
->wpmapped
= FALSE
;
9015 vm_object_paging_end(page
->object
);
9018 #if DEVELOPMENT || DEBUG
9019 unsigned long upl_encrypt_upls
= 0;
9020 unsigned long upl_encrypt_pages
= 0;
9027 * Encrypts all the pages in the UPL, within the specified range.
9033 upl_offset_t crypt_offset
,
9034 upl_size_t crypt_size
)
9036 upl_size_t upl_size
, subupl_size
=crypt_size
;
9037 upl_offset_t offset_in_upl
, subupl_offset
=crypt_offset
;
9038 vm_object_t upl_object
;
9039 vm_object_offset_t upl_offset
;
9041 vm_object_t shadow_object
;
9042 vm_object_offset_t shadow_offset
;
9043 vm_object_offset_t paging_offset
;
9044 vm_object_offset_t base_offset
;
9045 int isVectorUPL
= 0;
9046 upl_t vector_upl
= NULL
;
9048 if((isVectorUPL
= vector_upl_is_valid(upl
)))
9051 process_upl_to_encrypt
:
9053 crypt_size
= subupl_size
;
9054 crypt_offset
= subupl_offset
;
9055 upl
= vector_upl_subupl_byoffset(vector_upl
, &crypt_offset
, &crypt_size
);
9057 panic("upl_encrypt: Accessing a sub-upl that doesn't exist\n");
9058 subupl_size
-= crypt_size
;
9059 subupl_offset
+= crypt_size
;
9062 #if DEVELOPMENT || DEBUG
9064 upl_encrypt_pages
+= crypt_size
/ PAGE_SIZE
;
9066 upl_object
= upl
->map_object
;
9067 upl_offset
= upl
->offset
;
9068 upl_size
= upl
->size
;
9070 vm_object_lock(upl_object
);
9073 * Find the VM object that contains the actual pages.
9075 if (upl_object
->pageout
) {
9076 shadow_object
= upl_object
->shadow
;
9078 * The offset in the shadow object is actually also
9079 * accounted for in upl->offset. It possibly shouldn't be
9080 * this way, but for now don't account for it twice.
9083 assert(upl_object
->paging_offset
== 0); /* XXX ? */
9084 vm_object_lock(shadow_object
);
9086 shadow_object
= upl_object
;
9090 paging_offset
= shadow_object
->paging_offset
;
9091 vm_object_paging_begin(shadow_object
);
9093 if (shadow_object
!= upl_object
)
9094 vm_object_unlock(upl_object
);
9097 base_offset
= shadow_offset
;
9098 base_offset
+= upl_offset
;
9099 base_offset
+= crypt_offset
;
9100 base_offset
-= paging_offset
;
9102 assert(crypt_offset
+ crypt_size
<= upl_size
);
9104 for (offset_in_upl
= 0;
9105 offset_in_upl
< crypt_size
;
9106 offset_in_upl
+= PAGE_SIZE
) {
9107 page
= vm_page_lookup(shadow_object
,
9108 base_offset
+ offset_in_upl
);
9109 if (page
== VM_PAGE_NULL
) {
9110 panic("upl_encrypt: "
9111 "no page for (obj=%p,off=0x%llx+0x%x)!\n",
9117 * Disconnect the page from all pmaps, so that nobody can
9118 * access it while it's encrypted. After that point, all
9119 * accesses to this page will cause a page fault and block
9120 * while the page is busy being encrypted. After the
9121 * encryption completes, any access will cause a
9122 * page fault and the page gets decrypted at that time.
9124 pmap_disconnect(page
->phys_page
);
9125 vm_page_encrypt(page
, 0);
9127 if (vm_object_lock_avoid(shadow_object
)) {
9129 * Give vm_pageout_scan() a chance to convert more
9130 * pages from "clean-in-place" to "clean-and-free",
9131 * if it's interested in the same pages we selected
9134 vm_object_unlock(shadow_object
);
9136 vm_object_lock(shadow_object
);
9140 vm_object_paging_end(shadow_object
);
9141 vm_object_unlock(shadow_object
);
9143 if(isVectorUPL
&& subupl_size
)
9144 goto process_upl_to_encrypt
;
9147 #else /* ENCRYPTED_SWAP */
9151 __unused upl_offset_t crypt_offset
,
9152 __unused upl_size_t crypt_size
)
9158 __unused vm_page_t page
,
9159 __unused vm_map_offset_t kernel_mapping_offset
)
9165 __unused vm_page_t page
,
9166 __unused vm_map_offset_t kernel_mapping_offset
)
9170 #endif /* ENCRYPTED_SWAP */
9173 * page->object must be locked
9176 vm_pageout_steal_laundry(vm_page_t page
, boolean_t queues_locked
)
9178 if (!queues_locked
) {
9179 vm_page_lockspin_queues();
9183 * need to drop the laundry count...
9184 * we may also need to remove it
9185 * from the I/O paging queue...
9186 * vm_pageout_throttle_up handles both cases
9188 * the laundry and pageout_queue flags are cleared...
9190 vm_pageout_throttle_up(page
);
9192 vm_page_steal_pageout_page
++;
9194 if (!queues_locked
) {
9195 vm_page_unlock_queues();
9200 vector_upl_create(vm_offset_t upl_offset
)
9202 int vector_upl_size
= sizeof(struct _vector_upl
);
9205 vector_upl_t vector_upl
= (vector_upl_t
)kalloc(vector_upl_size
);
9207 upl
= upl_create(0,UPL_VECTOR
,0);
9208 upl
->vector_upl
= vector_upl
;
9209 upl
->offset
= upl_offset
;
9210 vector_upl
->size
= 0;
9211 vector_upl
->offset
= upl_offset
;
9212 vector_upl
->invalid_upls
=0;
9213 vector_upl
->num_upls
=0;
9214 vector_upl
->pagelist
= NULL
;
9216 for(i
=0; i
< MAX_VECTOR_UPL_ELEMENTS
; i
++) {
9217 vector_upl
->upl_iostates
[i
].size
= 0;
9218 vector_upl
->upl_iostates
[i
].offset
= 0;
9225 vector_upl_deallocate(upl_t upl
)
9228 vector_upl_t vector_upl
= upl
->vector_upl
;
9230 if(vector_upl
->invalid_upls
!= vector_upl
->num_upls
)
9231 panic("Deallocating non-empty Vectored UPL\n");
9232 kfree(vector_upl
->pagelist
,(sizeof(struct upl_page_info
)*(vector_upl
->size
/PAGE_SIZE
)));
9233 vector_upl
->invalid_upls
=0;
9234 vector_upl
->num_upls
= 0;
9235 vector_upl
->pagelist
= NULL
;
9236 vector_upl
->size
= 0;
9237 vector_upl
->offset
= 0;
9238 kfree(vector_upl
, sizeof(struct _vector_upl
));
9239 vector_upl
= (vector_upl_t
)0xfeedfeed;
9242 panic("vector_upl_deallocate was passed a non-vectored upl\n");
9245 panic("vector_upl_deallocate was passed a NULL upl\n");
9249 vector_upl_is_valid(upl_t upl
)
9251 if(upl
&& ((upl
->flags
& UPL_VECTOR
)==UPL_VECTOR
)) {
9252 vector_upl_t vector_upl
= upl
->vector_upl
;
9253 if(vector_upl
== NULL
|| vector_upl
== (vector_upl_t
)0xfeedfeed || vector_upl
== (vector_upl_t
)0xfeedbeef)
9262 vector_upl_set_subupl(upl_t upl
,upl_t subupl
, uint32_t io_size
)
9264 if(vector_upl_is_valid(upl
)) {
9265 vector_upl_t vector_upl
= upl
->vector_upl
;
9270 if(io_size
< PAGE_SIZE
)
9271 io_size
= PAGE_SIZE
;
9272 subupl
->vector_upl
= (void*)vector_upl
;
9273 vector_upl
->upl_elems
[vector_upl
->num_upls
++] = subupl
;
9274 vector_upl
->size
+= io_size
;
9275 upl
->size
+= io_size
;
9278 uint32_t i
=0,invalid_upls
=0;
9279 for(i
= 0; i
< vector_upl
->num_upls
; i
++) {
9280 if(vector_upl
->upl_elems
[i
] == subupl
)
9283 if(i
== vector_upl
->num_upls
)
9284 panic("Trying to remove sub-upl when none exists");
9286 vector_upl
->upl_elems
[i
] = NULL
;
9287 invalid_upls
= hw_atomic_add(&(vector_upl
)->invalid_upls
, 1);
9288 if(invalid_upls
== vector_upl
->num_upls
)
9295 panic("vector_upl_set_subupl was passed a NULL upl element\n");
9298 panic("vector_upl_set_subupl was passed a non-vectored upl\n");
9301 panic("vector_upl_set_subupl was passed a NULL upl\n");
9307 vector_upl_set_pagelist(upl_t upl
)
9309 if(vector_upl_is_valid(upl
)) {
9311 vector_upl_t vector_upl
= upl
->vector_upl
;
9314 vm_offset_t pagelist_size
=0, cur_upl_pagelist_size
=0;
9316 vector_upl
->pagelist
= (upl_page_info_array_t
)kalloc(sizeof(struct upl_page_info
)*(vector_upl
->size
/PAGE_SIZE
));
9318 for(i
=0; i
< vector_upl
->num_upls
; i
++) {
9319 cur_upl_pagelist_size
= sizeof(struct upl_page_info
) * vector_upl
->upl_elems
[i
]->size
/PAGE_SIZE
;
9320 bcopy(UPL_GET_INTERNAL_PAGE_LIST_SIMPLE(vector_upl
->upl_elems
[i
]), (char*)vector_upl
->pagelist
+ pagelist_size
, cur_upl_pagelist_size
);
9321 pagelist_size
+= cur_upl_pagelist_size
;
9322 if(vector_upl
->upl_elems
[i
]->highest_page
> upl
->highest_page
)
9323 upl
->highest_page
= vector_upl
->upl_elems
[i
]->highest_page
;
9325 assert( pagelist_size
== (sizeof(struct upl_page_info
)*(vector_upl
->size
/PAGE_SIZE
)) );
9328 panic("vector_upl_set_pagelist was passed a non-vectored upl\n");
9331 panic("vector_upl_set_pagelist was passed a NULL upl\n");
9336 vector_upl_subupl_byindex(upl_t upl
, uint32_t index
)
9338 if(vector_upl_is_valid(upl
)) {
9339 vector_upl_t vector_upl
= upl
->vector_upl
;
9341 if(index
< vector_upl
->num_upls
)
9342 return vector_upl
->upl_elems
[index
];
9345 panic("vector_upl_subupl_byindex was passed a non-vectored upl\n");
9351 vector_upl_subupl_byoffset(upl_t upl
, upl_offset_t
*upl_offset
, upl_size_t
*upl_size
)
9353 if(vector_upl_is_valid(upl
)) {
9355 vector_upl_t vector_upl
= upl
->vector_upl
;
9358 upl_t subupl
= NULL
;
9359 vector_upl_iostates_t subupl_state
;
9361 for(i
=0; i
< vector_upl
->num_upls
; i
++) {
9362 subupl
= vector_upl
->upl_elems
[i
];
9363 subupl_state
= vector_upl
->upl_iostates
[i
];
9364 if( *upl_offset
<= (subupl_state
.offset
+ subupl_state
.size
- 1)) {
9365 /* We could have been passed an offset/size pair that belongs
9366 * to an UPL element that has already been committed/aborted.
9367 * If so, return NULL.
9371 if((subupl_state
.offset
+ subupl_state
.size
) < (*upl_offset
+ *upl_size
)) {
9372 *upl_size
= (subupl_state
.offset
+ subupl_state
.size
) - *upl_offset
;
9373 if(*upl_size
> subupl_state
.size
)
9374 *upl_size
= subupl_state
.size
;
9376 if(*upl_offset
>= subupl_state
.offset
)
9377 *upl_offset
-= subupl_state
.offset
;
9379 panic("Vector UPL offset miscalculation\n");
9385 panic("vector_upl_subupl_byoffset was passed a non-vectored UPL\n");
9391 vector_upl_get_submap(upl_t upl
, vm_map_t
*v_upl_submap
, vm_offset_t
*submap_dst_addr
)
9393 *v_upl_submap
= NULL
;
9395 if(vector_upl_is_valid(upl
)) {
9396 vector_upl_t vector_upl
= upl
->vector_upl
;
9398 *v_upl_submap
= vector_upl
->submap
;
9399 *submap_dst_addr
= vector_upl
->submap_dst_addr
;
9402 panic("vector_upl_get_submap was passed a non-vectored UPL\n");
9405 panic("vector_upl_get_submap was passed a null UPL\n");
9409 vector_upl_set_submap(upl_t upl
, vm_map_t submap
, vm_offset_t submap_dst_addr
)
9411 if(vector_upl_is_valid(upl
)) {
9412 vector_upl_t vector_upl
= upl
->vector_upl
;
9414 vector_upl
->submap
= submap
;
9415 vector_upl
->submap_dst_addr
= submap_dst_addr
;
9418 panic("vector_upl_get_submap was passed a non-vectored UPL\n");
9421 panic("vector_upl_get_submap was passed a NULL UPL\n");
9425 vector_upl_set_iostate(upl_t upl
, upl_t subupl
, upl_offset_t offset
, upl_size_t size
)
9427 if(vector_upl_is_valid(upl
)) {
9429 vector_upl_t vector_upl
= upl
->vector_upl
;
9432 for(i
= 0; i
< vector_upl
->num_upls
; i
++) {
9433 if(vector_upl
->upl_elems
[i
] == subupl
)
9437 if(i
== vector_upl
->num_upls
)
9438 panic("setting sub-upl iostate when none exists");
9440 vector_upl
->upl_iostates
[i
].offset
= offset
;
9441 if(size
< PAGE_SIZE
)
9443 vector_upl
->upl_iostates
[i
].size
= size
;
9446 panic("vector_upl_set_iostate was passed a non-vectored UPL\n");
9449 panic("vector_upl_set_iostate was passed a NULL UPL\n");
9453 vector_upl_get_iostate(upl_t upl
, upl_t subupl
, upl_offset_t
*offset
, upl_size_t
*size
)
9455 if(vector_upl_is_valid(upl
)) {
9457 vector_upl_t vector_upl
= upl
->vector_upl
;
9460 for(i
= 0; i
< vector_upl
->num_upls
; i
++) {
9461 if(vector_upl
->upl_elems
[i
] == subupl
)
9465 if(i
== vector_upl
->num_upls
)
9466 panic("getting sub-upl iostate when none exists");
9468 *offset
= vector_upl
->upl_iostates
[i
].offset
;
9469 *size
= vector_upl
->upl_iostates
[i
].size
;
9472 panic("vector_upl_get_iostate was passed a non-vectored UPL\n");
9475 panic("vector_upl_get_iostate was passed a NULL UPL\n");
9479 vector_upl_get_iostate_byindex(upl_t upl
, uint32_t index
, upl_offset_t
*offset
, upl_size_t
*size
)
9481 if(vector_upl_is_valid(upl
)) {
9482 vector_upl_t vector_upl
= upl
->vector_upl
;
9484 if(index
< vector_upl
->num_upls
) {
9485 *offset
= vector_upl
->upl_iostates
[index
].offset
;
9486 *size
= vector_upl
->upl_iostates
[index
].size
;
9489 *offset
= *size
= 0;
9492 panic("vector_upl_get_iostate_byindex was passed a non-vectored UPL\n");
9495 panic("vector_upl_get_iostate_byindex was passed a NULL UPL\n");
9499 upl_get_internal_vectorupl_pagelist(upl_t upl
)
9501 return ((vector_upl_t
)(upl
->vector_upl
))->pagelist
;
9505 upl_get_internal_vectorupl(upl_t upl
)
9507 return upl
->vector_upl
;
9511 upl_get_internal_pagelist_offset(void)
9513 return sizeof(struct upl
);
9522 upl
->flags
|= UPL_CLEAR_DIRTY
;
9524 upl
->flags
&= ~UPL_CLEAR_DIRTY
;
9535 upl
->ext_ref_count
++;
9537 if (!upl
->ext_ref_count
) {
9538 panic("upl_set_referenced not %p\n", upl
);
9540 upl
->ext_ref_count
--;
9549 vm_offset_t upl_offset
,
9554 if ((upl
->flags
& UPL_EXPEDITE_SUPPORTED
) == 0)
9557 assert(upl
->upl_reprio_info
!= 0);
9558 for(i
= (int)(upl_offset
/ PAGE_SIZE
), j
= 0; j
< io_size
; i
++, j
+= PAGE_SIZE
) {
9559 UPL_SET_REPRIO_INFO(upl
, i
, blkno
, io_size
);
9565 vm_page_is_slideable(vm_page_t m
)
9567 boolean_t result
= FALSE
;
9568 vm_shared_region_slide_info_t si
;
9570 vm_object_lock_assert_held(m
->object
);
9572 /* make sure our page belongs to the one object allowed to do this */
9573 if (!m
->object
->object_slid
) {
9577 si
= m
->object
->vo_slide_info
;
9582 if(!m
->slid
&& (si
->start
<= m
->offset
&& si
->end
> m
->offset
)) {
9590 int vm_page_slide_counter
= 0;
9591 int vm_page_slide_errors
= 0;
9595 vm_map_offset_t kernel_mapping_offset
)
9598 vm_map_size_t kernel_mapping_size
;
9599 boolean_t kernel_mapping_needs_unmap
;
9600 vm_offset_t kernel_vaddr
;
9601 uint32_t pageIndex
= 0;
9603 assert(!page
->slid
);
9604 assert(page
->object
->object_slid
);
9605 vm_object_lock_assert_exclusive(page
->object
);
9608 return KERN_FAILURE
;
9611 * Take a paging-in-progress reference to keep the object
9612 * alive even if we have to unlock it (in vm_paging_map_object()
9615 vm_object_paging_begin(page
->object
);
9617 if (kernel_mapping_offset
== 0) {
9619 * The page hasn't already been mapped in kernel space
9620 * by the caller. Map it now, so that we can access
9621 * its contents and decrypt them.
9623 kernel_mapping_size
= PAGE_SIZE
;
9624 kernel_mapping_needs_unmap
= FALSE
;
9625 kr
= vm_paging_map_object(page
,
9628 VM_PROT_READ
| VM_PROT_WRITE
,
9630 &kernel_mapping_size
,
9631 &kernel_mapping_offset
,
9632 &kernel_mapping_needs_unmap
);
9633 if (kr
!= KERN_SUCCESS
) {
9634 panic("vm_page_slide: "
9635 "could not map page in kernel: 0x%x\n",
9639 kernel_mapping_size
= 0;
9640 kernel_mapping_needs_unmap
= FALSE
;
9642 kernel_vaddr
= CAST_DOWN(vm_offset_t
, kernel_mapping_offset
);
9645 * Slide the pointers on the page.
9648 /*assert that slide_file_info.start/end are page-aligned?*/
9650 assert(!page
->slid
);
9651 assert(page
->object
->object_slid
);
9653 /* on some platforms this is an extern int, on others it's a cpp macro */
9654 __unreachable_ok_push
9655 /* TODO: Consider this */
9656 if (!TEST_PAGE_SIZE_4K
) {
9657 for (int i
= 0; i
< 4; i
++) {
9658 pageIndex
= (uint32_t)((page
->offset
- page
->object
->vo_slide_info
->start
)/0x1000);
9659 kr
= vm_shared_region_slide_page(page
->object
->vo_slide_info
, kernel_vaddr
+ (0x1000*i
), pageIndex
+ i
);
9662 pageIndex
= (uint32_t)((page
->offset
- page
->object
->vo_slide_info
->start
)/PAGE_SIZE
);
9663 kr
= vm_shared_region_slide_page(page
->object
->vo_slide_info
, kernel_vaddr
, pageIndex
);
9665 __unreachable_ok_pop
9667 vm_page_slide_counter
++;
9670 * Unmap the page from the kernel's address space,
9672 if (kernel_mapping_needs_unmap
) {
9673 vm_paging_unmap_object(page
->object
,
9675 kernel_vaddr
+ PAGE_SIZE
);
9678 page
->dirty
= FALSE
;
9679 pmap_clear_refmod(page
->phys_page
, VM_MEM_MODIFIED
| VM_MEM_REFERENCED
);
9681 if (kr
!= KERN_SUCCESS
|| cs_debug
> 1) {
9682 printf("vm_page_slide(%p): "
9683 "obj %p off 0x%llx mobj %p moff 0x%llx\n",
9685 page
->object
, page
->offset
,
9686 page
->object
->pager
,
9687 page
->offset
+ page
->object
->paging_offset
);
9690 if (kr
== KERN_SUCCESS
) {
9694 vm_page_slide_errors
++;
9697 vm_object_paging_end(page
->object
);
9702 void inline memoryshot(unsigned int event
, unsigned int control
)
9704 if (vm_debug_events
) {
9705 KERNEL_DEBUG_CONSTANT1((MACHDBG_CODE(DBG_MACH_VM_PRESSURE
, event
)) | control
,
9706 vm_page_active_count
, vm_page_inactive_count
,
9707 vm_page_free_count
, vm_page_speculative_count
,
9708 vm_page_throttled_count
);
9718 boolean_t
upl_device_page(upl_page_info_t
*upl
)
9720 return(UPL_DEVICE_PAGE(upl
));
9722 boolean_t
upl_page_present(upl_page_info_t
*upl
, int index
)
9724 return(UPL_PAGE_PRESENT(upl
, index
));
9726 boolean_t
upl_speculative_page(upl_page_info_t
*upl
, int index
)
9728 return(UPL_SPECULATIVE_PAGE(upl
, index
));
9730 boolean_t
upl_dirty_page(upl_page_info_t
*upl
, int index
)
9732 return(UPL_DIRTY_PAGE(upl
, index
));
9734 boolean_t
upl_valid_page(upl_page_info_t
*upl
, int index
)
9736 return(UPL_VALID_PAGE(upl
, index
));
9738 ppnum_t
upl_phys_page(upl_page_info_t
*upl
, int index
)
9740 return(UPL_PHYS_PAGE(upl
, index
));
9744 vm_countdirtypages(void)
9756 vm_page_lock_queues();
9757 m
= (vm_page_t
) queue_first(&vm_page_queue_inactive
);
9759 if (m
==(vm_page_t
)0) break;
9761 if(m
->dirty
) dpages
++;
9762 if(m
->pageout
) pgopages
++;
9763 if(m
->precious
) precpages
++;
9765 assert(m
->object
!= kernel_object
);
9766 m
= (vm_page_t
) queue_next(&m
->pageq
);
9767 if (m
==(vm_page_t
)0) break;
9769 } while (!queue_end(&vm_page_queue_inactive
,(queue_entry_t
) m
));
9770 vm_page_unlock_queues();
9772 vm_page_lock_queues();
9773 m
= (vm_page_t
) queue_first(&vm_page_queue_throttled
);
9775 if (m
==(vm_page_t
)0) break;
9779 assert(!m
->pageout
);
9780 assert(m
->object
!= kernel_object
);
9781 m
= (vm_page_t
) queue_next(&m
->pageq
);
9782 if (m
==(vm_page_t
)0) break;
9784 } while (!queue_end(&vm_page_queue_throttled
,(queue_entry_t
) m
));
9785 vm_page_unlock_queues();
9787 vm_page_lock_queues();
9788 m
= (vm_page_t
) queue_first(&vm_page_queue_anonymous
);
9790 if (m
==(vm_page_t
)0) break;
9792 if(m
->dirty
) dpages
++;
9793 if(m
->pageout
) pgopages
++;
9794 if(m
->precious
) precpages
++;
9796 assert(m
->object
!= kernel_object
);
9797 m
= (vm_page_t
) queue_next(&m
->pageq
);
9798 if (m
==(vm_page_t
)0) break;
9800 } while (!queue_end(&vm_page_queue_anonymous
,(queue_entry_t
) m
));
9801 vm_page_unlock_queues();
9803 printf("IN Q: %d : %d : %d\n", dpages
, pgopages
, precpages
);
9809 vm_page_lock_queues();
9810 m
= (vm_page_t
) queue_first(&vm_page_queue_active
);
9813 if(m
== (vm_page_t
)0) break;
9814 if(m
->dirty
) dpages
++;
9815 if(m
->pageout
) pgopages
++;
9816 if(m
->precious
) precpages
++;
9818 assert(m
->object
!= kernel_object
);
9819 m
= (vm_page_t
) queue_next(&m
->pageq
);
9820 if(m
== (vm_page_t
)0) break;
9822 } while (!queue_end(&vm_page_queue_active
,(queue_entry_t
) m
));
9823 vm_page_unlock_queues();
9825 printf("AC Q: %d : %d : %d\n", dpages
, pgopages
, precpages
);
9828 #endif /* MACH_BSD */
9830 ppnum_t
upl_get_highest_page(
9833 return upl
->highest_page
;
9836 upl_size_t
upl_get_size(
9843 kern_return_t
upl_ubc_alias_set(upl_t upl
, uintptr_t alias1
, uintptr_t alias2
)
9845 upl
->ubc_alias1
= alias1
;
9846 upl
->ubc_alias2
= alias2
;
9847 return KERN_SUCCESS
;
9849 int upl_ubc_alias_get(upl_t upl
, uintptr_t * al
, uintptr_t * al2
)
9852 *al
= upl
->ubc_alias1
;
9854 *al2
= upl
->ubc_alias2
;
9855 return KERN_SUCCESS
;
9857 #endif /* UPL_DEBUG */
9859 #if VM_PRESSURE_EVENTS
9861 * Upward trajectory.
9863 extern boolean_t
vm_compressor_low_on_space(void);
9866 VM_PRESSURE_NORMAL_TO_WARNING(void) {
9868 if (DEFAULT_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPLESS
) {
9870 /* Available pages below our threshold */
9871 if (memorystatus_available_pages
< memorystatus_available_pages_pressure
) {
9872 /* No frozen processes to kill */
9873 if (memorystatus_frozen_count
== 0) {
9874 /* Not enough suspended processes available. */
9875 if (memorystatus_suspended_count
< MEMORYSTATUS_SUSPENDED_THRESHOLD
) {
9883 return ((AVAILABLE_NON_COMPRESSED_MEMORY
< VM_PAGE_COMPRESSOR_COMPACT_THRESHOLD
) ? 1 : 0);
9888 VM_PRESSURE_WARNING_TO_CRITICAL(void) {
9890 if (DEFAULT_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPLESS
) {
9891 /* Available pages below our threshold */
9892 if (memorystatus_available_pages
< memorystatus_available_pages_critical
) {
9897 return (vm_compressor_low_on_space() || (AVAILABLE_NON_COMPRESSED_MEMORY
< ((12 * VM_PAGE_COMPRESSOR_SWAP_UNTHROTTLE_THRESHOLD
) / 10)) ? 1 : 0);
9902 * Downward trajectory.
9905 VM_PRESSURE_WARNING_TO_NORMAL(void) {
9907 if (DEFAULT_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPLESS
) {
9908 /* Available pages above our threshold */
9909 unsigned int target_threshold
= memorystatus_available_pages_pressure
+ ((15 * memorystatus_available_pages_pressure
) / 100);
9910 if (memorystatus_available_pages
> target_threshold
) {
9915 return ((AVAILABLE_NON_COMPRESSED_MEMORY
> ((12 * VM_PAGE_COMPRESSOR_COMPACT_THRESHOLD
) / 10)) ? 1 : 0);
9920 VM_PRESSURE_CRITICAL_TO_WARNING(void) {
9922 if (DEFAULT_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPLESS
) {
9923 /* Available pages above our threshold */
9924 unsigned int target_threshold
= memorystatus_available_pages_critical
+ ((15 * memorystatus_available_pages_critical
) / 100);
9925 if (memorystatus_available_pages
> target_threshold
) {
9930 return ((AVAILABLE_NON_COMPRESSED_MEMORY
> ((14 * VM_PAGE_COMPRESSOR_SWAP_UNTHROTTLE_THRESHOLD
) / 10)) ? 1 : 0);
9933 #endif /* VM_PRESSURE_EVENTS */