]>
Commit | Line | Data |
---|---|---|
39236c6e A |
1 | /* |
2 | * Copyright (c) 2000-2013 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 | #include "vm_compressor_backing_store.h" | |
30 | #include <vm/vm_protos.h> | |
31 | ||
32 | #include <IOKit/IOHibernatePrivate.h> | |
33 | ||
34 | ||
35 | boolean_t compressor_store_stop_compaction = FALSE; | |
36 | boolean_t vm_swap_up = FALSE; | |
37 | boolean_t vm_swapfile_mgmt_needed = FALSE; | |
38 | ||
39 | int swapper_throttle = -1; | |
40 | boolean_t swapper_throttle_inited = FALSE; | |
41 | uint64_t vm_swapout_thread_id; | |
42 | ||
43 | uint64_t vm_swap_put_failures = 0; | |
44 | uint64_t vm_swap_get_failures = 0; | |
45 | int vm_num_swap_files = 0; | |
46 | int vm_swapout_thread_processed_segments = 0; | |
47 | int vm_swapout_thread_awakened = 0; | |
48 | int vm_swapfile_mgmt_thread_awakened = 0; | |
49 | int vm_swapfile_mgmt_thread_running = 0; | |
50 | ||
51 | unsigned int vm_swapfile_total_segs_alloced = 0; | |
52 | unsigned int vm_swapfile_total_segs_used = 0; | |
53 | ||
54 | ||
55 | #define SWAP_READY 0x1 /* Swap file is ready to be used */ | |
56 | #define SWAP_RECLAIM 0x2 /* Swap file is marked to be reclaimed */ | |
57 | #define SWAP_WANTED 0x4 /* Swap file has waiters */ | |
58 | #define SWAP_REUSE 0x8 /* Swap file is on the Q and has a name. Reuse after init-ing.*/ | |
59 | ||
60 | struct swapfile{ | |
61 | queue_head_t swp_queue; /* list of swap files */ | |
62 | char *swp_path; /* saved pathname of swap file */ | |
63 | struct vnode *swp_vp; /* backing vnode */ | |
64 | uint64_t swp_size; /* size of this swap file */ | |
65 | uint8_t *swp_bitmap; /* bitmap showing the alloced/freed slots in the swap file */ | |
66 | unsigned int swp_pathlen; /* length of pathname */ | |
67 | unsigned int swp_nsegs; /* #segments we can use */ | |
68 | unsigned int swp_nseginuse; /* #segments in use */ | |
69 | unsigned int swp_index; /* index of this swap file */ | |
70 | unsigned int swp_flags; /* state of swap file */ | |
71 | unsigned int swp_free_hint; /* offset of 1st free chunk */ | |
72 | unsigned int swp_io_count; /* count of outstanding I/Os */ | |
73 | c_segment_t *swp_csegs; /* back pointers to the c_segments. Used during swap reclaim. */ | |
74 | ||
75 | struct trim_list *swp_delayed_trim_list_head; | |
76 | unsigned int swp_delayed_trim_count; | |
77 | boolean_t swp_trim_supported; | |
78 | }; | |
79 | ||
80 | queue_head_t swf_global_queue; | |
81 | ||
82 | #define VM_SWAPFILE_DELAYED_TRIM_MAX 128 | |
83 | ||
84 | extern clock_sec_t dont_trim_until_ts; | |
85 | clock_sec_t vm_swapfile_last_failed_to_create_ts = 0; | |
86 | ||
87 | static void vm_swapout_thread_throttle_adjust(void); | |
88 | static void vm_swap_free_now(struct swapfile *swf, uint64_t f_offset); | |
89 | static void vm_swapout_thread(void); | |
90 | static void vm_swapfile_mgmt_thread(void); | |
91 | static void vm_swap_defragment(); | |
92 | static void vm_swap_handle_delayed_trims(boolean_t); | |
93 | static void vm_swap_do_delayed_trim(); | |
94 | ||
95 | ||
96 | #define VM_SWAPFILE_DELAYED_CREATE 30 | |
97 | #define VM_SWAP_SHOULD_DEFRAGMENT() (c_swappedout_sparse_count > (vm_swapfile_total_segs_used / 4) ? 1 : 0) | |
98 | #define VM_SWAP_SHOULD_RECLAIM() (((vm_swapfile_total_segs_alloced - vm_swapfile_total_segs_used) >= SWAPFILE_RECLAIM_THRESHOLD_SEGS) ? 1 : 0) | |
99 | #define VM_SWAP_SHOULD_CREATE(cur_ts) (((vm_swapfile_total_segs_alloced - vm_swapfile_total_segs_used) < (unsigned int)VM_SWAPFILE_HIWATER_SEGS) && \ | |
100 | ((cur_ts - vm_swapfile_last_failed_to_create_ts) > VM_SWAPFILE_DELAYED_CREATE) ? 1 : 0) | |
101 | #define VM_SWAP_SHOULD_TRIM(swf) ((swf->swp_delayed_trim_count >= VM_SWAPFILE_DELAYED_TRIM_MAX) ? 1 : 0) | |
102 | ||
103 | ||
104 | #define VM_SWAP_BUSY() ((c_swapout_count && (swapper_throttle == THROTTLE_LEVEL_COMPRESSOR_TIER1 || swapper_throttle == THROTTLE_LEVEL_COMPRESSOR_TIER0)) ? 1 : 0) | |
105 | ||
106 | ||
107 | #if CHECKSUM_THE_SWAP | |
108 | extern unsigned int hash_string(char *cp, int len); | |
109 | #endif | |
110 | ||
111 | #if CRYPTO | |
112 | extern boolean_t swap_crypt_ctx_initialized; | |
113 | extern void swap_crypt_ctx_initialize(void); | |
114 | extern const unsigned char swap_crypt_null_iv[AES_BLOCK_SIZE]; | |
115 | extern aes_ctx swap_crypt_ctx; | |
116 | extern unsigned long vm_page_encrypt_counter; | |
117 | extern unsigned long vm_page_decrypt_counter; | |
118 | #endif /* CRYPTO */ | |
119 | ||
120 | extern void vm_pageout_io_throttle(void); | |
121 | ||
122 | struct swapfile *vm_swapfile_for_handle(uint64_t); | |
123 | ||
124 | /* | |
125 | * Called with the vm_swap_data_lock held. | |
126 | */ | |
127 | ||
128 | struct swapfile * | |
129 | vm_swapfile_for_handle(uint64_t f_offset) | |
130 | { | |
131 | ||
132 | uint64_t file_offset = 0; | |
133 | unsigned int swapfile_index = 0; | |
134 | struct swapfile* swf = NULL; | |
135 | ||
136 | file_offset = (f_offset & SWAP_SLOT_MASK); | |
137 | swapfile_index = (f_offset >> SWAP_DEVICE_SHIFT); | |
138 | ||
139 | swf = (struct swapfile*) queue_first(&swf_global_queue); | |
140 | ||
141 | while(queue_end(&swf_global_queue, (queue_entry_t)swf) == FALSE) { | |
142 | ||
143 | if (swapfile_index == swf->swp_index) { | |
144 | break; | |
145 | } | |
146 | ||
147 | swf = (struct swapfile*) queue_next(&swf->swp_queue); | |
148 | } | |
149 | ||
150 | if (queue_end(&swf_global_queue, (queue_entry_t) swf)) { | |
151 | swf = NULL; | |
152 | } | |
153 | ||
154 | return swf; | |
155 | } | |
156 | ||
157 | void | |
158 | vm_swap_init() | |
159 | { | |
160 | static boolean_t vm_swap_try_init = FALSE; | |
161 | thread_t thread = NULL; | |
162 | ||
163 | if (vm_swap_try_init == TRUE) { | |
164 | return; | |
165 | } | |
166 | ||
167 | vm_swap_try_init = TRUE; | |
168 | ||
169 | lck_grp_attr_setdefault(&vm_swap_data_lock_grp_attr); | |
170 | lck_grp_init(&vm_swap_data_lock_grp, | |
171 | "vm_swap_data", | |
172 | &vm_swap_data_lock_grp_attr); | |
173 | lck_attr_setdefault(&vm_swap_data_lock_attr); | |
174 | lck_mtx_init_ext(&vm_swap_data_lock, | |
175 | &vm_swap_data_lock_ext, | |
176 | &vm_swap_data_lock_grp, | |
177 | &vm_swap_data_lock_attr); | |
178 | ||
179 | queue_init(&swf_global_queue); | |
180 | ||
181 | if (vm_swap_create_file()) { | |
182 | ||
183 | if (kernel_thread_start_priority((thread_continue_t)vm_swapout_thread, NULL, | |
184 | BASEPRI_PREEMPT - 1, &thread) != KERN_SUCCESS) { | |
185 | panic("vm_swapout_thread: create failed"); | |
186 | } | |
187 | thread->options |= TH_OPT_VMPRIV; | |
188 | vm_swapout_thread_id = thread->thread_id; | |
189 | ||
190 | thread_deallocate(thread); | |
191 | ||
192 | if (kernel_thread_start_priority((thread_continue_t)vm_swapfile_mgmt_thread, NULL, | |
193 | BASEPRI_PREEMPT - 1, &thread) != KERN_SUCCESS) { | |
194 | panic("vm_swapfile_mgmt_thread: create failed"); | |
195 | } | |
196 | thread->options |= TH_OPT_VMPRIV; | |
197 | ||
198 | thread_deallocate(thread); | |
199 | ||
200 | #if CRYPTO | |
201 | if (swap_crypt_ctx_initialized == FALSE) { | |
202 | swap_crypt_ctx_initialize(); | |
203 | } | |
204 | #endif /* CRYPTO */ | |
205 | ||
206 | vm_swap_up = TRUE; | |
207 | ||
208 | #if SANITY_CHECK_SWAP_ROUTINES | |
209 | extern lck_attr_t *vm_compressor_lck_attr; | |
210 | extern lck_grp_t *vm_compressor_lck_grp; | |
211 | ||
212 | /* | |
213 | * Changes COMPRESSED_SWAP_CHUNK_SIZE to make it (4*KB). | |
214 | * Changes MIN_SWAP_FILE_SIZE to (4*KB). | |
215 | * Changes MAX_SWAP_FILE_SIZE to (4*KB). | |
216 | * That will then cause the below allocations to create | |
217 | * 4 new swap files and put/get/free from them. | |
218 | */ | |
219 | { | |
220 | c_segment_t c_seg = NULL, c_seg1 = NULL, c_seg2 = NULL, c_seg3 = NULL; | |
221 | vm_offset_t addr = 0; | |
222 | vm_offset_t dup_addr = 0; | |
223 | kern_return_t kr = KERN_SUCCESS; | |
224 | uint64_t f_offset = 0; | |
225 | uint64_t f_offset1 = 0; | |
226 | uint64_t f_offset2 = 0; | |
227 | uint64_t f_offset3 = 0; | |
228 | ||
229 | if ((kr = kernel_memory_allocate(kernel_map, | |
230 | &addr, | |
231 | 4 * COMPRESSED_SWAP_CHUNK_SIZE, | |
232 | 0, | |
233 | KMA_KOBJECT))) { | |
234 | printf("kernel_memory_allocate failed with %d\n", kr); | |
235 | goto done; | |
236 | } | |
237 | ||
238 | if ((kr = kernel_memory_allocate(kernel_map, | |
239 | &dup_addr, | |
240 | 4 * COMPRESSED_SWAP_CHUNK_SIZE, | |
241 | 0, | |
242 | KMA_KOBJECT))) { | |
243 | printf("kernel_memory_allocate failed with %d\n", kr); | |
244 | goto done; | |
245 | } | |
246 | ||
247 | c_seg = (c_segment_t) kalloc(sizeof(*c_seg)); | |
248 | memset(c_seg, 0, sizeof(*c_seg)); | |
249 | #if __i386__ || __x86_64__ | |
250 | lck_mtx_init(&c_seg->c_lock, vm_compressor_lck_grp, vm_compressor_lck_attr); | |
251 | #else /* __i386__ || __x86_64__ */ | |
252 | lck_spin_init(&c_seg->c_lock, vm_compressor_lck_grp, vm_compressor_lck_attr); | |
253 | #endif /* __i386__ || __x86_64__ */ | |
254 | ||
255 | ||
256 | c_seg1 = (c_segment_t) kalloc(sizeof(*c_seg)); | |
257 | memset(c_seg1, 0, sizeof(*c_seg)); | |
258 | #if __i386__ || __x86_64__ | |
259 | lck_mtx_init(&c_seg1->c_lock, vm_compressor_lck_grp, vm_compressor_lck_attr); | |
260 | #else /* __i386__ || __x86_64__ */ | |
261 | lck_spin_init(&c_seg1->c_lock, vm_compressor_lck_grp, vm_compressor_lck_attr); | |
262 | #endif /* __i386__ || __x86_64__ */ | |
263 | ||
264 | ||
265 | c_seg2 = (c_segment_t) kalloc(sizeof(*c_seg)); | |
266 | memset(c_seg2, 0, sizeof(*c_seg)); | |
267 | #if __i386__ || __x86_64__ | |
268 | lck_mtx_init(&c_seg2->c_lock, vm_compressor_lck_grp, vm_compressor_lck_attr); | |
269 | #else /* __i386__ || __x86_64__ */ | |
270 | lck_spin_init(&c_seg2->c_lock, vm_compressor_lck_grp, vm_compressor_lck_attr); | |
271 | #endif /* __i386__ || __x86_64__ */ | |
272 | ||
273 | ||
274 | c_seg3 = (c_segment_t) kalloc(sizeof(*c_seg)); | |
275 | memset(c_seg3, 0, sizeof(*c_seg)); | |
276 | #if __i386__ || __x86_64__ | |
277 | lck_mtx_init(&c_seg3->c_lock, vm_compressor_lck_grp, vm_compressor_lck_attr); | |
278 | #else /* __i386__ || __x86_64__ */ | |
279 | lck_spin_init(&c_seg3->c_lock, vm_compressor_lck_grp, vm_compressor_lck_attr); | |
280 | #endif /* __i386__ || __x86_64__ */ | |
281 | ||
282 | ||
283 | memset((void*)addr, (int) 'a', PAGE_SIZE_64); | |
284 | memset((void*)(addr + PAGE_SIZE_64), (int) 'b', PAGE_SIZE_64); | |
285 | memset((void*)(addr + (2 * PAGE_SIZE_64)), (int) 'c', PAGE_SIZE_64); | |
286 | memset((void*)(addr + (3 * PAGE_SIZE_64)), (int) 'd', PAGE_SIZE_64); | |
287 | ||
288 | vm_swap_put(addr, &f_offset, PAGE_SIZE_64, c_seg); | |
289 | c_seg->c_store.c_swap_handle = f_offset; | |
290 | ||
291 | vm_swap_put(addr + PAGE_SIZE_64, &f_offset1, PAGE_SIZE_64, c_seg1); | |
292 | c_seg1->c_store.c_swap_handle = f_offset1; | |
293 | ||
294 | vm_swap_put(addr + (2 * PAGE_SIZE_64), &f_offset2, PAGE_SIZE_64, c_seg2); | |
295 | c_seg2->c_store.c_swap_handle = f_offset2; | |
296 | ||
297 | vm_swap_put(addr + (3 * PAGE_SIZE_64), &f_offset3, PAGE_SIZE_64, c_seg3); | |
298 | c_seg3->c_store.c_swap_handle = f_offset3; | |
299 | ||
300 | //vm_swap_free(f_offset); | |
301 | vm_swap_get(dup_addr, f_offset, PAGE_SIZE_64); | |
302 | ||
303 | //vm_swap_free(f_offset1); | |
304 | vm_swap_reclaim(); | |
305 | vm_swap_get(dup_addr + PAGE_SIZE_64, c_seg1->c_store.c_swap_handle, PAGE_SIZE_64); | |
306 | ||
307 | //vm_swap_free(f_offset2); | |
308 | vm_swap_reclaim(); | |
309 | vm_swap_get(dup_addr + (2 * PAGE_SIZE_64), c_seg2->c_store.c_swap_handle, PAGE_SIZE_64); | |
310 | ||
311 | //vm_swap_free(f_offset3); | |
312 | vm_swap_reclaim(); | |
313 | vm_swap_get(dup_addr + (3 * PAGE_SIZE_64), c_seg3->c_store.c_swap_handle, PAGE_SIZE_64); | |
314 | ||
315 | if (memcmp((void*)addr, (void*)dup_addr, PAGE_SIZE_64)) { | |
316 | panic("First page data mismatch\n"); | |
317 | kr = KERN_FAILURE; | |
318 | goto done; | |
319 | } | |
320 | ||
321 | if (memcmp((void*)(addr + PAGE_SIZE_64), (void*)(dup_addr + PAGE_SIZE_64), PAGE_SIZE_64)) { | |
322 | panic("Second page data mismatch 0x%lx, 0x%lxn", addr, dup_addr); | |
323 | kr = KERN_FAILURE; | |
324 | goto done; | |
325 | } | |
326 | ||
327 | if (memcmp((void*)(addr + (2 * PAGE_SIZE_64)), (void*)(dup_addr + (2 * PAGE_SIZE_64)), PAGE_SIZE_64)) { | |
328 | panic("Third page data mismatch\n"); | |
329 | kr = KERN_FAILURE; | |
330 | goto done; | |
331 | } | |
332 | ||
333 | if (memcmp((void*)(addr + (3 * PAGE_SIZE_64)), (void*)(dup_addr + (3 * PAGE_SIZE_64)), PAGE_SIZE_64)) { | |
334 | panic("Fourth page data mismatch 0x%lx, 0x%lxn", addr, dup_addr); | |
335 | kr = KERN_FAILURE; | |
336 | goto done; | |
337 | } | |
338 | ||
339 | done: | |
340 | printf("Sanity check %s\n", ((kr != KERN_SUCCESS) ? "FAILED" : "SUCCEEDED")); | |
341 | kfree((void*)addr, 4 * COMPRESSED_SWAP_CHUNK_SIZE); | |
342 | addr = 0; | |
343 | kfree((void*)dup_addr, 4 * COMPRESSED_SWAP_CHUNK_SIZE); | |
344 | dup_addr = 0; | |
345 | } | |
346 | #endif /* SANITY_CHECK_SWAP_ROUTINES */ | |
347 | } | |
348 | ||
349 | printf("VM Swap Subsystem is %s\n", (vm_swap_up == TRUE) ? "ON" : "OFF"); | |
350 | } | |
351 | ||
352 | #if CRYPTO | |
353 | void | |
354 | vm_swap_encrypt(c_segment_t c_seg) | |
355 | { | |
356 | vm_offset_t kernel_vaddr = 0; | |
357 | uint64_t size = 0; | |
358 | ||
359 | union { | |
360 | unsigned char aes_iv[AES_BLOCK_SIZE]; | |
361 | void *c_seg; | |
362 | } encrypt_iv; | |
363 | ||
364 | assert(swap_crypt_ctx_initialized); | |
365 | ||
366 | bzero(&encrypt_iv.aes_iv[0], sizeof (encrypt_iv.aes_iv)); | |
367 | ||
368 | encrypt_iv.c_seg = (void*)c_seg; | |
369 | ||
370 | /* encrypt the "initial vector" */ | |
371 | aes_encrypt_cbc((const unsigned char *) &encrypt_iv.aes_iv[0], | |
372 | swap_crypt_null_iv, | |
373 | 1, | |
374 | &encrypt_iv.aes_iv[0], | |
375 | &swap_crypt_ctx.encrypt); | |
376 | ||
377 | kernel_vaddr = (vm_offset_t) c_seg->c_store.c_buffer; | |
378 | size = round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset)); | |
379 | ||
380 | /* | |
381 | * Encrypt the c_segment. | |
382 | */ | |
383 | aes_encrypt_cbc((const unsigned char *) kernel_vaddr, | |
384 | &encrypt_iv.aes_iv[0], | |
385 | (unsigned int)(size / AES_BLOCK_SIZE), | |
386 | (unsigned char *) kernel_vaddr, | |
387 | &swap_crypt_ctx.encrypt); | |
388 | ||
389 | vm_page_encrypt_counter += (size/PAGE_SIZE_64); | |
390 | } | |
391 | ||
392 | void | |
393 | vm_swap_decrypt(c_segment_t c_seg) | |
394 | { | |
395 | ||
396 | vm_offset_t kernel_vaddr = 0; | |
397 | uint64_t size = 0; | |
398 | ||
399 | union { | |
400 | unsigned char aes_iv[AES_BLOCK_SIZE]; | |
401 | void *c_seg; | |
402 | } decrypt_iv; | |
403 | ||
404 | ||
405 | assert(swap_crypt_ctx_initialized); | |
406 | ||
407 | /* | |
408 | * Prepare an "initial vector" for the decryption. | |
409 | * It has to be the same as the "initial vector" we | |
410 | * used to encrypt that page. | |
411 | */ | |
412 | bzero(&decrypt_iv.aes_iv[0], sizeof (decrypt_iv.aes_iv)); | |
413 | ||
414 | decrypt_iv.c_seg = (void*)c_seg; | |
415 | ||
416 | /* encrypt the "initial vector" */ | |
417 | aes_encrypt_cbc((const unsigned char *) &decrypt_iv.aes_iv[0], | |
418 | swap_crypt_null_iv, | |
419 | 1, | |
420 | &decrypt_iv.aes_iv[0], | |
421 | &swap_crypt_ctx.encrypt); | |
422 | ||
423 | kernel_vaddr = (vm_offset_t) c_seg->c_store.c_buffer; | |
424 | size = round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset)); | |
425 | ||
426 | /* | |
427 | * Decrypt the c_segment. | |
428 | */ | |
429 | aes_decrypt_cbc((const unsigned char *) kernel_vaddr, | |
430 | &decrypt_iv.aes_iv[0], | |
431 | (unsigned int) (size / AES_BLOCK_SIZE), | |
432 | (unsigned char *) kernel_vaddr, | |
433 | &swap_crypt_ctx.decrypt); | |
434 | ||
435 | vm_page_decrypt_counter += (size/PAGE_SIZE_64); | |
436 | } | |
437 | #endif /* CRYPTO */ | |
438 | ||
439 | ||
440 | void | |
441 | vm_swap_consider_defragmenting() | |
442 | { | |
443 | if (compressor_store_stop_compaction == FALSE && !VM_SWAP_BUSY() && (VM_SWAP_SHOULD_DEFRAGMENT() || VM_SWAP_SHOULD_RECLAIM())) { | |
444 | ||
445 | if (!vm_swapfile_mgmt_thread_running) { | |
446 | lck_mtx_lock(&vm_swap_data_lock); | |
447 | ||
448 | if (!vm_swapfile_mgmt_thread_running) | |
449 | thread_wakeup((event_t) &vm_swapfile_mgmt_needed); | |
450 | ||
451 | lck_mtx_unlock(&vm_swap_data_lock); | |
452 | } | |
453 | } | |
454 | } | |
455 | ||
456 | ||
457 | int vm_swap_defragment_yielded = 0; | |
458 | int vm_swap_defragment_swapin = 0; | |
459 | int vm_swap_defragment_free = 0; | |
460 | int vm_swap_defragment_busy = 0; | |
461 | ||
462 | ||
463 | static void | |
464 | vm_swap_defragment() | |
465 | { | |
466 | c_segment_t c_seg; | |
467 | ||
468 | /* | |
469 | * have to grab the master lock w/o holding | |
470 | * any locks in spin mode | |
471 | */ | |
472 | PAGE_REPLACEMENT_DISALLOWED(TRUE); | |
473 | ||
474 | lck_mtx_lock_spin_always(c_list_lock); | |
475 | ||
476 | while (!queue_empty(&c_swappedout_sparse_list_head)) { | |
477 | ||
478 | if (compressor_store_stop_compaction == TRUE || VM_SWAP_BUSY()) { | |
479 | vm_swap_defragment_yielded++; | |
480 | break; | |
481 | } | |
482 | c_seg = (c_segment_t)queue_first(&c_swappedout_sparse_list_head); | |
483 | ||
484 | lck_mtx_lock_spin_always(&c_seg->c_lock); | |
485 | ||
486 | assert(c_seg->c_on_swappedout_sparse_q); | |
487 | ||
488 | if (c_seg->c_busy) { | |
489 | lck_mtx_unlock_always(c_list_lock); | |
490 | ||
491 | PAGE_REPLACEMENT_DISALLOWED(FALSE); | |
492 | /* | |
493 | * c_seg_wait_on_busy consumes c_seg->c_lock | |
494 | */ | |
495 | c_seg_wait_on_busy(c_seg); | |
496 | ||
497 | PAGE_REPLACEMENT_DISALLOWED(TRUE); | |
498 | ||
499 | lck_mtx_lock_spin_always(c_list_lock); | |
500 | ||
501 | vm_swap_defragment_busy++; | |
502 | continue; | |
503 | } | |
504 | if (c_seg->c_bytes_used == 0) { | |
505 | /* | |
506 | * c_seg_free_locked consumes the c_list_lock | |
507 | * and c_seg->c_lock | |
508 | */ | |
509 | c_seg_free_locked(c_seg); | |
510 | ||
511 | vm_swap_defragment_free++; | |
512 | } else { | |
513 | lck_mtx_unlock_always(c_list_lock); | |
514 | ||
515 | c_seg_swapin(c_seg, TRUE); | |
516 | lck_mtx_unlock_always(&c_seg->c_lock); | |
517 | ||
518 | vm_swap_defragment_swapin++; | |
519 | } | |
520 | PAGE_REPLACEMENT_DISALLOWED(FALSE); | |
521 | ||
522 | vm_pageout_io_throttle(); | |
523 | ||
524 | /* | |
525 | * because write waiters have privilege over readers, | |
526 | * dropping and immediately retaking the master lock will | |
527 | * still allow any thread waiting to acquire the | |
528 | * master lock exclusively an opportunity to take it | |
529 | */ | |
530 | PAGE_REPLACEMENT_DISALLOWED(TRUE); | |
531 | ||
532 | lck_mtx_lock_spin_always(c_list_lock); | |
533 | } | |
534 | lck_mtx_unlock_always(c_list_lock); | |
535 | ||
536 | PAGE_REPLACEMENT_DISALLOWED(FALSE); | |
537 | } | |
538 | ||
539 | ||
540 | ||
541 | static void | |
542 | vm_swapfile_mgmt_thread(void) | |
543 | { | |
544 | ||
545 | boolean_t did_work = FALSE; | |
546 | clock_sec_t sec; | |
547 | clock_nsec_t nsec; | |
548 | ||
549 | vm_swapfile_mgmt_thread_awakened++; | |
550 | vm_swapfile_mgmt_thread_running = 1; | |
551 | ||
552 | try_again: | |
553 | ||
554 | do { | |
555 | if (vm_swap_up == FALSE) | |
556 | break; | |
557 | did_work = FALSE; | |
558 | clock_get_system_nanotime(&sec, &nsec); | |
559 | ||
560 | /* | |
561 | * walk through the list of swap files | |
562 | * and do the delayed frees/trims for | |
563 | * any swap file whose count of delayed | |
564 | * frees is above the batch limit | |
565 | */ | |
566 | vm_swap_handle_delayed_trims(FALSE); | |
567 | ||
568 | if (VM_SWAP_SHOULD_CREATE(sec)) { | |
569 | if (vm_swap_create_file() == TRUE) | |
570 | did_work = TRUE; | |
571 | else { | |
572 | vm_swapfile_last_failed_to_create_ts = sec; | |
573 | HIBLOG("vm_swap_create_file failed @ %lu secs\n", sec); | |
574 | } | |
575 | } | |
576 | if (VM_SWAP_SHOULD_DEFRAGMENT()) { | |
577 | proc_set_task_policy_thread(kernel_task, current_thread()->thread_id, | |
578 | TASK_POLICY_INTERNAL, TASK_POLICY_IO, THROTTLE_LEVEL_COMPRESSOR_TIER2); | |
579 | ||
580 | vm_swap_defragment(); | |
581 | ||
582 | if (!VM_SWAP_BUSY()) | |
583 | did_work = TRUE; | |
584 | ||
585 | proc_set_task_policy_thread(kernel_task, current_thread()->thread_id, | |
586 | TASK_POLICY_INTERNAL, TASK_POLICY_IO, THROTTLE_LEVEL_COMPRESSOR_TIER1); | |
587 | } | |
588 | if (VM_SWAP_SHOULD_RECLAIM()) { | |
589 | proc_set_task_policy_thread(kernel_task, current_thread()->thread_id, | |
590 | TASK_POLICY_INTERNAL, TASK_POLICY_IO, THROTTLE_LEVEL_COMPRESSOR_TIER2); | |
591 | ||
592 | vm_swap_defragment(); | |
593 | vm_swap_reclaim(); | |
594 | ||
595 | if (!VM_SWAP_BUSY()) | |
596 | did_work = TRUE; | |
597 | ||
598 | proc_set_task_policy_thread(kernel_task, current_thread()->thread_id, | |
599 | TASK_POLICY_INTERNAL, TASK_POLICY_IO, THROTTLE_LEVEL_COMPRESSOR_TIER1); | |
600 | } | |
601 | ||
602 | } while (did_work == TRUE); | |
603 | ||
604 | lck_mtx_lock(&vm_swap_data_lock); | |
605 | ||
606 | clock_get_system_nanotime(&sec, &nsec); | |
607 | ||
608 | if (vm_swap_up == TRUE && (VM_SWAP_SHOULD_CREATE(sec) || ((!VM_SWAP_BUSY() && compressor_store_stop_compaction == FALSE) && | |
609 | (VM_SWAP_SHOULD_DEFRAGMENT() || VM_SWAP_SHOULD_RECLAIM())))) { | |
610 | lck_mtx_unlock(&vm_swap_data_lock); | |
611 | goto try_again; | |
612 | } | |
613 | ||
614 | vm_swapfile_mgmt_thread_running = 0; | |
615 | ||
616 | assert_wait((event_t)&vm_swapfile_mgmt_needed, THREAD_UNINT); | |
617 | ||
618 | lck_mtx_unlock(&vm_swap_data_lock); | |
619 | ||
620 | thread_block((thread_continue_t)vm_swapfile_mgmt_thread); | |
621 | ||
622 | /* NOTREACHED */ | |
623 | } | |
624 | ||
625 | ||
626 | ||
627 | int swapper_entered_T0 = 0; | |
628 | int swapper_entered_T1 = 0; | |
629 | int swapper_entered_T2 = 0; | |
630 | ||
631 | static void | |
632 | vm_swapout_thread_throttle_adjust(void) | |
633 | { | |
634 | int swapper_throttle_new; | |
635 | ||
636 | if (swapper_throttle_inited == FALSE) { | |
637 | /* | |
638 | * force this thread to be set to the correct | |
639 | * throttling tier | |
640 | */ | |
641 | swapper_throttle_new = THROTTLE_LEVEL_COMPRESSOR_TIER2; | |
642 | swapper_throttle = THROTTLE_LEVEL_COMPRESSOR_TIER1; | |
643 | swapper_throttle_inited = TRUE; | |
644 | swapper_entered_T2++; | |
645 | goto done; | |
646 | } | |
647 | swapper_throttle_new = swapper_throttle; | |
648 | ||
649 | ||
650 | switch(swapper_throttle) { | |
651 | ||
652 | case THROTTLE_LEVEL_COMPRESSOR_TIER2: | |
653 | ||
654 | if (SWAPPER_NEEDS_TO_UNTHROTTLE() || swapout_target_age || hibernate_flushing == TRUE) { | |
655 | swapper_throttle_new = THROTTLE_LEVEL_COMPRESSOR_TIER1; | |
656 | swapper_entered_T1++; | |
657 | break; | |
658 | } | |
659 | break; | |
660 | ||
661 | case THROTTLE_LEVEL_COMPRESSOR_TIER1: | |
662 | ||
663 | if (VM_PAGEOUT_SCAN_NEEDS_TO_THROTTLE()) { | |
664 | swapper_throttle_new = THROTTLE_LEVEL_COMPRESSOR_TIER0; | |
665 | swapper_entered_T0++; | |
666 | break; | |
667 | } | |
668 | if (COMPRESSOR_NEEDS_TO_SWAP() == 0 && swapout_target_age == 0 && hibernate_flushing == FALSE) { | |
669 | swapper_throttle_new = THROTTLE_LEVEL_COMPRESSOR_TIER2; | |
670 | swapper_entered_T2++; | |
671 | break; | |
672 | } | |
673 | break; | |
674 | ||
675 | case THROTTLE_LEVEL_COMPRESSOR_TIER0: | |
676 | ||
677 | if (COMPRESSOR_NEEDS_TO_SWAP() == 0) { | |
678 | swapper_throttle_new = THROTTLE_LEVEL_COMPRESSOR_TIER2; | |
679 | swapper_entered_T2++; | |
680 | break; | |
681 | } | |
682 | if (SWAPPER_NEEDS_TO_UNTHROTTLE() == 0) { | |
683 | swapper_throttle_new = THROTTLE_LEVEL_COMPRESSOR_TIER1; | |
684 | swapper_entered_T1++; | |
685 | break; | |
686 | } | |
687 | break; | |
688 | } | |
689 | done: | |
690 | if (swapper_throttle != swapper_throttle_new) { | |
691 | proc_set_task_policy_thread(kernel_task, vm_swapout_thread_id, | |
692 | TASK_POLICY_INTERNAL, TASK_POLICY_IO, swapper_throttle_new); | |
693 | proc_set_task_policy_thread(kernel_task, vm_swapout_thread_id, | |
694 | TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE); | |
695 | ||
696 | swapper_throttle = swapper_throttle_new; | |
697 | } | |
698 | } | |
699 | ||
700 | ||
701 | static void | |
702 | vm_swapout_thread(void) | |
703 | { | |
704 | uint64_t f_offset = 0; | |
705 | uint32_t size = 0; | |
706 | c_segment_t c_seg = NULL; | |
707 | kern_return_t kr = KERN_SUCCESS; | |
708 | vm_offset_t addr = 0; | |
709 | ||
710 | vm_swapout_thread_awakened++; | |
711 | ||
712 | lck_mtx_lock_spin_always(c_list_lock); | |
713 | ||
714 | while (!queue_empty(&c_swapout_list_head)) { | |
715 | ||
716 | c_seg = (c_segment_t)queue_first(&c_swapout_list_head); | |
717 | ||
718 | lck_mtx_lock_spin_always(&c_seg->c_lock); | |
719 | ||
720 | assert(c_seg->c_on_swapout_q); | |
721 | ||
722 | if (c_seg->c_busy) { | |
723 | lck_mtx_unlock_always(&c_seg->c_lock); | |
724 | lck_mtx_unlock_always(c_list_lock); | |
725 | ||
726 | mutex_pause(2); | |
727 | ||
728 | lck_mtx_lock_spin_always(c_list_lock); | |
729 | ||
730 | continue; | |
731 | } | |
732 | queue_remove(&c_swapout_list_head, c_seg, c_segment_t, c_age_list); | |
733 | c_seg->c_on_swapout_q = 0; | |
734 | c_swapout_count--; | |
735 | ||
736 | c_seg->c_busy = 1; | |
737 | c_seg->c_busy_swapping = 1; | |
738 | ||
739 | vm_swapout_thread_processed_segments++; | |
740 | ||
741 | thread_wakeup((event_t)&compaction_swapper_running); | |
742 | ||
743 | lck_mtx_unlock_always(c_list_lock); | |
744 | ||
745 | addr = (vm_offset_t) c_seg->c_store.c_buffer; | |
746 | ||
747 | size = round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset)); | |
748 | ||
749 | lck_mtx_unlock_always(&c_seg->c_lock); | |
750 | ||
751 | #if CHECKSUM_THE_SWAP | |
752 | c_seg->cseg_hash = hash_string((char*)addr, (int)size); | |
753 | c_seg->cseg_swap_size = size; | |
754 | #endif /* CHECKSUM_THE_SWAP */ | |
755 | ||
756 | #if CRYPTO | |
757 | vm_swap_encrypt(c_seg); | |
758 | #endif /* CRYPTO */ | |
759 | ||
760 | vm_swapout_thread_throttle_adjust(); | |
761 | ||
762 | kr = vm_swap_put((vm_offset_t) addr, &f_offset, size, c_seg); | |
763 | ||
764 | PAGE_REPLACEMENT_DISALLOWED(TRUE); | |
765 | ||
766 | lck_mtx_lock_spin_always(c_list_lock); | |
767 | lck_mtx_lock_spin_always(&c_seg->c_lock); | |
768 | ||
769 | if (kr == KERN_SUCCESS) { | |
770 | ||
771 | if (C_SEG_ONDISK_IS_SPARSE(c_seg) && hibernate_flushing == FALSE) { | |
772 | ||
773 | c_seg_insert_into_q(&c_swappedout_sparse_list_head, c_seg); | |
774 | c_seg->c_on_swappedout_sparse_q = 1; | |
775 | c_swappedout_sparse_count++; | |
776 | ||
777 | } else { | |
778 | if (hibernate_flushing == TRUE && (c_seg->c_generation_id >= first_c_segment_to_warm_generation_id && | |
779 | c_seg->c_generation_id <= last_c_segment_to_warm_generation_id)) | |
780 | queue_enter_first(&c_swappedout_list_head, c_seg, c_segment_t, c_age_list); | |
781 | else | |
782 | queue_enter(&c_swappedout_list_head, c_seg, c_segment_t, c_age_list); | |
783 | c_seg->c_on_swappedout_q = 1; | |
784 | c_swappedout_count++; | |
785 | } | |
786 | c_seg->c_store.c_swap_handle = f_offset; | |
787 | c_seg->c_ondisk = 1; | |
788 | ||
789 | VM_STAT_INCR_BY(swapouts, size >> PAGE_SHIFT); | |
790 | ||
791 | if (c_seg->c_bytes_used) | |
792 | OSAddAtomic64(-c_seg->c_bytes_used, &compressor_bytes_used); | |
793 | } else { | |
794 | #if CRYPTO | |
795 | vm_swap_decrypt(c_seg); | |
796 | #endif /* CRYPTO */ | |
797 | c_seg_insert_into_q(&c_age_list_head, c_seg); | |
798 | c_seg->c_on_age_q = 1; | |
799 | c_age_count++; | |
800 | ||
801 | vm_swap_put_failures++; | |
802 | } | |
803 | lck_mtx_unlock_always(c_list_lock); | |
804 | ||
805 | c_seg->c_busy_swapping = 0; | |
806 | ||
807 | C_SEG_WAKEUP_DONE(c_seg); | |
808 | ||
809 | if (c_seg->c_must_free) | |
810 | c_seg_free(c_seg); | |
811 | else | |
812 | lck_mtx_unlock_always(&c_seg->c_lock); | |
813 | ||
814 | if (kr == KERN_SUCCESS) | |
815 | kernel_memory_depopulate(kernel_map, (vm_offset_t) addr, size, KMA_COMPRESSOR); | |
816 | ||
817 | PAGE_REPLACEMENT_DISALLOWED(FALSE); | |
818 | ||
819 | if (kr == KERN_SUCCESS) | |
820 | kmem_free(kernel_map, (vm_offset_t) addr, C_SEG_ALLOCSIZE); | |
821 | ||
822 | vm_pageout_io_throttle(); | |
823 | ||
824 | if (c_swapout_count == 0) | |
825 | vm_swap_consider_defragmenting(); | |
826 | ||
827 | lck_mtx_lock_spin_always(c_list_lock); | |
828 | } | |
829 | ||
830 | assert_wait((event_t)&c_swapout_list_head, THREAD_UNINT); | |
831 | ||
832 | lck_mtx_unlock_always(c_list_lock); | |
833 | ||
834 | thread_block((thread_continue_t)vm_swapout_thread); | |
835 | ||
836 | /* NOTREACHED */ | |
837 | } | |
838 | ||
839 | boolean_t | |
840 | vm_swap_create_file() | |
841 | { | |
842 | uint64_t size = 0; | |
843 | int namelen = 0; | |
844 | boolean_t swap_file_created = FALSE; | |
845 | boolean_t swap_file_reuse = FALSE; | |
846 | struct swapfile *swf = NULL; | |
847 | ||
848 | ||
849 | if (DEFAULT_PAGER_IS_ACTIVE || DEFAULT_FREEZER_IS_ACTIVE) { | |
850 | } | |
851 | ||
852 | /* | |
853 | * Any swapfile structure ready for re-use? | |
854 | */ | |
855 | ||
856 | lck_mtx_lock(&vm_swap_data_lock); | |
857 | ||
858 | swf = (struct swapfile*) queue_first(&swf_global_queue); | |
859 | ||
860 | while (queue_end(&swf_global_queue, (queue_entry_t)swf) == FALSE) { | |
861 | if (swf->swp_flags == SWAP_REUSE) { | |
862 | swap_file_reuse = TRUE; | |
863 | break; | |
864 | } | |
865 | swf = (struct swapfile*) queue_next(&swf->swp_queue); | |
866 | } | |
867 | ||
868 | lck_mtx_unlock(&vm_swap_data_lock); | |
869 | ||
870 | if (swap_file_reuse == FALSE) { | |
871 | ||
872 | namelen = SWAPFILENAME_LEN + SWAPFILENAME_INDEX_LEN + 1; | |
873 | ||
874 | swf = (struct swapfile*) kalloc(sizeof *swf); | |
875 | memset(swf, 0, sizeof(*swf)); | |
876 | ||
877 | swf->swp_index = vm_num_swap_files + 1; | |
878 | swf->swp_pathlen = namelen; | |
879 | swf->swp_path = (char*)kalloc(swf->swp_pathlen); | |
880 | ||
881 | memset(swf->swp_path, 0, namelen); | |
882 | ||
883 | snprintf(swf->swp_path, namelen, "%s%d", SWAP_FILE_NAME, vm_num_swap_files + 1); | |
884 | } | |
885 | ||
886 | vm_swapfile_open(swf->swp_path, &swf->swp_vp); | |
887 | ||
888 | if (swf->swp_vp == NULL) { | |
889 | if (swap_file_reuse == FALSE) { | |
890 | kfree(swf->swp_path, swf->swp_pathlen); | |
891 | kfree(swf, sizeof *swf); | |
892 | } | |
893 | return FALSE; | |
894 | } | |
895 | size = MAX_SWAP_FILE_SIZE; | |
896 | ||
897 | while (size >= MIN_SWAP_FILE_SIZE) { | |
898 | ||
899 | if (vm_swapfile_preallocate(swf->swp_vp, &size) == 0) { | |
900 | ||
901 | int num_bytes_for_bitmap = 0; | |
902 | ||
903 | swap_file_created = TRUE; | |
904 | ||
905 | swf->swp_size = size; | |
906 | swf->swp_nsegs = (unsigned int) (size / COMPRESSED_SWAP_CHUNK_SIZE); | |
907 | swf->swp_nseginuse = 0; | |
908 | swf->swp_free_hint = 0; | |
909 | ||
910 | num_bytes_for_bitmap = MAX((swf->swp_nsegs >> 3) , 1); | |
911 | /* | |
912 | * Allocate a bitmap that describes the | |
913 | * number of segments held by this swapfile. | |
914 | */ | |
915 | swf->swp_bitmap = (uint8_t*)kalloc(num_bytes_for_bitmap); | |
916 | memset(swf->swp_bitmap, 0, num_bytes_for_bitmap); | |
917 | ||
918 | swf->swp_csegs = (c_segment_t *) kalloc(swf->swp_nsegs * sizeof(c_segment_t)); | |
919 | memset(swf->swp_csegs, 0, (swf->swp_nsegs * sizeof(c_segment_t))); | |
920 | ||
921 | /* | |
922 | * passing a NULL trim_list into vnode_trim_list | |
923 | * will return ENOTSUP if trim isn't supported | |
924 | * and 0 if it is | |
925 | */ | |
926 | if (vnode_trim_list(swf->swp_vp, NULL)) | |
927 | swf->swp_trim_supported = FALSE; | |
928 | else | |
929 | swf->swp_trim_supported = TRUE; | |
930 | ||
931 | lck_mtx_lock(&vm_swap_data_lock); | |
932 | ||
933 | swf->swp_flags = SWAP_READY; | |
934 | ||
935 | if (swap_file_reuse == FALSE) { | |
936 | queue_enter(&swf_global_queue, swf, struct swapfile*, swp_queue); | |
937 | } | |
938 | ||
939 | vm_num_swap_files++; | |
940 | ||
941 | vm_swapfile_total_segs_alloced += swf->swp_nsegs; | |
942 | ||
943 | lck_mtx_unlock(&vm_swap_data_lock); | |
944 | ||
945 | thread_wakeup((event_t) &vm_num_swap_files); | |
946 | ||
947 | break; | |
948 | } else { | |
949 | ||
950 | size = size / 2; | |
951 | } | |
952 | } | |
953 | if (swap_file_created == FALSE) { | |
954 | ||
955 | vm_swapfile_close((uint64_t)(swf->swp_path), swf->swp_vp); | |
956 | ||
957 | swf->swp_vp = NULL; | |
958 | ||
959 | if (swap_file_reuse == FALSE) { | |
960 | kfree(swf->swp_path, swf->swp_pathlen); | |
961 | kfree(swf, sizeof *swf); | |
962 | } | |
963 | } | |
964 | return swap_file_created; | |
965 | } | |
966 | ||
967 | ||
968 | kern_return_t | |
969 | vm_swap_get(vm_offset_t addr, uint64_t f_offset, uint64_t size) | |
970 | { | |
971 | struct swapfile *swf = NULL; | |
972 | uint64_t file_offset = 0; | |
973 | int retval; | |
974 | ||
975 | if (addr == 0) { | |
976 | return KERN_FAILURE; | |
977 | } | |
978 | ||
979 | lck_mtx_lock(&vm_swap_data_lock); | |
980 | ||
981 | swf = vm_swapfile_for_handle(f_offset); | |
982 | ||
983 | if (swf) { | |
984 | if ((swf->swp_flags & SWAP_READY) || (swf->swp_flags & SWAP_RECLAIM)) { | |
985 | ||
986 | swf->swp_io_count++; | |
987 | file_offset = (f_offset & SWAP_SLOT_MASK); | |
988 | ||
989 | lck_mtx_unlock(&vm_swap_data_lock); | |
990 | ||
991 | } else { | |
992 | ||
993 | lck_mtx_unlock(&vm_swap_data_lock); | |
994 | return KERN_FAILURE; | |
995 | } | |
996 | } else { | |
997 | ||
998 | lck_mtx_unlock(&vm_swap_data_lock); | |
999 | return KERN_FAILURE; | |
1000 | } | |
1001 | ||
1002 | retval = vm_swapfile_io(swf->swp_vp, file_offset, addr, (int)(size / PAGE_SIZE_64), SWAP_READ); | |
1003 | ||
1004 | /* | |
1005 | * Free this slot in the swap structure. | |
1006 | */ | |
1007 | vm_swap_free(f_offset); | |
1008 | ||
1009 | lck_mtx_lock(&vm_swap_data_lock); | |
1010 | swf->swp_io_count--; | |
1011 | ||
1012 | if ((swf->swp_flags & SWAP_WANTED) && swf->swp_io_count == 0) { | |
1013 | ||
1014 | swf->swp_flags &= ~SWAP_WANTED; | |
1015 | thread_wakeup((event_t) &swf->swp_flags); | |
1016 | } | |
1017 | if (retval == 0) | |
1018 | VM_STAT_INCR_BY(swapins, size >> PAGE_SHIFT); | |
1019 | lck_mtx_unlock(&vm_swap_data_lock); | |
1020 | ||
1021 | if (retval == 0) | |
1022 | return KERN_SUCCESS; | |
1023 | else { | |
1024 | vm_swap_get_failures++; | |
1025 | return KERN_FAILURE; | |
1026 | } | |
1027 | } | |
1028 | ||
1029 | kern_return_t | |
1030 | vm_swap_put(vm_offset_t addr, uint64_t *f_offset, uint64_t size, c_segment_t c_seg) | |
1031 | { | |
1032 | unsigned int segidx = 0; | |
1033 | struct swapfile *swf = NULL; | |
1034 | uint64_t file_offset = 0; | |
1035 | uint64_t swapfile_index = 0; | |
1036 | unsigned int byte_for_segidx = 0; | |
1037 | unsigned int offset_within_byte = 0; | |
1038 | boolean_t swf_eligible = FALSE; | |
1039 | boolean_t waiting = FALSE; | |
1040 | int error = 0; | |
1041 | clock_sec_t sec; | |
1042 | clock_nsec_t nsec; | |
1043 | ||
1044 | if (addr == 0 || f_offset == NULL) { | |
1045 | return KERN_FAILURE; | |
1046 | } | |
1047 | ||
1048 | lck_mtx_lock(&vm_swap_data_lock); | |
1049 | ||
1050 | swf = (struct swapfile*) queue_first(&swf_global_queue); | |
1051 | ||
1052 | while(queue_end(&swf_global_queue, (queue_entry_t)swf) == FALSE) { | |
1053 | ||
1054 | segidx = swf->swp_free_hint; | |
1055 | ||
1056 | swf_eligible = (swf->swp_flags & SWAP_READY) && (swf->swp_nseginuse < swf->swp_nsegs); | |
1057 | ||
1058 | if (swf_eligible) { | |
1059 | ||
1060 | while(segidx < swf->swp_nsegs) { | |
1061 | ||
1062 | byte_for_segidx = segidx >> 3; | |
1063 | offset_within_byte = segidx % 8; | |
1064 | ||
1065 | if ((swf->swp_bitmap)[byte_for_segidx] & (1 << offset_within_byte)) { | |
1066 | segidx++; | |
1067 | continue; | |
1068 | } | |
1069 | ||
1070 | (swf->swp_bitmap)[byte_for_segidx] |= (1 << offset_within_byte); | |
1071 | ||
1072 | file_offset = segidx * COMPRESSED_SWAP_CHUNK_SIZE; | |
1073 | swf->swp_nseginuse++; | |
1074 | swf->swp_io_count++; | |
1075 | swapfile_index = swf->swp_index; | |
1076 | ||
1077 | vm_swapfile_total_segs_used++; | |
1078 | ||
1079 | clock_get_system_nanotime(&sec, &nsec); | |
1080 | ||
1081 | if (VM_SWAP_SHOULD_CREATE(sec) && !vm_swapfile_mgmt_thread_running) | |
1082 | thread_wakeup((event_t) &vm_swapfile_mgmt_needed); | |
1083 | ||
1084 | lck_mtx_unlock(&vm_swap_data_lock); | |
1085 | ||
1086 | goto done; | |
1087 | } | |
1088 | } | |
1089 | swf = (struct swapfile*) queue_next(&swf->swp_queue); | |
1090 | } | |
1091 | assert(queue_end(&swf_global_queue, (queue_entry_t) swf)); | |
1092 | ||
1093 | /* | |
1094 | * we've run out of swap segments, but may not | |
1095 | * be in a position to immediately create a new swap | |
1096 | * file if we've recently failed to create due to a lack | |
1097 | * of free space in the root filesystem... we'll try | |
1098 | * to kick that create off, but in any event we're going | |
1099 | * to take a breather (up to 1 second) so that we're not caught in a tight | |
1100 | * loop back in "vm_compressor_compact_and_swap" trying to stuff | |
1101 | * segments into swap files only to have them immediately put back | |
1102 | * on the c_age queue due to vm_swap_put failing. | |
1103 | * | |
1104 | * if we're doing these puts due to a hibernation flush, | |
1105 | * no need to block... setting hibernate_no_swapspace to TRUE, | |
1106 | * will cause "vm_compressor_compact_and_swap" to immediately abort | |
1107 | */ | |
1108 | clock_get_system_nanotime(&sec, &nsec); | |
1109 | ||
1110 | if (VM_SWAP_SHOULD_CREATE(sec) && !vm_swapfile_mgmt_thread_running) | |
1111 | thread_wakeup((event_t) &vm_swapfile_mgmt_needed); | |
1112 | ||
1113 | if (hibernate_flushing == FALSE || VM_SWAP_SHOULD_CREATE(sec)) { | |
1114 | waiting = TRUE; | |
1115 | assert_wait_timeout((event_t) &vm_num_swap_files, THREAD_INTERRUPTIBLE, 1000, 1000*NSEC_PER_USEC); | |
1116 | } else | |
1117 | hibernate_no_swapspace = TRUE; | |
1118 | ||
1119 | lck_mtx_unlock(&vm_swap_data_lock); | |
1120 | ||
1121 | if (waiting == TRUE) | |
1122 | thread_block(THREAD_CONTINUE_NULL); | |
1123 | ||
1124 | return KERN_FAILURE; | |
1125 | ||
1126 | done: | |
1127 | error = vm_swapfile_io(swf->swp_vp, file_offset, addr, (int) (size / PAGE_SIZE_64), SWAP_WRITE); | |
1128 | ||
1129 | lck_mtx_lock(&vm_swap_data_lock); | |
1130 | ||
1131 | swf->swp_csegs[segidx] = c_seg; | |
1132 | ||
1133 | swf->swp_io_count--; | |
1134 | ||
1135 | *f_offset = (swapfile_index << SWAP_DEVICE_SHIFT) | file_offset; | |
1136 | ||
1137 | if ((swf->swp_flags & SWAP_WANTED) && swf->swp_io_count == 0) { | |
1138 | ||
1139 | swf->swp_flags &= ~SWAP_WANTED; | |
1140 | thread_wakeup((event_t) &swf->swp_flags); | |
1141 | } | |
1142 | ||
1143 | lck_mtx_unlock(&vm_swap_data_lock); | |
1144 | ||
1145 | #if SANITY_CHECK_SWAP_ROUTINES | |
1146 | printf("Returned 0x%llx as offset\n", *f_offset); | |
1147 | #endif /* SANITY_CHECK_SWAP_ROUTINES */ | |
1148 | ||
1149 | if (error) { | |
1150 | vm_swap_free(*f_offset); | |
1151 | ||
1152 | return KERN_FAILURE; | |
1153 | } | |
1154 | return KERN_SUCCESS; | |
1155 | } | |
1156 | ||
1157 | ||
1158 | ||
1159 | static void | |
1160 | vm_swap_free_now(struct swapfile *swf, uint64_t f_offset) | |
1161 | { | |
1162 | uint64_t file_offset = 0; | |
1163 | unsigned int segidx = 0; | |
1164 | ||
1165 | ||
1166 | if ((swf->swp_flags & SWAP_READY) || (swf->swp_flags & SWAP_RECLAIM)) { | |
1167 | ||
1168 | unsigned int byte_for_segidx = 0; | |
1169 | unsigned int offset_within_byte = 0; | |
1170 | ||
1171 | file_offset = (f_offset & SWAP_SLOT_MASK); | |
1172 | segidx = (unsigned int) (file_offset / COMPRESSED_SWAP_CHUNK_SIZE); | |
1173 | ||
1174 | byte_for_segidx = segidx >> 3; | |
1175 | offset_within_byte = segidx % 8; | |
1176 | ||
1177 | if ((swf->swp_bitmap)[byte_for_segidx] & (1 << offset_within_byte)) { | |
1178 | ||
1179 | (swf->swp_bitmap)[byte_for_segidx] &= ~(1 << offset_within_byte); | |
1180 | ||
1181 | swf->swp_csegs[segidx] = NULL; | |
1182 | ||
1183 | swf->swp_nseginuse--; | |
1184 | vm_swapfile_total_segs_used--; | |
1185 | ||
1186 | if (segidx < swf->swp_free_hint) { | |
1187 | swf->swp_free_hint = segidx; | |
1188 | } | |
1189 | } | |
1190 | if (VM_SWAP_SHOULD_RECLAIM() && !vm_swapfile_mgmt_thread_running) | |
1191 | thread_wakeup((event_t) &vm_swapfile_mgmt_needed); | |
1192 | } | |
1193 | lck_mtx_unlock(&vm_swap_data_lock); | |
1194 | } | |
1195 | ||
1196 | ||
1197 | uint32_t vm_swap_free_now_count = 0; | |
1198 | uint32_t vm_swap_free_delayed_count = 0; | |
1199 | ||
1200 | ||
1201 | void | |
1202 | vm_swap_free(uint64_t f_offset) | |
1203 | { | |
1204 | struct swapfile *swf = NULL; | |
1205 | struct trim_list *tl; | |
1206 | clock_sec_t sec; | |
1207 | clock_nsec_t nsec; | |
1208 | ||
1209 | lck_mtx_lock(&vm_swap_data_lock); | |
1210 | ||
1211 | swf = vm_swapfile_for_handle(f_offset); | |
1212 | ||
1213 | if (swf && (swf->swp_flags & (SWAP_READY | SWAP_RECLAIM))) { | |
1214 | ||
1215 | if (swf->swp_trim_supported == FALSE || (swf->swp_flags & SWAP_RECLAIM)) { | |
1216 | /* | |
1217 | * don't delay the free if the underlying disk doesn't support | |
1218 | * trim, or we're in the midst of reclaiming this swap file since | |
1219 | * we don't want to move segments that are technically free | |
1220 | * but not yet handled by the delayed free mechanism | |
1221 | */ | |
1222 | vm_swap_free_now(swf, f_offset); | |
1223 | ||
1224 | vm_swap_free_now_count++; | |
1225 | return; | |
1226 | } | |
1227 | tl = kalloc(sizeof(struct trim_list)); | |
1228 | ||
1229 | tl->tl_offset = f_offset & SWAP_SLOT_MASK; | |
1230 | tl->tl_length = COMPRESSED_SWAP_CHUNK_SIZE; | |
1231 | ||
1232 | tl->tl_next = swf->swp_delayed_trim_list_head; | |
1233 | swf->swp_delayed_trim_list_head = tl; | |
1234 | swf->swp_delayed_trim_count++; | |
1235 | ||
1236 | if (VM_SWAP_SHOULD_TRIM(swf) && !vm_swapfile_mgmt_thread_running) { | |
1237 | clock_get_system_nanotime(&sec, &nsec); | |
1238 | ||
1239 | if (sec > dont_trim_until_ts) | |
1240 | thread_wakeup((event_t) &vm_swapfile_mgmt_needed); | |
1241 | } | |
1242 | vm_swap_free_delayed_count++; | |
1243 | } | |
1244 | lck_mtx_unlock(&vm_swap_data_lock); | |
1245 | } | |
1246 | ||
1247 | ||
1248 | static void | |
1249 | vm_swap_handle_delayed_trims(boolean_t force_now) | |
1250 | { | |
1251 | struct swapfile *swf = NULL; | |
1252 | ||
1253 | /* | |
1254 | * because swap files are created or reclaimed on the | |
1255 | * same thread that calls this function, it's safe | |
1256 | * to iterate "swf_global_queue" w/o holding | |
1257 | * the lock since those are the only 2 cases that can | |
1258 | * change the items on the "swf_global_queue" | |
1259 | */ | |
1260 | swf = (struct swapfile*) queue_first(&swf_global_queue); | |
1261 | ||
1262 | while (queue_end(&swf_global_queue, (queue_entry_t)swf) == FALSE) { | |
1263 | ||
1264 | assert(!(swf->swp_flags & SWAP_RECLAIM)); | |
1265 | ||
1266 | if ((swf->swp_flags & SWAP_READY) && (force_now == TRUE || VM_SWAP_SHOULD_TRIM(swf))) | |
1267 | vm_swap_do_delayed_trim(swf); | |
1268 | ||
1269 | swf = (struct swapfile*) queue_next(&swf->swp_queue); | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | ||
1274 | static void | |
1275 | vm_swap_do_delayed_trim(struct swapfile *swf) | |
1276 | { | |
1277 | struct trim_list *tl, *tl_head; | |
1278 | ||
1279 | lck_mtx_lock(&vm_swap_data_lock); | |
1280 | ||
1281 | tl_head = swf->swp_delayed_trim_list_head; | |
1282 | swf->swp_delayed_trim_list_head = NULL; | |
1283 | swf->swp_delayed_trim_count = 0; | |
1284 | ||
1285 | lck_mtx_unlock(&vm_swap_data_lock); | |
1286 | ||
1287 | vnode_trim_list(swf->swp_vp, tl_head); | |
1288 | ||
1289 | while ((tl = tl_head) != NULL) { | |
1290 | unsigned int segidx = 0; | |
1291 | unsigned int byte_for_segidx = 0; | |
1292 | unsigned int offset_within_byte = 0; | |
1293 | ||
1294 | lck_mtx_lock(&vm_swap_data_lock); | |
1295 | ||
1296 | segidx = (unsigned int) (tl->tl_offset / COMPRESSED_SWAP_CHUNK_SIZE); | |
1297 | ||
1298 | byte_for_segidx = segidx >> 3; | |
1299 | offset_within_byte = segidx % 8; | |
1300 | ||
1301 | if ((swf->swp_bitmap)[byte_for_segidx] & (1 << offset_within_byte)) { | |
1302 | ||
1303 | (swf->swp_bitmap)[byte_for_segidx] &= ~(1 << offset_within_byte); | |
1304 | ||
1305 | swf->swp_csegs[segidx] = NULL; | |
1306 | ||
1307 | swf->swp_nseginuse--; | |
1308 | vm_swapfile_total_segs_used--; | |
1309 | ||
1310 | if (segidx < swf->swp_free_hint) { | |
1311 | swf->swp_free_hint = segidx; | |
1312 | } | |
1313 | } | |
1314 | lck_mtx_unlock(&vm_swap_data_lock); | |
1315 | ||
1316 | tl_head = tl->tl_next; | |
1317 | ||
1318 | kfree(tl, sizeof(struct trim_list)); | |
1319 | } | |
1320 | } | |
1321 | ||
1322 | ||
1323 | void | |
1324 | vm_swap_flush() | |
1325 | { | |
1326 | return; | |
1327 | } | |
1328 | ||
1329 | int vm_swap_reclaim_yielded = 0; | |
1330 | ||
1331 | void | |
1332 | vm_swap_reclaim(void) | |
1333 | { | |
1334 | vm_offset_t addr = 0; | |
1335 | unsigned int segidx = 0; | |
1336 | uint64_t f_offset = 0; | |
1337 | struct swapfile *swf = NULL; | |
1338 | struct swapfile *smallest_swf = NULL; | |
1339 | unsigned int min_nsegs = 0; | |
1340 | unsigned int byte_for_segidx = 0; | |
1341 | unsigned int offset_within_byte = 0; | |
1342 | uint32_t c_size = 0; | |
1343 | ||
1344 | c_segment_t c_seg = NULL; | |
1345 | ||
1346 | if (kernel_memory_allocate(kernel_map, (vm_offset_t *)(&addr), C_SEG_BUFSIZE, 0, KMA_KOBJECT) != KERN_SUCCESS) { | |
1347 | panic("vm_swap_reclaim: kernel_memory_allocate failed\n"); | |
1348 | } | |
1349 | ||
1350 | lck_mtx_lock(&vm_swap_data_lock); | |
1351 | ||
1352 | swf = (struct swapfile*) queue_first(&swf_global_queue); | |
1353 | min_nsegs = MAX_SWAP_FILE_SIZE / COMPRESSED_SWAP_CHUNK_SIZE; | |
1354 | smallest_swf = NULL; | |
1355 | ||
1356 | while (queue_end(&swf_global_queue, (queue_entry_t)swf) == FALSE) { | |
1357 | ||
1358 | if ((swf->swp_flags & SWAP_READY) && (swf->swp_nseginuse <= min_nsegs)) { | |
1359 | ||
1360 | smallest_swf = swf; | |
1361 | min_nsegs = swf->swp_nseginuse; | |
1362 | } | |
1363 | swf = (struct swapfile*) queue_next(&swf->swp_queue); | |
1364 | } | |
1365 | ||
1366 | if (smallest_swf == NULL) | |
1367 | goto done; | |
1368 | ||
1369 | swf = smallest_swf; | |
1370 | ||
1371 | ||
1372 | swf->swp_flags &= ~SWAP_READY; | |
1373 | swf->swp_flags |= SWAP_RECLAIM; | |
1374 | ||
1375 | if (swf->swp_delayed_trim_count) { | |
1376 | ||
1377 | lck_mtx_unlock(&vm_swap_data_lock); | |
1378 | ||
1379 | vm_swap_do_delayed_trim(swf); | |
1380 | ||
1381 | lck_mtx_lock(&vm_swap_data_lock); | |
1382 | } | |
1383 | segidx = 0; | |
1384 | ||
1385 | while (segidx < swf->swp_nsegs) { | |
1386 | ||
1387 | ReTry_for_cseg: | |
1388 | if (compressor_store_stop_compaction == TRUE || (swf->swp_trim_supported == FALSE && VM_SWAP_BUSY())) { | |
1389 | vm_swap_reclaim_yielded++; | |
1390 | break; | |
1391 | } | |
1392 | /* | |
1393 | * Wait for outgoing I/Os. | |
1394 | */ | |
1395 | while (swf->swp_io_count) { | |
1396 | ||
1397 | swf->swp_flags |= SWAP_WANTED; | |
1398 | ||
1399 | assert_wait((event_t) &swf->swp_flags, THREAD_UNINT); | |
1400 | lck_mtx_unlock(&vm_swap_data_lock); | |
1401 | ||
1402 | thread_block(THREAD_CONTINUE_NULL); | |
1403 | ||
1404 | lck_mtx_lock(&vm_swap_data_lock); | |
1405 | } | |
1406 | ||
1407 | byte_for_segidx = segidx >> 3; | |
1408 | offset_within_byte = segidx % 8; | |
1409 | ||
1410 | if (((swf->swp_bitmap)[byte_for_segidx] & (1 << offset_within_byte)) == 0) { | |
1411 | ||
1412 | segidx++; | |
1413 | continue; | |
1414 | } | |
1415 | ||
1416 | c_seg = swf->swp_csegs[segidx]; | |
1417 | ||
1418 | lck_mtx_lock_spin_always(&c_seg->c_lock); | |
1419 | ||
1420 | assert(c_seg->c_ondisk); | |
1421 | ||
1422 | if (c_seg->c_busy) { | |
1423 | ||
1424 | c_seg->c_wanted = 1; | |
1425 | ||
1426 | assert_wait((event_t) (c_seg), THREAD_UNINT); | |
1427 | lck_mtx_unlock_always(&c_seg->c_lock); | |
1428 | ||
1429 | lck_mtx_unlock(&vm_swap_data_lock); | |
1430 | ||
1431 | thread_block(THREAD_CONTINUE_NULL); | |
1432 | ||
1433 | lck_mtx_lock(&vm_swap_data_lock); | |
1434 | ||
1435 | goto ReTry_for_cseg; | |
1436 | } | |
1437 | (swf->swp_bitmap)[byte_for_segidx] &= ~(1 << offset_within_byte); | |
1438 | ||
1439 | f_offset = segidx * COMPRESSED_SWAP_CHUNK_SIZE; | |
1440 | ||
1441 | swf->swp_csegs[segidx] = NULL; | |
1442 | swf->swp_nseginuse--; | |
1443 | ||
1444 | vm_swapfile_total_segs_used--; | |
1445 | ||
1446 | lck_mtx_unlock(&vm_swap_data_lock); | |
1447 | ||
1448 | if (c_seg->c_must_free) { | |
1449 | ||
1450 | c_seg_free(c_seg); | |
1451 | } else { | |
1452 | ||
1453 | c_seg->c_busy = 1; | |
1454 | c_seg->c_busy_swapping = 1; | |
1455 | #if !CHECKSUM_THE_SWAP | |
1456 | c_seg_trim_tail(c_seg); | |
1457 | #endif | |
1458 | ||
1459 | #if SANITY_CHECK_SWAP_ROUTINES | |
1460 | ||
1461 | c_size = COMPRESSED_SWAP_CHUNK_SIZE; | |
1462 | ||
1463 | #else /* SANITY_CHECK_SWAP_ROUTINES */ | |
1464 | ||
1465 | c_size = round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset)); | |
1466 | ||
1467 | assert(c_size <= C_SEG_BUFSIZE); | |
1468 | ||
1469 | #endif /* SANITY_CHECK_SWAP_ROUTINES */ | |
1470 | ||
1471 | lck_mtx_unlock_always(&c_seg->c_lock); | |
1472 | ||
1473 | if (vm_swapfile_io(swf->swp_vp, f_offset, addr, (int)(c_size / PAGE_SIZE_64), SWAP_READ)) { | |
1474 | ||
1475 | /* | |
1476 | * reading the data back in failed, so convert c_seg | |
1477 | * to a swapped in c_segment that contains no data | |
1478 | */ | |
1479 | c_seg->c_store.c_buffer = (int32_t *)NULL; | |
1480 | c_seg_swapin_requeue(c_seg); | |
1481 | ||
1482 | goto swap_io_failed; | |
1483 | } | |
1484 | VM_STAT_INCR_BY(swapins, c_size >> PAGE_SHIFT); | |
1485 | ||
1486 | if (vm_swap_put(addr, &f_offset, c_size, c_seg)) { | |
1487 | vm_offset_t c_buffer; | |
1488 | ||
1489 | /* | |
1490 | * the put failed, so convert c_seg to a fully swapped in c_segment | |
1491 | * with valid data | |
1492 | */ | |
1493 | if (kernel_memory_allocate(kernel_map, &c_buffer, C_SEG_ALLOCSIZE, 0, KMA_COMPRESSOR | KMA_VAONLY) != KERN_SUCCESS) | |
1494 | panic("vm_swap_reclaim: kernel_memory_allocate failed\n"); | |
1495 | kernel_memory_populate(kernel_map, c_buffer, c_size, KMA_COMPRESSOR); | |
1496 | ||
1497 | memcpy((char *)c_buffer, (char *)addr, c_size); | |
1498 | ||
1499 | c_seg->c_store.c_buffer = (int32_t *)c_buffer; | |
1500 | #if CRYPTO | |
1501 | vm_swap_decrypt(c_seg); | |
1502 | #endif /* CRYPTO */ | |
1503 | c_seg_swapin_requeue(c_seg); | |
1504 | ||
1505 | OSAddAtomic64(c_seg->c_bytes_used, &compressor_bytes_used); | |
1506 | ||
1507 | goto swap_io_failed; | |
1508 | } | |
1509 | VM_STAT_INCR_BY(swapouts, c_size >> PAGE_SHIFT); | |
1510 | ||
1511 | lck_mtx_lock_spin_always(&c_seg->c_lock); | |
1512 | ||
1513 | assert(c_seg->c_ondisk); | |
1514 | /* | |
1515 | * The c_seg will now know about the new location on disk. | |
1516 | */ | |
1517 | c_seg->c_store.c_swap_handle = f_offset; | |
1518 | swap_io_failed: | |
1519 | c_seg->c_busy_swapping = 0; | |
1520 | ||
1521 | if (c_seg->c_must_free) | |
1522 | c_seg_free(c_seg); | |
1523 | else { | |
1524 | C_SEG_WAKEUP_DONE(c_seg); | |
1525 | ||
1526 | lck_mtx_unlock_always(&c_seg->c_lock); | |
1527 | } | |
1528 | } | |
1529 | lck_mtx_lock(&vm_swap_data_lock); | |
1530 | } | |
1531 | ||
1532 | if (swf->swp_nseginuse) { | |
1533 | ||
1534 | swf->swp_flags &= ~SWAP_RECLAIM; | |
1535 | swf->swp_flags |= SWAP_READY; | |
1536 | ||
1537 | goto done; | |
1538 | } | |
1539 | /* | |
1540 | * We don't remove this inactive swf from the queue. | |
1541 | * That way, we can re-use it when needed again and | |
1542 | * preserve the namespace. | |
1543 | */ | |
1544 | //queue_remove(&swf_global_queue, swf, struct swapfile*, swp_queue); | |
1545 | ||
1546 | vm_num_swap_files--; | |
1547 | ||
1548 | vm_swapfile_total_segs_alloced -= swf->swp_nsegs; | |
1549 | ||
1550 | lck_mtx_unlock(&vm_swap_data_lock); | |
1551 | ||
1552 | vm_swapfile_close((uint64_t)(swf->swp_path), swf->swp_vp); | |
1553 | ||
1554 | kfree(swf->swp_csegs, swf->swp_nsegs * sizeof(c_segment_t)); | |
1555 | kfree(swf->swp_bitmap, MAX((swf->swp_nsegs >> 3), 1)); | |
1556 | ||
1557 | lck_mtx_lock(&vm_swap_data_lock); | |
1558 | ||
1559 | swf->swp_vp = NULL; | |
1560 | swf->swp_size = 0; | |
1561 | swf->swp_free_hint = 0; | |
1562 | swf->swp_nsegs = 0; | |
1563 | swf->swp_flags = SWAP_REUSE; | |
1564 | ||
1565 | thread_wakeup((event_t) &swf->swp_flags); | |
1566 | done: | |
1567 | lck_mtx_unlock(&vm_swap_data_lock); | |
1568 | ||
1569 | kmem_free(kernel_map, (vm_offset_t) addr, C_SEG_BUFSIZE); | |
1570 | } | |
1571 | ||
1572 | ||
1573 | uint64_t | |
1574 | vm_swap_get_total_space(void) | |
1575 | { | |
1576 | uint64_t total_space = 0; | |
1577 | ||
1578 | total_space = (uint64_t)vm_swapfile_total_segs_alloced * COMPRESSED_SWAP_CHUNK_SIZE; | |
1579 | ||
1580 | return total_space; | |
1581 | } | |
1582 | ||
1583 | uint64_t | |
1584 | vm_swap_get_used_space(void) | |
1585 | { | |
1586 | uint64_t used_space = 0; | |
1587 | ||
1588 | used_space = (uint64_t)vm_swapfile_total_segs_used * COMPRESSED_SWAP_CHUNK_SIZE; | |
1589 | ||
1590 | return used_space; | |
1591 | } | |
1592 | ||
1593 | uint64_t | |
1594 | vm_swap_get_free_space(void) | |
1595 | { | |
1596 | return (vm_swap_get_total_space() - vm_swap_get_used_space()); | |
1597 | } |