]> git.saurik.com Git - apple/xnu.git/blame - osfmk/vm/vm_compressor_backing_store.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / vm / vm_compressor_backing_store.c
CommitLineData
39236c6e
A
1/*
2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
39236c6e
A
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.
0a7de745 14 *
39236c6e
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
39236c6e
A
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.
0a7de745 25 *
39236c6e
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include "vm_compressor_backing_store.h"
d9a64523 30#include <vm/vm_pageout.h>
39236c6e
A
31#include <vm/vm_protos.h>
32
33#include <IOKit/IOHibernatePrivate.h>
34
39037602 35#include <kern/policy_internal.h>
39236c6e 36
f427ee49
A
37LCK_GRP_DECLARE(vm_swap_data_lock_grp, "vm_swap_data");
38LCK_MTX_EARLY_DECLARE(vm_swap_data_lock, &vm_swap_data_lock_grp);
39
40#if defined(XNU_TARGET_OS_OSX)
41/*
42 * launchd explicitly turns ON swap later during boot on macOS devices.
43 */
44boolean_t compressor_store_stop_compaction = TRUE;
45#else
0a7de745 46boolean_t compressor_store_stop_compaction = FALSE;
f427ee49
A
47#endif
48
0a7de745
A
49boolean_t vm_swapfile_create_needed = FALSE;
50boolean_t vm_swapfile_gc_needed = FALSE;
51
52int vm_swapper_throttle = -1;
53uint64_t vm_swapout_thread_id;
54
55uint64_t vm_swap_put_failures = 0; /* Likely failed I/O. Data is still in memory. */
56uint64_t vm_swap_get_failures = 0; /* Fatal */
57uint64_t vm_swap_put_failures_no_swap_file = 0; /* Possibly not fatal because we might just need a new swapfile. */
58int vm_num_swap_files_config = 0;
59int vm_num_swap_files = 0;
60int vm_num_pinned_swap_files = 0;
61int vm_swapout_thread_processed_segments = 0;
62int vm_swapout_thread_awakened = 0;
f427ee49 63bool vm_swapout_thread_running = FALSE;
0a7de745
A
64int vm_swapfile_create_thread_awakened = 0;
65int vm_swapfile_create_thread_running = 0;
66int vm_swapfile_gc_thread_awakened = 0;
67int vm_swapfile_gc_thread_running = 0;
68
69int64_t vm_swappin_avail = 0;
70boolean_t vm_swappin_enabled = FALSE;
71unsigned int vm_swapfile_total_segs_alloced = 0;
72unsigned int vm_swapfile_total_segs_used = 0;
73
74char swapfilename[MAX_SWAPFILENAME_LEN + 1] = SWAP_FILE_NAME;
5ba3f43e 75
39037602
A
76extern vm_map_t compressor_map;
77
39236c6e 78
0a7de745
A
79#define SWAP_READY 0x1 /* Swap file is ready to be used */
80#define SWAP_RECLAIM 0x2 /* Swap file is marked to be reclaimed */
81#define SWAP_WANTED 0x4 /* Swap file has waiters */
82#define SWAP_REUSE 0x8 /* Swap file is on the Q and has a name. Reuse after init-ing.*/
83#define SWAP_PINNED 0x10 /* Swap file is pinned (FusionDrive) */
84
85
86struct swapfile {
87 queue_head_t swp_queue; /* list of swap files */
88 char *swp_path; /* saved pathname of swap file */
89 struct vnode *swp_vp; /* backing vnode */
90 uint64_t swp_size; /* size of this swap file */
91 uint8_t *swp_bitmap; /* bitmap showing the alloced/freed slots in the swap file */
92 unsigned int swp_pathlen; /* length of pathname */
93 unsigned int swp_nsegs; /* #segments we can use */
94 unsigned int swp_nseginuse; /* #segments in use */
95 unsigned int swp_index; /* index of this swap file */
96 unsigned int swp_flags; /* state of swap file */
97 unsigned int swp_free_hint; /* offset of 1st free chunk */
98 unsigned int swp_io_count; /* count of outstanding I/Os */
99 c_segment_t *swp_csegs; /* back pointers to the c_segments. Used during swap reclaim. */
100
101 struct trim_list *swp_delayed_trim_list_head;
102 unsigned int swp_delayed_trim_count;
39236c6e
A
103};
104
0a7de745
A
105queue_head_t swf_global_queue;
106boolean_t swp_trim_supported = FALSE;
39236c6e 107
0a7de745
A
108extern clock_sec_t dont_trim_until_ts;
109clock_sec_t vm_swapfile_last_failed_to_create_ts = 0;
110clock_sec_t vm_swapfile_last_successful_create_ts = 0;
111int vm_swapfile_can_be_created = FALSE;
112boolean_t delayed_trim_handling_in_progress = FALSE;
39236c6e 113
0a7de745 114boolean_t hibernate_in_progress_with_pinned_swap = FALSE;
39037602 115
39236c6e
A
116static void vm_swapout_thread_throttle_adjust(void);
117static void vm_swap_free_now(struct swapfile *swf, uint64_t f_offset);
118static void vm_swapout_thread(void);
fe8ab488
A
119static void vm_swapfile_create_thread(void);
120static void vm_swapfile_gc_thread(void);
5ba3f43e 121static void vm_swap_defragment(void);
39236c6e 122static void vm_swap_handle_delayed_trims(boolean_t);
5ba3f43e 123static void vm_swap_do_delayed_trim(struct swapfile *);
fe8ab488
A
124static void vm_swap_wait_on_trim_handling_in_progress(void);
125
f427ee49 126extern int vnode_getwithref(struct vnode* vp);
39236c6e 127
d9a64523
A
128boolean_t vm_swap_force_defrag = FALSE, vm_swap_force_reclaim = FALSE;
129
c3c9b80d 130#if !XNU_TARGET_OS_OSX
d9a64523 131
f427ee49
A
132/*
133 * For CONFIG_FREEZE, we scale the c_segments_limit based on the
134 * number of swapfiles allowed. That increases wired memory overhead.
135 * So we want to keep the max swapfiles same on both DEV/RELEASE so
136 * that the memory overhead is similar for performance comparisons.
137 */
0a7de745 138#define VM_MAX_SWAP_FILE_NUM 5
d9a64523 139
0a7de745 140#define VM_SWAPFILE_DELAYED_TRIM_MAX 4
5ba3f43e 141
0a7de745
A
142#define VM_SWAP_SHOULD_DEFRAGMENT() (((vm_swap_force_defrag == TRUE) || (c_swappedout_sparse_count > (vm_swapfile_total_segs_used / 16))) ? 1 : 0)
143#define VM_SWAP_SHOULD_PIN(_size) FALSE
144#define VM_SWAP_SHOULD_CREATE(cur_ts) ((vm_num_swap_files < vm_num_swap_files_config) && ((vm_swapfile_total_segs_alloced - vm_swapfile_total_segs_used) < (unsigned int)VM_SWAPFILE_HIWATER_SEGS) && \
145 ((cur_ts - vm_swapfile_last_failed_to_create_ts) > VM_SWAPFILE_DELAYED_CREATE) ? 1 : 0)
146#define VM_SWAP_SHOULD_TRIM(swf) ((swf->swp_delayed_trim_count >= VM_SWAPFILE_DELAYED_TRIM_MAX) ? 1 : 0)
5ba3f43e 147
c3c9b80d 148#else /* !XNU_TARGET_OS_OSX */
39236c6e 149
0a7de745
A
150#define VM_MAX_SWAP_FILE_NUM 100
151#define VM_SWAPFILE_DELAYED_TRIM_MAX 128
3e170ce0 152
0a7de745
A
153#define VM_SWAP_SHOULD_DEFRAGMENT() (((vm_swap_force_defrag == TRUE) || (c_swappedout_sparse_count > (vm_swapfile_total_segs_used / 4))) ? 1 : 0)
154#define VM_SWAP_SHOULD_PIN(_size) (vm_swappin_avail > 0 && vm_swappin_avail >= (int64_t)(_size))
155#define VM_SWAP_SHOULD_CREATE(cur_ts) ((vm_num_swap_files < vm_num_swap_files_config) && ((vm_swapfile_total_segs_alloced - vm_swapfile_total_segs_used) < (unsigned int)VM_SWAPFILE_HIWATER_SEGS) && \
156 ((cur_ts - vm_swapfile_last_failed_to_create_ts) > VM_SWAPFILE_DELAYED_CREATE) ? 1 : 0)
157#define VM_SWAP_SHOULD_TRIM(swf) ((swf->swp_delayed_trim_count >= VM_SWAPFILE_DELAYED_TRIM_MAX) ? 1 : 0)
39236c6e 158
c3c9b80d 159#endif /* !XNU_TARGET_OS_OSX */
39236c6e 160
0a7de745
A
161#define VM_SWAP_SHOULD_RECLAIM() (((vm_swap_force_reclaim == TRUE) || ((vm_swapfile_total_segs_alloced - vm_swapfile_total_segs_used) >= SWAPFILE_RECLAIM_THRESHOLD_SEGS)) ? 1 : 0)
162#define VM_SWAP_SHOULD_ABORT_RECLAIM() (((vm_swap_force_reclaim == FALSE) && ((vm_swapfile_total_segs_alloced - vm_swapfile_total_segs_used) <= SWAPFILE_RECLAIM_MINIMUM_SEGS)) ? 1 : 0)
163#define VM_SWAPFILE_DELAYED_CREATE 15
fe8ab488 164
0a7de745 165#define VM_SWAP_BUSY() ((c_swapout_count && (vm_swapper_throttle == THROTTLE_LEVEL_COMPRESSOR_TIER0)) ? 1 : 0)
39236c6e
A
166
167
168#if CHECKSUM_THE_SWAP
169extern unsigned int hash_string(char *cp, int len);
170#endif
171
3e170ce0 172#if RECORD_THE_COMPRESSED_DATA
0a7de745
A
173boolean_t c_compressed_record_init_done = FALSE;
174int c_compressed_record_write_error = 0;
175struct vnode *c_compressed_record_vp = NULL;
176uint64_t c_compressed_record_file_offset = 0;
177void c_compressed_record_init(void);
178void c_compressed_record_write(char *, int);
3e170ce0
A
179#endif
180
0a7de745 181extern void vm_pageout_io_throttle(void);
39236c6e 182
39037602 183static struct swapfile *vm_swapfile_for_handle(uint64_t);
39236c6e
A
184
185/*
186 * Called with the vm_swap_data_lock held.
0a7de745 187 */
39236c6e 188
39037602 189static struct swapfile *
0a7de745 190vm_swapfile_for_handle(uint64_t f_offset)
39236c6e 191{
0a7de745
A
192 uint64_t file_offset = 0;
193 unsigned int swapfile_index = 0;
194 struct swapfile* swf = NULL;
39236c6e 195
0a7de745 196 file_offset = (f_offset & SWAP_SLOT_MASK);
39236c6e
A
197 swapfile_index = (f_offset >> SWAP_DEVICE_SHIFT);
198
199 swf = (struct swapfile*) queue_first(&swf_global_queue);
200
0a7de745 201 while (queue_end(&swf_global_queue, (queue_entry_t)swf) == FALSE) {
39236c6e
A
202 if (swapfile_index == swf->swp_index) {
203 break;
204 }
205
206 swf = (struct swapfile*) queue_next(&swf->swp_queue);
207 }
208
209 if (queue_end(&swf_global_queue, (queue_entry_t) swf)) {
210 swf = NULL;
211 }
212
213 return swf;
214}
215
fe8ab488 216#if ENCRYPTED_SWAP
39236c6e 217
d9a64523 218#include <libkern/crypto/aesxts.h>
39236c6e 219
d9a64523 220extern int cc_rand_generate(void *, size_t); /* from libkern/cyrpto/rand.h> */
39236c6e 221
0a7de745
A
222boolean_t swap_crypt_initialized;
223void swap_crypt_initialize(void);
3e170ce0 224
d9a64523
A
225symmetric_xts xts_modectx;
226uint32_t swap_crypt_key1[8]; /* big enough for a 256 bit random key */
227uint32_t swap_crypt_key2[8]; /* big enough for a 256 bit random key */
3e170ce0 228
d9a64523 229#if DEVELOPMENT || DEBUG
0a7de745 230boolean_t swap_crypt_xts_tested = FALSE;
d9a64523
A
231unsigned char swap_crypt_test_page_ref[4096] __attribute__((aligned(4096)));
232unsigned char swap_crypt_test_page_encrypt[4096] __attribute__((aligned(4096)));
233unsigned char swap_crypt_test_page_decrypt[4096] __attribute__((aligned(4096)));
234#endif /* DEVELOPMENT || DEBUG */
3e170ce0 235
0a7de745
A
236unsigned long vm_page_encrypt_counter;
237unsigned long vm_page_decrypt_counter;
3e170ce0
A
238
239
fe8ab488 240void
d9a64523 241swap_crypt_initialize(void)
fe8ab488 242{
0a7de745 243 uint8_t *enckey1, *enckey2;
d9a64523
A
244 int keylen1, keylen2;
245 int error;
39037602 246
d9a64523
A
247 assert(swap_crypt_initialized == FALSE);
248
249 keylen1 = sizeof(swap_crypt_key1);
250 enckey1 = (uint8_t *)&swap_crypt_key1;
251 keylen2 = sizeof(swap_crypt_key2);
252 enckey2 = (uint8_t *)&swap_crypt_key2;
253
254 error = cc_rand_generate((void *)enckey1, keylen1);
255 assert(!error);
256
257 error = cc_rand_generate((void *)enckey2, keylen2);
258 assert(!error);
259
260 error = xts_start(0, NULL, enckey1, keylen1, enckey2, keylen2, 0, 0, &xts_modectx);
261 assert(!error);
262
263 swap_crypt_initialized = TRUE;
264
265#if DEVELOPMENT || DEBUG
0a7de745
A
266 uint8_t *encptr;
267 uint8_t *decptr;
268 uint8_t *refptr;
d9a64523
A
269 uint8_t *iv;
270 uint64_t ivnum[2];
271 int size = 0;
272 int i = 0;
273 int rc = 0;
274
275 assert(swap_crypt_xts_tested == FALSE);
39236c6e 276
5ba3f43e
A
277 /*
278 * Validate the encryption algorithms.
d9a64523
A
279 *
280 * First initialize the test data.
5ba3f43e 281 */
d9a64523 282 for (i = 0; i < 4096; i++) {
0a7de745 283 swap_crypt_test_page_ref[i] = (char) i;
d9a64523
A
284 }
285 ivnum[0] = (uint64_t)0xaa;
286 ivnum[1] = 0;
287 iv = (uint8_t *)ivnum;
0a7de745 288
d9a64523
A
289 refptr = (uint8_t *)swap_crypt_test_page_ref;
290 encptr = (uint8_t *)swap_crypt_test_page_encrypt;
291 decptr = (uint8_t *)swap_crypt_test_page_decrypt;
292 size = 4096;
293
294 /* encrypt */
295 rc = xts_encrypt(refptr, size, encptr, iv, &xts_modectx);
296 assert(!rc);
297
298 /* compare result with original - should NOT match */
0a7de745
A
299 for (i = 0; i < 4096; i++) {
300 if (swap_crypt_test_page_encrypt[i] !=
d9a64523 301 swap_crypt_test_page_ref[i]) {
0a7de745 302 break;
5ba3f43e 303 }
d9a64523
A
304 }
305 assert(i != 4096);
39037602 306
d9a64523
A
307 /* decrypt */
308 rc = xts_decrypt(encptr, size, decptr, iv, &xts_modectx);
309 assert(!rc);
39037602 310
d9a64523 311 /* compare result with original */
0a7de745
A
312 for (i = 0; i < 4096; i++) {
313 if (swap_crypt_test_page_decrypt[i] !=
d9a64523 314 swap_crypt_test_page_ref[i]) {
0a7de745 315 panic("encryption test failed");
d9a64523
A
316 }
317 }
318 /* encrypt in place */
319 rc = xts_encrypt(decptr, size, decptr, iv, &xts_modectx);
320 assert(!rc);
321
322 /* decrypt in place */
323 rc = xts_decrypt(decptr, size, decptr, iv, &xts_modectx);
324 assert(!rc);
325
0a7de745
A
326 for (i = 0; i < 4096; i++) {
327 if (swap_crypt_test_page_decrypt[i] !=
d9a64523 328 swap_crypt_test_page_ref[i]) {
0a7de745 329 panic("in place encryption test failed");
d9a64523 330 }
39037602 331 }
d9a64523
A
332 swap_crypt_xts_tested = TRUE;
333#endif /* DEVELOPMENT || DEBUG */
39236c6e
A
334}
335
fe8ab488 336
39236c6e
A
337void
338vm_swap_encrypt(c_segment_t c_seg)
339{
0a7de745 340 uint8_t *ptr;
d9a64523
A
341 uint8_t *iv;
342 uint64_t ivnum[2];
343 int size = 0;
344 int rc = 0;
345
0a7de745 346 if (swap_crypt_initialized == FALSE) {
d9a64523 347 swap_crypt_initialize();
0a7de745 348 }
39236c6e 349
39037602
A
350#if DEVELOPMENT || DEBUG
351 C_SEG_MAKE_WRITEABLE(c_seg);
352#endif
d9a64523 353 ptr = (uint8_t *)c_seg->c_store.c_buffer;
39236c6e
A
354 size = round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset));
355
d9a64523
A
356 ivnum[0] = (uint64_t)c_seg;
357 ivnum[1] = 0;
358 iv = (uint8_t *)ivnum;
359
360 rc = xts_encrypt(ptr, size, ptr, iv, &xts_modectx);
361 assert(!rc);
39236c6e 362
0a7de745 363 vm_page_encrypt_counter += (size / PAGE_SIZE_64);
39037602
A
364
365#if DEVELOPMENT || DEBUG
366 C_SEG_WRITE_PROTECT(c_seg);
367#endif
39236c6e
A
368}
369
370void
371vm_swap_decrypt(c_segment_t c_seg)
372{
0a7de745 373 uint8_t *ptr;
d9a64523
A
374 uint8_t *iv;
375 uint64_t ivnum[2];
376 int size = 0;
377 int rc = 0;
39236c6e 378
d9a64523 379 assert(swap_crypt_initialized);
39236c6e 380
39037602
A
381#if DEVELOPMENT || DEBUG
382 C_SEG_MAKE_WRITEABLE(c_seg);
383#endif
d9a64523 384 ptr = (uint8_t *)c_seg->c_store.c_buffer;
39236c6e
A
385 size = round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset));
386
d9a64523
A
387 ivnum[0] = (uint64_t)c_seg;
388 ivnum[1] = 0;
389 iv = (uint8_t *)ivnum;
390
391 rc = xts_decrypt(ptr, size, ptr, iv, &xts_modectx);
392 assert(!rc);
39236c6e 393
0a7de745 394 vm_page_decrypt_counter += (size / PAGE_SIZE_64);
39037602
A
395
396#if DEVELOPMENT || DEBUG
397 C_SEG_WRITE_PROTECT(c_seg);
398#endif
39236c6e 399}
fe8ab488 400#endif /* ENCRYPTED_SWAP */
39236c6e
A
401
402
5ba3f43e
A
403void
404vm_compressor_swap_init()
405{
0a7de745 406 thread_t thread = NULL;
5ba3f43e 407
5ba3f43e
A
408 queue_init(&swf_global_queue);
409
5ba3f43e 410 if (kernel_thread_start_priority((thread_continue_t)vm_swapout_thread, NULL,
0a7de745 411 BASEPRI_VM, &thread) != KERN_SUCCESS) {
5ba3f43e
A
412 panic("vm_swapout_thread: create failed");
413 }
d9a64523 414 thread_set_thread_name(thread, "VM_swapout");
5ba3f43e
A
415 vm_swapout_thread_id = thread->thread_id;
416
417 thread_deallocate(thread);
418
419 if (kernel_thread_start_priority((thread_continue_t)vm_swapfile_create_thread, NULL,
0a7de745 420 BASEPRI_VM, &thread) != KERN_SUCCESS) {
5ba3f43e
A
421 panic("vm_swapfile_create_thread: create failed");
422 }
423
d9a64523 424 thread_set_thread_name(thread, "VM_swapfile_create");
5ba3f43e
A
425 thread_deallocate(thread);
426
427 if (kernel_thread_start_priority((thread_continue_t)vm_swapfile_gc_thread, NULL,
0a7de745 428 BASEPRI_VM, &thread) != KERN_SUCCESS) {
5ba3f43e
A
429 panic("vm_swapfile_gc_thread: create failed");
430 }
d9a64523 431 thread_set_thread_name(thread, "VM_swapfile_gc");
cb323159
A
432
433 /*
434 * Swapfile garbage collection will need to allocate memory
435 * to complete its swap reclaim and in-memory compaction.
436 * So allow it to dip into the reserved VM page pool.
437 */
438 thread_lock(thread);
439 thread->options |= TH_OPT_VMPRIV;
440 thread_unlock(thread);
441
5ba3f43e
A
442 thread_deallocate(thread);
443
444 proc_set_thread_policy_with_tid(kernel_task, thread->thread_id,
0a7de745 445 TASK_POLICY_INTERNAL, TASK_POLICY_IO, THROTTLE_LEVEL_COMPRESSOR_TIER2);
5ba3f43e 446 proc_set_thread_policy_with_tid(kernel_task, thread->thread_id,
0a7de745 447 TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
5ba3f43e 448
c3c9b80d 449#if !XNU_TARGET_OS_OSX
5ba3f43e 450 /*
0a7de745
A
451 * dummy value until the swap file gets created
452 * when we drive the first c_segment_t to the
5ba3f43e
A
453 * swapout queue... at that time we will
454 * know the true size we have to work with
455 */
456 c_overage_swapped_limit = 16;
c3c9b80d 457#endif /* !XNU_TARGET_OS_OSX */
d9a64523
A
458
459 vm_num_swap_files_config = VM_MAX_SWAP_FILE_NUM;
c3c9b80d
A
460#if DEVELOPMENT || DEBUG
461 typeof(vm_num_swap_files_config) parsed_vm_max_num_swap_files = 0;
462 if (PE_parse_boot_argn("vm_max_num_swap_files", &parsed_vm_max_num_swap_files, sizeof(parsed_vm_max_num_swap_files))) {
463 if (parsed_vm_max_num_swap_files > 0) {
464 vm_num_swap_files_config = parsed_vm_max_num_swap_files;
465 } else {
466 printf("WARNING: Ignoring vm_max_num_swap_files=%d boot-arg. Value must be > 0\n", parsed_vm_max_num_swap_files);
467 }
468 }
469#endif
470 printf("Maximum number of VM swap files: %d\n", vm_num_swap_files_config);
d9a64523 471
5ba3f43e
A
472 printf("VM Swap Subsystem is ON\n");
473}
474
475
476#if RECORD_THE_COMPRESSED_DATA
477
478void
479c_compressed_record_init()
480{
481 if (c_compressed_record_init_done == FALSE) {
482 vm_swapfile_open("/tmp/compressed_data", &c_compressed_record_vp);
483 c_compressed_record_init_done = TRUE;
484 }
485}
486
487void
488c_compressed_record_write(char *buf, int size)
489{
490 if (c_compressed_record_write_error == 0) {
491 c_compressed_record_write_error = vm_record_file_write(c_compressed_record_vp, c_compressed_record_file_offset, buf, size);
492 c_compressed_record_file_offset += size;
493 }
494}
495#endif
496
497
0a7de745 498int compaction_swapper_inited = 0;
5ba3f43e
A
499
500void
501vm_compaction_swapper_do_init(void)
502{
0a7de745
A
503 struct vnode *vp;
504 char *pathname;
505 int namelen;
5ba3f43e 506
0a7de745 507 if (compaction_swapper_inited) {
5ba3f43e 508 return;
0a7de745 509 }
5ba3f43e
A
510
511 if (vm_compressor_mode != VM_PAGER_COMPRESSOR_WITH_SWAP) {
512 compaction_swapper_inited = 1;
513 return;
514 }
515 lck_mtx_lock(&vm_swap_data_lock);
516
0a7de745 517 if (!compaction_swapper_inited) {
5ba3f43e 518 namelen = (int)strlen(swapfilename) + SWAPFILENAME_INDEX_LEN + 1;
f427ee49 519 pathname = kheap_alloc(KHEAP_TEMP, namelen, Z_WAITOK | Z_ZERO);
5ba3f43e
A
520 snprintf(pathname, namelen, "%s%d", swapfilename, 0);
521
522 vm_swapfile_open(pathname, &vp);
523
524 if (vp) {
5ba3f43e 525 if (vnode_pager_isSSD(vp) == FALSE) {
0a7de745 526 /*
d9a64523 527 * swap files live on an HDD, so let's make sure to start swapping
0a7de745 528 * much earlier since we're not worried about SSD write-wear and
d9a64523
A
529 * we have so little write bandwidth to work with
530 * these values were derived expermentially by running the performance
0a7de745 531 * teams stock test for evaluating HDD performance against various
d9a64523
A
532 * combinations and looking and comparing overall results.
533 * Note that the > relationship between these 4 values must be maintained
534 */
0a7de745
A
535 if (vm_compressor_minorcompact_threshold_divisor_overridden == 0) {
536 vm_compressor_minorcompact_threshold_divisor = 15;
537 }
538 if (vm_compressor_majorcompact_threshold_divisor_overridden == 0) {
539 vm_compressor_majorcompact_threshold_divisor = 18;
540 }
541 if (vm_compressor_unthrottle_threshold_divisor_overridden == 0) {
542 vm_compressor_unthrottle_threshold_divisor = 24;
543 }
544 if (vm_compressor_catchup_threshold_divisor_overridden == 0) {
545 vm_compressor_catchup_threshold_divisor = 30;
546 }
5ba3f43e 547 }
c3c9b80d 548#if XNU_TARGET_OS_OSX
5ba3f43e
A
549 vnode_setswapmount(vp);
550 vm_swappin_avail = vnode_getswappin_avail(vp);
551
0a7de745 552 if (vm_swappin_avail) {
5ba3f43e 553 vm_swappin_enabled = TRUE;
0a7de745 554 }
c3c9b80d 555#endif /* XNU_TARGET_OS_OSX */
5ba3f43e
A
556 vm_swapfile_close((uint64_t)pathname, vp);
557 }
f427ee49 558 kheap_free(KHEAP_TEMP, pathname, namelen);
5ba3f43e
A
559
560 compaction_swapper_inited = 1;
561 }
562 lck_mtx_unlock(&vm_swap_data_lock);
563}
564
565
39236c6e 566void
d9a64523 567vm_swap_consider_defragmenting(int flags)
39236c6e 568{
d9a64523
A
569 boolean_t force_defrag = (flags & VM_SWAP_FLAGS_FORCE_DEFRAG);
570 boolean_t force_reclaim = (flags & VM_SWAP_FLAGS_FORCE_RECLAIM);
571
fe8ab488 572 if (compressor_store_stop_compaction == FALSE && !VM_SWAP_BUSY() &&
d9a64523 573 (force_defrag || force_reclaim || VM_SWAP_SHOULD_DEFRAGMENT() || VM_SWAP_SHOULD_RECLAIM())) {
d9a64523 574 if (!vm_swapfile_gc_thread_running || force_defrag || force_reclaim) {
39236c6e
A
575 lck_mtx_lock(&vm_swap_data_lock);
576
d9a64523
A
577 if (force_defrag) {
578 vm_swap_force_defrag = TRUE;
579 }
580
581 if (force_reclaim) {
582 vm_swap_force_reclaim = TRUE;
583 }
584
0a7de745 585 if (!vm_swapfile_gc_thread_running) {
fe8ab488 586 thread_wakeup((event_t) &vm_swapfile_gc_needed);
0a7de745 587 }
39236c6e
A
588
589 lck_mtx_unlock(&vm_swap_data_lock);
590 }
591 }
592}
593
594
595int vm_swap_defragment_yielded = 0;
596int vm_swap_defragment_swapin = 0;
597int vm_swap_defragment_free = 0;
598int vm_swap_defragment_busy = 0;
599
f427ee49
A
600#if CONFIG_FREEZE
601extern uint32_t c_segment_pages_compressed_incore;
602extern uint32_t c_segment_pages_compressed_nearing_limit;
603extern uint32_t c_segment_count;
604extern uint32_t c_segments_nearing_limit;
605
606boolean_t memorystatus_kill_on_VM_compressor_space_shortage(boolean_t);
607
608extern bool freezer_incore_cseg_acct;
609#endif /* CONFIG_FREEZE */
39236c6e
A
610
611static void
612vm_swap_defragment()
613{
0a7de745 614 c_segment_t c_seg;
39236c6e
A
615
616 /*
617 * have to grab the master lock w/o holding
618 * any locks in spin mode
619 */
620 PAGE_REPLACEMENT_DISALLOWED(TRUE);
621
622 lck_mtx_lock_spin_always(c_list_lock);
0a7de745 623
39236c6e 624 while (!queue_empty(&c_swappedout_sparse_list_head)) {
39236c6e
A
625 if (compressor_store_stop_compaction == TRUE || VM_SWAP_BUSY()) {
626 vm_swap_defragment_yielded++;
627 break;
628 }
629 c_seg = (c_segment_t)queue_first(&c_swappedout_sparse_list_head);
630
631 lck_mtx_lock_spin_always(&c_seg->c_lock);
632
3e170ce0 633 assert(c_seg->c_state == C_ON_SWAPPEDOUTSPARSE_Q);
39236c6e
A
634
635 if (c_seg->c_busy) {
636 lck_mtx_unlock_always(c_list_lock);
637
638 PAGE_REPLACEMENT_DISALLOWED(FALSE);
639 /*
640 * c_seg_wait_on_busy consumes c_seg->c_lock
641 */
642 c_seg_wait_on_busy(c_seg);
643
644 PAGE_REPLACEMENT_DISALLOWED(TRUE);
645
646 lck_mtx_lock_spin_always(c_list_lock);
647
648 vm_swap_defragment_busy++;
649 continue;
650 }
651 if (c_seg->c_bytes_used == 0) {
652 /*
653 * c_seg_free_locked consumes the c_list_lock
654 * and c_seg->c_lock
655 */
3e170ce0 656 C_SEG_BUSY(c_seg);
39236c6e
A
657 c_seg_free_locked(c_seg);
658
659 vm_swap_defragment_free++;
660 } else {
661 lck_mtx_unlock_always(c_list_lock);
662
f427ee49
A
663#if CONFIG_FREEZE
664 if (freezer_incore_cseg_acct) {
665 if ((c_seg->c_slots_used + c_segment_pages_compressed_incore) >= c_segment_pages_compressed_nearing_limit) {
666 memorystatus_kill_on_VM_compressor_space_shortage(TRUE /* async */);
667 }
668
669 uint32_t incore_seg_count = c_segment_count - c_swappedout_count - c_swappedout_sparse_count;
670 if ((incore_seg_count + 1) >= c_segments_nearing_limit) {
671 memorystatus_kill_on_VM_compressor_space_shortage(TRUE /* async */);
672 }
673 }
674#endif /* CONFIG_FREEZE */
0a7de745 675 if (c_seg_swapin(c_seg, TRUE, FALSE) == 0) {
39037602 676 lck_mtx_unlock_always(&c_seg->c_lock);
0a7de745 677 }
39236c6e
A
678
679 vm_swap_defragment_swapin++;
680 }
681 PAGE_REPLACEMENT_DISALLOWED(FALSE);
0a7de745 682
39236c6e
A
683 vm_pageout_io_throttle();
684
685 /*
686 * because write waiters have privilege over readers,
0a7de745 687 * dropping and immediately retaking the master lock will
39236c6e
A
688 * still allow any thread waiting to acquire the
689 * master lock exclusively an opportunity to take it
690 */
691 PAGE_REPLACEMENT_DISALLOWED(TRUE);
692
693 lck_mtx_lock_spin_always(c_list_lock);
694 }
695 lck_mtx_unlock_always(c_list_lock);
696
697 PAGE_REPLACEMENT_DISALLOWED(FALSE);
698}
699
700
701
702static void
fe8ab488 703vm_swapfile_create_thread(void)
39236c6e 704{
0a7de745
A
705 clock_sec_t sec;
706 clock_nsec_t nsec;
39236c6e 707
3e170ce0
A
708 current_thread()->options |= TH_OPT_VMPRIV;
709
fe8ab488
A
710 vm_swapfile_create_thread_awakened++;
711 vm_swapfile_create_thread_running = 1;
39236c6e 712
fe8ab488 713 while (TRUE) {
39236c6e
A
714 /*
715 * walk through the list of swap files
716 * and do the delayed frees/trims for
717 * any swap file whose count of delayed
718 * frees is above the batch limit
719 */
720 vm_swap_handle_delayed_trims(FALSE);
721
fe8ab488 722 lck_mtx_lock(&vm_swap_data_lock);
39236c6e 723
0a7de745 724 if (hibernate_in_progress_with_pinned_swap == TRUE) {
39037602 725 break;
0a7de745 726 }
39037602 727
cb323159
A
728 if (compressor_store_stop_compaction == TRUE) {
729 break;
730 }
731
fe8ab488 732 clock_get_system_nanotime(&sec, &nsec);
39236c6e 733
0a7de745 734 if (VM_SWAP_SHOULD_CREATE(sec) == 0) {
fe8ab488 735 break;
0a7de745 736 }
39236c6e 737
fe8ab488 738 lck_mtx_unlock(&vm_swap_data_lock);
39236c6e 739
fe8ab488
A
740 if (vm_swap_create_file() == FALSE) {
741 vm_swapfile_last_failed_to_create_ts = sec;
742 HIBLOG("vm_swap_create_file failed @ %lu secs\n", (unsigned long)sec);
0a7de745 743 } else {
fe8ab488 744 vm_swapfile_last_successful_create_ts = sec;
0a7de745 745 }
fe8ab488
A
746 }
747 vm_swapfile_create_thread_running = 0;
39236c6e 748
0a7de745 749 if (hibernate_in_progress_with_pinned_swap == TRUE) {
39037602 750 thread_wakeup((event_t)&hibernate_in_progress_with_pinned_swap);
0a7de745 751 }
39037602 752
cb323159
A
753 if (compressor_store_stop_compaction == TRUE) {
754 thread_wakeup((event_t)&compressor_store_stop_compaction);
755 }
756
fe8ab488 757 assert_wait((event_t)&vm_swapfile_create_needed, THREAD_UNINT);
39236c6e 758
fe8ab488 759 lck_mtx_unlock(&vm_swap_data_lock);
39236c6e 760
fe8ab488 761 thread_block((thread_continue_t)vm_swapfile_create_thread);
0a7de745 762
fe8ab488
A
763 /* NOTREACHED */
764}
39236c6e 765
39236c6e 766
39037602
A
767#if HIBERNATION
768
769kern_return_t
770hibernate_pin_swap(boolean_t start)
771{
772 vm_compaction_swapper_do_init();
773
774 if (start == FALSE) {
39037602
A
775 lck_mtx_lock(&vm_swap_data_lock);
776 hibernate_in_progress_with_pinned_swap = FALSE;
777 lck_mtx_unlock(&vm_swap_data_lock);
778
0a7de745
A
779 return KERN_SUCCESS;
780 }
781 if (vm_swappin_enabled == FALSE) {
782 return KERN_SUCCESS;
39037602 783 }
39037602
A
784
785 lck_mtx_lock(&vm_swap_data_lock);
786
787 hibernate_in_progress_with_pinned_swap = TRUE;
39037602 788
0a7de745 789 while (vm_swapfile_create_thread_running || vm_swapfile_gc_thread_running) {
39037602
A
790 assert_wait((event_t)&hibernate_in_progress_with_pinned_swap, THREAD_UNINT);
791
792 lck_mtx_unlock(&vm_swap_data_lock);
793
794 thread_block(THREAD_CONTINUE_NULL);
795
796 lck_mtx_lock(&vm_swap_data_lock);
797 }
798 if (vm_num_swap_files > vm_num_pinned_swap_files) {
799 hibernate_in_progress_with_pinned_swap = FALSE;
800 lck_mtx_unlock(&vm_swap_data_lock);
801
802 HIBLOG("hibernate_pin_swap failed - vm_num_swap_files = %d, vm_num_pinned_swap_files = %d\n",
0a7de745
A
803 vm_num_swap_files, vm_num_pinned_swap_files);
804 return KERN_FAILURE;
39037602
A
805 }
806 lck_mtx_unlock(&vm_swap_data_lock);
807
808 while (VM_SWAP_SHOULD_PIN(MAX_SWAP_FILE_SIZE)) {
0a7de745 809 if (vm_swap_create_file() == FALSE) {
39037602 810 break;
0a7de745 811 }
39037602 812 }
0a7de745 813 return KERN_SUCCESS;
39037602
A
814}
815#endif
816
fe8ab488
A
817static void
818vm_swapfile_gc_thread(void)
819{
0a7de745
A
820 boolean_t need_defragment;
821 boolean_t need_reclaim;
fe8ab488
A
822
823 vm_swapfile_gc_thread_awakened++;
824 vm_swapfile_gc_thread_running = 1;
825
826 while (TRUE) {
fe8ab488 827 lck_mtx_lock(&vm_swap_data_lock);
0a7de745
A
828
829 if (hibernate_in_progress_with_pinned_swap == TRUE) {
39037602 830 break;
0a7de745 831 }
39037602 832
0a7de745 833 if (VM_SWAP_BUSY() || compressor_store_stop_compaction == TRUE) {
fe8ab488 834 break;
0a7de745 835 }
fe8ab488
A
836
837 need_defragment = FALSE;
838 need_reclaim = FALSE;
839
0a7de745 840 if (VM_SWAP_SHOULD_DEFRAGMENT()) {
fe8ab488 841 need_defragment = TRUE;
0a7de745 842 }
fe8ab488
A
843
844 if (VM_SWAP_SHOULD_RECLAIM()) {
845 need_defragment = TRUE;
846 need_reclaim = TRUE;
847 }
0a7de745 848 if (need_defragment == FALSE && need_reclaim == FALSE) {
fe8ab488 849 break;
0a7de745 850 }
fe8ab488 851
d9a64523
A
852 vm_swap_force_defrag = FALSE;
853 vm_swap_force_reclaim = FALSE;
854
39236c6e 855 lck_mtx_unlock(&vm_swap_data_lock);
39236c6e 856
0a7de745 857 if (need_defragment == TRUE) {
fe8ab488 858 vm_swap_defragment();
0a7de745
A
859 }
860 if (need_reclaim == TRUE) {
fe8ab488 861 vm_swap_reclaim();
0a7de745 862 }
fe8ab488
A
863 }
864 vm_swapfile_gc_thread_running = 0;
39236c6e 865
0a7de745 866 if (hibernate_in_progress_with_pinned_swap == TRUE) {
39037602 867 thread_wakeup((event_t)&hibernate_in_progress_with_pinned_swap);
0a7de745 868 }
39037602 869
cb323159
A
870 if (compressor_store_stop_compaction == TRUE) {
871 thread_wakeup((event_t)&compressor_store_stop_compaction);
872 }
873
fe8ab488 874 assert_wait((event_t)&vm_swapfile_gc_needed, THREAD_UNINT);
39236c6e
A
875
876 lck_mtx_unlock(&vm_swap_data_lock);
877
fe8ab488 878 thread_block((thread_continue_t)vm_swapfile_gc_thread);
0a7de745 879
39236c6e
A
880 /* NOTREACHED */
881}
882
883
884
d9a64523
A
885#define VM_SWAPOUT_LIMIT_T2P 4
886#define VM_SWAPOUT_LIMIT_T1P 4
887#define VM_SWAPOUT_LIMIT_T0P 6
888#define VM_SWAPOUT_LIMIT_T0 8
889#define VM_SWAPOUT_LIMIT_MAX 8
890
891#define VM_SWAPOUT_START 0
892#define VM_SWAPOUT_T2_PASSIVE 1
893#define VM_SWAPOUT_T1_PASSIVE 2
894#define VM_SWAPOUT_T0_PASSIVE 3
895#define VM_SWAPOUT_T0 4
896
897int vm_swapout_state = VM_SWAPOUT_START;
898int vm_swapout_limit = 1;
899
900int vm_swapper_entered_T0 = 0;
901int vm_swapper_entered_T0P = 0;
902int vm_swapper_entered_T1P = 0;
903int vm_swapper_entered_T2P = 0;
904
39236c6e
A
905
906static void
907vm_swapout_thread_throttle_adjust(void)
908{
0a7de745 909 switch (vm_swapout_state) {
d9a64523 910 case VM_SWAPOUT_START:
0a7de745 911
d9a64523
A
912 vm_swapper_throttle = THROTTLE_LEVEL_COMPRESSOR_TIER2;
913 vm_swapper_entered_T2P++;
914
915 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 916 TASK_POLICY_INTERNAL, TASK_POLICY_IO, vm_swapper_throttle);
d9a64523 917 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 918 TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
d9a64523
A
919 vm_swapout_limit = VM_SWAPOUT_LIMIT_T2P;
920 vm_swapout_state = VM_SWAPOUT_T2_PASSIVE;
921
922 break;
39236c6e 923
d9a64523 924 case VM_SWAPOUT_T2_PASSIVE:
39236c6e 925
d9a64523
A
926 if (SWAPPER_NEEDS_TO_UNTHROTTLE()) {
927 vm_swapper_throttle = THROTTLE_LEVEL_COMPRESSOR_TIER0;
928 vm_swapper_entered_T0P++;
39236c6e 929
d9a64523 930 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 931 TASK_POLICY_INTERNAL, TASK_POLICY_IO, vm_swapper_throttle);
d9a64523 932 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 933 TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
d9a64523
A
934 vm_swapout_limit = VM_SWAPOUT_LIMIT_T0P;
935 vm_swapout_state = VM_SWAPOUT_T0_PASSIVE;
39236c6e 936
39236c6e
A
937 break;
938 }
d9a64523
A
939 if (swapout_target_age || hibernate_flushing == TRUE) {
940 vm_swapper_throttle = THROTTLE_LEVEL_COMPRESSOR_TIER1;
941 vm_swapper_entered_T1P++;
942
943 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 944 TASK_POLICY_INTERNAL, TASK_POLICY_IO, vm_swapper_throttle);
d9a64523 945 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 946 TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
d9a64523
A
947 vm_swapout_limit = VM_SWAPOUT_LIMIT_T1P;
948 vm_swapout_state = VM_SWAPOUT_T1_PASSIVE;
949 }
39236c6e
A
950 break;
951
d9a64523
A
952 case VM_SWAPOUT_T1_PASSIVE:
953
954 if (SWAPPER_NEEDS_TO_UNTHROTTLE()) {
955 vm_swapper_throttle = THROTTLE_LEVEL_COMPRESSOR_TIER0;
956 vm_swapper_entered_T0P++;
957
958 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 959 TASK_POLICY_INTERNAL, TASK_POLICY_IO, vm_swapper_throttle);
d9a64523 960 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 961 TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
d9a64523
A
962 vm_swapout_limit = VM_SWAPOUT_LIMIT_T0P;
963 vm_swapout_state = VM_SWAPOUT_T0_PASSIVE;
39236c6e 964
39236c6e
A
965 break;
966 }
d9a64523 967 if (swapout_target_age == 0 && hibernate_flushing == FALSE) {
0a7de745 968 vm_swapper_throttle = THROTTLE_LEVEL_COMPRESSOR_TIER2;
d9a64523
A
969 vm_swapper_entered_T2P++;
970
971 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 972 TASK_POLICY_INTERNAL, TASK_POLICY_IO, vm_swapper_throttle);
d9a64523 973 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 974 TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
d9a64523
A
975 vm_swapout_limit = VM_SWAPOUT_LIMIT_T2P;
976 vm_swapout_state = VM_SWAPOUT_T2_PASSIVE;
39236c6e 977 }
0a7de745 978 break;
39236c6e 979
d9a64523
A
980 case VM_SWAPOUT_T0_PASSIVE:
981
0a7de745 982 if (SWAPPER_NEEDS_TO_RETHROTTLE()) {
d9a64523
A
983 vm_swapper_throttle = THROTTLE_LEVEL_COMPRESSOR_TIER2;
984 vm_swapper_entered_T2P++;
985
986 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 987 TASK_POLICY_INTERNAL, TASK_POLICY_IO, vm_swapper_throttle);
d9a64523 988 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 989 TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
d9a64523
A
990 vm_swapout_limit = VM_SWAPOUT_LIMIT_T2P;
991 vm_swapout_state = VM_SWAPOUT_T2_PASSIVE;
39236c6e 992
39236c6e
A
993 break;
994 }
d9a64523 995 if (SWAPPER_NEEDS_TO_CATCHUP()) {
0a7de745 996 vm_swapper_entered_T0++;
d9a64523
A
997
998 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 999 TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_DISABLE);
d9a64523
A
1000 vm_swapout_limit = VM_SWAPOUT_LIMIT_T0;
1001 vm_swapout_state = VM_SWAPOUT_T0;
1002 }
1003 break;
1004
1005 case VM_SWAPOUT_T0:
1006
1007 if (SWAPPER_HAS_CAUGHTUP()) {
0a7de745 1008 vm_swapper_entered_T0P++;
d9a64523
A
1009
1010 proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
0a7de745 1011 TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
d9a64523
A
1012 vm_swapout_limit = VM_SWAPOUT_LIMIT_T0P;
1013 vm_swapout_state = VM_SWAPOUT_T0_PASSIVE;
39236c6e
A
1014 }
1015 break;
1016 }
d9a64523
A
1017}
1018
1019int vm_swapout_found_empty = 0;
1020
1021struct swapout_io_completion vm_swapout_ctx[VM_SWAPOUT_LIMIT_MAX];
39236c6e 1022
d9a64523
A
1023int vm_swapout_soc_busy = 0;
1024int vm_swapout_soc_done = 0;
1025
1026
1027static struct swapout_io_completion *
1028vm_swapout_find_free_soc(void)
0a7de745
A
1029{
1030 int i;
d9a64523 1031
0a7de745
A
1032 for (i = 0; i < VM_SWAPOUT_LIMIT_MAX; i++) {
1033 if (vm_swapout_ctx[i].swp_io_busy == 0) {
1034 return &vm_swapout_ctx[i];
1035 }
39236c6e 1036 }
d9a64523
A
1037 assert(vm_swapout_soc_busy == VM_SWAPOUT_LIMIT_MAX);
1038
1039 return NULL;
39236c6e
A
1040}
1041
d9a64523
A
1042static struct swapout_io_completion *
1043vm_swapout_find_done_soc(void)
0a7de745
A
1044{
1045 int i;
d9a64523 1046
0a7de745
A
1047 if (vm_swapout_soc_done) {
1048 for (i = 0; i < VM_SWAPOUT_LIMIT_MAX; i++) {
1049 if (vm_swapout_ctx[i].swp_io_done) {
1050 return &vm_swapout_ctx[i];
1051 }
d9a64523
A
1052 }
1053 }
1054 return NULL;
1055}
1056
1057static void
1058vm_swapout_complete_soc(struct swapout_io_completion *soc)
1059{
0a7de745 1060 kern_return_t kr;
d9a64523 1061
0a7de745
A
1062 if (soc->swp_io_error) {
1063 kr = KERN_FAILURE;
1064 } else {
1065 kr = KERN_SUCCESS;
1066 }
d9a64523
A
1067
1068 lck_mtx_unlock_always(c_list_lock);
1069
f427ee49 1070 vm_swap_put_finish(soc->swp_swf, &soc->swp_f_offset, soc->swp_io_error, TRUE /*drop iocount*/);
d9a64523
A
1071 vm_swapout_finish(soc->swp_c_seg, soc->swp_f_offset, soc->swp_c_size, kr);
1072
1073 lck_mtx_lock_spin_always(c_list_lock);
1074
1075 soc->swp_io_done = 0;
1076 soc->swp_io_busy = 0;
1077
1078 vm_swapout_soc_busy--;
1079 vm_swapout_soc_done--;
1080}
39236c6e 1081
3e170ce0 1082
39236c6e
A
1083static void
1084vm_swapout_thread(void)
1085{
0a7de745
A
1086 uint32_t size = 0;
1087 c_segment_t c_seg = NULL;
1088 kern_return_t kr = KERN_SUCCESS;
d9a64523 1089 struct swapout_io_completion *soc;
39236c6e 1090
3e170ce0
A
1091 current_thread()->options |= TH_OPT_VMPRIV;
1092
39236c6e
A
1093 vm_swapout_thread_awakened++;
1094
1095 lck_mtx_lock_spin_always(c_list_lock);
f427ee49
A
1096
1097 vm_swapout_thread_running = TRUE;
d9a64523 1098again:
f427ee49 1099 while (!queue_empty(&c_swapout_list_head) && vm_swapout_soc_busy < vm_swapout_limit && !compressor_store_stop_compaction) {
39236c6e
A
1100 c_seg = (c_segment_t)queue_first(&c_swapout_list_head);
1101
1102 lck_mtx_lock_spin_always(&c_seg->c_lock);
1103
3e170ce0 1104 assert(c_seg->c_state == C_ON_SWAPOUT_Q);
39236c6e
A
1105
1106 if (c_seg->c_busy) {
39236c6e
A
1107 lck_mtx_unlock_always(c_list_lock);
1108
8a3053a0 1109 c_seg_wait_on_busy(c_seg);
39236c6e
A
1110
1111 lck_mtx_lock_spin_always(c_list_lock);
1112
1113 continue;
1114 }
39236c6e
A
1115 vm_swapout_thread_processed_segments++;
1116
8a3053a0 1117 size = round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset));
0a7de745 1118
8a3053a0 1119 if (size == 0) {
3e170ce0
A
1120 assert(c_seg->c_bytes_used == 0);
1121
0a7de745 1122 if (!c_seg->c_on_minorcompact_q) {
39037602 1123 c_seg_need_delayed_compaction(c_seg, TRUE);
0a7de745 1124 }
39037602 1125
3e170ce0
A
1126 c_seg_switch_state(c_seg, C_IS_EMPTY, FALSE);
1127 lck_mtx_unlock_always(&c_seg->c_lock);
1128 lck_mtx_unlock_always(c_list_lock);
1129
1130 vm_swapout_found_empty++;
1131 goto c_seg_is_empty;
8a3053a0 1132 }
fe8ab488 1133 C_SEG_BUSY(c_seg);
8a3053a0
A
1134 c_seg->c_busy_swapping = 1;
1135
d9a64523 1136 c_seg_switch_state(c_seg, C_ON_SWAPIO_Q, FALSE);
39236c6e 1137
d9a64523 1138 lck_mtx_unlock_always(c_list_lock);
39236c6e
A
1139 lck_mtx_unlock_always(&c_seg->c_lock);
1140
0a7de745 1141#if CHECKSUM_THE_SWAP
d9a64523 1142 c_seg->cseg_hash = hash_string((char *)c_seg->c_store.c_buffer, (int)size);
39236c6e
A
1143 c_seg->cseg_swap_size = size;
1144#endif /* CHECKSUM_THE_SWAP */
1145
fe8ab488 1146#if ENCRYPTED_SWAP
39236c6e 1147 vm_swap_encrypt(c_seg);
fe8ab488 1148#endif /* ENCRYPTED_SWAP */
39236c6e 1149
d9a64523
A
1150 soc = vm_swapout_find_free_soc();
1151 assert(soc);
39236c6e 1152
d9a64523
A
1153 soc->swp_upl_ctx.io_context = (void *)soc;
1154 soc->swp_upl_ctx.io_done = (void *)vm_swapout_iodone;
1155 soc->swp_upl_ctx.io_error = 0;
39236c6e 1156
d9a64523 1157 kr = vm_swap_put((vm_offset_t)c_seg->c_store.c_buffer, &soc->swp_f_offset, size, c_seg, soc);
39236c6e 1158
d9a64523 1159 if (kr != KERN_SUCCESS) {
0a7de745
A
1160 if (soc->swp_io_done) {
1161 lck_mtx_lock_spin_always(c_list_lock);
d9a64523 1162
0a7de745 1163 soc->swp_io_done = 0;
d9a64523
A
1164 vm_swapout_soc_done--;
1165
1166 lck_mtx_unlock_always(c_list_lock);
1167 }
0a7de745 1168 vm_swapout_finish(c_seg, soc->swp_f_offset, size, kr);
d9a64523 1169 } else {
0a7de745 1170 soc->swp_io_busy = 1;
d9a64523 1171 vm_swapout_soc_busy++;
39037602 1172 }
d9a64523
A
1173
1174c_seg_is_empty:
0a7de745
A
1175 if (c_swapout_count == 0) {
1176 vm_swap_consider_defragmenting(VM_SWAP_FLAGS_NONE);
1177 }
d9a64523 1178
39236c6e 1179 lck_mtx_lock_spin_always(c_list_lock);
39236c6e 1180
f427ee49 1181 while ((soc = vm_swapout_find_done_soc())) {
0a7de745
A
1182 vm_swapout_complete_soc(soc);
1183 }
cb323159
A
1184 lck_mtx_unlock_always(c_list_lock);
1185
1186 vm_swapout_thread_throttle_adjust();
cb323159
A
1187
1188 lck_mtx_lock_spin_always(c_list_lock);
d9a64523 1189 }
f427ee49 1190 while ((soc = vm_swapout_find_done_soc())) {
0a7de745 1191 vm_swapout_complete_soc(soc);
f427ee49
A
1192 }
1193 lck_mtx_unlock_always(c_list_lock);
1194
1195 vm_pageout_io_throttle();
1196
1197 lck_mtx_lock_spin_always(c_list_lock);
1198
1199 /*
1200 * Recheck if we have some c_segs to wakeup
1201 * post throttle. And, check to see if we
1202 * have any more swapouts needed.
1203 */
1204 if (vm_swapout_soc_done) {
d9a64523
A
1205 goto again;
1206 }
f427ee49 1207
d9a64523 1208 assert_wait((event_t)&c_swapout_list_head, THREAD_UNINT);
39236c6e 1209
f427ee49
A
1210 vm_swapout_thread_running = FALSE;
1211
d9a64523 1212 lck_mtx_unlock_always(c_list_lock);
39236c6e 1213
d9a64523 1214 thread_block((thread_continue_t)vm_swapout_thread);
0a7de745 1215
d9a64523
A
1216 /* NOTREACHED */
1217}
39236c6e 1218
39236c6e 1219
d9a64523
A
1220void
1221vm_swapout_iodone(void *io_context, int error)
1222{
0a7de745 1223 struct swapout_io_completion *soc;
39037602 1224
d9a64523 1225 soc = (struct swapout_io_completion *)io_context;
39236c6e 1226
d9a64523 1227 lck_mtx_lock_spin_always(c_list_lock);
39037602 1228
d9a64523
A
1229 soc->swp_io_done = 1;
1230 soc->swp_io_error = error;
1231 vm_swapout_soc_done++;
0a7de745 1232
f427ee49
A
1233 if (!vm_swapout_thread_running) {
1234 thread_wakeup((event_t)&c_swapout_list_head);
1235 }
0a7de745 1236
d9a64523
A
1237 lck_mtx_unlock_always(c_list_lock);
1238}
39236c6e 1239
39236c6e 1240
d9a64523 1241static void
0a7de745 1242vm_swapout_finish(c_segment_t c_seg, uint64_t f_offset, uint32_t size, kern_return_t kr)
d9a64523 1243{
d9a64523
A
1244 PAGE_REPLACEMENT_DISALLOWED(TRUE);
1245
1246 if (kr == KERN_SUCCESS) {
f427ee49
A
1247 kernel_memory_depopulate(compressor_map, (vm_offset_t)c_seg->c_store.c_buffer, size,
1248 KMA_COMPRESSOR, VM_KERN_MEMORY_COMPRESSOR);
d9a64523
A
1249 }
1250#if ENCRYPTED_SWAP
1251 else {
0a7de745 1252 vm_swap_decrypt(c_seg);
39236c6e 1253 }
d9a64523
A
1254#endif /* ENCRYPTED_SWAP */
1255 lck_mtx_lock_spin_always(c_list_lock);
1256 lck_mtx_lock_spin_always(&c_seg->c_lock);
39236c6e 1257
d9a64523 1258 if (kr == KERN_SUCCESS) {
0a7de745
A
1259 int new_state = C_ON_SWAPPEDOUT_Q;
1260 boolean_t insert_head = FALSE;
d9a64523
A
1261
1262 if (hibernate_flushing == TRUE) {
0a7de745
A
1263 if (c_seg->c_generation_id >= first_c_segment_to_warm_generation_id &&
1264 c_seg->c_generation_id <= last_c_segment_to_warm_generation_id) {
1265 insert_head = TRUE;
1266 }
1267 } else if (C_SEG_ONDISK_IS_SPARSE(c_seg)) {
1268 new_state = C_ON_SWAPPEDOUTSPARSE_Q;
1269 }
d9a64523
A
1270
1271 c_seg_switch_state(c_seg, new_state, insert_head);
1272
1273 c_seg->c_store.c_swap_handle = f_offset;
39236c6e 1274
c3c9b80d 1275 counter_add(&vm_statistics_swapouts, size >> PAGE_SHIFT);
0a7de745
A
1276
1277 if (c_seg->c_bytes_used) {
1278 OSAddAtomic64(-c_seg->c_bytes_used, &compressor_bytes_used);
1279 }
f427ee49
A
1280
1281#if CONFIG_FREEZE
1282 /*
1283 * Successful swapout. Decrement the in-core compressed pages count.
1284 */
1285 OSAddAtomic(-(c_seg->c_slots_used), &c_segment_pages_compressed_incore);
1286 assertf(c_segment_pages_compressed_incore >= 0, "-ve incore count %p 0x%x", c_seg, c_segment_pages_compressed_incore);
1287#endif /* CONFIG_FREEZE */
d9a64523 1288 } else {
0a7de745
A
1289 if (c_seg->c_overage_swap == TRUE) {
1290 c_seg->c_overage_swap = FALSE;
d9a64523
A
1291 c_overage_swapped_count--;
1292 }
f427ee49
A
1293
1294#if CONFIG_FREEZE
1295 if (c_seg->c_task_owner) {
1296 c_seg_update_task_owner(c_seg, NULL);
1297 }
1298#endif /* CONFIG_FREEZE */
1299
d9a64523
A
1300 c_seg_switch_state(c_seg, C_ON_AGE_Q, FALSE);
1301
0a7de745
A
1302 if (!c_seg->c_on_minorcompact_q && C_SEG_UNUSED_BYTES(c_seg) >= PAGE_SIZE) {
1303 c_seg_need_delayed_compaction(c_seg, TRUE);
1304 }
d9a64523
A
1305 }
1306 assert(c_seg->c_busy_swapping);
1307 assert(c_seg->c_busy);
1308
1309 c_seg->c_busy_swapping = 0;
39236c6e
A
1310 lck_mtx_unlock_always(c_list_lock);
1311
d9a64523
A
1312 C_SEG_WAKEUP_DONE(c_seg);
1313 lck_mtx_unlock_always(&c_seg->c_lock);
1314
1315 PAGE_REPLACEMENT_DISALLOWED(FALSE);
39236c6e
A
1316}
1317
d9a64523 1318
39236c6e
A
1319boolean_t
1320vm_swap_create_file()
1321{
0a7de745
A
1322 uint64_t size = 0;
1323 int namelen = 0;
1324 boolean_t swap_file_created = FALSE;
1325 boolean_t swap_file_reuse = FALSE;
1326 boolean_t swap_file_pin = FALSE;
39236c6e
A
1327 struct swapfile *swf = NULL;
1328
39037602
A
1329 /*
1330 * make sure we've got all the info we need
1331 * to potentially pin a swap file... we could
1332 * be swapping out due to hibernation w/o ever
1333 * having run vm_pageout_scan, which is normally
1334 * the trigger to do the init
1335 */
1336 vm_compaction_swapper_do_init();
1337
39236c6e 1338 /*
0a7de745
A
1339 * Any swapfile structure ready for re-use?
1340 */
1341
39236c6e
A
1342 lck_mtx_lock(&vm_swap_data_lock);
1343
1344 swf = (struct swapfile*) queue_first(&swf_global_queue);
1345
1346 while (queue_end(&swf_global_queue, (queue_entry_t)swf) == FALSE) {
1347 if (swf->swp_flags == SWAP_REUSE) {
1348 swap_file_reuse = TRUE;
1349 break;
0a7de745 1350 }
39236c6e
A
1351 swf = (struct swapfile*) queue_next(&swf->swp_queue);
1352 }
1353
1354 lck_mtx_unlock(&vm_swap_data_lock);
1355
1356 if (swap_file_reuse == FALSE) {
fe8ab488 1357 namelen = (int)strlen(swapfilename) + SWAPFILENAME_INDEX_LEN + 1;
0a7de745 1358
f427ee49 1359 swf = kalloc_flags(sizeof *swf, Z_WAITOK | Z_ZERO);
39236c6e
A
1360 swf->swp_index = vm_num_swap_files + 1;
1361 swf->swp_pathlen = namelen;
f427ee49
A
1362 swf->swp_path = kheap_alloc(KHEAP_DATA_BUFFERS, swf->swp_pathlen,
1363 Z_WAITOK | Z_ZERO);
39236c6e 1364
fe8ab488 1365 snprintf(swf->swp_path, namelen, "%s%d", swapfilename, vm_num_swap_files);
39236c6e
A
1366 }
1367
1368 vm_swapfile_open(swf->swp_path, &swf->swp_vp);
1369
1370 if (swf->swp_vp == NULL) {
1371 if (swap_file_reuse == FALSE) {
f427ee49 1372 kheap_free(KHEAP_DATA_BUFFERS, swf->swp_path, swf->swp_pathlen);
39236c6e
A
1373 kfree(swf, sizeof *swf);
1374 }
1375 return FALSE;
1376 }
fe8ab488
A
1377 vm_swapfile_can_be_created = TRUE;
1378
39236c6e
A
1379 size = MAX_SWAP_FILE_SIZE;
1380
1381 while (size >= MIN_SWAP_FILE_SIZE) {
3e170ce0
A
1382 swap_file_pin = VM_SWAP_SHOULD_PIN(size);
1383
1384 if (vm_swapfile_preallocate(swf->swp_vp, &size, &swap_file_pin) == 0) {
39236c6e
A
1385 int num_bytes_for_bitmap = 0;
1386
1387 swap_file_created = TRUE;
1388
1389 swf->swp_size = size;
1390 swf->swp_nsegs = (unsigned int) (size / COMPRESSED_SWAP_CHUNK_SIZE);
1391 swf->swp_nseginuse = 0;
1392 swf->swp_free_hint = 0;
1393
0a7de745 1394 num_bytes_for_bitmap = MAX((swf->swp_nsegs >> 3), 1);
39236c6e
A
1395 /*
1396 * Allocate a bitmap that describes the
1397 * number of segments held by this swapfile.
1398 */
f427ee49
A
1399 swf->swp_bitmap = kheap_alloc(KHEAP_DATA_BUFFERS,
1400 num_bytes_for_bitmap, Z_WAITOK | Z_ZERO);
39236c6e 1401
f427ee49
A
1402 swf->swp_csegs = kalloc_flags(swf->swp_nsegs * sizeof(c_segment_t),
1403 Z_WAITOK | Z_ZERO);
39236c6e
A
1404
1405 /*
1406 * passing a NULL trim_list into vnode_trim_list
1407 * will return ENOTSUP if trim isn't supported
1408 * and 0 if it is
1409 */
0a7de745 1410 if (vnode_trim_list(swf->swp_vp, NULL, FALSE) == 0) {
fe8ab488 1411 swp_trim_supported = TRUE;
0a7de745 1412 }
39236c6e
A
1413
1414 lck_mtx_lock(&vm_swap_data_lock);
1415
1416 swf->swp_flags = SWAP_READY;
1417
1418 if (swap_file_reuse == FALSE) {
1419 queue_enter(&swf_global_queue, swf, struct swapfile*, swp_queue);
1420 }
0a7de745 1421
39236c6e
A
1422 vm_num_swap_files++;
1423
1424 vm_swapfile_total_segs_alloced += swf->swp_nsegs;
1425
3e170ce0 1426 if (swap_file_pin == TRUE) {
39037602 1427 vm_num_pinned_swap_files++;
3e170ce0
A
1428 swf->swp_flags |= SWAP_PINNED;
1429 vm_swappin_avail -= swf->swp_size;
1430 }
1431
39236c6e
A
1432 lck_mtx_unlock(&vm_swap_data_lock);
1433
1434 thread_wakeup((event_t) &vm_num_swap_files);
c3c9b80d 1435#if !XNU_TARGET_OS_OSX
5ba3f43e 1436 if (vm_num_swap_files == 1) {
5ba3f43e
A
1437 c_overage_swapped_limit = (uint32_t)size / C_SEG_BUFSIZE;
1438
0a7de745 1439 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
5ba3f43e 1440 c_overage_swapped_limit /= 2;
0a7de745 1441 }
5ba3f43e 1442 }
c3c9b80d 1443#endif /* !XNU_TARGET_OS_OSX */
39236c6e
A
1444 break;
1445 } else {
39236c6e
A
1446 size = size / 2;
1447 }
1448 }
1449 if (swap_file_created == FALSE) {
39236c6e
A
1450 vm_swapfile_close((uint64_t)(swf->swp_path), swf->swp_vp);
1451
1452 swf->swp_vp = NULL;
1453
1454 if (swap_file_reuse == FALSE) {
f427ee49 1455 kheap_free(KHEAP_DATA_BUFFERS, swf->swp_path, swf->swp_pathlen);
39236c6e
A
1456 kfree(swf, sizeof *swf);
1457 }
1458 }
1459 return swap_file_created;
1460}
1461
f427ee49 1462extern void vnode_put(struct vnode* vp);
39236c6e 1463kern_return_t
39037602 1464vm_swap_get(c_segment_t c_seg, uint64_t f_offset, uint64_t size)
39236c6e
A
1465{
1466 struct swapfile *swf = NULL;
0a7de745
A
1467 uint64_t file_offset = 0;
1468 int retval = 0;
39236c6e 1469
39037602 1470 assert(c_seg->c_store.c_buffer);
39236c6e
A
1471
1472 lck_mtx_lock(&vm_swap_data_lock);
1473
1474 swf = vm_swapfile_for_handle(f_offset);
1475
0a7de745
A
1476 if (swf == NULL || (!(swf->swp_flags & SWAP_READY) && !(swf->swp_flags & SWAP_RECLAIM))) {
1477 vm_swap_get_failures++;
fe8ab488
A
1478 retval = 1;
1479 goto done;
39236c6e 1480 }
fe8ab488
A
1481 swf->swp_io_count++;
1482
1483 lck_mtx_unlock(&vm_swap_data_lock);
39236c6e 1484
39037602
A
1485#if DEVELOPMENT || DEBUG
1486 C_SEG_MAKE_WRITEABLE(c_seg);
1487#endif
fe8ab488 1488 file_offset = (f_offset & SWAP_SLOT_MASK);
f427ee49
A
1489
1490 if ((retval = vnode_getwithref(swf->swp_vp)) != 0) {
1491 printf("vm_swap_get: vnode_getwithref on swapfile failed with %d\n", retval);
1492 } else {
1493 retval = vm_swapfile_io(swf->swp_vp, file_offset, (uint64_t)c_seg->c_store.c_buffer, (int)(size / PAGE_SIZE_64), SWAP_READ, NULL);
1494 vnode_put(swf->swp_vp);
1495 }
39236c6e 1496
39037602
A
1497#if DEVELOPMENT || DEBUG
1498 C_SEG_WRITE_PROTECT(c_seg);
1499#endif
0a7de745 1500 if (retval == 0) {
c3c9b80d 1501 counter_add(&vm_statistics_swapins, size >> PAGE_SHIFT);
0a7de745 1502 } else {
fe8ab488 1503 vm_swap_get_failures++;
0a7de745 1504 }
fe8ab488 1505
39236c6e
A
1506 /*
1507 * Free this slot in the swap structure.
1508 */
1509 vm_swap_free(f_offset);
1510
1511 lck_mtx_lock(&vm_swap_data_lock);
1512 swf->swp_io_count--;
1513
1514 if ((swf->swp_flags & SWAP_WANTED) && swf->swp_io_count == 0) {
39236c6e
A
1515 swf->swp_flags &= ~SWAP_WANTED;
1516 thread_wakeup((event_t) &swf->swp_flags);
1517 }
fe8ab488 1518done:
39236c6e
A
1519 lck_mtx_unlock(&vm_swap_data_lock);
1520
0a7de745 1521 if (retval == 0) {
39236c6e 1522 return KERN_SUCCESS;
0a7de745 1523 } else {
39236c6e 1524 return KERN_FAILURE;
0a7de745 1525 }
39236c6e
A
1526}
1527
1528kern_return_t
d9a64523 1529vm_swap_put(vm_offset_t addr, uint64_t *f_offset, uint32_t size, c_segment_t c_seg, struct swapout_io_completion *soc)
39236c6e 1530{
0a7de745 1531 unsigned int segidx = 0;
39236c6e 1532 struct swapfile *swf = NULL;
0a7de745
A
1533 uint64_t file_offset = 0;
1534 uint64_t swapfile_index = 0;
1535 unsigned int byte_for_segidx = 0;
1536 unsigned int offset_within_byte = 0;
1537 boolean_t swf_eligible = FALSE;
1538 boolean_t waiting = FALSE;
1539 boolean_t retried = FALSE;
1540 int error = 0;
1541 clock_sec_t sec;
1542 clock_nsec_t nsec;
d9a64523 1543 void *upl_ctx = NULL;
f427ee49 1544 boolean_t drop_iocount = FALSE;
39236c6e 1545
f427ee49 1546 if (addr == 0 || f_offset == NULL || compressor_store_stop_compaction) {
39236c6e
A
1547 return KERN_FAILURE;
1548 }
fe8ab488 1549retry:
39236c6e
A
1550 lck_mtx_lock(&vm_swap_data_lock);
1551
1552 swf = (struct swapfile*) queue_first(&swf_global_queue);
1553
0a7de745 1554 while (queue_end(&swf_global_queue, (queue_entry_t)swf) == FALSE) {
39236c6e
A
1555 segidx = swf->swp_free_hint;
1556
0a7de745 1557 swf_eligible = (swf->swp_flags & SWAP_READY) && (swf->swp_nseginuse < swf->swp_nsegs);
39236c6e
A
1558
1559 if (swf_eligible) {
0a7de745 1560 while (segidx < swf->swp_nsegs) {
39236c6e
A
1561 byte_for_segidx = segidx >> 3;
1562 offset_within_byte = segidx % 8;
0a7de745 1563
39236c6e
A
1564 if ((swf->swp_bitmap)[byte_for_segidx] & (1 << offset_within_byte)) {
1565 segidx++;
1566 continue;
1567 }
0a7de745 1568
39236c6e
A
1569 (swf->swp_bitmap)[byte_for_segidx] |= (1 << offset_within_byte);
1570
1571 file_offset = segidx * COMPRESSED_SWAP_CHUNK_SIZE;
1572 swf->swp_nseginuse++;
1573 swf->swp_io_count++;
d9a64523 1574 swf->swp_csegs[segidx] = c_seg;
39236c6e 1575
d9a64523 1576 swapfile_index = swf->swp_index;
39236c6e
A
1577 vm_swapfile_total_segs_used++;
1578
1579 clock_get_system_nanotime(&sec, &nsec);
1580
0a7de745 1581 if (VM_SWAP_SHOULD_CREATE(sec) && !vm_swapfile_create_thread_running) {
fe8ab488 1582 thread_wakeup((event_t) &vm_swapfile_create_needed);
0a7de745 1583 }
39236c6e
A
1584
1585 lck_mtx_unlock(&vm_swap_data_lock);
0a7de745 1586
d9a64523 1587 goto issue_io;
39236c6e
A
1588 }
1589 }
1590 swf = (struct swapfile*) queue_next(&swf->swp_queue);
1591 }
1592 assert(queue_end(&swf_global_queue, (queue_entry_t) swf));
0a7de745 1593
39236c6e
A
1594 /*
1595 * we've run out of swap segments, but may not
1596 * be in a position to immediately create a new swap
1597 * file if we've recently failed to create due to a lack
1598 * of free space in the root filesystem... we'll try
1599 * to kick that create off, but in any event we're going
1600 * to take a breather (up to 1 second) so that we're not caught in a tight
1601 * loop back in "vm_compressor_compact_and_swap" trying to stuff
1602 * segments into swap files only to have them immediately put back
1603 * on the c_age queue due to vm_swap_put failing.
1604 *
1605 * if we're doing these puts due to a hibernation flush,
1606 * no need to block... setting hibernate_no_swapspace to TRUE,
1607 * will cause "vm_compressor_compact_and_swap" to immediately abort
1608 */
1609 clock_get_system_nanotime(&sec, &nsec);
1610
0a7de745 1611 if (VM_SWAP_SHOULD_CREATE(sec) && !vm_swapfile_create_thread_running) {
fe8ab488 1612 thread_wakeup((event_t) &vm_swapfile_create_needed);
0a7de745 1613 }
39236c6e
A
1614
1615 if (hibernate_flushing == FALSE || VM_SWAP_SHOULD_CREATE(sec)) {
1616 waiting = TRUE;
0a7de745
A
1617 assert_wait_timeout((event_t) &vm_num_swap_files, THREAD_INTERRUPTIBLE, 1000, 1000 * NSEC_PER_USEC);
1618 } else {
39236c6e 1619 hibernate_no_swapspace = TRUE;
0a7de745 1620 }
39236c6e
A
1621
1622 lck_mtx_unlock(&vm_swap_data_lock);
1623
fe8ab488 1624 if (waiting == TRUE) {
39236c6e
A
1625 thread_block(THREAD_CONTINUE_NULL);
1626
fe8ab488
A
1627 if (retried == FALSE && hibernate_flushing == TRUE) {
1628 retried = TRUE;
1629 goto retry;
1630 }
1631 }
0a7de745 1632 vm_swap_put_failures_no_swap_file++;
fe8ab488 1633
39236c6e
A
1634 return KERN_FAILURE;
1635
0a7de745 1636issue_io:
a39ff7e2
A
1637 assert(c_seg->c_busy_swapping);
1638 assert(c_seg->c_busy);
1639 assert(!c_seg->c_on_minorcompact_q);
1640
d9a64523
A
1641 *f_offset = (swapfile_index << SWAP_DEVICE_SHIFT) | file_offset;
1642
1643 if (soc) {
1644 soc->swp_c_seg = c_seg;
1645 soc->swp_c_size = size;
39236c6e 1646
0a7de745 1647 soc->swp_swf = swf;
39236c6e 1648
d9a64523
A
1649 soc->swp_io_error = 0;
1650 soc->swp_io_done = 0;
39236c6e 1651
d9a64523
A
1652 upl_ctx = (void *)&soc->swp_upl_ctx;
1653 }
f427ee49
A
1654
1655 if ((error = vnode_getwithref(swf->swp_vp)) != 0) {
1656 printf("vm_swap_put: vnode_getwithref on swapfile failed with %d\n", error);
1657 } else {
1658 error = vm_swapfile_io(swf->swp_vp, file_offset, addr, (int) (size / PAGE_SIZE_64), SWAP_WRITE, upl_ctx);
1659 drop_iocount = TRUE;
1660 }
39236c6e 1661
0a7de745 1662 if (error || upl_ctx == NULL) {
f427ee49 1663 return vm_swap_put_finish(swf, f_offset, error, drop_iocount);
0a7de745 1664 }
d9a64523
A
1665
1666 return KERN_SUCCESS;
1667}
1668
1669kern_return_t
f427ee49 1670vm_swap_put_finish(struct swapfile *swf, uint64_t *f_offset, int error, boolean_t drop_iocount)
d9a64523 1671{
f427ee49
A
1672 if (drop_iocount) {
1673 vnode_put(swf->swp_vp);
1674 }
1675
d9a64523
A
1676 lck_mtx_lock(&vm_swap_data_lock);
1677
1678 swf->swp_io_count--;
39236c6e
A
1679
1680 if ((swf->swp_flags & SWAP_WANTED) && swf->swp_io_count == 0) {
39236c6e
A
1681 swf->swp_flags &= ~SWAP_WANTED;
1682 thread_wakeup((event_t) &swf->swp_flags);
1683 }
39236c6e
A
1684 lck_mtx_unlock(&vm_swap_data_lock);
1685
39236c6e
A
1686 if (error) {
1687 vm_swap_free(*f_offset);
3e170ce0
A
1688 vm_swap_put_failures++;
1689
39236c6e
A
1690 return KERN_FAILURE;
1691 }
1692 return KERN_SUCCESS;
1693}
1694
1695
39236c6e
A
1696static void
1697vm_swap_free_now(struct swapfile *swf, uint64_t f_offset)
1698{
0a7de745
A
1699 uint64_t file_offset = 0;
1700 unsigned int segidx = 0;
39236c6e
A
1701
1702
1703 if ((swf->swp_flags & SWAP_READY) || (swf->swp_flags & SWAP_RECLAIM)) {
39236c6e
A
1704 unsigned int byte_for_segidx = 0;
1705 unsigned int offset_within_byte = 0;
1706
1707 file_offset = (f_offset & SWAP_SLOT_MASK);
1708 segidx = (unsigned int) (file_offset / COMPRESSED_SWAP_CHUNK_SIZE);
0a7de745 1709
39236c6e
A
1710 byte_for_segidx = segidx >> 3;
1711 offset_within_byte = segidx % 8;
1712
1713 if ((swf->swp_bitmap)[byte_for_segidx] & (1 << offset_within_byte)) {
39236c6e
A
1714 (swf->swp_bitmap)[byte_for_segidx] &= ~(1 << offset_within_byte);
1715
1716 swf->swp_csegs[segidx] = NULL;
1717
1718 swf->swp_nseginuse--;
1719 vm_swapfile_total_segs_used--;
1720
1721 if (segidx < swf->swp_free_hint) {
1722 swf->swp_free_hint = segidx;
1723 }
1724 }
0a7de745 1725 if (VM_SWAP_SHOULD_RECLAIM() && !vm_swapfile_gc_thread_running) {
fe8ab488 1726 thread_wakeup((event_t) &vm_swapfile_gc_needed);
0a7de745 1727 }
39236c6e 1728 }
39236c6e
A
1729}
1730
1731
1732uint32_t vm_swap_free_now_count = 0;
1733uint32_t vm_swap_free_delayed_count = 0;
1734
1735
1736void
1737vm_swap_free(uint64_t f_offset)
1738{
1739 struct swapfile *swf = NULL;
fe8ab488 1740 struct trim_list *tl = NULL;
0a7de745
A
1741 clock_sec_t sec;
1742 clock_nsec_t nsec;
39236c6e 1743
0a7de745 1744 if (swp_trim_supported == TRUE) {
fe8ab488 1745 tl = kalloc(sizeof(struct trim_list));
0a7de745 1746 }
fe8ab488 1747
39236c6e
A
1748 lck_mtx_lock(&vm_swap_data_lock);
1749
1750 swf = vm_swapfile_for_handle(f_offset);
1751
1752 if (swf && (swf->swp_flags & (SWAP_READY | SWAP_RECLAIM))) {
fe8ab488 1753 if (swp_trim_supported == FALSE || (swf->swp_flags & SWAP_RECLAIM)) {
39236c6e
A
1754 /*
1755 * don't delay the free if the underlying disk doesn't support
1756 * trim, or we're in the midst of reclaiming this swap file since
1757 * we don't want to move segments that are technically free
1758 * but not yet handled by the delayed free mechanism
1759 */
1760 vm_swap_free_now(swf, f_offset);
1761
1762 vm_swap_free_now_count++;
fe8ab488 1763 goto done;
39236c6e 1764 }
39236c6e
A
1765 tl->tl_offset = f_offset & SWAP_SLOT_MASK;
1766 tl->tl_length = COMPRESSED_SWAP_CHUNK_SIZE;
1767
1768 tl->tl_next = swf->swp_delayed_trim_list_head;
1769 swf->swp_delayed_trim_list_head = tl;
1770 swf->swp_delayed_trim_count++;
fe8ab488 1771 tl = NULL;
39236c6e 1772
fe8ab488 1773 if (VM_SWAP_SHOULD_TRIM(swf) && !vm_swapfile_create_thread_running) {
39236c6e
A
1774 clock_get_system_nanotime(&sec, &nsec);
1775
0a7de745 1776 if (sec > dont_trim_until_ts) {
fe8ab488 1777 thread_wakeup((event_t) &vm_swapfile_create_needed);
0a7de745 1778 }
39236c6e
A
1779 }
1780 vm_swap_free_delayed_count++;
1781 }
fe8ab488 1782done:
39236c6e 1783 lck_mtx_unlock(&vm_swap_data_lock);
fe8ab488 1784
0a7de745 1785 if (tl != NULL) {
fe8ab488 1786 kfree(tl, sizeof(struct trim_list));
0a7de745
A
1787 }
1788}
39236c6e
A
1789
1790
fe8ab488
A
1791static void
1792vm_swap_wait_on_trim_handling_in_progress()
1793{
1794 while (delayed_trim_handling_in_progress == TRUE) {
fe8ab488
A
1795 assert_wait((event_t) &delayed_trim_handling_in_progress, THREAD_UNINT);
1796 lck_mtx_unlock(&vm_swap_data_lock);
0a7de745 1797
fe8ab488 1798 thread_block(THREAD_CONTINUE_NULL);
0a7de745 1799
fe8ab488
A
1800 lck_mtx_lock(&vm_swap_data_lock);
1801 }
1802}
1803
1804
39236c6e
A
1805static void
1806vm_swap_handle_delayed_trims(boolean_t force_now)
1807{
1808 struct swapfile *swf = NULL;
1809
1810 /*
fe8ab488
A
1811 * serialize the race between us and vm_swap_reclaim...
1812 * if vm_swap_reclaim wins it will turn off SWAP_READY
1813 * on the victim it has chosen... we can just skip over
1814 * that file since vm_swap_reclaim will first process
1815 * all of the delayed trims associated with it
1816 */
f427ee49
A
1817
1818 if (compressor_store_stop_compaction == TRUE) {
1819 return;
1820 }
1821
fe8ab488
A
1822 lck_mtx_lock(&vm_swap_data_lock);
1823
1824 delayed_trim_handling_in_progress = TRUE;
1825
1826 lck_mtx_unlock(&vm_swap_data_lock);
1827
1828 /*
1829 * no need to hold the lock to walk the swf list since
1830 * vm_swap_create (the only place where we add to this list)
1831 * is run on the same thread as this function
1832 * and vm_swap_reclaim doesn't remove items from this list
1833 * instead marking them with SWAP_REUSE for future re-use
39236c6e
A
1834 */
1835 swf = (struct swapfile*) queue_first(&swf_global_queue);
1836
1837 while (queue_end(&swf_global_queue, (queue_entry_t)swf) == FALSE) {
fe8ab488 1838 if ((swf->swp_flags & SWAP_READY) && (force_now == TRUE || VM_SWAP_SHOULD_TRIM(swf))) {
fe8ab488 1839 assert(!(swf->swp_flags & SWAP_RECLAIM));
39236c6e 1840 vm_swap_do_delayed_trim(swf);
fe8ab488 1841 }
39236c6e
A
1842 swf = (struct swapfile*) queue_next(&swf->swp_queue);
1843 }
fe8ab488
A
1844 lck_mtx_lock(&vm_swap_data_lock);
1845
1846 delayed_trim_handling_in_progress = FALSE;
1847 thread_wakeup((event_t) &delayed_trim_handling_in_progress);
39236c6e 1848
0a7de745 1849 if (VM_SWAP_SHOULD_RECLAIM() && !vm_swapfile_gc_thread_running) {
fe8ab488 1850 thread_wakeup((event_t) &vm_swapfile_gc_needed);
0a7de745 1851 }
fe8ab488
A
1852
1853 lck_mtx_unlock(&vm_swap_data_lock);
fe8ab488 1854}
39236c6e
A
1855
1856static void
1857vm_swap_do_delayed_trim(struct swapfile *swf)
1858{
1859 struct trim_list *tl, *tl_head;
f427ee49
A
1860 int error;
1861
1862 if (compressor_store_stop_compaction == TRUE) {
1863 return;
1864 }
1865
1866 if ((error = vnode_getwithref(swf->swp_vp)) != 0) {
1867 printf("vm_swap_do_delayed_trim: vnode_getwithref on swapfile failed with %d\n", error);
1868 return;
1869 }
39236c6e
A
1870
1871 lck_mtx_lock(&vm_swap_data_lock);
1872
1873 tl_head = swf->swp_delayed_trim_list_head;
1874 swf->swp_delayed_trim_list_head = NULL;
1875 swf->swp_delayed_trim_count = 0;
1876
1877 lck_mtx_unlock(&vm_swap_data_lock);
1878
fe8ab488 1879 vnode_trim_list(swf->swp_vp, tl_head, TRUE);
0a7de745 1880
f427ee49
A
1881 (void) vnode_put(swf->swp_vp);
1882
39236c6e 1883 while ((tl = tl_head) != NULL) {
0a7de745
A
1884 unsigned int segidx = 0;
1885 unsigned int byte_for_segidx = 0;
1886 unsigned int offset_within_byte = 0;
39236c6e
A
1887
1888 lck_mtx_lock(&vm_swap_data_lock);
1889
1890 segidx = (unsigned int) (tl->tl_offset / COMPRESSED_SWAP_CHUNK_SIZE);
0a7de745 1891
39236c6e
A
1892 byte_for_segidx = segidx >> 3;
1893 offset_within_byte = segidx % 8;
1894
1895 if ((swf->swp_bitmap)[byte_for_segidx] & (1 << offset_within_byte)) {
39236c6e 1896 (swf->swp_bitmap)[byte_for_segidx] &= ~(1 << offset_within_byte);
0a7de745 1897
39236c6e
A
1898 swf->swp_csegs[segidx] = NULL;
1899
1900 swf->swp_nseginuse--;
1901 vm_swapfile_total_segs_used--;
1902
1903 if (segidx < swf->swp_free_hint) {
1904 swf->swp_free_hint = segidx;
1905 }
1906 }
1907 lck_mtx_unlock(&vm_swap_data_lock);
1908
1909 tl_head = tl->tl_next;
1910
1911 kfree(tl, sizeof(struct trim_list));
0a7de745 1912 }
39236c6e
A
1913}
1914
1915
1916void
1917vm_swap_flush()
1918{
1919 return;
1920}
1921
0a7de745 1922int vm_swap_reclaim_yielded = 0;
39236c6e
A
1923
1924void
1925vm_swap_reclaim(void)
1926{
0a7de745
A
1927 vm_offset_t addr = 0;
1928 unsigned int segidx = 0;
1929 uint64_t f_offset = 0;
39236c6e
A
1930 struct swapfile *swf = NULL;
1931 struct swapfile *smallest_swf = NULL;
0a7de745
A
1932 unsigned int min_nsegs = 0;
1933 unsigned int byte_for_segidx = 0;
1934 unsigned int offset_within_byte = 0;
1935 uint32_t c_size = 0;
1936
1937 c_segment_t c_seg = NULL;
39236c6e 1938
39037602 1939 if (kernel_memory_allocate(compressor_map, (vm_offset_t *)(&addr), C_SEG_BUFSIZE, 0, KMA_KOBJECT, VM_KERN_MEMORY_COMPRESSOR) != KERN_SUCCESS) {
39236c6e
A
1940 panic("vm_swap_reclaim: kernel_memory_allocate failed\n");
1941 }
1942
1943 lck_mtx_lock(&vm_swap_data_lock);
1944
fe8ab488
A
1945 /*
1946 * if we're running the swapfile list looking for
1947 * candidates with delayed trims, we need to
1948 * wait before making our decision concerning
1949 * the swapfile we want to reclaim
1950 */
1951 vm_swap_wait_on_trim_handling_in_progress();
1952
1953 /*
1954 * from here until we knock down the SWAP_READY bit,
1955 * we need to remain behind the vm_swap_data_lock...
1956 * once that bit has been turned off, "vm_swap_handle_delayed_trims"
1957 * will not consider this swapfile for processing
1958 */
39236c6e
A
1959 swf = (struct swapfile*) queue_first(&swf_global_queue);
1960 min_nsegs = MAX_SWAP_FILE_SIZE / COMPRESSED_SWAP_CHUNK_SIZE;
1961 smallest_swf = NULL;
1962
1963 while (queue_end(&swf_global_queue, (queue_entry_t)swf) == FALSE) {
39236c6e 1964 if ((swf->swp_flags & SWAP_READY) && (swf->swp_nseginuse <= min_nsegs)) {
39236c6e
A
1965 smallest_swf = swf;
1966 min_nsegs = swf->swp_nseginuse;
0a7de745 1967 }
39236c6e
A
1968 swf = (struct swapfile*) queue_next(&swf->swp_queue);
1969 }
0a7de745
A
1970
1971 if (smallest_swf == NULL) {
39236c6e 1972 goto done;
0a7de745 1973 }
39236c6e
A
1974
1975 swf = smallest_swf;
1976
1977
1978 swf->swp_flags &= ~SWAP_READY;
1979 swf->swp_flags |= SWAP_RECLAIM;
1980
1981 if (swf->swp_delayed_trim_count) {
39236c6e
A
1982 lck_mtx_unlock(&vm_swap_data_lock);
1983
1984 vm_swap_do_delayed_trim(swf);
1985
1986 lck_mtx_lock(&vm_swap_data_lock);
1987 }
1988 segidx = 0;
1989
1990 while (segidx < swf->swp_nsegs) {
0a7de745 1991ReTry_for_cseg:
39236c6e
A
1992 /*
1993 * Wait for outgoing I/Os.
1994 */
1995 while (swf->swp_io_count) {
39236c6e
A
1996 swf->swp_flags |= SWAP_WANTED;
1997
1998 assert_wait((event_t) &swf->swp_flags, THREAD_UNINT);
1999 lck_mtx_unlock(&vm_swap_data_lock);
0a7de745 2000
39236c6e 2001 thread_block(THREAD_CONTINUE_NULL);
0a7de745 2002
39236c6e
A
2003 lck_mtx_lock(&vm_swap_data_lock);
2004 }
0a7de745 2005 if (compressor_store_stop_compaction == TRUE || VM_SWAP_SHOULD_ABORT_RECLAIM() || VM_SWAP_BUSY()) {
fe8ab488
A
2006 vm_swap_reclaim_yielded++;
2007 break;
2008 }
39236c6e
A
2009
2010 byte_for_segidx = segidx >> 3;
2011 offset_within_byte = segidx % 8;
2012
2013 if (((swf->swp_bitmap)[byte_for_segidx] & (1 << offset_within_byte)) == 0) {
39236c6e
A
2014 segidx++;
2015 continue;
2016 }
2017
2018 c_seg = swf->swp_csegs[segidx];
3e170ce0 2019 assert(c_seg);
39236c6e
A
2020
2021 lck_mtx_lock_spin_always(&c_seg->c_lock);
2022
39236c6e 2023 if (c_seg->c_busy) {
3e170ce0
A
2024 /*
2025 * a swapped out c_segment in the process of being freed will remain in the
2026 * busy state until after the vm_swap_free is called on it... vm_swap_free
2027 * takes the vm_swap_data_lock, so can't change the swap state until after
2028 * we drop the vm_swap_data_lock... once we do, vm_swap_free will complete
2029 * which will allow c_seg_free_locked to clear busy and wake up this thread...
2030 * at that point, we re-look up the swap state which will now indicate that
2031 * this c_segment no longer exists.
2032 */
39236c6e 2033 c_seg->c_wanted = 1;
0a7de745 2034
39236c6e
A
2035 assert_wait((event_t) (c_seg), THREAD_UNINT);
2036 lck_mtx_unlock_always(&c_seg->c_lock);
0a7de745 2037
39236c6e 2038 lck_mtx_unlock(&vm_swap_data_lock);
0a7de745 2039
39236c6e
A
2040 thread_block(THREAD_CONTINUE_NULL);
2041
2042 lck_mtx_lock(&vm_swap_data_lock);
0a7de745 2043
39236c6e
A
2044 goto ReTry_for_cseg;
2045 }
2046 (swf->swp_bitmap)[byte_for_segidx] &= ~(1 << offset_within_byte);
2047
2048 f_offset = segidx * COMPRESSED_SWAP_CHUNK_SIZE;
3e170ce0
A
2049
2050 assert(c_seg == swf->swp_csegs[segidx]);
39236c6e
A
2051 swf->swp_csegs[segidx] = NULL;
2052 swf->swp_nseginuse--;
2053
2054 vm_swapfile_total_segs_used--;
0a7de745 2055
39236c6e 2056 lck_mtx_unlock(&vm_swap_data_lock);
39236c6e 2057
0a7de745 2058 assert(C_SEG_IS_ONDISK(c_seg));
3e170ce0
A
2059
2060 C_SEG_BUSY(c_seg);
2061 c_seg->c_busy_swapping = 1;
39236c6e 2062#if !CHECKSUM_THE_SWAP
3e170ce0 2063 c_seg_trim_tail(c_seg);
39236c6e 2064#endif
3e170ce0 2065 c_size = round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset));
0a7de745 2066
3e170ce0 2067 assert(c_size <= C_SEG_BUFSIZE && c_size);
39236c6e 2068
3e170ce0 2069 lck_mtx_unlock_always(&c_seg->c_lock);
39236c6e 2070
f427ee49
A
2071 if (vnode_getwithref(swf->swp_vp)) {
2072 printf("vm_swap_reclaim: vnode_getwithref on swapfile failed.\n");
3e170ce0
A
2073 vm_swap_get_failures++;
2074 goto swap_io_failed;
f427ee49
A
2075 } else {
2076 if (vm_swapfile_io(swf->swp_vp, f_offset, addr, (int)(c_size / PAGE_SIZE_64), SWAP_READ, NULL)) {
2077 /*
2078 * reading the data back in failed, so convert c_seg
2079 * to a swapped in c_segment that contains no data
2080 */
2081 c_seg_swapin_requeue(c_seg, FALSE, TRUE, FALSE);
2082 /*
2083 * returns with c_busy_swapping cleared
2084 */
2085 vnode_put(swf->swp_vp);
2086 vm_swap_get_failures++;
2087 goto swap_io_failed;
2088 }
2089 vnode_put(swf->swp_vp);
3e170ce0 2090 }
f427ee49 2091
c3c9b80d 2092 counter_add(&vm_statistics_swapins, c_size >> PAGE_SHIFT);
39236c6e 2093
d9a64523 2094 if (vm_swap_put(addr, &f_offset, c_size, c_seg, NULL)) {
0a7de745 2095 vm_offset_t c_buffer;
39236c6e 2096
3e170ce0
A
2097 /*
2098 * the put failed, so convert c_seg to a fully swapped in c_segment
2099 * with valid data
2100 */
2101 c_buffer = (vm_offset_t)C_SEG_BUFFER_ADDRESS(c_seg->c_mysegno);
fe8ab488 2102
39037602 2103 kernel_memory_populate(compressor_map, c_buffer, c_size, KMA_COMPRESSOR, VM_KERN_MEMORY_COMPRESSOR);
39236c6e 2104
3e170ce0 2105 memcpy((char *)c_buffer, (char *)addr, c_size);
39236c6e 2106
3e170ce0 2107 c_seg->c_store.c_buffer = (int32_t *)c_buffer;
fe8ab488 2108#if ENCRYPTED_SWAP
3e170ce0 2109 vm_swap_decrypt(c_seg);
fe8ab488 2110#endif /* ENCRYPTED_SWAP */
39037602 2111 c_seg_swapin_requeue(c_seg, TRUE, TRUE, FALSE);
3e170ce0
A
2112 /*
2113 * returns with c_busy_swapping cleared
2114 */
2115 OSAddAtomic64(c_seg->c_bytes_used, &compressor_bytes_used);
39236c6e 2116
3e170ce0
A
2117 goto swap_io_failed;
2118 }
c3c9b80d 2119 counter_add(&vm_statistics_swapouts, c_size >> PAGE_SHIFT);
39236c6e 2120
3e170ce0 2121 lck_mtx_lock_spin_always(&c_seg->c_lock);
0a7de745 2122
3e170ce0
A
2123 assert(C_SEG_IS_ONDISK(c_seg));
2124 /*
2125 * The c_seg will now know about the new location on disk.
2126 */
2127 c_seg->c_store.c_swap_handle = f_offset;
39037602
A
2128
2129 assert(c_seg->c_busy_swapping);
3e170ce0 2130 c_seg->c_busy_swapping = 0;
39236c6e 2131swap_io_failed:
39037602 2132 assert(c_seg->c_busy);
3e170ce0 2133 C_SEG_WAKEUP_DONE(c_seg);
0a7de745 2134
3e170ce0 2135 lck_mtx_unlock_always(&c_seg->c_lock);
39236c6e
A
2136 lck_mtx_lock(&vm_swap_data_lock);
2137 }
2138
2139 if (swf->swp_nseginuse) {
39236c6e
A
2140 swf->swp_flags &= ~SWAP_RECLAIM;
2141 swf->swp_flags |= SWAP_READY;
2142
2143 goto done;
2144 }
2145 /*
0a7de745 2146 * We don't remove this inactive swf from the queue.
39236c6e 2147 * That way, we can re-use it when needed again and
fe8ab488
A
2148 * preserve the namespace. The delayed_trim processing
2149 * is also dependent on us not removing swfs from the queue.
0a7de745 2150 */
39236c6e
A
2151 //queue_remove(&swf_global_queue, swf, struct swapfile*, swp_queue);
2152
39236c6e
A
2153 vm_swapfile_total_segs_alloced -= swf->swp_nsegs;
2154
2155 lck_mtx_unlock(&vm_swap_data_lock);
2156
2157 vm_swapfile_close((uint64_t)(swf->swp_path), swf->swp_vp);
2158
2159 kfree(swf->swp_csegs, swf->swp_nsegs * sizeof(c_segment_t));
f427ee49
A
2160 kheap_free(KHEAP_DATA_BUFFERS, swf->swp_bitmap,
2161 MAX((swf->swp_nsegs >> 3), 1));
0a7de745 2162
39236c6e
A
2163 lck_mtx_lock(&vm_swap_data_lock);
2164
3e170ce0 2165 if (swf->swp_flags & SWAP_PINNED) {
39037602 2166 vm_num_pinned_swap_files--;
3e170ce0
A
2167 vm_swappin_avail += swf->swp_size;
2168 }
2169
0a7de745 2170 swf->swp_vp = NULL;
39236c6e
A
2171 swf->swp_size = 0;
2172 swf->swp_free_hint = 0;
2173 swf->swp_nsegs = 0;
2174 swf->swp_flags = SWAP_REUSE;
2175
0a7de745
A
2176 vm_num_swap_files--;
2177
39236c6e 2178done:
fe8ab488 2179 thread_wakeup((event_t) &swf->swp_flags);
39236c6e
A
2180 lck_mtx_unlock(&vm_swap_data_lock);
2181
39037602 2182 kmem_free(compressor_map, (vm_offset_t) addr, C_SEG_BUFSIZE);
39236c6e
A
2183}
2184
2185
2186uint64_t
2187vm_swap_get_total_space(void)
2188{
2189 uint64_t total_space = 0;
2190
2191 total_space = (uint64_t)vm_swapfile_total_segs_alloced * COMPRESSED_SWAP_CHUNK_SIZE;
2192
2193 return total_space;
2194}
2195
2196uint64_t
2197vm_swap_get_used_space(void)
2198{
2199 uint64_t used_space = 0;
2200
2201 used_space = (uint64_t)vm_swapfile_total_segs_used * COMPRESSED_SWAP_CHUNK_SIZE;
2202
2203 return used_space;
2204}
2205
2206uint64_t
2207vm_swap_get_free_space(void)
2208{
0a7de745 2209 return vm_swap_get_total_space() - vm_swap_get_used_space();
39236c6e 2210}
fe8ab488 2211
f427ee49
A
2212uint64_t
2213vm_swap_get_max_configured_space(void)
2214{
2215 int num_swap_files = (vm_num_swap_files_config ? vm_num_swap_files_config : VM_MAX_SWAP_FILE_NUM);
2216 return num_swap_files * MAX_SWAP_FILE_SIZE;
2217}
fe8ab488
A
2218
2219int
2220vm_swap_low_on_space(void)
2221{
0a7de745
A
2222 if (vm_num_swap_files == 0 && vm_swapfile_can_be_created == FALSE) {
2223 return 0;
2224 }
fe8ab488
A
2225
2226 if (((vm_swapfile_total_segs_alloced - vm_swapfile_total_segs_used) < ((unsigned int)VM_SWAPFILE_HIWATER_SEGS) / 8)) {
0a7de745
A
2227 if (vm_num_swap_files == 0 && !SWAPPER_NEEDS_TO_UNTHROTTLE()) {
2228 return 0;
2229 }
fe8ab488 2230
0a7de745
A
2231 if (vm_swapfile_last_failed_to_create_ts >= vm_swapfile_last_successful_create_ts) {
2232 return 1;
2233 }
fe8ab488 2234 }
0a7de745 2235 return 0;
fe8ab488 2236}
39037602 2237
f427ee49
A
2238int
2239vm_swap_out_of_space(void)
2240{
2241 if ((vm_num_swap_files == vm_num_swap_files_config) &&
2242 ((vm_swapfile_total_segs_alloced - vm_swapfile_total_segs_used) < VM_SWAPOUT_LIMIT_MAX)) {
2243 /*
2244 * Last swapfile and we have only space for the
2245 * last few swapouts.
2246 */
2247 return 1;
2248 }
2249
2250 return 0;
2251}
2252
39037602
A
2253boolean_t
2254vm_swap_files_pinned(void)
2255{
0a7de745 2256 boolean_t result;
39037602 2257
0a7de745
A
2258 if (vm_swappin_enabled == FALSE) {
2259 return TRUE;
2260 }
39037602 2261
0a7de745 2262 result = (vm_num_pinned_swap_files == vm_num_swap_files);
39037602 2263
0a7de745 2264 return result;
39037602 2265}
d9a64523
A
2266
2267#if CONFIG_FREEZE
2268boolean_t
2269vm_swap_max_budget(uint64_t *freeze_daily_budget)
2270{
0a7de745 2271 boolean_t use_device_value = FALSE;
d9a64523
A
2272 struct swapfile *swf = NULL;
2273
2274 if (vm_num_swap_files) {
2275 lck_mtx_lock(&vm_swap_data_lock);
2276
2277 swf = (struct swapfile*) queue_first(&swf_global_queue);
2278
2279 if (swf) {
0a7de745 2280 while (queue_end(&swf_global_queue, (queue_entry_t)swf) == FALSE) {
d9a64523 2281 if (swf->swp_flags == SWAP_READY) {
d9a64523
A
2282 assert(swf->swp_vp);
2283
2284 if (vm_swap_vol_get_budget(swf->swp_vp, freeze_daily_budget) == 0) {
2285 use_device_value = TRUE;
2286 }
2287 break;
2288 }
2289 swf = (struct swapfile*) queue_next(&swf->swp_queue);
2290 }
2291 }
2292
2293 lck_mtx_unlock(&vm_swap_data_lock);
d9a64523 2294 } else {
d9a64523
A
2295 /*
2296 * This block is used for the initial budget value before any swap files
2297 * are created. We create a temp swap file to get the budget.
2298 */
2299
2300 struct vnode *temp_vp = NULL;
2301
2302 vm_swapfile_open(swapfilename, &temp_vp);
2303
2304 if (temp_vp) {
d9a64523
A
2305 if (vm_swap_vol_get_budget(temp_vp, freeze_daily_budget) == 0) {
2306 use_device_value = TRUE;
2307 }
2308
2309 vm_swapfile_close((uint64_t)&swapfilename, temp_vp);
2310 temp_vp = NULL;
2311 } else {
2312 *freeze_daily_budget = 0;
2313 }
2314 }
2315
2316 return use_device_value;
2317}
2318#endif /* CONFIG_FREEZE */