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>
92 #include <kern/policy_internal.h>
93 #include <kern/thread_group.h>
95 #include <machine/vm_tuning.h>
96 #include <machine/commpage.h>
99 #include <vm/vm_compressor_pager.h>
100 #include <vm/vm_fault.h>
101 #include <vm/vm_map.h>
102 #include <vm/vm_object.h>
103 #include <vm/vm_page.h>
104 #include <vm/vm_pageout.h>
105 #include <vm/vm_protos.h> /* must be last */
106 #include <vm/memory_object.h>
107 #include <vm/vm_purgeable_internal.h>
108 #include <vm/vm_shared_region.h>
109 #include <vm/vm_compressor.h>
111 #include <san/kasan.h>
113 #if CONFIG_PHANTOM_CACHE
114 #include <vm/vm_phantom_cache.h>
118 #include <libkern/OSDebug.h>
123 extern void mbuf_drain(boolean_t
);
125 #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 #else /* CONFIG_JETSAM */
131 extern uint64_t memorystatus_available_pages
;
132 extern uint64_t memorystatus_available_pages_pressure
;
133 extern uint64_t memorystatus_available_pages_critical
;
134 #endif /* CONFIG_JETSAM */
136 extern unsigned int memorystatus_frozen_count
;
137 extern unsigned int memorystatus_suspended_count
;
138 extern vm_pressure_level_t memorystatus_vm_pressure_level
;
140 void vm_pressure_response(void);
141 extern void consider_vm_pressure_events(void);
143 #define MEMORYSTATUS_SUSPENDED_THRESHOLD 4
144 #endif /* VM_PRESSURE_EVENTS */
147 #ifndef VM_PAGEOUT_BURST_INACTIVE_THROTTLE /* maximum iterations of the inactive queue w/o stealing/cleaning a page */
148 #ifdef CONFIG_EMBEDDED
149 #define VM_PAGEOUT_BURST_INACTIVE_THROTTLE 1024
151 #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_PAGE_LAUNDRY_MAX
160 #define VM_PAGE_LAUNDRY_MAX 128UL /* maximum pageouts on a given pageout queue */
161 #endif /* VM_PAGEOUT_LAUNDRY_MAX */
163 #ifndef VM_PAGEOUT_BURST_WAIT
164 #define VM_PAGEOUT_BURST_WAIT 1 /* milliseconds */
165 #endif /* VM_PAGEOUT_BURST_WAIT */
167 #ifndef VM_PAGEOUT_EMPTY_WAIT
168 #define VM_PAGEOUT_EMPTY_WAIT 50 /* milliseconds */
169 #endif /* VM_PAGEOUT_EMPTY_WAIT */
171 #ifndef VM_PAGEOUT_DEADLOCK_WAIT
172 #define VM_PAGEOUT_DEADLOCK_WAIT 100 /* milliseconds */
173 #endif /* VM_PAGEOUT_DEADLOCK_WAIT */
175 #ifndef VM_PAGEOUT_IDLE_WAIT
176 #define VM_PAGEOUT_IDLE_WAIT 10 /* milliseconds */
177 #endif /* VM_PAGEOUT_IDLE_WAIT */
179 #ifndef VM_PAGEOUT_SWAP_WAIT
180 #define VM_PAGEOUT_SWAP_WAIT 10 /* milliseconds */
181 #endif /* VM_PAGEOUT_SWAP_WAIT */
184 #ifndef VM_PAGE_SPECULATIVE_TARGET
185 #define VM_PAGE_SPECULATIVE_TARGET(total) ((total) * 1 / (100 / vm_pageout_state.vm_page_speculative_percentage))
186 #endif /* VM_PAGE_SPECULATIVE_TARGET */
190 * To obtain a reasonable LRU approximation, the inactive queue
191 * needs to be large enough to give pages on it a chance to be
192 * referenced a second time. This macro defines the fraction
193 * of active+inactive pages that should be inactive.
194 * The pageout daemon uses it to update vm_page_inactive_target.
196 * If vm_page_free_count falls below vm_page_free_target and
197 * vm_page_inactive_count is below vm_page_inactive_target,
198 * then the pageout daemon starts running.
201 #ifndef VM_PAGE_INACTIVE_TARGET
202 #define VM_PAGE_INACTIVE_TARGET(avail) ((avail) * 1 / 2)
203 #endif /* VM_PAGE_INACTIVE_TARGET */
206 * Once the pageout daemon starts running, it keeps going
207 * until vm_page_free_count meets or exceeds vm_page_free_target.
210 #ifndef VM_PAGE_FREE_TARGET
211 #ifdef CONFIG_EMBEDDED
212 #define VM_PAGE_FREE_TARGET(free) (15 + (free) / 100)
214 #define VM_PAGE_FREE_TARGET(free) (15 + (free) / 80)
216 #endif /* VM_PAGE_FREE_TARGET */
220 * The pageout daemon always starts running once vm_page_free_count
221 * falls below vm_page_free_min.
224 #ifndef VM_PAGE_FREE_MIN
225 #ifdef CONFIG_EMBEDDED
226 #define VM_PAGE_FREE_MIN(free) (10 + (free) / 200)
228 #define VM_PAGE_FREE_MIN(free) (10 + (free) / 100)
230 #endif /* VM_PAGE_FREE_MIN */
232 #ifdef CONFIG_EMBEDDED
233 #define VM_PAGE_FREE_RESERVED_LIMIT 100
234 #define VM_PAGE_FREE_MIN_LIMIT 1500
235 #define VM_PAGE_FREE_TARGET_LIMIT 2000
237 #define VM_PAGE_FREE_RESERVED_LIMIT 1700
238 #define VM_PAGE_FREE_MIN_LIMIT 3500
239 #define VM_PAGE_FREE_TARGET_LIMIT 4000
243 * When vm_page_free_count falls below vm_page_free_reserved,
244 * only vm-privileged threads can allocate pages. vm-privilege
245 * allows the pageout daemon and default pager (and any other
246 * associated threads needed for default pageout) to continue
247 * operation by dipping into the reserved pool of pages.
250 #ifndef VM_PAGE_FREE_RESERVED
251 #define VM_PAGE_FREE_RESERVED(n) \
252 ((unsigned) (6 * VM_PAGE_LAUNDRY_MAX) + (n))
253 #endif /* VM_PAGE_FREE_RESERVED */
256 * When we dequeue pages from the inactive list, they are
257 * reactivated (ie, put back on the active queue) if referenced.
258 * However, it is possible to starve the free list if other
259 * processors are referencing pages faster than we can turn off
260 * the referenced bit. So we limit the number of reactivations
261 * we will make per call of vm_pageout_scan().
263 #define VM_PAGE_REACTIVATE_LIMIT_MAX 20000
265 #ifndef VM_PAGE_REACTIVATE_LIMIT
266 #ifdef CONFIG_EMBEDDED
267 #define VM_PAGE_REACTIVATE_LIMIT(avail) (VM_PAGE_INACTIVE_TARGET(avail) / 2)
269 #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 1000
274 extern boolean_t hibernate_cleaning_in_progress
;
277 * Forward declarations for internal routines.
280 struct vm_pageout_queue
*q
;
286 struct cq ciq
[MAX_COMPRESSOR_THREAD_COUNT
];
289 #if VM_PRESSURE_EVENTS
290 void vm_pressure_thread(void);
292 boolean_t
VM_PRESSURE_NORMAL_TO_WARNING(void);
293 boolean_t
VM_PRESSURE_WARNING_TO_CRITICAL(void);
295 boolean_t
VM_PRESSURE_WARNING_TO_NORMAL(void);
296 boolean_t
VM_PRESSURE_CRITICAL_TO_WARNING(void);
299 void vm_pageout_garbage_collect(int);
300 static void vm_pageout_iothread_external(void);
301 static void vm_pageout_iothread_internal(struct cq
*cq
);
302 static void vm_pageout_adjust_eq_iothrottle(struct vm_pageout_queue
*, boolean_t
);
304 extern void vm_pageout_continue(void);
305 extern void vm_pageout_scan(void);
307 void vm_tests(void); /* forward */
310 static boolean_t vm_pageout_waiter
= FALSE
;
311 static boolean_t vm_pageout_running
= FALSE
;
312 #endif /* !CONFIG_EMBEDDED */
315 #if DEVELOPMENT || DEBUG
316 struct vm_pageout_debug vm_pageout_debug
;
318 struct vm_pageout_vminfo vm_pageout_vminfo
;
319 struct vm_pageout_state vm_pageout_state
;
320 struct vm_config vm_config
;
322 struct vm_pageout_queue vm_pageout_queue_internal
__attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT
)));
323 struct vm_pageout_queue vm_pageout_queue_external
__attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT
)));
325 int vm_upl_wait_for_pages
= 0;
326 vm_object_t vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
328 boolean_t (* volatile consider_buffer_cache_collect
)(int) = NULL
;
330 int vm_debug_events
= 0;
332 #if CONFIG_MEMORYSTATUS
333 extern boolean_t
memorystatus_kill_on_VM_page_shortage(boolean_t async
);
335 uint32_t vm_pageout_memorystatus_fb_factor_nr
= 5;
336 uint32_t vm_pageout_memorystatus_fb_factor_dr
= 2;
343 * Routine: vm_pageout_object_terminate
345 * Destroy the pageout_object, and perform all of the
346 * required cleanup actions.
349 * The object must be locked, and will be returned locked.
352 vm_pageout_object_terminate(
355 vm_object_t shadow_object
;
358 * Deal with the deallocation (last reference) of a pageout object
359 * (used for cleaning-in-place) by dropping the paging references/
360 * freeing pages in the original object.
363 assert(object
->pageout
);
364 shadow_object
= object
->shadow
;
365 vm_object_lock(shadow_object
);
367 while (!vm_page_queue_empty(&object
->memq
)) {
369 vm_object_offset_t offset
;
371 p
= (vm_page_t
) vm_page_queue_first(&object
->memq
);
373 assert(p
->vmp_private
);
374 assert(p
->vmp_free_when_done
);
375 p
->vmp_free_when_done
= FALSE
;
376 assert(!p
->vmp_cleaning
);
377 assert(!p
->vmp_laundry
);
379 offset
= p
->vmp_offset
;
383 m
= vm_page_lookup(shadow_object
,
384 offset
+ object
->vo_shadow_offset
);
386 if(m
== VM_PAGE_NULL
)
389 assert((m
->vmp_dirty
) || (m
->vmp_precious
) ||
390 (m
->vmp_busy
&& m
->vmp_cleaning
));
393 * Handle the trusted pager throttle.
394 * Also decrement the burst throttle (if external).
396 vm_page_lock_queues();
397 if (m
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
)
398 vm_pageout_throttle_up(m
);
401 * Handle the "target" page(s). These pages are to be freed if
402 * successfully cleaned. Target pages are always busy, and are
403 * wired exactly once. The initial target pages are not mapped,
404 * (so cannot be referenced or modified) but converted target
405 * pages may have been modified between the selection as an
406 * adjacent page and conversion to a target.
408 if (m
->vmp_free_when_done
) {
410 assert(m
->vmp_q_state
== VM_PAGE_IS_WIRED
);
411 assert(m
->vmp_wire_count
== 1);
412 m
->vmp_cleaning
= FALSE
;
413 m
->vmp_free_when_done
= FALSE
;
415 * Revoke all access to the page. Since the object is
416 * locked, and the page is busy, this prevents the page
417 * from being dirtied after the pmap_disconnect() call
420 * Since the page is left "dirty" but "not modifed", we
421 * can detect whether the page was redirtied during
422 * pageout by checking the modify state.
424 if (pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
)) & VM_MEM_MODIFIED
) {
425 SET_PAGE_DIRTY(m
, FALSE
);
427 m
->vmp_dirty
= FALSE
;
431 vm_page_unwire(m
, TRUE
); /* reactivates */
432 VM_STAT_INCR(reactivations
);
435 vm_page_free(m
); /* clears busy, etc. */
437 vm_page_unlock_queues();
441 * Handle the "adjacent" pages. These pages were cleaned in
442 * place, and should be left alone.
443 * If prep_pin_count is nonzero, then someone is using the
444 * page, so make it active.
446 if ((m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
) && !m
->vmp_private
) {
447 if (m
->vmp_reference
)
450 vm_page_deactivate(m
);
452 if (m
->vmp_overwriting
) {
454 * the (COPY_OUT_FROM == FALSE) request_page_list case
458 * We do not re-set m->vmp_dirty !
459 * The page was busy so no extraneous activity
460 * could have occurred. COPY_INTO is a read into the
461 * new pages. CLEAN_IN_PLACE does actually write
462 * out the pages but handling outside of this code
463 * will take care of resetting dirty. We clear the
464 * modify however for the Programmed I/O case.
466 pmap_clear_modify(VM_PAGE_GET_PHYS_PAGE(m
));
469 m
->vmp_absent
= FALSE
;
472 * alternate (COPY_OUT_FROM == FALSE) request_page_list case
473 * Occurs when the original page was wired
474 * at the time of the list request
476 assert(VM_PAGE_WIRED(m
));
477 vm_page_unwire(m
, TRUE
); /* reactivates */
479 m
->vmp_overwriting
= FALSE
;
481 m
->vmp_dirty
= FALSE
;
483 m
->vmp_cleaning
= FALSE
;
486 * Wakeup any thread waiting for the page to be un-cleaning.
489 vm_page_unlock_queues();
492 * Account for the paging reference taken in vm_paging_object_allocate.
494 vm_object_activity_end(shadow_object
);
495 vm_object_unlock(shadow_object
);
497 assert(object
->ref_count
== 0);
498 assert(object
->paging_in_progress
== 0);
499 assert(object
->activity_in_progress
== 0);
500 assert(object
->resident_page_count
== 0);
505 * Routine: vm_pageclean_setup
507 * Purpose: setup a page to be cleaned (made non-dirty), but not
508 * necessarily flushed from the VM page cache.
509 * This is accomplished by cleaning in place.
511 * The page must not be busy, and new_object
519 vm_object_t new_object
,
520 vm_object_offset_t new_offset
)
522 assert(!m
->vmp_busy
);
524 assert(!m
->vmp_cleaning
);
528 "vm_pageclean_setup, obj 0x%X off 0x%X page 0x%X new 0x%X new_off 0x%X\n",
529 VM_PAGE_OBJECT(m
), m
->vmp_offset
, m
,
532 pmap_clear_modify(VM_PAGE_GET_PHYS_PAGE(m
));
535 * Mark original page as cleaning in place.
537 m
->vmp_cleaning
= TRUE
;
538 SET_PAGE_DIRTY(m
, FALSE
);
539 m
->vmp_precious
= FALSE
;
542 * Convert the fictitious page to a private shadow of
545 assert(new_m
->vmp_fictitious
);
546 assert(VM_PAGE_GET_PHYS_PAGE(new_m
) == vm_page_fictitious_addr
);
547 new_m
->vmp_fictitious
= FALSE
;
548 new_m
->vmp_private
= TRUE
;
549 new_m
->vmp_free_when_done
= TRUE
;
550 VM_PAGE_SET_PHYS_PAGE(new_m
, VM_PAGE_GET_PHYS_PAGE(m
));
552 vm_page_lockspin_queues();
553 vm_page_wire(new_m
, VM_KERN_MEMORY_NONE
, TRUE
);
554 vm_page_unlock_queues();
556 vm_page_insert_wired(new_m
, new_object
, new_offset
, VM_KERN_MEMORY_NONE
);
557 assert(!new_m
->vmp_wanted
);
558 new_m
->vmp_busy
= FALSE
;
562 * Routine: vm_pageout_initialize_page
564 * Causes the specified page to be initialized in
565 * the appropriate memory object. This routine is used to push
566 * pages into a copy-object when they are modified in the
569 * The page is moved to a temporary object and paged out.
572 * The page in question must not be on any pageout queues.
573 * The object to which it belongs must be locked.
574 * The page must be busy, but not hold a paging reference.
577 * Move this page to a completely new object.
580 vm_pageout_initialize_page(
584 vm_object_offset_t paging_offset
;
585 memory_object_t pager
;
588 "vm_pageout_initialize_page, page 0x%X\n",
591 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
593 object
= VM_PAGE_OBJECT(m
);
596 assert(object
->internal
);
599 * Verify that we really want to clean this page
601 assert(!m
->vmp_absent
);
602 assert(!m
->vmp_error
);
603 assert(m
->vmp_dirty
);
606 * Create a paging reference to let us play with the object.
608 paging_offset
= m
->vmp_offset
+ object
->paging_offset
;
610 if (m
->vmp_absent
|| m
->vmp_error
|| m
->vmp_restart
|| (!m
->vmp_dirty
&& !m
->vmp_precious
)) {
611 panic("reservation without pageout?"); /* alan */
614 vm_object_unlock(object
);
620 * If there's no pager, then we can't clean the page. This should
621 * never happen since this should be a copy object and therefore not
622 * an external object, so the pager should always be there.
625 pager
= object
->pager
;
627 if (pager
== MEMORY_OBJECT_NULL
) {
628 panic("missing pager for copy object");
635 * set the page for future call to vm_fault_list_request
637 pmap_clear_modify(VM_PAGE_GET_PHYS_PAGE(m
));
638 SET_PAGE_DIRTY(m
, FALSE
);
641 * keep the object from collapsing or terminating
643 vm_object_paging_begin(object
);
644 vm_object_unlock(object
);
647 * Write the data to its pager.
648 * Note that the data is passed by naming the new object,
649 * not a virtual address; the pager interface has been
650 * manipulated to use the "internal memory" data type.
651 * [The object reference from its allocation is donated
652 * to the eventual recipient.]
654 memory_object_data_initialize(pager
, paging_offset
, PAGE_SIZE
);
656 vm_object_lock(object
);
657 vm_object_paging_end(object
);
662 * vm_pageout_cluster:
664 * Given a page, queue it to the appropriate I/O thread,
665 * which will page it out and attempt to clean adjacent pages
666 * in the same operation.
668 * The object and queues must be locked. We will take a
669 * paging reference to prevent deallocation or collapse when we
670 * release the object lock back at the call site. The I/O thread
671 * is responsible for consuming this reference
673 * The page must not be on any pageout queue.
675 #if DEVELOPMENT || DEBUG
676 vmct_stats_t vmct_stats
;
678 int32_t vmct_active
= 0;
679 uint64_t vm_compressor_epoch_start
= 0;
680 uint64_t vm_compressor_epoch_stop
= 0;
682 typedef enum vmct_state_t
{
687 vmct_state_t vmct_state
[MAX_COMPRESSOR_THREAD_COUNT
];
692 vm_pageout_cluster(vm_page_t m
)
694 vm_object_t object
= VM_PAGE_OBJECT(m
);
695 struct vm_pageout_queue
*q
;
699 "vm_pageout_cluster, object 0x%X offset 0x%X page 0x%X\n",
700 object
, m
->vmp_offset
, m
, 0, 0);
703 LCK_MTX_ASSERT(&vm_page_queue_lock
, LCK_MTX_ASSERT_OWNED
);
704 vm_object_lock_assert_exclusive(object
);
707 * Only a certain kind of page is appreciated here.
709 assert((m
->vmp_dirty
|| m
->vmp_precious
) && (!VM_PAGE_WIRED(m
)));
710 assert(!m
->vmp_cleaning
&& !m
->vmp_laundry
);
711 assert(m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
);
714 * protect the object from collapse or termination
716 vm_object_activity_begin(object
);
718 if (object
->internal
== TRUE
) {
719 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
723 q
= &vm_pageout_queue_internal
;
725 q
= &vm_pageout_queue_external
;
728 * pgo_laundry count is tied to the laundry bit
730 m
->vmp_laundry
= TRUE
;
733 m
->vmp_q_state
= VM_PAGE_ON_PAGEOUT_Q
;
734 vm_page_queue_enter(&q
->pgo_pending
, m
, vm_page_t
, vmp_pageq
);
736 if (q
->pgo_idle
== TRUE
) {
738 thread_wakeup((event_t
) &q
->pgo_pending
);
745 * A page is back from laundry or we are stealing it back from
746 * the laundering state. See if there are some pages waiting to
747 * go to laundry and if we can let some of them go now.
749 * Object and page queues must be locked.
752 vm_pageout_throttle_up(
755 struct vm_pageout_queue
*q
;
756 vm_object_t m_object
;
758 m_object
= VM_PAGE_OBJECT(m
);
760 assert(m_object
!= VM_OBJECT_NULL
);
761 assert(m_object
!= kernel_object
);
763 LCK_MTX_ASSERT(&vm_page_queue_lock
, LCK_MTX_ASSERT_OWNED
);
764 vm_object_lock_assert_exclusive(m_object
);
766 if (m_object
->internal
== TRUE
)
767 q
= &vm_pageout_queue_internal
;
769 q
= &vm_pageout_queue_external
;
771 if (m
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
) {
773 vm_page_queue_remove(&q
->pgo_pending
, m
, vm_page_t
, vmp_pageq
);
774 m
->vmp_q_state
= VM_PAGE_NOT_ON_Q
;
776 VM_PAGE_ZERO_PAGEQ_ENTRY(m
);
778 vm_object_activity_end(m_object
);
780 VM_PAGEOUT_DEBUG(vm_page_steal_pageout_page
, 1);
782 if (m
->vmp_laundry
== TRUE
) {
784 m
->vmp_laundry
= FALSE
;
787 if (q
->pgo_throttled
== TRUE
) {
788 q
->pgo_throttled
= FALSE
;
789 thread_wakeup((event_t
) &q
->pgo_laundry
);
791 if (q
->pgo_draining
== TRUE
&& q
->pgo_laundry
== 0) {
792 q
->pgo_draining
= FALSE
;
793 thread_wakeup((event_t
) (&q
->pgo_laundry
+1));
795 VM_PAGEOUT_DEBUG(vm_pageout_throttle_up_count
, 1);
801 vm_pageout_throttle_up_batch(
802 struct vm_pageout_queue
*q
,
805 LCK_MTX_ASSERT(&vm_page_queue_lock
, LCK_MTX_ASSERT_OWNED
);
807 VM_PAGEOUT_DEBUG(vm_pageout_throttle_up_count
, batch_cnt
);
809 q
->pgo_laundry
-= batch_cnt
;
811 if (q
->pgo_throttled
== TRUE
) {
812 q
->pgo_throttled
= FALSE
;
813 thread_wakeup((event_t
) &q
->pgo_laundry
);
815 if (q
->pgo_draining
== TRUE
&& q
->pgo_laundry
== 0) {
816 q
->pgo_draining
= FALSE
;
817 thread_wakeup((event_t
) (&q
->pgo_laundry
+1));
824 * VM memory pressure monitoring.
826 * vm_pageout_scan() keeps track of the number of pages it considers and
827 * reclaims, in the currently active vm_pageout_stat[vm_pageout_stat_now].
829 * compute_memory_pressure() is called every second from compute_averages()
830 * and moves "vm_pageout_stat_now" forward, to start accumulating the number
831 * of recalimed pages in a new vm_pageout_stat[] bucket.
833 * mach_vm_pressure_monitor() collects past statistics about memory pressure.
834 * The caller provides the number of seconds ("nsecs") worth of statistics
835 * it wants, up to 30 seconds.
836 * It computes the number of pages reclaimed in the past "nsecs" seconds and
837 * also returns the number of pages the system still needs to reclaim at this
840 #if DEVELOPMENT || DEBUG
841 #define VM_PAGEOUT_STAT_SIZE (30 * 8) + 1
843 #define VM_PAGEOUT_STAT_SIZE (1 * 8) + 1
845 struct vm_pageout_stat
{
846 unsigned long vm_page_active_count
;
847 unsigned long vm_page_speculative_count
;
848 unsigned long vm_page_inactive_count
;
849 unsigned long vm_page_anonymous_count
;
851 unsigned long vm_page_free_count
;
852 unsigned long vm_page_wire_count
;
853 unsigned long vm_page_compressor_count
;
855 unsigned long vm_page_pages_compressed
;
856 unsigned long vm_page_pageable_internal_count
;
857 unsigned long vm_page_pageable_external_count
;
858 unsigned long vm_page_xpmapped_external_count
;
860 unsigned int pages_grabbed
;
861 unsigned int pages_freed
;
863 unsigned int pages_compressed
;
864 unsigned int pages_grabbed_by_compressor
;
865 unsigned int failed_compressions
;
867 unsigned int pages_evicted
;
868 unsigned int pages_purged
;
870 unsigned int considered
;
871 unsigned int considered_bq_internal
;
872 unsigned int considered_bq_external
;
874 unsigned int skipped_external
;
875 unsigned int filecache_min_reactivations
;
877 unsigned int freed_speculative
;
878 unsigned int freed_cleaned
;
879 unsigned int freed_internal
;
880 unsigned int freed_external
;
882 unsigned int cleaned_dirty_external
;
883 unsigned int cleaned_dirty_internal
;
885 unsigned int inactive_referenced
;
886 unsigned int inactive_nolock
;
887 unsigned int reactivation_limit_exceeded
;
888 unsigned int forced_inactive_reclaim
;
890 unsigned int throttled_internal_q
;
891 unsigned int throttled_external_q
;
893 unsigned int phantom_ghosts_found
;
894 unsigned int phantom_ghosts_added
;
895 } vm_pageout_stats
[VM_PAGEOUT_STAT_SIZE
] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };
897 unsigned int vm_pageout_stat_now
= 0;
899 #define VM_PAGEOUT_STAT_BEFORE(i) \
900 (((i) == 0) ? VM_PAGEOUT_STAT_SIZE - 1 : (i) - 1)
901 #define VM_PAGEOUT_STAT_AFTER(i) \
902 (((i) == VM_PAGEOUT_STAT_SIZE - 1) ? 0 : (i) + 1)
904 #if VM_PAGE_BUCKETS_CHECK
905 int vm_page_buckets_check_interval
= 80; /* in eighths of a second */
906 #endif /* VM_PAGE_BUCKETS_CHECK */
910 record_memory_pressure(void);
912 record_memory_pressure(void)
914 unsigned int vm_pageout_next
;
916 #if VM_PAGE_BUCKETS_CHECK
917 /* check the consistency of VM page buckets at regular interval */
918 static int counter
= 0;
919 if ((++counter
% vm_page_buckets_check_interval
) == 0) {
920 vm_page_buckets_check();
922 #endif /* VM_PAGE_BUCKETS_CHECK */
924 vm_pageout_state
.vm_memory_pressure
=
925 vm_pageout_stats
[VM_PAGEOUT_STAT_BEFORE(vm_pageout_stat_now
)].freed_speculative
+
926 vm_pageout_stats
[VM_PAGEOUT_STAT_BEFORE(vm_pageout_stat_now
)].freed_cleaned
+
927 vm_pageout_stats
[VM_PAGEOUT_STAT_BEFORE(vm_pageout_stat_now
)].freed_internal
+
928 vm_pageout_stats
[VM_PAGEOUT_STAT_BEFORE(vm_pageout_stat_now
)].freed_external
;
930 commpage_set_memory_pressure( (unsigned int)vm_pageout_state
.vm_memory_pressure
);
932 /* move "now" forward */
933 vm_pageout_next
= VM_PAGEOUT_STAT_AFTER(vm_pageout_stat_now
);
935 bzero(&vm_pageout_stats
[vm_pageout_next
], sizeof(struct vm_pageout_stat
));
937 vm_pageout_stat_now
= vm_pageout_next
;
943 * mach_vm_ctl_page_free_wanted() is called indirectly, via
944 * mach_vm_pressure_monitor(), when taking a stackshot. Therefore,
945 * it must be safe in the restricted stackshot context. Locks and/or
946 * blocking are not allowable.
949 mach_vm_ctl_page_free_wanted(void)
951 unsigned int page_free_target
, page_free_count
, page_free_wanted
;
953 page_free_target
= vm_page_free_target
;
954 page_free_count
= vm_page_free_count
;
955 if (page_free_target
> page_free_count
) {
956 page_free_wanted
= page_free_target
- page_free_count
;
958 page_free_wanted
= 0;
961 return page_free_wanted
;
967 * mach_vm_pressure_monitor() is called when taking a stackshot, with
968 * wait_for_pressure FALSE, so that code path must remain safe in the
969 * restricted stackshot context. No blocking or locks are allowable.
974 mach_vm_pressure_monitor(
975 boolean_t wait_for_pressure
,
976 unsigned int nsecs_monitored
,
977 unsigned int *pages_reclaimed_p
,
978 unsigned int *pages_wanted_p
)
981 unsigned int vm_pageout_then
, vm_pageout_now
;
982 unsigned int pages_reclaimed
;
983 unsigned int units_of_monitor
;
985 units_of_monitor
= 8 * nsecs_monitored
;
987 * We don't take the vm_page_queue_lock here because we don't want
988 * vm_pressure_monitor() to get in the way of the vm_pageout_scan()
989 * thread when it's trying to reclaim memory. We don't need fully
990 * accurate monitoring anyway...
993 if (wait_for_pressure
) {
994 /* wait until there's memory pressure */
995 while (vm_page_free_count
>= vm_page_free_target
) {
996 wr
= assert_wait((event_t
) &vm_page_free_wanted
,
997 THREAD_INTERRUPTIBLE
);
998 if (wr
== THREAD_WAITING
) {
999 wr
= thread_block(THREAD_CONTINUE_NULL
);
1001 if (wr
== THREAD_INTERRUPTED
) {
1002 return KERN_ABORTED
;
1004 if (wr
== THREAD_AWAKENED
) {
1006 * The memory pressure might have already
1007 * been relieved but let's not block again
1008 * and let's report that there was memory
1009 * pressure at some point.
1016 /* provide the number of pages the system wants to reclaim */
1017 if (pages_wanted_p
!= NULL
) {
1018 *pages_wanted_p
= mach_vm_ctl_page_free_wanted();
1021 if (pages_reclaimed_p
== NULL
) {
1022 return KERN_SUCCESS
;
1025 /* provide number of pages reclaimed in the last "nsecs_monitored" */
1026 vm_pageout_now
= vm_pageout_stat_now
;
1027 pages_reclaimed
= 0;
1028 for (vm_pageout_then
=
1029 VM_PAGEOUT_STAT_BEFORE(vm_pageout_now
);
1030 vm_pageout_then
!= vm_pageout_now
&&
1031 units_of_monitor
-- != 0;
1033 VM_PAGEOUT_STAT_BEFORE(vm_pageout_then
)) {
1034 pages_reclaimed
+= vm_pageout_stats
[vm_pageout_then
].freed_speculative
;
1035 pages_reclaimed
+= vm_pageout_stats
[vm_pageout_then
].freed_cleaned
;
1036 pages_reclaimed
+= vm_pageout_stats
[vm_pageout_then
].freed_internal
;
1037 pages_reclaimed
+= vm_pageout_stats
[vm_pageout_then
].freed_external
;
1039 *pages_reclaimed_p
= pages_reclaimed
;
1041 return KERN_SUCCESS
;
1046 #if DEVELOPMENT || DEBUG
1049 vm_pageout_disconnect_all_pages_in_queue(vm_page_queue_head_t
*, int);
1052 * condition variable used to make sure there is
1053 * only a single sweep going on at a time
1055 boolean_t vm_pageout_disconnect_all_pages_active
= FALSE
;
1059 vm_pageout_disconnect_all_pages()
1061 vm_page_lock_queues();
1063 if (vm_pageout_disconnect_all_pages_active
== TRUE
) {
1064 vm_page_unlock_queues();
1067 vm_pageout_disconnect_all_pages_active
= TRUE
;
1068 vm_page_unlock_queues();
1070 vm_pageout_disconnect_all_pages_in_queue(&vm_page_queue_throttled
, vm_page_throttled_count
);
1071 vm_pageout_disconnect_all_pages_in_queue(&vm_page_queue_anonymous
, vm_page_anonymous_count
);
1072 vm_pageout_disconnect_all_pages_in_queue(&vm_page_queue_active
, vm_page_active_count
);
1074 vm_pageout_disconnect_all_pages_active
= FALSE
;
1079 vm_pageout_disconnect_all_pages_in_queue(vm_page_queue_head_t
*q
, int qcount
)
1082 vm_object_t t_object
= NULL
;
1083 vm_object_t l_object
= NULL
;
1084 vm_object_t m_object
= NULL
;
1085 int delayed_unlock
= 0;
1086 int try_failed_count
= 0;
1087 int disconnected_count
= 0;
1088 int paused_count
= 0;
1089 int object_locked_count
= 0;
1091 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_ALL_PAGE_MAPPINGS
)) | DBG_FUNC_START
,
1092 q
, qcount
, 0, 0, 0);
1094 vm_page_lock_queues();
1096 while (qcount
&& !vm_page_queue_empty(q
)) {
1098 LCK_MTX_ASSERT(&vm_page_queue_lock
, LCK_MTX_ASSERT_OWNED
);
1100 m
= (vm_page_t
) vm_page_queue_first(q
);
1101 m_object
= VM_PAGE_OBJECT(m
);
1104 * check to see if we currently are working
1105 * with the same object... if so, we've
1106 * already got the lock
1108 if (m_object
!= l_object
) {
1110 * the object associated with candidate page is
1111 * different from the one we were just working
1112 * with... dump the lock if we still own it
1114 if (l_object
!= NULL
) {
1115 vm_object_unlock(l_object
);
1118 if (m_object
!= t_object
)
1119 try_failed_count
= 0;
1122 * Try to lock object; since we've alread got the
1123 * page queues lock, we can only 'try' for this one.
1124 * if the 'try' fails, we need to do a mutex_pause
1125 * to allow the owner of the object lock a chance to
1128 if ( !vm_object_lock_try_scan(m_object
)) {
1130 if (try_failed_count
> 20) {
1131 goto reenter_pg_on_q
;
1133 vm_page_unlock_queues();
1134 mutex_pause(try_failed_count
++);
1135 vm_page_lock_queues();
1140 t_object
= m_object
;
1143 object_locked_count
++;
1145 l_object
= m_object
;
1147 if ( !m_object
->alive
|| m
->vmp_cleaning
|| m
->vmp_laundry
|| m
->vmp_busy
|| m
->vmp_absent
|| m
->vmp_error
|| m
->vmp_free_when_done
) {
1149 * put it back on the head of its queue
1151 goto reenter_pg_on_q
;
1153 if (m
->vmp_pmapped
== TRUE
) {
1155 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
1157 disconnected_count
++;
1160 vm_page_queue_remove(q
, m
, vm_page_t
, vmp_pageq
);
1161 vm_page_queue_enter(q
, m
, vm_page_t
, vmp_pageq
);
1164 try_failed_count
= 0;
1166 if (delayed_unlock
++ > 128) {
1168 if (l_object
!= NULL
) {
1169 vm_object_unlock(l_object
);
1172 lck_mtx_yield(&vm_page_queue_lock
);
1176 if (l_object
!= NULL
) {
1177 vm_object_unlock(l_object
);
1180 vm_page_unlock_queues();
1182 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (MACHDBG_CODE(DBG_MACH_WORKINGSET
, VM_DISCONNECT_ALL_PAGE_MAPPINGS
)) | DBG_FUNC_END
,
1183 q
, disconnected_count
, object_locked_count
, paused_count
, 0);
1190 vm_pageout_page_queue(vm_page_queue_head_t
*, int);
1193 * condition variable used to make sure there is
1194 * only a single sweep going on at a time
1196 boolean_t vm_pageout_anonymous_pages_active
= FALSE
;
1200 vm_pageout_anonymous_pages()
1202 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
) {
1204 vm_page_lock_queues();
1206 if (vm_pageout_anonymous_pages_active
== TRUE
) {
1207 vm_page_unlock_queues();
1210 vm_pageout_anonymous_pages_active
= TRUE
;
1211 vm_page_unlock_queues();
1213 vm_pageout_page_queue(&vm_page_queue_throttled
, vm_page_throttled_count
);
1214 vm_pageout_page_queue(&vm_page_queue_anonymous
, vm_page_anonymous_count
);
1215 vm_pageout_page_queue(&vm_page_queue_active
, vm_page_active_count
);
1217 if (VM_CONFIG_SWAP_IS_PRESENT
)
1218 vm_consider_swapping();
1220 vm_page_lock_queues();
1221 vm_pageout_anonymous_pages_active
= FALSE
;
1222 vm_page_unlock_queues();
1228 vm_pageout_page_queue(vm_page_queue_head_t
*q
, int qcount
)
1231 vm_object_t t_object
= NULL
;
1232 vm_object_t l_object
= NULL
;
1233 vm_object_t m_object
= NULL
;
1234 int delayed_unlock
= 0;
1235 int try_failed_count
= 0;
1238 struct vm_pageout_queue
*iq
;
1242 iq
= &vm_pageout_queue_internal
;
1244 vm_page_lock_queues();
1246 while (qcount
&& !vm_page_queue_empty(q
)) {
1248 LCK_MTX_ASSERT(&vm_page_queue_lock
, LCK_MTX_ASSERT_OWNED
);
1250 if (VM_PAGE_Q_THROTTLED(iq
)) {
1252 if (l_object
!= NULL
) {
1253 vm_object_unlock(l_object
);
1256 iq
->pgo_draining
= TRUE
;
1258 assert_wait((event_t
) (&iq
->pgo_laundry
+ 1), THREAD_INTERRUPTIBLE
);
1259 vm_page_unlock_queues();
1261 thread_block(THREAD_CONTINUE_NULL
);
1263 vm_page_lock_queues();
1267 m
= (vm_page_t
) vm_page_queue_first(q
);
1268 m_object
= VM_PAGE_OBJECT(m
);
1271 * check to see if we currently are working
1272 * with the same object... if so, we've
1273 * already got the lock
1275 if (m_object
!= l_object
) {
1276 if ( !m_object
->internal
)
1277 goto reenter_pg_on_q
;
1280 * the object associated with candidate page is
1281 * different from the one we were just working
1282 * with... dump the lock if we still own it
1284 if (l_object
!= NULL
) {
1285 vm_object_unlock(l_object
);
1288 if (m_object
!= t_object
)
1289 try_failed_count
= 0;
1292 * Try to lock object; since we've alread got the
1293 * page queues lock, we can only 'try' for this one.
1294 * if the 'try' fails, we need to do a mutex_pause
1295 * to allow the owner of the object lock a chance to
1298 if ( !vm_object_lock_try_scan(m_object
)) {
1300 if (try_failed_count
> 20) {
1301 goto reenter_pg_on_q
;
1303 vm_page_unlock_queues();
1304 mutex_pause(try_failed_count
++);
1305 vm_page_lock_queues();
1308 t_object
= m_object
;
1311 l_object
= m_object
;
1313 if ( !m_object
->alive
|| m
->vmp_cleaning
|| m
->vmp_laundry
|| m
->vmp_busy
|| m
->vmp_absent
|| m
->vmp_error
|| m
->vmp_free_when_done
) {
1315 * page is not to be cleaned
1316 * put it back on the head of its queue
1318 goto reenter_pg_on_q
;
1320 phys_page
= VM_PAGE_GET_PHYS_PAGE(m
);
1322 if (m
->vmp_reference
== FALSE
&& m
->vmp_pmapped
== TRUE
) {
1323 refmod_state
= pmap_get_refmod(phys_page
);
1325 if (refmod_state
& VM_MEM_REFERENCED
)
1326 m
->vmp_reference
= TRUE
;
1327 if (refmod_state
& VM_MEM_MODIFIED
) {
1328 SET_PAGE_DIRTY(m
, FALSE
);
1331 if (m
->vmp_reference
== TRUE
) {
1332 m
->vmp_reference
= FALSE
;
1333 pmap_clear_refmod_options(phys_page
, VM_MEM_REFERENCED
, PMAP_OPTIONS_NOFLUSH
, (void *)NULL
);
1334 goto reenter_pg_on_q
;
1336 if (m
->vmp_pmapped
== TRUE
) {
1337 if (m
->vmp_dirty
|| m
->vmp_precious
) {
1338 pmap_options
= PMAP_OPTIONS_COMPRESSOR
;
1340 pmap_options
= PMAP_OPTIONS_COMPRESSOR_IFF_MODIFIED
;
1342 refmod_state
= pmap_disconnect_options(phys_page
, pmap_options
, NULL
);
1343 if (refmod_state
& VM_MEM_MODIFIED
) {
1344 SET_PAGE_DIRTY(m
, FALSE
);
1348 if ( !m
->vmp_dirty
&& !m
->vmp_precious
) {
1349 vm_page_unlock_queues();
1351 vm_page_lock_queues();
1356 if (!m_object
->pager_initialized
|| m_object
->pager
== MEMORY_OBJECT_NULL
) {
1358 if (!m_object
->pager_initialized
) {
1360 vm_page_unlock_queues();
1362 vm_object_collapse(m_object
, (vm_object_offset_t
) 0, TRUE
);
1364 if (!m_object
->pager_initialized
)
1365 vm_object_compressor_pager_create(m_object
);
1367 vm_page_lock_queues();
1370 if (!m_object
->pager_initialized
|| m_object
->pager
== MEMORY_OBJECT_NULL
)
1371 goto reenter_pg_on_q
;
1373 * vm_object_compressor_pager_create will drop the object lock
1374 * which means 'm' may no longer be valid to use
1379 * we've already factored out pages in the laundry which
1380 * means this page can't be on the pageout queue so it's
1381 * safe to do the vm_page_queues_remove
1383 vm_page_queues_remove(m
, TRUE
);
1385 LCK_MTX_ASSERT(&vm_page_queue_lock
, LCK_MTX_ASSERT_OWNED
);
1387 vm_pageout_cluster(m
);
1392 vm_page_queue_remove(q
, m
, vm_page_t
, vmp_pageq
);
1393 vm_page_queue_enter(q
, m
, vm_page_t
, vmp_pageq
);
1396 try_failed_count
= 0;
1398 if (delayed_unlock
++ > 128) {
1400 if (l_object
!= NULL
) {
1401 vm_object_unlock(l_object
);
1404 lck_mtx_yield(&vm_page_queue_lock
);
1408 if (l_object
!= NULL
) {
1409 vm_object_unlock(l_object
);
1412 vm_page_unlock_queues();
1418 * function in BSD to apply I/O throttle to the pageout thread
1420 extern void vm_pageout_io_throttle(void);
1422 #define VM_PAGEOUT_SCAN_HANDLE_REUSABLE_PAGE(m, obj) \
1425 * If a "reusable" page somehow made it back into \
1426 * the active queue, it's been re-used and is not \
1427 * quite re-usable. \
1428 * If the VM object was "all_reusable", consider it \
1429 * as "all re-used" instead of converting it to \
1430 * "partially re-used", which could be expensive. \
1432 assert(VM_PAGE_OBJECT((m)) == (obj)); \
1433 if ((m)->vmp_reusable || \
1434 (obj)->all_reusable) { \
1435 vm_object_reuse_pages((obj), \
1437 (m)->vmp_offset + PAGE_SIZE_64, \
1443 #define VM_PAGEOUT_DELAYED_UNLOCK_LIMIT 64
1444 #define VM_PAGEOUT_DELAYED_UNLOCK_LIMIT_MAX 1024
1447 #define FCS_DELAYED 1
1448 #define FCS_DEADLOCK_DETECTED 2
1450 struct flow_control
{
1456 #if CONFIG_BACKGROUND_QUEUE
1457 uint64_t vm_pageout_rejected_bq_internal
= 0;
1458 uint64_t vm_pageout_rejected_bq_external
= 0;
1459 uint64_t vm_pageout_skipped_bq_internal
= 0;
1462 #define ANONS_GRABBED_LIMIT 2
1466 static void vm_pageout_delayed_unlock(int *, int *, vm_page_t
*);
1468 static void vm_pageout_prepare_to_block(vm_object_t
*, int *, vm_page_t
*, int *, int);
1470 #define VM_PAGEOUT_PB_NO_ACTION 0
1471 #define VM_PAGEOUT_PB_CONSIDER_WAKING_COMPACTOR_SWAPPER 1
1472 #define VM_PAGEOUT_PB_THREAD_YIELD 2
1477 vm_pageout_delayed_unlock(int *delayed_unlock
, int *local_freed
, vm_page_t
*local_freeq
)
1480 vm_page_unlock_queues();
1482 VM_DEBUG_CONSTANT_EVENT(
1483 vm_pageout_freelist
, VM_PAGEOUT_FREELIST
, DBG_FUNC_START
,
1484 vm_page_free_count
, 0, 0, 1);
1486 vm_page_free_list(*local_freeq
, TRUE
);
1488 VM_DEBUG_CONSTANT_EVENT(vm_pageout_freelist
,VM_PAGEOUT_FREELIST
, DBG_FUNC_END
,
1489 vm_page_free_count
, *local_freed
, 0, 1);
1491 *local_freeq
= NULL
;
1494 vm_page_lock_queues();
1496 lck_mtx_yield(&vm_page_queue_lock
);
1498 *delayed_unlock
= 1;
1504 vm_pageout_prepare_to_block(vm_object_t
*object
, int *delayed_unlock
,
1505 vm_page_t
*local_freeq
, int *local_freed
, int action
)
1507 vm_page_unlock_queues();
1509 if (*object
!= NULL
) {
1510 vm_object_unlock(*object
);
1515 vm_page_free_list(*local_freeq
, TRUE
);
1517 *local_freeq
= NULL
;
1520 *delayed_unlock
= 1;
1524 case VM_PAGEOUT_PB_CONSIDER_WAKING_COMPACTOR_SWAPPER
:
1525 vm_consider_waking_compactor_swapper();
1527 case VM_PAGEOUT_PB_THREAD_YIELD
:
1528 thread_yield_internal(1);
1530 case VM_PAGEOUT_PB_NO_ACTION
:
1534 vm_page_lock_queues();
1538 static struct vm_pageout_vminfo last
;
1540 uint64_t last_vm_page_pages_grabbed
= 0;
1542 extern uint32_t c_segment_pages_compressed
;
1544 extern uint64_t shared_region_pager_reclaimed
;
1545 extern struct memory_object_pager_ops shared_region_pager_ops
;
1547 void update_vm_info(void)
1551 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_active_count
= vm_page_active_count
;
1552 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_speculative_count
= vm_page_speculative_count
;
1553 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_inactive_count
= vm_page_inactive_count
;
1554 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_anonymous_count
= vm_page_anonymous_count
;
1556 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_free_count
= vm_page_free_count
;
1557 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_wire_count
= vm_page_wire_count
;
1558 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_compressor_count
= VM_PAGE_COMPRESSOR_COUNT
;
1560 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_pages_compressed
= c_segment_pages_compressed
;
1561 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_pageable_internal_count
= vm_page_pageable_internal_count
;
1562 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_pageable_external_count
= vm_page_pageable_external_count
;
1563 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_xpmapped_external_count
= vm_page_xpmapped_external_count
;
1566 tmp
= vm_pageout_vminfo
.vm_pageout_considered_page
;
1567 vm_pageout_stats
[vm_pageout_stat_now
].considered
= (unsigned int)(tmp
- last
.vm_pageout_considered_page
);
1568 last
.vm_pageout_considered_page
= tmp
;
1570 tmp
= vm_pageout_vminfo
.vm_pageout_compressions
;
1571 vm_pageout_stats
[vm_pageout_stat_now
].pages_compressed
= (unsigned int)(tmp
- last
.vm_pageout_compressions
);
1572 last
.vm_pageout_compressions
= tmp
;
1574 tmp
= vm_pageout_vminfo
.vm_compressor_failed
;
1575 vm_pageout_stats
[vm_pageout_stat_now
].failed_compressions
= (unsigned int)(tmp
- last
.vm_compressor_failed
);
1576 last
.vm_compressor_failed
= tmp
;
1578 tmp
= vm_pageout_vminfo
.vm_compressor_pages_grabbed
;
1579 vm_pageout_stats
[vm_pageout_stat_now
].pages_grabbed_by_compressor
= (unsigned int)(tmp
- last
.vm_compressor_pages_grabbed
);
1580 last
.vm_compressor_pages_grabbed
= tmp
;
1582 tmp
= vm_pageout_vminfo
.vm_phantom_cache_found_ghost
;
1583 vm_pageout_stats
[vm_pageout_stat_now
].phantom_ghosts_found
= (unsigned int)(tmp
- last
.vm_phantom_cache_found_ghost
);
1584 last
.vm_phantom_cache_found_ghost
= tmp
;
1586 tmp
= vm_pageout_vminfo
.vm_phantom_cache_added_ghost
;
1587 vm_pageout_stats
[vm_pageout_stat_now
].phantom_ghosts_added
= (unsigned int)(tmp
- last
.vm_phantom_cache_added_ghost
);
1588 last
.vm_phantom_cache_added_ghost
= tmp
;
1590 tmp
= get_pages_grabbed_count();
1591 vm_pageout_stats
[vm_pageout_stat_now
].pages_grabbed
= (unsigned int)(tmp
- last_vm_page_pages_grabbed
);
1592 last_vm_page_pages_grabbed
= tmp
;
1594 tmp
= vm_pageout_vminfo
.vm_page_pages_freed
;
1595 vm_pageout_stats
[vm_pageout_stat_now
].pages_freed
= (unsigned int)(tmp
- last
.vm_page_pages_freed
);
1596 last
.vm_page_pages_freed
= tmp
;
1599 if (vm_pageout_stats
[vm_pageout_stat_now
].considered
) {
1601 tmp
= vm_pageout_vminfo
.vm_pageout_pages_evicted
;
1602 vm_pageout_stats
[vm_pageout_stat_now
].pages_evicted
= (unsigned int)(tmp
- last
.vm_pageout_pages_evicted
);
1603 last
.vm_pageout_pages_evicted
= tmp
;
1605 tmp
= vm_pageout_vminfo
.vm_pageout_pages_purged
;
1606 vm_pageout_stats
[vm_pageout_stat_now
].pages_purged
= (unsigned int)(tmp
- last
.vm_pageout_pages_purged
);
1607 last
.vm_pageout_pages_purged
= tmp
;
1609 tmp
= vm_pageout_vminfo
.vm_pageout_freed_speculative
;
1610 vm_pageout_stats
[vm_pageout_stat_now
].freed_speculative
= (unsigned int)(tmp
- last
.vm_pageout_freed_speculative
);
1611 last
.vm_pageout_freed_speculative
= tmp
;
1613 tmp
= vm_pageout_vminfo
.vm_pageout_freed_external
;
1614 vm_pageout_stats
[vm_pageout_stat_now
].freed_external
= (unsigned int)(tmp
- last
.vm_pageout_freed_external
);
1615 last
.vm_pageout_freed_external
= tmp
;
1617 tmp
= vm_pageout_vminfo
.vm_pageout_inactive_referenced
;
1618 vm_pageout_stats
[vm_pageout_stat_now
].inactive_referenced
= (unsigned int)(tmp
- last
.vm_pageout_inactive_referenced
);
1619 last
.vm_pageout_inactive_referenced
= tmp
;
1621 tmp
= vm_pageout_vminfo
.vm_pageout_scan_inactive_throttled_external
;
1622 vm_pageout_stats
[vm_pageout_stat_now
].throttled_external_q
= (unsigned int)(tmp
- last
.vm_pageout_scan_inactive_throttled_external
);
1623 last
.vm_pageout_scan_inactive_throttled_external
= tmp
;
1625 tmp
= vm_pageout_vminfo
.vm_pageout_inactive_dirty_external
;
1626 vm_pageout_stats
[vm_pageout_stat_now
].cleaned_dirty_external
= (unsigned int)(tmp
- last
.vm_pageout_inactive_dirty_external
);
1627 last
.vm_pageout_inactive_dirty_external
= tmp
;
1629 tmp
= vm_pageout_vminfo
.vm_pageout_freed_cleaned
;
1630 vm_pageout_stats
[vm_pageout_stat_now
].freed_cleaned
= (unsigned int)(tmp
- last
.vm_pageout_freed_cleaned
);
1631 last
.vm_pageout_freed_cleaned
= tmp
;
1633 tmp
= vm_pageout_vminfo
.vm_pageout_inactive_nolock
;
1634 vm_pageout_stats
[vm_pageout_stat_now
].inactive_nolock
= (unsigned int)(tmp
- last
.vm_pageout_inactive_nolock
);
1635 last
.vm_pageout_inactive_nolock
= tmp
;
1637 tmp
= vm_pageout_vminfo
.vm_pageout_scan_inactive_throttled_internal
;
1638 vm_pageout_stats
[vm_pageout_stat_now
].throttled_internal_q
= (unsigned int)(tmp
- last
.vm_pageout_scan_inactive_throttled_internal
);
1639 last
.vm_pageout_scan_inactive_throttled_internal
= tmp
;
1641 tmp
= vm_pageout_vminfo
.vm_pageout_skipped_external
;
1642 vm_pageout_stats
[vm_pageout_stat_now
].skipped_external
= (unsigned int)(tmp
- last
.vm_pageout_skipped_external
);
1643 last
.vm_pageout_skipped_external
= tmp
;
1645 tmp
= vm_pageout_vminfo
.vm_pageout_reactivation_limit_exceeded
;
1646 vm_pageout_stats
[vm_pageout_stat_now
].reactivation_limit_exceeded
= (unsigned int)(tmp
- last
.vm_pageout_reactivation_limit_exceeded
);
1647 last
.vm_pageout_reactivation_limit_exceeded
= tmp
;
1649 tmp
= vm_pageout_vminfo
.vm_pageout_inactive_force_reclaim
;
1650 vm_pageout_stats
[vm_pageout_stat_now
].forced_inactive_reclaim
= (unsigned int)(tmp
- last
.vm_pageout_inactive_force_reclaim
);
1651 last
.vm_pageout_inactive_force_reclaim
= tmp
;
1653 tmp
= vm_pageout_vminfo
.vm_pageout_freed_internal
;
1654 vm_pageout_stats
[vm_pageout_stat_now
].freed_internal
= (unsigned int)(tmp
- last
.vm_pageout_freed_internal
);
1655 last
.vm_pageout_freed_internal
= tmp
;
1657 tmp
= vm_pageout_vminfo
.vm_pageout_considered_bq_internal
;
1658 vm_pageout_stats
[vm_pageout_stat_now
].considered_bq_internal
= (unsigned int)(tmp
- last
.vm_pageout_considered_bq_internal
);
1659 last
.vm_pageout_considered_bq_internal
= tmp
;
1661 tmp
= vm_pageout_vminfo
.vm_pageout_considered_bq_external
;
1662 vm_pageout_stats
[vm_pageout_stat_now
].considered_bq_external
= (unsigned int)(tmp
- last
.vm_pageout_considered_bq_external
);
1663 last
.vm_pageout_considered_bq_external
= tmp
;
1665 tmp
= vm_pageout_vminfo
.vm_pageout_filecache_min_reactivated
;
1666 vm_pageout_stats
[vm_pageout_stat_now
].filecache_min_reactivations
= (unsigned int)(tmp
- last
.vm_pageout_filecache_min_reactivated
);
1667 last
.vm_pageout_filecache_min_reactivated
= tmp
;
1669 tmp
= vm_pageout_vminfo
.vm_pageout_inactive_dirty_internal
;
1670 vm_pageout_stats
[vm_pageout_stat_now
].cleaned_dirty_internal
= (unsigned int)(tmp
- last
.vm_pageout_inactive_dirty_internal
);
1671 last
.vm_pageout_inactive_dirty_internal
= tmp
;
1674 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_INFO1
)) | DBG_FUNC_NONE
,
1675 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_active_count
,
1676 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_speculative_count
,
1677 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_inactive_count
,
1678 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_anonymous_count
,
1681 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_INFO2
)) | DBG_FUNC_NONE
,
1682 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_free_count
,
1683 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_wire_count
,
1684 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_compressor_count
,
1688 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_INFO3
)) | DBG_FUNC_NONE
,
1689 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_pages_compressed
,
1690 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_pageable_internal_count
,
1691 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_pageable_external_count
,
1692 vm_pageout_stats
[vm_pageout_stat_now
].vm_page_xpmapped_external_count
,
1695 if (vm_pageout_stats
[vm_pageout_stat_now
].considered
||
1696 vm_pageout_stats
[vm_pageout_stat_now
].pages_compressed
||
1697 vm_pageout_stats
[vm_pageout_stat_now
].failed_compressions
) {
1699 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_INFO4
)) | DBG_FUNC_NONE
,
1700 vm_pageout_stats
[vm_pageout_stat_now
].considered
,
1701 vm_pageout_stats
[vm_pageout_stat_now
].freed_speculative
,
1702 vm_pageout_stats
[vm_pageout_stat_now
].freed_external
,
1703 vm_pageout_stats
[vm_pageout_stat_now
].inactive_referenced
,
1706 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_INFO5
)) | DBG_FUNC_NONE
,
1707 vm_pageout_stats
[vm_pageout_stat_now
].throttled_external_q
,
1708 vm_pageout_stats
[vm_pageout_stat_now
].cleaned_dirty_external
,
1709 vm_pageout_stats
[vm_pageout_stat_now
].freed_cleaned
,
1710 vm_pageout_stats
[vm_pageout_stat_now
].inactive_nolock
,
1713 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_INFO6
)) | DBG_FUNC_NONE
,
1714 vm_pageout_stats
[vm_pageout_stat_now
].throttled_internal_q
,
1715 vm_pageout_stats
[vm_pageout_stat_now
].pages_compressed
,
1716 vm_pageout_stats
[vm_pageout_stat_now
].pages_grabbed_by_compressor
,
1717 vm_pageout_stats
[vm_pageout_stat_now
].skipped_external
,
1720 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_INFO7
)) | DBG_FUNC_NONE
,
1721 vm_pageout_stats
[vm_pageout_stat_now
].reactivation_limit_exceeded
,
1722 vm_pageout_stats
[vm_pageout_stat_now
].forced_inactive_reclaim
,
1723 vm_pageout_stats
[vm_pageout_stat_now
].failed_compressions
,
1724 vm_pageout_stats
[vm_pageout_stat_now
].freed_internal
,
1727 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_INFO8
)) | DBG_FUNC_NONE
,
1728 vm_pageout_stats
[vm_pageout_stat_now
].considered_bq_internal
,
1729 vm_pageout_stats
[vm_pageout_stat_now
].considered_bq_external
,
1730 vm_pageout_stats
[vm_pageout_stat_now
].filecache_min_reactivations
,
1731 vm_pageout_stats
[vm_pageout_stat_now
].cleaned_dirty_internal
,
1735 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, VM_INFO9
)) | DBG_FUNC_NONE
,
1736 vm_pageout_stats
[vm_pageout_stat_now
].pages_grabbed
,
1737 vm_pageout_stats
[vm_pageout_stat_now
].pages_freed
,
1738 vm_pageout_stats
[vm_pageout_stat_now
].phantom_ghosts_found
,
1739 vm_pageout_stats
[vm_pageout_stat_now
].phantom_ghosts_added
,
1742 record_memory_pressure();
1747 vm_page_balance_inactive(int max_to_move
)
1751 LCK_MTX_ASSERT(&vm_page_queue_lock
, LCK_MTX_ASSERT_OWNED
);
1753 vm_page_inactive_target
= VM_PAGE_INACTIVE_TARGET(vm_page_active_count
+
1754 vm_page_inactive_count
+
1755 vm_page_speculative_count
);
1757 while (max_to_move
-- && (vm_page_inactive_count
+ vm_page_speculative_count
) < vm_page_inactive_target
) {
1759 VM_PAGEOUT_DEBUG(vm_pageout_balanced
, 1);
1761 m
= (vm_page_t
) vm_page_queue_first(&vm_page_queue_active
);
1763 assert(m
->vmp_q_state
== VM_PAGE_ON_ACTIVE_Q
);
1764 assert(!m
->vmp_laundry
);
1765 assert(VM_PAGE_OBJECT(m
) != kernel_object
);
1766 assert(VM_PAGE_GET_PHYS_PAGE(m
) != vm_page_guard_addr
);
1768 DTRACE_VM2(scan
, int, 1, (uint64_t *), NULL
);
1771 * by not passing in a pmap_flush_context we will forgo any TLB flushing, local or otherwise...
1773 * a TLB flush isn't really needed here since at worst we'll miss the reference bit being
1774 * updated in the PTE if a remote processor still has this mapping cached in its TLB when the
1775 * new reference happens. If no futher references happen on the page after that remote TLB flushes
1776 * we'll see a clean, non-referenced page when it eventually gets pulled out of the inactive queue
1777 * by pageout_scan, which is just fine since the last reference would have happened quite far
1778 * in the past (TLB caches don't hang around for very long), and of course could just as easily
1779 * have happened before we moved the page
1781 if (m
->vmp_pmapped
== TRUE
)
1782 pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE(m
), VM_MEM_REFERENCED
, PMAP_OPTIONS_NOFLUSH
, (void *)NULL
);
1785 * The page might be absent or busy,
1786 * but vm_page_deactivate can handle that.
1787 * FALSE indicates that we don't want a H/W clear reference
1789 vm_page_deactivate_internal(m
, FALSE
);
1795 * vm_pageout_scan does the dirty work for the pageout daemon.
1796 * It returns with both vm_page_queue_free_lock and vm_page_queue_lock
1797 * held and vm_page_free_wanted == 0.
1800 vm_pageout_scan(void)
1802 unsigned int loop_count
= 0;
1803 unsigned int inactive_burst_count
= 0;
1804 unsigned int reactivated_this_call
;
1805 unsigned int reactivate_limit
;
1806 vm_page_t local_freeq
= NULL
;
1807 int local_freed
= 0;
1809 int delayed_unlock_limit
= 0;
1810 int refmod_state
= 0;
1811 int vm_pageout_deadlock_target
= 0;
1812 struct vm_pageout_queue
*iq
;
1813 struct vm_pageout_queue
*eq
;
1814 struct vm_speculative_age_q
*sq
;
1815 struct flow_control flow_control
= { 0, { 0, 0 } };
1816 boolean_t inactive_throttled
= FALSE
;
1818 unsigned int msecs
= 0;
1819 vm_object_t object
= NULL
;
1820 uint32_t inactive_reclaim_run
;
1821 boolean_t exceeded_burst_throttle
;
1822 boolean_t grab_anonymous
= FALSE
;
1823 boolean_t force_anonymous
= FALSE
;
1824 boolean_t force_speculative_aging
= FALSE
;
1825 int anons_grabbed
= 0;
1826 int page_prev_q_state
= 0;
1827 #if CONFIG_BACKGROUND_QUEUE
1828 boolean_t page_from_bg_q
= FALSE
;
1830 int cache_evict_throttle
= 0;
1831 uint32_t vm_pageout_inactive_external_forced_reactivate_limit
= 0;
1832 uint32_t inactive_external_count
;
1833 int force_purge
= 0;
1835 #define DELAY_SPECULATIVE_AGE 1000
1836 int delay_speculative_age
= 0;
1837 vm_object_t m_object
= VM_OBJECT_NULL
;
1839 #if VM_PRESSURE_EVENTS
1840 vm_pressure_level_t pressure_level
;
1841 #endif /* VM_PRESSURE_EVENTS */
1843 VM_DEBUG_CONSTANT_EVENT(vm_pageout_scan
, VM_PAGEOUT_SCAN
, DBG_FUNC_START
,
1844 vm_pageout_vminfo
.vm_pageout_freed_speculative
,
1845 vm_pageout_state
.vm_pageout_inactive_clean
,
1846 vm_pageout_vminfo
.vm_pageout_inactive_dirty_internal
,
1847 vm_pageout_vminfo
.vm_pageout_inactive_dirty_external
);
1849 flow_control
.state
= FCS_IDLE
;
1850 iq
= &vm_pageout_queue_internal
;
1851 eq
= &vm_pageout_queue_external
;
1852 sq
= &vm_page_queue_speculative
[VM_PAGE_SPECULATIVE_AGED_Q
];
1855 XPR(XPR_VM_PAGEOUT
, "vm_pageout_scan\n", 0, 0, 0, 0, 0);
1857 /* Ask the pmap layer to return any pages it no longer needs. */
1858 uint64_t pmap_wired_pages_freed
= pmap_release_pages_fast();
1860 vm_page_lock_queues();
1862 vm_page_wire_count
-= pmap_wired_pages_freed
;
1867 * Calculate the max number of referenced pages on the inactive
1868 * queue that we will reactivate.
1870 reactivated_this_call
= 0;
1871 reactivate_limit
= VM_PAGE_REACTIVATE_LIMIT(vm_page_active_count
+
1872 vm_page_inactive_count
);
1873 inactive_reclaim_run
= 0;
1875 vm_pageout_inactive_external_forced_reactivate_limit
= vm_page_active_count
+ vm_page_inactive_count
;
1878 * We must limit the rate at which we send pages to the pagers
1879 * so that we don't tie up too many pages in the I/O queues.
1880 * We implement a throttling mechanism using the laundry count
1881 * to limit the number of pages outstanding to the default
1882 * and external pagers. We can bypass the throttles and look
1883 * for clean pages if the pageout queues don't drain in a timely
1884 * fashion since this may indicate that the pageout paths are
1885 * stalled waiting for memory, which only we can provide.
1890 assert(object
== NULL
);
1891 assert(delayed_unlock
!= 0);
1893 vm_page_anonymous_min
= vm_page_inactive_target
/ 20;
1895 if (vm_pageout_state
.vm_page_speculative_percentage
> 50)
1896 vm_pageout_state
.vm_page_speculative_percentage
= 50;
1897 else if (vm_pageout_state
.vm_page_speculative_percentage
<= 0)
1898 vm_pageout_state
.vm_page_speculative_percentage
= 1;
1900 vm_pageout_state
.vm_page_speculative_target
= VM_PAGE_SPECULATIVE_TARGET(vm_page_active_count
+
1901 vm_page_inactive_count
);
1906 DTRACE_VM2(rev
, int, 1, (uint64_t *), NULL
);
1908 if (vm_upl_wait_for_pages
< 0)
1909 vm_upl_wait_for_pages
= 0;
1911 delayed_unlock_limit
= VM_PAGEOUT_DELAYED_UNLOCK_LIMIT
+ vm_upl_wait_for_pages
;
1913 if (delayed_unlock_limit
> VM_PAGEOUT_DELAYED_UNLOCK_LIMIT_MAX
)
1914 delayed_unlock_limit
= VM_PAGEOUT_DELAYED_UNLOCK_LIMIT_MAX
;
1916 #if CONFIG_SECLUDED_MEMORY
1918 * Deal with secluded_q overflow.
1920 if (vm_page_secluded_count
> vm_page_secluded_target
) {
1921 vm_page_t secluded_page
;
1924 * SECLUDED_AGING_BEFORE_ACTIVE:
1925 * Excess secluded pages go to the active queue and
1926 * will later go to the inactive queue.
1928 assert((vm_page_secluded_count_free
+
1929 vm_page_secluded_count_inuse
) ==
1930 vm_page_secluded_count
);
1931 secluded_page
= (vm_page_t
)vm_page_queue_first(&vm_page_queue_secluded
);
1932 assert(secluded_page
->vmp_q_state
== VM_PAGE_ON_SECLUDED_Q
);
1934 vm_page_queues_remove(secluded_page
, FALSE
);
1935 assert(!secluded_page
->vmp_fictitious
);
1936 assert(!VM_PAGE_WIRED(secluded_page
));
1938 if (secluded_page
->vmp_object
== 0) {
1939 /* transfer to free queue */
1940 assert(secluded_page
->vmp_busy
);
1941 secluded_page
->vmp_snext
= local_freeq
;
1942 local_freeq
= secluded_page
;
1945 /* transfer to head of active queue */
1946 vm_page_enqueue_active(secluded_page
, FALSE
);
1947 secluded_page
= VM_PAGE_NULL
;
1950 #endif /* CONFIG_SECLUDED_MEMORY */
1952 assert(delayed_unlock
);
1955 * maintain our balance
1957 vm_page_balance_inactive(1);
1960 /**********************************************************************
1961 * above this point we're playing with the active and secluded queues
1962 * below this point we're playing with the throttling mechanisms
1963 * and the inactive queue
1964 **********************************************************************/
1966 if (vm_page_free_count
+ local_freed
>= vm_page_free_target
)
1968 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
1970 vm_pageout_prepare_to_block(&object
, &delayed_unlock
, &local_freeq
, &local_freed
,
1971 VM_PAGEOUT_PB_CONSIDER_WAKING_COMPACTOR_SWAPPER
);
1973 * make sure the pageout I/O threads are running
1974 * throttled in case there are still requests
1975 * in the laundry... since we have met our targets
1976 * we don't need the laundry to be cleaned in a timely
1977 * fashion... so let's avoid interfering with foreground
1980 vm_pageout_adjust_eq_iothrottle(eq
, TRUE
);
1982 lck_mtx_lock(&vm_page_queue_free_lock
);
1984 if ((vm_page_free_count
>= vm_page_free_target
) &&
1985 (vm_page_free_wanted
== 0) && (vm_page_free_wanted_privileged
== 0)) {
1987 * done - we have met our target *and*
1988 * there is no one waiting for a page.
1991 assert(vm_pageout_scan_wants_object
== VM_OBJECT_NULL
);
1993 VM_DEBUG_CONSTANT_EVENT(vm_pageout_scan
, VM_PAGEOUT_SCAN
, DBG_FUNC_NONE
,
1994 vm_pageout_state
.vm_pageout_inactive
,
1995 vm_pageout_state
.vm_pageout_inactive_used
, 0, 0);
1996 VM_DEBUG_CONSTANT_EVENT(vm_pageout_scan
, VM_PAGEOUT_SCAN
, DBG_FUNC_END
,
1997 vm_pageout_vminfo
.vm_pageout_freed_speculative
,
1998 vm_pageout_state
.vm_pageout_inactive_clean
,
1999 vm_pageout_vminfo
.vm_pageout_inactive_dirty_internal
,
2000 vm_pageout_vminfo
.vm_pageout_inactive_dirty_external
);
2004 lck_mtx_unlock(&vm_page_queue_free_lock
);
2008 * Before anything, we check if we have any ripe volatile
2009 * objects around. If so, try to purge the first object.
2010 * If the purge fails, fall through to reclaim a page instead.
2011 * If the purge succeeds, go back to the top and reevalute
2012 * the new memory situation.
2015 assert (available_for_purge
>=0);
2016 force_purge
= 0; /* no force-purging */
2018 #if VM_PRESSURE_EVENTS
2019 pressure_level
= memorystatus_vm_pressure_level
;
2021 if (pressure_level
> kVMPressureNormal
) {
2023 if (pressure_level
>= kVMPressureCritical
) {
2024 force_purge
= vm_pageout_state
.memorystatus_purge_on_critical
;
2025 } else if (pressure_level
>= kVMPressureUrgent
) {
2026 force_purge
= vm_pageout_state
.memorystatus_purge_on_urgent
;
2027 } else if (pressure_level
>= kVMPressureWarning
) {
2028 force_purge
= vm_pageout_state
.memorystatus_purge_on_warning
;
2031 #endif /* VM_PRESSURE_EVENTS */
2033 if (available_for_purge
|| force_purge
) {
2035 if (object
!= NULL
) {
2036 vm_object_unlock(object
);
2040 memoryshot(VM_PAGEOUT_PURGEONE
, DBG_FUNC_START
);
2042 VM_DEBUG_EVENT(vm_pageout_purgeone
, VM_PAGEOUT_PURGEONE
, DBG_FUNC_START
, vm_page_free_count
, 0, 0, 0);
2043 if (vm_purgeable_object_purge_one(force_purge
, C_DONT_BLOCK
)) {
2044 VM_PAGEOUT_DEBUG(vm_pageout_purged_objects
, 1);
2045 VM_DEBUG_EVENT(vm_pageout_purgeone
, VM_PAGEOUT_PURGEONE
, DBG_FUNC_END
, vm_page_free_count
, 0, 0, 0);
2046 memoryshot(VM_PAGEOUT_PURGEONE
, DBG_FUNC_END
);
2049 VM_DEBUG_EVENT(vm_pageout_purgeone
, VM_PAGEOUT_PURGEONE
, DBG_FUNC_END
, 0, 0, 0, -1);
2050 memoryshot(VM_PAGEOUT_PURGEONE
, DBG_FUNC_END
);
2053 if (vm_page_queue_empty(&sq
->age_q
) && vm_page_speculative_count
) {
2055 * try to pull pages from the aging bins...
2056 * see vm_page.h for an explanation of how
2057 * this mechanism works
2059 struct vm_speculative_age_q
*aq
;
2060 boolean_t can_steal
= FALSE
;
2061 int num_scanned_queues
;
2063 aq
= &vm_page_queue_speculative
[speculative_steal_index
];
2065 num_scanned_queues
= 0;
2066 while (vm_page_queue_empty(&aq
->age_q
) &&
2067 num_scanned_queues
++ != VM_PAGE_MAX_SPECULATIVE_AGE_Q
) {
2069 speculative_steal_index
++;
2071 if (speculative_steal_index
> VM_PAGE_MAX_SPECULATIVE_AGE_Q
)
2072 speculative_steal_index
= VM_PAGE_MIN_SPECULATIVE_AGE_Q
;
2074 aq
= &vm_page_queue_speculative
[speculative_steal_index
];
2077 if (num_scanned_queues
== VM_PAGE_MAX_SPECULATIVE_AGE_Q
+ 1) {
2079 * XXX We've scanned all the speculative
2080 * queues but still haven't found one
2081 * that is not empty, even though
2082 * vm_page_speculative_count is not 0.
2084 if (!vm_page_queue_empty(&sq
->age_q
))
2086 #if DEVELOPMENT || DEBUG
2087 panic("vm_pageout_scan: vm_page_speculative_count=%d but queues are empty", vm_page_speculative_count
);
2090 vm_page_speculative_count
= 0;
2091 /* ... and continue */
2095 if (vm_page_speculative_count
> vm_pageout_state
.vm_page_speculative_target
|| force_speculative_aging
== TRUE
)
2098 if (!delay_speculative_age
) {
2099 mach_timespec_t ts_fully_aged
;
2101 ts_fully_aged
.tv_sec
= (VM_PAGE_MAX_SPECULATIVE_AGE_Q
* vm_pageout_state
.vm_page_speculative_q_age_ms
) / 1000;
2102 ts_fully_aged
.tv_nsec
= ((VM_PAGE_MAX_SPECULATIVE_AGE_Q
* vm_pageout_state
.vm_page_speculative_q_age_ms
) % 1000)
2103 * 1000 * NSEC_PER_USEC
;
2105 ADD_MACH_TIMESPEC(&ts_fully_aged
, &aq
->age_ts
);
2109 clock_get_system_nanotime(&sec
, &nsec
);
2110 ts
.tv_sec
= (unsigned int) sec
;
2113 if (CMP_MACH_TIMESPEC(&ts
, &ts_fully_aged
) >= 0)
2116 delay_speculative_age
++;
2118 delay_speculative_age
++;
2119 if (delay_speculative_age
== DELAY_SPECULATIVE_AGE
)
2120 delay_speculative_age
= 0;
2123 if (can_steal
== TRUE
)
2124 vm_page_speculate_ageit(aq
);
2126 force_speculative_aging
= FALSE
;
2128 if (vm_page_queue_empty(&sq
->age_q
) && cache_evict_throttle
== 0) {
2132 if (object
!= NULL
) {
2133 vm_object_unlock(object
);
2136 KERNEL_DEBUG_CONSTANT(0x13001ec | DBG_FUNC_START
, 0, 0, 0, 0, 0);
2138 pages_evicted
= vm_object_cache_evict(100, 10);
2140 KERNEL_DEBUG_CONSTANT(0x13001ec | DBG_FUNC_END
, pages_evicted
, 0, 0, 0, 0);
2142 if (pages_evicted
) {
2144 vm_pageout_vminfo
.vm_pageout_pages_evicted
+= pages_evicted
;
2146 VM_DEBUG_EVENT(vm_pageout_cache_evict
, VM_PAGEOUT_CACHE_EVICT
, DBG_FUNC_NONE
,
2147 vm_page_free_count
, pages_evicted
, vm_pageout_vminfo
.vm_pageout_pages_evicted
, 0);
2148 memoryshot(VM_PAGEOUT_CACHE_EVICT
, DBG_FUNC_NONE
);
2151 * we just freed up to 100 pages,
2152 * so go back to the top of the main loop
2153 * and re-evaulate the memory situation
2157 cache_evict_throttle
= 1000;
2159 if (cache_evict_throttle
)
2160 cache_evict_throttle
--;
2162 divisor
= vm_pageout_state
.vm_page_filecache_min_divisor
;
2166 * don't let the filecache_min fall below 15% of available memory
2167 * on systems with an active compressor that isn't nearing its
2168 * limits w/r to accepting new data
2170 * on systems w/o the compressor/swapper, the filecache is always
2171 * a very large percentage of the AVAILABLE_NON_COMPRESSED_MEMORY
2172 * since most (if not all) of the anonymous pages are in the
2173 * throttled queue (which isn't counted as available) which
2174 * effectively disables this filter
2176 if (vm_compressor_low_on_space() || divisor
== 0)
2177 vm_pageout_state
.vm_page_filecache_min
= 0;
2179 vm_pageout_state
.vm_page_filecache_min
=
2180 ((AVAILABLE_NON_COMPRESSED_MEMORY
) * 10) / divisor
;
2182 if (vm_compressor_out_of_space() || divisor
== 0)
2183 vm_pageout_state
.vm_page_filecache_min
= 0;
2186 * don't let the filecache_min fall below the specified critical level
2188 vm_pageout_state
.vm_page_filecache_min
=
2189 ((AVAILABLE_NON_COMPRESSED_MEMORY
) * 10) / divisor
;
2192 if (vm_page_free_count
< (vm_page_free_reserved
/ 4))
2193 vm_pageout_state
.vm_page_filecache_min
= 0;
2195 exceeded_burst_throttle
= FALSE
;
2197 * Sometimes we have to pause:
2198 * 1) No inactive pages - nothing to do.
2199 * 2) Loop control - no acceptable pages found on the inactive queue
2200 * within the last vm_pageout_burst_inactive_throttle iterations
2201 * 3) Flow control - default pageout queue is full
2203 if (vm_page_queue_empty(&vm_page_queue_inactive
) &&
2204 vm_page_queue_empty(&vm_page_queue_anonymous
) &&
2205 vm_page_queue_empty(&vm_page_queue_cleaned
) &&
2206 vm_page_queue_empty(&sq
->age_q
)) {
2207 VM_PAGEOUT_DEBUG(vm_pageout_scan_empty_throttle
, 1);
2208 msecs
= vm_pageout_state
.vm_pageout_empty_wait
;
2209 goto vm_pageout_scan_delay
;
2211 } else if (inactive_burst_count
>=
2212 MIN(vm_pageout_state
.vm_pageout_burst_inactive_throttle
,
2213 (vm_page_inactive_count
+
2214 vm_page_speculative_count
))) {
2215 VM_PAGEOUT_DEBUG(vm_pageout_scan_burst_throttle
, 1);
2216 msecs
= vm_pageout_state
.vm_pageout_burst_wait
;
2218 exceeded_burst_throttle
= TRUE
;
2219 goto vm_pageout_scan_delay
;
2221 } else if (VM_PAGE_Q_THROTTLED(iq
) &&
2222 VM_DYNAMIC_PAGING_ENABLED()) {
2226 switch (flow_control
.state
) {
2229 if ((vm_page_free_count
+ local_freed
) < vm_page_free_target
&&
2230 vm_pageout_state
.vm_restricted_to_single_processor
== FALSE
) {
2232 * since the compressor is running independently of vm_pageout_scan
2233 * let's not wait for it just yet... as long as we have a healthy supply
2234 * of filecache pages to work with, let's keep stealing those.
2236 inactive_external_count
= vm_page_inactive_count
- vm_page_anonymous_count
;
2238 if (vm_page_pageable_external_count
> vm_pageout_state
.vm_page_filecache_min
&&
2239 (inactive_external_count
>= VM_PAGE_INACTIVE_TARGET(vm_page_pageable_external_count
))) {
2240 anons_grabbed
= ANONS_GRABBED_LIMIT
;
2241 VM_PAGEOUT_DEBUG(vm_pageout_scan_throttle_deferred
, 1);
2242 goto consider_inactive
;
2245 reset_deadlock_timer
:
2246 ts
.tv_sec
= vm_pageout_state
.vm_pageout_deadlock_wait
/ 1000;
2247 ts
.tv_nsec
= (vm_pageout_state
.vm_pageout_deadlock_wait
% 1000) * 1000 * NSEC_PER_USEC
;
2248 clock_get_system_nanotime(&sec
, &nsec
);
2249 flow_control
.ts
.tv_sec
= (unsigned int) sec
;
2250 flow_control
.ts
.tv_nsec
= nsec
;
2251 ADD_MACH_TIMESPEC(&flow_control
.ts
, &ts
);
2253 flow_control
.state
= FCS_DELAYED
;
2254 msecs
= vm_pageout_state
.vm_pageout_deadlock_wait
;
2256 vm_pageout_vminfo
.vm_pageout_scan_inactive_throttled_internal
++;
2260 clock_get_system_nanotime(&sec
, &nsec
);
2261 ts
.tv_sec
= (unsigned int) sec
;
2264 if (CMP_MACH_TIMESPEC(&ts
, &flow_control
.ts
) >= 0) {
2266 * the pageout thread for the default pager is potentially
2267 * deadlocked since the
2268 * default pager queue has been throttled for more than the
2269 * allowable time... we need to move some clean pages or dirty
2270 * pages belonging to the external pagers if they aren't throttled
2271 * vm_page_free_wanted represents the number of threads currently
2272 * blocked waiting for pages... we'll move one page for each of
2273 * these plus a fixed amount to break the logjam... once we're done
2274 * moving this number of pages, we'll re-enter the FSC_DELAYED state
2275 * with a new timeout target since we have no way of knowing
2276 * whether we've broken the deadlock except through observation
2277 * of the queue associated with the default pager... we need to
2278 * stop moving pages and allow the system to run to see what
2279 * state it settles into.
2281 vm_pageout_deadlock_target
= vm_pageout_state
.vm_pageout_deadlock_relief
+
2282 vm_page_free_wanted
+ vm_page_free_wanted_privileged
;
2283 VM_PAGEOUT_DEBUG(vm_pageout_scan_deadlock_detected
, 1);
2284 flow_control
.state
= FCS_DEADLOCK_DETECTED
;
2285 thread_wakeup((event_t
) &vm_pageout_garbage_collect
);
2286 goto consider_inactive
;
2289 * just resniff instead of trying
2290 * to compute a new delay time... we're going to be
2291 * awakened immediately upon a laundry completion,
2292 * so we won't wait any longer than necessary
2294 msecs
= vm_pageout_state
.vm_pageout_idle_wait
;
2297 case FCS_DEADLOCK_DETECTED
:
2298 if (vm_pageout_deadlock_target
)
2299 goto consider_inactive
;
2300 goto reset_deadlock_timer
;
2303 vm_pageout_scan_delay
:
2304 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
2306 vm_pageout_prepare_to_block(&object
, &delayed_unlock
, &local_freeq
, &local_freed
,
2307 VM_PAGEOUT_PB_CONSIDER_WAKING_COMPACTOR_SWAPPER
);
2309 if (vm_page_free_count
>= vm_page_free_target
) {
2311 * we're here because
2312 * 1) someone else freed up some pages while we had
2313 * the queues unlocked above
2314 * and we've hit one of the 3 conditions that
2315 * cause us to pause the pageout scan thread
2317 * since we already have enough free pages,
2318 * let's avoid stalling and return normally
2320 * before we return, make sure the pageout I/O threads
2321 * are running throttled in case there are still requests
2322 * in the laundry... since we have enough free pages
2323 * we don't need the laundry to be cleaned in a timely
2324 * fashion... so let's avoid interfering with foreground
2327 * we don't want to hold vm_page_queue_free_lock when
2328 * calling vm_pageout_adjust_eq_iothrottle (since it
2329 * may cause other locks to be taken), we do the intitial
2330 * check outside of the lock. Once we take the lock,
2331 * we recheck the condition since it may have changed.
2332 * if it has, no problem, we will make the threads
2333 * non-throttled before actually blocking
2335 vm_pageout_adjust_eq_iothrottle(eq
, TRUE
);
2337 lck_mtx_lock(&vm_page_queue_free_lock
);
2339 if (vm_page_free_count
>= vm_page_free_target
&&
2340 (vm_page_free_wanted
== 0) && (vm_page_free_wanted_privileged
== 0)) {
2341 goto return_from_scan
;
2343 lck_mtx_unlock(&vm_page_queue_free_lock
);
2345 if ((vm_page_free_count
+ vm_page_cleaned_count
) < vm_page_free_target
) {
2347 * we're most likely about to block due to one of
2348 * the 3 conditions that cause vm_pageout_scan to
2349 * not be able to make forward progress w/r
2350 * to providing new pages to the free queue,
2351 * so unthrottle the I/O threads in case we
2352 * have laundry to be cleaned... it needs
2353 * to be completed ASAP.
2355 * even if we don't block, we want the io threads
2356 * running unthrottled since the sum of free +
2357 * clean pages is still under our free target
2359 vm_pageout_adjust_eq_iothrottle(eq
, FALSE
);
2361 if (vm_page_cleaned_count
> 0 && exceeded_burst_throttle
== FALSE
) {
2363 * if we get here we're below our free target and
2364 * we're stalling due to a full laundry queue or
2365 * we don't have any inactive pages other then
2366 * those in the clean queue...
2367 * however, we have pages on the clean queue that
2368 * can be moved to the free queue, so let's not
2369 * stall the pageout scan
2371 flow_control
.state
= FCS_IDLE
;
2372 goto consider_inactive
;
2374 if (flow_control
.state
== FCS_DELAYED
&& !VM_PAGE_Q_THROTTLED(iq
)) {
2375 flow_control
.state
= FCS_IDLE
;
2376 goto consider_inactive
;
2379 VM_CHECK_MEMORYSTATUS
;
2381 if (flow_control
.state
!= FCS_IDLE
)
2382 VM_PAGEOUT_DEBUG(vm_pageout_scan_throttle
, 1);
2384 iq
->pgo_throttled
= TRUE
;
2385 assert_wait_timeout((event_t
) &iq
->pgo_laundry
, THREAD_INTERRUPTIBLE
, msecs
, 1000*NSEC_PER_USEC
);
2387 counter(c_vm_pageout_scan_block
++);
2389 vm_page_unlock_queues();
2391 assert(vm_pageout_scan_wants_object
== VM_OBJECT_NULL
);
2393 VM_DEBUG_EVENT(vm_pageout_thread_block
, VM_PAGEOUT_THREAD_BLOCK
, DBG_FUNC_START
,
2394 iq
->pgo_laundry
, iq
->pgo_maxlaundry
, msecs
, 0);
2395 memoryshot(VM_PAGEOUT_THREAD_BLOCK
, DBG_FUNC_START
);
2397 thread_block(THREAD_CONTINUE_NULL
);
2399 VM_DEBUG_EVENT(vm_pageout_thread_block
, VM_PAGEOUT_THREAD_BLOCK
, DBG_FUNC_END
,
2400 iq
->pgo_laundry
, iq
->pgo_maxlaundry
, msecs
, 0);
2401 memoryshot(VM_PAGEOUT_THREAD_BLOCK
, DBG_FUNC_END
);
2403 vm_page_lock_queues();
2405 iq
->pgo_throttled
= FALSE
;
2407 if (loop_count
>= vm_page_inactive_count
)
2409 inactive_burst_count
= 0;
2416 flow_control
.state
= FCS_IDLE
;
2418 vm_pageout_inactive_external_forced_reactivate_limit
= MIN((vm_page_active_count
+ vm_page_inactive_count
),
2419 vm_pageout_inactive_external_forced_reactivate_limit
);
2421 inactive_burst_count
++;
2422 vm_pageout_state
.vm_pageout_inactive
++;
2429 #if CONFIG_BACKGROUND_QUEUE
2430 page_from_bg_q
= FALSE
;
2431 #endif /* CONFIG_BACKGROUND_QUEUE */
2434 m_object
= VM_OBJECT_NULL
;
2436 if (VM_DYNAMIC_PAGING_ENABLED()) {
2437 assert(vm_page_throttled_count
== 0);
2438 assert(vm_page_queue_empty(&vm_page_queue_throttled
));
2442 * Try for a clean-queue inactive page.
2443 * These are pages that vm_pageout_scan tried to steal earlier, but
2444 * were dirty and had to be cleaned. Pick them up now that they are clean.
2446 if (!vm_page_queue_empty(&vm_page_queue_cleaned
)) {
2447 m
= (vm_page_t
) vm_page_queue_first(&vm_page_queue_cleaned
);
2449 assert(m
->vmp_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
);
2455 * The next most eligible pages are ones we paged in speculatively,
2456 * but which have not yet been touched and have been aged out.
2458 if (!vm_page_queue_empty(&sq
->age_q
)) {
2459 m
= (vm_page_t
) vm_page_queue_first(&sq
->age_q
);
2461 assert(m
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
);
2463 if (!m
->vmp_dirty
|| force_anonymous
== FALSE
)
2469 #if CONFIG_BACKGROUND_QUEUE
2470 if (vm_page_background_mode
!= VM_PAGE_BG_DISABLED
&& (vm_page_background_count
> vm_page_background_target
)) {
2471 vm_object_t bg_m_object
= NULL
;
2473 m
= (vm_page_t
) vm_page_queue_first(&vm_page_queue_background
);
2475 bg_m_object
= VM_PAGE_OBJECT(m
);
2477 if (!VM_PAGE_PAGEABLE(m
)) {
2479 * This page is on the background queue
2480 * but not on a pageable queue. This is
2481 * likely a transient state and whoever
2482 * took it out of its pageable queue
2483 * will likely put it back on a pageable
2484 * queue soon but we can't deal with it
2485 * at this point, so let's ignore this
2488 } else if (force_anonymous
== FALSE
|| bg_m_object
->internal
) {
2490 if (bg_m_object
->internal
&&
2491 (VM_PAGE_Q_THROTTLED(iq
) ||
2492 vm_compressor_out_of_space() == TRUE
||
2493 vm_page_free_count
< (vm_page_free_reserved
/ 4))) {
2495 vm_pageout_skipped_bq_internal
++;
2497 page_from_bg_q
= TRUE
;
2499 if (bg_m_object
->internal
)
2500 vm_pageout_vminfo
.vm_pageout_considered_bq_internal
++;
2502 vm_pageout_vminfo
.vm_pageout_considered_bq_external
++;
2508 inactive_external_count
= vm_page_inactive_count
- vm_page_anonymous_count
;
2510 if ((vm_page_pageable_external_count
< vm_pageout_state
.vm_page_filecache_min
|| force_anonymous
== TRUE
) ||
2511 (inactive_external_count
< VM_PAGE_INACTIVE_TARGET(vm_page_pageable_external_count
))) {
2512 grab_anonymous
= TRUE
;
2515 vm_pageout_vminfo
.vm_pageout_skipped_external
++;
2516 goto want_anonymous
;
2518 grab_anonymous
= (vm_page_anonymous_count
> vm_page_anonymous_min
);
2521 /* If the file-backed pool has accumulated
2522 * significantly more pages than the jetsam
2523 * threshold, prefer to reclaim those
2524 * inline to minimise compute overhead of reclaiming
2526 * This calculation does not account for the CPU local
2527 * external page queues, as those are expected to be
2528 * much smaller relative to the global pools.
2530 if (grab_anonymous
== TRUE
&& !VM_PAGE_Q_THROTTLED(eq
)) {
2531 if (vm_page_pageable_external_count
>
2532 vm_pageout_state
.vm_page_filecache_min
) {
2533 if ((vm_page_pageable_external_count
*
2534 vm_pageout_memorystatus_fb_factor_dr
) >
2535 (memorystatus_available_pages_critical
*
2536 vm_pageout_memorystatus_fb_factor_nr
)) {
2537 grab_anonymous
= FALSE
;
2539 VM_PAGEOUT_DEBUG(vm_grab_anon_overrides
, 1);
2542 if (grab_anonymous
) {
2543 VM_PAGEOUT_DEBUG(vm_grab_anon_nops
, 1);
2546 #endif /* CONFIG_JETSAM */
2549 if (grab_anonymous
== FALSE
|| anons_grabbed
>= ANONS_GRABBED_LIMIT
|| vm_page_queue_empty(&vm_page_queue_anonymous
)) {
2551 if ( !vm_page_queue_empty(&vm_page_queue_inactive
) ) {
2552 m
= (vm_page_t
) vm_page_queue_first(&vm_page_queue_inactive
);
2554 assert(m
->vmp_q_state
== VM_PAGE_ON_INACTIVE_EXTERNAL_Q
);
2557 if (vm_page_pageable_external_count
< vm_pageout_state
.vm_page_filecache_min
) {
2559 if ( !vm_page_queue_empty(&vm_page_queue_anonymous
) ) {
2560 if ((++reactivated_this_call
% 100)) {
2561 vm_pageout_vminfo
.vm_pageout_filecache_min_reactivated
++;
2562 goto must_activate_page
;
2565 * steal 1% of the file backed pages even if
2566 * we are under the limit that has been set
2567 * for a healthy filecache
2574 if ( !vm_page_queue_empty(&vm_page_queue_anonymous
) ) {
2575 m
= (vm_page_t
) vm_page_queue_first(&vm_page_queue_anonymous
);
2577 assert(m
->vmp_q_state
== VM_PAGE_ON_INACTIVE_INTERNAL_Q
);
2584 * if we've gotten here, we have no victim page.
2585 * check to see if we've not finished balancing the queues
2586 * or we have a page on the aged speculative queue that we
2587 * skipped due to force_anonymous == TRUE.. or we have
2588 * speculative pages that we can prematurely age... if
2589 * one of these cases we'll keep going, else panic
2591 force_anonymous
= FALSE
;
2592 VM_PAGEOUT_DEBUG(vm_pageout_no_victim
, 1);
2594 if (!vm_page_queue_empty(&sq
->age_q
))
2595 goto done_with_inactivepage
;
2597 if (vm_page_speculative_count
) {
2598 force_speculative_aging
= TRUE
;
2599 goto done_with_inactivepage
;
2601 panic("vm_pageout: no victim");
2605 assert(VM_PAGE_PAGEABLE(m
));
2606 m_object
= VM_PAGE_OBJECT(m
);
2607 force_anonymous
= FALSE
;
2609 page_prev_q_state
= m
->vmp_q_state
;
2611 * we just found this page on one of our queues...
2612 * it can't also be on the pageout queue, so safe
2613 * to call vm_page_queues_remove
2615 vm_page_queues_remove(m
, TRUE
);
2617 assert(!m
->vmp_laundry
);
2618 assert(!m
->vmp_private
);
2619 assert(!m
->vmp_fictitious
);
2620 assert(m_object
!= kernel_object
);
2621 assert(VM_PAGE_GET_PHYS_PAGE(m
) != vm_page_guard_addr
);
2623 vm_pageout_vminfo
.vm_pageout_considered_page
++;
2625 DTRACE_VM2(scan
, int, 1, (uint64_t *), NULL
);
2628 * check to see if we currently are working
2629 * with the same object... if so, we've
2630 * already got the lock
2632 if (m_object
!= object
) {
2634 * the object associated with candidate page is
2635 * different from the one we were just working
2636 * with... dump the lock if we still own it
2638 if (object
!= NULL
) {
2639 vm_object_unlock(object
);
2643 * Try to lock object; since we've alread got the
2644 * page queues lock, we can only 'try' for this one.
2645 * if the 'try' fails, we need to do a mutex_pause
2646 * to allow the owner of the object lock a chance to
2647 * run... otherwise, we're likely to trip over this
2648 * object in the same state as we work our way through
2649 * the queue... clumps of pages associated with the same
2650 * object are fairly typical on the inactive and active queues
2652 if (!vm_object_lock_try_scan(m_object
)) {
2653 vm_page_t m_want
= NULL
;
2655 vm_pageout_vminfo
.vm_pageout_inactive_nolock
++;
2657 if (page_prev_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
)
2658 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_nolock
, 1);
2660 pmap_clear_reference(VM_PAGE_GET_PHYS_PAGE(m
));
2662 m
->vmp_reference
= FALSE
;
2664 if ( !m_object
->object_is_shared_cache
) {
2666 * don't apply this optimization if this is the shared cache
2667 * object, it's too easy to get rid of very hot and important
2669 * m->vmp_object must be stable since we hold the page queues lock...
2670 * we can update the scan_collisions field sans the object lock
2671 * since it is a separate field and this is the only spot that does
2672 * a read-modify-write operation and it is never executed concurrently...
2673 * we can asynchronously set this field to 0 when creating a UPL, so it
2674 * is possible for the value to be a bit non-determistic, but that's ok
2675 * since it's only used as a hint
2677 m_object
->scan_collisions
= 1;
2679 if ( !vm_page_queue_empty(&vm_page_queue_cleaned
))
2680 m_want
= (vm_page_t
) vm_page_queue_first(&vm_page_queue_cleaned
);
2681 else if ( !vm_page_queue_empty(&sq
->age_q
))
2682 m_want
= (vm_page_t
) vm_page_queue_first(&sq
->age_q
);
2683 else if ( (grab_anonymous
== FALSE
|| anons_grabbed
>= ANONS_GRABBED_LIMIT
||
2684 vm_page_queue_empty(&vm_page_queue_anonymous
)) &&
2685 !vm_page_queue_empty(&vm_page_queue_inactive
))
2686 m_want
= (vm_page_t
) vm_page_queue_first(&vm_page_queue_inactive
);
2687 else if ( !vm_page_queue_empty(&vm_page_queue_anonymous
))
2688 m_want
= (vm_page_t
) vm_page_queue_first(&vm_page_queue_anonymous
);
2691 * this is the next object we're going to be interested in
2692 * try to make sure its available after the mutex_pause
2696 vm_pageout_scan_wants_object
= VM_PAGE_OBJECT(m_want
);
2701 vm_pageout_scan_wants_object
= VM_OBJECT_NULL
;
2703 assert(m_object
== object
);
2704 assert(VM_PAGE_OBJECT(m
) == m_object
);
2708 * Somebody is already playing with this page.
2709 * Put it back on the appropriate queue
2712 VM_PAGEOUT_DEBUG(vm_pageout_inactive_busy
, 1);
2714 if (page_prev_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
)
2715 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_busy
, 1);
2717 if (page_prev_q_state
== VM_PAGE_ON_SPECULATIVE_Q
)
2718 vm_page_enqueue_inactive(m
, FALSE
);
2720 vm_page_activate(m
);
2721 #if CONFIG_BACKGROUND_QUEUE
2722 #if DEVELOPMENT || DEBUG
2723 if (page_from_bg_q
== TRUE
) {
2724 if (m_object
->internal
)
2725 vm_pageout_rejected_bq_internal
++;
2727 vm_pageout_rejected_bq_external
++;
2731 goto done_with_inactivepage
;
2735 * if (m->vmp_cleaning && !m->vmp_free_when_done)
2736 * If already cleaning this page in place
2737 * just leave if off the paging queues.
2738 * We can leave the page mapped, and upl_commit_range
2739 * will put it on the clean queue.
2741 * if (m->vmp_free_when_done && !m->vmp_cleaning)
2742 * an msync INVALIDATE is in progress...
2743 * this page has been marked for destruction
2744 * after it has been cleaned,
2745 * but not yet gathered into a UPL
2746 * where 'cleaning' will be set...
2747 * just leave it off the paging queues
2749 * if (m->vmp_free_when_done && m->vmp_clenaing)
2750 * an msync INVALIDATE is in progress
2751 * and the UPL has already gathered this page...
2752 * just leave it off the paging queues
2754 if (m
->vmp_free_when_done
|| m
->vmp_cleaning
) {
2755 goto done_with_inactivepage
;
2760 * If it's absent, in error or the object is no longer alive,
2761 * we can reclaim the page... in the no longer alive case,
2762 * there are 2 states the page can be in that preclude us
2763 * from reclaiming it - busy or cleaning - that we've already
2766 if (m
->vmp_absent
|| m
->vmp_error
|| !object
->alive
) {
2769 VM_PAGEOUT_DEBUG(vm_pageout_inactive_absent
, 1);
2770 else if (!object
->alive
)
2771 VM_PAGEOUT_DEBUG(vm_pageout_inactive_notalive
, 1);
2773 VM_PAGEOUT_DEBUG(vm_pageout_inactive_error
, 1);
2775 if (vm_pageout_deadlock_target
) {
2776 VM_PAGEOUT_DEBUG(vm_pageout_scan_inactive_throttle_success
, 1);
2777 vm_pageout_deadlock_target
--;
2780 DTRACE_VM2(dfree
, int, 1, (uint64_t *), NULL
);
2782 if (object
->internal
) {
2783 DTRACE_VM2(anonfree
, int, 1, (uint64_t *), NULL
);
2785 DTRACE_VM2(fsfree
, int, 1, (uint64_t *), NULL
);
2787 assert(!m
->vmp_cleaning
);
2788 assert(!m
->vmp_laundry
);
2790 if (!object
->internal
&&
2791 object
->pager
!= NULL
&&
2792 object
->pager
->mo_pager_ops
== &shared_region_pager_ops
) {
2793 shared_region_pager_reclaimed
++;
2799 * remove page from object here since we're already
2800 * behind the object lock... defer the rest of the work
2801 * we'd normally do in vm_page_free_prepare_object
2802 * until 'vm_page_free_list' is called
2805 vm_page_remove(m
, TRUE
);
2807 assert(m
->vmp_pageq
.next
== 0 && m
->vmp_pageq
.prev
== 0);
2808 m
->vmp_snext
= local_freeq
;
2812 if (page_prev_q_state
== VM_PAGE_ON_SPECULATIVE_Q
)
2813 vm_pageout_vminfo
.vm_pageout_freed_speculative
++;
2814 else if (page_prev_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
)
2815 vm_pageout_vminfo
.vm_pageout_freed_cleaned
++;
2816 else if (page_prev_q_state
== VM_PAGE_ON_INACTIVE_INTERNAL_Q
)
2817 vm_pageout_vminfo
.vm_pageout_freed_internal
++;
2819 vm_pageout_vminfo
.vm_pageout_freed_external
++;
2821 inactive_burst_count
= 0;
2822 goto done_with_inactivepage
;
2824 if (object
->copy
== VM_OBJECT_NULL
) {
2826 * No one else can have any interest in this page.
2827 * If this is an empty purgable object, the page can be
2828 * reclaimed even if dirty.
2829 * If the page belongs to a volatile purgable object, we
2830 * reactivate it if the compressor isn't active.
2832 if (object
->purgable
== VM_PURGABLE_EMPTY
) {
2833 if (m
->vmp_pmapped
== TRUE
) {
2834 /* unmap the page */
2835 refmod_state
= pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
2836 if (refmod_state
& VM_MEM_MODIFIED
) {
2837 SET_PAGE_DIRTY(m
, FALSE
);
2840 if (m
->vmp_dirty
|| m
->vmp_precious
) {
2841 /* we saved the cost of cleaning this page ! */
2842 vm_page_purged_count
++;
2847 if (VM_CONFIG_COMPRESSOR_IS_ACTIVE
) {
2849 * With the VM compressor, the cost of
2850 * reclaiming a page is much lower (no I/O),
2851 * so if we find a "volatile" page, it's better
2852 * to let it get compressed rather than letting
2853 * it occupy a full page until it gets purged.
2854 * So no need to check for "volatile" here.
2856 } else if (object
->purgable
== VM_PURGABLE_VOLATILE
) {
2858 * Avoid cleaning a "volatile" page which might
2862 /* if it's wired, we can't put it on our queue */
2863 assert(!VM_PAGE_WIRED(m
));
2865 /* just stick it back on! */
2866 reactivated_this_call
++;
2868 if (page_prev_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
)
2869 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_volatile_reactivated
, 1);
2871 goto reactivate_page
;
2875 * If it's being used, reactivate.
2876 * (Fictitious pages are either busy or absent.)
2877 * First, update the reference and dirty bits
2878 * to make sure the page is unreferenced.
2882 if (m
->vmp_reference
== FALSE
&& m
->vmp_pmapped
== TRUE
) {
2883 refmod_state
= pmap_get_refmod(VM_PAGE_GET_PHYS_PAGE(m
));
2885 if (refmod_state
& VM_MEM_REFERENCED
)
2886 m
->vmp_reference
= TRUE
;
2887 if (refmod_state
& VM_MEM_MODIFIED
) {
2888 SET_PAGE_DIRTY(m
, FALSE
);
2892 if (m
->vmp_reference
|| m
->vmp_dirty
) {
2893 /* deal with a rogue "reusable" page */
2894 VM_PAGEOUT_SCAN_HANDLE_REUSABLE_PAGE(m
, m_object
);
2896 divisor
= vm_pageout_state
.vm_page_xpmapped_min_divisor
;
2899 vm_pageout_state
.vm_page_xpmapped_min
= 0;
2901 vm_pageout_state
.vm_page_xpmapped_min
= (vm_page_external_count
* 10) / divisor
;
2903 if (!m
->vmp_no_cache
&&
2904 #if CONFIG_BACKGROUND_QUEUE
2905 page_from_bg_q
== FALSE
&&
2907 (m
->vmp_reference
|| (m
->vmp_xpmapped
&& !object
->internal
&&
2908 (vm_page_xpmapped_external_count
< vm_pageout_state
.vm_page_xpmapped_min
)))) {
2910 * The page we pulled off the inactive list has
2911 * been referenced. It is possible for other
2912 * processors to be touching pages faster than we
2913 * can clear the referenced bit and traverse the
2914 * inactive queue, so we limit the number of
2917 if (++reactivated_this_call
>= reactivate_limit
) {
2918 vm_pageout_vminfo
.vm_pageout_reactivation_limit_exceeded
++;
2919 } else if (++inactive_reclaim_run
>= VM_PAGEOUT_INACTIVE_FORCE_RECLAIM
) {
2920 vm_pageout_vminfo
.vm_pageout_inactive_force_reclaim
++;
2924 if (page_prev_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
)
2925 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_reference_reactivated
, 1);
2927 vm_pageout_vminfo
.vm_pageout_inactive_referenced
++;
2929 if ( !object
->internal
&& object
->pager
!= MEMORY_OBJECT_NULL
&&
2930 vnode_pager_get_isinuse(object
->pager
, &isinuse
) == KERN_SUCCESS
&& !isinuse
) {
2932 * no explict mappings of this object exist
2933 * and it's not open via the filesystem
2935 vm_page_deactivate(m
);
2936 VM_PAGEOUT_DEBUG(vm_pageout_inactive_deactivated
, 1);
2940 * The page was/is being used, so put back on active list.
2942 vm_page_activate(m
);
2943 VM_STAT_INCR(reactivations
);
2944 inactive_burst_count
= 0;
2946 #if CONFIG_BACKGROUND_QUEUE
2947 #if DEVELOPMENT || DEBUG
2948 if (page_from_bg_q
== TRUE
) {
2949 if (m_object
->internal
)
2950 vm_pageout_rejected_bq_internal
++;
2952 vm_pageout_rejected_bq_external
++;
2956 if (page_prev_q_state
== VM_PAGE_ON_INACTIVE_CLEANED_Q
)
2957 VM_PAGEOUT_DEBUG(vm_pageout_cleaned_reactivated
, 1);
2958 vm_pageout_state
.vm_pageout_inactive_used
++;
2960 goto done_with_inactivepage
;
2963 * Make sure we call pmap_get_refmod() if it
2964 * wasn't already called just above, to update
2967 if ((refmod_state
== -1) && !m
->vmp_dirty
&& m
->vmp_pmapped
) {
2968 refmod_state
= pmap_get_refmod(VM_PAGE_GET_PHYS_PAGE(m
));
2969 if (refmod_state
& VM_MEM_MODIFIED
) {
2970 SET_PAGE_DIRTY(m
, FALSE
);
2976 "vm_pageout_scan, replace object 0x%X offset 0x%X page 0x%X\n",
2977 object
, m
->vmp_offset
, m
, 0,0);
2980 * we've got a candidate page to steal...
2982 * m->vmp_dirty is up to date courtesy of the
2983 * preceding check for m->vmp_reference... if
2984 * we get here, then m->vmp_reference had to be
2985 * FALSE (or possibly "reactivate_limit" was
2986 * exceeded), but in either case we called
2987 * pmap_get_refmod() and updated both
2988 * m->vmp_reference and m->vmp_dirty
2990 * if it's dirty or precious we need to
2991 * see if the target queue is throtttled
2992 * it if is, we need to skip over it by moving it back
2993 * to the end of the inactive queue
2996 inactive_throttled
= FALSE
;
2998 if (m
->vmp_dirty
|| m
->vmp_precious
) {
2999 if (object
->internal
) {
3000 if (VM_PAGE_Q_THROTTLED(iq
))
3001 inactive_throttled
= TRUE
;
3002 } else if (VM_PAGE_Q_THROTTLED(eq
)) {
3003 inactive_throttled
= TRUE
;
3007 if (!VM_DYNAMIC_PAGING_ENABLED() &&
3008 object
->internal
&& m
->vmp_dirty
&&
3009 (object
->purgable
== VM_PURGABLE_DENY
||
3010 object
->purgable
== VM_PURGABLE_NONVOLATILE
||
3011 object
->purgable
== VM_PURGABLE_VOLATILE
)) {
3012 vm_page_check_pageable_safe(m
);
3013 assert(m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
);
3014 vm_page_queue_enter(&vm_page_queue_throttled
, m
,
3015 vm_page_t
, vmp_pageq
);
3016 m
->vmp_q_state
= VM_PAGE_ON_THROTTLED_Q
;
3017 vm_page_throttled_count
++;
3019 VM_PAGEOUT_DEBUG(vm_pageout_scan_reclaimed_throttled
, 1);
3021 inactive_burst_count
= 0;
3022 goto done_with_inactivepage
;
3024 if (inactive_throttled
== TRUE
) {
3026 if (object
->internal
== FALSE
) {
3028 * we need to break up the following potential deadlock case...
3029 * a) The external pageout thread is stuck on the truncate lock for a file that is being extended i.e. written.
3030 * b) The thread doing the writing is waiting for pages while holding the truncate lock
3031 * c) Most of the pages in the inactive queue belong to this file.
3033 * we are potentially in this deadlock because...
3034 * a) the external pageout queue is throttled
3035 * b) we're done with the active queue and moved on to the inactive queue
3036 * c) we've got a dirty external page
3038 * since we don't know the reason for the external pageout queue being throttled we
3039 * must suspect that we are deadlocked, so move the current page onto the active queue
3040 * in an effort to cause a page from the active queue to 'age' to the inactive queue
3042 * if we don't have jetsam configured (i.e. we have a dynamic pager), set
3043 * 'force_anonymous' to TRUE to cause us to grab a page from the cleaned/anonymous
3044 * pool the next time we select a victim page... if we can make enough new free pages,
3045 * the deadlock will break, the external pageout queue will empty and it will no longer
3048 * if we have jetsam configured, keep a count of the pages reactivated this way so
3049 * that we can try to find clean pages in the active/inactive queues before
3050 * deciding to jetsam a process
3052 vm_pageout_vminfo
.vm_pageout_scan_inactive_throttled_external
++;
3054 vm_page_check_pageable_safe(m
);
3055 assert(m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
);
3056 vm_page_queue_enter(&vm_page_queue_active
, m
, vm_page_t
, vmp_pageq
);
3057 m
->vmp_q_state
= VM_PAGE_ON_ACTIVE_Q
;
3058 vm_page_active_count
++;
3059 vm_page_pageable_external_count
++;
3061 vm_pageout_adjust_eq_iothrottle(eq
, FALSE
);
3063 #if CONFIG_MEMORYSTATUS && CONFIG_JETSAM
3064 vm_pageout_inactive_external_forced_reactivate_limit
--;
3066 if (vm_pageout_inactive_external_forced_reactivate_limit
<= 0) {
3067 vm_pageout_inactive_external_forced_reactivate_limit
= vm_page_active_count
+ vm_page_inactive_count
;
3069 * Possible deadlock scenario so request jetsam action
3072 vm_object_unlock(object
);
3073 object
= VM_OBJECT_NULL
;
3074 vm_page_unlock_queues();
3076 VM_DEBUG_CONSTANT_EVENT(vm_pageout_jetsam
, VM_PAGEOUT_JETSAM
, DBG_FUNC_START
,
3077 vm_page_active_count
, vm_page_inactive_count
, vm_page_free_count
, vm_page_free_count
);
3079 /* Kill first suitable process. If this call returned FALSE, we might have simply purged a process instead. */
3080 if (memorystatus_kill_on_VM_page_shortage(FALSE
) == TRUE
) {
3081 VM_PAGEOUT_DEBUG(vm_pageout_inactive_external_forced_jetsam_count
, 1);
3084 VM_DEBUG_CONSTANT_EVENT(vm_pageout_jetsam
, VM_PAGEOUT_JETSAM
, DBG_FUNC_END
,
3085 vm_page_active_count
, vm_page_inactive_count
, vm_page_free_count
, vm_page_free_count
);
3087 vm_page_lock_queues();
3090 #else /* CONFIG_MEMORYSTATUS && CONFIG_JETSAM */
3091 force_anonymous
= TRUE
;
3093 inactive_burst_count
= 0;
3094 goto done_with_inactivepage
;
3096 goto must_activate_page
;
3101 * we've got a page that we can steal...
3102 * eliminate all mappings and make sure
3103 * we have the up-to-date modified state
3105 * if we need to do a pmap_disconnect then we
3106 * need to re-evaluate m->vmp_dirty since the pmap_disconnect
3107 * provides the true state atomically... the
3108 * page was still mapped up to the pmap_disconnect
3109 * and may have been dirtied at the last microsecond
3111 * Note that if 'pmapped' is FALSE then the page is not
3112 * and has not been in any map, so there is no point calling
3113 * pmap_disconnect(). m->vmp_dirty could have been set in anticipation
3114 * of likely usage of the page.
3116 if (m
->vmp_pmapped
== TRUE
) {
3120 * Don't count this page as going into the compressor
3121 * if any of these are true:
3122 * 1) compressed pager isn't enabled
3123 * 2) Freezer enabled device with compressed pager
3124 * backend (exclusive use) i.e. most of the VM system
3125 * (including vm_pageout_scan) has no knowledge of
3127 * 3) This page belongs to a file and hence will not be
3128 * sent into the compressor
3130 if ( !VM_CONFIG_COMPRESSOR_IS_ACTIVE
||
3131 object
->internal
== FALSE
) {
3133 } else if (m
->vmp_dirty
|| m
->vmp_precious
) {
3135 * VM knows that this page is dirty (or
3136 * precious) and needs to be compressed
3137 * rather than freed.
3138 * Tell the pmap layer to count this page
3141 pmap_options
= PMAP_OPTIONS_COMPRESSOR
;
3144 * VM does not know if the page needs to
3145 * be preserved but the pmap layer might tell
3146 * us if any mapping has "modified" it.
3147 * Let's the pmap layer to count this page
3148 * as compressed if and only if it has been
3152 PMAP_OPTIONS_COMPRESSOR_IFF_MODIFIED
;
3154 refmod_state
= pmap_disconnect_options(VM_PAGE_GET_PHYS_PAGE(m
),
3157 if (refmod_state
& VM_MEM_MODIFIED
) {
3158 SET_PAGE_DIRTY(m
, FALSE
);
3163 * reset our count of pages that have been reclaimed
3164 * since the last page was 'stolen'
3166 inactive_reclaim_run
= 0;
3169 * If it's clean and not precious, we can free the page.
3171 if (!m
->vmp_dirty
&& !m
->vmp_precious
) {
3173 vm_pageout_state
.vm_pageout_inactive_clean
++;
3176 * OK, at this point we have found a page we are going to free.
3178 #if CONFIG_PHANTOM_CACHE
3179 if (!object
->internal
)
3180 vm_phantom_cache_add_ghost(m
);
3186 * The page may have been dirtied since the last check
3187 * for a throttled target queue (which may have been skipped
3188 * if the page was clean then). With the dirty page
3189 * disconnected here, we can make one final check.
3191 if (object
->internal
) {
3192 if (VM_PAGE_Q_THROTTLED(iq
))
3193 inactive_throttled
= TRUE
;
3194 } else if (VM_PAGE_Q_THROTTLED(eq
)) {
3195 inactive_throttled
= TRUE
;
3198 if (inactive_throttled
== TRUE
)
3199 goto throttle_inactive
;
3201 #if VM_PRESSURE_EVENTS
3205 * If Jetsam is enabled, then the sending
3206 * of memory pressure notifications is handled
3207 * from the same thread that takes care of high-water
3208 * and other jetsams i.e. the memorystatus_thread.
3211 #else /* CONFIG_JETSAM */
3213 vm_pressure_response();
3215 #endif /* CONFIG_JETSAM */
3216 #endif /* VM_PRESSURE_EVENTS */
3218 if (page_prev_q_state
== VM_PAGE_ON_SPECULATIVE_Q
)
3219 VM_PAGEOUT_DEBUG(vm_pageout_speculative_dirty
, 1);
3221 if (object
->internal
)
3222 vm_pageout_vminfo
.vm_pageout_inactive_dirty_internal
++;
3224 vm_pageout_vminfo
.vm_pageout_inactive_dirty_external
++;
3227 * internal pages will go to the compressor...
3228 * external pages will go to the appropriate pager to be cleaned
3229 * and upon completion will end up on 'vm_page_queue_cleaned' which
3230 * is a preferred queue to steal from
3232 vm_pageout_cluster(m
);
3233 inactive_burst_count
= 0;
3235 done_with_inactivepage
:
3237 if (delayed_unlock
++ > delayed_unlock_limit
) {
3238 int freed
= local_freed
;
3240 vm_pageout_prepare_to_block(&object
, &delayed_unlock
, &local_freeq
, &local_freed
,
3241 VM_PAGEOUT_PB_CONSIDER_WAKING_COMPACTOR_SWAPPER
);
3243 lck_mtx_yield(&vm_page_queue_lock
);
3244 } else if (vm_pageout_scan_wants_object
) {
3245 vm_page_unlock_queues();
3247 vm_page_lock_queues();
3250 * back to top of pageout scan loop
3257 vm_page_free_reserve(
3260 int free_after_reserve
;
3262 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
) {
3264 if ((vm_page_free_reserved
+ pages
+ COMPRESSOR_FREE_RESERVED_LIMIT
) >= (VM_PAGE_FREE_RESERVED_LIMIT
+ COMPRESSOR_FREE_RESERVED_LIMIT
))
3265 vm_page_free_reserved
= VM_PAGE_FREE_RESERVED_LIMIT
+ COMPRESSOR_FREE_RESERVED_LIMIT
;
3267 vm_page_free_reserved
+= (pages
+ COMPRESSOR_FREE_RESERVED_LIMIT
);
3270 if ((vm_page_free_reserved
+ pages
) >= VM_PAGE_FREE_RESERVED_LIMIT
)
3271 vm_page_free_reserved
= VM_PAGE_FREE_RESERVED_LIMIT
;
3273 vm_page_free_reserved
+= pages
;
3275 free_after_reserve
= vm_pageout_state
.vm_page_free_count_init
- vm_page_free_reserved
;
3277 vm_page_free_min
= vm_page_free_reserved
+
3278 VM_PAGE_FREE_MIN(free_after_reserve
);
3280 if (vm_page_free_min
> VM_PAGE_FREE_MIN_LIMIT
)
3281 vm_page_free_min
= VM_PAGE_FREE_MIN_LIMIT
;
3283 vm_page_free_target
= vm_page_free_reserved
+
3284 VM_PAGE_FREE_TARGET(free_after_reserve
);
3286 if (vm_page_free_target
> VM_PAGE_FREE_TARGET_LIMIT
)
3287 vm_page_free_target
= VM_PAGE_FREE_TARGET_LIMIT
;
3289 if (vm_page_free_target
< vm_page_free_min
+ 5)
3290 vm_page_free_target
= vm_page_free_min
+ 5;
3292 vm_page_throttle_limit
= vm_page_free_target
- (vm_page_free_target
/ 2);
3296 * vm_pageout is the high level pageout daemon.
3300 vm_pageout_continue(void)
3302 DTRACE_VM2(pgrrun
, int, 1, (uint64_t *), NULL
);
3303 VM_PAGEOUT_DEBUG(vm_pageout_scan_event_counter
, 1);
3305 #if !CONFIG_EMBEDDED
3306 lck_mtx_lock(&vm_page_queue_free_lock
);
3307 vm_pageout_running
= TRUE
;
3308 lck_mtx_unlock(&vm_page_queue_free_lock
);
3309 #endif /* CONFIG_EMBEDDED */
3313 * we hold both the vm_page_queue_free_lock
3314 * and the vm_page_queues_lock at this point
3316 assert(vm_page_free_wanted
== 0);
3317 assert(vm_page_free_wanted_privileged
== 0);
3318 assert_wait((event_t
) &vm_page_free_wanted
, THREAD_UNINT
);
3320 #if !CONFIG_EMBEDDED
3321 vm_pageout_running
= FALSE
;
3322 if (vm_pageout_waiter
) {
3323 vm_pageout_waiter
= FALSE
;
3324 thread_wakeup((event_t
)&vm_pageout_waiter
);
3326 #endif /* !CONFIG_EMBEDDED */
3328 lck_mtx_unlock(&vm_page_queue_free_lock
);
3329 vm_page_unlock_queues();
3331 counter(c_vm_pageout_block
++);
3332 thread_block((thread_continue_t
)vm_pageout_continue
);
3336 #if !CONFIG_EMBEDDED
3338 vm_pageout_wait(uint64_t deadline
)
3342 lck_mtx_lock(&vm_page_queue_free_lock
);
3343 for (kr
= KERN_SUCCESS
; vm_pageout_running
&& (KERN_SUCCESS
== kr
); ) {
3344 vm_pageout_waiter
= TRUE
;
3345 if (THREAD_AWAKENED
!= lck_mtx_sleep_deadline(
3346 &vm_page_queue_free_lock
, LCK_SLEEP_DEFAULT
,
3347 (event_t
) &vm_pageout_waiter
, THREAD_UNINT
, deadline
)) {
3348 kr
= KERN_OPERATION_TIMED_OUT
;
3351 lck_mtx_unlock(&vm_page_queue_free_lock
);
3355 #endif /* !CONFIG_EMBEDDED */
3359 vm_pageout_iothread_external_continue(struct vm_pageout_queue
*q
)
3363 vm_object_offset_t offset
;
3364 memory_object_t pager
;
3366 /* On systems with a compressor, the external IO thread clears its
3367 * VM privileged bit to accommodate large allocations (e.g. bulk UPL
3370 if (vm_pageout_state
.vm_pageout_internal_iothread
!= THREAD_NULL
)
3371 current_thread()->options
&= ~TH_OPT_VMPRIV
;
3373 vm_page_lockspin_queues();
3375 while ( !vm_page_queue_empty(&q
->pgo_pending
) ) {
3378 vm_page_queue_remove_first(&q
->pgo_pending
, m
, vm_page_t
, vmp_pageq
);
3380 assert(m
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
);
3383 * grab a snapshot of the object and offset this
3384 * page is tabled in so that we can relookup this
3385 * page after we've taken the object lock - these
3386 * fields are stable while we hold the page queues lock
3387 * but as soon as we drop it, there is nothing to keep
3388 * this page in this object... we hold an activity_in_progress
3389 * on this object which will keep it from terminating
3391 object
= VM_PAGE_OBJECT(m
);
3392 offset
= m
->vmp_offset
;
3394 m
->vmp_q_state
= VM_PAGE_NOT_ON_Q
;
3395 VM_PAGE_ZERO_PAGEQ_ENTRY(m
);
3397 vm_page_unlock_queues();
3399 vm_object_lock(object
);
3401 m
= vm_page_lookup(object
, offset
);
3404 m
->vmp_busy
|| m
->vmp_cleaning
|| !m
->vmp_laundry
|| (m
->vmp_q_state
!= VM_PAGE_NOT_ON_Q
)) {
3406 * it's either the same page that someone else has
3407 * started cleaning (or it's finished cleaning or
3408 * been put back on the pageout queue), or
3409 * the page has been freed or we have found a
3410 * new page at this offset... in all of these cases
3411 * we merely need to release the activity_in_progress
3412 * we took when we put the page on the pageout queue
3414 vm_object_activity_end(object
);
3415 vm_object_unlock(object
);
3417 vm_page_lockspin_queues();
3420 pager
= object
->pager
;
3422 if (pager
== MEMORY_OBJECT_NULL
) {
3424 * This pager has been destroyed by either
3425 * memory_object_destroy or vm_object_destroy, and
3426 * so there is nowhere for the page to go.
3428 if (m
->vmp_free_when_done
) {
3430 * Just free the page... VM_PAGE_FREE takes
3431 * care of cleaning up all the state...
3432 * including doing the vm_pageout_throttle_up
3436 vm_page_lockspin_queues();
3438 vm_pageout_throttle_up(m
);
3439 vm_page_activate(m
);
3441 vm_page_unlock_queues();
3444 * And we are done with it.
3447 vm_object_activity_end(object
);
3448 vm_object_unlock(object
);
3450 vm_page_lockspin_queues();
3455 * we don't hold the page queue lock
3456 * so this check isn't safe to make
3461 * give back the activity_in_progress reference we
3462 * took when we queued up this page and replace it
3463 * it with a paging_in_progress reference that will
3464 * also hold the paging offset from changing and
3465 * prevent the object from terminating
3467 vm_object_activity_end(object
);
3468 vm_object_paging_begin(object
);
3469 vm_object_unlock(object
);
3472 * Send the data to the pager.
3473 * any pageout clustering happens there
3475 memory_object_data_return(pager
,
3476 m
->vmp_offset
+ object
->paging_offset
,
3484 vm_object_lock(object
);
3485 vm_object_paging_end(object
);
3486 vm_object_unlock(object
);
3488 vm_pageout_io_throttle();
3490 vm_page_lockspin_queues();
3492 q
->pgo_busy
= FALSE
;
3495 assert_wait((event_t
) &q
->pgo_pending
, THREAD_UNINT
);
3496 vm_page_unlock_queues();
3498 thread_block_parameter((thread_continue_t
)vm_pageout_iothread_external_continue
, (void *) q
);
3503 #define MAX_FREE_BATCH 32
3504 uint32_t vm_compressor_time_thread
; /* Set via sysctl to record time accrued by
3510 vm_pageout_iothread_internal_continue(struct cq
*);
3512 vm_pageout_iothread_internal_continue(struct cq
*cq
)
3514 struct vm_pageout_queue
*q
;
3516 boolean_t pgo_draining
;
3519 vm_page_t local_freeq
= NULL
;
3520 int local_freed
= 0;
3521 int local_batch_size
;
3522 #if DEVELOPMENT || DEBUG
3524 boolean_t marked_active
= FALSE
;
3526 KERNEL_DEBUG(0xe040000c | DBG_FUNC_END
, 0, 0, 0, 0, 0);
3529 local_batch_size
= q
->pgo_maxlaundry
/ (vm_pageout_state
.vm_compressor_thread_count
* 2);
3531 #if RECORD_THE_COMPRESSED_DATA
3533 c_compressed_record_init();
3536 int pages_left_on_q
= 0;
3541 KERNEL_DEBUG(0xe0400014 | DBG_FUNC_START
, 0, 0, 0, 0, 0);
3543 vm_page_lock_queues();
3544 #if DEVELOPMENT || DEBUG
3545 if (marked_active
== FALSE
) {
3547 vmct_state
[cq
->id
] = VMCT_ACTIVE
;
3548 marked_active
= TRUE
;
3549 if (vmct_active
== 1) {
3550 vm_compressor_epoch_start
= mach_absolute_time();
3554 KERNEL_DEBUG(0xe0400014 | DBG_FUNC_END
, 0, 0, 0, 0, 0);
3556 KERNEL_DEBUG(0xe0400018 | DBG_FUNC_START
, q
->pgo_laundry
, 0, 0, 0, 0);
3558 while ( !vm_page_queue_empty(&q
->pgo_pending
) && local_cnt
< local_batch_size
) {
3560 vm_page_queue_remove_first(&q
->pgo_pending
, m
, vm_page_t
, vmp_pageq
);
3561 assert(m
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
);
3564 m
->vmp_q_state
= VM_PAGE_NOT_ON_Q
;
3565 VM_PAGE_ZERO_PAGEQ_ENTRY(m
);
3566 m
->vmp_laundry
= FALSE
;
3568 m
->vmp_snext
= local_q
;
3572 if (local_q
== NULL
)
3577 if ((pgo_draining
= q
->pgo_draining
) == FALSE
) {
3578 vm_pageout_throttle_up_batch(q
, local_cnt
);
3579 pages_left_on_q
= q
->pgo_laundry
;
3581 pages_left_on_q
= q
->pgo_laundry
- local_cnt
;
3583 vm_page_unlock_queues();
3585 #if !RECORD_THE_COMPRESSED_DATA
3586 if (pages_left_on_q
>= local_batch_size
&& cq
->id
< (vm_pageout_state
.vm_compressor_thread_count
- 1)) {
3587 thread_wakeup((event_t
) ((uintptr_t)&q
->pgo_pending
+ cq
->id
+ 1));
3590 KERNEL_DEBUG(0xe0400018 | DBG_FUNC_END
, q
->pgo_laundry
, 0, 0, 0, 0);
3594 KERNEL_DEBUG(0xe0400024 | DBG_FUNC_START
, local_cnt
, 0, 0, 0, 0);
3597 local_q
= m
->vmp_snext
;
3598 m
->vmp_snext
= NULL
;
3600 if (vm_pageout_compress_page(&cq
->current_chead
, cq
->scratch_buf
, m
) == KERN_SUCCESS
) {
3601 #if DEVELOPMENT || DEBUG
3604 KERNEL_DEBUG(0xe0400024 | DBG_FUNC_END
, local_cnt
, 0, 0, 0, 0);
3606 m
->vmp_snext
= local_freeq
;
3610 if (local_freed
>= MAX_FREE_BATCH
) {
3612 OSAddAtomic64(local_freed
, &vm_pageout_vminfo
.vm_pageout_compressions
);
3614 vm_page_free_list(local_freeq
, TRUE
);
3621 while (vm_page_free_count
< COMPRESSOR_FREE_RESERVED_LIMIT
) {
3622 kern_return_t wait_result
;
3623 int need_wakeup
= 0;
3626 OSAddAtomic64(local_freed
, &vm_pageout_vminfo
.vm_pageout_compressions
);
3628 vm_page_free_list(local_freeq
, TRUE
);
3634 lck_mtx_lock_spin(&vm_page_queue_free_lock
);
3636 if (vm_page_free_count
< COMPRESSOR_FREE_RESERVED_LIMIT
) {
3638 if (vm_page_free_wanted_privileged
++ == 0)
3640 wait_result
= assert_wait((event_t
)&vm_page_free_wanted_privileged
, THREAD_UNINT
);
3642 lck_mtx_unlock(&vm_page_queue_free_lock
);
3645 thread_wakeup((event_t
)&vm_page_free_wanted
);
3647 if (wait_result
== THREAD_WAITING
)
3649 thread_block(THREAD_CONTINUE_NULL
);
3651 lck_mtx_unlock(&vm_page_queue_free_lock
);
3656 OSAddAtomic64(local_freed
, &vm_pageout_vminfo
.vm_pageout_compressions
);
3658 vm_page_free_list(local_freeq
, TRUE
);
3662 if (pgo_draining
== TRUE
) {
3663 vm_page_lockspin_queues();
3664 vm_pageout_throttle_up_batch(q
, local_cnt
);
3665 vm_page_unlock_queues();
3668 KERNEL_DEBUG(0xe040000c | DBG_FUNC_START
, 0, 0, 0, 0, 0);
3671 * queue lock is held and our q is empty
3673 q
->pgo_busy
= FALSE
;
3676 assert_wait((event_t
) ((uintptr_t)&q
->pgo_pending
+ cq
->id
), THREAD_UNINT
);
3677 #if DEVELOPMENT || DEBUG
3678 if (marked_active
== TRUE
) {
3680 vmct_state
[cq
->id
] = VMCT_IDLE
;
3682 if (vmct_active
== 0) {
3683 vm_compressor_epoch_stop
= mach_absolute_time();
3684 assertf(vm_compressor_epoch_stop
>= vm_compressor_epoch_start
,
3685 "Compressor epoch non-monotonic: 0x%llx -> 0x%llx",
3686 vm_compressor_epoch_start
, vm_compressor_epoch_stop
);
3687 /* This interval includes intervals where one or more
3688 * compressor threads were pre-empted
3690 vmct_stats
.vmct_cthreads_total
+= vm_compressor_epoch_stop
- vm_compressor_epoch_start
;
3694 vm_page_unlock_queues();
3695 #if DEVELOPMENT || DEBUG
3696 if (__improbable(vm_compressor_time_thread
)) {
3697 vmct_stats
.vmct_runtimes
[cq
->id
] = thread_get_runtime_self();
3698 vmct_stats
.vmct_pages
[cq
->id
] += ncomps
;
3699 vmct_stats
.vmct_iterations
[cq
->id
]++;
3700 if (ncomps
> vmct_stats
.vmct_maxpages
[cq
->id
]) {
3701 vmct_stats
.vmct_maxpages
[cq
->id
] = ncomps
;
3703 if (ncomps
< vmct_stats
.vmct_minpages
[cq
->id
]) {
3704 vmct_stats
.vmct_minpages
[cq
->id
] = ncomps
;
3709 KERNEL_DEBUG(0xe0400018 | DBG_FUNC_END
, 0, 0, 0, 0, 0);
3711 thread_block_parameter((thread_continue_t
)vm_pageout_iothread_internal_continue
, (void *) cq
);
3717 vm_pageout_compress_page(void **current_chead
, char *scratch_buf
, vm_page_t m
)
3720 memory_object_t pager
;
3721 int compressed_count_delta
;
3722 kern_return_t retval
;
3724 object
= VM_PAGE_OBJECT(m
);
3726 assert(!m
->vmp_free_when_done
);
3727 assert(!m
->vmp_laundry
);
3729 pager
= object
->pager
;
3731 if (!object
->pager_initialized
|| pager
== MEMORY_OBJECT_NULL
) {
3733 KERNEL_DEBUG(0xe0400010 | DBG_FUNC_START
, object
, pager
, 0, 0, 0);
3735 vm_object_lock(object
);
3738 * If there is no memory object for the page, create
3739 * one and hand it to the compression pager.
3742 if (!object
->pager_initialized
)
3743 vm_object_collapse(object
, (vm_object_offset_t
) 0, TRUE
);
3744 if (!object
->pager_initialized
)
3745 vm_object_compressor_pager_create(object
);
3747 pager
= object
->pager
;
3749 if (!object
->pager_initialized
|| pager
== MEMORY_OBJECT_NULL
) {
3751 * Still no pager for the object,
3752 * or the pager has been destroyed.
3753 * Reactivate the page.
3755 * Should only happen if there is no
3758 PAGE_WAKEUP_DONE(m
);
3760 vm_page_lockspin_queues();
3761 vm_page_activate(m
);
3762 VM_PAGEOUT_DEBUG(vm_pageout_dirty_no_pager
, 1);
3763 vm_page_unlock_queues();
3766 * And we are done with it.
3768 vm_object_activity_end(object
);
3769 vm_object_unlock(object
);
3771 return KERN_FAILURE
;
3773 vm_object_unlock(object
);
3775 KERNEL_DEBUG(0xe0400010 | DBG_FUNC_END
, object
, pager
, 0, 0, 0);
3777 assert(object
->pager_initialized
&& pager
!= MEMORY_OBJECT_NULL
);
3778 assert(object
->activity_in_progress
> 0);
3780 retval
= vm_compressor_pager_put(
3782 m
->vmp_offset
+ object
->paging_offset
,
3783 VM_PAGE_GET_PHYS_PAGE(m
),
3786 &compressed_count_delta
);
3788 vm_object_lock(object
);
3790 assert(object
->activity_in_progress
> 0);
3791 assert(VM_PAGE_OBJECT(m
) == object
);
3792 assert( !VM_PAGE_WIRED(m
));
3794 vm_compressor_pager_count(pager
,
3795 compressed_count_delta
,
3796 FALSE
, /* shared_lock */
3799 if (retval
== KERN_SUCCESS
) {
3801 * If the object is purgeable, its owner's
3802 * purgeable ledgers will be updated in
3803 * vm_page_remove() but the page still
3804 * contributes to the owner's memory footprint,
3805 * so account for it as such.
3807 if ((object
->purgable
!= VM_PURGABLE_DENY
||
3808 object
->vo_ledger_tag
) &&
3809 object
->vo_owner
!= NULL
) {
3810 /* one more compressed purgeable/tagged page */
3811 vm_object_owner_compressed_update(object
,
3814 VM_STAT_INCR(compressions
);
3817 vm_page_remove(m
, TRUE
);
3820 PAGE_WAKEUP_DONE(m
);
3822 vm_page_lockspin_queues();
3824 vm_page_activate(m
);
3825 vm_pageout_vminfo
.vm_compressor_failed
++;
3827 vm_page_unlock_queues();
3829 vm_object_activity_end(object
);
3830 vm_object_unlock(object
);
3837 vm_pageout_adjust_eq_iothrottle(struct vm_pageout_queue
*eq
, boolean_t req_lowpriority
)
3841 if (hibernate_cleaning_in_progress
== TRUE
)
3842 req_lowpriority
= FALSE
;
3844 if (eq
->pgo_inited
== TRUE
&& eq
->pgo_lowpriority
!= req_lowpriority
) {
3846 vm_page_unlock_queues();
3848 if (req_lowpriority
== TRUE
) {
3849 policy
= THROTTLE_LEVEL_PAGEOUT_THROTTLED
;
3850 DTRACE_VM(laundrythrottle
);
3852 policy
= THROTTLE_LEVEL_PAGEOUT_UNTHROTTLED
;
3853 DTRACE_VM(laundryunthrottle
);
3855 proc_set_thread_policy_with_tid(kernel_task
, eq
->pgo_tid
,
3856 TASK_POLICY_EXTERNAL
, TASK_POLICY_IO
, policy
);
3858 eq
->pgo_lowpriority
= req_lowpriority
;
3860 vm_page_lock_queues();
3866 vm_pageout_iothread_external(void)
3868 thread_t self
= current_thread();
3870 self
->options
|= TH_OPT_VMPRIV
;
3872 DTRACE_VM2(laundrythrottle
, int, 1, (uint64_t *), NULL
);
3874 proc_set_thread_policy(self
, TASK_POLICY_EXTERNAL
,
3875 TASK_POLICY_IO
, THROTTLE_LEVEL_PAGEOUT_THROTTLED
);
3877 vm_page_lock_queues();
3879 vm_pageout_queue_external
.pgo_tid
= self
->thread_id
;
3880 vm_pageout_queue_external
.pgo_lowpriority
= TRUE
;
3881 vm_pageout_queue_external
.pgo_inited
= TRUE
;
3883 vm_page_unlock_queues();
3885 vm_pageout_iothread_external_continue(&vm_pageout_queue_external
);
3892 vm_pageout_iothread_internal(struct cq
*cq
)
3894 thread_t self
= current_thread();
3896 self
->options
|= TH_OPT_VMPRIV
;
3898 vm_page_lock_queues();
3900 vm_pageout_queue_internal
.pgo_tid
= self
->thread_id
;
3901 vm_pageout_queue_internal
.pgo_lowpriority
= TRUE
;
3902 vm_pageout_queue_internal
.pgo_inited
= TRUE
;
3904 vm_page_unlock_queues();
3906 if (vm_pageout_state
.vm_restricted_to_single_processor
== TRUE
)
3907 thread_vm_bind_group_add();
3910 thread_set_thread_name(current_thread(), "VM_compressor");
3911 #if DEVELOPMENT || DEBUG
3912 vmct_stats
.vmct_minpages
[cq
->id
] = INT32_MAX
;
3914 vm_pageout_iothread_internal_continue(cq
);
3920 vm_set_buffer_cleanup_callout(boolean_t (*func
)(int))
3922 if (OSCompareAndSwapPtr(NULL
, func
, (void * volatile *) &consider_buffer_cache_collect
)) {
3923 return KERN_SUCCESS
;
3925 return KERN_FAILURE
; /* Already set */
3929 extern boolean_t memorystatus_manual_testing_on
;
3930 extern unsigned int memorystatus_level
;
3933 #if VM_PRESSURE_EVENTS
3935 boolean_t vm_pressure_events_enabled
= FALSE
;
3938 vm_pressure_response(void)
3941 vm_pressure_level_t old_level
= kVMPressureNormal
;
3943 unsigned int total_pages
;
3944 uint64_t available_memory
= 0;
3946 if (vm_pressure_events_enabled
== FALSE
)
3951 available_memory
= (uint64_t) memorystatus_available_pages
;
3953 #else /* CONFIG_EMBEDDED */
3955 available_memory
= (uint64_t) AVAILABLE_NON_COMPRESSED_MEMORY
;
3956 memorystatus_available_pages
= (uint64_t) AVAILABLE_NON_COMPRESSED_MEMORY
;
3958 #endif /* CONFIG_EMBEDDED */
3960 total_pages
= (unsigned int) atop_64(max_mem
);
3961 #if CONFIG_SECLUDED_MEMORY
3962 total_pages
-= vm_page_secluded_count
;
3963 #endif /* CONFIG_SECLUDED_MEMORY */
3964 memorystatus_level
= (unsigned int) ((available_memory
* 100) / total_pages
);
3966 if (memorystatus_manual_testing_on
) {
3970 old_level
= memorystatus_vm_pressure_level
;
3972 switch (memorystatus_vm_pressure_level
) {
3974 case kVMPressureNormal
:
3976 if (VM_PRESSURE_WARNING_TO_CRITICAL()) {
3977 new_level
= kVMPressureCritical
;
3978 } else if (VM_PRESSURE_NORMAL_TO_WARNING()) {
3979 new_level
= kVMPressureWarning
;
3984 case kVMPressureWarning
:
3985 case kVMPressureUrgent
:
3987 if (VM_PRESSURE_WARNING_TO_NORMAL()) {
3988 new_level
= kVMPressureNormal
;
3989 } else if (VM_PRESSURE_WARNING_TO_CRITICAL()) {
3990 new_level
= kVMPressureCritical
;
3995 case kVMPressureCritical
:
3997 if (VM_PRESSURE_WARNING_TO_NORMAL()) {
3998 new_level
= kVMPressureNormal
;
3999 } else if (VM_PRESSURE_CRITICAL_TO_WARNING()) {
4000 new_level
= kVMPressureWarning
;
4009 if (new_level
!= -1) {
4010 memorystatus_vm_pressure_level
= (vm_pressure_level_t
) new_level
;
4012 if (new_level
!= old_level
) {
4013 VM_DEBUG_CONSTANT_EVENT(vm_pressure_level_change
, VM_PRESSURE_LEVEL_CHANGE
, DBG_FUNC_NONE
,
4014 new_level
, old_level
, 0, 0);
4017 if ((memorystatus_vm_pressure_level
!= kVMPressureNormal
) || (old_level
!= memorystatus_vm_pressure_level
)) {
4018 if (vm_pageout_state
.vm_pressure_thread_running
== FALSE
) {
4019 thread_wakeup(&vm_pressure_thread
);
4022 if (old_level
!= memorystatus_vm_pressure_level
) {
4023 thread_wakeup(&vm_pageout_state
.vm_pressure_changed
);
4029 #endif /* VM_PRESSURE_EVENTS */
4032 mach_vm_pressure_level_monitor(__unused boolean_t wait_for_pressure
, __unused
unsigned int *pressure_level
) {
4036 return KERN_FAILURE
;
4038 #elif !VM_PRESSURE_EVENTS
4040 return KERN_FAILURE
;
4042 #else /* VM_PRESSURE_EVENTS */
4044 kern_return_t kr
= KERN_SUCCESS
;
4046 if (pressure_level
!= NULL
) {
4048 vm_pressure_level_t old_level
= memorystatus_vm_pressure_level
;
4050 if (wait_for_pressure
== TRUE
) {
4051 wait_result_t wr
= 0;
4053 while (old_level
== *pressure_level
) {
4054 wr
= assert_wait((event_t
) &vm_pageout_state
.vm_pressure_changed
,
4055 THREAD_INTERRUPTIBLE
);
4056 if (wr
== THREAD_WAITING
) {
4057 wr
= thread_block(THREAD_CONTINUE_NULL
);
4059 if (wr
== THREAD_INTERRUPTED
) {
4060 return KERN_ABORTED
;
4062 if (wr
== THREAD_AWAKENED
) {
4064 old_level
= memorystatus_vm_pressure_level
;
4066 if (old_level
!= *pressure_level
) {
4073 *pressure_level
= old_level
;
4076 kr
= KERN_INVALID_ARGUMENT
;
4080 #endif /* VM_PRESSURE_EVENTS */
4083 #if VM_PRESSURE_EVENTS
4085 vm_pressure_thread(void) {
4086 static boolean_t thread_initialized
= FALSE
;
4088 if (thread_initialized
== TRUE
) {
4089 vm_pageout_state
.vm_pressure_thread_running
= TRUE
;
4090 consider_vm_pressure_events();
4091 vm_pageout_state
.vm_pressure_thread_running
= FALSE
;
4094 thread_set_thread_name(current_thread(), "VM_pressure");
4095 thread_initialized
= TRUE
;
4096 assert_wait((event_t
) &vm_pressure_thread
, THREAD_UNINT
);
4097 thread_block((thread_continue_t
)vm_pressure_thread
);
4099 #endif /* VM_PRESSURE_EVENTS */
4103 * called once per-second via "compute_averages"
4106 compute_pageout_gc_throttle(__unused
void *arg
)
4108 if (vm_pageout_vminfo
.vm_pageout_considered_page
!= vm_pageout_state
.vm_pageout_considered_page_last
) {
4110 vm_pageout_state
.vm_pageout_considered_page_last
= vm_pageout_vminfo
.vm_pageout_considered_page
;
4112 thread_wakeup((event_t
) &vm_pageout_garbage_collect
);
4117 * vm_pageout_garbage_collect can also be called when the zone allocator needs
4118 * to call zone_gc on a different thread in order to trigger zone-map-exhaustion
4119 * jetsams. We need to check if the zone map size is above its jetsam limit to
4120 * decide if this was indeed the case.
4122 * We need to do this on a different thread because of the following reasons:
4124 * 1. In the case of synchronous jetsams, the leaking process can try to jetsam
4125 * itself causing the system to hang. We perform synchronous jetsams if we're
4126 * leaking in the VM map entries zone, so the leaking process could be doing a
4127 * zalloc for a VM map entry while holding its vm_map lock, when it decides to
4128 * jetsam itself. We also need the vm_map lock on the process termination path,
4129 * which would now lead the dying process to deadlock against itself.
4131 * 2. The jetsam path might need to allocate zone memory itself. We could try
4132 * using the non-blocking variant of zalloc for this path, but we can still
4133 * end up trying to do a kernel_memory_allocate when the zone_map is almost
4137 extern boolean_t
is_zone_map_nearing_exhaustion(void);
4140 vm_pageout_garbage_collect(int collect
)
4143 if (is_zone_map_nearing_exhaustion()) {
4145 * Woken up by the zone allocator for zone-map-exhaustion jetsams.
4147 * Bail out after calling zone_gc (which triggers the
4148 * zone-map-exhaustion jetsams). If we fall through, the subsequent
4149 * operations that clear out a bunch of caches might allocate zone
4150 * memory themselves (for eg. vm_map operations would need VM map
4151 * entries). Since the zone map is almost full at this point, we
4152 * could end up with a panic. We just need to quickly jetsam a
4153 * process and exit here.
4155 * It could so happen that we were woken up to relieve memory
4156 * pressure and the zone map also happened to be near its limit at
4157 * the time, in which case we'll skip out early. But that should be
4158 * ok; if memory pressure persists, the thread will simply be woken
4161 consider_zone_gc(TRUE
);
4164 /* Woken up by vm_pageout_scan or compute_pageout_gc_throttle. */
4165 boolean_t buf_large_zfree
= FALSE
;
4166 boolean_t first_try
= TRUE
;
4170 consider_machine_collect();
4174 if (consider_buffer_cache_collect
!= NULL
) {
4175 buf_large_zfree
= (*consider_buffer_cache_collect
)(0);
4177 if (first_try
== TRUE
|| buf_large_zfree
== TRUE
) {
4179 * consider_zone_gc should be last, because the other operations
4180 * might return memory to zones.
4182 consider_zone_gc(FALSE
);
4186 } while (buf_large_zfree
== TRUE
&& vm_page_free_count
< vm_page_free_target
);
4188 consider_machine_adjust();
4192 assert_wait((event_t
) &vm_pageout_garbage_collect
, THREAD_UNINT
);
4194 thread_block_parameter((thread_continue_t
) vm_pageout_garbage_collect
, (void *)1);
4199 #if VM_PAGE_BUCKETS_CHECK
4200 #if VM_PAGE_FAKE_BUCKETS
4201 extern vm_map_offset_t vm_page_fake_buckets_start
, vm_page_fake_buckets_end
;
4202 #endif /* VM_PAGE_FAKE_BUCKETS */
4203 #endif /* VM_PAGE_BUCKETS_CHECK */
4208 vm_set_restrictions()
4210 host_basic_info_data_t hinfo
;
4211 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
4214 host_info((host_t
)BSD_HOST
, HOST_BASIC_INFO
, (host_info_t
)&hinfo
, &count
);
4216 assert(hinfo
.max_cpus
> 0);
4218 if (hinfo
.max_cpus
<= 3) {
4220 * on systems with a limited number of CPUS, bind the
4221 * 4 major threads that can free memory and that tend to use
4222 * a fair bit of CPU under pressured conditions to a single processor.
4223 * This insures that these threads don't hog all of the available CPUs
4224 * (important for camera launch), while allowing them to run independently
4225 * w/r to locks... the 4 threads are
4226 * vm_pageout_scan, vm_pageout_iothread_internal (compressor),
4227 * vm_compressor_swap_trigger_thread (minor and major compactions),
4228 * memorystatus_thread (jetsams).
4230 * the first time the thread is run, it is responsible for checking the
4231 * state of vm_restricted_to_single_processor, and if TRUE it calls
4232 * thread_bind_master... someday this should be replaced with a group
4233 * scheduling mechanism and KPI.
4235 vm_pageout_state
.vm_restricted_to_single_processor
= TRUE
;
4237 vm_pageout_state
.vm_restricted_to_single_processor
= FALSE
;
4243 thread_t self
= current_thread();
4245 kern_return_t result
;
4249 * Set thread privileges.
4254 self
->options
|= TH_OPT_VMPRIV
;
4255 sched_set_thread_base_priority(self
, BASEPRI_VM
);
4256 thread_unlock(self
);
4258 if (!self
->reserved_stack
)
4259 self
->reserved_stack
= self
->kernel_stack
;
4261 if (vm_pageout_state
.vm_restricted_to_single_processor
== TRUE
)
4262 thread_vm_bind_group_add();
4266 thread_set_thread_name(current_thread(), "VM_pageout_scan");
4269 * Initialize some paging parameters.
4272 vm_pageout_state
.vm_pressure_thread_running
= FALSE
;
4273 vm_pageout_state
.vm_pressure_changed
= FALSE
;
4274 vm_pageout_state
.memorystatus_purge_on_warning
= 2;
4275 vm_pageout_state
.memorystatus_purge_on_urgent
= 5;
4276 vm_pageout_state
.memorystatus_purge_on_critical
= 8;
4277 vm_pageout_state
.vm_page_speculative_q_age_ms
= VM_PAGE_SPECULATIVE_Q_AGE_MS
;
4278 vm_pageout_state
.vm_page_speculative_percentage
= 5;
4279 vm_pageout_state
.vm_page_speculative_target
= 0;
4281 vm_pageout_state
.vm_pageout_external_iothread
= THREAD_NULL
;
4282 vm_pageout_state
.vm_pageout_internal_iothread
= THREAD_NULL
;
4284 vm_pageout_state
.vm_pageout_swap_wait
= 0;
4285 vm_pageout_state
.vm_pageout_idle_wait
= 0;
4286 vm_pageout_state
.vm_pageout_empty_wait
= 0;
4287 vm_pageout_state
.vm_pageout_burst_wait
= 0;
4288 vm_pageout_state
.vm_pageout_deadlock_wait
= 0;
4289 vm_pageout_state
.vm_pageout_deadlock_relief
= 0;
4290 vm_pageout_state
.vm_pageout_burst_inactive_throttle
= 0;
4292 vm_pageout_state
.vm_pageout_inactive
= 0;
4293 vm_pageout_state
.vm_pageout_inactive_used
= 0;
4294 vm_pageout_state
.vm_pageout_inactive_clean
= 0;
4296 vm_pageout_state
.vm_memory_pressure
= 0;
4297 vm_pageout_state
.vm_page_filecache_min
= 0;
4299 vm_pageout_state
.vm_page_filecache_min_divisor
= 70;
4300 vm_pageout_state
.vm_page_xpmapped_min_divisor
= 40;
4302 vm_pageout_state
.vm_page_filecache_min_divisor
= 27;
4303 vm_pageout_state
.vm_page_xpmapped_min_divisor
= 36;
4305 vm_pageout_state
.vm_page_free_count_init
= vm_page_free_count
;
4307 vm_pageout_state
.vm_pageout_considered_page_last
= 0;
4309 if (vm_pageout_state
.vm_pageout_swap_wait
== 0)
4310 vm_pageout_state
.vm_pageout_swap_wait
= VM_PAGEOUT_SWAP_WAIT
;
4312 if (vm_pageout_state
.vm_pageout_idle_wait
== 0)
4313 vm_pageout_state
.vm_pageout_idle_wait
= VM_PAGEOUT_IDLE_WAIT
;
4315 if (vm_pageout_state
.vm_pageout_burst_wait
== 0)
4316 vm_pageout_state
.vm_pageout_burst_wait
= VM_PAGEOUT_BURST_WAIT
;
4318 if (vm_pageout_state
.vm_pageout_empty_wait
== 0)
4319 vm_pageout_state
.vm_pageout_empty_wait
= VM_PAGEOUT_EMPTY_WAIT
;
4321 if (vm_pageout_state
.vm_pageout_deadlock_wait
== 0)
4322 vm_pageout_state
.vm_pageout_deadlock_wait
= VM_PAGEOUT_DEADLOCK_WAIT
;
4324 if (vm_pageout_state
.vm_pageout_deadlock_relief
== 0)
4325 vm_pageout_state
.vm_pageout_deadlock_relief
= VM_PAGEOUT_DEADLOCK_RELIEF
;
4327 if (vm_pageout_state
.vm_pageout_burst_inactive_throttle
== 0)
4328 vm_pageout_state
.vm_pageout_burst_inactive_throttle
= VM_PAGEOUT_BURST_INACTIVE_THROTTLE
;
4330 * even if we've already called vm_page_free_reserve
4331 * call it again here to insure that the targets are
4332 * accurately calculated (it uses vm_page_free_count_init)
4333 * calling it with an arg of 0 will not change the reserve
4334 * but will re-calculate free_min and free_target
4336 if (vm_page_free_reserved
< VM_PAGE_FREE_RESERVED(processor_count
)) {
4337 vm_page_free_reserve((VM_PAGE_FREE_RESERVED(processor_count
)) - vm_page_free_reserved
);
4339 vm_page_free_reserve(0);
4342 vm_page_queue_init(&vm_pageout_queue_external
.pgo_pending
);
4343 vm_pageout_queue_external
.pgo_maxlaundry
= VM_PAGE_LAUNDRY_MAX
;
4344 vm_pageout_queue_external
.pgo_laundry
= 0;
4345 vm_pageout_queue_external
.pgo_idle
= FALSE
;
4346 vm_pageout_queue_external
.pgo_busy
= FALSE
;
4347 vm_pageout_queue_external
.pgo_throttled
= FALSE
;
4348 vm_pageout_queue_external
.pgo_draining
= FALSE
;
4349 vm_pageout_queue_external
.pgo_lowpriority
= FALSE
;
4350 vm_pageout_queue_external
.pgo_tid
= -1;
4351 vm_pageout_queue_external
.pgo_inited
= FALSE
;
4353 vm_page_queue_init(&vm_pageout_queue_internal
.pgo_pending
);
4354 vm_pageout_queue_internal
.pgo_maxlaundry
= 0;
4355 vm_pageout_queue_internal
.pgo_laundry
= 0;
4356 vm_pageout_queue_internal
.pgo_idle
= FALSE
;
4357 vm_pageout_queue_internal
.pgo_busy
= FALSE
;
4358 vm_pageout_queue_internal
.pgo_throttled
= FALSE
;
4359 vm_pageout_queue_internal
.pgo_draining
= FALSE
;
4360 vm_pageout_queue_internal
.pgo_lowpriority
= FALSE
;
4361 vm_pageout_queue_internal
.pgo_tid
= -1;
4362 vm_pageout_queue_internal
.pgo_inited
= FALSE
;
4364 /* internal pageout thread started when default pager registered first time */
4365 /* external pageout and garbage collection threads started here */
4367 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_iothread_external
, NULL
,
4369 &vm_pageout_state
.vm_pageout_external_iothread
);
4370 if (result
!= KERN_SUCCESS
)
4371 panic("vm_pageout_iothread_external: create failed");
4373 thread_deallocate(vm_pageout_state
.vm_pageout_external_iothread
);
4375 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_garbage_collect
, NULL
,
4378 if (result
!= KERN_SUCCESS
)
4379 panic("vm_pageout_garbage_collect: create failed");
4381 thread_deallocate(thread
);
4383 #if VM_PRESSURE_EVENTS
4384 result
= kernel_thread_start_priority((thread_continue_t
)vm_pressure_thread
, NULL
,
4388 if (result
!= KERN_SUCCESS
)
4389 panic("vm_pressure_thread: create failed");
4391 thread_deallocate(thread
);
4394 vm_object_reaper_init();
4397 bzero(&vm_config
, sizeof(vm_config
));
4399 switch(vm_compressor_mode
) {
4401 case VM_PAGER_DEFAULT
:
4402 printf("mapping deprecated VM_PAGER_DEFAULT to VM_PAGER_COMPRESSOR_WITH_SWAP\n");
4404 case VM_PAGER_COMPRESSOR_WITH_SWAP
:
4405 vm_config
.compressor_is_present
= TRUE
;
4406 vm_config
.swap_is_present
= TRUE
;
4407 vm_config
.compressor_is_active
= TRUE
;
4408 vm_config
.swap_is_active
= TRUE
;
4411 case VM_PAGER_COMPRESSOR_NO_SWAP
:
4412 vm_config
.compressor_is_present
= TRUE
;
4413 vm_config
.swap_is_present
= TRUE
;
4414 vm_config
.compressor_is_active
= TRUE
;
4417 case VM_PAGER_FREEZER_DEFAULT
:
4418 printf("mapping deprecated VM_PAGER_FREEZER_DEFAULT to VM_PAGER_FREEZER_COMPRESSOR_NO_SWAP\n");
4420 case VM_PAGER_FREEZER_COMPRESSOR_NO_SWAP
:
4421 vm_config
.compressor_is_present
= TRUE
;
4422 vm_config
.swap_is_present
= TRUE
;
4425 case VM_PAGER_COMPRESSOR_NO_SWAP_PLUS_FREEZER_COMPRESSOR_WITH_SWAP
:
4426 vm_config
.compressor_is_present
= TRUE
;
4427 vm_config
.swap_is_present
= TRUE
;
4428 vm_config
.compressor_is_active
= TRUE
;
4429 vm_config
.freezer_swap_is_active
= TRUE
;
4432 case VM_PAGER_NOT_CONFIGURED
:
4436 printf("unknown compressor mode - %x\n", vm_compressor_mode
);
4439 if (VM_CONFIG_COMPRESSOR_IS_PRESENT
)
4440 vm_compressor_pager_init();
4442 #if VM_PRESSURE_EVENTS
4443 vm_pressure_events_enabled
= TRUE
;
4444 #endif /* VM_PRESSURE_EVENTS */
4446 #if CONFIG_PHANTOM_CACHE
4447 vm_phantom_cache_init();
4449 #if VM_PAGE_BUCKETS_CHECK
4450 #if VM_PAGE_FAKE_BUCKETS
4451 printf("**** DEBUG: protecting fake buckets [0x%llx:0x%llx]\n",
4452 (uint64_t) vm_page_fake_buckets_start
,
4453 (uint64_t) vm_page_fake_buckets_end
);
4454 pmap_protect(kernel_pmap
,
4455 vm_page_fake_buckets_start
,
4456 vm_page_fake_buckets_end
,
4458 // *(char *) vm_page_fake_buckets_start = 'x'; /* panic! */
4459 #endif /* VM_PAGE_FAKE_BUCKETS */
4460 #endif /* VM_PAGE_BUCKETS_CHECK */
4462 #if VM_OBJECT_TRACKING
4463 vm_object_tracking_init();
4464 #endif /* VM_OBJECT_TRACKING */
4468 vm_pageout_continue();
4473 * The vm_pageout_continue() call above never returns, so the code below is never
4474 * executed. We take advantage of this to declare several DTrace VM related probe
4475 * points that our kernel doesn't have an analog for. These are probe points that
4476 * exist in Solaris and are in the DTrace documentation, so people may have written
4477 * scripts that use them. Declaring the probe points here means their scripts will
4478 * compile and execute which we want for portability of the scripts, but since this
4479 * section of code is never reached, the probe points will simply never fire. Yes,
4480 * this is basically a hack. The problem is the DTrace probe points were chosen with
4481 * Solaris specific VM events in mind, not portability to different VM implementations.
4484 DTRACE_VM2(execfree
, int, 1, (uint64_t *), NULL
);
4485 DTRACE_VM2(execpgin
, int, 1, (uint64_t *), NULL
);
4486 DTRACE_VM2(execpgout
, int, 1, (uint64_t *), NULL
);
4487 DTRACE_VM2(pgswapin
, int, 1, (uint64_t *), NULL
);
4488 DTRACE_VM2(pgswapout
, int, 1, (uint64_t *), NULL
);
4489 DTRACE_VM2(swapin
, int, 1, (uint64_t *), NULL
);
4490 DTRACE_VM2(swapout
, int, 1, (uint64_t *), NULL
);
4497 vm_pageout_internal_start(void)
4499 kern_return_t result
;
4501 host_basic_info_data_t hinfo
;
4503 assert (VM_CONFIG_COMPRESSOR_IS_PRESENT
);
4505 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
4507 host_info((host_t
)BSD_HOST
, HOST_BASIC_INFO
, (host_info_t
)&hinfo
, &count
);
4509 assert(hinfo
.max_cpus
> 0);
4512 vm_pageout_state
.vm_compressor_thread_count
= 1;
4514 if (hinfo
.max_cpus
> 4)
4515 vm_pageout_state
.vm_compressor_thread_count
= 2;
4517 vm_pageout_state
.vm_compressor_thread_count
= 1;
4519 PE_parse_boot_argn("vmcomp_threads", &vm_pageout_state
.vm_compressor_thread_count
,
4520 sizeof(vm_pageout_state
.vm_compressor_thread_count
));
4522 if (vm_pageout_state
.vm_compressor_thread_count
>= hinfo
.max_cpus
)
4523 vm_pageout_state
.vm_compressor_thread_count
= hinfo
.max_cpus
- 1;
4524 if (vm_pageout_state
.vm_compressor_thread_count
<= 0)
4525 vm_pageout_state
.vm_compressor_thread_count
= 1;
4526 else if (vm_pageout_state
.vm_compressor_thread_count
> MAX_COMPRESSOR_THREAD_COUNT
)
4527 vm_pageout_state
.vm_compressor_thread_count
= MAX_COMPRESSOR_THREAD_COUNT
;
4529 vm_pageout_queue_internal
.pgo_maxlaundry
= (vm_pageout_state
.vm_compressor_thread_count
* 4) * VM_PAGE_LAUNDRY_MAX
;
4531 PE_parse_boot_argn("vmpgoi_maxlaundry", &vm_pageout_queue_internal
.pgo_maxlaundry
, sizeof(vm_pageout_queue_internal
.pgo_maxlaundry
));
4533 for (i
= 0; i
< vm_pageout_state
.vm_compressor_thread_count
; i
++) {
4535 ciq
[i
].q
= &vm_pageout_queue_internal
;
4536 ciq
[i
].current_chead
= NULL
;
4537 ciq
[i
].scratch_buf
= kalloc(COMPRESSOR_SCRATCH_BUF_SIZE
);
4539 result
= kernel_thread_start_priority((thread_continue_t
)vm_pageout_iothread_internal
, (void *)&ciq
[i
],
4540 BASEPRI_VM
, &vm_pageout_state
.vm_pageout_internal_iothread
);
4542 if (result
== KERN_SUCCESS
)
4543 thread_deallocate(vm_pageout_state
.vm_pageout_internal_iothread
);
4552 * To support I/O Expedite for compressed files we mark the upls with special flags.
4553 * The way decmpfs works is that we create a big upl which marks all the pages needed to
4554 * represent the compressed file as busy. We tag this upl with the flag UPL_DECMP_REQ. Decmpfs
4555 * then issues smaller I/Os for compressed I/Os, deflates them and puts the data into the pages
4556 * being held in the big original UPL. We mark each of these smaller UPLs with the flag
4557 * UPL_DECMP_REAL_IO. Any outstanding real I/O UPL is tracked by the big req upl using the
4558 * decmp_io_upl field (in the upl structure). This link is protected in the forward direction
4559 * by the req upl lock (the reverse link doesnt need synch. since we never inspect this link
4560 * unless the real I/O upl is being destroyed).
4565 upl_set_decmp_info(upl_t upl
, upl_t src_upl
)
4567 assert((src_upl
->flags
& UPL_DECMP_REQ
) != 0);
4570 if (src_upl
->decmp_io_upl
) {
4572 * If there is already an alive real I/O UPL, ignore this new UPL.
4573 * This case should rarely happen and even if it does, it just means
4574 * that we might issue a spurious expedite which the driver is expected
4577 upl_unlock(src_upl
);
4580 src_upl
->decmp_io_upl
= (void *)upl
;
4581 src_upl
->ref_count
++;
4583 upl
->flags
|= UPL_DECMP_REAL_IO
;
4584 upl
->decmp_io_upl
= (void *)src_upl
;
4585 upl_unlock(src_upl
);
4587 #endif /* CONFIG_IOSCHED */
4590 int upl_debug_enabled
= 1;
4592 int upl_debug_enabled
= 0;
4596 upl_create(int type
, int flags
, upl_size_t size
)
4599 vm_size_t page_field_size
= 0;
4601 vm_size_t upl_size
= sizeof(struct upl
);
4603 size
= round_page_32(size
);
4605 if (type
& UPL_CREATE_LITE
) {
4606 page_field_size
= (atop(size
) + 7) >> 3;
4607 page_field_size
= (page_field_size
+ 3) & 0xFFFFFFFC;
4609 upl_flags
|= UPL_LITE
;
4611 if (type
& UPL_CREATE_INTERNAL
) {
4612 upl_size
+= sizeof(struct upl_page_info
) * atop(size
);
4614 upl_flags
|= UPL_INTERNAL
;
4616 upl
= (upl_t
)kalloc(upl_size
+ page_field_size
);
4618 if (page_field_size
)
4619 bzero((char *)upl
+ upl_size
, page_field_size
);
4621 upl
->flags
= upl_flags
| flags
;
4622 upl
->kaddr
= (vm_offset_t
)0;
4624 upl
->map_object
= NULL
;
4626 upl
->ext_ref_count
= 0;
4627 upl
->highest_page
= 0;
4629 upl
->vector_upl
= NULL
;
4630 upl
->associated_upl
= NULL
;
4631 upl
->upl_iodone
= NULL
;
4633 if (type
& UPL_CREATE_IO_TRACKING
) {
4634 upl
->upl_priority
= proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO
);
4637 upl
->upl_reprio_info
= 0;
4638 upl
->decmp_io_upl
= 0;
4639 if ((type
& UPL_CREATE_INTERNAL
) && (type
& UPL_CREATE_EXPEDITE_SUP
)) {
4640 /* Only support expedite on internal UPLs */
4641 thread_t curthread
= current_thread();
4642 upl
->upl_reprio_info
= (uint64_t *)kalloc(sizeof(uint64_t) * atop(size
));
4643 bzero(upl
->upl_reprio_info
, (sizeof(uint64_t) * atop(size
)));
4644 upl
->flags
|= UPL_EXPEDITE_SUPPORTED
;
4645 if (curthread
->decmp_upl
!= NULL
)
4646 upl_set_decmp_info(upl
, curthread
->decmp_upl
);
4649 #if CONFIG_IOSCHED || UPL_DEBUG
4650 if ((type
& UPL_CREATE_IO_TRACKING
) || upl_debug_enabled
) {
4651 upl
->upl_creator
= current_thread();
4654 upl
->flags
|= UPL_TRACKED_BY_OBJECT
;
4659 upl
->ubc_alias1
= 0;
4660 upl
->ubc_alias2
= 0;
4663 upl
->upl_commit_index
= 0;
4664 bzero(&upl
->upl_commit_records
[0], sizeof(upl
->upl_commit_records
));
4666 (void) OSBacktrace(&upl
->upl_create_retaddr
[0], UPL_DEBUG_STACK_FRAMES
);
4667 #endif /* UPL_DEBUG */
4673 upl_destroy(upl_t upl
)
4675 int page_field_size
; /* bit field in word size buf */
4678 if (upl
->ext_ref_count
) {
4679 panic("upl(%p) ext_ref_count", upl
);
4683 if ((upl
->flags
& UPL_DECMP_REAL_IO
) && upl
->decmp_io_upl
) {
4685 src_upl
= upl
->decmp_io_upl
;
4686 assert((src_upl
->flags
& UPL_DECMP_REQ
) != 0);
4688 src_upl
->decmp_io_upl
= NULL
;
4689 upl_unlock(src_upl
);
4690 upl_deallocate(src_upl
);
4692 #endif /* CONFIG_IOSCHED */
4694 #if CONFIG_IOSCHED || UPL_DEBUG
4695 if ((upl
->flags
& UPL_TRACKED_BY_OBJECT
) && !(upl
->flags
& UPL_VECTOR
)) {
4698 if (upl
->flags
& UPL_SHADOWED
) {
4699 object
= upl
->map_object
->shadow
;
4701 object
= upl
->map_object
;
4704 vm_object_lock(object
);
4705 queue_remove(&object
->uplq
, upl
, upl_t
, uplq
);
4706 vm_object_activity_end(object
);
4707 vm_object_collapse(object
, 0, TRUE
);
4708 vm_object_unlock(object
);
4712 * drop a reference on the map_object whether or
4713 * not a pageout object is inserted
4715 if (upl
->flags
& UPL_SHADOWED
)
4716 vm_object_deallocate(upl
->map_object
);
4718 if (upl
->flags
& UPL_DEVICE_MEMORY
)
4722 page_field_size
= 0;
4724 if (upl
->flags
& UPL_LITE
) {
4725 page_field_size
= ((size
/PAGE_SIZE
) + 7) >> 3;
4726 page_field_size
= (page_field_size
+ 3) & 0xFFFFFFFC;
4728 upl_lock_destroy(upl
);
4729 upl
->vector_upl
= (vector_upl_t
) 0xfeedbeef;
4732 if (upl
->flags
& UPL_EXPEDITE_SUPPORTED
)
4733 kfree(upl
->upl_reprio_info
, sizeof(uint64_t) * (size
/PAGE_SIZE
));
4736 if (upl
->flags
& UPL_INTERNAL
) {
4738 sizeof(struct upl
) +
4739 (sizeof(struct upl_page_info
) * (size
/PAGE_SIZE
))
4742 kfree(upl
, sizeof(struct upl
) + page_field_size
);
4747 upl_deallocate(upl_t upl
)
4751 if (--upl
->ref_count
== 0) {
4752 if(vector_upl_is_valid(upl
))
4753 vector_upl_deallocate(upl
);
4756 if (upl
->upl_iodone
)
4757 upl_callout_iodone(upl
);
4766 upl_mark_decmp(upl_t upl
)
4768 if (upl
->flags
& UPL_TRACKED_BY_OBJECT
) {
4769 upl
->flags
|= UPL_DECMP_REQ
;
4770 upl
->upl_creator
->decmp_upl
= (void *)upl
;
4775 upl_unmark_decmp(upl_t upl
)
4777 if(upl
&& (upl
->flags
& UPL_DECMP_REQ
)) {
4778 upl
->upl_creator
->decmp_upl
= NULL
;
4782 #endif /* CONFIG_IOSCHED */
4784 #define VM_PAGE_Q_BACKING_UP(q) \
4785 ((q)->pgo_laundry >= (((q)->pgo_maxlaundry * 8) / 10))
4787 boolean_t
must_throttle_writes(void);
4790 must_throttle_writes()
4792 if (VM_PAGE_Q_BACKING_UP(&vm_pageout_queue_external
) &&
4793 vm_page_pageable_external_count
> (AVAILABLE_NON_COMPRESSED_MEMORY
* 6) / 10)
4801 * Routine: vm_object_upl_request
4803 * Cause the population of a portion of a vm_object.
4804 * Depending on the nature of the request, the pages
4805 * returned may be contain valid data or be uninitialized.
4806 * A page list structure, listing the physical pages
4807 * will be returned upon request.
4808 * This function is called by the file system or any other
4809 * supplier of backing store to a pager.
4810 * IMPORTANT NOTE: The caller must still respect the relationship
4811 * between the vm_object and its backing memory object. The
4812 * caller MUST NOT substitute changes in the backing file
4813 * without first doing a memory_object_lock_request on the
4814 * target range unless it is know that the pages are not
4815 * shared with another entity at the pager level.
4817 * if a page list structure is present
4818 * return the mapped physical pages, where a
4819 * page is not present, return a non-initialized
4820 * one. If the no_sync bit is turned on, don't
4821 * call the pager unlock to synchronize with other
4822 * possible copies of the page. Leave pages busy
4823 * in the original object, if a page list structure
4824 * was specified. When a commit of the page list
4825 * pages is done, the dirty bit will be set for each one.
4827 * If a page list structure is present, return
4828 * all mapped pages. Where a page does not exist
4829 * map a zero filled one. Leave pages busy in
4830 * the original object. If a page list structure
4831 * is not specified, this call is a no-op.
4833 * Note: access of default pager objects has a rather interesting
4834 * twist. The caller of this routine, presumably the file system
4835 * page cache handling code, will never actually make a request
4836 * against a default pager backed object. Only the default
4837 * pager will make requests on backing store related vm_objects
4838 * In this way the default pager can maintain the relationship
4839 * between backing store files (abstract memory objects) and
4840 * the vm_objects (cache objects), they support.
4844 __private_extern__ kern_return_t
4845 vm_object_upl_request(
4847 vm_object_offset_t offset
,
4850 upl_page_info_array_t user_page_list
,
4851 unsigned int *page_list_count
,
4852 upl_control_flags_t cntrl_flags
,
4855 vm_page_t dst_page
= VM_PAGE_NULL
;
4856 vm_object_offset_t dst_offset
;
4857 upl_size_t xfer_size
;
4858 unsigned int size_in_pages
;
4863 vm_page_t alias_page
= NULL
;
4864 int refmod_state
= 0;
4865 wpl_array_t lite_list
= NULL
;
4866 vm_object_t last_copy_object
;
4867 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
4868 struct vm_page_delayed_work
*dwp
;
4871 int io_tracking_flag
= 0;
4873 int page_grab_count
= 0;
4875 pmap_flush_context pmap_flush_context_storage
;
4876 boolean_t pmap_flushes_delayed
= FALSE
;
4878 if (cntrl_flags
& ~UPL_VALID_FLAGS
) {
4880 * For forward compatibility's sake,
4881 * reject any unknown flag.
4883 return KERN_INVALID_VALUE
;
4885 if ( (!object
->internal
) && (object
->paging_offset
!= 0) )
4886 panic("vm_object_upl_request: external object with non-zero paging offset\n");
4887 if (object
->phys_contiguous
)
4888 panic("vm_object_upl_request: contiguous object specified\n");
4890 VM_DEBUG_CONSTANT_EVENT(vm_object_upl_request
, VM_UPL_REQUEST
, DBG_FUNC_START
, size
, cntrl_flags
, 0, 0);
4892 if (size
> MAX_UPL_SIZE_BYTES
)
4893 size
= MAX_UPL_SIZE_BYTES
;
4895 if ( (cntrl_flags
& UPL_SET_INTERNAL
) && page_list_count
!= NULL
)
4896 *page_list_count
= MAX_UPL_SIZE_BYTES
>> PAGE_SHIFT
;
4898 #if CONFIG_IOSCHED || UPL_DEBUG
4899 if (object
->io_tracking
|| upl_debug_enabled
)
4900 io_tracking_flag
|= UPL_CREATE_IO_TRACKING
;
4903 if (object
->io_tracking
)
4904 io_tracking_flag
|= UPL_CREATE_EXPEDITE_SUP
;
4907 if (cntrl_flags
& UPL_SET_INTERNAL
) {
4908 if (cntrl_flags
& UPL_SET_LITE
) {
4910 upl
= upl_create(UPL_CREATE_INTERNAL
| UPL_CREATE_LITE
| io_tracking_flag
, 0, size
);
4912 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
4913 lite_list
= (wpl_array_t
)
4914 (((uintptr_t)user_page_list
) +
4915 ((size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
4917 user_page_list
= NULL
;
4921 upl
= upl_create(UPL_CREATE_INTERNAL
| io_tracking_flag
, 0, size
);
4923 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
4925 user_page_list
= NULL
;
4929 if (cntrl_flags
& UPL_SET_LITE
) {
4931 upl
= upl_create(UPL_CREATE_EXTERNAL
| UPL_CREATE_LITE
| io_tracking_flag
, 0, size
);
4933 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
4938 upl
= upl_create(UPL_CREATE_EXTERNAL
| io_tracking_flag
, 0, size
);
4944 user_page_list
[0].device
= FALSE
;
4946 if (cntrl_flags
& UPL_SET_LITE
) {
4947 upl
->map_object
= object
;
4949 upl
->map_object
= vm_object_allocate(size
);
4951 * No neeed to lock the new object: nobody else knows
4952 * about it yet, so it's all ours so far.
4954 upl
->map_object
->shadow
= object
;
4955 upl
->map_object
->pageout
= TRUE
;
4956 upl
->map_object
->can_persist
= FALSE
;
4957 upl
->map_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
4958 upl
->map_object
->vo_shadow_offset
= offset
;
4959 upl
->map_object
->wimg_bits
= object
->wimg_bits
;
4961 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
4963 upl
->flags
|= UPL_SHADOWED
;
4965 if (cntrl_flags
& UPL_FOR_PAGEOUT
)
4966 upl
->flags
|= UPL_PAGEOUT
;
4968 vm_object_lock(object
);
4969 vm_object_activity_begin(object
);
4972 #if CONFIG_SECLUDED_MEMORY
4973 if (object
->can_grab_secluded
) {
4974 grab_options
|= VM_PAGE_GRAB_SECLUDED
;
4976 #endif /* CONFIG_SECLUDED_MEMORY */
4979 * we can lock in the paging_offset once paging_in_progress is set
4982 upl
->offset
= offset
+ object
->paging_offset
;
4984 #if CONFIG_IOSCHED || UPL_DEBUG
4985 if (object
->io_tracking
|| upl_debug_enabled
) {
4986 vm_object_activity_begin(object
);
4987 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
4990 if ((cntrl_flags
& UPL_WILL_MODIFY
) && object
->copy
!= VM_OBJECT_NULL
) {
4992 * Honor copy-on-write obligations
4994 * The caller is gathering these pages and
4995 * might modify their contents. We need to
4996 * make sure that the copy object has its own
4997 * private copies of these pages before we let
4998 * the caller modify them.
5000 vm_object_update(object
,
5005 FALSE
, /* should_return */
5006 MEMORY_OBJECT_COPY_SYNC
,
5009 VM_PAGEOUT_DEBUG(upl_cow
, 1);
5010 VM_PAGEOUT_DEBUG(upl_cow_pages
, (size
>> PAGE_SHIFT
));
5013 * remember which copy object we synchronized with
5015 last_copy_object
= object
->copy
;
5019 dst_offset
= offset
;
5020 size_in_pages
= size
/ PAGE_SIZE
;
5024 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
5026 if (vm_page_free_count
> (vm_page_free_target
+ size_in_pages
) ||
5027 object
->resident_page_count
< ((MAX_UPL_SIZE_BYTES
* 2) >> PAGE_SHIFT
))
5028 object
->scan_collisions
= 0;
5030 if ((cntrl_flags
& UPL_WILL_MODIFY
) && must_throttle_writes() == TRUE
) {
5031 boolean_t isSSD
= FALSE
;
5036 vnode_pager_get_isSSD(object
->pager
, &isSSD
);
5038 vm_object_unlock(object
);
5040 OSAddAtomic(size_in_pages
, &vm_upl_wait_for_pages
);
5043 delay(1000 * size_in_pages
);
5045 delay(5000 * size_in_pages
);
5046 OSAddAtomic(-size_in_pages
, &vm_upl_wait_for_pages
);
5048 vm_object_lock(object
);
5055 if ((alias_page
== NULL
) && !(cntrl_flags
& UPL_SET_LITE
)) {
5056 vm_object_unlock(object
);
5057 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
5058 vm_object_lock(object
);
5060 if (cntrl_flags
& UPL_COPYOUT_FROM
) {
5061 upl
->flags
|= UPL_PAGE_SYNC_DONE
;
5063 if ( ((dst_page
= vm_page_lookup(object
, dst_offset
)) == VM_PAGE_NULL
) ||
5064 dst_page
->vmp_fictitious
||
5065 dst_page
->vmp_absent
||
5066 dst_page
->vmp_error
||
5067 dst_page
->vmp_cleaning
||
5068 (VM_PAGE_WIRED(dst_page
))) {
5071 user_page_list
[entry
].phys_addr
= 0;
5075 phys_page
= VM_PAGE_GET_PHYS_PAGE(dst_page
);
5078 * grab this up front...
5079 * a high percentange of the time we're going to
5080 * need the hardware modification state a bit later
5081 * anyway... so we can eliminate an extra call into
5082 * the pmap layer by grabbing it here and recording it
5084 if (dst_page
->vmp_pmapped
)
5085 refmod_state
= pmap_get_refmod(phys_page
);
5089 if ( (refmod_state
& VM_MEM_REFERENCED
) && VM_PAGE_INACTIVE(dst_page
)) {
5091 * page is on inactive list and referenced...
5092 * reactivate it now... this gets it out of the
5093 * way of vm_pageout_scan which would have to
5094 * reactivate it upon tripping over it
5096 dwp
->dw_mask
|= DW_vm_page_activate
;
5098 if (cntrl_flags
& UPL_RET_ONLY_DIRTY
) {
5100 * we're only asking for DIRTY pages to be returned
5102 if (dst_page
->vmp_laundry
|| !(cntrl_flags
& UPL_FOR_PAGEOUT
)) {
5104 * if we were the page stolen by vm_pageout_scan to be
5105 * cleaned (as opposed to a buddy being clustered in
5106 * or this request is not being driven by a PAGEOUT cluster
5107 * then we only need to check for the page being dirty or
5108 * precious to decide whether to return it
5110 if (dst_page
->vmp_dirty
|| dst_page
->vmp_precious
|| (refmod_state
& VM_MEM_MODIFIED
))
5115 * this is a request for a PAGEOUT cluster and this page
5116 * is merely along for the ride as a 'buddy'... not only
5117 * does it have to be dirty to be returned, but it also
5118 * can't have been referenced recently...
5120 if ( (hibernate_cleaning_in_progress
== TRUE
||
5121 (!((refmod_state
& VM_MEM_REFERENCED
) || dst_page
->vmp_reference
) ||
5122 (dst_page
->vmp_q_state
== VM_PAGE_ON_THROTTLED_Q
))) &&
5123 ((refmod_state
& VM_MEM_MODIFIED
) || dst_page
->vmp_dirty
|| dst_page
->vmp_precious
) ) {
5128 * if we reach here, we're not to return
5129 * the page... go on to the next one
5131 if (dst_page
->vmp_laundry
== TRUE
) {
5133 * if we get here, the page is not 'cleaning' (filtered out above).
5134 * since it has been referenced, remove it from the laundry
5135 * so we don't pay the cost of an I/O to clean a page
5136 * we're just going to take back
5138 vm_page_lockspin_queues();
5140 vm_pageout_steal_laundry(dst_page
, TRUE
);
5141 vm_page_activate(dst_page
);
5143 vm_page_unlock_queues();
5146 user_page_list
[entry
].phys_addr
= 0;
5151 if (dst_page
->vmp_busy
) {
5152 if (cntrl_flags
& UPL_NOBLOCK
) {
5154 user_page_list
[entry
].phys_addr
= 0;
5160 * someone else is playing with the
5161 * page. We will have to wait.
5163 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
5167 if (dst_page
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
) {
5169 vm_page_lockspin_queues();
5171 if (dst_page
->vmp_q_state
== VM_PAGE_ON_PAGEOUT_Q
) {
5173 * we've buddied up a page for a clustered pageout
5174 * that has already been moved to the pageout
5175 * queue by pageout_scan... we need to remove
5176 * it from the queue and drop the laundry count
5179 vm_pageout_throttle_up(dst_page
);
5181 vm_page_unlock_queues();
5183 hw_dirty
= refmod_state
& VM_MEM_MODIFIED
;
5184 dirty
= hw_dirty
? TRUE
: dst_page
->vmp_dirty
;
5186 if (phys_page
> upl
->highest_page
)
5187 upl
->highest_page
= phys_page
;
5189 assert (!pmap_is_noencrypt(phys_page
));
5191 if (cntrl_flags
& UPL_SET_LITE
) {
5192 unsigned int pg_num
;
5194 pg_num
= (unsigned int) ((dst_offset
-offset
)/PAGE_SIZE
);
5195 assert(pg_num
== (dst_offset
-offset
)/PAGE_SIZE
);
5196 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
5199 if (pmap_flushes_delayed
== FALSE
) {
5200 pmap_flush_context_init(&pmap_flush_context_storage
);
5201 pmap_flushes_delayed
= TRUE
;
5203 pmap_clear_refmod_options(phys_page
,
5205 PMAP_OPTIONS_NOFLUSH
| PMAP_OPTIONS_CLEAR_WRITE
,
5206 &pmap_flush_context_storage
);
5210 * Mark original page as cleaning
5213 dst_page
->vmp_cleaning
= TRUE
;
5214 dst_page
->vmp_precious
= FALSE
;
5217 * use pageclean setup, it is more
5218 * convenient even for the pageout
5221 vm_object_lock(upl
->map_object
);
5222 vm_pageclean_setup(dst_page
, alias_page
, upl
->map_object
, size
- xfer_size
);
5223 vm_object_unlock(upl
->map_object
);
5225 alias_page
->vmp_absent
= FALSE
;
5229 SET_PAGE_DIRTY(dst_page
, FALSE
);
5231 dst_page
->vmp_dirty
= FALSE
;
5235 dst_page
->vmp_precious
= TRUE
;
5237 if ( !(cntrl_flags
& UPL_CLEAN_IN_PLACE
) ) {
5238 if ( !VM_PAGE_WIRED(dst_page
))
5239 dst_page
->vmp_free_when_done
= TRUE
;
5242 if ((cntrl_flags
& UPL_WILL_MODIFY
) && object
->copy
!= last_copy_object
) {
5244 * Honor copy-on-write obligations
5246 * The copy object has changed since we
5247 * last synchronized for copy-on-write.
5248 * Another copy object might have been
5249 * inserted while we released the object's
5250 * lock. Since someone could have seen the
5251 * original contents of the remaining pages
5252 * through that new object, we have to
5253 * synchronize with it again for the remaining
5254 * pages only. The previous pages are "busy"
5255 * so they can not be seen through the new
5256 * mapping. The new mapping will see our
5257 * upcoming changes for those previous pages,
5258 * but that's OK since they couldn't see what
5259 * was there before. It's just a race anyway
5260 * and there's no guarantee of consistency or
5261 * atomicity. We just don't want new mappings
5262 * to see both the *before* and *after* pages.
5264 if (object
->copy
!= VM_OBJECT_NULL
) {
5267 dst_offset
,/* current offset */
5268 xfer_size
, /* remaining size */
5271 FALSE
, /* should_return */
5272 MEMORY_OBJECT_COPY_SYNC
,
5275 VM_PAGEOUT_DEBUG(upl_cow_again
, 1);
5276 VM_PAGEOUT_DEBUG(upl_cow_again_pages
, (xfer_size
>> PAGE_SHIFT
));
5279 * remember the copy object we synced with
5281 last_copy_object
= object
->copy
;
5283 dst_page
= vm_page_lookup(object
, dst_offset
);
5285 if (dst_page
!= VM_PAGE_NULL
) {
5287 if ((cntrl_flags
& UPL_RET_ONLY_ABSENT
)) {
5289 * skip over pages already present in the cache
5292 user_page_list
[entry
].phys_addr
= 0;
5296 if (dst_page
->vmp_fictitious
) {
5297 panic("need corner case for fictitious page");
5300 if (dst_page
->vmp_busy
|| dst_page
->vmp_cleaning
) {
5302 * someone else is playing with the
5303 * page. We will have to wait.
5305 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
5309 if (dst_page
->vmp_laundry
)
5310 vm_pageout_steal_laundry(dst_page
, FALSE
);
5312 if (object
->private) {
5314 * This is a nasty wrinkle for users
5315 * of upl who encounter device or
5316 * private memory however, it is
5317 * unavoidable, only a fault can
5318 * resolve the actual backing
5319 * physical page by asking the
5323 user_page_list
[entry
].phys_addr
= 0;
5327 if (object
->scan_collisions
) {
5329 * the pageout_scan thread is trying to steal
5330 * pages from this object, but has run into our
5331 * lock... grab 2 pages from the head of the object...
5332 * the first is freed on behalf of pageout_scan, the
5333 * 2nd is for our own use... we use vm_object_page_grab
5334 * in both cases to avoid taking pages from the free
5335 * list since we are under memory pressure and our
5336 * lock on this object is getting in the way of
5339 dst_page
= vm_object_page_grab(object
);
5341 if (dst_page
!= VM_PAGE_NULL
)
5342 vm_page_release(dst_page
,
5345 dst_page
= vm_object_page_grab(object
);
5347 if (dst_page
== VM_PAGE_NULL
) {
5349 * need to allocate a page
5351 dst_page
= vm_page_grab_options(grab_options
);
5352 if (dst_page
!= VM_PAGE_NULL
)
5355 if (dst_page
== VM_PAGE_NULL
) {
5356 if ( (cntrl_flags
& (UPL_RET_ONLY_ABSENT
| UPL_NOBLOCK
)) == (UPL_RET_ONLY_ABSENT
| UPL_NOBLOCK
)) {
5358 * we don't want to stall waiting for pages to come onto the free list
5359 * while we're already holding absent pages in this UPL
5360 * the caller will deal with the empty slots
5363 user_page_list
[entry
].phys_addr
= 0;
5368 * no pages available... wait
5369 * then try again for the same
5372 vm_object_unlock(object
);
5374 OSAddAtomic(size_in_pages
, &vm_upl_wait_for_pages
);
5376 VM_DEBUG_EVENT(vm_upl_page_wait
, VM_UPL_PAGE_WAIT
, DBG_FUNC_START
, vm_upl_wait_for_pages
, 0, 0, 0);
5379 OSAddAtomic(-size_in_pages
, &vm_upl_wait_for_pages
);
5381 VM_DEBUG_EVENT(vm_upl_page_wait
, VM_UPL_PAGE_WAIT
, DBG_FUNC_END
, vm_upl_wait_for_pages
, 0, 0, 0);
5383 vm_object_lock(object
);
5387 vm_page_insert(dst_page
, object
, dst_offset
);
5389 dst_page
->vmp_absent
= TRUE
;
5390 dst_page
->vmp_busy
= FALSE
;
5392 if (cntrl_flags
& UPL_RET_ONLY_ABSENT
) {
5394 * if UPL_RET_ONLY_ABSENT was specified,
5395 * than we're definitely setting up a
5396 * upl for a clustered read/pagein
5397 * operation... mark the pages as clustered
5398 * so upl_commit_range can put them on the
5401 dst_page
->vmp_clustered
= TRUE
;
5403 if ( !(cntrl_flags
& UPL_FILE_IO
))
5404 VM_STAT_INCR(pageins
);
5407 phys_page
= VM_PAGE_GET_PHYS_PAGE(dst_page
);
5409 dst_page
->vmp_overwriting
= TRUE
;
5411 if (dst_page
->vmp_pmapped
) {
5412 if ( !(cntrl_flags
& UPL_FILE_IO
))
5414 * eliminate all mappings from the
5415 * original object and its prodigy
5417 refmod_state
= pmap_disconnect(phys_page
);
5419 refmod_state
= pmap_get_refmod(phys_page
);
5423 hw_dirty
= refmod_state
& VM_MEM_MODIFIED
;
5424 dirty
= hw_dirty
? TRUE
: dst_page
->vmp_dirty
;
5426 if (cntrl_flags
& UPL_SET_LITE
) {
5427 unsigned int pg_num
;
5429 pg_num
= (unsigned int) ((dst_offset
-offset
)/PAGE_SIZE
);
5430 assert(pg_num
== (dst_offset
-offset
)/PAGE_SIZE
);
5431 lite_list
[pg_num
>>5] |= 1 << (pg_num
& 31);
5434 pmap_clear_modify(phys_page
);
5437 * Mark original page as cleaning
5440 dst_page
->vmp_cleaning
= TRUE
;
5441 dst_page
->vmp_precious
= FALSE
;
5444 * use pageclean setup, it is more
5445 * convenient even for the pageout
5448 vm_object_lock(upl
->map_object
);
5449 vm_pageclean_setup(dst_page
, alias_page
, upl
->map_object
, size
- xfer_size
);
5450 vm_object_unlock(upl
->map_object
);
5452 alias_page
->vmp_absent
= FALSE
;
5456 if (cntrl_flags
& UPL_REQUEST_SET_DIRTY
) {
5457 upl
->flags
&= ~UPL_CLEAR_DIRTY
;
5458 upl
->flags
|= UPL_SET_DIRTY
;
5460 upl
->flags
|= UPL_SET_DIRTY
;
5461 } else if (cntrl_flags
& UPL_CLEAN_IN_PLACE
) {
5463 * clean in place for read implies
5464 * that a write will be done on all
5465 * the pages that are dirty before
5466 * a upl commit is done. The caller
5467 * is obligated to preserve the
5468 * contents of all pages marked dirty
5470 upl
->flags
|= UPL_CLEAR_DIRTY
;
5472 dst_page
->vmp_dirty
= dirty
;
5475 dst_page
->vmp_precious
= TRUE
;
5477 if ( !VM_PAGE_WIRED(dst_page
)) {
5479 * deny access to the target page while
5480 * it is being worked on
5482 dst_page
->vmp_busy
= TRUE
;
5484 dwp
->dw_mask
|= DW_vm_page_wire
;
5487 * We might be about to satisfy a fault which has been
5488 * requested. So no need for the "restart" bit.
5490 dst_page
->vmp_restart
= FALSE
;
5491 if (!dst_page
->vmp_absent
&& !(cntrl_flags
& UPL_WILL_MODIFY
)) {
5493 * expect the page to be used
5495 dwp
->dw_mask
|= DW_set_reference
;
5497 if (cntrl_flags
& UPL_PRECIOUS
) {
5498 if (object
->internal
) {
5499 SET_PAGE_DIRTY(dst_page
, FALSE
);
5500 dst_page
->vmp_precious
= FALSE
;
5502 dst_page
->vmp_precious
= TRUE
;
5505 dst_page
->vmp_precious
= FALSE
;
5508 if (dst_page
->vmp_busy
)
5509 upl
->flags
|= UPL_HAS_BUSY
;
5511 if (phys_page
> upl
->highest_page
)
5512 upl
->highest_page
= phys_page
;
5513 assert (!pmap_is_noencrypt(phys_page
));
5514 if (user_page_list
) {
5515 user_page_list
[entry
].phys_addr
= phys_page
;
5516 user_page_list
[entry
].free_when_done
= dst_page
->vmp_free_when_done
;
5517 user_page_list
[entry
].absent
= dst_page
->vmp_absent
;
5518 user_page_list
[entry
].dirty
= dst_page
->vmp_dirty
;
5519 user_page_list
[entry
].precious
= dst_page
->vmp_precious
;
5520 user_page_list
[entry
].device
= FALSE
;
5521 user_page_list
[entry
].needed
= FALSE
;
5522 if (dst_page
->vmp_clustered
== TRUE
)
5523 user_page_list
[entry
].speculative
= (dst_page
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) ? TRUE
: FALSE
;
5525 user_page_list
[entry
].speculative
= FALSE
;
5526 user_page_list
[entry
].cs_validated
= dst_page
->vmp_cs_validated
;
5527 user_page_list
[entry
].cs_tainted
= dst_page
->vmp_cs_tainted
;
5528 user_page_list
[entry
].cs_nx
= dst_page
->vmp_cs_nx
;
5529 user_page_list
[entry
].mark
= FALSE
;
5532 * if UPL_RET_ONLY_ABSENT is set, then
5533 * we are working with a fresh page and we've
5534 * just set the clustered flag on it to
5535 * indicate that it was drug in as part of a
5536 * speculative cluster... so leave it alone
5538 if ( !(cntrl_flags
& UPL_RET_ONLY_ABSENT
)) {
5540 * someone is explicitly grabbing this page...
5541 * update clustered and speculative state
5544 if (dst_page
->vmp_clustered
)
5545 VM_PAGE_CONSUME_CLUSTERED(dst_page
);
5549 if (dwp
->dw_mask
& DW_vm_page_activate
)
5550 VM_STAT_INCR(reactivations
);
5552 VM_PAGE_ADD_DELAYED_WORK(dwp
, dst_page
, dw_count
);
5554 if (dw_count
>= dw_limit
) {
5555 vm_page_do_delayed_work(object
, tag
, &dw_array
[0], dw_count
);
5562 dst_offset
+= PAGE_SIZE_64
;
5563 xfer_size
-= PAGE_SIZE
;
5566 vm_page_do_delayed_work(object
, tag
, &dw_array
[0], dw_count
);
5568 if (alias_page
!= NULL
) {
5569 VM_PAGE_FREE(alias_page
);
5571 if (pmap_flushes_delayed
== TRUE
)
5572 pmap_flush(&pmap_flush_context_storage
);
5574 if (page_list_count
!= NULL
) {
5575 if (upl
->flags
& UPL_INTERNAL
)
5576 *page_list_count
= 0;
5577 else if (*page_list_count
> entry
)
5578 *page_list_count
= entry
;
5583 vm_object_unlock(object
);
5585 VM_DEBUG_CONSTANT_EVENT(vm_object_upl_request
, VM_UPL_REQUEST
, DBG_FUNC_END
, page_grab_count
, 0, 0, 0);
5587 return KERN_SUCCESS
;
5591 * Routine: vm_object_super_upl_request
5593 * Cause the population of a portion of a vm_object
5594 * in much the same way as memory_object_upl_request.
5595 * Depending on the nature of the request, the pages
5596 * returned may be contain valid data or be uninitialized.
5597 * However, the region may be expanded up to the super
5598 * cluster size provided.
5601 __private_extern__ kern_return_t
5602 vm_object_super_upl_request(
5604 vm_object_offset_t offset
,
5606 upl_size_t super_cluster
,
5608 upl_page_info_t
*user_page_list
,
5609 unsigned int *page_list_count
,
5610 upl_control_flags_t cntrl_flags
,
5613 if (object
->paging_offset
> offset
|| ((cntrl_flags
& UPL_VECTOR
)==UPL_VECTOR
))
5614 return KERN_FAILURE
;
5616 assert(object
->paging_in_progress
);
5617 offset
= offset
- object
->paging_offset
;
5619 if (super_cluster
> size
) {
5621 vm_object_offset_t base_offset
;
5622 upl_size_t super_size
;
5623 vm_object_size_t super_size_64
;
5625 base_offset
= (offset
& ~((vm_object_offset_t
) super_cluster
- 1));
5626 super_size
= (offset
+ size
) > (base_offset
+ super_cluster
) ? super_cluster
<<1 : super_cluster
;
5627 super_size_64
= ((base_offset
+ super_size
) > object
->vo_size
) ? (object
->vo_size
- base_offset
) : super_size
;
5628 super_size
= (upl_size_t
) super_size_64
;
5629 assert(super_size
== super_size_64
);
5631 if (offset
> (base_offset
+ super_size
)) {
5632 panic("vm_object_super_upl_request: Missed target pageout"
5633 " %#llx,%#llx, %#x, %#x, %#x, %#llx\n",
5634 offset
, base_offset
, super_size
, super_cluster
,
5635 size
, object
->paging_offset
);
5638 * apparently there is a case where the vm requests a
5639 * page to be written out who's offset is beyond the
5642 if ((offset
+ size
) > (base_offset
+ super_size
)) {
5643 super_size_64
= (offset
+ size
) - base_offset
;
5644 super_size
= (upl_size_t
) super_size_64
;
5645 assert(super_size
== super_size_64
);
5648 offset
= base_offset
;
5651 return vm_object_upl_request(object
, offset
, size
, upl
, user_page_list
, page_list_count
, cntrl_flags
, tag
);
5655 int cs_executable_create_upl
= 0;
5656 extern int proc_selfpid(void);
5657 extern char *proc_name_address(void *p
);
5658 #endif /* CONFIG_EMBEDDED */
5663 vm_map_address_t offset
,
5664 upl_size_t
*upl_size
,
5666 upl_page_info_array_t page_list
,
5667 unsigned int *count
,
5668 upl_control_flags_t
*flags
,
5671 vm_map_entry_t entry
;
5672 upl_control_flags_t caller_flags
;
5673 int force_data_sync
;
5675 vm_object_t local_object
;
5676 vm_map_offset_t local_offset
;
5677 vm_map_offset_t local_start
;
5680 assert(page_aligned(offset
));
5682 caller_flags
= *flags
;
5684 if (caller_flags
& ~UPL_VALID_FLAGS
) {
5686 * For forward compatibility's sake,
5687 * reject any unknown flag.
5689 return KERN_INVALID_VALUE
;
5691 force_data_sync
= (caller_flags
& UPL_FORCE_DATA_SYNC
);
5692 sync_cow_data
= !(caller_flags
& UPL_COPYOUT_FROM
);
5695 return KERN_INVALID_ARGUMENT
;
5698 vm_map_lock_read(map
);
5700 if (!vm_map_lookup_entry(map
, offset
, &entry
)) {
5701 vm_map_unlock_read(map
);
5702 return KERN_FAILURE
;
5705 if ((entry
->vme_end
- offset
) < *upl_size
) {
5706 *upl_size
= (upl_size_t
) (entry
->vme_end
- offset
);
5707 assert(*upl_size
== entry
->vme_end
- offset
);
5710 if (caller_flags
& UPL_QUERY_OBJECT_TYPE
) {
5713 if (!entry
->is_sub_map
&&
5714 VME_OBJECT(entry
) != VM_OBJECT_NULL
) {
5715 if (VME_OBJECT(entry
)->private)
5716 *flags
= UPL_DEV_MEMORY
;
5718 if (VME_OBJECT(entry
)->phys_contiguous
)
5719 *flags
|= UPL_PHYS_CONTIG
;
5721 vm_map_unlock_read(map
);
5722 return KERN_SUCCESS
;
5725 if (VME_OBJECT(entry
) == VM_OBJECT_NULL
||
5726 !VME_OBJECT(entry
)->phys_contiguous
) {
5727 if (*upl_size
> MAX_UPL_SIZE_BYTES
)
5728 *upl_size
= MAX_UPL_SIZE_BYTES
;
5732 * Create an object if necessary.
5734 if (VME_OBJECT(entry
) == VM_OBJECT_NULL
) {
5736 if (vm_map_lock_read_to_write(map
))
5737 goto REDISCOVER_ENTRY
;
5739 VME_OBJECT_SET(entry
,
5740 vm_object_allocate((vm_size_t
)
5742 entry
->vme_start
)));
5743 VME_OFFSET_SET(entry
, 0);
5744 assert(entry
->use_pmap
);
5746 vm_map_lock_write_to_read(map
);
5749 if (!(caller_flags
& UPL_COPYOUT_FROM
) &&
5750 !entry
->is_sub_map
&&
5751 !(entry
->protection
& VM_PROT_WRITE
)) {
5752 vm_map_unlock_read(map
);
5753 return KERN_PROTECTION_FAILURE
;
5757 if (map
->pmap
!= kernel_pmap
&&
5758 (caller_flags
& UPL_COPYOUT_FROM
) &&
5759 (entry
->protection
& VM_PROT_EXECUTE
) &&
5760 !(entry
->protection
& VM_PROT_WRITE
)) {
5765 * We're about to create a read-only UPL backed by
5766 * memory from an executable mapping.
5767 * Wiring the pages would result in the pages being copied
5768 * (due to the "MAP_PRIVATE" mapping) and no longer
5769 * code-signed, so no longer eligible for execution.
5770 * Instead, let's copy the data into a kernel buffer and
5771 * create the UPL from this kernel buffer.
5772 * The kernel buffer is then freed, leaving the UPL holding
5773 * the last reference on the VM object, so the memory will
5774 * be released when the UPL is committed.
5777 vm_map_unlock_read(map
);
5778 /* allocate kernel buffer */
5779 ksize
= round_page(*upl_size
);
5781 ret
= kmem_alloc_pageable(kernel_map
,
5785 if (ret
== KERN_SUCCESS
) {
5786 /* copyin the user data */
5787 assert(page_aligned(offset
));
5788 ret
= copyinmap(map
, offset
, (void *)kaddr
, *upl_size
);
5790 if (ret
== KERN_SUCCESS
) {
5791 if (ksize
> *upl_size
) {
5792 /* zero out the extra space in kernel buffer */
5793 memset((void *)(kaddr
+ *upl_size
),
5797 /* create the UPL from the kernel buffer */
5798 ret
= vm_map_create_upl(kernel_map
, kaddr
, upl_size
,
5799 upl
, page_list
, count
, flags
, tag
);
5802 /* free the kernel buffer */
5803 kmem_free(kernel_map
, kaddr
, ksize
);
5807 #if DEVELOPMENT || DEBUG
5808 DTRACE_VM4(create_upl_from_executable
,
5810 vm_map_address_t
, offset
,
5811 upl_size_t
, *upl_size
,
5812 kern_return_t
, ret
);
5813 #endif /* DEVELOPMENT || DEBUG */
5816 #endif /* CONFIG_EMBEDDED */
5818 local_object
= VME_OBJECT(entry
);
5819 assert(local_object
!= VM_OBJECT_NULL
);
5821 if (!entry
->is_sub_map
&&
5822 !entry
->needs_copy
&&
5824 local_object
->vo_size
> *upl_size
&& /* partial UPL */
5825 entry
->wired_count
== 0 && /* No COW for entries that are wired */
5826 (map
->pmap
!= kernel_pmap
) && /* alias checks */
5827 (vm_map_entry_should_cow_for_true_share(entry
) /* case 1 */
5830 local_object
->internal
&&
5831 (local_object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
) &&
5832 local_object
->ref_count
> 1))) {
5837 * Set up the targeted range for copy-on-write to avoid
5838 * applying true_share/copy_delay to the entire object.
5841 * This map entry covers only part of an internal
5842 * object. There could be other map entries covering
5843 * other areas of this object and some of these map
5844 * entries could be marked as "needs_copy", which
5845 * assumes that the object is COPY_SYMMETRIC.
5846 * To avoid marking this object as COPY_DELAY and
5847 * "true_share", let's shadow it and mark the new
5848 * (smaller) object as "true_share" and COPY_DELAY.
5851 if (vm_map_lock_read_to_write(map
)) {
5852 goto REDISCOVER_ENTRY
;
5854 vm_map_lock_assert_exclusive(map
);
5855 assert(VME_OBJECT(entry
) == local_object
);
5857 vm_map_clip_start(map
,
5859 vm_map_trunc_page(offset
,
5860 VM_MAP_PAGE_MASK(map
)));
5861 vm_map_clip_end(map
,
5863 vm_map_round_page(offset
+ *upl_size
,
5864 VM_MAP_PAGE_MASK(map
)));
5865 if ((entry
->vme_end
- offset
) < *upl_size
) {
5866 *upl_size
= (upl_size_t
) (entry
->vme_end
- offset
);
5867 assert(*upl_size
== entry
->vme_end
- offset
);
5870 prot
= entry
->protection
& ~VM_PROT_WRITE
;
5871 if (override_nx(map
, VME_ALIAS(entry
)) && prot
)
5872 prot
|= VM_PROT_EXECUTE
;
5873 vm_object_pmap_protect(local_object
,
5875 entry
->vme_end
- entry
->vme_start
,
5876 ((entry
->is_shared
||
5877 map
->mapped_in_other_pmaps
)
5883 assert(entry
->wired_count
== 0);
5886 * Lock the VM object and re-check its status: if it's mapped
5887 * in another address space, we could still be racing with
5888 * another thread holding that other VM map exclusively.
5890 vm_object_lock(local_object
);
5891 if (local_object
->true_share
) {
5892 /* object is already in proper state: no COW needed */
5893 assert(local_object
->copy_strategy
!=
5894 MEMORY_OBJECT_COPY_SYMMETRIC
);
5896 /* not true_share: ask for copy-on-write below */
5897 assert(local_object
->copy_strategy
==
5898 MEMORY_OBJECT_COPY_SYMMETRIC
);
5899 entry
->needs_copy
= TRUE
;
5901 vm_object_unlock(local_object
);
5903 vm_map_lock_write_to_read(map
);
5906 if (entry
->needs_copy
) {
5908 * Honor copy-on-write for COPY_SYMMETRIC
5913 vm_object_offset_t new_offset
;
5916 vm_map_version_t version
;
5918 vm_prot_t fault_type
;
5922 if (caller_flags
& UPL_COPYOUT_FROM
) {
5923 fault_type
= VM_PROT_READ
| VM_PROT_COPY
;
5924 vm_counters
.create_upl_extra_cow
++;
5925 vm_counters
.create_upl_extra_cow_pages
+=
5926 (entry
->vme_end
- entry
->vme_start
) / PAGE_SIZE
;
5928 fault_type
= VM_PROT_WRITE
;
5930 if (vm_map_lookup_locked(&local_map
,
5932 OBJECT_LOCK_EXCLUSIVE
,
5934 &new_offset
, &prot
, &wired
,
5936 &real_map
) != KERN_SUCCESS
) {
5937 if (fault_type
== VM_PROT_WRITE
) {
5938 vm_counters
.create_upl_lookup_failure_write
++;
5940 vm_counters
.create_upl_lookup_failure_copy
++;
5942 vm_map_unlock_read(local_map
);
5943 return KERN_FAILURE
;
5945 if (real_map
!= map
)
5946 vm_map_unlock(real_map
);
5947 vm_map_unlock_read(local_map
);
5949 vm_object_unlock(object
);
5951 goto REDISCOVER_ENTRY
;
5954 if (entry
->is_sub_map
) {
5957 submap
= VME_SUBMAP(entry
);
5958 local_start
= entry
->vme_start
;
5959 local_offset
= VME_OFFSET(entry
);
5961 vm_map_reference(submap
);
5962 vm_map_unlock_read(map
);
5964 ret
= vm_map_create_upl(submap
,
5965 local_offset
+ (offset
- local_start
),
5966 upl_size
, upl
, page_list
, count
, flags
, tag
);
5967 vm_map_deallocate(submap
);
5972 if (sync_cow_data
&&
5973 (VME_OBJECT(entry
)->shadow
||
5974 VME_OBJECT(entry
)->copy
)) {
5975 local_object
= VME_OBJECT(entry
);
5976 local_start
= entry
->vme_start
;
5977 local_offset
= VME_OFFSET(entry
);
5979 vm_object_reference(local_object
);
5980 vm_map_unlock_read(map
);
5982 if (local_object
->shadow
&& local_object
->copy
) {
5983 vm_object_lock_request(local_object
->shadow
,
5984 ((vm_object_offset_t
)
5985 ((offset
- local_start
) +
5987 local_object
->vo_shadow_offset
),
5989 MEMORY_OBJECT_DATA_SYNC
,
5992 sync_cow_data
= FALSE
;
5993 vm_object_deallocate(local_object
);
5995 goto REDISCOVER_ENTRY
;
5997 if (force_data_sync
) {
5998 local_object
= VME_OBJECT(entry
);
5999 local_start
= entry
->vme_start
;
6000 local_offset
= VME_OFFSET(entry
);
6002 vm_object_reference(local_object
);
6003 vm_map_unlock_read(map
);
6005 vm_object_lock_request(local_object
,
6006 ((vm_object_offset_t
)
6007 ((offset
- local_start
) +
6009 (vm_object_size_t
)*upl_size
,
6011 MEMORY_OBJECT_DATA_SYNC
,
6014 force_data_sync
= FALSE
;
6015 vm_object_deallocate(local_object
);
6017 goto REDISCOVER_ENTRY
;
6019 if (VME_OBJECT(entry
)->private)
6020 *flags
= UPL_DEV_MEMORY
;
6024 if (VME_OBJECT(entry
)->phys_contiguous
)
6025 *flags
|= UPL_PHYS_CONTIG
;
6027 local_object
= VME_OBJECT(entry
);
6028 local_offset
= VME_OFFSET(entry
);
6029 local_start
= entry
->vme_start
;
6033 * Wiring will copy the pages to the shadow object.
6034 * The shadow object will not be code-signed so
6035 * attempting to execute code from these copied pages
6036 * would trigger a code-signing violation.
6038 if (entry
->protection
& VM_PROT_EXECUTE
) {
6040 printf("pid %d[%s] create_upl out of executable range from "
6041 "0x%llx to 0x%llx: side effects may include "
6042 "code-signing violations later on\n",
6044 (current_task()->bsd_info
6045 ? proc_name_address(current_task()->bsd_info
)
6047 (uint64_t) entry
->vme_start
,
6048 (uint64_t) entry
->vme_end
);
6049 #endif /* MACH_ASSERT */
6050 DTRACE_VM2(cs_executable_create_upl
,
6051 uint64_t, (uint64_t)entry
->vme_start
,
6052 uint64_t, (uint64_t)entry
->vme_end
);
6053 cs_executable_create_upl
++;
6055 #endif /* CONFIG_EMBEDDED */
6057 vm_object_lock(local_object
);
6060 * Ensure that this object is "true_share" and "copy_delay" now,
6061 * while we're still holding the VM map lock. After we unlock the map,
6062 * anything could happen to that mapping, including some copy-on-write
6063 * activity. We need to make sure that the IOPL will point at the
6064 * same memory as the mapping.
6066 if (local_object
->true_share
) {
6067 assert(local_object
->copy_strategy
!=
6068 MEMORY_OBJECT_COPY_SYMMETRIC
);
6069 } else if (local_object
!= kernel_object
&&
6070 local_object
!= compressor_object
&&
6071 !local_object
->phys_contiguous
) {
6072 #if VM_OBJECT_TRACKING_OP_TRUESHARE
6073 if (!local_object
->true_share
&&
6074 vm_object_tracking_inited
) {
6075 void *bt
[VM_OBJECT_TRACKING_BTDEPTH
];
6077 num
= OSBacktrace(bt
,
6078 VM_OBJECT_TRACKING_BTDEPTH
);
6079 btlog_add_entry(vm_object_tracking_btlog
,
6081 VM_OBJECT_TRACKING_OP_TRUESHARE
,
6085 #endif /* VM_OBJECT_TRACKING_OP_TRUESHARE */
6086 local_object
->true_share
= TRUE
;
6087 if (local_object
->copy_strategy
==
6088 MEMORY_OBJECT_COPY_SYMMETRIC
) {
6089 local_object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
6093 vm_object_reference_locked(local_object
);
6094 vm_object_unlock(local_object
);
6096 vm_map_unlock_read(map
);
6098 ret
= vm_object_iopl_request(local_object
,
6099 ((vm_object_offset_t
)
6100 ((offset
- local_start
) + local_offset
)),
6107 vm_object_deallocate(local_object
);
6113 * Internal routine to enter a UPL into a VM map.
6115 * JMM - This should just be doable through the standard
6116 * vm_map_enter() API.
6122 vm_map_offset_t
*dst_addr
)
6125 vm_object_offset_t offset
;
6126 vm_map_offset_t addr
;
6129 int isVectorUPL
= 0, curr_upl
=0;
6130 upl_t vector_upl
= NULL
;
6131 vm_offset_t vector_upl_dst_addr
= 0;
6132 vm_map_t vector_upl_submap
= NULL
;
6133 upl_offset_t subupl_offset
= 0;
6134 upl_size_t subupl_size
= 0;
6136 if (upl
== UPL_NULL
)
6137 return KERN_INVALID_ARGUMENT
;
6139 if((isVectorUPL
= vector_upl_is_valid(upl
))) {
6140 int mapped
=0,valid_upls
=0;
6143 upl_lock(vector_upl
);
6144 for(curr_upl
=0; curr_upl
< MAX_VECTOR_UPL_ELEMENTS
; curr_upl
++) {
6145 upl
= vector_upl_subupl_byindex(vector_upl
, curr_upl
);
6149 if (UPL_PAGE_LIST_MAPPED
& upl
->flags
)
6154 if(mapped
!= valid_upls
)
6155 panic("Only %d of the %d sub-upls within the Vector UPL are alread mapped\n", mapped
, valid_upls
);
6157 upl_unlock(vector_upl
);
6158 return KERN_FAILURE
;
6162 kr
= kmem_suballoc(map
, &vector_upl_dst_addr
, vector_upl
->size
, FALSE
,
6163 VM_FLAGS_ANYWHERE
, VM_MAP_KERNEL_FLAGS_NONE
, VM_KERN_MEMORY_NONE
,
6164 &vector_upl_submap
);
6165 if( kr
!= KERN_SUCCESS
)
6166 panic("Vector UPL submap allocation failed\n");
6167 map
= vector_upl_submap
;
6168 vector_upl_set_submap(vector_upl
, vector_upl_submap
, vector_upl_dst_addr
);
6174 process_upl_to_enter
:
6176 if(curr_upl
== MAX_VECTOR_UPL_ELEMENTS
) {
6177 *dst_addr
= vector_upl_dst_addr
;
6178 upl_unlock(vector_upl
);
6179 return KERN_SUCCESS
;
6181 upl
= vector_upl_subupl_byindex(vector_upl
, curr_upl
++ );
6183 goto process_upl_to_enter
;
6185 vector_upl_get_iostate(vector_upl
, upl
, &subupl_offset
, &subupl_size
);
6186 *dst_addr
= (vm_map_offset_t
)(vector_upl_dst_addr
+ (vm_map_offset_t
)subupl_offset
);
6189 * check to see if already mapped
6191 if (UPL_PAGE_LIST_MAPPED
& upl
->flags
) {
6193 return KERN_FAILURE
;
6196 if ((!(upl
->flags
& UPL_SHADOWED
)) &&
6197 ((upl
->flags
& UPL_HAS_BUSY
) ||
6198 !((upl
->flags
& (UPL_DEVICE_MEMORY
| UPL_IO_WIRE
)) || (upl
->map_object
->phys_contiguous
)))) {
6201 vm_page_t alias_page
;
6202 vm_object_offset_t new_offset
;
6203 unsigned int pg_num
;
6204 wpl_array_t lite_list
;
6206 if (upl
->flags
& UPL_INTERNAL
) {
6207 lite_list
= (wpl_array_t
)
6208 ((((uintptr_t)upl
) + sizeof(struct upl
))
6209 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
6211 lite_list
= (wpl_array_t
)(((uintptr_t)upl
) + sizeof(struct upl
));
6213 object
= upl
->map_object
;
6214 upl
->map_object
= vm_object_allocate(upl
->size
);
6216 vm_object_lock(upl
->map_object
);
6218 upl
->map_object
->shadow
= object
;
6219 upl
->map_object
->pageout
= TRUE
;
6220 upl
->map_object
->can_persist
= FALSE
;
6221 upl
->map_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
6222 upl
->map_object
->vo_shadow_offset
= upl
->offset
- object
->paging_offset
;
6223 upl
->map_object
->wimg_bits
= object
->wimg_bits
;
6224 offset
= upl
->map_object
->vo_shadow_offset
;
6228 upl
->flags
|= UPL_SHADOWED
;
6231 pg_num
= (unsigned int) (new_offset
/ PAGE_SIZE
);
6232 assert(pg_num
== new_offset
/ PAGE_SIZE
);
6234 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
6236 VM_PAGE_GRAB_FICTITIOUS(alias_page
);
6238 vm_object_lock(object
);
6240 m
= vm_page_lookup(object
, offset
);
6241 if (m
== VM_PAGE_NULL
) {
6242 panic("vm_upl_map: page missing\n");
6246 * Convert the fictitious page to a private
6247 * shadow of the real page.
6249 assert(alias_page
->vmp_fictitious
);
6250 alias_page
->vmp_fictitious
= FALSE
;
6251 alias_page
->vmp_private
= TRUE
;
6252 alias_page
->vmp_free_when_done
= TRUE
;
6254 * since m is a page in the upl it must
6255 * already be wired or BUSY, so it's
6256 * safe to assign the underlying physical
6259 VM_PAGE_SET_PHYS_PAGE(alias_page
, VM_PAGE_GET_PHYS_PAGE(m
));
6261 vm_object_unlock(object
);
6263 vm_page_lockspin_queues();
6264 vm_page_wire(alias_page
, VM_KERN_MEMORY_NONE
, TRUE
);
6265 vm_page_unlock_queues();
6267 vm_page_insert_wired(alias_page
, upl
->map_object
, new_offset
, VM_KERN_MEMORY_NONE
);
6269 assert(!alias_page
->vmp_wanted
);
6270 alias_page
->vmp_busy
= FALSE
;
6271 alias_page
->vmp_absent
= FALSE
;
6274 offset
+= PAGE_SIZE_64
;
6275 new_offset
+= PAGE_SIZE_64
;
6277 vm_object_unlock(upl
->map_object
);
6279 if (upl
->flags
& UPL_SHADOWED
)
6282 offset
= upl
->offset
- upl
->map_object
->paging_offset
;
6286 vm_object_reference(upl
->map_object
);
6291 * NEED A UPL_MAP ALIAS
6293 kr
= vm_map_enter(map
, dst_addr
, (vm_map_size_t
)size
, (vm_map_offset_t
) 0,
6294 VM_FLAGS_ANYWHERE
, VM_MAP_KERNEL_FLAGS_NONE
, VM_KERN_MEMORY_OSFMK
,
6295 upl
->map_object
, offset
, FALSE
,
6296 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
6298 if (kr
!= KERN_SUCCESS
) {
6299 vm_object_deallocate(upl
->map_object
);
6305 kr
= vm_map_enter(map
, dst_addr
, (vm_map_size_t
)size
, (vm_map_offset_t
) 0,
6306 VM_FLAGS_FIXED
, VM_MAP_KERNEL_FLAGS_NONE
, VM_KERN_MEMORY_OSFMK
,
6307 upl
->map_object
, offset
, FALSE
,
6308 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
6310 panic("vm_map_enter failed for a Vector UPL\n");
6312 vm_object_lock(upl
->map_object
);
6314 for (addr
= *dst_addr
; size
> 0; size
-= PAGE_SIZE
, addr
+= PAGE_SIZE
) {
6315 m
= vm_page_lookup(upl
->map_object
, offset
);
6318 m
->vmp_pmapped
= TRUE
;
6320 /* CODE SIGNING ENFORCEMENT: page has been wpmapped,
6321 * but only in kernel space. If this was on a user map,
6322 * we'd have to set the wpmapped bit. */
6323 /* m->vmp_wpmapped = TRUE; */
6324 assert(map
->pmap
== kernel_pmap
);
6326 PMAP_ENTER(map
->pmap
, addr
, m
, VM_PROT_DEFAULT
, VM_PROT_NONE
, 0, TRUE
, kr
);
6328 assert(kr
== KERN_SUCCESS
);
6330 kasan_notify_address(addr
, PAGE_SIZE_64
);
6333 offset
+= PAGE_SIZE_64
;
6335 vm_object_unlock(upl
->map_object
);
6338 * hold a reference for the mapping
6341 upl
->flags
|= UPL_PAGE_LIST_MAPPED
;
6342 upl
->kaddr
= (vm_offset_t
) *dst_addr
;
6343 assert(upl
->kaddr
== *dst_addr
);
6346 goto process_upl_to_enter
;
6350 return KERN_SUCCESS
;
6354 * Internal routine to remove a UPL mapping from a VM map.
6356 * XXX - This should just be doable through a standard
6357 * vm_map_remove() operation. Otherwise, implicit clean-up
6358 * of the target map won't be able to correctly remove
6359 * these (and release the reference on the UPL). Having
6360 * to do this means we can't map these into user-space
6370 int isVectorUPL
= 0, curr_upl
= 0;
6371 upl_t vector_upl
= NULL
;
6373 if (upl
== UPL_NULL
)
6374 return KERN_INVALID_ARGUMENT
;
6376 if((isVectorUPL
= vector_upl_is_valid(upl
))) {
6377 int unmapped
=0, valid_upls
=0;
6379 upl_lock(vector_upl
);
6380 for(curr_upl
=0; curr_upl
< MAX_VECTOR_UPL_ELEMENTS
; curr_upl
++) {
6381 upl
= vector_upl_subupl_byindex(vector_upl
, curr_upl
);
6385 if (!(UPL_PAGE_LIST_MAPPED
& upl
->flags
))
6390 if(unmapped
!= valid_upls
)
6391 panic("%d of the %d sub-upls within the Vector UPL is/are not mapped\n", unmapped
, valid_upls
);
6393 upl_unlock(vector_upl
);
6394 return KERN_FAILURE
;
6402 process_upl_to_remove
:
6404 if(curr_upl
== MAX_VECTOR_UPL_ELEMENTS
) {
6405 vm_map_t v_upl_submap
;
6406 vm_offset_t v_upl_submap_dst_addr
;
6407 vector_upl_get_submap(vector_upl
, &v_upl_submap
, &v_upl_submap_dst_addr
);
6409 vm_map_remove(map
, v_upl_submap_dst_addr
, v_upl_submap_dst_addr
+ vector_upl
->size
, VM_MAP_REMOVE_NO_FLAGS
);
6410 vm_map_deallocate(v_upl_submap
);
6411 upl_unlock(vector_upl
);
6412 return KERN_SUCCESS
;
6415 upl
= vector_upl_subupl_byindex(vector_upl
, curr_upl
++ );
6417 goto process_upl_to_remove
;
6420 if (upl
->flags
& UPL_PAGE_LIST_MAPPED
) {
6424 assert(upl
->ref_count
> 1);
6425 upl
->ref_count
--; /* removing mapping ref */
6427 upl
->flags
&= ~UPL_PAGE_LIST_MAPPED
;
6428 upl
->kaddr
= (vm_offset_t
) 0;
6435 vm_map_trunc_page(addr
,
6436 VM_MAP_PAGE_MASK(map
)),
6437 vm_map_round_page(addr
+ size
,
6438 VM_MAP_PAGE_MASK(map
)),
6439 VM_MAP_REMOVE_NO_FLAGS
);
6440 return KERN_SUCCESS
;
6444 * If it's a Vectored UPL, we'll be removing the entire
6445 * submap anyways, so no need to remove individual UPL
6446 * element mappings from within the submap
6448 goto process_upl_to_remove
;
6453 return KERN_FAILURE
;
6460 upl_offset_t offset
,
6463 upl_page_info_t
*page_list
,
6464 mach_msg_type_number_t count
,
6467 upl_size_t xfer_size
, subupl_size
= size
;
6468 vm_object_t shadow_object
;
6470 vm_object_t m_object
;
6471 vm_object_offset_t target_offset
;
6472 upl_offset_t subupl_offset
= offset
;
6474 wpl_array_t lite_list
;
6476 int clear_refmod
= 0;
6477 int pgpgout_count
= 0;
6478 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
6479 struct vm_page_delayed_work
*dwp
;
6482 int isVectorUPL
= 0;
6483 upl_t vector_upl
= NULL
;
6484 boolean_t should_be_throttled
= FALSE
;
6486 vm_page_t nxt_page
= VM_PAGE_NULL
;
6487 int fast_path_possible
= 0;
6488 int fast_path_full_commit
= 0;
6489 int throttle_page
= 0;
6490 int unwired_count
= 0;
6491 int local_queue_count
= 0;
6492 vm_page_t first_local
, last_local
;
6496 if (upl
== UPL_NULL
)
6497 return KERN_INVALID_ARGUMENT
;
6502 if((isVectorUPL
= vector_upl_is_valid(upl
))) {
6504 upl_lock(vector_upl
);
6509 process_upl_to_commit
:
6513 offset
= subupl_offset
;
6515 upl_unlock(vector_upl
);
6516 return KERN_SUCCESS
;
6518 upl
= vector_upl_subupl_byoffset(vector_upl
, &offset
, &size
);
6520 upl_unlock(vector_upl
);
6521 return KERN_FAILURE
;
6523 page_list
= UPL_GET_INTERNAL_PAGE_LIST_SIMPLE(upl
);
6524 subupl_size
-= size
;
6525 subupl_offset
+= size
;
6529 if (upl
->upl_commit_index
< UPL_DEBUG_COMMIT_RECORDS
) {
6530 (void) OSBacktrace(&upl
->upl_commit_records
[upl
->upl_commit_index
].c_retaddr
[0], UPL_DEBUG_STACK_FRAMES
);
6532 upl
->upl_commit_records
[upl
->upl_commit_index
].c_beg
= offset
;
6533 upl
->upl_commit_records
[upl
->upl_commit_index
].c_end
= (offset
+ size
);
6535 upl
->upl_commit_index
++;
6538 if (upl
->flags
& UPL_DEVICE_MEMORY
)
6540 else if ((offset
+ size
) <= upl
->size
)
6546 upl_unlock(vector_upl
);
6548 return KERN_FAILURE
;
6550 if (upl
->flags
& UPL_SET_DIRTY
)
6551 flags
|= UPL_COMMIT_SET_DIRTY
;
6552 if (upl
->flags
& UPL_CLEAR_DIRTY
)
6553 flags
|= UPL_COMMIT_CLEAR_DIRTY
;
6555 if (upl
->flags
& UPL_INTERNAL
)
6556 lite_list
= (wpl_array_t
) ((((uintptr_t)upl
) + sizeof(struct upl
))
6557 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
6559 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
6561 object
= upl
->map_object
;
6563 if (upl
->flags
& UPL_SHADOWED
) {
6564 vm_object_lock(object
);
6565 shadow_object
= object
->shadow
;
6567 shadow_object
= object
;
6569 entry
= offset
/PAGE_SIZE
;
6570 target_offset
= (vm_object_offset_t
)offset
;
6572 assert(!(target_offset
& PAGE_MASK
));
6573 assert(!(xfer_size
& PAGE_MASK
));
6575 if (upl
->flags
& UPL_KERNEL_OBJECT
)
6576 vm_object_lock_shared(shadow_object
);
6578 vm_object_lock(shadow_object
);
6580 VM_OBJECT_WIRED_PAGE_UPDATE_START(shadow_object
);
6582 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
6583 assert(shadow_object
->blocked_access
);
6584 shadow_object
->blocked_access
= FALSE
;
6585 vm_object_wakeup(object
, VM_OBJECT_EVENT_UNBLOCKED
);
6588 if (shadow_object
->code_signed
) {
6591 * If the object is code-signed, do not let this UPL tell
6592 * us if the pages are valid or not. Let the pages be
6593 * validated by VM the normal way (when they get mapped or
6596 flags
&= ~UPL_COMMIT_CS_VALIDATED
;
6600 * No page list to get the code-signing info from !?
6602 flags
&= ~UPL_COMMIT_CS_VALIDATED
;
6604 if (!VM_DYNAMIC_PAGING_ENABLED() && shadow_object
->internal
)
6605 should_be_throttled
= TRUE
;
6609 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
6611 if ((upl
->flags
& UPL_IO_WIRE
) &&
6612 !(flags
& UPL_COMMIT_FREE_ABSENT
) &&
6614 shadow_object
->purgable
!= VM_PURGABLE_VOLATILE
&&
6615 shadow_object
->purgable
!= VM_PURGABLE_EMPTY
) {
6617 if (!vm_page_queue_empty(&shadow_object
->memq
)) {
6619 if (size
== shadow_object
->vo_size
) {
6620 nxt_page
= (vm_page_t
)vm_page_queue_first(&shadow_object
->memq
);
6621 fast_path_full_commit
= 1;
6623 fast_path_possible
= 1;
6625 if (!VM_DYNAMIC_PAGING_ENABLED() && shadow_object
->internal
&&
6626 (shadow_object
->purgable
== VM_PURGABLE_DENY
||
6627 shadow_object
->purgable
== VM_PURGABLE_NONVOLATILE
||
6628 shadow_object
->purgable
== VM_PURGABLE_VOLATILE
)) {
6633 first_local
= VM_PAGE_NULL
;
6634 last_local
= VM_PAGE_NULL
;
6644 if (upl
->flags
& UPL_LITE
) {
6645 unsigned int pg_num
;
6647 if (nxt_page
!= VM_PAGE_NULL
) {
6649 nxt_page
= (vm_page_t
)vm_page_queue_next(&nxt_page
->vmp_listq
);
6650 target_offset
= m
->vmp_offset
;
6652 pg_num
= (unsigned int) (target_offset
/PAGE_SIZE
);
6653 assert(pg_num
== target_offset
/PAGE_SIZE
);
6655 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
6656 lite_list
[pg_num
>>5] &= ~(1 << (pg_num
& 31));
6658 if (!(upl
->flags
& UPL_KERNEL_OBJECT
) && m
== VM_PAGE_NULL
)
6659 m
= vm_page_lookup(shadow_object
, target_offset
+ (upl
->offset
- shadow_object
->paging_offset
));
6663 if (upl
->flags
& UPL_SHADOWED
) {
6664 if ((t
= vm_page_lookup(object
, target_offset
)) != VM_PAGE_NULL
) {
6666 t
->vmp_free_when_done
= FALSE
;
6670 if (!(upl
->flags
& UPL_KERNEL_OBJECT
) && m
== VM_PAGE_NULL
)
6671 m
= vm_page_lookup(shadow_object
, target_offset
+ object
->vo_shadow_offset
);
6674 if (m
== VM_PAGE_NULL
)
6675 goto commit_next_page
;
6677 m_object
= VM_PAGE_OBJECT(m
);
6679 if (m
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) {
6680 assert(m
->vmp_busy
);
6682 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
6683 goto commit_next_page
;
6686 if (flags
& UPL_COMMIT_CS_VALIDATED
) {
6689 * Set the code signing bits according to
6690 * what the UPL says they should be.
6692 m
->vmp_cs_validated
= page_list
[entry
].cs_validated
;
6693 m
->vmp_cs_tainted
= page_list
[entry
].cs_tainted
;
6694 m
->vmp_cs_nx
= page_list
[entry
].cs_nx
;
6696 if (flags
& UPL_COMMIT_WRITTEN_BY_KERNEL
)
6697 m
->vmp_written_by_kernel
= TRUE
;
6699 if (upl
->flags
& UPL_IO_WIRE
) {
6702 page_list
[entry
].phys_addr
= 0;
6704 if (flags
& UPL_COMMIT_SET_DIRTY
) {
6705 SET_PAGE_DIRTY(m
, FALSE
);
6706 } else if (flags
& UPL_COMMIT_CLEAR_DIRTY
) {
6707 m
->vmp_dirty
= FALSE
;
6709 if (! (flags
& UPL_COMMIT_CS_VALIDATED
) &&
6710 m
->vmp_cs_validated
&& !m
->vmp_cs_tainted
) {
6713 * This page is no longer dirty
6714 * but could have been modified,
6715 * so it will need to be
6718 m
->vmp_cs_validated
= FALSE
;
6720 VM_PAGEOUT_DEBUG(vm_cs_validated_resets
, 1);
6722 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
6724 clear_refmod
|= VM_MEM_MODIFIED
;
6726 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
6728 * We blocked access to the pages in this UPL.
6729 * Clear the "busy" bit and wake up any waiter
6732 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
6734 if (fast_path_possible
) {
6735 assert(m_object
->purgable
!= VM_PURGABLE_EMPTY
);
6736 assert(m_object
->purgable
!= VM_PURGABLE_VOLATILE
);
6737 if (m
->vmp_absent
) {
6738 assert(m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
);
6739 assert(m
->vmp_wire_count
== 0);
6740 assert(m
->vmp_busy
);
6742 m
->vmp_absent
= FALSE
;
6743 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
6745 if (m
->vmp_wire_count
== 0)
6746 panic("wire_count == 0, m = %p, obj = %p\n", m
, shadow_object
);
6747 assert(m
->vmp_q_state
== VM_PAGE_IS_WIRED
);
6750 * XXX FBDP need to update some other
6751 * counters here (purgeable_wired_count)
6754 assert(m
->vmp_wire_count
> 0);
6755 m
->vmp_wire_count
--;
6757 if (m
->vmp_wire_count
== 0) {
6758 m
->vmp_q_state
= VM_PAGE_NOT_ON_Q
;
6762 if (m
->vmp_wire_count
== 0) {
6763 assert(m
->vmp_pageq
.next
== 0 && m
->vmp_pageq
.prev
== 0);
6765 if (last_local
== VM_PAGE_NULL
) {
6766 assert(first_local
== VM_PAGE_NULL
);
6771 assert(first_local
!= VM_PAGE_NULL
);
6773 m
->vmp_pageq
.next
= VM_PAGE_CONVERT_TO_QUEUE_ENTRY(first_local
);
6774 first_local
->vmp_pageq
.prev
= VM_PAGE_CONVERT_TO_QUEUE_ENTRY(m
);
6777 local_queue_count
++;
6779 if (throttle_page
) {
6780 m
->vmp_q_state
= VM_PAGE_ON_THROTTLED_Q
;
6782 if (flags
& UPL_COMMIT_INACTIVATE
) {
6783 if (shadow_object
->internal
)
6784 m
->vmp_q_state
= VM_PAGE_ON_INACTIVE_INTERNAL_Q
;
6786 m
->vmp_q_state
= VM_PAGE_ON_INACTIVE_EXTERNAL_Q
;
6788 m
->vmp_q_state
= VM_PAGE_ON_ACTIVE_Q
;
6792 if (flags
& UPL_COMMIT_INACTIVATE
) {
6793 dwp
->dw_mask
|= DW_vm_page_deactivate_internal
;
6794 clear_refmod
|= VM_MEM_REFERENCED
;
6796 if (m
->vmp_absent
) {
6797 if (flags
& UPL_COMMIT_FREE_ABSENT
)
6798 dwp
->dw_mask
|= DW_vm_page_free
;
6800 m
->vmp_absent
= FALSE
;
6801 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
6803 if ( !(dwp
->dw_mask
& DW_vm_page_deactivate_internal
))
6804 dwp
->dw_mask
|= DW_vm_page_activate
;
6807 dwp
->dw_mask
|= DW_vm_page_unwire
;
6809 goto commit_next_page
;
6811 assert(m
->vmp_q_state
!= VM_PAGE_USED_BY_COMPRESSOR
);
6814 page_list
[entry
].phys_addr
= 0;
6817 * make sure to clear the hardware
6818 * modify or reference bits before
6819 * releasing the BUSY bit on this page
6820 * otherwise we risk losing a legitimate
6823 if (flags
& UPL_COMMIT_CLEAR_DIRTY
) {
6824 m
->vmp_dirty
= FALSE
;
6826 clear_refmod
|= VM_MEM_MODIFIED
;
6829 dwp
->dw_mask
|= DW_vm_pageout_throttle_up
;
6831 if (VM_PAGE_WIRED(m
))
6832 m
->vmp_free_when_done
= FALSE
;
6834 if (! (flags
& UPL_COMMIT_CS_VALIDATED
) &&
6835 m
->vmp_cs_validated
&& !m
->vmp_cs_tainted
) {
6838 * This page is no longer dirty
6839 * but could have been modified,
6840 * so it will need to be
6843 m
->vmp_cs_validated
= FALSE
;
6845 VM_PAGEOUT_DEBUG(vm_cs_validated_resets
, 1);
6847 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
6849 if (m
->vmp_overwriting
) {
6851 * the (COPY_OUT_FROM == FALSE) request_page_list case
6854 #if CONFIG_PHANTOM_CACHE
6855 if (m
->vmp_absent
&& !m_object
->internal
)
6856 dwp
->dw_mask
|= DW_vm_phantom_cache_update
;
6858 m
->vmp_absent
= FALSE
;
6860 dwp
->dw_mask
|= DW_clear_busy
;
6863 * alternate (COPY_OUT_FROM == FALSE) page_list case
6864 * Occurs when the original page was wired
6865 * at the time of the list request
6867 assert(VM_PAGE_WIRED(m
));
6869 dwp
->dw_mask
|= DW_vm_page_unwire
; /* reactivates */
6871 m
->vmp_overwriting
= FALSE
;
6873 m
->vmp_cleaning
= FALSE
;
6875 if (m
->vmp_free_when_done
) {
6877 * With the clean queue enabled, UPL_PAGEOUT should
6878 * no longer set the pageout bit. It's pages now go
6879 * to the clean queue.
6881 assert(!(flags
& UPL_PAGEOUT
));
6882 assert(!m_object
->internal
);
6884 m
->vmp_free_when_done
= FALSE
;
6886 if ((flags
& UPL_COMMIT_SET_DIRTY
) ||
6887 (m
->vmp_pmapped
&& (pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
)) & VM_MEM_MODIFIED
))) {
6889 * page was re-dirtied after we started
6890 * the pageout... reactivate it since
6891 * we don't know whether the on-disk
6892 * copy matches what is now in memory
6894 SET_PAGE_DIRTY(m
, FALSE
);
6896 dwp
->dw_mask
|= DW_vm_page_activate
| DW_PAGE_WAKEUP
;
6898 if (upl
->flags
& UPL_PAGEOUT
) {
6899 VM_STAT_INCR(reactivations
);
6900 DTRACE_VM2(pgrec
, int, 1, (uint64_t *), NULL
);
6904 * page has been successfully cleaned
6905 * go ahead and free it for other use
6907 if (m_object
->internal
) {
6908 DTRACE_VM2(anonpgout
, int, 1, (uint64_t *), NULL
);
6910 DTRACE_VM2(fspgout
, int, 1, (uint64_t *), NULL
);
6912 m
->vmp_dirty
= FALSE
;
6915 dwp
->dw_mask
|= DW_vm_page_free
;
6917 goto commit_next_page
;
6920 * It is a part of the semantic of COPYOUT_FROM
6921 * UPLs that a commit implies cache sync
6922 * between the vm page and the backing store
6923 * this can be used to strip the precious bit
6926 if ((upl
->flags
& UPL_PAGE_SYNC_DONE
) || (flags
& UPL_COMMIT_CLEAR_PRECIOUS
))
6927 m
->vmp_precious
= FALSE
;
6929 if (flags
& UPL_COMMIT_SET_DIRTY
) {
6930 SET_PAGE_DIRTY(m
, FALSE
);
6932 m
->vmp_dirty
= FALSE
;
6935 /* with the clean queue on, move *all* cleaned pages to the clean queue */
6936 if (hibernate_cleaning_in_progress
== FALSE
&& !m
->vmp_dirty
&& (upl
->flags
& UPL_PAGEOUT
)) {
6939 VM_STAT_INCR(pageouts
);
6940 DTRACE_VM2(pgout
, int, 1, (uint64_t *), NULL
);
6942 dwp
->dw_mask
|= DW_enqueue_cleaned
;
6943 } else if (should_be_throttled
== TRUE
&& (m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
)) {
6945 * page coming back in from being 'frozen'...
6946 * it was dirty before it was frozen, so keep it so
6947 * the vm_page_activate will notice that it really belongs
6948 * on the throttle queue and put it there
6950 SET_PAGE_DIRTY(m
, FALSE
);
6951 dwp
->dw_mask
|= DW_vm_page_activate
;
6954 if ((flags
& UPL_COMMIT_INACTIVATE
) && !m
->vmp_clustered
&& (m
->vmp_q_state
!= VM_PAGE_ON_SPECULATIVE_Q
)) {
6955 dwp
->dw_mask
|= DW_vm_page_deactivate_internal
;
6956 clear_refmod
|= VM_MEM_REFERENCED
;
6957 } else if ( !VM_PAGE_PAGEABLE(m
)) {
6959 if (m
->vmp_clustered
|| (flags
& UPL_COMMIT_SPECULATE
))
6960 dwp
->dw_mask
|= DW_vm_page_speculate
;
6961 else if (m
->vmp_reference
)
6962 dwp
->dw_mask
|= DW_vm_page_activate
;
6964 dwp
->dw_mask
|= DW_vm_page_deactivate_internal
;
6965 clear_refmod
|= VM_MEM_REFERENCED
;
6969 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
6971 * We blocked access to the pages in this URL.
6972 * Clear the "busy" bit on this page before we
6973 * wake up any waiter.
6975 dwp
->dw_mask
|= DW_clear_busy
;
6978 * Wakeup any thread waiting for the page to be un-cleaning.
6980 dwp
->dw_mask
|= DW_PAGE_WAKEUP
;
6984 pmap_clear_refmod(VM_PAGE_GET_PHYS_PAGE(m
), clear_refmod
);
6986 target_offset
+= PAGE_SIZE_64
;
6987 xfer_size
-= PAGE_SIZE
;
6991 if (dwp
->dw_mask
& ~(DW_clear_busy
| DW_PAGE_WAKEUP
)) {
6992 VM_PAGE_ADD_DELAYED_WORK(dwp
, m
, dw_count
);
6994 if (dw_count
>= dw_limit
) {
6995 vm_page_do_delayed_work(shadow_object
, VM_KERN_MEMORY_NONE
, &dw_array
[0], dw_count
);
7001 if (dwp
->dw_mask
& DW_clear_busy
)
7002 m
->vmp_busy
= FALSE
;
7004 if (dwp
->dw_mask
& DW_PAGE_WAKEUP
)
7010 vm_page_do_delayed_work(shadow_object
, VM_KERN_MEMORY_NONE
, &dw_array
[0], dw_count
);
7012 if (fast_path_possible
) {
7014 assert(shadow_object
->purgable
!= VM_PURGABLE_VOLATILE
);
7015 assert(shadow_object
->purgable
!= VM_PURGABLE_EMPTY
);
7017 if (local_queue_count
|| unwired_count
) {
7019 if (local_queue_count
) {
7020 vm_page_t first_target
;
7021 vm_page_queue_head_t
*target_queue
;
7024 target_queue
= &vm_page_queue_throttled
;
7026 if (flags
& UPL_COMMIT_INACTIVATE
) {
7027 if (shadow_object
->internal
)
7028 target_queue
= &vm_page_queue_anonymous
;
7030 target_queue
= &vm_page_queue_inactive
;
7032 target_queue
= &vm_page_queue_active
;
7035 * Transfer the entire local queue to a regular LRU page queues.
7037 vm_page_lockspin_queues();
7039 first_target
= (vm_page_t
) vm_page_queue_first(target_queue
);
7041 if (vm_page_queue_empty(target_queue
))
7042 target_queue
->prev
= VM_PAGE_CONVERT_TO_QUEUE_ENTRY(last_local
);
7044 first_target
->vmp_pageq
.prev
= VM_PAGE_CONVERT_TO_QUEUE_ENTRY(last_local
);
7046 target_queue
->next
= VM_PAGE_CONVERT_TO_QUEUE_ENTRY(first_local
);
7047 first_local
->vmp_pageq
.prev
= VM_PAGE_CONVERT_TO_QUEUE_ENTRY(target_queue
);
7048 last_local
->vmp_pageq
.next
= VM_PAGE_CONVERT_TO_QUEUE_ENTRY(first_target
);
7051 * Adjust the global page counts.
7053 if (throttle_page
) {
7054 vm_page_throttled_count
+= local_queue_count
;
7056 if (flags
& UPL_COMMIT_INACTIVATE
) {
7057 if (shadow_object
->internal
)
7058 vm_page_anonymous_count
+= local_queue_count
;
7059 vm_page_inactive_count
+= local_queue_count
;
7061 token_new_pagecount
+= local_queue_count
;
7063 vm_page_active_count
+= local_queue_count
;
7065 if (shadow_object
->internal
)
7066 vm_page_pageable_internal_count
+= local_queue_count
;
7068 vm_page_pageable_external_count
+= local_queue_count
;
7071 vm_page_lockspin_queues();
7073 if (unwired_count
) {
7074 vm_page_wire_count
-= unwired_count
;
7075 VM_CHECK_MEMORYSTATUS
;
7077 vm_page_unlock_queues();
7079 VM_OBJECT_WIRED_PAGE_COUNT(shadow_object
, -unwired_count
);
7084 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
7086 } else if (upl
->flags
& UPL_LITE
) {
7092 if (!fast_path_full_commit
) {
7093 pg_num
= upl
->size
/PAGE_SIZE
;
7094 pg_num
= (pg_num
+ 31) >> 5;
7096 for (i
= 0; i
< pg_num
; i
++) {
7097 if (lite_list
[i
] != 0) {
7104 if (vm_page_queue_empty(&upl
->map_object
->memq
))
7107 if (occupied
== 0) {
7109 * If this UPL element belongs to a Vector UPL and is
7110 * empty, then this is the right function to deallocate
7111 * it. So go ahead set the *empty variable. The flag
7112 * UPL_COMMIT_NOTIFY_EMPTY, from the caller's point of view
7113 * should be considered relevant for the Vector UPL and not
7114 * the internal UPLs.
7116 if ((upl
->flags
& UPL_COMMIT_NOTIFY_EMPTY
) || isVectorUPL
)
7119 if (object
== shadow_object
&& !(upl
->flags
& UPL_KERNEL_OBJECT
)) {
7121 * this is not a paging object
7122 * so we need to drop the paging reference
7123 * that was taken when we created the UPL
7124 * against this object
7126 vm_object_activity_end(shadow_object
);
7127 vm_object_collapse(shadow_object
, 0, TRUE
);
7130 * we dontated the paging reference to
7131 * the map object... vm_pageout_object_terminate
7132 * will drop this reference
7136 VM_OBJECT_WIRED_PAGE_UPDATE_END(shadow_object
, shadow_object
->wire_tag
);
7137 vm_object_unlock(shadow_object
);
7138 if (object
!= shadow_object
)
7139 vm_object_unlock(object
);
7145 * If we completed our operations on an UPL that is
7146 * part of a Vectored UPL and if empty is TRUE, then
7147 * we should go ahead and deallocate this UPL element.
7148 * Then we check if this was the last of the UPL elements
7149 * within that Vectored UPL. If so, set empty to TRUE
7150 * so that in ubc_upl_commit_range or ubc_upl_commit, we
7151 * can go ahead and deallocate the Vector UPL too.
7154 *empty
= vector_upl_set_subupl(vector_upl
, upl
, 0);
7155 upl_deallocate(upl
);
7157 goto process_upl_to_commit
;
7159 if (pgpgout_count
) {
7160 DTRACE_VM2(pgpgout
, int, pgpgout_count
, (uint64_t *), NULL
);
7163 return KERN_SUCCESS
;
7169 upl_offset_t offset
,
7174 upl_page_info_t
*user_page_list
= NULL
;
7175 upl_size_t xfer_size
, subupl_size
= size
;
7176 vm_object_t shadow_object
;
7178 vm_object_offset_t target_offset
;
7179 upl_offset_t subupl_offset
= offset
;
7181 wpl_array_t lite_list
;
7183 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
7184 struct vm_page_delayed_work
*dwp
;
7187 int isVectorUPL
= 0;
7188 upl_t vector_upl
= NULL
;
7192 if (upl
== UPL_NULL
)
7193 return KERN_INVALID_ARGUMENT
;
7195 if ( (upl
->flags
& UPL_IO_WIRE
) && !(error
& UPL_ABORT_DUMP_PAGES
) )
7196 return upl_commit_range(upl
, offset
, size
, UPL_COMMIT_FREE_ABSENT
, NULL
, 0, empty
);
7198 if((isVectorUPL
= vector_upl_is_valid(upl
))) {
7200 upl_lock(vector_upl
);
7205 process_upl_to_abort
:
7208 offset
= subupl_offset
;
7210 upl_unlock(vector_upl
);
7211 return KERN_SUCCESS
;
7213 upl
= vector_upl_subupl_byoffset(vector_upl
, &offset
, &size
);
7215 upl_unlock(vector_upl
);
7216 return KERN_FAILURE
;
7218 subupl_size
-= size
;
7219 subupl_offset
+= size
;
7225 if (upl
->upl_commit_index
< UPL_DEBUG_COMMIT_RECORDS
) {
7226 (void) OSBacktrace(&upl
->upl_commit_records
[upl
->upl_commit_index
].c_retaddr
[0], UPL_DEBUG_STACK_FRAMES
);
7228 upl
->upl_commit_records
[upl
->upl_commit_index
].c_beg
= offset
;
7229 upl
->upl_commit_records
[upl
->upl_commit_index
].c_end
= (offset
+ size
);
7230 upl
->upl_commit_records
[upl
->upl_commit_index
].c_aborted
= 1;
7232 upl
->upl_commit_index
++;
7235 if (upl
->flags
& UPL_DEVICE_MEMORY
)
7237 else if ((offset
+ size
) <= upl
->size
)
7243 upl_unlock(vector_upl
);
7246 return KERN_FAILURE
;
7248 if (upl
->flags
& UPL_INTERNAL
) {
7249 lite_list
= (wpl_array_t
)
7250 ((((uintptr_t)upl
) + sizeof(struct upl
))
7251 + ((upl
->size
/PAGE_SIZE
) * sizeof(upl_page_info_t
)));
7253 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
7255 lite_list
= (wpl_array_t
)
7256 (((uintptr_t)upl
) + sizeof(struct upl
));
7258 object
= upl
->map_object
;
7260 if (upl
->flags
& UPL_SHADOWED
) {
7261 vm_object_lock(object
);
7262 shadow_object
= object
->shadow
;
7264 shadow_object
= object
;
7266 entry
= offset
/PAGE_SIZE
;
7267 target_offset
= (vm_object_offset_t
)offset
;
7269 assert(!(target_offset
& PAGE_MASK
));
7270 assert(!(xfer_size
& PAGE_MASK
));
7272 if (upl
->flags
& UPL_KERNEL_OBJECT
)
7273 vm_object_lock_shared(shadow_object
);
7275 vm_object_lock(shadow_object
);
7277 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
7278 assert(shadow_object
->blocked_access
);
7279 shadow_object
->blocked_access
= FALSE
;
7280 vm_object_wakeup(object
, VM_OBJECT_EVENT_UNBLOCKED
);
7285 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
7287 if ((error
& UPL_ABORT_DUMP_PAGES
) && (upl
->flags
& UPL_KERNEL_OBJECT
))
7288 panic("upl_abort_range: kernel_object being DUMPED");
7292 unsigned int pg_num
;
7295 pg_num
= (unsigned int) (target_offset
/PAGE_SIZE
);
7296 assert(pg_num
== target_offset
/PAGE_SIZE
);
7301 needed
= user_page_list
[pg_num
].needed
;
7306 if (upl
->flags
& UPL_LITE
) {
7308 if (lite_list
[pg_num
>>5] & (1 << (pg_num
& 31))) {
7309 lite_list
[pg_num
>>5] &= ~(1 << (pg_num
& 31));
7311 if ( !(upl
->flags
& UPL_KERNEL_OBJECT
))
7312 m
= vm_page_lookup(shadow_object
, target_offset
+
7313 (upl
->offset
- shadow_object
->paging_offset
));
7316 if (upl
->flags
& UPL_SHADOWED
) {
7317 if ((t
= vm_page_lookup(object
, target_offset
)) != VM_PAGE_NULL
) {
7318 t
->vmp_free_when_done
= FALSE
;
7322 if (m
== VM_PAGE_NULL
)
7323 m
= vm_page_lookup(shadow_object
, target_offset
+ object
->vo_shadow_offset
);
7326 if ((upl
->flags
& UPL_KERNEL_OBJECT
))
7327 goto abort_next_page
;
7329 if (m
!= VM_PAGE_NULL
) {
7331 assert(m
->vmp_q_state
!= VM_PAGE_USED_BY_COMPRESSOR
);
7333 if (m
->vmp_absent
) {
7334 boolean_t must_free
= TRUE
;
7337 * COPYOUT = FALSE case
7338 * check for error conditions which must
7339 * be passed back to the pages customer
7341 if (error
& UPL_ABORT_RESTART
) {
7342 m
->vmp_restart
= TRUE
;
7343 m
->vmp_absent
= FALSE
;
7344 m
->vmp_unusual
= TRUE
;
7346 } else if (error
& UPL_ABORT_UNAVAILABLE
) {
7347 m
->vmp_restart
= FALSE
;
7348 m
->vmp_unusual
= TRUE
;
7350 } else if (error
& UPL_ABORT_ERROR
) {
7351 m
->vmp_restart
= FALSE
;
7352 m
->vmp_absent
= FALSE
;
7353 m
->vmp_error
= TRUE
;
7354 m
->vmp_unusual
= TRUE
;
7357 if (m
->vmp_clustered
&& needed
== FALSE
) {
7359 * This page was a part of a speculative
7360 * read-ahead initiated by the kernel
7361 * itself. No one is expecting this
7362 * page and no one will clean up its
7363 * error state if it ever becomes valid
7365 * We have to free it here.
7369 m
->vmp_cleaning
= FALSE
;
7371 if (m
->vmp_overwriting
&& !m
->vmp_busy
) {
7373 * this shouldn't happen since
7374 * this is an 'absent' page, but
7375 * it doesn't hurt to check for
7376 * the 'alternate' method of
7377 * stabilizing the page...
7378 * we will mark 'busy' to be cleared
7379 * in the following code which will
7380 * take care of the primary stabilzation
7381 * method (i.e. setting 'busy' to TRUE)
7383 dwp
->dw_mask
|= DW_vm_page_unwire
;
7385 m
->vmp_overwriting
= FALSE
;
7387 dwp
->dw_mask
|= (DW_clear_busy
| DW_PAGE_WAKEUP
);
7389 if (must_free
== TRUE
)
7390 dwp
->dw_mask
|= DW_vm_page_free
;
7392 dwp
->dw_mask
|= DW_vm_page_activate
;
7395 * Handle the trusted pager throttle.
7398 dwp
->dw_mask
|= DW_vm_pageout_throttle_up
;
7400 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
7402 * We blocked access to the pages in this UPL.
7403 * Clear the "busy" bit and wake up any waiter
7406 dwp
->dw_mask
|= DW_clear_busy
;
7408 if (m
->vmp_overwriting
) {
7410 dwp
->dw_mask
|= DW_clear_busy
;
7413 * deal with the 'alternate' method
7414 * of stabilizing the page...
7415 * we will either free the page
7416 * or mark 'busy' to be cleared
7417 * in the following code which will
7418 * take care of the primary stabilzation
7419 * method (i.e. setting 'busy' to TRUE)
7421 dwp
->dw_mask
|= DW_vm_page_unwire
;
7423 m
->vmp_overwriting
= FALSE
;
7425 m
->vmp_free_when_done
= FALSE
;
7426 m
->vmp_cleaning
= FALSE
;
7428 if (error
& UPL_ABORT_DUMP_PAGES
) {
7429 pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m
));
7431 dwp
->dw_mask
|= DW_vm_page_free
;
7433 if (!(dwp
->dw_mask
& DW_vm_page_unwire
)) {
7434 if (error
& UPL_ABORT_REFERENCE
) {
7436 * we've been told to explictly
7437 * reference this page... for
7438 * file I/O, this is done by
7439 * implementing an LRU on the inactive q
7441 dwp
->dw_mask
|= DW_vm_page_lru
;
7443 } else if ( !VM_PAGE_PAGEABLE(m
))
7444 dwp
->dw_mask
|= DW_vm_page_deactivate_internal
;
7446 dwp
->dw_mask
|= DW_PAGE_WAKEUP
;
7451 target_offset
+= PAGE_SIZE_64
;
7452 xfer_size
-= PAGE_SIZE
;
7456 if (dwp
->dw_mask
& ~(DW_clear_busy
| DW_PAGE_WAKEUP
)) {
7457 VM_PAGE_ADD_DELAYED_WORK(dwp
, m
, dw_count
);
7459 if (dw_count
>= dw_limit
) {
7460 vm_page_do_delayed_work(shadow_object
, VM_KERN_MEMORY_NONE
, &dw_array
[0], dw_count
);
7466 if (dwp
->dw_mask
& DW_clear_busy
)
7467 m
->vmp_busy
= FALSE
;
7469 if (dwp
->dw_mask
& DW_PAGE_WAKEUP
)
7475 vm_page_do_delayed_work(shadow_object
, VM_KERN_MEMORY_NONE
, &dw_array
[0], dw_count
);
7479 if (upl
->flags
& UPL_DEVICE_MEMORY
) {
7481 } else if (upl
->flags
& UPL_LITE
) {
7485 pg_num
= upl
->size
/PAGE_SIZE
;
7486 pg_num
= (pg_num
+ 31) >> 5;
7489 for (i
= 0; i
< pg_num
; i
++) {
7490 if (lite_list
[i
] != 0) {
7496 if (vm_page_queue_empty(&upl
->map_object
->memq
))
7499 if (occupied
== 0) {
7501 * If this UPL element belongs to a Vector UPL and is
7502 * empty, then this is the right function to deallocate
7503 * it. So go ahead set the *empty variable. The flag
7504 * UPL_COMMIT_NOTIFY_EMPTY, from the caller's point of view
7505 * should be considered relevant for the Vector UPL and
7506 * not the internal UPLs.
7508 if ((upl
->flags
& UPL_COMMIT_NOTIFY_EMPTY
) || isVectorUPL
)
7511 if (object
== shadow_object
&& !(upl
->flags
& UPL_KERNEL_OBJECT
)) {
7513 * this is not a paging object
7514 * so we need to drop the paging reference
7515 * that was taken when we created the UPL
7516 * against this object
7518 vm_object_activity_end(shadow_object
);
7519 vm_object_collapse(shadow_object
, 0, TRUE
);
7522 * we dontated the paging reference to
7523 * the map object... vm_pageout_object_terminate
7524 * will drop this reference
7528 vm_object_unlock(shadow_object
);
7529 if (object
!= shadow_object
)
7530 vm_object_unlock(object
);
7536 * If we completed our operations on an UPL that is
7537 * part of a Vectored UPL and if empty is TRUE, then
7538 * we should go ahead and deallocate this UPL element.
7539 * Then we check if this was the last of the UPL elements
7540 * within that Vectored UPL. If so, set empty to TRUE
7541 * so that in ubc_upl_abort_range or ubc_upl_abort, we
7542 * can go ahead and deallocate the Vector UPL too.
7544 if(*empty
== TRUE
) {
7545 *empty
= vector_upl_set_subupl(vector_upl
, upl
,0);
7546 upl_deallocate(upl
);
7548 goto process_upl_to_abort
;
7551 return KERN_SUCCESS
;
7562 if (upl
== UPL_NULL
)
7563 return KERN_INVALID_ARGUMENT
;
7565 return upl_abort_range(upl
, 0, upl
->size
, error
, &empty
);
7569 /* an option on commit should be wire */
7573 upl_page_info_t
*page_list
,
7574 mach_msg_type_number_t count
)
7578 if (upl
== UPL_NULL
)
7579 return KERN_INVALID_ARGUMENT
;
7581 return upl_commit_range(upl
, 0, upl
->size
, 0, page_list
, count
, &empty
);
7592 vm_page_t m
, nxt_page
= VM_PAGE_NULL
;
7594 int wired_count
= 0;
7597 panic("iopl_valid_data: NULL upl");
7598 if (vector_upl_is_valid(upl
))
7599 panic("iopl_valid_data: vector upl");
7600 if ((upl
->flags
& (UPL_DEVICE_MEMORY
|UPL_SHADOWED
|UPL_ACCESS_BLOCKED
|UPL_IO_WIRE
|UPL_INTERNAL
)) != UPL_IO_WIRE
)
7601 panic("iopl_valid_data: unsupported upl, flags = %x", upl
->flags
);
7603 object
= upl
->map_object
;
7605 if (object
== kernel_object
|| object
== compressor_object
)
7606 panic("iopl_valid_data: object == kernel or compressor");
7608 if (object
->purgable
== VM_PURGABLE_VOLATILE
||
7609 object
->purgable
== VM_PURGABLE_EMPTY
)
7610 panic("iopl_valid_data: object %p purgable %d",
7611 object
, object
->purgable
);
7615 vm_object_lock(object
);
7616 VM_OBJECT_WIRED_PAGE_UPDATE_START(object
);
7618 if (object
->vo_size
== size
&& object
->resident_page_count
== (size
/ PAGE_SIZE
))
7619 nxt_page
= (vm_page_t
)vm_page_queue_first(&object
->memq
);
7621 offset
= 0 + upl
->offset
- object
->paging_offset
;
7625 if (nxt_page
!= VM_PAGE_NULL
) {
7627 nxt_page
= (vm_page_t
)vm_page_queue_next(&nxt_page
->vmp_listq
);
7629 m
= vm_page_lookup(object
, offset
);
7630 offset
+= PAGE_SIZE
;
7632 if (m
== VM_PAGE_NULL
)
7633 panic("iopl_valid_data: missing expected page at offset %lx", (long)offset
);
7637 panic("iopl_valid_data: busy page w/o absent");
7639 if (m
->vmp_pageq
.next
|| m
->vmp_pageq
.prev
)
7640 panic("iopl_valid_data: busy+absent page on page queue");
7641 if (m
->vmp_reusable
) {
7642 panic("iopl_valid_data: %p is reusable", m
);
7645 m
->vmp_absent
= FALSE
;
7646 m
->vmp_dirty
= TRUE
;
7647 assert(m
->vmp_q_state
== VM_PAGE_NOT_ON_Q
);
7648 assert(m
->vmp_wire_count
== 0);
7649 m
->vmp_wire_count
++;
7650 assert(m
->vmp_wire_count
);
7651 if (m
->vmp_wire_count
== 1) {
7652 m
->vmp_q_state
= VM_PAGE_IS_WIRED
;
7655 panic("iopl_valid_data: %p already wired\n", m
);
7658 PAGE_WAKEUP_DONE(m
);
7664 VM_OBJECT_WIRED_PAGE_COUNT(object
, wired_count
);
7665 assert(object
->resident_page_count
>= object
->wired_page_count
);
7667 /* no need to adjust purgeable accounting for this object: */
7668 assert(object
->purgable
!= VM_PURGABLE_VOLATILE
);
7669 assert(object
->purgable
!= VM_PURGABLE_EMPTY
);
7671 vm_page_lockspin_queues();
7672 vm_page_wire_count
+= wired_count
;
7673 vm_page_unlock_queues();
7675 VM_OBJECT_WIRED_PAGE_UPDATE_END(object
, tag
);
7676 vm_object_unlock(object
);
7681 vm_object_set_pmap_cache_attr(
7683 upl_page_info_array_t user_page_list
,
7684 unsigned int num_pages
,
7685 boolean_t batch_pmap_op
)
7687 unsigned int cache_attr
= 0;
7689 cache_attr
= object
->wimg_bits
& VM_WIMG_MASK
;
7690 assert(user_page_list
);
7691 if (cache_attr
!= VM_WIMG_USE_DEFAULT
) {
7692 PMAP_BATCH_SET_CACHE_ATTR(object
, user_page_list
, cache_attr
, num_pages
, batch_pmap_op
);
7697 boolean_t
vm_object_iopl_wire_full(vm_object_t
, upl_t
, upl_page_info_array_t
, wpl_array_t
, upl_control_flags_t
, vm_tag_t
);
7698 kern_return_t
vm_object_iopl_wire_empty(vm_object_t
, upl_t
, upl_page_info_array_t
, wpl_array_t
, upl_control_flags_t
, vm_tag_t
, vm_object_offset_t
*, int, int*);
7703 vm_object_iopl_wire_full(vm_object_t object
, upl_t upl
, upl_page_info_array_t user_page_list
,
7704 wpl_array_t lite_list
, upl_control_flags_t cntrl_flags
, vm_tag_t tag
)
7709 int delayed_unlock
= 0;
7710 boolean_t retval
= TRUE
;
7713 vm_object_lock_assert_exclusive(object
);
7714 assert(object
->purgable
!= VM_PURGABLE_VOLATILE
);
7715 assert(object
->purgable
!= VM_PURGABLE_EMPTY
);
7716 assert(object
->pager
== NULL
);
7717 assert(object
->copy
== NULL
);
7718 assert(object
->shadow
== NULL
);
7720 page_count
= object
->resident_page_count
;
7721 dst_page
= (vm_page_t
)vm_page_queue_first(&object
->memq
);
7723 vm_page_lock_queues();
7725 while (page_count
--) {
7727 if (dst_page
->vmp_busy
||
7728 dst_page
->vmp_fictitious
||
7729 dst_page
->vmp_absent
||
7730 dst_page
->vmp_error
||
7731 dst_page
->vmp_cleaning
||
7732 dst_page
->vmp_restart
||
7733 dst_page
->vmp_laundry
) {
7737 if ((cntrl_flags
& UPL_REQUEST_FORCE_COHERENCY
) && dst_page
->vmp_written_by_kernel
== TRUE
) {
7741 dst_page
->vmp_reference
= TRUE
;
7743 vm_page_wire(dst_page
, tag
, FALSE
);
7745 if (!(cntrl_flags
& UPL_COPYOUT_FROM
)) {
7746 SET_PAGE_DIRTY(dst_page
, FALSE
);
7748 entry
= (unsigned int)(dst_page
->vmp_offset
/ PAGE_SIZE
);
7749 assert(entry
>= 0 && entry
< object
->resident_page_count
);
7750 lite_list
[entry
>>5] |= 1 << (entry
& 31);
7752 phys_page
= VM_PAGE_GET_PHYS_PAGE(dst_page
);
7754 if (phys_page
> upl
->highest_page
)
7755 upl
->highest_page
= phys_page
;
7757 if (user_page_list
) {
7758 user_page_list
[entry
].phys_addr
= phys_page
;
7759 user_page_list
[entry
].absent
= dst_page
->vmp_absent
;
7760 user_page_list
[entry
].dirty
= dst_page
->vmp_dirty
;
7761 user_page_list
[entry
].free_when_done
= dst_page
->vmp_free_when_done
;
7762 user_page_list
[entry
].precious
= dst_page
->vmp_precious
;
7763 user_page_list
[entry
].device
= FALSE
;
7764 user_page_list
[entry
].speculative
= FALSE
;
7765 user_page_list
[entry
].cs_validated
= FALSE
;
7766 user_page_list
[entry
].cs_tainted
= FALSE
;
7767 user_page_list
[entry
].cs_nx
= FALSE
;
7768 user_page_list
[entry
].needed
= FALSE
;
7769 user_page_list
[entry
].mark
= FALSE
;
7771 if (delayed_unlock
++ > 256) {
7773 lck_mtx_yield(&vm_page_queue_lock
);
7775 VM_CHECK_MEMORYSTATUS
;
7777 dst_page
= (vm_page_t
)vm_page_queue_next(&dst_page
->vmp_listq
);
7780 vm_page_unlock_queues();
7782 VM_CHECK_MEMORYSTATUS
;
7789 vm_object_iopl_wire_empty(vm_object_t object
, upl_t upl
, upl_page_info_array_t user_page_list
,
7790 wpl_array_t lite_list
, upl_control_flags_t cntrl_flags
, vm_tag_t tag
, vm_object_offset_t
*dst_offset
,
7791 int page_count
, int* page_grab_count
)
7794 boolean_t no_zero_fill
= FALSE
;
7796 int pages_wired
= 0;
7797 int pages_inserted
= 0;
7799 uint64_t delayed_ledger_update
= 0;
7800 kern_return_t ret
= KERN_SUCCESS
;
7804 vm_object_lock_assert_exclusive(object
);
7805 assert(object
->purgable
!= VM_PURGABLE_VOLATILE
);
7806 assert(object
->purgable
!= VM_PURGABLE_EMPTY
);
7807 assert(object
->pager
== NULL
);
7808 assert(object
->copy
== NULL
);
7809 assert(object
->shadow
== NULL
);
7811 if (cntrl_flags
& UPL_SET_INTERRUPTIBLE
)
7812 interruptible
= THREAD_ABORTSAFE
;
7814 interruptible
= THREAD_UNINT
;
7816 if (cntrl_flags
& (UPL_NOZEROFILL
| UPL_NOZEROFILLIO
))
7817 no_zero_fill
= TRUE
;
7820 #if CONFIG_SECLUDED_MEMORY
7821 if (object
->can_grab_secluded
) {
7822 grab_options
|= VM_PAGE_GRAB_SECLUDED
;
7824 #endif /* CONFIG_SECLUDED_MEMORY */
7826 while (page_count
--) {
7828 while ((dst_page
= vm_page_grab_options(grab_options
))
7831 OSAddAtomic(page_count
, &vm_upl_wait_for_pages
);
7833 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_START
, vm_upl_wait_for_pages
, 0, 0, 0);
7835 if (vm_page_wait(interruptible
) == FALSE
) {
7839 OSAddAtomic(-page_count
, &vm_upl_wait_for_pages
);
7841 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_END
, vm_upl_wait_for_pages
, 0, 0, -1);
7843 ret
= MACH_SEND_INTERRUPTED
;
7846 OSAddAtomic(-page_count
, &vm_upl_wait_for_pages
);
7848 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_END
, vm_upl_wait_for_pages
, 0, 0, 0);
7850 if (no_zero_fill
== FALSE
)
7851 vm_page_zero_fill(dst_page
);
7853 dst_page
->vmp_absent
= TRUE
;
7855 dst_page
->vmp_reference
= TRUE
;
7857 if (!(cntrl_flags
& UPL_COPYOUT_FROM
)) {
7858 SET_PAGE_DIRTY(dst_page
, FALSE
);
7860 if (dst_page
->vmp_absent
== FALSE
) {
7861 assert(dst_page
->vmp_q_state
== VM_PAGE_NOT_ON_Q
);
7862 assert(dst_page
->vmp_wire_count
== 0);
7863 dst_page
->vmp_wire_count
++;
7864 dst_page
->vmp_q_state
= VM_PAGE_IS_WIRED
;
7865 assert(dst_page
->vmp_wire_count
);
7867 PAGE_WAKEUP_DONE(dst_page
);
7871 vm_page_insert_internal(dst_page
, object
, *dst_offset
, tag
, FALSE
, TRUE
, TRUE
, TRUE
, &delayed_ledger_update
);
7873 lite_list
[entry
>>5] |= 1 << (entry
& 31);
7875 phys_page
= VM_PAGE_GET_PHYS_PAGE(dst_page
);
7877 if (phys_page
> upl
->highest_page
)
7878 upl
->highest_page
= phys_page
;
7880 if (user_page_list
) {
7881 user_page_list
[entry
].phys_addr
= phys_page
;
7882 user_page_list
[entry
].absent
= dst_page
->vmp_absent
;
7883 user_page_list
[entry
].dirty
= dst_page
->vmp_dirty
;
7884 user_page_list
[entry
].free_when_done
= FALSE
;
7885 user_page_list
[entry
].precious
= FALSE
;
7886 user_page_list
[entry
].device
= FALSE
;
7887 user_page_list
[entry
].speculative
= FALSE
;
7888 user_page_list
[entry
].cs_validated
= FALSE
;
7889 user_page_list
[entry
].cs_tainted
= FALSE
;
7890 user_page_list
[entry
].cs_nx
= FALSE
;
7891 user_page_list
[entry
].needed
= FALSE
;
7892 user_page_list
[entry
].mark
= FALSE
;
7895 *dst_offset
+= PAGE_SIZE_64
;
7899 vm_page_lockspin_queues();
7900 vm_page_wire_count
+= pages_wired
;
7901 vm_page_unlock_queues();
7903 if (pages_inserted
) {
7904 if (object
->internal
) {
7905 OSAddAtomic(pages_inserted
, &vm_page_internal_count
);
7907 OSAddAtomic(pages_inserted
, &vm_page_external_count
);
7910 if (delayed_ledger_update
) {
7912 int ledger_idx_volatile
;
7913 int ledger_idx_nonvolatile
;
7914 int ledger_idx_volatile_compressed
;
7915 int ledger_idx_nonvolatile_compressed
;
7916 boolean_t do_footprint
;
7918 owner
= VM_OBJECT_OWNER(object
);
7921 vm_object_ledger_tag_ledgers(object
,
7922 &ledger_idx_volatile
,
7923 &ledger_idx_nonvolatile
,
7924 &ledger_idx_volatile_compressed
,
7925 &ledger_idx_nonvolatile_compressed
,
7928 /* more non-volatile bytes */
7929 ledger_credit(owner
->ledger
,
7930 ledger_idx_nonvolatile
,
7931 delayed_ledger_update
);
7933 /* more footprint */
7934 ledger_credit(owner
->ledger
,
7935 task_ledgers
.phys_footprint
,
7936 delayed_ledger_update
);
7940 assert(page_grab_count
);
7941 *page_grab_count
= pages_inserted
;
7949 vm_object_iopl_request(
7951 vm_object_offset_t offset
,
7954 upl_page_info_array_t user_page_list
,
7955 unsigned int *page_list_count
,
7956 upl_control_flags_t cntrl_flags
,
7960 vm_object_offset_t dst_offset
;
7961 upl_size_t xfer_size
;
7964 wpl_array_t lite_list
= NULL
;
7965 int no_zero_fill
= FALSE
;
7966 unsigned int size_in_pages
;
7967 int page_grab_count
= 0;
7971 struct vm_object_fault_info fault_info
= {};
7972 struct vm_page_delayed_work dw_array
[DEFAULT_DELAYED_WORK_LIMIT
];
7973 struct vm_page_delayed_work
*dwp
;
7977 boolean_t caller_lookup
;
7978 int io_tracking_flag
= 0;
7982 boolean_t set_cache_attr_needed
= FALSE
;
7983 boolean_t free_wired_pages
= FALSE
;
7984 boolean_t fast_path_empty_req
= FALSE
;
7985 boolean_t fast_path_full_req
= FALSE
;
7987 if (cntrl_flags
& ~UPL_VALID_FLAGS
) {
7989 * For forward compatibility's sake,
7990 * reject any unknown flag.
7992 return KERN_INVALID_VALUE
;
7994 if (vm_lopage_needed
== FALSE
)
7995 cntrl_flags
&= ~UPL_NEED_32BIT_ADDR
;
7997 if (cntrl_flags
& UPL_NEED_32BIT_ADDR
) {
7998 if ( (cntrl_flags
& (UPL_SET_IO_WIRE
| UPL_SET_LITE
)) != (UPL_SET_IO_WIRE
| UPL_SET_LITE
))
7999 return KERN_INVALID_VALUE
;
8001 if (object
->phys_contiguous
) {
8002 if ((offset
+ object
->vo_shadow_offset
) >= (vm_object_offset_t
)max_valid_dma_address
)
8003 return KERN_INVALID_ADDRESS
;
8005 if (((offset
+ object
->vo_shadow_offset
) + size
) >= (vm_object_offset_t
)max_valid_dma_address
)
8006 return KERN_INVALID_ADDRESS
;
8009 if (cntrl_flags
& (UPL_NOZEROFILL
| UPL_NOZEROFILLIO
))
8010 no_zero_fill
= TRUE
;
8012 if (cntrl_flags
& UPL_COPYOUT_FROM
)
8013 prot
= VM_PROT_READ
;
8015 prot
= VM_PROT_READ
| VM_PROT_WRITE
;
8017 if ((!object
->internal
) && (object
->paging_offset
!= 0))
8018 panic("vm_object_iopl_request: external object with non-zero paging offset\n");
8020 VM_DEBUG_CONSTANT_EVENT(vm_object_iopl_request
, VM_IOPL_REQUEST
, DBG_FUNC_START
, size
, cntrl_flags
, prot
, 0);
8022 #if CONFIG_IOSCHED || UPL_DEBUG
8023 if ((object
->io_tracking
&& object
!= kernel_object
) || upl_debug_enabled
)
8024 io_tracking_flag
|= UPL_CREATE_IO_TRACKING
;
8028 if (object
->io_tracking
) {
8029 /* Check if we're dealing with the kernel object. We do not support expedite on kernel object UPLs */
8030 if (object
!= kernel_object
)
8031 io_tracking_flag
|= UPL_CREATE_EXPEDITE_SUP
;
8035 if (object
->phys_contiguous
)
8040 if (cntrl_flags
& UPL_SET_INTERNAL
) {
8041 upl
= upl_create(UPL_CREATE_INTERNAL
| UPL_CREATE_LITE
| io_tracking_flag
, UPL_IO_WIRE
, psize
);
8043 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
8044 lite_list
= (wpl_array_t
) (((uintptr_t)user_page_list
) +
8045 ((psize
/ PAGE_SIZE
) * sizeof(upl_page_info_t
)));
8047 user_page_list
= NULL
;
8051 upl
= upl_create(UPL_CREATE_LITE
| io_tracking_flag
, UPL_IO_WIRE
, psize
);
8053 lite_list
= (wpl_array_t
) (((uintptr_t)upl
) + sizeof(struct upl
));
8059 user_page_list
[0].device
= FALSE
;
8062 if (cntrl_flags
& UPL_NOZEROFILLIO
) {
8063 DTRACE_VM4(upl_nozerofillio
,
8064 vm_object_t
, object
,
8065 vm_object_offset_t
, offset
,
8070 upl
->map_object
= object
;
8073 size_in_pages
= size
/ PAGE_SIZE
;
8075 if (object
== kernel_object
&&
8076 !(cntrl_flags
& (UPL_NEED_32BIT_ADDR
| UPL_BLOCK_ACCESS
))) {
8077 upl
->flags
|= UPL_KERNEL_OBJECT
;
8079 vm_object_lock(object
);
8081 vm_object_lock_shared(object
);
8084 vm_object_lock(object
);
8085 vm_object_activity_begin(object
);
8088 * paging in progress also protects the paging_offset
8090 upl
->offset
= offset
+ object
->paging_offset
;
8092 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
8094 * The user requested that access to the pages in this UPL
8095 * be blocked until the UPL is commited or aborted.
8097 upl
->flags
|= UPL_ACCESS_BLOCKED
;
8100 #if CONFIG_IOSCHED || UPL_DEBUG
8101 if (upl
->flags
& UPL_TRACKED_BY_OBJECT
) {
8102 vm_object_activity_begin(object
);
8103 queue_enter(&object
->uplq
, upl
, upl_t
, uplq
);
8107 if (object
->phys_contiguous
) {
8109 if (upl
->flags
& UPL_ACCESS_BLOCKED
) {
8110 assert(!object
->blocked_access
);
8111 object
->blocked_access
= TRUE
;
8114 vm_object_unlock(object
);
8117 * don't need any shadow mappings for this one
8118 * since it is already I/O memory
8120 upl
->flags
|= UPL_DEVICE_MEMORY
;
8122 upl
->highest_page
= (ppnum_t
) ((offset
+ object
->vo_shadow_offset
+ size
- 1)>>PAGE_SHIFT
);
8124 if (user_page_list
) {
8125 user_page_list
[0].phys_addr
= (ppnum_t
) ((offset
+ object
->vo_shadow_offset
)>>PAGE_SHIFT
);
8126 user_page_list
[0].device
= TRUE
;
8128 if (page_list_count
!= NULL
) {
8129 if (upl
->flags
& UPL_INTERNAL
)
8130 *page_list_count
= 0;
8132 *page_list_count
= 1;
8135 VM_DEBUG_CONSTANT_EVENT(vm_object_iopl_request
, VM_IOPL_REQUEST
, DBG_FUNC_END
, page_grab_count
, KERN_SUCCESS
, 0, 0);
8136 return KERN_SUCCESS
;
8138 if (object
!= kernel_object
&& object
!= compressor_object
) {
8140 * Protect user space from future COW operations
8142 #if VM_OBJECT_TRACKING_OP_TRUESHARE
8143 if (!object
->true_share
&&
8144 vm_object_tracking_inited
) {
8145 void *bt
[VM_OBJECT_TRACKING_BTDEPTH
];
8148 num
= OSBacktrace(bt
,
8149 VM_OBJECT_TRACKING_BTDEPTH
);
8150 btlog_add_entry(vm_object_tracking_btlog
,
8152 VM_OBJECT_TRACKING_OP_TRUESHARE
,
8156 #endif /* VM_OBJECT_TRACKING_OP_TRUESHARE */
8158 vm_object_lock_assert_exclusive(object
);
8159 object
->true_share
= TRUE
;
8161 if (object
->copy_strategy
== MEMORY_OBJECT_COPY_SYMMETRIC
)
8162 object
->copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
8165 if (!(cntrl_flags
& UPL_COPYOUT_FROM
) &&
8166 object
->copy
!= VM_OBJECT_NULL
) {
8168 * Honor copy-on-write obligations
8170 * The caller is gathering these pages and
8171 * might modify their contents. We need to
8172 * make sure that the copy object has its own
8173 * private copies of these pages before we let
8174 * the caller modify them.
8176 * NOTE: someone else could map the original object
8177 * after we've done this copy-on-write here, and they
8178 * could then see an inconsistent picture of the memory
8179 * while it's being modified via the UPL. To prevent this,
8180 * we would have to block access to these pages until the
8181 * UPL is released. We could use the UPL_BLOCK_ACCESS
8182 * code path for that...
8184 vm_object_update(object
,
8189 FALSE
, /* should_return */
8190 MEMORY_OBJECT_COPY_SYNC
,
8192 VM_PAGEOUT_DEBUG(iopl_cow
, 1);
8193 VM_PAGEOUT_DEBUG(iopl_cow_pages
, (size
>> PAGE_SHIFT
));
8195 if (!(cntrl_flags
& (UPL_NEED_32BIT_ADDR
| UPL_BLOCK_ACCESS
)) &&
8196 object
->purgable
!= VM_PURGABLE_VOLATILE
&&
8197 object
->purgable
!= VM_PURGABLE_EMPTY
&&
8198 object
->copy
== NULL
&&
8199 size
== object
->vo_size
&&
8201 object
->shadow
== NULL
&&
8202 object
->pager
== NULL
)
8204 if (object
->resident_page_count
== size_in_pages
)
8206 assert(object
!= compressor_object
);
8207 assert(object
!= kernel_object
);
8208 fast_path_full_req
= TRUE
;
8210 else if (object
->resident_page_count
== 0)
8212 assert(object
!= compressor_object
);
8213 assert(object
!= kernel_object
);
8214 fast_path_empty_req
= TRUE
;
8215 set_cache_attr_needed
= TRUE
;
8219 if (cntrl_flags
& UPL_SET_INTERRUPTIBLE
)
8220 interruptible
= THREAD_ABORTSAFE
;
8222 interruptible
= THREAD_UNINT
;
8227 dst_offset
= offset
;
8230 if (fast_path_full_req
) {
8232 if (vm_object_iopl_wire_full(object
, upl
, user_page_list
, lite_list
, cntrl_flags
, tag
) == TRUE
)
8235 * we couldn't complete the processing of this request on the fast path
8236 * so fall through to the slow path and finish up
8239 } else if (fast_path_empty_req
) {
8241 if (cntrl_flags
& UPL_REQUEST_NO_FAULT
) {
8242 ret
= KERN_MEMORY_ERROR
;
8245 ret
= vm_object_iopl_wire_empty(object
, upl
, user_page_list
, lite_list
, cntrl_flags
, tag
, &dst_offset
, size_in_pages
, &page_grab_count
);
8248 free_wired_pages
= TRUE
;
8254 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
8255 fault_info
.lo_offset
= offset
;
8256 fault_info
.hi_offset
= offset
+ xfer_size
;
8257 fault_info
.mark_zf_absent
= TRUE
;
8258 fault_info
.interruptible
= interruptible
;
8259 fault_info
.batch_pmap_op
= TRUE
;
8262 dw_limit
= DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT
);
8265 vm_fault_return_t result
;
8269 if (fast_path_full_req
) {
8271 * if we get here, it means that we ran into a page
8272 * state we couldn't handle in the fast path and
8273 * bailed out to the slow path... since the order
8274 * we look at pages is different between the 2 paths,
8275 * the following check is needed to determine whether
8276 * this page was already processed in the fast path
8278 if (lite_list
[entry
>>5] & (1 << (entry
& 31)))
8281 dst_page
= vm_page_lookup(object
, dst_offset
);
8283 if (dst_page
== VM_PAGE_NULL
||
8284 dst_page
->vmp_busy
||
8285 dst_page
->vmp_error
||
8286 dst_page
->vmp_restart
||
8287 dst_page
->vmp_absent
||
8288 dst_page
->vmp_fictitious
) {
8290 if (object
== kernel_object
)
8291 panic("vm_object_iopl_request: missing/bad page in kernel object\n");
8292 if (object
== compressor_object
)
8293 panic("vm_object_iopl_request: missing/bad page in compressor object\n");
8295 if (cntrl_flags
& UPL_REQUEST_NO_FAULT
) {
8296 ret
= KERN_MEMORY_ERROR
;
8299 set_cache_attr_needed
= TRUE
;
8302 * We just looked up the page and the result remains valid
8303 * until the object lock is release, so send it to
8304 * vm_fault_page() (as "dst_page"), to avoid having to
8305 * look it up again there.
8307 caller_lookup
= TRUE
;
8311 kern_return_t error_code
;
8313 fault_info
.cluster_size
= xfer_size
;
8315 vm_object_paging_begin(object
);
8317 result
= vm_fault_page(object
, dst_offset
,
8318 prot
| VM_PROT_WRITE
, FALSE
,
8320 &prot
, &dst_page
, &top_page
,
8322 &error_code
, no_zero_fill
,
8323 FALSE
, &fault_info
);
8325 /* our lookup is no longer valid at this point */
8326 caller_lookup
= FALSE
;
8330 case VM_FAULT_SUCCESS
:
8333 if ( !dst_page
->vmp_absent
) {
8334 PAGE_WAKEUP_DONE(dst_page
);
8337 * we only get back an absent page if we
8338 * requested that it not be zero-filled
8339 * because we are about to fill it via I/O
8341 * absent pages should be left BUSY
8342 * to prevent them from being faulted
8343 * into an address space before we've
8344 * had a chance to complete the I/O on
8345 * them since they may contain info that
8346 * shouldn't be seen by the faulting task
8350 * Release paging references and
8351 * top-level placeholder page, if any.
8353 if (top_page
!= VM_PAGE_NULL
) {
8354 vm_object_t local_object
;
8356 local_object
= VM_PAGE_OBJECT(top_page
);
8359 * comparing 2 packed pointers
8361 if (top_page
->vmp_object
!= dst_page
->vmp_object
) {
8362 vm_object_lock(local_object
);
8363 VM_PAGE_FREE(top_page
);
8364 vm_object_paging_end(local_object
);
8365 vm_object_unlock(local_object
);
8367 VM_PAGE_FREE(top_page
);
8368 vm_object_paging_end(local_object
);
8371 vm_object_paging_end(object
);
8374 case VM_FAULT_RETRY
:
8375 vm_object_lock(object
);
8378 case VM_FAULT_MEMORY_SHORTAGE
:
8379 OSAddAtomic((size_in_pages
- entry
), &vm_upl_wait_for_pages
);
8381 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_START
, vm_upl_wait_for_pages
, 0, 0, 0);
8383 if (vm_page_wait(interruptible
)) {
8384 OSAddAtomic(-(size_in_pages
- entry
), &vm_upl_wait_for_pages
);
8386 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_END
, vm_upl_wait_for_pages
, 0, 0, 0);
8387 vm_object_lock(object
);
8391 OSAddAtomic(-(size_in_pages
- entry
), &vm_upl_wait_for_pages
);
8393 VM_DEBUG_EVENT(vm_iopl_page_wait
, VM_IOPL_PAGE_WAIT
, DBG_FUNC_END
, vm_upl_wait_for_pages
, 0, 0, -1);
8397 case VM_FAULT_INTERRUPTED
:
8398 error_code
= MACH_SEND_INTERRUPTED
;
8399 case VM_FAULT_MEMORY_ERROR
:
8401 ret
= (error_code
? error_code
: KERN_MEMORY_ERROR
);
8403 vm_object_lock(object
);
8406 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
8407 /* success but no page: fail */
8408 vm_object_paging_end(object
);
8409 vm_object_unlock(object
);
8413 panic("vm_object_iopl_request: unexpected error"
8414 " 0x%x from vm_fault_page()\n", result
);
8416 } while (result
!= VM_FAULT_SUCCESS
);
8419 phys_page
= VM_PAGE_GET_PHYS_PAGE(dst_page
);
8421 if (upl
->flags
& UPL_KERNEL_OBJECT
)
8422 goto record_phys_addr
;
8424 if (dst_page
->vmp_q_state
== VM_PAGE_USED_BY_COMPRESSOR
) {
8425 dst_page
->vmp_busy
= TRUE
;
8426 goto record_phys_addr
;
8429 if (dst_page
->vmp_cleaning
) {
8431 * Someone else is cleaning this page in place.
8432 * In theory, we should be able to proceed and use this
8433 * page but they'll probably end up clearing the "busy"
8434 * bit on it in upl_commit_range() but they didn't set
8435 * it, so they would clear our "busy" bit and open
8436 * us to race conditions.
8437 * We'd better wait for the cleaning to complete and
8440 VM_PAGEOUT_DEBUG(vm_object_iopl_request_sleep_for_cleaning
, 1);
8441 PAGE_SLEEP(object
, dst_page
, THREAD_UNINT
);
8444 if (dst_page
->vmp_laundry
)
8445 vm_pageout_steal_laundry(dst_page
, FALSE
);
8447 if ( (cntrl_flags
& UPL_NEED_32BIT_ADDR
) &&
8448 phys_page
>= (max_valid_dma_address
>> PAGE_SHIFT
) ) {
8453 * support devices that can't DMA above 32 bits
8454 * by substituting pages from a pool of low address
8455 * memory for any pages we find above the 4G mark
8456 * can't substitute if the page is already wired because
8457 * we don't know whether that physical address has been
8458 * handed out to some other 64 bit capable DMA device to use
8460 if (VM_PAGE_WIRED(dst_page
)) {
8461 ret
= KERN_PROTECTION_FAILURE
;
8464 low_page
= vm_page_grablo();
8466 if (low_page
== VM_PAGE_NULL
) {
8467 ret
= KERN_RESOURCE_SHORTAGE
;
8471 * from here until the vm_page_replace completes
8472 * we musn't drop the object lock... we don't
8473 * want anyone refaulting this page in and using
8474 * it after we disconnect it... we want the fault
8475 * to find the new page being substituted.
8477 if (dst_page
->vmp_pmapped
)
8478 refmod
= pmap_disconnect(phys_page
);
8482 if (!dst_page
->vmp_absent
)
8483 vm_page_copy(dst_page
, low_page
);
8485 low_page
->vmp_reference
= dst_page
->vmp_reference
;
8486 low_page
->vmp_dirty
= dst_page
->vmp_dirty
;
8487 low_page
->vmp_absent
= dst_page
->vmp_absent
;
8489 if (refmod
& VM_MEM_REFERENCED
)
8490 low_page
->vmp_reference
= TRUE
;
8491 if (refmod
& VM_MEM_MODIFIED
) {
8492 SET_PAGE_DIRTY(low_page
, FALSE
);
8495 vm_page_replace(low_page
, object
, dst_offset
);
8497 dst_page
= low_page
;
8499 * vm_page_grablo returned the page marked
8500 * BUSY... we don't need a PAGE_WAKEUP_DONE
8501 * here, because we've never dropped the object lock
8503 if ( !dst_page
->vmp_absent
)
8504 dst_page
->vmp_busy
= FALSE
;
8506 phys_page
= VM_PAGE_GET_PHYS_PAGE(dst_page
);
8508 if ( !dst_page
->vmp_busy
)
8509 dwp
->dw_mask
|= DW_vm_page_wire
;
8511 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
8513 * Mark the page "busy" to block any future page fault
8514 * on this page in addition to wiring it.
8515 * We'll also remove the mapping
8516 * of all these pages before leaving this routine.
8518 assert(!dst_page
->vmp_fictitious
);
8519 dst_page
->vmp_busy
= TRUE
;
8522 * expect the page to be used
8523 * page queues lock must be held to set 'reference'
8525 dwp
->dw_mask
|= DW_set_reference
;
8527 if (!(cntrl_flags
& UPL_COPYOUT_FROM
)) {
8528 SET_PAGE_DIRTY(dst_page
, TRUE
);
8530 if ((cntrl_flags
& UPL_REQUEST_FORCE_COHERENCY
) && dst_page
->vmp_written_by_kernel
== TRUE
) {
8531 pmap_sync_page_attributes_phys(phys_page
);
8532 dst_page
->vmp_written_by_kernel
= FALSE
;
8536 if (dst_page
->vmp_busy
)
8537 upl
->flags
|= UPL_HAS_BUSY
;
8539 lite_list
[entry
>>5] |= 1 << (entry
& 31);
8541 if (phys_page
> upl
->highest_page
)
8542 upl
->highest_page
= phys_page
;
8544 if (user_page_list
) {
8545 user_page_list
[entry
].phys_addr
= phys_page
;
8546 user_page_list
[entry
].free_when_done
= dst_page
->vmp_free_when_done
;
8547 user_page_list
[entry
].absent
= dst_page
->vmp_absent
;
8548 user_page_list
[entry
].dirty
= dst_page
->vmp_dirty
;
8549 user_page_list
[entry
].precious
= dst_page
->vmp_precious
;
8550 user_page_list
[entry
].device
= FALSE
;
8551 user_page_list
[entry
].needed
= FALSE
;
8552 if (dst_page
->vmp_clustered
== TRUE
)
8553 user_page_list
[entry
].speculative
= (dst_page
->vmp_q_state
== VM_PAGE_ON_SPECULATIVE_Q
) ? TRUE
: FALSE
;
8555 user_page_list
[entry
].speculative
= FALSE
;
8556 user_page_list
[entry
].cs_validated
= dst_page
->vmp_cs_validated
;
8557 user_page_list
[entry
].cs_tainted
= dst_page
->vmp_cs_tainted
;
8558 user_page_list
[entry
].cs_nx
= dst_page
->vmp_cs_nx
;
8559 user_page_list
[entry
].mark
= FALSE
;
8561 if (object
!= kernel_object
&& object
!= compressor_object
) {
8563 * someone is explicitly grabbing this page...
8564 * update clustered and speculative state
8567 if (dst_page
->vmp_clustered
)
8568 VM_PAGE_CONSUME_CLUSTERED(dst_page
);
8572 dst_offset
+= PAGE_SIZE_64
;
8573 xfer_size
-= PAGE_SIZE
;
8576 VM_PAGE_ADD_DELAYED_WORK(dwp
, dst_page
, dw_count
);
8578 if (dw_count
>= dw_limit
) {
8579 vm_page_do_delayed_work(object
, tag
, &dw_array
[0], dw_count
);
8586 assert(entry
== size_in_pages
);
8589 vm_page_do_delayed_work(object
, tag
, &dw_array
[0], dw_count
);
8591 if (user_page_list
&& set_cache_attr_needed
== TRUE
)
8592 vm_object_set_pmap_cache_attr(object
, user_page_list
, size_in_pages
, TRUE
);
8594 if (page_list_count
!= NULL
) {
8595 if (upl
->flags
& UPL_INTERNAL
)
8596 *page_list_count
= 0;
8597 else if (*page_list_count
> size_in_pages
)
8598 *page_list_count
= size_in_pages
;
8600 vm_object_unlock(object
);
8602 if (cntrl_flags
& UPL_BLOCK_ACCESS
) {
8604 * We've marked all the pages "busy" so that future
8605 * page faults will block.
8606 * Now remove the mapping for these pages, so that they
8607 * can't be accessed without causing a page fault.
8609 vm_object_pmap_protect(object
, offset
, (vm_object_size_t
)size
,
8610 PMAP_NULL
, 0, VM_PROT_NONE
);
8611 assert(!object
->blocked_access
);
8612 object
->blocked_access
= TRUE
;
8615 VM_DEBUG_CONSTANT_EVENT(vm_object_iopl_request
, VM_IOPL_REQUEST
, DBG_FUNC_END
, page_grab_count
, KERN_SUCCESS
, 0, 0);
8616 return KERN_SUCCESS
;
8621 for (; offset
< dst_offset
; offset
+= PAGE_SIZE
) {
8622 boolean_t need_unwire
;
8624 dst_page
= vm_page_lookup(object
, offset
);
8626 if (dst_page
== VM_PAGE_NULL
)
8627 panic("vm_object_iopl_request: Wired page missing. \n");
8630 * if we've already processed this page in an earlier
8631 * dw_do_work, we need to undo the wiring... we will
8632 * leave the dirty and reference bits on if they
8633 * were set, since we don't have a good way of knowing
8634 * what the previous state was and we won't get here
8635 * under any normal circumstances... we will always
8636 * clear BUSY and wakeup any waiters via vm_page_free
8637 * or PAGE_WAKEUP_DONE
8642 if (dw_array
[dw_index
].dw_m
== dst_page
) {
8644 * still in the deferred work list
8645 * which means we haven't yet called
8646 * vm_page_wire on this page
8648 need_unwire
= FALSE
;
8654 vm_page_lock_queues();
8656 if (dst_page
->vmp_absent
|| free_wired_pages
== TRUE
) {
8657 vm_page_free(dst_page
);
8659 need_unwire
= FALSE
;
8661 if (need_unwire
== TRUE
)
8662 vm_page_unwire(dst_page
, TRUE
);
8664 PAGE_WAKEUP_DONE(dst_page
);
8666 vm_page_unlock_queues();
8668 if (need_unwire
== TRUE
)
8669 VM_STAT_INCR(reactivations
);
8674 if (! (upl
->flags
& UPL_KERNEL_OBJECT
)) {
8675 vm_object_activity_end(object
);
8676 vm_object_collapse(object
, 0, TRUE
);
8678 vm_object_unlock(object
);
8681 VM_DEBUG_CONSTANT_EVENT(vm_object_iopl_request
, VM_IOPL_REQUEST
, DBG_FUNC_END
, page_grab_count
, ret
, 0, 0);
8690 kern_return_t retval
;
8691 boolean_t upls_locked
;
8692 vm_object_t object1
, object2
;
8694 if (upl1
== UPL_NULL
|| upl2
== UPL_NULL
|| upl1
== upl2
|| ((upl1
->flags
& UPL_VECTOR
)==UPL_VECTOR
) || ((upl2
->flags
& UPL_VECTOR
)==UPL_VECTOR
)) {
8695 return KERN_INVALID_ARGUMENT
;
8698 upls_locked
= FALSE
;
8701 * Since we need to lock both UPLs at the same time,
8702 * avoid deadlocks by always taking locks in the same order.
8711 upls_locked
= TRUE
; /* the UPLs will need to be unlocked */
8713 object1
= upl1
->map_object
;
8714 object2
= upl2
->map_object
;
8716 if (upl1
->offset
!= 0 || upl2
->offset
!= 0 ||
8717 upl1
->size
!= upl2
->size
) {
8719 * We deal only with full objects, not subsets.
8720 * That's because we exchange the entire backing store info
8721 * for the objects: pager, resident pages, etc... We can't do
8724 retval
= KERN_INVALID_VALUE
;
8729 * Tranpose the VM objects' backing store.
8731 retval
= vm_object_transpose(object1
, object2
,
8732 (vm_object_size_t
) upl1
->size
);
8734 if (retval
== KERN_SUCCESS
) {
8736 * Make each UPL point to the correct VM object, i.e. the
8737 * object holding the pages that the UPL refers to...
8739 #if CONFIG_IOSCHED || UPL_DEBUG
8740 if ((upl1
->flags
& UPL_TRACKED_BY_OBJECT
) || (upl2
->flags
& UPL_TRACKED_BY_OBJECT
)) {
8741 vm_object_lock(object1
);
8742 vm_object_lock(object2
);
8744 if (upl1
->flags
& UPL_TRACKED_BY_OBJECT
)
8745 queue_remove(&object1
->uplq
, upl1
, upl_t
, uplq
);
8746 if (upl2
->flags
& UPL_TRACKED_BY_OBJECT
)
8747 queue_remove(&object2
->uplq
, upl2
, upl_t
, uplq
);
8749 upl1
->map_object
= object2
;
8750 upl2
->map_object
= object1
;
8752 #if CONFIG_IOSCHED || UPL_DEBUG
8753 if (upl1
->flags
& UPL_TRACKED_BY_OBJECT
)
8754 queue_enter(&object2
->uplq
, upl1
, upl_t
, uplq
);
8755 if (upl2
->flags
& UPL_TRACKED_BY_OBJECT
)
8756 queue_enter(&object1
->uplq
, upl2
, upl_t
, uplq
);
8757 if ((upl1
->flags
& UPL_TRACKED_BY_OBJECT
) || (upl2
->flags
& UPL_TRACKED_BY_OBJECT
)) {
8758 vm_object_unlock(object2
);
8759 vm_object_unlock(object1
);
8771 upls_locked
= FALSE
;
8783 upl_page_info_t
*user_page_list
;
8786 if ( !(upl
->flags
& UPL_INTERNAL
) || count
<= 0)
8789 size_in_pages
= upl
->size
/ PAGE_SIZE
;
8791 user_page_list
= (upl_page_info_t
*) (((uintptr_t)upl
) + sizeof(struct upl
));
8793 while (count
-- && index
< size_in_pages
)
8794 user_page_list
[index
++].needed
= TRUE
;
8799 * Reserve of virtual addresses in the kernel address space.
8800 * We need to map the physical pages in the kernel, so that we
8801 * can call the code-signing or slide routines with a kernel
8802 * virtual address. We keep this pool of pre-allocated kernel
8803 * virtual addresses so that we don't have to scan the kernel's
8804 * virtaul address space each time we need to work with
8807 decl_simple_lock_data(,vm_paging_lock
)
8808 #define VM_PAGING_NUM_PAGES 64
8809 vm_map_offset_t vm_paging_base_address
= 0;
8810 boolean_t vm_paging_page_inuse
[VM_PAGING_NUM_PAGES
] = { FALSE
, };
8811 int vm_paging_max_index
= 0;
8812 int vm_paging_page_waiter
= 0;
8813 int vm_paging_page_waiter_total
= 0;
8815 unsigned long vm_paging_no_kernel_page
= 0;
8816 unsigned long vm_paging_objects_mapped
= 0;
8817 unsigned long vm_paging_pages_mapped
= 0;
8818 unsigned long vm_paging_objects_mapped_slow
= 0;
8819 unsigned long vm_paging_pages_mapped_slow
= 0;
8822 vm_paging_map_init(void)
8825 vm_map_offset_t page_map_offset
;
8826 vm_map_entry_t map_entry
;
8828 assert(vm_paging_base_address
== 0);
8831 * Initialize our pool of pre-allocated kernel
8832 * virtual addresses.
8834 page_map_offset
= 0;
8835 kr
= vm_map_find_space(kernel_map
,
8837 VM_PAGING_NUM_PAGES
* PAGE_SIZE
,
8840 VM_MAP_KERNEL_FLAGS_NONE
,
8841 VM_KERN_MEMORY_NONE
,
8843 if (kr
!= KERN_SUCCESS
) {
8844 panic("vm_paging_map_init: kernel_map full\n");
8846 VME_OBJECT_SET(map_entry
, kernel_object
);
8847 VME_OFFSET_SET(map_entry
, page_map_offset
);
8848 map_entry
->protection
= VM_PROT_NONE
;
8849 map_entry
->max_protection
= VM_PROT_NONE
;
8850 map_entry
->permanent
= TRUE
;
8851 vm_object_reference(kernel_object
);
8852 vm_map_unlock(kernel_map
);
8854 assert(vm_paging_base_address
== 0);
8855 vm_paging_base_address
= page_map_offset
;
8859 * vm_paging_map_object:
8860 * Maps part of a VM object's pages in the kernel
8861 * virtual address space, using the pre-allocated
8862 * kernel virtual addresses, if possible.
8864 * The VM object is locked. This lock will get
8865 * dropped and re-acquired though, so the caller
8866 * must make sure the VM object is kept alive
8867 * (by holding a VM map that has a reference
8868 * on it, for example, or taking an extra reference).
8869 * The page should also be kept busy to prevent
8870 * it from being reclaimed.
8873 vm_paging_map_object(
8876 vm_object_offset_t offset
,
8877 vm_prot_t protection
,
8878 boolean_t can_unlock_object
,
8879 vm_map_size_t
*size
, /* IN/OUT */
8880 vm_map_offset_t
*address
, /* OUT */
8881 boolean_t
*need_unmap
) /* OUT */
8884 vm_map_offset_t page_map_offset
;
8885 vm_map_size_t map_size
;
8886 vm_object_offset_t object_offset
;
8889 if (page
!= VM_PAGE_NULL
&& *size
== PAGE_SIZE
) {
8890 /* use permanent 1-to-1 kernel mapping of physical memory ? */
8892 *address
= (vm_map_offset_t
)
8893 PHYSMAP_PTOV((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(page
) <<
8895 *need_unmap
= FALSE
;
8896 return KERN_SUCCESS
;
8897 #elif __arm__ || __arm64__
8898 *address
= (vm_map_offset_t
)
8899 phystokv((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(page
) << PAGE_SHIFT
);
8900 *need_unmap
= FALSE
;
8901 return KERN_SUCCESS
;
8903 #warn "vm_paging_map_object: no 1-to-1 kernel mapping of physical memory..."
8906 assert(page
->vmp_busy
);
8908 * Use one of the pre-allocated kernel virtual addresses
8909 * and just enter the VM page in the kernel address space
8910 * at that virtual address.
8912 simple_lock(&vm_paging_lock
);
8915 * Try and find an available kernel virtual address
8916 * from our pre-allocated pool.
8918 page_map_offset
= 0;
8920 for (i
= 0; i
< VM_PAGING_NUM_PAGES
; i
++) {
8921 if (vm_paging_page_inuse
[i
] == FALSE
) {
8923 vm_paging_base_address
+
8928 if (page_map_offset
!= 0) {
8929 /* found a space to map our page ! */
8933 if (can_unlock_object
) {
8935 * If we can afford to unlock the VM object,
8936 * let's take the slow path now...
8941 * We can't afford to unlock the VM object, so
8942 * let's wait for a space to become available...
8944 vm_paging_page_waiter_total
++;
8945 vm_paging_page_waiter
++;
8946 kr
= assert_wait((event_t
)&vm_paging_page_waiter
, THREAD_UNINT
);
8947 if (kr
== THREAD_WAITING
) {
8948 simple_unlock(&vm_paging_lock
);
8949 kr
= thread_block(THREAD_CONTINUE_NULL
);
8950 simple_lock(&vm_paging_lock
);
8952 vm_paging_page_waiter
--;
8953 /* ... and try again */
8956 if (page_map_offset
!= 0) {
8958 * We found a kernel virtual address;
8959 * map the physical page to that virtual address.
8961 if (i
> vm_paging_max_index
) {
8962 vm_paging_max_index
= i
;
8964 vm_paging_page_inuse
[i
] = TRUE
;
8965 simple_unlock(&vm_paging_lock
);
8967 page
->vmp_pmapped
= TRUE
;
8970 * Keep the VM object locked over the PMAP_ENTER
8971 * and the actual use of the page by the kernel,
8972 * or this pmap mapping might get undone by a
8973 * vm_object_pmap_protect() call...
8975 PMAP_ENTER(kernel_pmap
,
8983 assert(kr
== KERN_SUCCESS
);
8984 vm_paging_objects_mapped
++;
8985 vm_paging_pages_mapped
++;
8986 *address
= page_map_offset
;
8990 kasan_notify_address(page_map_offset
, PAGE_SIZE
);
8993 /* all done and mapped, ready to use ! */
8994 return KERN_SUCCESS
;
8998 * We ran out of pre-allocated kernel virtual
8999 * addresses. Just map the page in the kernel
9000 * the slow and regular way.
9002 vm_paging_no_kernel_page
++;
9003 simple_unlock(&vm_paging_lock
);
9006 if (! can_unlock_object
) {
9009 *need_unmap
= FALSE
;
9010 return KERN_NOT_SUPPORTED
;
9013 object_offset
= vm_object_trunc_page(offset
);
9014 map_size
= vm_map_round_page(*size
,
9015 VM_MAP_PAGE_MASK(kernel_map
));
9018 * Try and map the required range of the object
9022 vm_object_reference_locked(object
); /* for the map entry */
9023 vm_object_unlock(object
);
9025 kr
= vm_map_enter(kernel_map
,
9030 VM_MAP_KERNEL_FLAGS_NONE
,
9031 VM_KERN_MEMORY_NONE
,
9038 if (kr
!= KERN_SUCCESS
) {
9041 *need_unmap
= FALSE
;
9042 vm_object_deallocate(object
); /* for the map entry */
9043 vm_object_lock(object
);
9050 * Enter the mapped pages in the page table now.
9052 vm_object_lock(object
);
9054 * VM object must be kept locked from before PMAP_ENTER()
9055 * until after the kernel is done accessing the page(s).
9056 * Otherwise, the pmap mappings in the kernel could be
9057 * undone by a call to vm_object_pmap_protect().
9060 for (page_map_offset
= 0;
9062 map_size
-= PAGE_SIZE_64
, page_map_offset
+= PAGE_SIZE_64
) {
9064 page
= vm_page_lookup(object
, offset
+ page_map_offset
);
9065 if (page
== VM_PAGE_NULL
) {
9066 printf("vm_paging_map_object: no page !?");
9067 vm_object_unlock(object
);
9068 kr
= vm_map_remove(kernel_map
, *address
, *size
,
9069 VM_MAP_REMOVE_NO_FLAGS
);
9070 assert(kr
== KERN_SUCCESS
);
9073 *need_unmap
= FALSE
;
9074 vm_object_lock(object
);
9075 return KERN_MEMORY_ERROR
;
9077 page
->vmp_pmapped
= TRUE
;
9079 //assert(pmap_verify_free(VM_PAGE_GET_PHYS_PAGE(page)));
9080 PMAP_ENTER(kernel_pmap
,
9081 *address
+ page_map_offset
,
9088 assert(kr
== KERN_SUCCESS
);
9090 kasan_notify_address(*address
+ page_map_offset
, PAGE_SIZE
);
9094 vm_paging_objects_mapped_slow
++;
9095 vm_paging_pages_mapped_slow
+= (unsigned long) (map_size
/ PAGE_SIZE_64
);
9099 return KERN_SUCCESS
;
9103 * vm_paging_unmap_object:
9104 * Unmaps part of a VM object's pages from the kernel
9105 * virtual address space.
9107 * The VM object is locked. This lock will get
9108 * dropped and re-acquired though.
9111 vm_paging_unmap_object(
9113 vm_map_offset_t start
,
9114 vm_map_offset_t end
)
9119 if ((vm_paging_base_address
== 0) ||
9120 (start
< vm_paging_base_address
) ||
9121 (end
> (vm_paging_base_address
9122 + (VM_PAGING_NUM_PAGES
* PAGE_SIZE
)))) {
9124 * We didn't use our pre-allocated pool of
9125 * kernel virtual address. Deallocate the
9128 if (object
!= VM_OBJECT_NULL
) {
9129 vm_object_unlock(object
);
9131 kr
= vm_map_remove(kernel_map
, start
, end
,
9132 VM_MAP_REMOVE_NO_FLAGS
);
9133 if (object
!= VM_OBJECT_NULL
) {
9134 vm_object_lock(object
);
9136 assert(kr
== KERN_SUCCESS
);
9139 * We used a kernel virtual address from our
9140 * pre-allocated pool. Put it back in the pool
9143 assert(end
- start
== PAGE_SIZE
);
9144 i
= (int) ((start
- vm_paging_base_address
) >> PAGE_SHIFT
);
9145 assert(i
>= 0 && i
< VM_PAGING_NUM_PAGES
);
9147 /* undo the pmap mapping */
9148 pmap_remove(kernel_pmap
, start
, end
);
9150 simple_lock(&vm_paging_lock
);
9151 vm_paging_page_inuse
[i
] = FALSE
;
9152 if (vm_paging_page_waiter
) {
9153 thread_wakeup(&vm_paging_page_waiter
);
9155 simple_unlock(&vm_paging_lock
);
9161 * page->vmp_object must be locked
9164 vm_pageout_steal_laundry(vm_page_t page
, boolean_t queues_locked
)
9166 if (!queues_locked
) {
9167 vm_page_lockspin_queues();
9170 page
->vmp_free_when_done
= FALSE
;
9172 * need to drop the laundry count...
9173 * we may also need to remove it
9174 * from the I/O paging queue...
9175 * vm_pageout_throttle_up handles both cases
9177 * the laundry and pageout_queue flags are cleared...
9179 vm_pageout_throttle_up(page
);
9181 if (!queues_locked
) {
9182 vm_page_unlock_queues();
9187 vector_upl_create(vm_offset_t upl_offset
)
9189 int vector_upl_size
= sizeof(struct _vector_upl
);
9192 vector_upl_t vector_upl
= (vector_upl_t
)kalloc(vector_upl_size
);
9194 upl
= upl_create(0,UPL_VECTOR
,0);
9195 upl
->vector_upl
= vector_upl
;
9196 upl
->offset
= upl_offset
;
9197 vector_upl
->size
= 0;
9198 vector_upl
->offset
= upl_offset
;
9199 vector_upl
->invalid_upls
=0;
9200 vector_upl
->num_upls
=0;
9201 vector_upl
->pagelist
= NULL
;
9203 for(i
=0; i
< MAX_VECTOR_UPL_ELEMENTS
; i
++) {
9204 vector_upl
->upl_iostates
[i
].size
= 0;
9205 vector_upl
->upl_iostates
[i
].offset
= 0;
9212 vector_upl_deallocate(upl_t upl
)
9215 vector_upl_t vector_upl
= upl
->vector_upl
;
9217 if(vector_upl
->invalid_upls
!= vector_upl
->num_upls
)
9218 panic("Deallocating non-empty Vectored UPL\n");
9219 kfree(vector_upl
->pagelist
,(sizeof(struct upl_page_info
)*(vector_upl
->size
/PAGE_SIZE
)));
9220 vector_upl
->invalid_upls
=0;
9221 vector_upl
->num_upls
= 0;
9222 vector_upl
->pagelist
= NULL
;
9223 vector_upl
->size
= 0;
9224 vector_upl
->offset
= 0;
9225 kfree(vector_upl
, sizeof(struct _vector_upl
));
9226 vector_upl
= (vector_upl_t
)0xfeedfeed;
9229 panic("vector_upl_deallocate was passed a non-vectored upl\n");
9232 panic("vector_upl_deallocate was passed a NULL upl\n");
9236 vector_upl_is_valid(upl_t upl
)
9238 if(upl
&& ((upl
->flags
& UPL_VECTOR
)==UPL_VECTOR
)) {
9239 vector_upl_t vector_upl
= upl
->vector_upl
;
9240 if(vector_upl
== NULL
|| vector_upl
== (vector_upl_t
)0xfeedfeed || vector_upl
== (vector_upl_t
)0xfeedbeef)
9249 vector_upl_set_subupl(upl_t upl
,upl_t subupl
, uint32_t io_size
)
9251 if(vector_upl_is_valid(upl
)) {
9252 vector_upl_t vector_upl
= upl
->vector_upl
;
9257 if(io_size
< PAGE_SIZE
)
9258 io_size
= PAGE_SIZE
;
9259 subupl
->vector_upl
= (void*)vector_upl
;
9260 vector_upl
->upl_elems
[vector_upl
->num_upls
++] = subupl
;
9261 vector_upl
->size
+= io_size
;
9262 upl
->size
+= io_size
;
9265 uint32_t i
=0,invalid_upls
=0;
9266 for(i
= 0; i
< vector_upl
->num_upls
; i
++) {
9267 if(vector_upl
->upl_elems
[i
] == subupl
)
9270 if(i
== vector_upl
->num_upls
)
9271 panic("Trying to remove sub-upl when none exists");
9273 vector_upl
->upl_elems
[i
] = NULL
;
9274 invalid_upls
= hw_atomic_add(&(vector_upl
)->invalid_upls
, 1);
9275 if(invalid_upls
== vector_upl
->num_upls
)
9282 panic("vector_upl_set_subupl was passed a NULL upl element\n");
9285 panic("vector_upl_set_subupl was passed a non-vectored upl\n");
9288 panic("vector_upl_set_subupl was passed a NULL upl\n");
9294 vector_upl_set_pagelist(upl_t upl
)
9296 if(vector_upl_is_valid(upl
)) {
9298 vector_upl_t vector_upl
= upl
->vector_upl
;
9301 vm_offset_t pagelist_size
=0, cur_upl_pagelist_size
=0;
9303 vector_upl
->pagelist
= (upl_page_info_array_t
)kalloc(sizeof(struct upl_page_info
)*(vector_upl
->size
/PAGE_SIZE
));
9305 for(i
=0; i
< vector_upl
->num_upls
; i
++) {
9306 cur_upl_pagelist_size
= sizeof(struct upl_page_info
) * vector_upl
->upl_elems
[i
]->size
/PAGE_SIZE
;
9307 bcopy(UPL_GET_INTERNAL_PAGE_LIST_SIMPLE(vector_upl
->upl_elems
[i
]), (char*)vector_upl
->pagelist
+ pagelist_size
, cur_upl_pagelist_size
);
9308 pagelist_size
+= cur_upl_pagelist_size
;
9309 if(vector_upl
->upl_elems
[i
]->highest_page
> upl
->highest_page
)
9310 upl
->highest_page
= vector_upl
->upl_elems
[i
]->highest_page
;
9312 assert( pagelist_size
== (sizeof(struct upl_page_info
)*(vector_upl
->size
/PAGE_SIZE
)) );
9315 panic("vector_upl_set_pagelist was passed a non-vectored upl\n");
9318 panic("vector_upl_set_pagelist was passed a NULL upl\n");
9323 vector_upl_subupl_byindex(upl_t upl
, uint32_t index
)
9325 if(vector_upl_is_valid(upl
)) {
9326 vector_upl_t vector_upl
= upl
->vector_upl
;
9328 if(index
< vector_upl
->num_upls
)
9329 return vector_upl
->upl_elems
[index
];
9332 panic("vector_upl_subupl_byindex was passed a non-vectored upl\n");
9338 vector_upl_subupl_byoffset(upl_t upl
, upl_offset_t
*upl_offset
, upl_size_t
*upl_size
)
9340 if(vector_upl_is_valid(upl
)) {
9342 vector_upl_t vector_upl
= upl
->vector_upl
;
9345 upl_t subupl
= NULL
;
9346 vector_upl_iostates_t subupl_state
;
9348 for(i
=0; i
< vector_upl
->num_upls
; i
++) {
9349 subupl
= vector_upl
->upl_elems
[i
];
9350 subupl_state
= vector_upl
->upl_iostates
[i
];
9351 if( *upl_offset
<= (subupl_state
.offset
+ subupl_state
.size
- 1)) {
9352 /* We could have been passed an offset/size pair that belongs
9353 * to an UPL element that has already been committed/aborted.
9354 * If so, return NULL.
9358 if((subupl_state
.offset
+ subupl_state
.size
) < (*upl_offset
+ *upl_size
)) {
9359 *upl_size
= (subupl_state
.offset
+ subupl_state
.size
) - *upl_offset
;
9360 if(*upl_size
> subupl_state
.size
)
9361 *upl_size
= subupl_state
.size
;
9363 if(*upl_offset
>= subupl_state
.offset
)
9364 *upl_offset
-= subupl_state
.offset
;
9366 panic("Vector UPL offset miscalculation\n");
9372 panic("vector_upl_subupl_byoffset was passed a non-vectored UPL\n");
9378 vector_upl_get_submap(upl_t upl
, vm_map_t
*v_upl_submap
, vm_offset_t
*submap_dst_addr
)
9380 *v_upl_submap
= NULL
;
9382 if(vector_upl_is_valid(upl
)) {
9383 vector_upl_t vector_upl
= upl
->vector_upl
;
9385 *v_upl_submap
= vector_upl
->submap
;
9386 *submap_dst_addr
= vector_upl
->submap_dst_addr
;
9389 panic("vector_upl_get_submap was passed a non-vectored UPL\n");
9392 panic("vector_upl_get_submap was passed a null UPL\n");
9396 vector_upl_set_submap(upl_t upl
, vm_map_t submap
, vm_offset_t submap_dst_addr
)
9398 if(vector_upl_is_valid(upl
)) {
9399 vector_upl_t vector_upl
= upl
->vector_upl
;
9401 vector_upl
->submap
= submap
;
9402 vector_upl
->submap_dst_addr
= submap_dst_addr
;
9405 panic("vector_upl_get_submap was passed a non-vectored UPL\n");
9408 panic("vector_upl_get_submap was passed a NULL UPL\n");
9412 vector_upl_set_iostate(upl_t upl
, upl_t subupl
, upl_offset_t offset
, upl_size_t size
)
9414 if(vector_upl_is_valid(upl
)) {
9416 vector_upl_t vector_upl
= upl
->vector_upl
;
9419 for(i
= 0; i
< vector_upl
->num_upls
; i
++) {
9420 if(vector_upl
->upl_elems
[i
] == subupl
)
9424 if(i
== vector_upl
->num_upls
)
9425 panic("setting sub-upl iostate when none exists");
9427 vector_upl
->upl_iostates
[i
].offset
= offset
;
9428 if(size
< PAGE_SIZE
)
9430 vector_upl
->upl_iostates
[i
].size
= size
;
9433 panic("vector_upl_set_iostate was passed a non-vectored UPL\n");
9436 panic("vector_upl_set_iostate was passed a NULL UPL\n");
9440 vector_upl_get_iostate(upl_t upl
, upl_t subupl
, upl_offset_t
*offset
, upl_size_t
*size
)
9442 if(vector_upl_is_valid(upl
)) {
9444 vector_upl_t vector_upl
= upl
->vector_upl
;
9447 for(i
= 0; i
< vector_upl
->num_upls
; i
++) {
9448 if(vector_upl
->upl_elems
[i
] == subupl
)
9452 if(i
== vector_upl
->num_upls
)
9453 panic("getting sub-upl iostate when none exists");
9455 *offset
= vector_upl
->upl_iostates
[i
].offset
;
9456 *size
= vector_upl
->upl_iostates
[i
].size
;
9459 panic("vector_upl_get_iostate was passed a non-vectored UPL\n");
9462 panic("vector_upl_get_iostate was passed a NULL UPL\n");
9466 vector_upl_get_iostate_byindex(upl_t upl
, uint32_t index
, upl_offset_t
*offset
, upl_size_t
*size
)
9468 if(vector_upl_is_valid(upl
)) {
9469 vector_upl_t vector_upl
= upl
->vector_upl
;
9471 if(index
< vector_upl
->num_upls
) {
9472 *offset
= vector_upl
->upl_iostates
[index
].offset
;
9473 *size
= vector_upl
->upl_iostates
[index
].size
;
9476 *offset
= *size
= 0;
9479 panic("vector_upl_get_iostate_byindex was passed a non-vectored UPL\n");
9482 panic("vector_upl_get_iostate_byindex was passed a NULL UPL\n");
9486 upl_get_internal_vectorupl_pagelist(upl_t upl
)
9488 return ((vector_upl_t
)(upl
->vector_upl
))->pagelist
;
9492 upl_get_internal_vectorupl(upl_t upl
)
9494 return upl
->vector_upl
;
9498 upl_get_internal_pagelist_offset(void)
9500 return sizeof(struct upl
);
9509 upl
->flags
|= UPL_CLEAR_DIRTY
;
9511 upl
->flags
&= ~UPL_CLEAR_DIRTY
;
9522 upl
->ext_ref_count
++;
9524 if (!upl
->ext_ref_count
) {
9525 panic("upl_set_referenced not %p\n", upl
);
9527 upl
->ext_ref_count
--;
9536 vm_offset_t upl_offset
,
9541 if ((upl
->flags
& UPL_EXPEDITE_SUPPORTED
) == 0)
9544 assert(upl
->upl_reprio_info
!= 0);
9545 for(i
= (int)(upl_offset
/ PAGE_SIZE
), j
= 0; j
< io_size
; i
++, j
+= PAGE_SIZE
) {
9546 UPL_SET_REPRIO_INFO(upl
, i
, blkno
, io_size
);
9551 void inline memoryshot(unsigned int event
, unsigned int control
)
9553 if (vm_debug_events
) {
9554 KERNEL_DEBUG_CONSTANT1((MACHDBG_CODE(DBG_MACH_VM_PRESSURE
, event
)) | control
,
9555 vm_page_active_count
, vm_page_inactive_count
,
9556 vm_page_free_count
, vm_page_speculative_count
,
9557 vm_page_throttled_count
);
9567 boolean_t
upl_device_page(upl_page_info_t
*upl
)
9569 return(UPL_DEVICE_PAGE(upl
));
9571 boolean_t
upl_page_present(upl_page_info_t
*upl
, int index
)
9573 return(UPL_PAGE_PRESENT(upl
, index
));
9575 boolean_t
upl_speculative_page(upl_page_info_t
*upl
, int index
)
9577 return(UPL_SPECULATIVE_PAGE(upl
, index
));
9579 boolean_t
upl_dirty_page(upl_page_info_t
*upl
, int index
)
9581 return(UPL_DIRTY_PAGE(upl
, index
));
9583 boolean_t
upl_valid_page(upl_page_info_t
*upl
, int index
)
9585 return(UPL_VALID_PAGE(upl
, index
));
9587 ppnum_t
upl_phys_page(upl_page_info_t
*upl
, int index
)
9589 return(UPL_PHYS_PAGE(upl
, index
));
9592 void upl_page_set_mark(upl_page_info_t
*upl
, int index
, boolean_t v
)
9594 upl
[index
].mark
= v
;
9597 boolean_t
upl_page_get_mark(upl_page_info_t
*upl
, int index
)
9599 return upl
[index
].mark
;
9603 vm_countdirtypages(void)
9615 vm_page_lock_queues();
9616 m
= (vm_page_t
) vm_page_queue_first(&vm_page_queue_inactive
);
9618 if (m
==(vm_page_t
)0) break;
9620 if(m
->vmp_dirty
) dpages
++;
9621 if(m
->vmp_free_when_done
) pgopages
++;
9622 if(m
->vmp_precious
) precpages
++;
9624 assert(VM_PAGE_OBJECT(m
) != kernel_object
);
9625 m
= (vm_page_t
) vm_page_queue_next(&m
->vmp_pageq
);
9626 if (m
==(vm_page_t
)0) break;
9628 } while (!vm_page_queue_end(&vm_page_queue_inactive
, (vm_page_queue_entry_t
) m
));
9629 vm_page_unlock_queues();
9631 vm_page_lock_queues();
9632 m
= (vm_page_t
) vm_page_queue_first(&vm_page_queue_throttled
);
9634 if (m
==(vm_page_t
)0) break;
9637 assert(m
->vmp_dirty
);
9638 assert(!m
->vmp_free_when_done
);
9639 assert(VM_PAGE_OBJECT(m
) != kernel_object
);
9640 m
= (vm_page_t
) vm_page_queue_next(&m
->vmp_pageq
);
9641 if (m
==(vm_page_t
)0) break;
9643 } while (!vm_page_queue_end(&vm_page_queue_throttled
, (vm_page_queue_entry_t
) m
));
9644 vm_page_unlock_queues();
9646 vm_page_lock_queues();
9647 m
= (vm_page_t
) vm_page_queue_first(&vm_page_queue_anonymous
);
9649 if (m
==(vm_page_t
)0) break;
9651 if(m
->vmp_dirty
) dpages
++;
9652 if(m
->vmp_free_when_done
) pgopages
++;
9653 if(m
->vmp_precious
) precpages
++;
9655 assert(VM_PAGE_OBJECT(m
) != kernel_object
);
9656 m
= (vm_page_t
) vm_page_queue_next(&m
->vmp_pageq
);
9657 if (m
==(vm_page_t
)0) break;
9659 } while (!vm_page_queue_end(&vm_page_queue_anonymous
, (vm_page_queue_entry_t
) m
));
9660 vm_page_unlock_queues();
9662 printf("IN Q: %d : %d : %d\n", dpages
, pgopages
, precpages
);
9668 vm_page_lock_queues();
9669 m
= (vm_page_t
) vm_page_queue_first(&vm_page_queue_active
);
9672 if(m
== (vm_page_t
)0) break;
9673 if(m
->vmp_dirty
) dpages
++;
9674 if(m
->vmp_free_when_done
) pgopages
++;
9675 if(m
->vmp_precious
) precpages
++;
9677 assert(VM_PAGE_OBJECT(m
) != kernel_object
);
9678 m
= (vm_page_t
) vm_page_queue_next(&m
->vmp_pageq
);
9679 if(m
== (vm_page_t
)0) break;
9681 } while (!vm_page_queue_end(&vm_page_queue_active
, (vm_page_queue_entry_t
) m
));
9682 vm_page_unlock_queues();
9684 printf("AC Q: %d : %d : %d\n", dpages
, pgopages
, precpages
);
9687 #endif /* MACH_BSD */
9691 int upl_get_cached_tier(upl_t upl
)
9694 if (upl
->flags
& UPL_TRACKED_BY_OBJECT
)
9695 return (upl
->upl_priority
);
9698 #endif /* CONFIG_IOSCHED */
9701 void upl_callout_iodone(upl_t upl
)
9703 struct upl_io_completion
*upl_ctx
= upl
->upl_iodone
;
9706 void (*iodone_func
)(void *, int) = upl_ctx
->io_done
;
9708 assert(upl_ctx
->io_done
);
9710 (*iodone_func
)(upl_ctx
->io_context
, upl_ctx
->io_error
);
9714 void upl_set_iodone(upl_t upl
, void *upl_iodone
)
9716 upl
->upl_iodone
= (struct upl_io_completion
*)upl_iodone
;
9719 void upl_set_iodone_error(upl_t upl
, int error
)
9721 struct upl_io_completion
*upl_ctx
= upl
->upl_iodone
;
9724 upl_ctx
->io_error
= error
;
9728 ppnum_t
upl_get_highest_page(
9731 return upl
->highest_page
;
9734 upl_size_t
upl_get_size(
9740 upl_t
upl_associated_upl(upl_t upl
)
9742 return upl
->associated_upl
;
9745 void upl_set_associated_upl(upl_t upl
, upl_t associated_upl
)
9747 upl
->associated_upl
= associated_upl
;
9750 struct vnode
* upl_lookup_vnode(upl_t upl
)
9752 if (!upl
->map_object
->internal
)
9753 return vnode_pager_lookup_vnode(upl
->map_object
->pager
);
9759 kern_return_t
upl_ubc_alias_set(upl_t upl
, uintptr_t alias1
, uintptr_t alias2
)
9761 upl
->ubc_alias1
= alias1
;
9762 upl
->ubc_alias2
= alias2
;
9763 return KERN_SUCCESS
;
9765 int upl_ubc_alias_get(upl_t upl
, uintptr_t * al
, uintptr_t * al2
)
9768 *al
= upl
->ubc_alias1
;
9770 *al2
= upl
->ubc_alias2
;
9771 return KERN_SUCCESS
;
9773 #endif /* UPL_DEBUG */
9775 #if VM_PRESSURE_EVENTS
9777 * Upward trajectory.
9779 extern boolean_t
vm_compressor_low_on_space(void);
9782 VM_PRESSURE_NORMAL_TO_WARNING(void) {
9784 if ( !VM_CONFIG_COMPRESSOR_IS_ACTIVE
) {
9786 /* Available pages below our threshold */
9787 if (memorystatus_available_pages
< memorystatus_available_pages_pressure
) {
9788 /* No frozen processes to kill */
9789 if (memorystatus_frozen_count
== 0) {
9790 /* Not enough suspended processes available. */
9791 if (memorystatus_suspended_count
< MEMORYSTATUS_SUSPENDED_THRESHOLD
) {
9799 return ((AVAILABLE_NON_COMPRESSED_MEMORY
< VM_PAGE_COMPRESSOR_COMPACT_THRESHOLD
) ? 1 : 0);
9804 VM_PRESSURE_WARNING_TO_CRITICAL(void) {
9806 if ( !VM_CONFIG_COMPRESSOR_IS_ACTIVE
) {
9808 /* Available pages below our threshold */
9809 if (memorystatus_available_pages
< memorystatus_available_pages_critical
) {
9814 return (vm_compressor_low_on_space() || (AVAILABLE_NON_COMPRESSED_MEMORY
< ((12 * VM_PAGE_COMPRESSOR_SWAP_UNTHROTTLE_THRESHOLD
) / 10)) ? 1 : 0);
9819 * Downward trajectory.
9822 VM_PRESSURE_WARNING_TO_NORMAL(void) {
9824 if ( !VM_CONFIG_COMPRESSOR_IS_ACTIVE
) {
9826 /* Available pages above our threshold */
9827 unsigned int target_threshold
= (unsigned int) (memorystatus_available_pages_pressure
+ ((15 * memorystatus_available_pages_pressure
) / 100));
9828 if (memorystatus_available_pages
> target_threshold
) {
9833 return ((AVAILABLE_NON_COMPRESSED_MEMORY
> ((12 * VM_PAGE_COMPRESSOR_COMPACT_THRESHOLD
) / 10)) ? 1 : 0);
9838 VM_PRESSURE_CRITICAL_TO_WARNING(void) {
9840 if ( !VM_CONFIG_COMPRESSOR_IS_ACTIVE
) {
9842 /* Available pages above our threshold */
9843 unsigned int target_threshold
= (unsigned int)(memorystatus_available_pages_critical
+ ((15 * memorystatus_available_pages_critical
) / 100));
9844 if (memorystatus_available_pages
> target_threshold
) {
9849 return ((AVAILABLE_NON_COMPRESSED_MEMORY
> ((14 * VM_PAGE_COMPRESSOR_SWAP_UNTHROTTLE_THRESHOLD
) / 10)) ? 1 : 0);
9852 #endif /* VM_PRESSURE_EVENTS */
9856 #define VM_TEST_COLLAPSE_COMPRESSOR 0
9857 #define VM_TEST_WIRE_AND_EXTRACT 0
9858 #define VM_TEST_PAGE_WIRE_OVERFLOW_PANIC 0
9860 #define VM_TEST_KERNEL_OBJECT_FAULT 0
9861 #endif /* __arm64__ */
9862 #define VM_TEST_DEVICE_PAGER_TRANSPOSE (DEVELOPMENT || DEBUG)
9864 #if VM_TEST_COLLAPSE_COMPRESSOR
9865 extern boolean_t vm_object_collapse_compressor_allowed
;
9866 #include <IOKit/IOLib.h>
9868 vm_test_collapse_compressor(void)
9870 vm_object_size_t backing_size
, top_size
;
9871 vm_object_t backing_object
, top_object
;
9872 vm_map_offset_t backing_offset
, top_offset
;
9873 unsigned char *backing_address
, *top_address
;
9876 printf("VM_TEST_COLLAPSE_COMPRESSOR:\n");
9878 /* create backing object */
9879 backing_size
= 15 * PAGE_SIZE
;
9880 backing_object
= vm_object_allocate(backing_size
);
9881 assert(backing_object
!= VM_OBJECT_NULL
);
9882 printf("VM_TEST_COLLAPSE_COMPRESSOR: created backing object %p\n",
9884 /* map backing object */
9886 kr
= vm_map_enter(kernel_map
, &backing_offset
, backing_size
, 0,
9887 VM_FLAGS_ANYWHERE
, VM_MAP_KERNEL_FLAGS_NONE
,
9888 backing_object
, 0, FALSE
,
9889 VM_PROT_DEFAULT
, VM_PROT_DEFAULT
, VM_INHERIT_DEFAULT
);
9890 assert(kr
== KERN_SUCCESS
);
9891 backing_address
= (unsigned char *) backing_offset
;
9892 printf("VM_TEST_COLLAPSE_COMPRESSOR: "
9893 "mapped backing object %p at 0x%llx\n",
9894 backing_object
, (uint64_t) backing_offset
);
9895 /* populate with pages to be compressed in backing object */
9896 backing_address
[0x1*PAGE_SIZE
] = 0xB1;
9897 backing_address
[0x4*PAGE_SIZE
] = 0xB4;
9898 backing_address
[0x7*PAGE_SIZE
] = 0xB7;
9899 backing_address
[0xa*PAGE_SIZE
] = 0xBA;
9900 backing_address
[0xd*PAGE_SIZE
] = 0xBD;
9901 printf("VM_TEST_COLLAPSE_COMPRESSOR: "
9902 "populated pages to be compressed in "
9903 "backing_object %p\n", backing_object
);
9904 /* compress backing object */
9905 vm_object_pageout(backing_object
);
9906 printf("VM_TEST_COLLAPSE_COMPRESSOR: compressing backing_object %p\n",
9908 /* wait for all the pages to be gone */
9909 while (*(volatile int *)&backing_object
->resident_page_count
!= 0)
9911 printf("VM_TEST_COLLAPSE_COMPRESSOR: backing_object %p compressed\n",
9913 /* populate with pages to be resident in backing object */
9914 backing_address
[0x0*PAGE_SIZE
] = 0xB0;
9915 backing_address
[0x3*PAGE_SIZE
] = 0xB3;
9916 backing_address
[0x6*PAGE_SIZE
] = 0xB6;
9917 backing_address
[0x9*PAGE_SIZE
] = 0xB9;
9918 backing_address
[0xc*PAGE_SIZE
] = 0xBC;
9919 printf("VM_TEST_COLLAPSE_COMPRESSOR: "
9920 "populated pages to be resident in "
9921 "backing_object %p\n", backing_object
);
9922 /* leave the other pages absent */
9923 /* mess with the paging_offset of the backing_object */
9924 assert(backing_object
->paging_offset
== 0);
9925 backing_object
->paging_offset
= 0x3000;
9927 /* create top object */
9928 top_size
= 9 * PAGE_SIZE
;
9929 top_object
= vm_object_allocate(top_size
);
9930 assert(top_object
!= VM_OBJECT_NULL
);
9931 printf("VM_TEST_COLLAPSE_COMPRESSOR: created top object %p\n",
9933 /* map top object */
9935 kr
= vm_map_enter(kernel_map
, &top_offset
, top_size
, 0,
9936 VM_FLAGS_ANYWHERE
, VM_MAP_KERNEL_FLAGS_NONE
,
9937 top_object
, 0, FALSE
,
9938 VM_PROT_DEFAULT
, VM_PROT_DEFAULT
, VM_INHERIT_DEFAULT
);
9939 assert(kr
== KERN_SUCCESS
);
9940 top_address
= (unsigned char *) top_offset
;
9941 printf("VM_TEST_COLLAPSE_COMPRESSOR: "
9942 "mapped top object %p at 0x%llx\n",
9943 top_object
, (uint64_t) top_offset
);
9944 /* populate with pages to be compressed in top object */
9945 top_address
[0x3*PAGE_SIZE
] = 0xA3;
9946 top_address
[0x4*PAGE_SIZE
] = 0xA4;
9947 top_address
[0x5*PAGE_SIZE
] = 0xA5;
9948 printf("VM_TEST_COLLAPSE_COMPRESSOR: "
9949 "populated pages to be compressed in "
9950 "top_object %p\n", top_object
);
9951 /* compress top object */
9952 vm_object_pageout(top_object
);
9953 printf("VM_TEST_COLLAPSE_COMPRESSOR: compressing top_object %p\n",
9955 /* wait for all the pages to be gone */
9956 while (top_object
->resident_page_count
!= 0)
9958 printf("VM_TEST_COLLAPSE_COMPRESSOR: top_object %p compressed\n",
9960 /* populate with pages to be resident in top object */
9961 top_address
[0x0*PAGE_SIZE
] = 0xA0;
9962 top_address
[0x1*PAGE_SIZE
] = 0xA1;
9963 top_address
[0x2*PAGE_SIZE
] = 0xA2;
9964 printf("VM_TEST_COLLAPSE_COMPRESSOR: "
9965 "populated pages to be resident in "
9966 "top_object %p\n", top_object
);
9967 /* leave the other pages absent */
9969 /* link the 2 objects */
9970 vm_object_reference(backing_object
);
9971 top_object
->shadow
= backing_object
;
9972 top_object
->vo_shadow_offset
= 0x3000;
9973 printf("VM_TEST_COLLAPSE_COMPRESSOR: linked %p and %p\n",
9974 top_object
, backing_object
);
9976 /* unmap backing object */
9977 vm_map_remove(kernel_map
,
9979 backing_offset
+ backing_size
,
9980 VM_MAP_REMOVE_NO_FLAGS
);
9981 printf("VM_TEST_COLLAPSE_COMPRESSOR: "
9982 "unmapped backing_object %p [0x%llx:0x%llx]\n",
9984 (uint64_t) backing_offset
,
9985 (uint64_t) (backing_offset
+ backing_size
));
9988 printf("VM_TEST_COLLAPSE_COMPRESSOR: collapsing %p\n", top_object
);
9989 vm_object_lock(top_object
);
9990 vm_object_collapse(top_object
, 0, FALSE
);
9991 vm_object_unlock(top_object
);
9992 printf("VM_TEST_COLLAPSE_COMPRESSOR: collapsed %p\n", top_object
);
9995 if (top_object
->shadow
!= VM_OBJECT_NULL
) {
9996 printf("VM_TEST_COLLAPSE_COMPRESSOR: not collapsed\n");
9997 printf("VM_TEST_COLLAPSE_COMPRESSOR: FAIL\n");
9998 if (vm_object_collapse_compressor_allowed
) {
9999 panic("VM_TEST_COLLAPSE_COMPRESSOR: FAIL\n");
10002 /* check the contents of the mapping */
10003 unsigned char expect
[9] =
10004 { 0xA0, 0xA1, 0xA2, /* resident in top */
10005 0xA3, 0xA4, 0xA5, /* compressed in top */
10006 0xB9, /* resident in backing + shadow_offset */
10007 0xBD, /* compressed in backing + shadow_offset + paging_offset */
10008 0x00 }; /* absent in both */
10009 unsigned char actual
[9];
10010 unsigned int i
, errors
;
10013 for (i
= 0; i
< sizeof (actual
); i
++) {
10014 actual
[i
] = (unsigned char) top_address
[i
*PAGE_SIZE
];
10015 if (actual
[i
] != expect
[i
]) {
10019 printf("VM_TEST_COLLAPSE_COMPRESSOR: "
10020 "actual [%x %x %x %x %x %x %x %x %x] "
10021 "expect [%x %x %x %x %x %x %x %x %x] "
10023 actual
[0], actual
[1], actual
[2], actual
[3],
10024 actual
[4], actual
[5], actual
[6], actual
[7],
10026 expect
[0], expect
[1], expect
[2], expect
[3],
10027 expect
[4], expect
[5], expect
[6], expect
[7],
10031 panic("VM_TEST_COLLAPSE_COMPRESSOR: FAIL\n");
10033 printf("VM_TEST_COLLAPSE_COMPRESSOR: PASS\n");
10037 #else /* VM_TEST_COLLAPSE_COMPRESSOR */
10038 #define vm_test_collapse_compressor()
10039 #endif /* VM_TEST_COLLAPSE_COMPRESSOR */
10041 #if VM_TEST_WIRE_AND_EXTRACT
10042 extern ledger_template_t task_ledger_template
;
10043 #include <mach/mach_vm.h>
10044 extern ppnum_t
vm_map_get_phys_page(vm_map_t map
,
10045 vm_offset_t offset
);
10047 vm_test_wire_and_extract(void)
10050 vm_map_t user_map
, wire_map
;
10051 mach_vm_address_t user_addr
, wire_addr
;
10052 mach_vm_size_t user_size
, wire_size
;
10053 mach_vm_offset_t cur_offset
;
10054 vm_prot_t cur_prot
, max_prot
;
10055 ppnum_t user_ppnum
, wire_ppnum
;
10058 ledger
= ledger_instantiate(task_ledger_template
,
10059 LEDGER_CREATE_ACTIVE_ENTRIES
);
10060 user_map
= vm_map_create(pmap_create(ledger
, 0, PMAP_CREATE_64BIT
),
10064 wire_map
= vm_map_create(NULL
,
10069 user_size
= 0x10000;
10070 kr
= mach_vm_allocate(user_map
,
10073 VM_FLAGS_ANYWHERE
);
10074 assert(kr
== KERN_SUCCESS
);
10076 wire_size
= user_size
;
10077 kr
= mach_vm_remap(wire_map
,
10088 assert(kr
== KERN_SUCCESS
);
10089 for (cur_offset
= 0;
10090 cur_offset
< wire_size
;
10091 cur_offset
+= PAGE_SIZE
) {
10092 kr
= vm_map_wire_and_extract(wire_map
,
10093 wire_addr
+ cur_offset
,
10094 VM_PROT_DEFAULT
| VM_PROT_MEMORY_TAG_MAKE(VM_KERN_MEMORY_OSFMK
),
10097 assert(kr
== KERN_SUCCESS
);
10098 user_ppnum
= vm_map_get_phys_page(user_map
,
10099 user_addr
+ cur_offset
);
10100 printf("VM_TEST_WIRE_AND_EXTRACT: kr=0x%x "
10101 "user[%p:0x%llx:0x%x] wire[%p:0x%llx:0x%x]\n",
10103 user_map
, user_addr
+ cur_offset
, user_ppnum
,
10104 wire_map
, wire_addr
+ cur_offset
, wire_ppnum
);
10105 if (kr
!= KERN_SUCCESS
||
10107 wire_ppnum
!= user_ppnum
) {
10108 panic("VM_TEST_WIRE_AND_EXTRACT: FAIL\n");
10111 cur_offset
-= PAGE_SIZE
;
10112 kr
= vm_map_wire_and_extract(wire_map
,
10113 wire_addr
+ cur_offset
,
10117 assert(kr
== KERN_SUCCESS
);
10118 printf("VM_TEST_WIRE_AND_EXTRACT: re-wire kr=0x%x "
10119 "user[%p:0x%llx:0x%x] wire[%p:0x%llx:0x%x]\n",
10121 user_map
, user_addr
+ cur_offset
, user_ppnum
,
10122 wire_map
, wire_addr
+ cur_offset
, wire_ppnum
);
10123 if (kr
!= KERN_SUCCESS
||
10125 wire_ppnum
!= user_ppnum
) {
10126 panic("VM_TEST_WIRE_AND_EXTRACT: FAIL\n");
10129 printf("VM_TEST_WIRE_AND_EXTRACT: PASS\n");
10131 #else /* VM_TEST_WIRE_AND_EXTRACT */
10132 #define vm_test_wire_and_extract()
10133 #endif /* VM_TEST_WIRE_AND_EXTRACT */
10135 #if VM_TEST_PAGE_WIRE_OVERFLOW_PANIC
10137 vm_test_page_wire_overflow_panic(void)
10139 vm_object_t object
;
10142 printf("VM_TEST_PAGE_WIRE_OVERFLOW_PANIC: starting...\n");
10144 object
= vm_object_allocate(PAGE_SIZE
);
10145 vm_object_lock(object
);
10146 page
= vm_page_alloc(object
, 0x0);
10147 vm_page_lock_queues();
10149 vm_page_wire(page
, 1, FALSE
);
10150 } while (page
->wire_count
!= 0);
10151 vm_page_unlock_queues();
10152 vm_object_unlock(object
);
10153 panic("FBDP(%p,%p): wire_count overflow not detected\n",
10156 #else /* VM_TEST_PAGE_WIRE_OVERFLOW_PANIC */
10157 #define vm_test_page_wire_overflow_panic()
10158 #endif /* VM_TEST_PAGE_WIRE_OVERFLOW_PANIC */
10160 #if __arm64__ && VM_TEST_KERNEL_OBJECT_FAULT
10161 extern int copyinframe(vm_address_t fp
, char *frame
, boolean_t is64bit
);
10163 vm_test_kernel_object_fault(void)
10167 uintptr_t frameb
[2];
10170 kr
= kernel_memory_allocate(kernel_map
, &stack
,
10171 kernel_stack_size
+ (2*PAGE_SIZE
),
10173 (KMA_KSTACK
| KMA_KOBJECT
|
10174 KMA_GUARD_FIRST
| KMA_GUARD_LAST
),
10175 VM_KERN_MEMORY_STACK
);
10176 if (kr
!= KERN_SUCCESS
) {
10177 panic("VM_TEST_KERNEL_OBJECT_FAULT: kernel_memory_allocate kr 0x%x\n", kr
);
10179 ret
= copyinframe((uintptr_t)stack
, (char *)frameb
, TRUE
);
10181 printf("VM_TEST_KERNEL_OBJECT_FAULT: PASS\n");
10183 printf("VM_TEST_KERNEL_OBJECT_FAULT: FAIL\n");
10185 vm_map_remove(kernel_map
,
10187 stack
+ kernel_stack_size
+ (2*PAGE_SIZE
),
10188 VM_MAP_REMOVE_KUNWIRE
);
10191 #else /* __arm64__ && VM_TEST_KERNEL_OBJECT_FAULT */
10192 #define vm_test_kernel_object_fault()
10193 #endif /* __arm64__ && VM_TEST_KERNEL_OBJECT_FAULT */
10195 #if VM_TEST_DEVICE_PAGER_TRANSPOSE
10197 vm_test_device_pager_transpose(void)
10199 memory_object_t device_pager
;
10200 vm_object_t anon_object
, device_object
;
10202 vm_map_offset_t anon_mapping
, device_mapping
;
10205 size
= 3 * PAGE_SIZE
;
10206 anon_object
= vm_object_allocate(size
);
10207 assert(anon_object
!= VM_OBJECT_NULL
);
10208 device_pager
= device_pager_setup(NULL
, 0, size
, 0);
10209 assert(device_pager
!= NULL
);
10210 device_object
= memory_object_to_vm_object(device_pager
);
10211 assert(device_object
!= VM_OBJECT_NULL
);
10213 kr
= vm_map_enter(kernel_map
, &anon_mapping
, size
, 0,
10214 VM_FLAGS_ANYWHERE
, VM_MAP_KERNEL_FLAGS_NONE
, VM_KERN_MEMORY_NONE
,
10215 anon_object
, 0, FALSE
, VM_PROT_DEFAULT
, VM_PROT_ALL
,
10216 VM_INHERIT_DEFAULT
);
10217 assert(kr
== KERN_SUCCESS
);
10218 device_mapping
= 0;
10219 kr
= vm_map_enter_mem_object(kernel_map
, &device_mapping
, size
, 0,
10221 VM_MAP_KERNEL_FLAGS_NONE
,
10222 VM_KERN_MEMORY_NONE
,
10223 (void *)device_pager
, 0, FALSE
,
10224 VM_PROT_DEFAULT
, VM_PROT_ALL
,
10225 VM_INHERIT_DEFAULT
);
10226 assert(kr
== KERN_SUCCESS
);
10227 memory_object_deallocate(device_pager
);
10229 vm_object_lock(anon_object
);
10230 vm_object_activity_begin(anon_object
);
10231 anon_object
->blocked_access
= TRUE
;
10232 vm_object_unlock(anon_object
);
10233 vm_object_lock(device_object
);
10234 vm_object_activity_begin(device_object
);
10235 device_object
->blocked_access
= TRUE
;
10236 vm_object_unlock(device_object
);
10238 assert(anon_object
->ref_count
== 1);
10239 assert(!anon_object
->named
);
10240 assert(device_object
->ref_count
== 2);
10241 assert(device_object
->named
);
10243 kr
= vm_object_transpose(device_object
, anon_object
, size
);
10244 assert(kr
== KERN_SUCCESS
);
10246 vm_object_lock(anon_object
);
10247 vm_object_activity_end(anon_object
);
10248 anon_object
->blocked_access
= FALSE
;
10249 vm_object_unlock(anon_object
);
10250 vm_object_lock(device_object
);
10251 vm_object_activity_end(device_object
);
10252 device_object
->blocked_access
= FALSE
;
10253 vm_object_unlock(device_object
);
10255 assert(anon_object
->ref_count
== 2);
10256 assert(anon_object
->named
);
10257 kr
= vm_deallocate(kernel_map
, anon_mapping
, size
);
10258 assert(kr
== KERN_SUCCESS
);
10259 assert(device_object
->ref_count
== 1);
10260 assert(!device_object
->named
);
10261 kr
= vm_deallocate(kernel_map
, device_mapping
, size
);
10262 assert(kr
== KERN_SUCCESS
);
10264 printf("VM_TEST_DEVICE_PAGER_TRANSPOSE: PASS\n");
10266 #else /* VM_TEST_DEVICE_PAGER_TRANSPOSE */
10267 #define vm_test_device_pager_transpose()
10268 #endif /* VM_TEST_DEVICE_PAGER_TRANSPOSE */
10273 vm_test_collapse_compressor();
10274 vm_test_wire_and_extract();
10275 vm_test_page_wire_overflow_panic();
10276 vm_test_kernel_object_fault();
10277 vm_test_device_pager_transpose();