xref: /xnu-11215.1.10/osfmk/vm/vm_reclaim_xnu.h (revision 8d741a5de7ff4191bf97d57b9f54c2f6d4a15585)
1 /*
2  * Copyright (c) 2023 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #ifndef __VM_RECLAIM_XNU__
30 #define __VM_RECLAIM_XNU__
31 
32 #ifdef XNU_KERNEL_PRIVATE
33 #if CONFIG_DEFERRED_RECLAIM
34 #include <mach/mach_types.h>
35 #if BSD_KERNEL_PRIVATE
36 #include <mach/mach_vm.h>
37 #else /* BSD_KERNEL_PRIVATE */
38 #include <mach/mach_vm_server.h>
39 #endif /* BSD_KERNEL_PRIVATE */
40 
41 extern uint64_t vm_reclaim_max_threshold;
42 typedef struct vm_deferred_reclamation_metadata_s *vm_deferred_reclamation_metadata_t;
43 
44 __options_closed_decl(vm_deferred_reclamation_options_t, uint8_t, {
45 	RECLAIM_OPTIONS_NONE = 0x00,
46 	/* Do not fault on the reclaim buffer if it is not resident */
47 	RECLAIM_NO_FAULT     = 0x01,
48 	/* Do not wait to acquire the buffer if it is owned by another thread */
49 	RECLAIM_NO_WAIT      = 0x02,
50 });
51 
52 /*
53  * Deallocate the kernel metadata associated with this reclamation buffer
54  * Note that this does NOT free the memory in the buffer.
55  * This is called from the task_destroy path, so we're about to reclaim all of the task's memory
56  * anyways.
57  */
58 void vm_deferred_reclamation_buffer_deallocate(vm_deferred_reclamation_metadata_t metadata);
59 
60 
61 /*
62  * Uninstall the the kernel metadata associated with this reclamation buffer from all global queues. This
63  * is called during task termination to ensure no kernel thread may start trying to reclaim from a task
64  * that is about to exit
65  */
66 void vm_deferred_reclamation_buffer_uninstall(vm_deferred_reclamation_metadata_t metadata);
67 
68 /*
69  * Equivalent to vm_deferred_reclamation_reclaim_memory(RECLAIM_FULL);
70  */
71 void vm_deferred_reclamation_reclaim_all_memory(
72 	vm_deferred_reclamation_options_t options);
73 
74 bool vm_deferred_reclamation_reclaim_from_task_async(task_t task);
75 
76 kern_return_t vm_deferred_reclamation_reclaim_from_task_sync(
77 	task_t task,
78 	size_t max_entries_to_reclaim);
79 
80 /*
81  * Create a fork of the given reclamation buffer for a new task.
82  * Parent buffer must be locked and will be unlocked on return.
83  *
84  * This must be called when forking a task that has a reclamation buffer
85  * to ensure that the kernel knows about the child's reclamation buffer.
86  * The caller must lock the parent's reclamation buffer BEFORE forking
87  * the parent's vm_map. Otherwise the parent's buffer could get reclaimed
88  * in between the map fork and the buffer fork causing the child's
89  * data strucutres to be out of sync.
90  */
91 vm_deferred_reclamation_metadata_t vm_deferred_reclamation_buffer_fork(
92 	task_t task,
93 	vm_deferred_reclamation_metadata_t parent);
94 
95 /*
96  * Set the current thread as the owner of a reclaim buffer. May block. Will
97  * propagate priority.
98  */
99 void vm_deferred_reclamation_buffer_own(vm_deferred_reclamation_metadata_t metadata);
100 
101 /*
102  * Release ownership of a reclaim buffer and wakeup any threads waiting for
103  * ownership. Must be called from the thread that acquired ownership.
104  */
105 void vm_deferred_reclamation_buffer_disown(vm_deferred_reclamation_metadata_t metadata);
106 
107 
108 #endif /* CONFIG_DEFERRED_RECLAIM */
109 #endif /* XNU_KERNEL_PRIVATE */
110 #endif  /* __VM_RECLAIM_XNU__ */
111