1/*
2 * Copyright (c) 2021 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_INTERNAL__
30#define __VM_RECLAIM_INTERNAL__
31
32#if CONFIG_DEFERRED_RECLAIM
33
34#ifdef XNU_KERNEL_PRIVATE
35
36#include <mach/mach_types.h>
37#include <mach/mach_vm.h>
38
39extern uint64_t vm_reclaim_max_threshold;
40
41typedef struct vm_deferred_reclamation_metadata_s *vm_deferred_reclamation_metadata_t;
42
43kern_return_t vm_deferred_reclamation_buffer_init_internal(task_t task, user_addr_t address, mach_vm_size_t size);
44
45/*
46 * Deallocate the kernel metadata associated with this reclamation buffer
47 * Note that this does NOT free the memory in the buffer.
48 * This is called from the task_destroy path, so we're about to reclaim all of the task's memory
49 * anyways.
50 */
51void vm_deferred_reclamation_buffer_deallocate(vm_deferred_reclamation_metadata_t metadata);
52
53/*
54 * Uninstall the the kernel metadata associated with this reclamation buffer from all global queues. This
55 * is called during task termination to ensure no kernel thread may start trying to reclaim from a task
56 * that is about to exit
57 */
58void vm_deferred_reclamation_buffer_uninstall(vm_deferred_reclamation_metadata_t metadata);
59
60kern_return_t vm_deferred_reclamation_buffer_synchronize_internal(task_t task, size_t max_entries_to_reclaim);
61
62__enum_decl(vm_deferred_reclamation_action_t, uint32_t, {
63 RECLAIM_TRIM, // Reclaim a bit of memory from everyone
64 RECLAIM_FULL, // Fully drain every reclaim buffer
65 RECLAIM_ASYNC, // Drain the async reclaim queue
66});
67
68/*
69 * Ask the deferred reclamation subsystem to reclaim memory.
70 * See the documentation for vm_deferred_reclamation_action above.
71 */
72void vm_deferred_reclamation_reclaim_memory(vm_deferred_reclamation_action_t action);
73/*
74 * Equivalent to vm_deferred_reclamation_reclaim_memory(RECLAIM_FULL);
75 */
76void vm_deferred_reclamation_reclaim_all_memory(void);
77
78bool vm_deferred_reclamation_reclaim_from_task_async(task_t task);
79bool vm_deferred_reclamation_reclaim_from_task_sync(task_t task, size_t max_entries_to_reclaim);
80
81kern_return_t vm_deferred_reclamation_buffer_update_reclaimable_bytes_internal(
82 task_t task, size_t reclaimable_bytes);
83
84/*
85 * Create a fork of the given reclamation buffer for a new task.
86 * Parent buffer must be locked and will be unlocked on return.
87 *
88 * This must be called when forking a task that has a reclamation buffer
89 * to ensure that the kernel knows about the child's reclamation buffer.
90 * The caller must lock the parent's reclamation buffer BEFORE forking
91 * the parent's vm_map. Otherwise the parent's buffer could get reclaimed
92 * in between the map fork and the buffer fork causing the child's
93 * data strucutres to be out of sync.
94 */
95vm_deferred_reclamation_metadata_t vm_deferred_reclamation_buffer_fork(
96 task_t task,
97 vm_deferred_reclamation_metadata_t parent);
98
99void vm_deferred_reclamation_buffer_lock(vm_deferred_reclamation_metadata_t metadata);
100void vm_deferred_reclamation_buffer_unlock(vm_deferred_reclamation_metadata_t metadata);
101
102#if DEVELOPMENT || DEBUG
103/*
104 * Testing helpers
105 */
106bool vm_deferred_reclamation_block_until_pid_has_been_reclaimed(int pid);
107#endif /* DEVELOPMENT || DEBUG */
108
109#endif /* XNU_KERNEL_PRIVATE */
110#endif /* CONFIG_DEFERRED_RECLAIM */
111#endif /*__VM_RECLAIM_INTERNAL__ */
112