]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_compressor.c
64b7d7bd9be50829648fa8d1641435c6b86fb8eb
[apple/xnu.git] / osfmk / vm / vm_compressor.c
1 /*
2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <vm/vm_compressor.h>
30
31 #if CONFIG_PHANTOM_CACHE
32 #include <vm/vm_phantom_cache.h>
33 #endif
34
35 #include <vm/vm_map.h>
36 #include <vm/vm_pageout.h>
37 #include <vm/memory_object.h>
38 #include <mach/mach_host.h> /* for host_info() */
39 #include <kern/ledger.h>
40
41 #include <i386/misc_protos.h>
42
43 #include <default_pager/default_pager_alerts.h>
44 #include <default_pager/default_pager_object_server.h>
45
46 #include <IOKit/IOHibernatePrivate.h>
47
48 /*
49 * vm_compressor_mode has a heirarchy of control to set its value.
50 * boot-args are checked first, then device-tree, and finally
51 * the default value that is defined below. See vm_fault_init() for
52 * the boot-arg & device-tree code.
53 */
54
55
56 int vm_compressor_mode = VM_PAGER_COMPRESSOR_WITH_SWAP;
57 int vm_scale = 16;
58
59
60 int vm_compressor_is_active = 0;
61 int vm_compression_limit = 0;
62 int vm_compressor_available = 0;
63
64 extern boolean_t vm_swap_up;
65 extern void vm_pageout_io_throttle(void);
66 extern int not_in_kdp;
67
68 #if CHECKSUM_THE_DATA || CHECKSUM_THE_SWAP || CHECKSUM_THE_COMPRESSED_DATA
69 extern unsigned int hash_string(char *cp, int len);
70 #endif
71
72 #define UNPACK_C_SIZE(cs) ((cs->c_size == (PAGE_SIZE-1)) ? PAGE_SIZE : cs->c_size)
73 #define PACK_C_SIZE(cs, size) (cs->c_size = ((size == PAGE_SIZE) ? PAGE_SIZE - 1 : size))
74
75
76 struct c_sv_hash_entry {
77 union {
78 struct {
79 uint32_t c_sv_he_ref;
80 uint32_t c_sv_he_data;
81 } c_sv_he;
82 uint64_t c_sv_he_record;
83
84 } c_sv_he_un;
85 };
86
87 #define he_ref c_sv_he_un.c_sv_he.c_sv_he_ref
88 #define he_data c_sv_he_un.c_sv_he.c_sv_he_data
89 #define he_record c_sv_he_un.c_sv_he_record
90
91 #define C_SV_HASH_MAX_MISS 32
92 #define C_SV_HASH_SIZE ((1 << 10))
93 #define C_SV_HASH_MASK ((1 << 10) - 1)
94 #define C_SV_CSEG_ID ((1 << 22) - 1)
95
96
97 struct c_slot_mapping {
98 uint32_t s_cseg:22, /* segment number + 1 */
99 s_cindx:10; /* index in the segment */
100 };
101 #define C_SLOT_MAX_INDEX (1 << 10)
102
103 typedef struct c_slot_mapping *c_slot_mapping_t;
104
105
106 union c_segu {
107 c_segment_t c_seg;
108 uint32_t c_segno;
109 };
110
111
112
113 #define C_SLOT_PACK_PTR(ptr) (((uintptr_t)ptr - (uintptr_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS) >> 2)
114 #define C_SLOT_UNPACK_PTR(cslot) ((uintptr_t)(cslot->c_packed_ptr << 2) + (uintptr_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS)
115
116
117 uint32_t c_segment_count = 0;
118 uint32_t c_segment_count_max = 0;
119
120 uint64_t c_generation_id = 0;
121 uint64_t c_generation_id_flush_barrier;
122
123
124 #define HIBERNATE_FLUSHING_SECS_TO_COMPLETE 120
125
126 boolean_t hibernate_no_swapspace = FALSE;
127 clock_sec_t hibernate_flushing_deadline = 0;
128
129
130 #if RECORD_THE_COMPRESSED_DATA
131 char *c_compressed_record_sbuf;
132 char *c_compressed_record_ebuf;
133 char *c_compressed_record_cptr;
134 #endif
135
136
137 queue_head_t c_age_list_head;
138 queue_head_t c_swapout_list_head;
139 queue_head_t c_swappedin_list_head;
140 queue_head_t c_swappedout_list_head;
141 queue_head_t c_swappedout_sparse_list_head;
142 queue_head_t c_major_list_head;
143 queue_head_t c_filling_list_head;
144 queue_head_t c_bad_list_head;
145
146 uint32_t c_age_count = 0;
147 uint32_t c_swapout_count = 0;
148 uint32_t c_swappedin_count = 0;
149 uint32_t c_swappedout_count = 0;
150 uint32_t c_swappedout_sparse_count = 0;
151 uint32_t c_major_count = 0;
152 uint32_t c_filling_count = 0;
153 uint32_t c_empty_count = 0;
154 uint32_t c_bad_count = 0;
155
156
157 queue_head_t c_minor_list_head;
158 uint32_t c_minor_count = 0;
159
160 int c_overage_swapped_count = 0;
161 int c_overage_swapped_limit = 0;
162
163 int c_seg_fixed_array_len;
164 union c_segu *c_segments;
165 vm_offset_t c_buffers;
166 vm_size_t c_buffers_size;
167 caddr_t c_segments_next_page;
168 boolean_t c_segments_busy;
169 uint32_t c_segments_available;
170 uint32_t c_segments_limit;
171 uint32_t c_segments_nearing_limit;
172
173 uint32_t c_segment_svp_in_hash;
174 uint32_t c_segment_svp_hash_succeeded;
175 uint32_t c_segment_svp_hash_failed;
176 uint32_t c_segment_svp_zero_compressions;
177 uint32_t c_segment_svp_nonzero_compressions;
178 uint32_t c_segment_svp_zero_decompressions;
179 uint32_t c_segment_svp_nonzero_decompressions;
180
181 uint32_t c_segment_noncompressible_pages;
182
183 uint32_t c_segment_pages_compressed;
184 uint32_t c_segment_pages_compressed_limit;
185 uint32_t c_segment_pages_compressed_nearing_limit;
186 uint32_t c_free_segno_head = (uint32_t)-1;
187
188 uint32_t vm_compressor_minorcompact_threshold_divisor = 10;
189 uint32_t vm_compressor_majorcompact_threshold_divisor = 10;
190 uint32_t vm_compressor_unthrottle_threshold_divisor = 10;
191 uint32_t vm_compressor_catchup_threshold_divisor = 10;
192
193 #define C_SEGMENTS_PER_PAGE (PAGE_SIZE / sizeof(union c_segu))
194
195
196 lck_grp_attr_t vm_compressor_lck_grp_attr;
197 lck_attr_t vm_compressor_lck_attr;
198 lck_grp_t vm_compressor_lck_grp;
199
200 #if __i386__ || __x86_64__
201 lck_mtx_t *c_list_lock;
202 #else /* __i386__ || __x86_64__ */
203 lck_spin_t *c_list_lock;
204 #endif /* __i386__ || __x86_64__ */
205
206 lck_rw_t c_master_lock;
207 boolean_t decompressions_blocked = FALSE;
208
209 zone_t compressor_segment_zone;
210 int c_compressor_swap_trigger = 0;
211
212 uint32_t compressor_cpus;
213 char *compressor_scratch_bufs;
214 char *kdp_compressor_scratch_buf;
215 char *kdp_compressor_decompressed_page;
216 addr64_t kdp_compressor_decompressed_page_paddr;
217 ppnum_t kdp_compressor_decompressed_page_ppnum;
218
219 clock_sec_t start_of_sample_period_sec = 0;
220 clock_nsec_t start_of_sample_period_nsec = 0;
221 clock_sec_t start_of_eval_period_sec = 0;
222 clock_nsec_t start_of_eval_period_nsec = 0;
223 uint32_t sample_period_decompression_count = 0;
224 uint32_t sample_period_compression_count = 0;
225 uint32_t last_eval_decompression_count = 0;
226 uint32_t last_eval_compression_count = 0;
227
228 #define DECOMPRESSION_SAMPLE_MAX_AGE (60 * 30)
229
230 boolean_t vm_swapout_ripe_segments = FALSE;
231 uint32_t vm_ripe_target_age = (60 * 60 * 48);
232
233 uint32_t swapout_target_age = 0;
234 uint32_t age_of_decompressions_during_sample_period[DECOMPRESSION_SAMPLE_MAX_AGE];
235 uint32_t overage_decompressions_during_sample_period = 0;
236
237 void do_fastwake_warmup(void);
238 boolean_t fastwake_warmup = FALSE;
239 boolean_t fastwake_recording_in_progress = FALSE;
240 clock_sec_t dont_trim_until_ts = 0;
241
242 uint64_t c_segment_warmup_count;
243 uint64_t first_c_segment_to_warm_generation_id = 0;
244 uint64_t last_c_segment_to_warm_generation_id = 0;
245 boolean_t hibernate_flushing = FALSE;
246
247 int64_t c_segment_input_bytes __attribute__((aligned(8))) = 0;
248 int64_t c_segment_compressed_bytes __attribute__((aligned(8))) = 0;
249 int64_t compressor_bytes_used __attribute__((aligned(8))) = 0;
250
251
252 struct c_sv_hash_entry c_segment_sv_hash_table[C_SV_HASH_SIZE] __attribute__ ((aligned (8)));
253
254
255 static boolean_t compressor_needs_to_swap(void);
256 static void vm_compressor_swap_trigger_thread(void);
257 static void vm_compressor_do_delayed_compactions(boolean_t);
258 static void vm_compressor_compact_and_swap(boolean_t);
259 static void vm_compressor_age_swapped_in_segments(boolean_t);
260
261 static void vm_compressor_take_paging_space_action(void);
262
263 boolean_t vm_compressor_low_on_space(void);
264
265 void compute_swapout_target_age(void);
266
267 boolean_t c_seg_major_compact(c_segment_t, c_segment_t);
268 boolean_t c_seg_major_compact_ok(c_segment_t, c_segment_t);
269
270 int c_seg_minor_compaction_and_unlock(c_segment_t, boolean_t);
271 int c_seg_do_minor_compaction_and_unlock(c_segment_t, boolean_t, boolean_t, boolean_t);
272 void c_seg_try_minor_compaction_and_unlock(c_segment_t c_seg);
273 void c_seg_need_delayed_compaction(c_segment_t);
274
275 void c_seg_move_to_sparse_list(c_segment_t);
276 void c_seg_insert_into_q(queue_head_t *, c_segment_t);
277
278 uint64_t vm_available_memory(void);
279 uint64_t vm_compressor_pages_compressed(void);
280
281 extern unsigned int dp_pages_free, dp_pages_reserve;
282
283 uint64_t
284 vm_available_memory(void)
285 {
286 return (((uint64_t)AVAILABLE_NON_COMPRESSED_MEMORY) * PAGE_SIZE_64);
287 }
288
289
290 uint64_t
291 vm_compressor_pages_compressed(void)
292 {
293 return (c_segment_pages_compressed * PAGE_SIZE_64);
294 }
295
296
297 boolean_t
298 vm_compression_available(void)
299 {
300 if ( !(COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE))
301 return (FALSE);
302
303 if (c_segments_available >= c_segments_limit || c_segment_pages_compressed >= c_segment_pages_compressed_limit)
304 return (FALSE);
305
306 return (TRUE);
307 }
308
309
310 boolean_t
311 vm_compressor_low_on_space(void)
312 {
313 if ((c_segment_pages_compressed > c_segment_pages_compressed_nearing_limit) ||
314 (c_segment_count > c_segments_nearing_limit))
315 return (TRUE);
316
317 return (FALSE);
318 }
319
320
321 int
322 vm_wants_task_throttled(task_t task)
323 {
324 if (task == kernel_task)
325 return (0);
326
327 if (COMPRESSED_PAGER_IS_SWAPLESS || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPLESS)
328 return (0);
329
330 if (COMPRESSED_PAGER_IS_SWAPBACKED || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED) {
331 if ((vm_compressor_low_on_space() || HARD_THROTTLE_LIMIT_REACHED()) &&
332 (unsigned int)pmap_compressed(task->map->pmap) > (c_segment_pages_compressed / 4))
333 return (1);
334 } else {
335 if (((dp_pages_free + dp_pages_reserve < 2000) && VM_DYNAMIC_PAGING_ENABLED(memory_manager_default)) &&
336 get_task_resident_size(task) > (((AVAILABLE_NON_COMPRESSED_MEMORY) * PAGE_SIZE) / 5))
337 return (1);
338 }
339 return (0);
340 }
341
342
343
344 static uint32_t no_paging_space_action_in_progress = 0;
345 extern void memorystatus_send_low_swap_note(void);
346
347 static void
348 vm_compressor_take_paging_space_action(void)
349 {
350 if (no_paging_space_action_in_progress == 0) {
351
352 if (OSCompareAndSwap(0, 1, (UInt32 *)&no_paging_space_action_in_progress)) {
353
354 if (no_paging_space_action()) {
355 memorystatus_send_low_swap_note();
356 }
357
358 no_paging_space_action_in_progress = 0;
359 }
360 }
361 }
362
363
364
365 void
366 vm_compressor_init_locks(void)
367 {
368 lck_grp_attr_setdefault(&vm_compressor_lck_grp_attr);
369 lck_grp_init(&vm_compressor_lck_grp, "vm_compressor", &vm_compressor_lck_grp_attr);
370 lck_attr_setdefault(&vm_compressor_lck_attr);
371
372 lck_rw_init(&c_master_lock, &vm_compressor_lck_grp, &vm_compressor_lck_attr);
373 }
374
375
376 void
377 vm_decompressor_lock(void)
378 {
379 PAGE_REPLACEMENT_ALLOWED(TRUE);
380
381 decompressions_blocked = TRUE;
382
383 PAGE_REPLACEMENT_ALLOWED(FALSE);
384 }
385
386 void
387 vm_decompressor_unlock(void)
388 {
389 PAGE_REPLACEMENT_ALLOWED(TRUE);
390
391 decompressions_blocked = FALSE;
392
393 PAGE_REPLACEMENT_ALLOWED(FALSE);
394
395 thread_wakeup((event_t)&decompressions_blocked);
396 }
397
398
399
400 void
401 vm_compressor_init(void)
402 {
403 thread_t thread;
404 struct c_slot cs_dummy;
405 c_slot_t cs = &cs_dummy;
406 int c_segment_min_size;
407 int c_segment_padded_size;
408
409 /*
410 * ensure that any pointer that gets created from
411 * the vm_page zone can be packed properly
412 */
413 cs->c_packed_ptr = C_SLOT_PACK_PTR(zone_map_min_address);
414
415 if (C_SLOT_UNPACK_PTR(cs) != (uintptr_t)zone_map_min_address)
416 panic("C_SLOT_UNPACK_PTR failed on zone_map_min_address - %p", (void *)zone_map_min_address);
417
418 cs->c_packed_ptr = C_SLOT_PACK_PTR(zone_map_max_address);
419
420 if (C_SLOT_UNPACK_PTR(cs) != (uintptr_t)zone_map_max_address)
421 panic("C_SLOT_UNPACK_PTR failed on zone_map_max_address - %p", (void *)zone_map_max_address);
422
423
424 assert((C_SEGMENTS_PER_PAGE * sizeof(union c_segu)) == PAGE_SIZE);
425
426 PE_parse_boot_argn("vm_compression_limit", &vm_compression_limit, sizeof (vm_compression_limit));
427
428 if (max_mem <= (3ULL * 1024ULL * 1024ULL * 1024ULL)) {
429 vm_compressor_minorcompact_threshold_divisor = 11;
430 vm_compressor_majorcompact_threshold_divisor = 13;
431 vm_compressor_unthrottle_threshold_divisor = 20;
432 vm_compressor_catchup_threshold_divisor = 35;
433 } else {
434 vm_compressor_minorcompact_threshold_divisor = 20;
435 vm_compressor_majorcompact_threshold_divisor = 25;
436 vm_compressor_unthrottle_threshold_divisor = 35;
437 vm_compressor_catchup_threshold_divisor = 50;
438 }
439 /*
440 * vm_page_init_lck_grp is now responsible for calling vm_compressor_init_locks
441 * c_master_lock needs to be available early so that "vm_page_find_contiguous" can
442 * use PAGE_REPLACEMENT_ALLOWED to coordinate with the compressor.
443 */
444
445 #if __i386__ || __x86_64__
446 c_list_lock = lck_mtx_alloc_init(&vm_compressor_lck_grp, &vm_compressor_lck_attr);
447 #else /* __i386__ || __x86_64__ */
448 c_list_lock = lck_spin_alloc_init(&vm_compressor_lck_grp, &vm_compressor_lck_attr);
449 #endif /* __i386__ || __x86_64__ */
450
451
452 queue_init(&c_bad_list_head);
453 queue_init(&c_age_list_head);
454 queue_init(&c_minor_list_head);
455 queue_init(&c_major_list_head);
456 queue_init(&c_filling_list_head);
457 queue_init(&c_swapout_list_head);
458 queue_init(&c_swappedin_list_head);
459 queue_init(&c_swappedout_list_head);
460 queue_init(&c_swappedout_sparse_list_head);
461
462 c_segment_min_size = sizeof(struct c_segment) + (C_SEG_SLOT_VAR_ARRAY_MIN_LEN * sizeof(struct c_slot));
463
464 for (c_segment_padded_size = 128; c_segment_padded_size < c_segment_min_size; c_segment_padded_size = c_segment_padded_size << 1);
465
466 compressor_segment_zone = zinit(c_segment_padded_size, 128000 * c_segment_padded_size, PAGE_SIZE, "compressor_segment");
467 zone_change(compressor_segment_zone, Z_CALLERACCT, FALSE);
468 zone_change(compressor_segment_zone, Z_NOENCRYPT, TRUE);
469
470 c_seg_fixed_array_len = (c_segment_padded_size - sizeof(struct c_segment)) / sizeof(struct c_slot);
471
472 c_free_segno_head = -1;
473 c_segments_available = 0;
474
475 if (vm_compression_limit == 0) {
476 c_segment_pages_compressed_limit = (uint32_t)((max_mem / PAGE_SIZE)) * vm_scale;
477
478 #define OLD_SWAP_LIMIT (1024 * 1024 * 16)
479 #define MAX_SWAP_LIMIT (1024 * 1024 * 128)
480
481 if (c_segment_pages_compressed_limit > (OLD_SWAP_LIMIT))
482 c_segment_pages_compressed_limit = OLD_SWAP_LIMIT;
483
484 if (c_segment_pages_compressed_limit < (uint32_t)(max_mem / PAGE_SIZE_64))
485 c_segment_pages_compressed_limit = (uint32_t)(max_mem / PAGE_SIZE_64);
486 } else {
487 if (vm_compression_limit < MAX_SWAP_LIMIT)
488 c_segment_pages_compressed_limit = vm_compression_limit;
489 else
490 c_segment_pages_compressed_limit = MAX_SWAP_LIMIT;
491 }
492 if ((c_segments_limit = c_segment_pages_compressed_limit / (C_SEG_BUFSIZE / PAGE_SIZE)) > C_SEG_MAX_LIMIT)
493 c_segments_limit = C_SEG_MAX_LIMIT;
494
495 c_segment_pages_compressed_nearing_limit = (c_segment_pages_compressed_limit * 98) / 100;
496 c_segments_nearing_limit = (c_segments_limit * 98) / 100;
497
498 c_segments_busy = FALSE;
499
500 if (kernel_memory_allocate(kernel_map, (vm_offset_t *)(&c_segments), (sizeof(union c_segu) * c_segments_limit), 0, KMA_KOBJECT | KMA_VAONLY | KMA_PERMANENT, VM_KERN_MEMORY_COMPRESSOR) != KERN_SUCCESS)
501 panic("vm_compressor_init: kernel_memory_allocate failed - c_segments\n");
502 c_buffers_size = (vm_size_t)C_SEG_ALLOCSIZE * (vm_size_t)c_segments_limit;
503 if (kernel_memory_allocate(kernel_map, &c_buffers, c_buffers_size, 0, KMA_COMPRESSOR | KMA_VAONLY | KMA_PERMANENT, VM_KERN_MEMORY_COMPRESSOR) != KERN_SUCCESS)
504 panic("vm_compressor_init: kernel_memory_allocate failed - c_buffers\n");
505
506 c_segments_next_page = (caddr_t)c_segments;
507
508 {
509 host_basic_info_data_t hinfo;
510 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
511
512 #define BSD_HOST 1
513 host_info((host_t)BSD_HOST, HOST_BASIC_INFO, (host_info_t)&hinfo, &count);
514
515 compressor_cpus = hinfo.max_cpus;
516
517 compressor_scratch_bufs = kalloc_tag(compressor_cpus * WKdm_SCRATCH_BUF_SIZE, VM_KERN_MEMORY_COMPRESSOR);
518
519 kdp_compressor_scratch_buf = kalloc_tag(WKdm_SCRATCH_BUF_SIZE, VM_KERN_MEMORY_COMPRESSOR);
520 kdp_compressor_decompressed_page = kalloc_tag(PAGE_SIZE, VM_KERN_MEMORY_COMPRESSOR);
521 kdp_compressor_decompressed_page_paddr = kvtophys((vm_offset_t)kdp_compressor_decompressed_page);
522 kdp_compressor_decompressed_page_ppnum = (ppnum_t) atop(kdp_compressor_decompressed_page_paddr);
523 }
524 #if CONFIG_FREEZE
525 freezer_compressor_scratch_buf = kalloc_tag(WKdm_SCRATCH_BUF_SIZE, VM_KERN_MEMORY_COMPRESSOR);
526 #endif
527
528 #if RECORD_THE_COMPRESSED_DATA
529 if (kernel_memory_allocate(kernel_map, (vm_offset_t *)&c_compressed_record_sbuf, (vm_size_t)C_SEG_ALLOCSIZE + (PAGE_SIZE * 2), 0, KMA_KOBJECT, VM_KERN_MEMORY_COMPRESSOR) != KERN_SUCCESS)
530 panic("vm_compressor_init: kernel_memory_allocate failed - c_compressed_record_sbuf\n");
531
532 c_compressed_record_cptr = c_compressed_record_sbuf;
533 c_compressed_record_ebuf = c_compressed_record_sbuf + C_SEG_ALLOCSIZE + (PAGE_SIZE * 2);
534 #endif
535
536 if (kernel_thread_start_priority((thread_continue_t)vm_compressor_swap_trigger_thread, NULL,
537 BASEPRI_PREEMPT - 1, &thread) != KERN_SUCCESS) {
538 panic("vm_compressor_swap_trigger_thread: create failed");
539 }
540 thread_deallocate(thread);
541
542 assert(default_pager_init_flag == 0);
543
544 if (vm_pageout_internal_start() != KERN_SUCCESS) {
545 panic("vm_compressor_init: Failed to start the internal pageout thread.\n");
546 }
547 if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE)
548 vm_compressor_swap_init();
549
550 if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED)
551 vm_compressor_is_active = 1;
552
553 #if CONFIG_FREEZE
554 memorystatus_freeze_enabled = TRUE;
555 #endif /* CONFIG_FREEZE */
556
557 default_pager_init_flag = 1;
558 vm_compressor_available = 1;
559
560 vm_page_reactivate_all_throttled();
561 }
562
563
564 #if VALIDATE_C_SEGMENTS
565
566 static void
567 c_seg_validate(c_segment_t c_seg, boolean_t must_be_compact)
568 {
569 int c_indx;
570 int32_t bytes_used;
571 int32_t bytes_unused;
572 uint32_t c_rounded_size;
573 uint32_t c_size;
574 c_slot_t cs;
575
576 if (c_seg->c_firstemptyslot < c_seg->c_nextslot) {
577 c_indx = c_seg->c_firstemptyslot;
578 cs = C_SEG_SLOT_FROM_INDEX(c_seg, c_indx);
579
580 if (cs == NULL)
581 panic("c_seg_validate: no slot backing c_firstemptyslot");
582
583 if (cs->c_size)
584 panic("c_seg_validate: c_firstemptyslot has non-zero size (%d)\n", cs->c_size);
585 }
586 bytes_used = 0;
587 bytes_unused = 0;
588
589 for (c_indx = 0; c_indx < c_seg->c_nextslot; c_indx++) {
590
591 cs = C_SEG_SLOT_FROM_INDEX(c_seg, c_indx);
592
593 c_size = UNPACK_C_SIZE(cs);
594
595 c_rounded_size = (c_size + C_SEG_OFFSET_ALIGNMENT_MASK) & ~C_SEG_OFFSET_ALIGNMENT_MASK;
596
597 bytes_used += c_rounded_size;
598
599 #if CHECKSUM_THE_COMPRESSED_DATA
600 if (c_size && cs->c_hash_compressed_data != hash_string((char *)&c_seg->c_store.c_buffer[cs->c_offset], c_size))
601 panic("compressed data doesn't match original");
602 #endif
603 }
604
605 if (bytes_used != c_seg->c_bytes_used)
606 panic("c_seg_validate: bytes_used mismatch - found %d, segment has %d\n", bytes_used, c_seg->c_bytes_used);
607
608 if (c_seg->c_bytes_used > C_SEG_OFFSET_TO_BYTES((int32_t)c_seg->c_nextoffset))
609 panic("c_seg_validate: c_bytes_used > c_nextoffset - c_nextoffset = %d, c_bytes_used = %d\n",
610 (int32_t)C_SEG_OFFSET_TO_BYTES((int32_t)c_seg->c_nextoffset), c_seg->c_bytes_used);
611
612 if (must_be_compact) {
613 if (c_seg->c_bytes_used != C_SEG_OFFSET_TO_BYTES((int32_t)c_seg->c_nextoffset))
614 panic("c_seg_validate: c_bytes_used doesn't match c_nextoffset - c_nextoffset = %d, c_bytes_used = %d\n",
615 (int32_t)C_SEG_OFFSET_TO_BYTES((int32_t)c_seg->c_nextoffset), c_seg->c_bytes_used);
616 }
617 }
618
619 #endif
620
621
622 void
623 c_seg_need_delayed_compaction(c_segment_t c_seg)
624 {
625 boolean_t clear_busy = FALSE;
626
627 if ( !lck_mtx_try_lock_spin_always(c_list_lock)) {
628 C_SEG_BUSY(c_seg);
629
630 lck_mtx_unlock_always(&c_seg->c_lock);
631 lck_mtx_lock_spin_always(c_list_lock);
632 lck_mtx_lock_spin_always(&c_seg->c_lock);
633
634 clear_busy = TRUE;
635 }
636 assert(c_seg->c_state != C_IS_FILLING);
637
638 if (!c_seg->c_on_minorcompact_q && !(C_SEG_IS_ONDISK(c_seg))) {
639 queue_enter(&c_minor_list_head, c_seg, c_segment_t, c_list);
640 c_seg->c_on_minorcompact_q = 1;
641 c_minor_count++;
642 }
643 lck_mtx_unlock_always(c_list_lock);
644
645 if (clear_busy == TRUE)
646 C_SEG_WAKEUP_DONE(c_seg);
647 }
648
649
650 unsigned int c_seg_moved_to_sparse_list = 0;
651
652 void
653 c_seg_move_to_sparse_list(c_segment_t c_seg)
654 {
655 boolean_t clear_busy = FALSE;
656
657 if ( !lck_mtx_try_lock_spin_always(c_list_lock)) {
658 C_SEG_BUSY(c_seg);
659
660 lck_mtx_unlock_always(&c_seg->c_lock);
661 lck_mtx_lock_spin_always(c_list_lock);
662 lck_mtx_lock_spin_always(&c_seg->c_lock);
663
664 clear_busy = TRUE;
665 }
666 c_seg_switch_state(c_seg, C_ON_SWAPPEDOUTSPARSE_Q, FALSE);
667
668 c_seg_moved_to_sparse_list++;
669
670 lck_mtx_unlock_always(c_list_lock);
671
672 if (clear_busy == TRUE)
673 C_SEG_WAKEUP_DONE(c_seg);
674 }
675
676
677 void
678 c_seg_insert_into_q(queue_head_t *qhead, c_segment_t c_seg)
679 {
680 c_segment_t c_seg_next;
681
682 if (queue_empty(qhead)) {
683 queue_enter(qhead, c_seg, c_segment_t, c_age_list);
684 } else {
685 c_seg_next = (c_segment_t)queue_first(qhead);
686
687 while (TRUE) {
688
689 if (c_seg->c_generation_id < c_seg_next->c_generation_id) {
690 queue_insert_before(qhead, c_seg, c_seg_next, c_segment_t, c_age_list);
691 break;
692 }
693 c_seg_next = (c_segment_t) queue_next(&c_seg_next->c_age_list);
694
695 if (queue_end(qhead, (queue_entry_t) c_seg_next)) {
696 queue_enter(qhead, c_seg, c_segment_t, c_age_list);
697 break;
698 }
699 }
700 }
701 }
702
703
704 int try_minor_compaction_failed = 0;
705 int try_minor_compaction_succeeded = 0;
706
707 void
708 c_seg_try_minor_compaction_and_unlock(c_segment_t c_seg)
709 {
710
711 assert(c_seg->c_on_minorcompact_q);
712 /*
713 * c_seg is currently on the delayed minor compaction
714 * queue and we have c_seg locked... if we can get the
715 * c_list_lock w/o blocking (if we blocked we could deadlock
716 * because the lock order is c_list_lock then c_seg's lock)
717 * we'll pull it from the delayed list and free it directly
718 */
719 if ( !lck_mtx_try_lock_spin_always(c_list_lock)) {
720 /*
721 * c_list_lock is held, we need to bail
722 */
723 try_minor_compaction_failed++;
724
725 lck_mtx_unlock_always(&c_seg->c_lock);
726 } else {
727 try_minor_compaction_succeeded++;
728
729 C_SEG_BUSY(c_seg);
730 c_seg_do_minor_compaction_and_unlock(c_seg, TRUE, FALSE, FALSE);
731 }
732 }
733
734
735 int
736 c_seg_do_minor_compaction_and_unlock(c_segment_t c_seg, boolean_t clear_busy, boolean_t need_list_lock, boolean_t disallow_page_replacement)
737 {
738 int c_seg_freed;
739
740 assert(c_seg->c_busy);
741
742 /*
743 * check for the case that can occur when we are not swapping
744 * and this segment has been major compacted in the past
745 * and moved to the majorcompact q to remove it from further
746 * consideration... if the occupancy falls too low we need
747 * to put it back on the age_q so that it will be considered
748 * in the next major compaction sweep... if we don't do this
749 * we will eventually run into the c_segments_limit
750 */
751 if (c_seg->c_state == C_ON_MAJORCOMPACT_Q && C_SEG_SHOULD_MAJORCOMPACT(c_seg)) {
752
753 c_seg_switch_state(c_seg, C_ON_AGE_Q, FALSE);
754 }
755 if (!c_seg->c_on_minorcompact_q) {
756 if (clear_busy == TRUE)
757 C_SEG_WAKEUP_DONE(c_seg);
758
759 lck_mtx_unlock_always(&c_seg->c_lock);
760
761 return (0);
762 }
763 queue_remove(&c_minor_list_head, c_seg, c_segment_t, c_list);
764 c_seg->c_on_minorcompact_q = 0;
765 c_minor_count--;
766
767 lck_mtx_unlock_always(c_list_lock);
768
769 if (disallow_page_replacement == TRUE) {
770 lck_mtx_unlock_always(&c_seg->c_lock);
771
772 PAGE_REPLACEMENT_DISALLOWED(TRUE);
773
774 lck_mtx_lock_spin_always(&c_seg->c_lock);
775 }
776 c_seg_freed = c_seg_minor_compaction_and_unlock(c_seg, clear_busy);
777
778 if (disallow_page_replacement == TRUE)
779 PAGE_REPLACEMENT_DISALLOWED(FALSE);
780
781 if (need_list_lock == TRUE)
782 lck_mtx_lock_spin_always(c_list_lock);
783
784 return (c_seg_freed);
785 }
786
787
788 void
789 c_seg_wait_on_busy(c_segment_t c_seg)
790 {
791 c_seg->c_wanted = 1;
792 assert_wait((event_t) (c_seg), THREAD_UNINT);
793
794 lck_mtx_unlock_always(&c_seg->c_lock);
795 thread_block(THREAD_CONTINUE_NULL);
796 }
797
798
799 void
800 c_seg_switch_state(c_segment_t c_seg, int new_state, boolean_t insert_head)
801 {
802 int old_state = c_seg->c_state;
803
804 #if DEVELOPMENT || DEBUG
805 #if __i386__ || __x86_64__
806 if (new_state != C_IS_FILLING)
807 lck_mtx_assert(&c_seg->c_lock, LCK_MTX_ASSERT_OWNED);
808 lck_mtx_assert(c_list_lock, LCK_MTX_ASSERT_OWNED);
809 #endif
810 #endif
811 switch (old_state) {
812
813 case C_IS_EMPTY:
814 assert(new_state == C_IS_FILLING || new_state == C_IS_FREE);
815
816 c_empty_count--;
817 break;
818
819 case C_IS_FILLING:
820 assert(new_state == C_ON_AGE_Q || new_state == C_ON_SWAPOUT_Q);
821
822 queue_remove(&c_filling_list_head, c_seg, c_segment_t, c_age_list);
823 c_filling_count--;
824 break;
825
826 case C_ON_AGE_Q:
827 assert(new_state == C_ON_SWAPOUT_Q || new_state == C_ON_MAJORCOMPACT_Q ||
828 new_state == C_IS_FREE);
829
830 queue_remove(&c_age_list_head, c_seg, c_segment_t, c_age_list);
831 c_age_count--;
832 break;
833
834 case C_ON_SWAPPEDIN_Q:
835 assert(new_state == C_ON_AGE_Q || new_state == C_IS_FREE);
836
837 queue_remove(&c_swappedin_list_head, c_seg, c_segment_t, c_age_list);
838 c_swappedin_count--;
839 break;
840
841 case C_ON_SWAPOUT_Q:
842 assert(new_state == C_ON_SWAPPEDOUT_Q || new_state == C_ON_SWAPPEDOUTSPARSE_Q ||
843 new_state == C_ON_AGE_Q || new_state == C_IS_FREE || new_state == C_IS_EMPTY);
844
845 queue_remove(&c_swapout_list_head, c_seg, c_segment_t, c_age_list);
846 thread_wakeup((event_t)&compaction_swapper_running);
847 c_swapout_count--;
848 break;
849
850 case C_ON_SWAPPEDOUT_Q:
851 assert(new_state == C_ON_SWAPPEDIN_Q || new_state == C_ON_SWAPPEDOUTSPARSE_Q ||
852 new_state == C_ON_BAD_Q || new_state == C_IS_EMPTY || new_state == C_IS_FREE);
853
854 queue_remove(&c_swappedout_list_head, c_seg, c_segment_t, c_age_list);
855 c_swappedout_count--;
856 break;
857
858 case C_ON_SWAPPEDOUTSPARSE_Q:
859 assert(new_state == C_ON_SWAPPEDIN_Q ||
860 new_state == C_ON_BAD_Q || new_state == C_IS_EMPTY || new_state == C_IS_FREE);
861
862 queue_remove(&c_swappedout_sparse_list_head, c_seg, c_segment_t, c_age_list);
863 c_swappedout_sparse_count--;
864 break;
865
866 case C_ON_MAJORCOMPACT_Q:
867 assert(new_state == C_ON_AGE_Q || new_state == C_IS_FREE);
868
869 queue_remove(&c_major_list_head, c_seg, c_segment_t, c_age_list);
870 c_major_count--;
871 break;
872
873 case C_ON_BAD_Q:
874 assert(new_state == C_IS_FREE);
875
876 queue_remove(&c_bad_list_head, c_seg, c_segment_t, c_age_list);
877 c_bad_count--;
878 break;
879
880 default:
881 panic("c_seg %p has bad c_state = %d\n", c_seg, old_state);
882 }
883
884 switch(new_state) {
885 case C_IS_FREE:
886 assert(old_state != C_IS_FILLING);
887
888 break;
889
890 case C_IS_EMPTY:
891 assert(old_state == C_ON_SWAPOUT_Q || old_state == C_ON_SWAPPEDOUT_Q || old_state == C_ON_SWAPPEDOUTSPARSE_Q);
892
893 c_empty_count++;
894 break;
895
896 case C_IS_FILLING:
897 assert(old_state == C_IS_EMPTY);
898
899 queue_enter(&c_filling_list_head, c_seg, c_segment_t, c_age_list);
900 c_filling_count++;
901 break;
902
903 case C_ON_AGE_Q:
904 assert(old_state == C_IS_FILLING || old_state == C_ON_SWAPPEDIN_Q ||
905 old_state == C_ON_MAJORCOMPACT_Q || old_state == C_ON_SWAPOUT_Q);
906
907 if (old_state == C_IS_FILLING)
908 queue_enter(&c_age_list_head, c_seg, c_segment_t, c_age_list);
909 else
910 c_seg_insert_into_q(&c_age_list_head, c_seg);
911 c_age_count++;
912 break;
913
914 case C_ON_SWAPPEDIN_Q:
915 assert(c_seg->c_state == C_ON_SWAPPEDOUT_Q || c_seg->c_state == C_ON_SWAPPEDOUTSPARSE_Q);
916
917 if (insert_head == TRUE)
918 queue_enter_first(&c_swappedin_list_head, c_seg, c_segment_t, c_age_list);
919 else
920 queue_enter(&c_swappedin_list_head, c_seg, c_segment_t, c_age_list);
921 c_swappedin_count++;
922 break;
923
924 case C_ON_SWAPOUT_Q:
925 assert(old_state == C_ON_AGE_Q || old_state == C_IS_FILLING);
926
927 if (insert_head == TRUE)
928 queue_enter_first(&c_swapout_list_head, c_seg, c_segment_t, c_age_list);
929 else
930 queue_enter(&c_swapout_list_head, c_seg, c_segment_t, c_age_list);
931 c_swapout_count++;
932 break;
933
934 case C_ON_SWAPPEDOUT_Q:
935 assert(c_seg->c_state == C_ON_SWAPOUT_Q);
936
937 if (insert_head == TRUE)
938 queue_enter_first(&c_swappedout_list_head, c_seg, c_segment_t, c_age_list);
939 else
940 queue_enter(&c_swappedout_list_head, c_seg, c_segment_t, c_age_list);
941 c_swappedout_count++;
942 break;
943
944 case C_ON_SWAPPEDOUTSPARSE_Q:
945 assert(c_seg->c_state == C_ON_SWAPOUT_Q || c_seg->c_state == C_ON_SWAPPEDOUT_Q);
946
947 c_seg_insert_into_q(&c_swappedout_sparse_list_head, c_seg);
948 c_swappedout_sparse_count++;
949 break;
950
951 case C_ON_MAJORCOMPACT_Q:
952 assert(c_seg->c_state == C_ON_AGE_Q);
953
954 if (insert_head == TRUE)
955 queue_enter_first(&c_major_list_head, c_seg, c_segment_t, c_age_list);
956 else
957 queue_enter(&c_major_list_head, c_seg, c_segment_t, c_age_list);
958 c_major_count++;
959 break;
960
961 case C_ON_BAD_Q:
962 assert(c_seg->c_state == C_ON_SWAPPEDOUT_Q || c_seg->c_state == C_ON_SWAPPEDOUTSPARSE_Q);
963
964 if (insert_head == TRUE)
965 queue_enter_first(&c_bad_list_head, c_seg, c_segment_t, c_age_list);
966 else
967 queue_enter(&c_bad_list_head, c_seg, c_segment_t, c_age_list);
968 c_bad_count++;
969 break;
970
971 default:
972 panic("c_seg %p requesting bad c_state = %d\n", c_seg, new_state);
973 }
974 c_seg->c_state = new_state;
975 }
976
977
978
979 void
980 c_seg_free(c_segment_t c_seg)
981 {
982 assert(c_seg->c_busy);
983
984 lck_mtx_unlock_always(&c_seg->c_lock);
985 lck_mtx_lock_spin_always(c_list_lock);
986 lck_mtx_lock_spin_always(&c_seg->c_lock);
987
988 c_seg_free_locked(c_seg);
989 }
990
991
992 void
993 c_seg_free_locked(c_segment_t c_seg)
994 {
995 int segno;
996 int pages_populated = 0;
997 int32_t *c_buffer = NULL;
998 uint64_t c_swap_handle = 0;
999
1000 assert(c_seg->c_busy);
1001 assert(!c_seg->c_on_minorcompact_q);
1002 assert(!c_seg->c_busy_swapping);
1003
1004 if (c_seg->c_overage_swap == TRUE) {
1005 c_overage_swapped_count--;
1006 c_seg->c_overage_swap = FALSE;
1007 }
1008 if ( !(C_SEG_IS_ONDISK(c_seg)))
1009 c_buffer = c_seg->c_store.c_buffer;
1010 else
1011 c_swap_handle = c_seg->c_store.c_swap_handle;
1012
1013 c_seg_switch_state(c_seg, C_IS_FREE, FALSE);
1014
1015 lck_mtx_unlock_always(c_list_lock);
1016
1017 if (c_buffer) {
1018 pages_populated = (round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset))) / PAGE_SIZE;
1019 c_seg->c_store.c_buffer = NULL;
1020 } else
1021 c_seg->c_store.c_swap_handle = (uint64_t)-1;
1022
1023 lck_mtx_unlock_always(&c_seg->c_lock);
1024
1025 if (c_buffer) {
1026 if (pages_populated)
1027 kernel_memory_depopulate(kernel_map, (vm_offset_t) c_buffer, pages_populated * PAGE_SIZE, KMA_COMPRESSOR);
1028
1029 } else if (c_swap_handle) {
1030 /*
1031 * Free swap space on disk.
1032 */
1033 vm_swap_free(c_swap_handle);
1034 }
1035 lck_mtx_lock_spin_always(&c_seg->c_lock);
1036
1037 C_SEG_WAKEUP_DONE(c_seg);
1038 lck_mtx_unlock_always(&c_seg->c_lock);
1039
1040 segno = c_seg->c_mysegno;
1041
1042 lck_mtx_lock_spin_always(c_list_lock);
1043 /*
1044 * because the c_buffer is now associated with the segno,
1045 * we can't put the segno back on the free list until
1046 * after we have depopulated the c_buffer range, or
1047 * we run the risk of depopulating a range that is
1048 * now being used in one of the compressor heads
1049 */
1050 c_segments[segno].c_segno = c_free_segno_head;
1051 c_free_segno_head = segno;
1052 c_segment_count--;
1053
1054 lck_mtx_unlock_always(c_list_lock);
1055
1056 #if __i386__ || __x86_64__
1057 lck_mtx_destroy(&c_seg->c_lock, &vm_compressor_lck_grp);
1058 #else /* __i386__ || __x86_64__ */
1059 lck_spin_destroy(&c_seg->c_lock, &vm_compressor_lck_grp);
1060 #endif /* __i386__ || __x86_64__ */
1061
1062 if (c_seg->c_slot_var_array_len)
1063 kfree(c_seg->c_slot_var_array, sizeof(struct c_slot) * c_seg->c_slot_var_array_len);
1064
1065 zfree(compressor_segment_zone, c_seg);
1066 }
1067
1068
1069 int c_seg_trim_page_count = 0;
1070
1071 void
1072 c_seg_trim_tail(c_segment_t c_seg)
1073 {
1074 c_slot_t cs;
1075 uint32_t c_size;
1076 uint32_t c_offset;
1077 uint32_t c_rounded_size;
1078 uint16_t current_nextslot;
1079 uint32_t current_populated_offset;
1080
1081 if (c_seg->c_bytes_used == 0)
1082 return;
1083 current_nextslot = c_seg->c_nextslot;
1084 current_populated_offset = c_seg->c_populated_offset;
1085
1086 while (c_seg->c_nextslot) {
1087
1088 cs = C_SEG_SLOT_FROM_INDEX(c_seg, (c_seg->c_nextslot - 1));
1089
1090 c_size = UNPACK_C_SIZE(cs);
1091
1092 if (c_size) {
1093 if (current_nextslot != c_seg->c_nextslot) {
1094 c_rounded_size = (c_size + C_SEG_OFFSET_ALIGNMENT_MASK) & ~C_SEG_OFFSET_ALIGNMENT_MASK;
1095 c_offset = cs->c_offset + C_SEG_BYTES_TO_OFFSET(c_rounded_size);
1096
1097 c_seg->c_nextoffset = c_offset;
1098 c_seg->c_populated_offset = (c_offset + (C_SEG_BYTES_TO_OFFSET(PAGE_SIZE) - 1)) & ~(C_SEG_BYTES_TO_OFFSET(PAGE_SIZE) - 1);
1099
1100 if (c_seg->c_firstemptyslot > c_seg->c_nextslot)
1101 c_seg->c_firstemptyslot = c_seg->c_nextslot;
1102
1103 c_seg_trim_page_count += ((round_page_32(C_SEG_OFFSET_TO_BYTES(current_populated_offset)) -
1104 round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset))) / PAGE_SIZE);
1105 }
1106 break;
1107 }
1108 c_seg->c_nextslot--;
1109 }
1110 assert(c_seg->c_nextslot);
1111 }
1112
1113
1114 int
1115 c_seg_minor_compaction_and_unlock(c_segment_t c_seg, boolean_t clear_busy)
1116 {
1117 c_slot_mapping_t slot_ptr;
1118 uint32_t c_offset = 0;
1119 uint32_t old_populated_offset;
1120 uint32_t c_rounded_size;
1121 uint32_t c_size;
1122 int c_indx = 0;
1123 int i;
1124 c_slot_t c_dst;
1125 c_slot_t c_src;
1126 boolean_t need_unlock = TRUE;
1127
1128 assert(c_seg->c_busy);
1129
1130 #if VALIDATE_C_SEGMENTS
1131 c_seg_validate(c_seg, FALSE);
1132 #endif
1133 if (c_seg->c_bytes_used == 0) {
1134 c_seg_free(c_seg);
1135 return (1);
1136 }
1137 if (c_seg->c_firstemptyslot >= c_seg->c_nextslot || C_SEG_UNUSED_BYTES(c_seg) < PAGE_SIZE)
1138 goto done;
1139
1140 #if VALIDATE_C_SEGMENTS
1141 c_seg->c_was_minor_compacted++;
1142 #endif
1143 c_indx = c_seg->c_firstemptyslot;
1144 c_dst = C_SEG_SLOT_FROM_INDEX(c_seg, c_indx);
1145
1146 old_populated_offset = c_seg->c_populated_offset;
1147 c_offset = c_dst->c_offset;
1148
1149 for (i = c_indx + 1; i < c_seg->c_nextslot && c_offset < c_seg->c_nextoffset; i++) {
1150
1151 c_src = C_SEG_SLOT_FROM_INDEX(c_seg, i);
1152
1153 c_size = UNPACK_C_SIZE(c_src);
1154
1155 if (c_size == 0)
1156 continue;
1157
1158 memcpy(&c_seg->c_store.c_buffer[c_offset], &c_seg->c_store.c_buffer[c_src->c_offset], c_size);
1159
1160 #if CHECKSUM_THE_DATA
1161 c_dst->c_hash_data = c_src->c_hash_data;
1162 #endif
1163 #if CHECKSUM_THE_COMPRESSED_DATA
1164 c_dst->c_hash_compressed_data = c_src->c_hash_compressed_data;
1165 #endif
1166 c_dst->c_size = c_src->c_size;
1167 c_dst->c_packed_ptr = c_src->c_packed_ptr;
1168 c_dst->c_offset = c_offset;
1169
1170 slot_ptr = (c_slot_mapping_t)C_SLOT_UNPACK_PTR(c_dst);
1171 slot_ptr->s_cindx = c_indx;
1172
1173 c_rounded_size = (c_size + C_SEG_OFFSET_ALIGNMENT_MASK) & ~C_SEG_OFFSET_ALIGNMENT_MASK;
1174
1175 c_offset += C_SEG_BYTES_TO_OFFSET(c_rounded_size);
1176 PACK_C_SIZE(c_src, 0);
1177 c_indx++;
1178
1179 c_dst = C_SEG_SLOT_FROM_INDEX(c_seg, c_indx);
1180 }
1181 c_seg->c_firstemptyslot = c_indx;
1182 c_seg->c_nextslot = c_indx;
1183 c_seg->c_nextoffset = c_offset;
1184 c_seg->c_populated_offset = (c_offset + (C_SEG_BYTES_TO_OFFSET(PAGE_SIZE) - 1)) & ~(C_SEG_BYTES_TO_OFFSET(PAGE_SIZE) - 1);
1185 c_seg->c_bytes_unused = 0;
1186
1187 #if VALIDATE_C_SEGMENTS
1188 c_seg_validate(c_seg, TRUE);
1189 #endif
1190
1191 if (old_populated_offset > c_seg->c_populated_offset) {
1192 uint32_t gc_size;
1193 int32_t *gc_ptr;
1194
1195 gc_size = C_SEG_OFFSET_TO_BYTES(old_populated_offset - c_seg->c_populated_offset);
1196 gc_ptr = &c_seg->c_store.c_buffer[c_seg->c_populated_offset];
1197
1198 lck_mtx_unlock_always(&c_seg->c_lock);
1199
1200 kernel_memory_depopulate(kernel_map, (vm_offset_t)gc_ptr, gc_size, KMA_COMPRESSOR);
1201
1202 if (clear_busy == TRUE)
1203 lck_mtx_lock_spin_always(&c_seg->c_lock);
1204 else
1205 need_unlock = FALSE;
1206 }
1207 done:
1208 if (need_unlock == TRUE) {
1209 if (clear_busy == TRUE)
1210 C_SEG_WAKEUP_DONE(c_seg);
1211
1212 lck_mtx_unlock_always(&c_seg->c_lock);
1213 }
1214 return (0);
1215 }
1216
1217
1218 static void
1219 c_seg_alloc_nextslot(c_segment_t c_seg)
1220 {
1221 struct c_slot *old_slot_array = NULL;
1222 struct c_slot *new_slot_array = NULL;
1223 int newlen;
1224 int oldlen;
1225
1226 if (c_seg->c_nextslot < c_seg_fixed_array_len)
1227 return;
1228
1229 if ((c_seg->c_nextslot - c_seg_fixed_array_len) >= c_seg->c_slot_var_array_len) {
1230
1231 oldlen = c_seg->c_slot_var_array_len;
1232 old_slot_array = c_seg->c_slot_var_array;
1233
1234 if (oldlen == 0)
1235 newlen = C_SEG_SLOT_VAR_ARRAY_MIN_LEN;
1236 else
1237 newlen = oldlen * 2;
1238
1239 new_slot_array = (struct c_slot *)kalloc(sizeof(struct c_slot) * newlen);
1240
1241 lck_mtx_lock_spin_always(&c_seg->c_lock);
1242
1243 if (old_slot_array)
1244 memcpy((char *)new_slot_array, (char *)old_slot_array, sizeof(struct c_slot) * oldlen);
1245
1246 c_seg->c_slot_var_array_len = newlen;
1247 c_seg->c_slot_var_array = new_slot_array;
1248
1249 lck_mtx_unlock_always(&c_seg->c_lock);
1250
1251 if (old_slot_array)
1252 kfree(old_slot_array, sizeof(struct c_slot) * oldlen);
1253 }
1254 }
1255
1256
1257
1258 struct {
1259 uint64_t asked_permission;
1260 uint64_t compactions;
1261 uint64_t moved_slots;
1262 uint64_t moved_bytes;
1263 uint64_t wasted_space_in_swapouts;
1264 uint64_t count_of_swapouts;
1265 uint64_t count_of_freed_segs;
1266 } c_seg_major_compact_stats;
1267
1268
1269 #define C_MAJOR_COMPACTION_SIZE_APPROPRIATE ((C_SEG_BUFSIZE * 90) / 100)
1270
1271
1272 boolean_t
1273 c_seg_major_compact_ok(
1274 c_segment_t c_seg_dst,
1275 c_segment_t c_seg_src)
1276 {
1277
1278 c_seg_major_compact_stats.asked_permission++;
1279
1280 if (c_seg_src->c_bytes_used >= C_MAJOR_COMPACTION_SIZE_APPROPRIATE &&
1281 c_seg_dst->c_bytes_used >= C_MAJOR_COMPACTION_SIZE_APPROPRIATE)
1282 return (FALSE);
1283
1284 if (c_seg_dst->c_nextoffset >= C_SEG_OFF_LIMIT || c_seg_dst->c_nextslot >= C_SLOT_MAX_INDEX) {
1285 /*
1286 * destination segment is full... can't compact
1287 */
1288 return (FALSE);
1289 }
1290
1291 return (TRUE);
1292 }
1293
1294
1295 boolean_t
1296 c_seg_major_compact(
1297 c_segment_t c_seg_dst,
1298 c_segment_t c_seg_src)
1299 {
1300 c_slot_mapping_t slot_ptr;
1301 uint32_t c_rounded_size;
1302 uint32_t c_size;
1303 uint16_t dst_slot;
1304 int i;
1305 c_slot_t c_dst;
1306 c_slot_t c_src;
1307 boolean_t keep_compacting = TRUE;
1308
1309 /*
1310 * segments are not locked but they are both marked c_busy
1311 * which keeps c_decompress from working on them...
1312 * we can safely allocate new pages, move compressed data
1313 * from c_seg_src to c_seg_dst and update both c_segment's
1314 * state w/o holding the master lock
1315 */
1316
1317 #if VALIDATE_C_SEGMENTS
1318 c_seg_dst->c_was_major_compacted++;
1319 c_seg_src->c_was_major_donor++;
1320 #endif
1321 c_seg_major_compact_stats.compactions++;
1322
1323 dst_slot = c_seg_dst->c_nextslot;
1324
1325 for (i = 0; i < c_seg_src->c_nextslot; i++) {
1326
1327 c_src = C_SEG_SLOT_FROM_INDEX(c_seg_src, i);
1328
1329 c_size = UNPACK_C_SIZE(c_src);
1330
1331 if (c_size == 0) {
1332 /* BATCH: move what we have so far; */
1333 continue;
1334 }
1335
1336 if (C_SEG_OFFSET_TO_BYTES(c_seg_dst->c_populated_offset - c_seg_dst->c_nextoffset) < (unsigned) c_size) {
1337 int size_to_populate;
1338
1339 /* doesn't fit */
1340 size_to_populate = C_SEG_BUFSIZE - C_SEG_OFFSET_TO_BYTES(c_seg_dst->c_populated_offset);
1341
1342 if (size_to_populate == 0) {
1343 /* can't fit */
1344 keep_compacting = FALSE;
1345 break;
1346 }
1347 if (size_to_populate > C_SEG_MAX_POPULATE_SIZE)
1348 size_to_populate = C_SEG_MAX_POPULATE_SIZE;
1349
1350 kernel_memory_populate(kernel_map,
1351 (vm_offset_t) &c_seg_dst->c_store.c_buffer[c_seg_dst->c_populated_offset],
1352 size_to_populate,
1353 KMA_COMPRESSOR,
1354 VM_KERN_MEMORY_COMPRESSOR);
1355
1356 c_seg_dst->c_populated_offset += C_SEG_BYTES_TO_OFFSET(size_to_populate);
1357 assert(C_SEG_OFFSET_TO_BYTES(c_seg_dst->c_populated_offset) <= C_SEG_BUFSIZE);
1358 }
1359 c_seg_alloc_nextslot(c_seg_dst);
1360
1361 c_dst = C_SEG_SLOT_FROM_INDEX(c_seg_dst, c_seg_dst->c_nextslot);
1362
1363 memcpy(&c_seg_dst->c_store.c_buffer[c_seg_dst->c_nextoffset], &c_seg_src->c_store.c_buffer[c_src->c_offset], c_size);
1364
1365 c_rounded_size = (c_size + C_SEG_OFFSET_ALIGNMENT_MASK) & ~C_SEG_OFFSET_ALIGNMENT_MASK;
1366
1367 c_seg_major_compact_stats.moved_slots++;
1368 c_seg_major_compact_stats.moved_bytes += c_size;
1369
1370 #if CHECKSUM_THE_DATA
1371 c_dst->c_hash_data = c_src->c_hash_data;
1372 #endif
1373 #if CHECKSUM_THE_COMPRESSED_DATA
1374 c_dst->c_hash_compressed_data = c_src->c_hash_compressed_data;
1375 #endif
1376 c_dst->c_size = c_src->c_size;
1377 c_dst->c_packed_ptr = c_src->c_packed_ptr;
1378 c_dst->c_offset = c_seg_dst->c_nextoffset;
1379
1380 if (c_seg_dst->c_firstemptyslot == c_seg_dst->c_nextslot)
1381 c_seg_dst->c_firstemptyslot++;
1382 c_seg_dst->c_nextslot++;
1383 c_seg_dst->c_bytes_used += c_rounded_size;
1384 c_seg_dst->c_nextoffset += C_SEG_BYTES_TO_OFFSET(c_rounded_size);
1385
1386 PACK_C_SIZE(c_src, 0);
1387
1388 c_seg_src->c_bytes_used -= c_rounded_size;
1389 c_seg_src->c_bytes_unused += c_rounded_size;
1390 c_seg_src->c_firstemptyslot = 0;
1391
1392 if (c_seg_dst->c_nextoffset >= C_SEG_OFF_LIMIT || c_seg_dst->c_nextslot >= C_SLOT_MAX_INDEX) {
1393 /* dest segment is now full */
1394 keep_compacting = FALSE;
1395 break;
1396 }
1397 }
1398 if (dst_slot < c_seg_dst->c_nextslot) {
1399
1400 PAGE_REPLACEMENT_ALLOWED(TRUE);
1401 /*
1402 * we've now locked out c_decompress from
1403 * converting the slot passed into it into
1404 * a c_segment_t which allows us to use
1405 * the backptr to change which c_segment and
1406 * index the slot points to
1407 */
1408 while (dst_slot < c_seg_dst->c_nextslot) {
1409
1410 c_dst = C_SEG_SLOT_FROM_INDEX(c_seg_dst, dst_slot);
1411
1412 slot_ptr = (c_slot_mapping_t)C_SLOT_UNPACK_PTR(c_dst);
1413 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
1414 slot_ptr->s_cseg = c_seg_dst->c_mysegno + 1;
1415 slot_ptr->s_cindx = dst_slot++;
1416 }
1417 PAGE_REPLACEMENT_ALLOWED(FALSE);
1418 }
1419 return (keep_compacting);
1420 }
1421
1422
1423 uint64_t
1424 vm_compressor_compute_elapsed_msecs(clock_sec_t end_sec, clock_nsec_t end_nsec, clock_sec_t start_sec, clock_nsec_t start_nsec)
1425 {
1426 uint64_t end_msecs;
1427 uint64_t start_msecs;
1428
1429 end_msecs = (end_sec * 1000) + end_nsec / 1000000;
1430 start_msecs = (start_sec * 1000) + start_nsec / 1000000;
1431
1432 return (end_msecs - start_msecs);
1433 }
1434
1435
1436
1437 uint32_t compressor_eval_period_in_msecs = 250;
1438 uint32_t compressor_sample_min_in_msecs = 500;
1439 uint32_t compressor_sample_max_in_msecs = 10000;
1440 uint32_t compressor_thrashing_threshold_per_10msecs = 50;
1441 uint32_t compressor_thrashing_min_per_10msecs = 20;
1442
1443 /* When true, reset sample data next chance we get. */
1444 static boolean_t compressor_need_sample_reset = FALSE;
1445
1446 extern uint32_t vm_page_filecache_min;
1447
1448
1449 void
1450 compute_swapout_target_age(void)
1451 {
1452 clock_sec_t cur_ts_sec;
1453 clock_nsec_t cur_ts_nsec;
1454 uint32_t min_operations_needed_in_this_sample;
1455 uint64_t elapsed_msecs_in_eval;
1456 uint64_t elapsed_msecs_in_sample;
1457 boolean_t need_eval_reset = FALSE;
1458
1459 clock_get_system_nanotime(&cur_ts_sec, &cur_ts_nsec);
1460
1461 elapsed_msecs_in_sample = vm_compressor_compute_elapsed_msecs(cur_ts_sec, cur_ts_nsec, start_of_sample_period_sec, start_of_sample_period_nsec);
1462
1463 if (compressor_need_sample_reset ||
1464 elapsed_msecs_in_sample >= compressor_sample_max_in_msecs) {
1465 compressor_need_sample_reset = TRUE;
1466 need_eval_reset = TRUE;
1467 goto done;
1468 }
1469 elapsed_msecs_in_eval = vm_compressor_compute_elapsed_msecs(cur_ts_sec, cur_ts_nsec, start_of_eval_period_sec, start_of_eval_period_nsec);
1470
1471 if (elapsed_msecs_in_eval < compressor_eval_period_in_msecs)
1472 goto done;
1473 need_eval_reset = TRUE;
1474
1475 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_START, elapsed_msecs_in_eval, sample_period_compression_count, sample_period_decompression_count, 0, 0);
1476
1477 min_operations_needed_in_this_sample = (compressor_thrashing_min_per_10msecs * (uint32_t)elapsed_msecs_in_eval) / 10;
1478
1479 if ((sample_period_compression_count - last_eval_compression_count) < min_operations_needed_in_this_sample ||
1480 (sample_period_decompression_count - last_eval_decompression_count) < min_operations_needed_in_this_sample) {
1481
1482 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END, sample_period_compression_count - last_eval_compression_count,
1483 sample_period_decompression_count - last_eval_decompression_count, 0, 1, 0);
1484
1485 swapout_target_age = 0;
1486
1487 compressor_need_sample_reset = TRUE;
1488 need_eval_reset = TRUE;
1489 goto done;
1490 }
1491 last_eval_compression_count = sample_period_compression_count;
1492 last_eval_decompression_count = sample_period_decompression_count;
1493
1494 if (elapsed_msecs_in_sample < compressor_sample_min_in_msecs) {
1495
1496 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END, swapout_target_age, 0, 0, 5, 0);
1497 goto done;
1498 }
1499 if (sample_period_decompression_count > ((compressor_thrashing_threshold_per_10msecs * elapsed_msecs_in_sample) / 10)) {
1500
1501 uint64_t running_total;
1502 uint64_t working_target;
1503 uint64_t aging_target;
1504 uint32_t oldest_age_of_csegs_sampled = 0;
1505 uint64_t working_set_approximation = 0;
1506
1507 swapout_target_age = 0;
1508
1509 working_target = (sample_period_decompression_count / 100) * 95; /* 95 percent */
1510 aging_target = (sample_period_decompression_count / 100) * 1; /* 1 percent */
1511 running_total = 0;
1512
1513 for (oldest_age_of_csegs_sampled = 0; oldest_age_of_csegs_sampled < DECOMPRESSION_SAMPLE_MAX_AGE; oldest_age_of_csegs_sampled++) {
1514
1515 running_total += age_of_decompressions_during_sample_period[oldest_age_of_csegs_sampled];
1516
1517 working_set_approximation += oldest_age_of_csegs_sampled * age_of_decompressions_during_sample_period[oldest_age_of_csegs_sampled];
1518
1519 if (running_total >= working_target)
1520 break;
1521 }
1522 if (oldest_age_of_csegs_sampled < DECOMPRESSION_SAMPLE_MAX_AGE) {
1523
1524 working_set_approximation = (working_set_approximation * 1000) / elapsed_msecs_in_sample;
1525
1526 if (working_set_approximation < VM_PAGE_COMPRESSOR_COUNT) {
1527
1528 running_total = overage_decompressions_during_sample_period;
1529
1530 for (oldest_age_of_csegs_sampled = DECOMPRESSION_SAMPLE_MAX_AGE - 1; oldest_age_of_csegs_sampled; oldest_age_of_csegs_sampled--) {
1531 running_total += age_of_decompressions_during_sample_period[oldest_age_of_csegs_sampled];
1532
1533 if (running_total >= aging_target)
1534 break;
1535 }
1536 swapout_target_age = (uint32_t)cur_ts_sec - oldest_age_of_csegs_sampled;
1537
1538 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END, swapout_target_age, working_set_approximation, VM_PAGE_COMPRESSOR_COUNT, 2, 0);
1539 } else {
1540 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END, working_set_approximation, VM_PAGE_COMPRESSOR_COUNT, 0, 3, 0);
1541 }
1542 } else
1543 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END, working_target, running_total, 0, 4, 0);
1544
1545 compressor_need_sample_reset = TRUE;
1546 need_eval_reset = TRUE;
1547 } else
1548 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END, sample_period_decompression_count, (compressor_thrashing_threshold_per_10msecs * elapsed_msecs_in_sample) / 10, 0, 6, 0);
1549 done:
1550 if (compressor_need_sample_reset == TRUE) {
1551 bzero(age_of_decompressions_during_sample_period, sizeof(age_of_decompressions_during_sample_period));
1552 overage_decompressions_during_sample_period = 0;
1553
1554 start_of_sample_period_sec = cur_ts_sec;
1555 start_of_sample_period_nsec = cur_ts_nsec;
1556 sample_period_decompression_count = 0;
1557 sample_period_compression_count = 0;
1558 last_eval_decompression_count = 0;
1559 last_eval_compression_count = 0;
1560 compressor_need_sample_reset = FALSE;
1561 }
1562 if (need_eval_reset == TRUE) {
1563 start_of_eval_period_sec = cur_ts_sec;
1564 start_of_eval_period_nsec = cur_ts_nsec;
1565 }
1566 }
1567
1568
1569 int compaction_swapper_inited = 0;
1570 int compaction_swapper_init_now = 0;
1571 int compaction_swapper_running = 0;
1572 int compaction_swapper_abort = 0;
1573
1574
1575 #if CONFIG_JETSAM
1576 boolean_t memorystatus_kill_on_VM_thrashing(boolean_t);
1577 boolean_t memorystatus_kill_on_FC_thrashing(boolean_t);
1578 int compressor_thrashing_induced_jetsam = 0;
1579 int filecache_thrashing_induced_jetsam = 0;
1580 static boolean_t vm_compressor_thrashing_detected = FALSE;
1581 #endif /* CONFIG_JETSAM */
1582
1583 static boolean_t
1584 compressor_needs_to_swap(void)
1585 {
1586 boolean_t should_swap = FALSE;
1587
1588 if (vm_swapout_ripe_segments == TRUE && c_overage_swapped_count < c_overage_swapped_limit) {
1589 c_segment_t c_seg;
1590 clock_sec_t now;
1591 clock_sec_t age;
1592 clock_nsec_t nsec;
1593
1594 clock_get_system_nanotime(&now, &nsec);
1595 age = 0;
1596
1597 lck_mtx_lock_spin_always(c_list_lock);
1598
1599 if ( !queue_empty(&c_age_list_head)) {
1600 c_seg = (c_segment_t) queue_first(&c_age_list_head);
1601
1602 age = now - c_seg->c_creation_ts;
1603 }
1604 lck_mtx_unlock_always(c_list_lock);
1605
1606 if (age >= vm_ripe_target_age)
1607 return (TRUE);
1608 }
1609 if ((vm_compressor_mode == VM_PAGER_COMPRESSOR_WITH_SWAP) && vm_swap_up == TRUE) {
1610 if (COMPRESSOR_NEEDS_TO_SWAP()) {
1611 return (TRUE);
1612 }
1613 if (VM_PAGE_Q_THROTTLED(&vm_pageout_queue_external) && vm_page_anonymous_count < (vm_page_inactive_count / 20)) {
1614 return (TRUE);
1615 }
1616 if (vm_page_free_count < (vm_page_free_reserved - (COMPRESSOR_FREE_RESERVED_LIMIT * 2)))
1617 return (TRUE);
1618 }
1619 compute_swapout_target_age();
1620
1621 if (swapout_target_age) {
1622 c_segment_t c_seg;
1623
1624 lck_mtx_lock_spin_always(c_list_lock);
1625
1626 if (!queue_empty(&c_age_list_head)) {
1627
1628 c_seg = (c_segment_t) queue_first(&c_age_list_head);
1629
1630 if (c_seg->c_creation_ts > swapout_target_age)
1631 swapout_target_age = 0;
1632 }
1633 lck_mtx_unlock_always(c_list_lock);
1634 }
1635 #if CONFIG_PHANTOM_CACHE
1636 if (vm_phantom_cache_check_pressure())
1637 should_swap = TRUE;
1638 #endif
1639 if (swapout_target_age)
1640 should_swap = TRUE;
1641
1642 #if CONFIG_JETSAM
1643 if (should_swap || c_segment_pages_compressed > c_segment_pages_compressed_nearing_limit) {
1644
1645 if (vm_compressor_thrashing_detected == FALSE) {
1646 vm_compressor_thrashing_detected = TRUE;
1647
1648 if (swapout_target_age || c_segment_pages_compressed > c_segment_pages_compressed_nearing_limit) {
1649 memorystatus_kill_on_VM_thrashing(TRUE /* async */);
1650 compressor_thrashing_induced_jetsam++;
1651 } else {
1652 memorystatus_kill_on_FC_thrashing(TRUE /* async */);
1653 filecache_thrashing_induced_jetsam++;
1654 }
1655 }
1656 /*
1657 * let the jetsam take precedence over
1658 * any major compactions we might have
1659 * been able to do... otherwise we run
1660 * the risk of doing major compactions
1661 * on segments we're about to free up
1662 * due to the jetsam activity.
1663 */
1664 should_swap = FALSE;
1665 }
1666
1667 #endif /* CONFIG_JETSAM */
1668
1669 if (should_swap == FALSE) {
1670 /*
1671 * COMPRESSOR_NEEDS_TO_MAJOR_COMPACT returns true only if we're
1672 * about to run out of available compressor segments... in this
1673 * case, we absolutely need to run a major compaction even if
1674 * we've just kicked off a jetsam or we don't otherwise need to
1675 * swap... terminating objects releases
1676 * pages back to the uncompressed cache, but does not guarantee
1677 * that we will free up even a single compression segment
1678 */
1679 should_swap = COMPRESSOR_NEEDS_TO_MAJOR_COMPACT();
1680 }
1681
1682 /*
1683 * returning TRUE when swap_supported == FALSE
1684 * will cause the major compaction engine to
1685 * run, but will not trigger any swapping...
1686 * segments that have been major compacted
1687 * will be moved to the majorcompact queue
1688 */
1689 return (should_swap);
1690 }
1691
1692 #if CONFIG_JETSAM
1693 /*
1694 * This function is called from the jetsam thread after killing something to
1695 * mitigate thrashing.
1696 *
1697 * We need to restart our thrashing detection heuristics since memory pressure
1698 * has potentially changed significantly, and we don't want to detect on old
1699 * data from before the jetsam.
1700 */
1701 void
1702 vm_thrashing_jetsam_done(void)
1703 {
1704 vm_compressor_thrashing_detected = FALSE;
1705
1706 /* Were we compressor-thrashing or filecache-thrashing? */
1707 if (swapout_target_age) {
1708 swapout_target_age = 0;
1709 compressor_need_sample_reset = TRUE;
1710 }
1711 #if CONFIG_PHANTOM_CACHE
1712 else {
1713 vm_phantom_cache_restart_sample();
1714 }
1715 #endif
1716 }
1717 #endif /* CONFIG_JETSAM */
1718
1719 uint32_t vm_wake_compactor_swapper_calls = 0;
1720
1721 void
1722 vm_wake_compactor_swapper(void)
1723 {
1724 if (compaction_swapper_running || c_segment_count == 0)
1725 return;
1726
1727 if (c_minor_count || COMPRESSOR_NEEDS_TO_MAJOR_COMPACT()) {
1728
1729 lck_mtx_lock_spin_always(c_list_lock);
1730
1731 fastwake_warmup = FALSE;
1732
1733 if (compaction_swapper_running == 0) {
1734
1735 vm_wake_compactor_swapper_calls++;
1736
1737 thread_wakeup((event_t)&c_compressor_swap_trigger);
1738
1739 compaction_swapper_running = 1;
1740 }
1741 lck_mtx_unlock_always(c_list_lock);
1742 }
1743 }
1744
1745
1746 void
1747 vm_consider_swapping()
1748 {
1749 c_segment_t c_seg, c_seg_next;
1750 clock_sec_t now;
1751 clock_nsec_t nsec;
1752
1753
1754 lck_mtx_lock_spin_always(c_list_lock);
1755
1756 compaction_swapper_abort = 1;
1757
1758 while (compaction_swapper_running) {
1759 assert_wait((event_t)&compaction_swapper_running, THREAD_UNINT);
1760
1761 lck_mtx_unlock_always(c_list_lock);
1762
1763 thread_block(THREAD_CONTINUE_NULL);
1764
1765 lck_mtx_lock_spin_always(c_list_lock);
1766 }
1767 compaction_swapper_abort = 0;
1768 compaction_swapper_running = 1;
1769
1770 vm_swapout_ripe_segments = TRUE;
1771
1772 if (!queue_empty(&c_major_list_head)) {
1773
1774 clock_get_system_nanotime(&now, &nsec);
1775
1776 c_seg = (c_segment_t)queue_first(&c_major_list_head);
1777
1778 while (!queue_end(&c_major_list_head, (queue_entry_t)c_seg)) {
1779
1780 if (c_overage_swapped_count >= c_overage_swapped_limit)
1781 break;
1782
1783 c_seg_next = (c_segment_t) queue_next(&c_seg->c_age_list);
1784
1785 if ((now - c_seg->c_creation_ts) >= vm_ripe_target_age) {
1786
1787 lck_mtx_lock_spin_always(&c_seg->c_lock);
1788
1789 c_seg_switch_state(c_seg, C_ON_AGE_Q, FALSE);
1790
1791 lck_mtx_unlock_always(&c_seg->c_lock);
1792 }
1793 c_seg = c_seg_next;
1794 }
1795 }
1796 vm_compressor_compact_and_swap(FALSE);
1797
1798 compaction_swapper_running = 0;
1799
1800 vm_swapout_ripe_segments = FALSE;
1801
1802 lck_mtx_unlock_always(c_list_lock);
1803 }
1804
1805
1806 void
1807 vm_consider_waking_compactor_swapper(void)
1808 {
1809 boolean_t need_wakeup = FALSE;
1810
1811 if (compaction_swapper_running)
1812 return;
1813
1814 if (c_segment_count == 0)
1815 return;
1816
1817 if (!compaction_swapper_inited && !compaction_swapper_init_now) {
1818 compaction_swapper_init_now = 1;
1819 need_wakeup = TRUE;
1820 }
1821
1822 if (c_minor_count && (COMPRESSOR_NEEDS_TO_MINOR_COMPACT())) {
1823
1824 need_wakeup = TRUE;
1825
1826 } else if (compressor_needs_to_swap()) {
1827
1828 need_wakeup = TRUE;
1829
1830 } else if (c_minor_count) {
1831 uint64_t total_bytes;
1832
1833 total_bytes = compressor_object->resident_page_count * PAGE_SIZE_64;
1834
1835 if ((total_bytes - compressor_bytes_used) > total_bytes / 10)
1836 need_wakeup = TRUE;
1837 }
1838 if (need_wakeup == TRUE) {
1839
1840 lck_mtx_lock_spin_always(c_list_lock);
1841
1842 fastwake_warmup = FALSE;
1843
1844 if (compaction_swapper_running == 0) {
1845 memoryshot(VM_WAKEUP_COMPACTOR_SWAPPER, DBG_FUNC_NONE);
1846
1847 thread_wakeup((event_t)&c_compressor_swap_trigger);
1848
1849 compaction_swapper_running = 1;
1850 }
1851 lck_mtx_unlock_always(c_list_lock);
1852 }
1853 }
1854
1855
1856 #define C_SWAPOUT_LIMIT 4
1857 #define DELAYED_COMPACTIONS_PER_PASS 30
1858
1859 void
1860 vm_compressor_do_delayed_compactions(boolean_t flush_all)
1861 {
1862 c_segment_t c_seg;
1863 int number_compacted = 0;
1864 boolean_t needs_to_swap = FALSE;
1865
1866
1867 lck_mtx_assert(c_list_lock, LCK_MTX_ASSERT_OWNED);
1868
1869 while (!queue_empty(&c_minor_list_head) && needs_to_swap == FALSE) {
1870
1871 c_seg = (c_segment_t)queue_first(&c_minor_list_head);
1872
1873 lck_mtx_lock_spin_always(&c_seg->c_lock);
1874
1875 if (c_seg->c_busy) {
1876
1877 lck_mtx_unlock_always(c_list_lock);
1878 c_seg_wait_on_busy(c_seg);
1879 lck_mtx_lock_spin_always(c_list_lock);
1880
1881 continue;
1882 }
1883 C_SEG_BUSY(c_seg);
1884
1885 c_seg_do_minor_compaction_and_unlock(c_seg, TRUE, FALSE, TRUE);
1886
1887 if (vm_swap_up == TRUE && (number_compacted++ > DELAYED_COMPACTIONS_PER_PASS)) {
1888
1889 if ((flush_all == TRUE || compressor_needs_to_swap() == TRUE) && c_swapout_count < C_SWAPOUT_LIMIT)
1890 needs_to_swap = TRUE;
1891
1892 number_compacted = 0;
1893 }
1894 lck_mtx_lock_spin_always(c_list_lock);
1895 }
1896 }
1897
1898
1899 #define C_SEGMENT_SWAPPEDIN_AGE_LIMIT 10
1900
1901 static void
1902 vm_compressor_age_swapped_in_segments(boolean_t flush_all)
1903 {
1904 c_segment_t c_seg;
1905 clock_sec_t now;
1906 clock_nsec_t nsec;
1907
1908 clock_get_system_nanotime(&now, &nsec);
1909
1910 while (!queue_empty(&c_swappedin_list_head)) {
1911
1912 c_seg = (c_segment_t)queue_first(&c_swappedin_list_head);
1913
1914 if (flush_all == FALSE && (now - c_seg->c_swappedin_ts) < C_SEGMENT_SWAPPEDIN_AGE_LIMIT)
1915 break;
1916
1917 lck_mtx_lock_spin_always(&c_seg->c_lock);
1918
1919 c_seg_switch_state(c_seg, C_ON_AGE_Q, FALSE);
1920
1921 lck_mtx_unlock_always(&c_seg->c_lock);
1922 }
1923 }
1924
1925
1926 void
1927 vm_compressor_flush(void)
1928 {
1929 uint64_t vm_swap_put_failures_at_start;
1930 wait_result_t wait_result = 0;
1931 AbsoluteTime startTime, endTime;
1932 clock_sec_t now_sec;
1933 clock_nsec_t now_nsec;
1934 uint64_t nsec;
1935
1936 HIBLOG("vm_compressor_flush - starting\n");
1937
1938 clock_get_uptime(&startTime);
1939
1940 lck_mtx_lock_spin_always(c_list_lock);
1941
1942 fastwake_warmup = FALSE;
1943 compaction_swapper_abort = 1;
1944
1945 while (compaction_swapper_running) {
1946 assert_wait((event_t)&compaction_swapper_running, THREAD_UNINT);
1947
1948 lck_mtx_unlock_always(c_list_lock);
1949
1950 thread_block(THREAD_CONTINUE_NULL);
1951
1952 lck_mtx_lock_spin_always(c_list_lock);
1953 }
1954 compaction_swapper_abort = 0;
1955 compaction_swapper_running = 1;
1956
1957 hibernate_flushing = TRUE;
1958 hibernate_no_swapspace = FALSE;
1959 c_generation_id_flush_barrier = c_generation_id + 1000;
1960
1961 clock_get_system_nanotime(&now_sec, &now_nsec);
1962 hibernate_flushing_deadline = now_sec + HIBERNATE_FLUSHING_SECS_TO_COMPLETE;
1963
1964 vm_swap_put_failures_at_start = vm_swap_put_failures;
1965
1966 vm_compressor_compact_and_swap(TRUE);
1967
1968 while (!queue_empty(&c_swapout_list_head)) {
1969
1970 assert_wait_timeout((event_t) &compaction_swapper_running, THREAD_INTERRUPTIBLE, 5000, 1000*NSEC_PER_USEC);
1971
1972 lck_mtx_unlock_always(c_list_lock);
1973
1974 wait_result = thread_block(THREAD_CONTINUE_NULL);
1975
1976 lck_mtx_lock_spin_always(c_list_lock);
1977
1978 if (wait_result == THREAD_TIMED_OUT)
1979 break;
1980 }
1981 hibernate_flushing = FALSE;
1982 compaction_swapper_running = 0;
1983
1984 if (vm_swap_put_failures > vm_swap_put_failures_at_start)
1985 HIBLOG("vm_compressor_flush failed to clean %llu segments - vm_page_compressor_count(%d)\n",
1986 vm_swap_put_failures - vm_swap_put_failures_at_start, VM_PAGE_COMPRESSOR_COUNT);
1987
1988 lck_mtx_unlock_always(c_list_lock);
1989
1990 clock_get_uptime(&endTime);
1991 SUB_ABSOLUTETIME(&endTime, &startTime);
1992 absolutetime_to_nanoseconds(endTime, &nsec);
1993
1994 HIBLOG("vm_compressor_flush completed - took %qd msecs\n", nsec / 1000000ULL);
1995 }
1996
1997
1998 extern void vm_swap_file_set_tuneables(void);
1999 int compaction_swap_trigger_thread_awakened = 0;
2000
2001
2002 static void
2003 vm_compressor_swap_trigger_thread(void)
2004 {
2005 current_thread()->options |= TH_OPT_VMPRIV;
2006
2007 /*
2008 * compaction_swapper_init_now is set when the first call to
2009 * vm_consider_waking_compactor_swapper is made from
2010 * vm_pageout_scan... since this function is called upon
2011 * thread creation, we want to make sure to delay adjusting
2012 * the tuneables until we are awakened via vm_pageout_scan
2013 * so that we are at a point where the vm_swapfile_open will
2014 * be operating on the correct directory (in case the default
2015 * of /var/vm/ is overridden by the dymanic_pager
2016 */
2017 if (compaction_swapper_init_now && !compaction_swapper_inited) {
2018 if (vm_compressor_mode == VM_PAGER_COMPRESSOR_WITH_SWAP)
2019 vm_swap_file_set_tuneables();
2020
2021 if (vm_restricted_to_single_processor == TRUE)
2022 thread_vm_bind_group_add();
2023
2024 compaction_swapper_inited = 1;
2025 }
2026 lck_mtx_lock_spin_always(c_list_lock);
2027
2028 compaction_swap_trigger_thread_awakened++;
2029
2030 vm_compressor_compact_and_swap(FALSE);
2031
2032 assert_wait((event_t)&c_compressor_swap_trigger, THREAD_UNINT);
2033
2034 compaction_swapper_running = 0;
2035 thread_wakeup((event_t)&compaction_swapper_running);
2036
2037 lck_mtx_unlock_always(c_list_lock);
2038
2039 thread_block((thread_continue_t)vm_compressor_swap_trigger_thread);
2040
2041 /* NOTREACHED */
2042 }
2043
2044
2045 void
2046 vm_compressor_record_warmup_start(void)
2047 {
2048 c_segment_t c_seg;
2049
2050 lck_mtx_lock_spin_always(c_list_lock);
2051
2052 if (first_c_segment_to_warm_generation_id == 0) {
2053 if (!queue_empty(&c_age_list_head)) {
2054
2055 c_seg = (c_segment_t)queue_last(&c_age_list_head);
2056
2057 first_c_segment_to_warm_generation_id = c_seg->c_generation_id;
2058 } else
2059 first_c_segment_to_warm_generation_id = 0;
2060
2061 fastwake_recording_in_progress = TRUE;
2062 }
2063 lck_mtx_unlock_always(c_list_lock);
2064 }
2065
2066
2067 void
2068 vm_compressor_record_warmup_end(void)
2069 {
2070 c_segment_t c_seg;
2071
2072 lck_mtx_lock_spin_always(c_list_lock);
2073
2074 if (fastwake_recording_in_progress == TRUE) {
2075
2076 if (!queue_empty(&c_age_list_head)) {
2077
2078 c_seg = (c_segment_t)queue_last(&c_age_list_head);
2079
2080 last_c_segment_to_warm_generation_id = c_seg->c_generation_id;
2081 } else
2082 last_c_segment_to_warm_generation_id = first_c_segment_to_warm_generation_id;
2083
2084 fastwake_recording_in_progress = FALSE;
2085
2086 HIBLOG("vm_compressor_record_warmup (%qd - %qd)\n", first_c_segment_to_warm_generation_id, last_c_segment_to_warm_generation_id);
2087 }
2088 lck_mtx_unlock_always(c_list_lock);
2089 }
2090
2091
2092 #define DELAY_TRIM_ON_WAKE_SECS 4
2093
2094 void
2095 vm_compressor_delay_trim(void)
2096 {
2097 clock_sec_t sec;
2098 clock_nsec_t nsec;
2099
2100 clock_get_system_nanotime(&sec, &nsec);
2101 dont_trim_until_ts = sec + DELAY_TRIM_ON_WAKE_SECS;
2102 }
2103
2104
2105 void
2106 vm_compressor_do_warmup(void)
2107 {
2108 lck_mtx_lock_spin_always(c_list_lock);
2109
2110 if (first_c_segment_to_warm_generation_id == last_c_segment_to_warm_generation_id) {
2111 first_c_segment_to_warm_generation_id = last_c_segment_to_warm_generation_id = 0;
2112
2113 lck_mtx_unlock_always(c_list_lock);
2114 return;
2115 }
2116
2117 if (compaction_swapper_running == 0) {
2118
2119 fastwake_warmup = TRUE;
2120 compaction_swapper_running = 1;
2121 thread_wakeup((event_t)&c_compressor_swap_trigger);
2122 }
2123 lck_mtx_unlock_always(c_list_lock);
2124 }
2125
2126
2127 void
2128 do_fastwake_warmup(void)
2129 {
2130 uint64_t my_thread_id;
2131 c_segment_t c_seg = NULL;
2132 AbsoluteTime startTime, endTime;
2133 uint64_t nsec;
2134
2135
2136 HIBLOG("vm_compressor_fastwake_warmup (%qd - %qd) - starting\n", first_c_segment_to_warm_generation_id, last_c_segment_to_warm_generation_id);
2137
2138 clock_get_uptime(&startTime);
2139
2140 lck_mtx_unlock_always(c_list_lock);
2141
2142 my_thread_id = current_thread()->thread_id;
2143 proc_set_task_policy_thread(kernel_task, my_thread_id,
2144 TASK_POLICY_INTERNAL, TASK_POLICY_IO, THROTTLE_LEVEL_COMPRESSOR_TIER2);
2145
2146 PAGE_REPLACEMENT_DISALLOWED(TRUE);
2147
2148 lck_mtx_lock_spin_always(c_list_lock);
2149
2150 while (!queue_empty(&c_swappedout_list_head) && fastwake_warmup == TRUE) {
2151
2152 c_seg = (c_segment_t) queue_first(&c_swappedout_list_head);
2153
2154 if (c_seg->c_generation_id < first_c_segment_to_warm_generation_id ||
2155 c_seg->c_generation_id > last_c_segment_to_warm_generation_id)
2156 break;
2157
2158 if (vm_page_free_count < (AVAILABLE_MEMORY / 4))
2159 break;
2160
2161 lck_mtx_lock_spin_always(&c_seg->c_lock);
2162 lck_mtx_unlock_always(c_list_lock);
2163
2164 if (c_seg->c_busy) {
2165 PAGE_REPLACEMENT_DISALLOWED(FALSE);
2166 c_seg_wait_on_busy(c_seg);
2167 PAGE_REPLACEMENT_DISALLOWED(TRUE);
2168 } else {
2169 c_seg_swapin(c_seg, TRUE);
2170
2171 lck_mtx_unlock_always(&c_seg->c_lock);
2172 c_segment_warmup_count++;
2173
2174 PAGE_REPLACEMENT_DISALLOWED(FALSE);
2175 vm_pageout_io_throttle();
2176 PAGE_REPLACEMENT_DISALLOWED(TRUE);
2177 }
2178 lck_mtx_lock_spin_always(c_list_lock);
2179 }
2180 lck_mtx_unlock_always(c_list_lock);
2181
2182 PAGE_REPLACEMENT_DISALLOWED(FALSE);
2183
2184 proc_set_task_policy_thread(kernel_task, my_thread_id,
2185 TASK_POLICY_INTERNAL, TASK_POLICY_IO, THROTTLE_LEVEL_COMPRESSOR_TIER0);
2186
2187 clock_get_uptime(&endTime);
2188 SUB_ABSOLUTETIME(&endTime, &startTime);
2189 absolutetime_to_nanoseconds(endTime, &nsec);
2190
2191 HIBLOG("vm_compressor_fastwake_warmup completed - took %qd msecs\n", nsec / 1000000ULL);
2192
2193 lck_mtx_lock_spin_always(c_list_lock);
2194
2195 first_c_segment_to_warm_generation_id = last_c_segment_to_warm_generation_id = 0;
2196 }
2197
2198
2199 void
2200 vm_compressor_compact_and_swap(boolean_t flush_all)
2201 {
2202 c_segment_t c_seg, c_seg_next;
2203 boolean_t keep_compacting;
2204 clock_sec_t now;
2205 clock_nsec_t nsec;
2206
2207
2208 if (fastwake_warmup == TRUE) {
2209 uint64_t starting_warmup_count;
2210
2211 starting_warmup_count = c_segment_warmup_count;
2212
2213 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 11) | DBG_FUNC_START, c_segment_warmup_count,
2214 first_c_segment_to_warm_generation_id, last_c_segment_to_warm_generation_id, 0, 0);
2215 do_fastwake_warmup();
2216 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 11) | DBG_FUNC_END, c_segment_warmup_count, c_segment_warmup_count - starting_warmup_count, 0, 0, 0);
2217
2218 fastwake_warmup = FALSE;
2219 }
2220
2221 /*
2222 * it's possible for the c_age_list_head to be empty if we
2223 * hit our limits for growing the compressor pool and we subsequently
2224 * hibernated... on the next hibernation we could see the queue as
2225 * empty and not proceeed even though we have a bunch of segments on
2226 * the swapped in queue that need to be dealt with.
2227 */
2228 vm_compressor_do_delayed_compactions(flush_all);
2229
2230 vm_compressor_age_swapped_in_segments(flush_all);
2231
2232 /*
2233 * we only need to grab the timestamp once per
2234 * invocation of this function since the
2235 * timescale we're interested in is measured
2236 * in days
2237 */
2238 clock_get_system_nanotime(&now, &nsec);
2239
2240 while (!queue_empty(&c_age_list_head) && compaction_swapper_abort == 0) {
2241
2242 if (hibernate_flushing == TRUE) {
2243 clock_sec_t sec;
2244
2245 if (hibernate_should_abort()) {
2246 HIBLOG("vm_compressor_flush - hibernate_should_abort returned TRUE\n");
2247 break;
2248 }
2249 if (hibernate_no_swapspace == TRUE) {
2250 HIBLOG("vm_compressor_flush - out of swap space\n");
2251 break;
2252 }
2253 clock_get_system_nanotime(&sec, &nsec);
2254
2255 if (sec > hibernate_flushing_deadline) {
2256 HIBLOG("vm_compressor_flush - failed to finish before deadline\n");
2257 break;
2258 }
2259 }
2260 if (c_swapout_count >= C_SWAPOUT_LIMIT) {
2261
2262 assert_wait_timeout((event_t) &compaction_swapper_running, THREAD_INTERRUPTIBLE, 100, 1000*NSEC_PER_USEC);
2263
2264 lck_mtx_unlock_always(c_list_lock);
2265
2266 thread_block(THREAD_CONTINUE_NULL);
2267
2268 lck_mtx_lock_spin_always(c_list_lock);
2269 }
2270 /*
2271 * Minor compactions
2272 */
2273 vm_compressor_do_delayed_compactions(flush_all);
2274
2275 vm_compressor_age_swapped_in_segments(flush_all);
2276
2277 if (c_swapout_count >= C_SWAPOUT_LIMIT) {
2278 /*
2279 * we timed out on the above thread_block
2280 * let's loop around and try again
2281 * the timeout allows us to continue
2282 * to do minor compactions to make
2283 * more memory available
2284 */
2285 continue;
2286 }
2287
2288 /*
2289 * Swap out segments?
2290 */
2291 if (flush_all == FALSE) {
2292 boolean_t needs_to_swap;
2293
2294 lck_mtx_unlock_always(c_list_lock);
2295
2296 needs_to_swap = compressor_needs_to_swap();
2297
2298 if (needs_to_swap == TRUE && vm_swap_low_on_space())
2299 vm_compressor_take_paging_space_action();
2300
2301 lck_mtx_lock_spin_always(c_list_lock);
2302
2303 if (needs_to_swap == FALSE)
2304 break;
2305 }
2306 if (queue_empty(&c_age_list_head))
2307 break;
2308 c_seg = (c_segment_t) queue_first(&c_age_list_head);
2309
2310 assert(c_seg->c_state == C_ON_AGE_Q);
2311
2312 if (flush_all == TRUE && c_seg->c_generation_id > c_generation_id_flush_barrier)
2313 break;
2314
2315 lck_mtx_lock_spin_always(&c_seg->c_lock);
2316
2317 if (c_seg->c_busy) {
2318
2319 lck_mtx_unlock_always(c_list_lock);
2320 c_seg_wait_on_busy(c_seg);
2321 lck_mtx_lock_spin_always(c_list_lock);
2322
2323 continue;
2324 }
2325 C_SEG_BUSY(c_seg);
2326
2327 if (c_seg_do_minor_compaction_and_unlock(c_seg, FALSE, TRUE, TRUE)) {
2328 /*
2329 * found an empty c_segment and freed it
2330 * so go grab the next guy in the queue
2331 */
2332 c_seg_major_compact_stats.count_of_freed_segs++;
2333 continue;
2334 }
2335 /*
2336 * Major compaction
2337 */
2338 keep_compacting = TRUE;
2339
2340 while (keep_compacting == TRUE) {
2341
2342 assert(c_seg->c_busy);
2343
2344 /* look for another segment to consolidate */
2345
2346 c_seg_next = (c_segment_t) queue_next(&c_seg->c_age_list);
2347
2348 if (queue_end(&c_age_list_head, (queue_entry_t)c_seg_next))
2349 break;
2350
2351 assert(c_seg_next->c_state == C_ON_AGE_Q);
2352
2353 if (c_seg_major_compact_ok(c_seg, c_seg_next) == FALSE)
2354 break;
2355
2356 lck_mtx_lock_spin_always(&c_seg_next->c_lock);
2357
2358 if (c_seg_next->c_busy) {
2359
2360 lck_mtx_unlock_always(c_list_lock);
2361 c_seg_wait_on_busy(c_seg_next);
2362 lck_mtx_lock_spin_always(c_list_lock);
2363
2364 continue;
2365 }
2366 /* grab that segment */
2367 C_SEG_BUSY(c_seg_next);
2368
2369 if (c_seg_do_minor_compaction_and_unlock(c_seg_next, FALSE, TRUE, TRUE)) {
2370 /*
2371 * found an empty c_segment and freed it
2372 * so we can't continue to use c_seg_next
2373 */
2374 c_seg_major_compact_stats.count_of_freed_segs++;
2375 continue;
2376 }
2377
2378 /* unlock the list ... */
2379 lck_mtx_unlock_always(c_list_lock);
2380
2381 /* do the major compaction */
2382
2383 keep_compacting = c_seg_major_compact(c_seg, c_seg_next);
2384
2385 PAGE_REPLACEMENT_DISALLOWED(TRUE);
2386
2387 lck_mtx_lock_spin_always(&c_seg_next->c_lock);
2388 /*
2389 * run a minor compaction on the donor segment
2390 * since we pulled at least some of it's
2391 * data into our target... if we've emptied
2392 * it, now is a good time to free it which
2393 * c_seg_minor_compaction_and_unlock also takes care of
2394 *
2395 * by passing TRUE, we ask for c_busy to be cleared
2396 * and c_wanted to be taken care of
2397 */
2398 if (c_seg_minor_compaction_and_unlock(c_seg_next, TRUE))
2399 c_seg_major_compact_stats.count_of_freed_segs++;
2400
2401 PAGE_REPLACEMENT_DISALLOWED(FALSE);
2402
2403 /* relock the list */
2404 lck_mtx_lock_spin_always(c_list_lock);
2405
2406 } /* major compaction */
2407
2408 lck_mtx_lock_spin_always(&c_seg->c_lock);
2409
2410 assert(c_seg->c_busy);
2411 assert(!c_seg->c_on_minorcompact_q);
2412
2413 if (vm_swap_up == TRUE) {
2414 /*
2415 * This mode of putting a generic c_seg on the swapout list is
2416 * only supported when we have general swap ON i.e.
2417 * we compress pages into c_segs as we process them off
2418 * the paging queues in vm_pageout_scan().
2419 */
2420 if (COMPRESSED_PAGER_IS_SWAPBACKED)
2421 c_seg_switch_state(c_seg, C_ON_SWAPOUT_Q, FALSE);
2422 else {
2423 if ((vm_swapout_ripe_segments == TRUE && c_overage_swapped_count < c_overage_swapped_limit)) {
2424 /*
2425 * we are running compressor sweeps with swap-behind
2426 * make sure the c_seg has aged enough before swapping it
2427 * out...
2428 */
2429 if ((now - c_seg->c_creation_ts) >= vm_ripe_target_age) {
2430 c_seg->c_overage_swap = TRUE;
2431 c_overage_swapped_count++;
2432 c_seg_switch_state(c_seg, C_ON_SWAPOUT_Q, FALSE);
2433 }
2434 }
2435 }
2436 }
2437 if (c_seg->c_state == C_ON_AGE_Q) {
2438 /*
2439 * this c_seg didn't get moved to the swapout queue
2440 * so we need to move it out of the way...
2441 * we just did a major compaction on it so put it
2442 * on that queue
2443 */
2444 c_seg_switch_state(c_seg, C_ON_MAJORCOMPACT_Q, FALSE);
2445 } else {
2446 c_seg_major_compact_stats.wasted_space_in_swapouts += C_SEG_BUFSIZE - c_seg->c_bytes_used;
2447 c_seg_major_compact_stats.count_of_swapouts++;
2448 }
2449 C_SEG_WAKEUP_DONE(c_seg);
2450
2451 lck_mtx_unlock_always(&c_seg->c_lock);
2452
2453 if (c_swapout_count) {
2454 lck_mtx_unlock_always(c_list_lock);
2455
2456 thread_wakeup((event_t)&c_swapout_list_head);
2457
2458 lck_mtx_lock_spin_always(c_list_lock);
2459 }
2460 }
2461 }
2462
2463
2464 static c_segment_t
2465 c_seg_allocate(c_segment_t *current_chead)
2466 {
2467 c_segment_t c_seg;
2468 int min_needed;
2469 int size_to_populate;
2470
2471 if (vm_compressor_low_on_space())
2472 vm_compressor_take_paging_space_action();
2473
2474 if ( (c_seg = *current_chead) == NULL ) {
2475 uint32_t c_segno;
2476
2477 lck_mtx_lock_spin_always(c_list_lock);
2478
2479 while (c_segments_busy == TRUE) {
2480 assert_wait((event_t) (&c_segments_busy), THREAD_UNINT);
2481
2482 lck_mtx_unlock_always(c_list_lock);
2483
2484 thread_block(THREAD_CONTINUE_NULL);
2485
2486 lck_mtx_lock_spin_always(c_list_lock);
2487 }
2488 if (c_free_segno_head == (uint32_t)-1) {
2489 uint32_t c_segments_available_new;
2490
2491 if (c_segments_available >= c_segments_limit || c_segment_pages_compressed >= c_segment_pages_compressed_limit) {
2492 lck_mtx_unlock_always(c_list_lock);
2493
2494 return (NULL);
2495 }
2496 c_segments_busy = TRUE;
2497 lck_mtx_unlock_always(c_list_lock);
2498
2499 kernel_memory_populate(kernel_map, (vm_offset_t)c_segments_next_page,
2500 PAGE_SIZE, KMA_KOBJECT, VM_KERN_MEMORY_COMPRESSOR);
2501 c_segments_next_page += PAGE_SIZE;
2502
2503 c_segments_available_new = c_segments_available + C_SEGMENTS_PER_PAGE;
2504
2505 if (c_segments_available_new > c_segments_limit)
2506 c_segments_available_new = c_segments_limit;
2507
2508 for (c_segno = c_segments_available + 1; c_segno < c_segments_available_new; c_segno++)
2509 c_segments[c_segno - 1].c_segno = c_segno;
2510
2511 lck_mtx_lock_spin_always(c_list_lock);
2512
2513 c_segments[c_segno - 1].c_segno = c_free_segno_head;
2514 c_free_segno_head = c_segments_available;
2515 c_segments_available = c_segments_available_new;
2516
2517 c_segments_busy = FALSE;
2518 thread_wakeup((event_t) (&c_segments_busy));
2519 }
2520 c_segno = c_free_segno_head;
2521 assert(c_segno >= 0 && c_segno < c_segments_limit);
2522
2523 c_free_segno_head = c_segments[c_segno].c_segno;
2524
2525 /*
2526 * do the rest of the bookkeeping now while we're still behind
2527 * the list lock and grab our generation id now into a local
2528 * so that we can install it once we have the c_seg allocated
2529 */
2530 c_segment_count++;
2531 if (c_segment_count > c_segment_count_max)
2532 c_segment_count_max = c_segment_count;
2533
2534 lck_mtx_unlock_always(c_list_lock);
2535
2536 c_seg = (c_segment_t)zalloc(compressor_segment_zone);
2537 bzero((char *)c_seg, sizeof(struct c_segment));
2538
2539 c_seg->c_store.c_buffer = (int32_t *)C_SEG_BUFFER_ADDRESS(c_segno);
2540
2541 #if __i386__ || __x86_64__
2542 lck_mtx_init(&c_seg->c_lock, &vm_compressor_lck_grp, &vm_compressor_lck_attr);
2543 #else /* __i386__ || __x86_64__ */
2544 lck_spin_init(&c_seg->c_lock, &vm_compressor_lck_grp, &vm_compressor_lck_attr);
2545 #endif /* __i386__ || __x86_64__ */
2546
2547 c_seg->c_state = C_IS_EMPTY;
2548 c_seg->c_firstemptyslot = C_SLOT_MAX_INDEX;
2549 c_seg->c_mysegno = c_segno;
2550
2551 lck_mtx_lock_spin_always(c_list_lock);
2552 c_empty_count++;
2553 c_seg_switch_state(c_seg, C_IS_FILLING, FALSE);
2554 c_segments[c_segno].c_seg = c_seg;
2555 lck_mtx_unlock_always(c_list_lock);
2556
2557 *current_chead = c_seg;
2558 }
2559 c_seg_alloc_nextslot(c_seg);
2560
2561 size_to_populate = C_SEG_ALLOCSIZE - C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset);
2562
2563 if (size_to_populate) {
2564
2565 min_needed = PAGE_SIZE + (C_SEG_ALLOCSIZE - C_SEG_BUFSIZE);
2566
2567 if (C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset - c_seg->c_nextoffset) < (unsigned) min_needed) {
2568
2569 if (size_to_populate > C_SEG_MAX_POPULATE_SIZE)
2570 size_to_populate = C_SEG_MAX_POPULATE_SIZE;
2571
2572 kernel_memory_populate(kernel_map,
2573 (vm_offset_t) &c_seg->c_store.c_buffer[c_seg->c_populated_offset],
2574 size_to_populate,
2575 KMA_COMPRESSOR,
2576 VM_KERN_MEMORY_COMPRESSOR);
2577 } else
2578 size_to_populate = 0;
2579 }
2580 PAGE_REPLACEMENT_DISALLOWED(TRUE);
2581
2582 lck_mtx_lock_spin_always(&c_seg->c_lock);
2583
2584 if (size_to_populate)
2585 c_seg->c_populated_offset += C_SEG_BYTES_TO_OFFSET(size_to_populate);
2586
2587 return (c_seg);
2588 }
2589
2590
2591 static void
2592 c_current_seg_filled(c_segment_t c_seg, c_segment_t *current_chead)
2593 {
2594 uint32_t unused_bytes;
2595 uint32_t offset_to_depopulate;
2596 int new_state = C_ON_AGE_Q;
2597 clock_sec_t sec;
2598 clock_nsec_t nsec;
2599
2600 unused_bytes = trunc_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset - c_seg->c_nextoffset));
2601
2602 if (unused_bytes) {
2603
2604 offset_to_depopulate = C_SEG_BYTES_TO_OFFSET(round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_nextoffset)));
2605
2606 /*
2607 * release the extra physical page(s) at the end of the segment
2608 */
2609 lck_mtx_unlock_always(&c_seg->c_lock);
2610
2611 kernel_memory_depopulate(
2612 kernel_map,
2613 (vm_offset_t) &c_seg->c_store.c_buffer[offset_to_depopulate],
2614 unused_bytes,
2615 KMA_COMPRESSOR);
2616
2617 lck_mtx_lock_spin_always(&c_seg->c_lock);
2618
2619 c_seg->c_populated_offset = offset_to_depopulate;
2620 }
2621 assert(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset) <= C_SEG_BUFSIZE);
2622
2623 #if CONFIG_FREEZE
2624 if (current_chead == (c_segment_t*)&freezer_chead && DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED &&
2625 c_freezer_swapout_count < VM_MAX_FREEZER_CSEG_SWAP_COUNT) {
2626 new_state = C_ON_SWAPOUT_Q;
2627 }
2628 #endif /* CONFIG_FREEZE */
2629
2630 clock_get_system_nanotime(&sec, &nsec);
2631 c_seg->c_creation_ts = (uint32_t)sec;
2632
2633 lck_mtx_lock_spin_always(c_list_lock);
2634
2635 #if CONFIG_FREEZE
2636 if (c_seg->c_state == C_ON_SWAPOUT_Q)
2637 c_freezer_swapout_count++;
2638 #endif /* CONFIG_FREEZE */
2639
2640 c_seg->c_generation_id = c_generation_id++;
2641 c_seg_switch_state(c_seg, new_state, FALSE);
2642
2643 lck_mtx_unlock_always(c_list_lock);
2644
2645 #if CONFIG_FREEZE
2646 if (c_seg->c_state == C_ON_SWAPOUT_Q)
2647 thread_wakeup((event_t)&c_swapout_list_head);
2648 #endif /* CONFIG_FREEZE */
2649
2650 if (c_seg->c_state == C_ON_AGE_Q && C_SEG_UNUSED_BYTES(c_seg) >= PAGE_SIZE)
2651 c_seg_need_delayed_compaction(c_seg);
2652
2653 *current_chead = NULL;
2654 }
2655
2656 /*
2657 * returns with c_seg locked
2658 */
2659 void
2660 c_seg_swapin_requeue(c_segment_t c_seg, boolean_t has_data)
2661 {
2662 clock_sec_t sec;
2663 clock_nsec_t nsec;
2664
2665 clock_get_system_nanotime(&sec, &nsec);
2666
2667 lck_mtx_lock_spin_always(c_list_lock);
2668 lck_mtx_lock_spin_always(&c_seg->c_lock);
2669
2670 c_seg->c_busy_swapping = 0;
2671
2672 if (c_seg->c_overage_swap == TRUE) {
2673 c_overage_swapped_count--;
2674 c_seg->c_overage_swap = FALSE;
2675 }
2676 if (has_data == TRUE) {
2677 c_seg_switch_state(c_seg, C_ON_SWAPPEDIN_Q, FALSE);
2678 } else {
2679 c_seg->c_store.c_buffer = (int32_t*) NULL;
2680 c_seg->c_populated_offset = C_SEG_BYTES_TO_OFFSET(0);
2681
2682 c_seg_switch_state(c_seg, C_ON_BAD_Q, FALSE);
2683 }
2684 c_seg->c_swappedin_ts = (uint32_t)sec;
2685
2686 lck_mtx_unlock_always(c_list_lock);
2687 }
2688
2689
2690
2691 /*
2692 * c_seg has to be locked and is returned locked.
2693 * PAGE_REPLACMENT_DISALLOWED has to be TRUE on entry and is returned TRUE
2694 */
2695
2696 void
2697 c_seg_swapin(c_segment_t c_seg, boolean_t force_minor_compaction)
2698 {
2699 vm_offset_t addr = 0;
2700 uint32_t io_size = 0;
2701 uint64_t f_offset;
2702
2703 assert(C_SEG_IS_ONDISK(c_seg));
2704
2705 #if !CHECKSUM_THE_SWAP
2706 c_seg_trim_tail(c_seg);
2707 #endif
2708 io_size = round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset));
2709 f_offset = c_seg->c_store.c_swap_handle;
2710
2711 C_SEG_BUSY(c_seg);
2712 c_seg->c_busy_swapping = 1;
2713 lck_mtx_unlock_always(&c_seg->c_lock);
2714
2715 PAGE_REPLACEMENT_DISALLOWED(FALSE);
2716
2717 addr = (vm_offset_t)C_SEG_BUFFER_ADDRESS(c_seg->c_mysegno);
2718
2719 kernel_memory_populate(kernel_map, addr, io_size, KMA_COMPRESSOR, VM_KERN_MEMORY_COMPRESSOR);
2720
2721 if (vm_swap_get(addr, f_offset, io_size) != KERN_SUCCESS) {
2722 PAGE_REPLACEMENT_DISALLOWED(TRUE);
2723
2724 kernel_memory_depopulate(kernel_map, addr, io_size, KMA_COMPRESSOR);
2725
2726 c_seg_swapin_requeue(c_seg, FALSE);
2727 } else {
2728 c_seg->c_store.c_buffer = (int32_t*) addr;
2729 #if ENCRYPTED_SWAP
2730 vm_swap_decrypt(c_seg);
2731 #endif /* ENCRYPTED_SWAP */
2732
2733 #if CHECKSUM_THE_SWAP
2734 if (c_seg->cseg_swap_size != io_size)
2735 panic("swapin size doesn't match swapout size");
2736
2737 if (c_seg->cseg_hash != hash_string((char*) c_seg->c_store.c_buffer, (int)io_size)) {
2738 panic("c_seg_swapin - Swap hash mismatch\n");
2739 }
2740 #endif /* CHECKSUM_THE_SWAP */
2741
2742 PAGE_REPLACEMENT_DISALLOWED(TRUE);
2743
2744 if (force_minor_compaction == TRUE) {
2745 lck_mtx_lock_spin_always(&c_seg->c_lock);
2746
2747 c_seg_minor_compaction_and_unlock(c_seg, FALSE);
2748 }
2749 OSAddAtomic64(c_seg->c_bytes_used, &compressor_bytes_used);
2750
2751 c_seg_swapin_requeue(c_seg, TRUE);
2752 }
2753 C_SEG_WAKEUP_DONE(c_seg);
2754 }
2755
2756
2757 static void
2758 c_segment_sv_hash_drop_ref(int hash_indx)
2759 {
2760 struct c_sv_hash_entry o_sv_he, n_sv_he;
2761
2762 while (1) {
2763
2764 o_sv_he.he_record = c_segment_sv_hash_table[hash_indx].he_record;
2765
2766 n_sv_he.he_ref = o_sv_he.he_ref - 1;
2767 n_sv_he.he_data = o_sv_he.he_data;
2768
2769 if (OSCompareAndSwap64((UInt64)o_sv_he.he_record, (UInt64)n_sv_he.he_record, (UInt64 *) &c_segment_sv_hash_table[hash_indx].he_record) == TRUE) {
2770 if (n_sv_he.he_ref == 0)
2771 OSAddAtomic(-1, &c_segment_svp_in_hash);
2772 break;
2773 }
2774 }
2775 }
2776
2777
2778 static int
2779 c_segment_sv_hash_insert(uint32_t data)
2780 {
2781 int hash_sindx;
2782 int misses;
2783 struct c_sv_hash_entry o_sv_he, n_sv_he;
2784 boolean_t got_ref = FALSE;
2785
2786 if (data == 0)
2787 OSAddAtomic(1, &c_segment_svp_zero_compressions);
2788 else
2789 OSAddAtomic(1, &c_segment_svp_nonzero_compressions);
2790
2791 hash_sindx = data & C_SV_HASH_MASK;
2792
2793 for (misses = 0; misses < C_SV_HASH_MAX_MISS; misses++)
2794 {
2795 o_sv_he.he_record = c_segment_sv_hash_table[hash_sindx].he_record;
2796
2797 while (o_sv_he.he_data == data || o_sv_he.he_ref == 0) {
2798 n_sv_he.he_ref = o_sv_he.he_ref + 1;
2799 n_sv_he.he_data = data;
2800
2801 if (OSCompareAndSwap64((UInt64)o_sv_he.he_record, (UInt64)n_sv_he.he_record, (UInt64 *) &c_segment_sv_hash_table[hash_sindx].he_record) == TRUE) {
2802 if (n_sv_he.he_ref == 1)
2803 OSAddAtomic(1, &c_segment_svp_in_hash);
2804 got_ref = TRUE;
2805 break;
2806 }
2807 o_sv_he.he_record = c_segment_sv_hash_table[hash_sindx].he_record;
2808 }
2809 if (got_ref == TRUE)
2810 break;
2811 hash_sindx++;
2812
2813 if (hash_sindx == C_SV_HASH_SIZE)
2814 hash_sindx = 0;
2815 }
2816 if (got_ref == FALSE)
2817 return(-1);
2818
2819 return (hash_sindx);
2820 }
2821
2822
2823 #if RECORD_THE_COMPRESSED_DATA
2824
2825 static void
2826 c_compressed_record_data(char *src, int c_size)
2827 {
2828 if ((c_compressed_record_cptr + c_size + 4) >= c_compressed_record_ebuf)
2829 panic("c_compressed_record_cptr >= c_compressed_record_ebuf");
2830
2831 *(int *)((void *)c_compressed_record_cptr) = c_size;
2832
2833 c_compressed_record_cptr += 4;
2834
2835 memcpy(c_compressed_record_cptr, src, c_size);
2836 c_compressed_record_cptr += c_size;
2837 }
2838 #endif
2839
2840
2841 static int
2842 c_compress_page(char *src, c_slot_mapping_t slot_ptr, c_segment_t *current_chead, char *scratch_buf)
2843 {
2844 int c_size;
2845 int c_rounded_size = 0;
2846 int max_csize;
2847 c_slot_t cs;
2848 c_segment_t c_seg;
2849
2850 KERNEL_DEBUG(0xe0400000 | DBG_FUNC_START, *current_chead, 0, 0, 0, 0);
2851 retry:
2852 if ((c_seg = c_seg_allocate(current_chead)) == NULL)
2853 return (1);
2854 /*
2855 * returns with c_seg lock held
2856 * and PAGE_REPLACEMENT_DISALLOWED(TRUE)...
2857 * c_nextslot has been allocated and
2858 * c_store.c_buffer populated
2859 */
2860 assert(c_seg->c_state == C_IS_FILLING);
2861
2862 cs = C_SEG_SLOT_FROM_INDEX(c_seg, c_seg->c_nextslot);
2863
2864 cs->c_packed_ptr = C_SLOT_PACK_PTR(slot_ptr);
2865 assert(slot_ptr == (c_slot_mapping_t)C_SLOT_UNPACK_PTR(cs));
2866
2867 cs->c_offset = c_seg->c_nextoffset;
2868
2869 max_csize = C_SEG_BUFSIZE - C_SEG_OFFSET_TO_BYTES((int32_t)cs->c_offset);
2870
2871 if (max_csize > PAGE_SIZE)
2872 max_csize = PAGE_SIZE;
2873
2874 #if CHECKSUM_THE_DATA
2875 cs->c_hash_data = hash_string(src, PAGE_SIZE);
2876 #endif
2877
2878 c_size = WKdm_compress_new((const WK_word *)(uintptr_t)src, (WK_word *)(uintptr_t)&c_seg->c_store.c_buffer[cs->c_offset],
2879 (WK_word *)(uintptr_t)scratch_buf, max_csize - 4);
2880 assert(c_size <= (max_csize - 4) && c_size >= -1);
2881
2882 if (c_size == -1) {
2883
2884 if (max_csize < PAGE_SIZE) {
2885 c_current_seg_filled(c_seg, current_chead);
2886 assert(*current_chead == NULL);
2887
2888 lck_mtx_unlock_always(&c_seg->c_lock);
2889
2890 PAGE_REPLACEMENT_DISALLOWED(FALSE);
2891 goto retry;
2892 }
2893 c_size = PAGE_SIZE;
2894
2895 memcpy(&c_seg->c_store.c_buffer[cs->c_offset], src, c_size);
2896
2897 OSAddAtomic(1, &c_segment_noncompressible_pages);
2898
2899 } else if (c_size == 0) {
2900 int hash_index;
2901
2902 /*
2903 * special case - this is a page completely full of a single 32 bit value
2904 */
2905 hash_index = c_segment_sv_hash_insert(*(uint32_t *)(uintptr_t)src);
2906
2907 if (hash_index != -1) {
2908 slot_ptr->s_cindx = hash_index;
2909 slot_ptr->s_cseg = C_SV_CSEG_ID;
2910
2911 OSAddAtomic(1, &c_segment_svp_hash_succeeded);
2912 #if RECORD_THE_COMPRESSED_DATA
2913 c_compressed_record_data(src, 4);
2914 #endif
2915 goto sv_compression;
2916 }
2917 c_size = 4;
2918
2919 memcpy(&c_seg->c_store.c_buffer[cs->c_offset], src, c_size);
2920
2921 OSAddAtomic(1, &c_segment_svp_hash_failed);
2922 }
2923
2924 #if RECORD_THE_COMPRESSED_DATA
2925 c_compressed_record_data((char *)&c_seg->c_store.c_buffer[cs->c_offset], c_size);
2926 #endif
2927
2928 #if CHECKSUM_THE_COMPRESSED_DATA
2929 cs->c_hash_compressed_data = hash_string((char *)&c_seg->c_store.c_buffer[cs->c_offset], c_size);
2930 #endif
2931 c_rounded_size = (c_size + C_SEG_OFFSET_ALIGNMENT_MASK) & ~C_SEG_OFFSET_ALIGNMENT_MASK;
2932
2933 PACK_C_SIZE(cs, c_size);
2934 c_seg->c_bytes_used += c_rounded_size;
2935 c_seg->c_nextoffset += C_SEG_BYTES_TO_OFFSET(c_rounded_size);
2936
2937 slot_ptr->s_cindx = c_seg->c_nextslot++;
2938 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
2939 slot_ptr->s_cseg = c_seg->c_mysegno + 1;
2940
2941 sv_compression:
2942 if (c_seg->c_nextoffset >= C_SEG_OFF_LIMIT || c_seg->c_nextslot >= C_SLOT_MAX_INDEX) {
2943 c_current_seg_filled(c_seg, current_chead);
2944 assert(*current_chead == NULL);
2945 }
2946 lck_mtx_unlock_always(&c_seg->c_lock);
2947
2948 PAGE_REPLACEMENT_DISALLOWED(FALSE);
2949
2950 #if RECORD_THE_COMPRESSED_DATA
2951 if ((c_compressed_record_cptr - c_compressed_record_sbuf) >= C_SEG_ALLOCSIZE) {
2952 c_compressed_record_write(c_compressed_record_sbuf, (int)(c_compressed_record_cptr - c_compressed_record_sbuf));
2953 c_compressed_record_cptr = c_compressed_record_sbuf;
2954 }
2955 #endif
2956 if (c_size) {
2957 OSAddAtomic64(c_size, &c_segment_compressed_bytes);
2958 OSAddAtomic64(c_rounded_size, &compressor_bytes_used);
2959 }
2960 OSAddAtomic64(PAGE_SIZE, &c_segment_input_bytes);
2961
2962 OSAddAtomic(1, &c_segment_pages_compressed);
2963 OSAddAtomic(1, &sample_period_compression_count);
2964
2965 KERNEL_DEBUG(0xe0400000 | DBG_FUNC_END, *current_chead, c_size, c_segment_input_bytes, c_segment_compressed_bytes, 0);
2966
2967 return (0);
2968 }
2969
2970
2971 static int
2972 c_decompress_page(char *dst, volatile c_slot_mapping_t slot_ptr, int flags, int *zeroslot)
2973 {
2974 c_slot_t cs;
2975 c_segment_t c_seg;
2976 int c_indx;
2977 int c_rounded_size;
2978 uint32_t c_size;
2979 int retval = 0;
2980 boolean_t need_unlock = TRUE;
2981 boolean_t consider_defragmenting = FALSE;
2982 boolean_t kdp_mode = FALSE;
2983
2984 if (flags & C_KDP) {
2985 if (not_in_kdp) {
2986 panic("C_KDP passed to decompress page from outside of debugger context");
2987 }
2988
2989 assert((flags & C_KEEP) == C_KEEP);
2990 assert((flags & C_DONT_BLOCK) == C_DONT_BLOCK);
2991
2992 if ((flags & (C_DONT_BLOCK | C_KEEP)) != (C_DONT_BLOCK | C_KEEP)) {
2993 return (-2);
2994 }
2995
2996 kdp_mode = TRUE;
2997 }
2998
2999 ReTry:
3000 if (!kdp_mode) {
3001 PAGE_REPLACEMENT_DISALLOWED(TRUE);
3002 } else {
3003 if (kdp_lck_rw_lock_is_acquired_exclusive(&c_master_lock)) {
3004 return (-2);
3005 }
3006 }
3007
3008 #if HIBERNATION
3009 /*
3010 * if hibernation is enabled, it indicates (via a call
3011 * to 'vm_decompressor_lock' that no further
3012 * decompressions are allowed once it reaches
3013 * the point of flushing all of the currently dirty
3014 * anonymous memory through the compressor and out
3015 * to disk... in this state we allow freeing of compressed
3016 * pages and must honor the C_DONT_BLOCK case
3017 */
3018 if (dst && decompressions_blocked == TRUE) {
3019 if (flags & C_DONT_BLOCK) {
3020
3021 if (!kdp_mode) {
3022 PAGE_REPLACEMENT_DISALLOWED(FALSE);
3023 }
3024
3025 *zeroslot = 0;
3026 return (-2);
3027 }
3028 /*
3029 * it's safe to atomically assert and block behind the
3030 * lock held in shared mode because "decompressions_blocked" is
3031 * only set and cleared and the thread_wakeup done when the lock
3032 * is held exclusively
3033 */
3034 assert_wait((event_t)&decompressions_blocked, THREAD_UNINT);
3035
3036 PAGE_REPLACEMENT_DISALLOWED(FALSE);
3037
3038 thread_block(THREAD_CONTINUE_NULL);
3039
3040 goto ReTry;
3041 }
3042 #endif
3043 /* s_cseg is actually "segno+1" */
3044 c_seg = c_segments[slot_ptr->s_cseg - 1].c_seg;
3045
3046 if (!kdp_mode) {
3047 lck_mtx_lock_spin_always(&c_seg->c_lock);
3048 } else {
3049 if (kdp_lck_mtx_lock_spin_is_acquired(&c_seg->c_lock)) {
3050 return (-2);
3051 }
3052 }
3053
3054 assert(c_seg->c_state != C_IS_EMPTY && c_seg->c_state != C_IS_FREE);
3055
3056 if (flags & C_DONT_BLOCK) {
3057 if (c_seg->c_busy || (C_SEG_IS_ONDISK(c_seg) && dst)) {
3058 *zeroslot = 0;
3059
3060 retval = -2;
3061 goto done;
3062 }
3063 }
3064 if (c_seg->c_busy) {
3065
3066 PAGE_REPLACEMENT_DISALLOWED(FALSE);
3067
3068 c_seg_wait_on_busy(c_seg);
3069
3070 goto ReTry;
3071 }
3072 c_indx = slot_ptr->s_cindx;
3073
3074 cs = C_SEG_SLOT_FROM_INDEX(c_seg, c_indx);
3075
3076 c_size = UNPACK_C_SIZE(cs);
3077
3078 c_rounded_size = (c_size + C_SEG_OFFSET_ALIGNMENT_MASK) & ~C_SEG_OFFSET_ALIGNMENT_MASK;
3079
3080 if (dst) {
3081 uint32_t age_of_cseg;
3082 clock_sec_t cur_ts_sec;
3083 clock_nsec_t cur_ts_nsec;
3084
3085 if (C_SEG_IS_ONDISK(c_seg)) {
3086 assert(kdp_mode == FALSE);
3087 c_seg_swapin(c_seg, FALSE);
3088
3089 retval = 1;
3090 }
3091 if (c_seg->c_state == C_ON_BAD_Q) {
3092 assert(c_seg->c_store.c_buffer == NULL);
3093
3094 retval = -1;
3095 goto c_seg_invalid_data;
3096 }
3097 #if CHECKSUM_THE_COMPRESSED_DATA
3098 if (cs->c_hash_compressed_data != hash_string((char *)&c_seg->c_store.c_buffer[cs->c_offset], c_size))
3099 panic("compressed data doesn't match original");
3100 #endif
3101 if (c_rounded_size == PAGE_SIZE) {
3102 /*
3103 * page wasn't compressible... just copy it out
3104 */
3105 memcpy(dst, &c_seg->c_store.c_buffer[cs->c_offset], PAGE_SIZE);
3106 } else if (c_size == 4) {
3107 int32_t data;
3108 int32_t *dptr;
3109
3110 /*
3111 * page was populated with a single value
3112 * that didn't fit into our fast hash
3113 * so we packed it in as a single non-compressed value
3114 * that we need to populate the page with
3115 */
3116 dptr = (int32_t *)(uintptr_t)dst;
3117 data = *(int32_t *)(&c_seg->c_store.c_buffer[cs->c_offset]);
3118 #if __x86_64__
3119 memset_word(dptr, data, PAGE_SIZE / sizeof(int32_t));
3120 #else
3121 {
3122 int i;
3123
3124 for (i = 0; i < (int)(PAGE_SIZE / sizeof(int32_t)); i++)
3125 *dptr++ = data;
3126 }
3127 #endif
3128 } else {
3129 uint32_t my_cpu_no;
3130 char *scratch_buf;
3131
3132 if (!kdp_mode) {
3133 /*
3134 * we're behind the c_seg lock held in spin mode
3135 * which means pre-emption is disabled... therefore
3136 * the following sequence is atomic and safe
3137 */
3138 my_cpu_no = cpu_number();
3139
3140 assert(my_cpu_no < compressor_cpus);
3141
3142 scratch_buf = &compressor_scratch_bufs[my_cpu_no * WKdm_SCRATCH_BUF_SIZE];
3143 } else {
3144 scratch_buf = kdp_compressor_scratch_buf;
3145 }
3146 WKdm_decompress_new((WK_word *)(uintptr_t)&c_seg->c_store.c_buffer[cs->c_offset],
3147 (WK_word *)(uintptr_t)dst, (WK_word *)(uintptr_t)scratch_buf, c_size);
3148 }
3149
3150 #if CHECKSUM_THE_DATA
3151 if (cs->c_hash_data != hash_string(dst, PAGE_SIZE))
3152 panic("decompressed data doesn't match original");
3153 #endif
3154 if (c_seg->c_swappedin_ts == 0 && !kdp_mode) {
3155
3156 clock_get_system_nanotime(&cur_ts_sec, &cur_ts_nsec);
3157
3158 age_of_cseg = (uint32_t)cur_ts_sec - c_seg->c_creation_ts;
3159
3160 if (age_of_cseg < DECOMPRESSION_SAMPLE_MAX_AGE)
3161 OSAddAtomic(1, &age_of_decompressions_during_sample_period[age_of_cseg]);
3162 else
3163 OSAddAtomic(1, &overage_decompressions_during_sample_period);
3164
3165 OSAddAtomic(1, &sample_period_decompression_count);
3166 }
3167 }
3168 c_seg_invalid_data:
3169
3170 if (flags & C_KEEP) {
3171 *zeroslot = 0;
3172 goto done;
3173 }
3174
3175 assert(kdp_mode == FALSE);
3176 c_seg->c_bytes_unused += c_rounded_size;
3177 c_seg->c_bytes_used -= c_rounded_size;
3178 PACK_C_SIZE(cs, 0);
3179
3180 if (c_indx < c_seg->c_firstemptyslot)
3181 c_seg->c_firstemptyslot = c_indx;
3182
3183 OSAddAtomic(-1, &c_segment_pages_compressed);
3184
3185 if (c_seg->c_state != C_ON_BAD_Q && !(C_SEG_IS_ONDISK(c_seg))) {
3186 /*
3187 * C_SEG_IS_ONDISK == TRUE can occur when we're doing a
3188 * free of a compressed page (i.e. dst == NULL)
3189 */
3190 OSAddAtomic64(-c_rounded_size, &compressor_bytes_used);
3191 }
3192 if (c_seg->c_state != C_IS_FILLING) {
3193 if (c_seg->c_bytes_used == 0) {
3194 if ( !(C_SEG_IS_ONDISK(c_seg))) {
3195 int pages_populated;
3196
3197 pages_populated = (round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg->c_populated_offset))) / PAGE_SIZE;
3198 c_seg->c_populated_offset = C_SEG_BYTES_TO_OFFSET(0);
3199
3200 if (pages_populated) {
3201
3202 assert(c_seg->c_state != C_ON_BAD_Q);
3203 assert(c_seg->c_store.c_buffer != NULL);
3204
3205 C_SEG_BUSY(c_seg);
3206 lck_mtx_unlock_always(&c_seg->c_lock);
3207
3208 kernel_memory_depopulate(kernel_map, (vm_offset_t) c_seg->c_store.c_buffer, pages_populated * PAGE_SIZE, KMA_COMPRESSOR);
3209
3210 lck_mtx_lock_spin_always(&c_seg->c_lock);
3211 C_SEG_WAKEUP_DONE(c_seg);
3212 }
3213 if (!c_seg->c_on_minorcompact_q)
3214 c_seg_need_delayed_compaction(c_seg);
3215 } else
3216 assert(c_seg->c_state == C_ON_SWAPPEDOUTSPARSE_Q);
3217
3218 } else if (c_seg->c_on_minorcompact_q) {
3219
3220 assert(c_seg->c_state != C_ON_BAD_Q);
3221
3222 if (C_SEG_SHOULD_MINORCOMPACT(c_seg)) {
3223 c_seg_try_minor_compaction_and_unlock(c_seg);
3224 need_unlock = FALSE;
3225 }
3226 } else if ( !(C_SEG_IS_ONDISK(c_seg))) {
3227
3228 if (c_seg->c_state != C_ON_BAD_Q && c_seg->c_state != C_ON_SWAPOUT_Q && C_SEG_UNUSED_BYTES(c_seg) >= PAGE_SIZE) {
3229 c_seg_need_delayed_compaction(c_seg);
3230 }
3231 } else if (c_seg->c_state != C_ON_SWAPPEDOUTSPARSE_Q && C_SEG_ONDISK_IS_SPARSE(c_seg)) {
3232
3233 c_seg_move_to_sparse_list(c_seg);
3234 consider_defragmenting = TRUE;
3235 }
3236 }
3237 done:
3238 if (kdp_mode) {
3239 return retval;
3240 }
3241
3242 if (need_unlock == TRUE)
3243 lck_mtx_unlock_always(&c_seg->c_lock);
3244
3245 PAGE_REPLACEMENT_DISALLOWED(FALSE);
3246
3247 if (consider_defragmenting == TRUE)
3248 vm_swap_consider_defragmenting();
3249
3250
3251 return (retval);
3252 }
3253
3254
3255 int
3256 vm_compressor_get(ppnum_t pn, int *slot, int flags)
3257 {
3258 c_slot_mapping_t slot_ptr;
3259 char *dst;
3260 int zeroslot = 1;
3261 int retval;
3262
3263 #if __x86_64__
3264 dst = PHYSMAP_PTOV((uint64_t)pn << (uint64_t)PAGE_SHIFT);
3265 #else
3266 #error "unsupported architecture"
3267 #endif
3268 slot_ptr = (c_slot_mapping_t)slot;
3269
3270 if (slot_ptr->s_cseg == C_SV_CSEG_ID) {
3271 int32_t data;
3272 int32_t *dptr;
3273
3274 /*
3275 * page was populated with a single value
3276 * that found a home in our hash table
3277 * grab that value from the hash and populate the page
3278 * that we need to populate the page with
3279 */
3280 dptr = (int32_t *)(uintptr_t)dst;
3281 data = c_segment_sv_hash_table[slot_ptr->s_cindx].he_data;
3282 #if __x86_64__
3283 memset_word(dptr, data, PAGE_SIZE / sizeof(int32_t));
3284 #else
3285 {
3286 int i;
3287
3288 for (i = 0; i < (int)(PAGE_SIZE / sizeof(int32_t)); i++)
3289 *dptr++ = data;
3290 }
3291 #endif
3292 c_segment_sv_hash_drop_ref(slot_ptr->s_cindx);
3293
3294 if ( !(flags & C_KEEP)) {
3295 OSAddAtomic(-1, &c_segment_pages_compressed);
3296 *slot = 0;
3297 }
3298 if (data)
3299 OSAddAtomic(1, &c_segment_svp_nonzero_decompressions);
3300 else
3301 OSAddAtomic(1, &c_segment_svp_zero_decompressions);
3302
3303 return (0);
3304 }
3305
3306 retval = c_decompress_page(dst, slot_ptr, flags, &zeroslot);
3307
3308 /*
3309 * zeroslot will be set to 0 by c_decompress_page if (flags & C_KEEP)
3310 * or (flags & C_DONT_BLOCK) and we found 'c_busy' or 'C_SEG_IS_ONDISK' to be TRUE
3311 */
3312 if (zeroslot) {
3313 *slot = 0;
3314 }
3315 /*
3316 * returns 0 if we successfully decompressed a page from a segment already in memory
3317 * returns 1 if we had to first swap in the segment, before successfully decompressing the page
3318 * returns -1 if we encountered an error swapping in the segment - decompression failed
3319 * returns -2 if (flags & C_DONT_BLOCK) and we found 'c_busy' or 'C_SEG_IS_ONDISK' to be true
3320 */
3321 return (retval);
3322 }
3323
3324
3325 int
3326 vm_compressor_free(int *slot, int flags)
3327 {
3328 c_slot_mapping_t slot_ptr;
3329 int zeroslot = 1;
3330 int retval;
3331
3332 assert(flags == 0 || flags == C_DONT_BLOCK);
3333
3334 slot_ptr = (c_slot_mapping_t)slot;
3335
3336 if (slot_ptr->s_cseg == C_SV_CSEG_ID) {
3337
3338 c_segment_sv_hash_drop_ref(slot_ptr->s_cindx);
3339 OSAddAtomic(-1, &c_segment_pages_compressed);
3340
3341 *slot = 0;
3342 return (0);
3343 }
3344 retval = c_decompress_page(NULL, slot_ptr, flags, &zeroslot);
3345 /*
3346 * returns 0 if we successfully freed the specified compressed page
3347 * returns -2 if (flags & C_DONT_BLOCK) and we found 'c_busy' set
3348 */
3349
3350 if (retval == 0)
3351 *slot = 0;
3352 else
3353 assert(retval == -2);
3354
3355 return (retval);
3356 }
3357
3358
3359 int
3360 vm_compressor_put(ppnum_t pn, int *slot, void **current_chead, char *scratch_buf)
3361 {
3362 char *src;
3363 int retval;
3364
3365 #if __x86_64__
3366 src = PHYSMAP_PTOV((uint64_t)pn << (uint64_t)PAGE_SHIFT);
3367 #else
3368 #error "unsupported architecture"
3369 #endif
3370 retval = c_compress_page(src, (c_slot_mapping_t)slot, (c_segment_t *)current_chead, scratch_buf);
3371
3372 return (retval);
3373 }
3374
3375 void
3376 vm_compressor_transfer(
3377 int *dst_slot_p,
3378 int *src_slot_p)
3379 {
3380 c_slot_mapping_t dst_slot, src_slot;
3381 c_segment_t c_seg;
3382 int c_indx;
3383 c_slot_t cs;
3384
3385 src_slot = (c_slot_mapping_t) src_slot_p;
3386
3387 if (src_slot->s_cseg == C_SV_CSEG_ID) {
3388 *dst_slot_p = *src_slot_p;
3389 *src_slot_p = 0;
3390 return;
3391 }
3392 dst_slot = (c_slot_mapping_t) dst_slot_p;
3393 Retry:
3394 PAGE_REPLACEMENT_DISALLOWED(TRUE);
3395 /* get segment for src_slot */
3396 c_seg = c_segments[src_slot->s_cseg -1].c_seg;
3397 /* lock segment */
3398 lck_mtx_lock_spin_always(&c_seg->c_lock);
3399 /* wait if it's busy */
3400 if (c_seg->c_busy && !c_seg->c_busy_swapping) {
3401 PAGE_REPLACEMENT_DISALLOWED(FALSE);
3402 c_seg_wait_on_busy(c_seg);
3403 goto Retry;
3404 }
3405 /* find the c_slot */
3406 c_indx = src_slot->s_cindx;
3407 cs = C_SEG_SLOT_FROM_INDEX(c_seg, c_indx);
3408 /* point the c_slot back to dst_slot instead of src_slot */
3409 cs->c_packed_ptr = C_SLOT_PACK_PTR(dst_slot);
3410 /* transfer */
3411 *dst_slot_p = *src_slot_p;
3412 *src_slot_p = 0;
3413 lck_mtx_unlock_always(&c_seg->c_lock);
3414 PAGE_REPLACEMENT_DISALLOWED(FALSE);
3415 }
3416
3417 #if CONFIG_FREEZE
3418
3419 int freezer_finished_filling = 0;
3420
3421 void
3422 vm_compressor_finished_filling(
3423 void **current_chead)
3424 {
3425 c_segment_t c_seg;
3426
3427 if ((c_seg = *(c_segment_t *)current_chead) == NULL)
3428 return;
3429
3430 assert(c_seg->c_state == C_IS_FILLING);
3431
3432 lck_mtx_lock_spin_always(&c_seg->c_lock);
3433
3434 c_current_seg_filled(c_seg, (c_segment_t *)current_chead);
3435
3436 lck_mtx_unlock_always(&c_seg->c_lock);
3437
3438 freezer_finished_filling++;
3439 }
3440
3441
3442 /*
3443 * This routine is used to transfer the compressed chunks from
3444 * the c_seg/cindx pointed to by slot_p into a new c_seg headed
3445 * by the current_chead and a new cindx within that c_seg.
3446 *
3447 * Currently, this routine is only used by the "freezer backed by
3448 * compressor with swap" mode to create a series of c_segs that
3449 * only contain compressed data belonging to one task. So, we
3450 * move a task's previously compressed data into a set of new
3451 * c_segs which will also hold the task's yet to be compressed data.
3452 */
3453
3454 kern_return_t
3455 vm_compressor_relocate(
3456 void **current_chead,
3457 int *slot_p)
3458 {
3459 c_slot_mapping_t slot_ptr;
3460 c_slot_mapping_t src_slot;
3461 uint32_t c_rounded_size;
3462 uint32_t c_size;
3463 uint16_t dst_slot;
3464 c_slot_t c_dst;
3465 c_slot_t c_src;
3466 int c_indx;
3467 c_segment_t c_seg_dst = NULL;
3468 c_segment_t c_seg_src = NULL;
3469 kern_return_t kr = KERN_SUCCESS;
3470
3471
3472 src_slot = (c_slot_mapping_t) slot_p;
3473
3474 if (src_slot->s_cseg == C_SV_CSEG_ID) {
3475 /*
3476 * no need to relocate... this is a page full of a single
3477 * value which is hashed to a single entry not contained
3478 * in a c_segment_t
3479 */
3480 return (kr);
3481 }
3482
3483 Relookup_dst:
3484 c_seg_dst = c_seg_allocate((c_segment_t *)current_chead);
3485 /*
3486 * returns with c_seg lock held
3487 * and PAGE_REPLACEMENT_DISALLOWED(TRUE)...
3488 * c_nextslot has been allocated and
3489 * c_store.c_buffer populated
3490 */
3491 if (c_seg_dst == NULL) {
3492 /*
3493 * Out of compression segments?
3494 */
3495 kr = KERN_RESOURCE_SHORTAGE;
3496 goto out;
3497 }
3498
3499 assert(c_seg_dst->c_busy == 0);
3500
3501 C_SEG_BUSY(c_seg_dst);
3502
3503 dst_slot = c_seg_dst->c_nextslot;
3504
3505 lck_mtx_unlock_always(&c_seg_dst->c_lock);
3506
3507 Relookup_src:
3508 c_seg_src = c_segments[src_slot->s_cseg - 1].c_seg;
3509
3510 assert(c_seg_dst != c_seg_src);
3511
3512 lck_mtx_lock_spin_always(&c_seg_src->c_lock);
3513
3514 if (C_SEG_IS_ONDISK(c_seg_src)) {
3515
3516 /*
3517 * A "thaw" can mark a process as eligible for
3518 * another freeze cycle without bringing any of
3519 * its swapped out c_segs back from disk (because
3520 * that is done on-demand).
3521 *
3522 * If the src c_seg we find for our pre-compressed
3523 * data is already on-disk, then we are dealing
3524 * with an app's data that is already packed and
3525 * swapped out. Don't do anything.
3526 */
3527
3528 PAGE_REPLACEMENT_DISALLOWED(FALSE);
3529
3530 lck_mtx_unlock_always(&c_seg_src->c_lock);
3531
3532 c_seg_src = NULL;
3533
3534 goto out;
3535 }
3536
3537 if (c_seg_src->c_busy) {
3538
3539 PAGE_REPLACEMENT_DISALLOWED(FALSE);
3540 c_seg_wait_on_busy(c_seg_src);
3541
3542 c_seg_src = NULL;
3543
3544 PAGE_REPLACEMENT_DISALLOWED(TRUE);
3545
3546 goto Relookup_src;
3547 }
3548
3549 C_SEG_BUSY(c_seg_src);
3550
3551 lck_mtx_unlock_always(&c_seg_src->c_lock);
3552
3553 PAGE_REPLACEMENT_DISALLOWED(FALSE);
3554
3555 /* find the c_slot */
3556 c_indx = src_slot->s_cindx;
3557
3558 c_src = C_SEG_SLOT_FROM_INDEX(c_seg_src, c_indx);
3559
3560 c_size = UNPACK_C_SIZE(c_src);
3561
3562 assert(c_size);
3563
3564 if (c_size > (uint32_t)(C_SEG_BUFSIZE - C_SEG_OFFSET_TO_BYTES((int32_t)c_seg_dst->c_nextoffset))) {
3565 /*
3566 * This segment is full. We need a new one.
3567 */
3568
3569 PAGE_REPLACEMENT_DISALLOWED(TRUE);
3570
3571 lck_mtx_lock_spin_always(&c_seg_src->c_lock);
3572 C_SEG_WAKEUP_DONE(c_seg_src);
3573 lck_mtx_unlock_always(&c_seg_src->c_lock);
3574
3575 c_seg_src = NULL;
3576
3577 lck_mtx_lock_spin_always(&c_seg_dst->c_lock);
3578
3579 assert(c_seg_dst->c_busy);
3580 assert(c_seg_dst->c_state == C_IS_FILLING);
3581 assert(!c_seg_dst->c_on_minorcompact_q);
3582
3583 c_current_seg_filled(c_seg_dst, (c_segment_t *)current_chead);
3584 assert(*current_chead == NULL);
3585
3586 C_SEG_WAKEUP_DONE(c_seg_dst);
3587
3588 lck_mtx_unlock_always(&c_seg_dst->c_lock);
3589
3590 c_seg_dst = NULL;
3591
3592 PAGE_REPLACEMENT_DISALLOWED(FALSE);
3593
3594 goto Relookup_dst;
3595 }
3596
3597 c_dst = C_SEG_SLOT_FROM_INDEX(c_seg_dst, c_seg_dst->c_nextslot);
3598
3599 memcpy(&c_seg_dst->c_store.c_buffer[c_seg_dst->c_nextoffset], &c_seg_src->c_store.c_buffer[c_src->c_offset], c_size);
3600
3601 c_rounded_size = (c_size + C_SEG_OFFSET_ALIGNMENT_MASK) & ~C_SEG_OFFSET_ALIGNMENT_MASK;
3602
3603 #if CHECKSUM_THE_DATA
3604 c_dst->c_hash_data = c_src->c_hash_data;
3605 #endif
3606 #if CHECKSUM_THE_COMPRESSED_DATA
3607 c_dst->c_hash_compressed_data = c_src->c_hash_compressed_data;
3608 #endif
3609
3610 c_dst->c_size = c_src->c_size;
3611 c_dst->c_packed_ptr = c_src->c_packed_ptr;
3612 c_dst->c_offset = c_seg_dst->c_nextoffset;
3613
3614 if (c_seg_dst->c_firstemptyslot == c_seg_dst->c_nextslot)
3615 c_seg_dst->c_firstemptyslot++;
3616
3617 c_seg_dst->c_nextslot++;
3618 c_seg_dst->c_bytes_used += c_rounded_size;
3619 c_seg_dst->c_nextoffset += C_SEG_BYTES_TO_OFFSET(c_rounded_size);
3620
3621
3622 PACK_C_SIZE(c_src, 0);
3623
3624 c_seg_src->c_bytes_used -= c_rounded_size;
3625 c_seg_src->c_bytes_unused += c_rounded_size;
3626
3627 if (c_indx < c_seg_src->c_firstemptyslot) {
3628 c_seg_src->c_firstemptyslot = c_indx;
3629 }
3630
3631 c_dst = C_SEG_SLOT_FROM_INDEX(c_seg_dst, dst_slot);
3632
3633 PAGE_REPLACEMENT_ALLOWED(TRUE);
3634 slot_ptr = (c_slot_mapping_t)C_SLOT_UNPACK_PTR(c_dst);
3635 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
3636 slot_ptr->s_cseg = c_seg_dst->c_mysegno + 1;
3637 slot_ptr->s_cindx = dst_slot;
3638
3639 PAGE_REPLACEMENT_ALLOWED(FALSE);
3640
3641 out:
3642 if (c_seg_src) {
3643
3644 lck_mtx_lock_spin_always(&c_seg_src->c_lock);
3645
3646 C_SEG_WAKEUP_DONE(c_seg_src);
3647
3648 if (c_seg_src->c_bytes_used == 0 && c_seg_src->c_state != C_IS_FILLING) {
3649 if (!c_seg_src->c_on_minorcompact_q)
3650 c_seg_need_delayed_compaction(c_seg_src);
3651 }
3652
3653 lck_mtx_unlock_always(&c_seg_src->c_lock);
3654 }
3655
3656 if (c_seg_dst) {
3657
3658 PAGE_REPLACEMENT_DISALLOWED(TRUE);
3659
3660 lck_mtx_lock_spin_always(&c_seg_dst->c_lock);
3661
3662 if (c_seg_dst->c_nextoffset >= C_SEG_OFF_LIMIT || c_seg_dst->c_nextslot >= C_SLOT_MAX_INDEX) {
3663 /*
3664 * Nearing or exceeded maximum slot and offset capacity.
3665 */
3666 assert(c_seg_dst->c_busy);
3667 assert(c_seg_dst->c_state == C_IS_FILLING);
3668 assert(!c_seg_dst->c_on_minorcompact_q);
3669
3670 c_current_seg_filled(c_seg_dst, (c_segment_t *)current_chead);
3671 assert(*current_chead == NULL);
3672 }
3673
3674 C_SEG_WAKEUP_DONE(c_seg_dst);
3675
3676 lck_mtx_unlock_always(&c_seg_dst->c_lock);
3677
3678 c_seg_dst = NULL;
3679
3680 PAGE_REPLACEMENT_DISALLOWED(FALSE);
3681 }
3682
3683 return kr;
3684 }
3685 #endif /* CONFIG_FREEZE */