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