This repository has been archived by the owner on May 22, 2023. It is now read-only.
/
vm_compressor_backing_store.c
2320 lines (1789 loc) · 61.1 KB
/
vm_compressor_backing_store.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000-2013 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
#include "vm_compressor_backing_store.h"
#include <vm/vm_pageout.h>
#include <vm/vm_protos.h>
#include <IOKit/IOHibernatePrivate.h>
#include <kern/policy_internal.h>
LCK_GRP_DECLARE(vm_swap_data_lock_grp, "vm_swap_data");
LCK_MTX_EARLY_DECLARE(vm_swap_data_lock, &vm_swap_data_lock_grp);
#if defined(XNU_TARGET_OS_OSX)
/*
* launchd explicitly turns ON swap later during boot on macOS devices.
*/
boolean_t compressor_store_stop_compaction = TRUE;
#else
boolean_t compressor_store_stop_compaction = FALSE;
#endif
boolean_t vm_swapfile_create_needed = FALSE;
boolean_t vm_swapfile_gc_needed = FALSE;
int vm_swapper_throttle = -1;
uint64_t vm_swapout_thread_id;
uint64_t vm_swap_put_failures = 0; /* Likely failed I/O. Data is still in memory. */
uint64_t vm_swap_get_failures = 0; /* Fatal */
uint64_t vm_swap_put_failures_no_swap_file = 0; /* Possibly not fatal because we might just need a new swapfile. */
int vm_num_swap_files_config = 0;
int vm_num_swap_files = 0;
int vm_num_pinned_swap_files = 0;
int vm_swapout_thread_processed_segments = 0;
int vm_swapout_thread_awakened = 0;
bool vm_swapout_thread_running = FALSE;
int vm_swapfile_create_thread_awakened = 0;
int vm_swapfile_create_thread_running = 0;
int vm_swapfile_gc_thread_awakened = 0;
int vm_swapfile_gc_thread_running = 0;
int64_t vm_swappin_avail = 0;
boolean_t vm_swappin_enabled = FALSE;
unsigned int vm_swapfile_total_segs_alloced = 0;
unsigned int vm_swapfile_total_segs_used = 0;
char swapfilename[MAX_SWAPFILENAME_LEN + 1] = SWAP_FILE_NAME;
extern vm_map_t compressor_map;
#define SWAP_READY 0x1 /* Swap file is ready to be used */
#define SWAP_RECLAIM 0x2 /* Swap file is marked to be reclaimed */
#define SWAP_WANTED 0x4 /* Swap file has waiters */
#define SWAP_REUSE 0x8 /* Swap file is on the Q and has a name. Reuse after init-ing.*/
#define SWAP_PINNED 0x10 /* Swap file is pinned (FusionDrive) */
struct swapfile {
queue_head_t swp_queue; /* list of swap files */
char *swp_path; /* saved pathname of swap file */
struct vnode *swp_vp; /* backing vnode */
uint64_t swp_size; /* size of this swap file */
uint8_t *swp_bitmap; /* bitmap showing the alloced/freed slots in the swap file */
unsigned int swp_pathlen; /* length of pathname */
unsigned int swp_nsegs; /* #segments we can use */
unsigned int swp_nseginuse; /* #segments in use */
unsigned int swp_index; /* index of this swap file */
unsigned int swp_flags; /* state of swap file */
unsigned int swp_free_hint; /* offset of 1st free chunk */
unsigned int swp_io_count; /* count of outstanding I/Os */
c_segment_t *swp_csegs; /* back pointers to the c_segments. Used during swap reclaim. */
struct trim_list *swp_delayed_trim_list_head;
unsigned int swp_delayed_trim_count;
};
queue_head_t swf_global_queue;
boolean_t swp_trim_supported = FALSE;
extern clock_sec_t dont_trim_until_ts;
clock_sec_t vm_swapfile_last_failed_to_create_ts = 0;
clock_sec_t vm_swapfile_last_successful_create_ts = 0;
int vm_swapfile_can_be_created = FALSE;
boolean_t delayed_trim_handling_in_progress = FALSE;
boolean_t hibernate_in_progress_with_pinned_swap = FALSE;
static void vm_swapout_thread_throttle_adjust(void);
static void vm_swap_free_now(struct swapfile *swf, uint64_t f_offset);
static void vm_swapout_thread(void);
static void vm_swapfile_create_thread(void);
static void vm_swapfile_gc_thread(void);
static void vm_swap_defragment(void);
static void vm_swap_handle_delayed_trims(boolean_t);
static void vm_swap_do_delayed_trim(struct swapfile *);
static void vm_swap_wait_on_trim_handling_in_progress(void);
extern int vnode_getwithref(struct vnode* vp);
boolean_t vm_swap_force_defrag = FALSE, vm_swap_force_reclaim = FALSE;
#if !XNU_TARGET_OS_OSX
/*
* For CONFIG_FREEZE, we scale the c_segments_limit based on the
* number of swapfiles allowed. That increases wired memory overhead.
* So we want to keep the max swapfiles same on both DEV/RELEASE so
* that the memory overhead is similar for performance comparisons.
*/
#define VM_MAX_SWAP_FILE_NUM 5
#define VM_SWAPFILE_DELAYED_TRIM_MAX 4
#define VM_SWAP_SHOULD_DEFRAGMENT() (((vm_swap_force_defrag == TRUE) || (c_swappedout_sparse_count > (vm_swapfile_total_segs_used / 16))) ? 1 : 0)
#define VM_SWAP_SHOULD_PIN(_size) FALSE
#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) && \
((cur_ts - vm_swapfile_last_failed_to_create_ts) > VM_SWAPFILE_DELAYED_CREATE) ? 1 : 0)
#define VM_SWAP_SHOULD_TRIM(swf) ((swf->swp_delayed_trim_count >= VM_SWAPFILE_DELAYED_TRIM_MAX) ? 1 : 0)
#else /* !XNU_TARGET_OS_OSX */
#define VM_MAX_SWAP_FILE_NUM 100
#define VM_SWAPFILE_DELAYED_TRIM_MAX 128
#define VM_SWAP_SHOULD_DEFRAGMENT() (((vm_swap_force_defrag == TRUE) || (c_swappedout_sparse_count > (vm_swapfile_total_segs_used / 4))) ? 1 : 0)
#define VM_SWAP_SHOULD_PIN(_size) (vm_swappin_avail > 0 && vm_swappin_avail >= (int64_t)(_size))
#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) && \
((cur_ts - vm_swapfile_last_failed_to_create_ts) > VM_SWAPFILE_DELAYED_CREATE) ? 1 : 0)
#define VM_SWAP_SHOULD_TRIM(swf) ((swf->swp_delayed_trim_count >= VM_SWAPFILE_DELAYED_TRIM_MAX) ? 1 : 0)
#endif /* !XNU_TARGET_OS_OSX */
#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)
#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)
#define VM_SWAPFILE_DELAYED_CREATE 15
#define VM_SWAP_BUSY() ((c_swapout_count && (vm_swapper_throttle == THROTTLE_LEVEL_COMPRESSOR_TIER0)) ? 1 : 0)
#if CHECKSUM_THE_SWAP
extern unsigned int hash_string(char *cp, int len);
#endif
#if RECORD_THE_COMPRESSED_DATA
boolean_t c_compressed_record_init_done = FALSE;
int c_compressed_record_write_error = 0;
struct vnode *c_compressed_record_vp = NULL;
uint64_t c_compressed_record_file_offset = 0;
void c_compressed_record_init(void);
void c_compressed_record_write(char *, int);
#endif
extern void vm_pageout_io_throttle(void);
static struct swapfile *vm_swapfile_for_handle(uint64_t);
/*
* Called with the vm_swap_data_lock held.
*/
static struct swapfile *
vm_swapfile_for_handle(uint64_t f_offset)
{
uint64_t file_offset = 0;
unsigned int swapfile_index = 0;
struct swapfile* swf = NULL;
file_offset = (f_offset & SWAP_SLOT_MASK);
swapfile_index = (f_offset >> SWAP_DEVICE_SHIFT);
swf = (struct swapfile*) queue_first(&swf_global_queue);
while (queue_end(&swf_global_queue, (queue_entry_t)swf) == FALSE) {
if (swapfile_index == swf->swp_index) {
break;
}
swf = (struct swapfile*) queue_next(&swf->swp_queue);
}
if (queue_end(&swf_global_queue, (queue_entry_t) swf)) {
swf = NULL;
}
return swf;
}
#if ENCRYPTED_SWAP
#include <libkern/crypto/aesxts.h>
extern int cc_rand_generate(void *, size_t); /* from libkern/cyrpto/rand.h> */
boolean_t swap_crypt_initialized;
void swap_crypt_initialize(void);
symmetric_xts xts_modectx;
uint32_t swap_crypt_key1[8]; /* big enough for a 256 bit random key */
uint32_t swap_crypt_key2[8]; /* big enough for a 256 bit random key */
#if DEVELOPMENT || DEBUG
boolean_t swap_crypt_xts_tested = FALSE;
unsigned char swap_crypt_test_page_ref[4096] __attribute__((aligned(4096)));
unsigned char swap_crypt_test_page_encrypt[4096] __attribute__((aligned(4096)));
unsigned char swap_crypt_test_page_decrypt[4096] __attribute__((aligned(4096)));
#endif /* DEVELOPMENT || DEBUG */
unsigned long vm_page_encrypt_counter;
unsigned long vm_page_decrypt_counter;
void
swap_crypt_initialize(void)
{
uint8_t *enckey1, *enckey2;
int keylen1, keylen2;
int error;
assert(swap_crypt_initialized == FALSE);
keylen1 = sizeof(swap_crypt_key1);
enckey1 = (uint8_t *)&swap_crypt_key1;
keylen2 = sizeof(swap_crypt_key2);
enckey2 = (uint8_t *)&swap_crypt_key2;
error = cc_rand_generate((void *)enckey1, keylen1);
assert(!error);
error = cc_rand_generate((void *)enckey2, keylen2);
assert(!error);
error = xts_start(0, NULL, enckey1, keylen1, enckey2, keylen2, 0, 0, &xts_modectx);
assert(!error);
swap_crypt_initialized = TRUE;
#if DEVELOPMENT || DEBUG
uint8_t *encptr;
uint8_t *decptr;
uint8_t *refptr;
uint8_t *iv;
uint64_t ivnum[2];
int size = 0;
int i = 0;
int rc = 0;
assert(swap_crypt_xts_tested == FALSE);
/*
* Validate the encryption algorithms.
*
* First initialize the test data.
*/
for (i = 0; i < 4096; i++) {
swap_crypt_test_page_ref[i] = (char) i;
}
ivnum[0] = (uint64_t)0xaa;
ivnum[1] = 0;
iv = (uint8_t *)ivnum;
refptr = (uint8_t *)swap_crypt_test_page_ref;
encptr = (uint8_t *)swap_crypt_test_page_encrypt;
decptr = (uint8_t *)swap_crypt_test_page_decrypt;
size = 4096;
/* encrypt */
rc = xts_encrypt(refptr, size, encptr, iv, &xts_modectx);
assert(!rc);
/* compare result with original - should NOT match */
for (i = 0; i < 4096; i++) {
if (swap_crypt_test_page_encrypt[i] !=
swap_crypt_test_page_ref[i]) {
break;
}
}
assert(i != 4096);
/* decrypt */
rc = xts_decrypt(encptr, size, decptr, iv, &xts_modectx);
assert(!rc);
/* compare result with original */
for (i = 0; i < 4096; i++) {
if (swap_crypt_test_page_decrypt[i] !=
swap_crypt_test_page_ref[i]) {
panic("encryption test failed");
}
}
/* encrypt in place */
rc = xts_encrypt(decptr, size, decptr, iv, &xts_modectx);
assert(!rc);
/* decrypt in place */
rc = xts_decrypt(decptr, size, decptr, iv, &xts_modectx);
assert(!rc);
for (i = 0; i < 4096; i++) {
if (swap_crypt_test_page_decrypt[i] !=
swap_crypt_test_page_ref[i]) {
panic("in place encryption test failed");
}
}
swap_crypt_xts_tested = TRUE;
#endif /* DEVELOPMENT || DEBUG */
}
void
vm_swap_encrypt(c_segment_t c_seg)
{
uint8_t *ptr;
uint8_t *iv;
uint64_t ivnum[2];
int size = 0;
int rc = 0;
if (swap_crypt_initialized == FALSE) {
swap_crypt_initialize();
}
#if DEVELOPMENT || DEBUG
C_SEG_MAKE_WRITEABLE(c_seg);
#endif
ptr = (uint8_t *)c_seg->c_store.c_buffer;
size = round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset));
ivnum[0] = (uint64_t)c_seg;
ivnum[1] = 0;
iv = (uint8_t *)ivnum;
rc = xts_encrypt(ptr, size, ptr, iv, &xts_modectx);
assert(!rc);
vm_page_encrypt_counter += (size / PAGE_SIZE_64);
#if DEVELOPMENT || DEBUG
C_SEG_WRITE_PROTECT(c_seg);
#endif
}
void
vm_swap_decrypt(c_segment_t c_seg)
{
uint8_t *ptr;
uint8_t *iv;
uint64_t ivnum[2];
int size = 0;
int rc = 0;
assert(swap_crypt_initialized);
#if DEVELOPMENT || DEBUG
C_SEG_MAKE_WRITEABLE(c_seg);
#endif
ptr = (uint8_t *)c_seg->c_store.c_buffer;
size = round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset));
ivnum[0] = (uint64_t)c_seg;
ivnum[1] = 0;
iv = (uint8_t *)ivnum;
rc = xts_decrypt(ptr, size, ptr, iv, &xts_modectx);
assert(!rc);
vm_page_decrypt_counter += (size / PAGE_SIZE_64);
#if DEVELOPMENT || DEBUG
C_SEG_WRITE_PROTECT(c_seg);
#endif
}
#endif /* ENCRYPTED_SWAP */
void
vm_compressor_swap_init()
{
thread_t thread = NULL;
queue_init(&swf_global_queue);
if (kernel_thread_start_priority((thread_continue_t)vm_swapout_thread, NULL,
BASEPRI_VM, &thread) != KERN_SUCCESS) {
panic("vm_swapout_thread: create failed");
}
thread_set_thread_name(thread, "VM_swapout");
vm_swapout_thread_id = thread->thread_id;
thread_deallocate(thread);
if (kernel_thread_start_priority((thread_continue_t)vm_swapfile_create_thread, NULL,
BASEPRI_VM, &thread) != KERN_SUCCESS) {
panic("vm_swapfile_create_thread: create failed");
}
thread_set_thread_name(thread, "VM_swapfile_create");
thread_deallocate(thread);
if (kernel_thread_start_priority((thread_continue_t)vm_swapfile_gc_thread, NULL,
BASEPRI_VM, &thread) != KERN_SUCCESS) {
panic("vm_swapfile_gc_thread: create failed");
}
thread_set_thread_name(thread, "VM_swapfile_gc");
/*
* Swapfile garbage collection will need to allocate memory
* to complete its swap reclaim and in-memory compaction.
* So allow it to dip into the reserved VM page pool.
*/
thread_lock(thread);
thread->options |= TH_OPT_VMPRIV;
thread_unlock(thread);
thread_deallocate(thread);
proc_set_thread_policy_with_tid(kernel_task, thread->thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_IO, THROTTLE_LEVEL_COMPRESSOR_TIER2);
proc_set_thread_policy_with_tid(kernel_task, thread->thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
#if !XNU_TARGET_OS_OSX
/*
* dummy value until the swap file gets created
* when we drive the first c_segment_t to the
* swapout queue... at that time we will
* know the true size we have to work with
*/
c_overage_swapped_limit = 16;
#endif /* !XNU_TARGET_OS_OSX */
vm_num_swap_files_config = VM_MAX_SWAP_FILE_NUM;
#if DEVELOPMENT || DEBUG
typeof(vm_num_swap_files_config) parsed_vm_max_num_swap_files = 0;
if (PE_parse_boot_argn("vm_max_num_swap_files", &parsed_vm_max_num_swap_files, sizeof(parsed_vm_max_num_swap_files))) {
if (parsed_vm_max_num_swap_files > 0) {
vm_num_swap_files_config = parsed_vm_max_num_swap_files;
} else {
printf("WARNING: Ignoring vm_max_num_swap_files=%d boot-arg. Value must be > 0\n", parsed_vm_max_num_swap_files);
}
}
#endif
printf("Maximum number of VM swap files: %d\n", vm_num_swap_files_config);
printf("VM Swap Subsystem is ON\n");
}
#if RECORD_THE_COMPRESSED_DATA
void
c_compressed_record_init()
{
if (c_compressed_record_init_done == FALSE) {
vm_swapfile_open("/tmp/compressed_data", &c_compressed_record_vp);
c_compressed_record_init_done = TRUE;
}
}
void
c_compressed_record_write(char *buf, int size)
{
if (c_compressed_record_write_error == 0) {
c_compressed_record_write_error = vm_record_file_write(c_compressed_record_vp, c_compressed_record_file_offset, buf, size);
c_compressed_record_file_offset += size;
}
}
#endif
int compaction_swapper_inited = 0;
void
vm_compaction_swapper_do_init(void)
{
struct vnode *vp;
char *pathname;
int namelen;
if (compaction_swapper_inited) {
return;
}
if (vm_compressor_mode != VM_PAGER_COMPRESSOR_WITH_SWAP) {
compaction_swapper_inited = 1;
return;
}
lck_mtx_lock(&vm_swap_data_lock);
if (!compaction_swapper_inited) {
namelen = (int)strlen(swapfilename) + SWAPFILENAME_INDEX_LEN + 1;
pathname = kheap_alloc(KHEAP_TEMP, namelen, Z_WAITOK | Z_ZERO);
snprintf(pathname, namelen, "%s%d", swapfilename, 0);
vm_swapfile_open(pathname, &vp);
if (vp) {
if (vnode_pager_isSSD(vp) == FALSE) {
/*
* swap files live on an HDD, so let's make sure to start swapping
* much earlier since we're not worried about SSD write-wear and
* we have so little write bandwidth to work with
* these values were derived expermentially by running the performance
* teams stock test for evaluating HDD performance against various
* combinations and looking and comparing overall results.
* Note that the > relationship between these 4 values must be maintained
*/
if (vm_compressor_minorcompact_threshold_divisor_overridden == 0) {
vm_compressor_minorcompact_threshold_divisor = 15;
}
if (vm_compressor_majorcompact_threshold_divisor_overridden == 0) {
vm_compressor_majorcompact_threshold_divisor = 18;
}
if (vm_compressor_unthrottle_threshold_divisor_overridden == 0) {
vm_compressor_unthrottle_threshold_divisor = 24;
}
if (vm_compressor_catchup_threshold_divisor_overridden == 0) {
vm_compressor_catchup_threshold_divisor = 30;
}
}
#if XNU_TARGET_OS_OSX
vnode_setswapmount(vp);
vm_swappin_avail = vnode_getswappin_avail(vp);
if (vm_swappin_avail) {
vm_swappin_enabled = TRUE;
}
#endif /* XNU_TARGET_OS_OSX */
vm_swapfile_close((uint64_t)pathname, vp);
}
kheap_free(KHEAP_TEMP, pathname, namelen);
compaction_swapper_inited = 1;
}
lck_mtx_unlock(&vm_swap_data_lock);
}
void
vm_swap_consider_defragmenting(int flags)
{
boolean_t force_defrag = (flags & VM_SWAP_FLAGS_FORCE_DEFRAG);
boolean_t force_reclaim = (flags & VM_SWAP_FLAGS_FORCE_RECLAIM);
if (compressor_store_stop_compaction == FALSE && !VM_SWAP_BUSY() &&
(force_defrag || force_reclaim || VM_SWAP_SHOULD_DEFRAGMENT() || VM_SWAP_SHOULD_RECLAIM())) {
if (!vm_swapfile_gc_thread_running || force_defrag || force_reclaim) {
lck_mtx_lock(&vm_swap_data_lock);
if (force_defrag) {
vm_swap_force_defrag = TRUE;
}
if (force_reclaim) {
vm_swap_force_reclaim = TRUE;
}
if (!vm_swapfile_gc_thread_running) {
thread_wakeup((event_t) &vm_swapfile_gc_needed);
}
lck_mtx_unlock(&vm_swap_data_lock);
}
}
}
int vm_swap_defragment_yielded = 0;
int vm_swap_defragment_swapin = 0;
int vm_swap_defragment_free = 0;
int vm_swap_defragment_busy = 0;
#if CONFIG_FREEZE
extern uint32_t c_segment_pages_compressed_incore;
extern uint32_t c_segment_pages_compressed_nearing_limit;
extern uint32_t c_segment_count;
extern uint32_t c_segments_nearing_limit;
boolean_t memorystatus_kill_on_VM_compressor_space_shortage(boolean_t);
extern bool freezer_incore_cseg_acct;
#endif /* CONFIG_FREEZE */
static void
vm_swap_defragment()
{
c_segment_t c_seg;
/*
* have to grab the master lock w/o holding
* any locks in spin mode
*/
PAGE_REPLACEMENT_DISALLOWED(TRUE);
lck_mtx_lock_spin_always(c_list_lock);
while (!queue_empty(&c_swappedout_sparse_list_head)) {
if (compressor_store_stop_compaction == TRUE || VM_SWAP_BUSY()) {
vm_swap_defragment_yielded++;
break;
}
c_seg = (c_segment_t)queue_first(&c_swappedout_sparse_list_head);
lck_mtx_lock_spin_always(&c_seg->c_lock);
assert(c_seg->c_state == C_ON_SWAPPEDOUTSPARSE_Q);
if (c_seg->c_busy) {
lck_mtx_unlock_always(c_list_lock);
PAGE_REPLACEMENT_DISALLOWED(FALSE);
/*
* c_seg_wait_on_busy consumes c_seg->c_lock
*/
c_seg_wait_on_busy(c_seg);
PAGE_REPLACEMENT_DISALLOWED(TRUE);
lck_mtx_lock_spin_always(c_list_lock);
vm_swap_defragment_busy++;
continue;
}
if (c_seg->c_bytes_used == 0) {
/*
* c_seg_free_locked consumes the c_list_lock
* and c_seg->c_lock
*/
C_SEG_BUSY(c_seg);
c_seg_free_locked(c_seg);
vm_swap_defragment_free++;
} else {
lck_mtx_unlock_always(c_list_lock);
#if CONFIG_FREEZE
if (freezer_incore_cseg_acct) {
if ((c_seg->c_slots_used + c_segment_pages_compressed_incore) >= c_segment_pages_compressed_nearing_limit) {
memorystatus_kill_on_VM_compressor_space_shortage(TRUE /* async */);
}
uint32_t incore_seg_count = c_segment_count - c_swappedout_count - c_swappedout_sparse_count;
if ((incore_seg_count + 1) >= c_segments_nearing_limit) {
memorystatus_kill_on_VM_compressor_space_shortage(TRUE /* async */);
}
}
#endif /* CONFIG_FREEZE */
if (c_seg_swapin(c_seg, TRUE, FALSE) == 0) {
lck_mtx_unlock_always(&c_seg->c_lock);
vmcs_stats.defrag_swapins += (round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset))) >> PAGE_SHIFT;
}
vm_swap_defragment_swapin++;
}
PAGE_REPLACEMENT_DISALLOWED(FALSE);
vm_pageout_io_throttle();
/*
* because write waiters have privilege over readers,
* dropping and immediately retaking the master lock will
* still allow any thread waiting to acquire the
* master lock exclusively an opportunity to take it
*/
PAGE_REPLACEMENT_DISALLOWED(TRUE);
lck_mtx_lock_spin_always(c_list_lock);
}
lck_mtx_unlock_always(c_list_lock);
PAGE_REPLACEMENT_DISALLOWED(FALSE);
}
static void
vm_swapfile_create_thread(void)
{
clock_sec_t sec;
clock_nsec_t nsec;
current_thread()->options |= TH_OPT_VMPRIV;
vm_swapfile_create_thread_awakened++;
vm_swapfile_create_thread_running = 1;
while (TRUE) {
/*
* walk through the list of swap files
* and do the delayed frees/trims for
* any swap file whose count of delayed
* frees is above the batch limit
*/
vm_swap_handle_delayed_trims(FALSE);
lck_mtx_lock(&vm_swap_data_lock);
if (hibernate_in_progress_with_pinned_swap == TRUE) {
break;
}
if (compressor_store_stop_compaction == TRUE) {
break;
}
clock_get_system_nanotime(&sec, &nsec);
if (VM_SWAP_SHOULD_CREATE(sec) == 0) {
break;
}
lck_mtx_unlock(&vm_swap_data_lock);
if (vm_swap_create_file() == FALSE) {
vm_swapfile_last_failed_to_create_ts = sec;
HIBLOG("vm_swap_create_file failed @ %lu secs\n", (unsigned long)sec);
} else {
vm_swapfile_last_successful_create_ts = sec;
}
}
vm_swapfile_create_thread_running = 0;
if (hibernate_in_progress_with_pinned_swap == TRUE) {
thread_wakeup((event_t)&hibernate_in_progress_with_pinned_swap);
}
if (compressor_store_stop_compaction == TRUE) {
thread_wakeup((event_t)&compressor_store_stop_compaction);
}
assert_wait((event_t)&vm_swapfile_create_needed, THREAD_UNINT);
lck_mtx_unlock(&vm_swap_data_lock);
thread_block((thread_continue_t)vm_swapfile_create_thread);
/* NOTREACHED */
}
#if HIBERNATION
kern_return_t
hibernate_pin_swap(boolean_t start)
{
vm_compaction_swapper_do_init();
if (start == FALSE) {
lck_mtx_lock(&vm_swap_data_lock);
hibernate_in_progress_with_pinned_swap = FALSE;
lck_mtx_unlock(&vm_swap_data_lock);
return KERN_SUCCESS;
}
if (vm_swappin_enabled == FALSE) {
return KERN_SUCCESS;
}
lck_mtx_lock(&vm_swap_data_lock);
hibernate_in_progress_with_pinned_swap = TRUE;
while (vm_swapfile_create_thread_running || vm_swapfile_gc_thread_running) {
assert_wait((event_t)&hibernate_in_progress_with_pinned_swap, THREAD_UNINT);
lck_mtx_unlock(&vm_swap_data_lock);
thread_block(THREAD_CONTINUE_NULL);
lck_mtx_lock(&vm_swap_data_lock);
}
if (vm_num_swap_files > vm_num_pinned_swap_files) {
hibernate_in_progress_with_pinned_swap = FALSE;
lck_mtx_unlock(&vm_swap_data_lock);
HIBLOG("hibernate_pin_swap failed - vm_num_swap_files = %d, vm_num_pinned_swap_files = %d\n",
vm_num_swap_files, vm_num_pinned_swap_files);
return KERN_FAILURE;
}
lck_mtx_unlock(&vm_swap_data_lock);
while (VM_SWAP_SHOULD_PIN(MAX_SWAP_FILE_SIZE)) {
if (vm_swap_create_file() == FALSE) {
break;
}
}
return KERN_SUCCESS;
}
#endif
static void
vm_swapfile_gc_thread(void)
{
boolean_t need_defragment;
boolean_t need_reclaim;
vm_swapfile_gc_thread_awakened++;
vm_swapfile_gc_thread_running = 1;
while (TRUE) {
lck_mtx_lock(&vm_swap_data_lock);
if (hibernate_in_progress_with_pinned_swap == TRUE) {
break;
}
if (VM_SWAP_BUSY() || compressor_store_stop_compaction == TRUE) {
break;
}
need_defragment = FALSE;
need_reclaim = FALSE;
if (VM_SWAP_SHOULD_DEFRAGMENT()) {
need_defragment = TRUE;
}
if (VM_SWAP_SHOULD_RECLAIM()) {
need_defragment = TRUE;
need_reclaim = TRUE;
}
if (need_defragment == FALSE && need_reclaim == FALSE) {
break;
}
vm_swap_force_defrag = FALSE;
vm_swap_force_reclaim = FALSE;
lck_mtx_unlock(&vm_swap_data_lock);
if (need_defragment == TRUE) {
vm_swap_defragment();
}
if (need_reclaim == TRUE) {
vm_swap_reclaim();
}
}
vm_swapfile_gc_thread_running = 0;
if (hibernate_in_progress_with_pinned_swap == TRUE) {
thread_wakeup((event_t)&hibernate_in_progress_with_pinned_swap);
}
if (compressor_store_stop_compaction == TRUE) {
thread_wakeup((event_t)&compressor_store_stop_compaction);
}
assert_wait((event_t)&vm_swapfile_gc_needed, THREAD_UNINT);
lck_mtx_unlock(&vm_swap_data_lock);
thread_block((thread_continue_t)vm_swapfile_gc_thread);
/* NOTREACHED */
}
#define VM_SWAPOUT_LIMIT_T2P 4
#define VM_SWAPOUT_LIMIT_T1P 4
#define VM_SWAPOUT_LIMIT_T0P 6
#define VM_SWAPOUT_LIMIT_T0 8
#define VM_SWAPOUT_LIMIT_MAX 8
#define VM_SWAPOUT_START 0
#define VM_SWAPOUT_T2_PASSIVE 1
#define VM_SWAPOUT_T1_PASSIVE 2
#define VM_SWAPOUT_T0_PASSIVE 3
#define VM_SWAPOUT_T0 4
int vm_swapout_state = VM_SWAPOUT_START;
int vm_swapout_limit = 1;
int vm_swapper_entered_T0 = 0;
int vm_swapper_entered_T0P = 0;
int vm_swapper_entered_T1P = 0;
int vm_swapper_entered_T2P = 0;
static void
vm_swapout_thread_throttle_adjust(void)
{
switch (vm_swapout_state) {
case VM_SWAPOUT_START:
vm_swapper_throttle = THROTTLE_LEVEL_COMPRESSOR_TIER2;
vm_swapper_entered_T2P++;
proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_IO, vm_swapper_throttle);
proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
vm_swapout_limit = VM_SWAPOUT_LIMIT_T2P;
vm_swapout_state = VM_SWAPOUT_T2_PASSIVE;
break;
case VM_SWAPOUT_T2_PASSIVE:
if (SWAPPER_NEEDS_TO_UNTHROTTLE()) {
vm_swapper_throttle = THROTTLE_LEVEL_COMPRESSOR_TIER0;
vm_swapper_entered_T0P++;
proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_IO, vm_swapper_throttle);
proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
vm_swapout_limit = VM_SWAPOUT_LIMIT_T0P;
vm_swapout_state = VM_SWAPOUT_T0_PASSIVE;
break;
}
if (swapout_target_age || hibernate_flushing == TRUE) {
vm_swapper_throttle = THROTTLE_LEVEL_COMPRESSOR_TIER1;
vm_swapper_entered_T1P++;
proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_IO, vm_swapper_throttle);
proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
vm_swapout_limit = VM_SWAPOUT_LIMIT_T1P;
vm_swapout_state = VM_SWAPOUT_T1_PASSIVE;
}
break;
case VM_SWAPOUT_T1_PASSIVE:
if (SWAPPER_NEEDS_TO_UNTHROTTLE()) {
vm_swapper_throttle = THROTTLE_LEVEL_COMPRESSOR_TIER0;
vm_swapper_entered_T0P++;
proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_IO, vm_swapper_throttle);
proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
vm_swapout_limit = VM_SWAPOUT_LIMIT_T0P;
vm_swapout_state = VM_SWAPOUT_T0_PASSIVE;
break;
}
if (swapout_target_age == 0 && hibernate_flushing == FALSE) {
vm_swapper_throttle = THROTTLE_LEVEL_COMPRESSOR_TIER2;
vm_swapper_entered_T2P++;
proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_IO, vm_swapper_throttle);
proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
vm_swapout_limit = VM_SWAPOUT_LIMIT_T2P;
vm_swapout_state = VM_SWAPOUT_T2_PASSIVE;
}
break;
case VM_SWAPOUT_T0_PASSIVE:
if (SWAPPER_NEEDS_TO_RETHROTTLE()) {
vm_swapper_throttle = THROTTLE_LEVEL_COMPRESSOR_TIER2;
vm_swapper_entered_T2P++;
proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_IO, vm_swapper_throttle);
proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE);
vm_swapout_limit = VM_SWAPOUT_LIMIT_T2P;
vm_swapout_state = VM_SWAPOUT_T2_PASSIVE;
break;
}
if (SWAPPER_NEEDS_TO_CATCHUP()) {
vm_swapper_entered_T0++;
proc_set_thread_policy_with_tid(kernel_task, vm_swapout_thread_id,
TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_DISABLE);