2 * Copyright (c) 2006-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Memory allocator with per-CPU caching, derived from the kmem magazine
31 * concept and implementation as described in the following paper:
32 * http://www.usenix.org/events/usenix01/full_papers/bonwick/bonwick.pdf
33 * That implementation is Copyright 2006 Sun Microsystems, Inc. All rights
34 * reserved. Use is subject to license terms.
36 * There are several major differences between this and the original kmem
37 * magazine: this derivative implementation allows for multiple objects to
38 * be allocated and freed from/to the object cache in one call; in addition,
39 * it provides for better flexibility where the user is allowed to define
40 * its own slab allocator (instead of the default zone allocator). Finally,
41 * no object construction/destruction takes place at the moment, although
42 * this could be added in future to improve efficiency.
45 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <sys/malloc.h>
49 #include <sys/queue.h>
50 #include <sys/kernel.h>
51 #include <sys/systm.h>
53 #include <kern/debug.h>
54 #include <kern/zalloc.h>
55 #include <kern/cpu_number.h>
56 #include <kern/locks.h>
57 #include <kern/thread_call.h>
59 #include <libkern/libkern.h>
60 #include <libkern/OSAtomic.h>
61 #include <libkern/OSDebug.h>
63 #include <mach/vm_param.h>
64 #include <machine/limits.h>
65 #include <machine/machine_routines.h>
69 #include <sys/mcache.h>
71 #define MCACHE_SIZE(n) \
72 __builtin_offsetof(mcache_t, mc_cpu[n])
74 /* Allocate extra in case we need to manually align the pointer */
75 #define MCACHE_ALLOC_SIZE \
76 (sizeof (void *) + MCACHE_SIZE(ncpu) + CPU_CACHE_LINE_SIZE)
78 #define MCACHE_CPU(c) \
79 (mcache_cpu_t *)((void *)((char *)(c) + MCACHE_SIZE(cpu_number())))
82 * MCACHE_LIST_LOCK() and MCACHE_LIST_UNLOCK() are macros used
83 * to serialize accesses to the global list of caches in the system.
84 * They also record the thread currently running in the critical
85 * section, so that we can avoid recursive requests to reap the
86 * caches when memory runs low.
88 #define MCACHE_LIST_LOCK() { \
89 lck_mtx_lock(mcache_llock); \
90 mcache_llock_owner = current_thread(); \
93 #define MCACHE_LIST_UNLOCK() { \
94 mcache_llock_owner = NULL; \
95 lck_mtx_unlock(mcache_llock); \
98 #define MCACHE_LOCK(l) lck_mtx_lock(l)
99 #define MCACHE_UNLOCK(l) lck_mtx_unlock(l)
100 #define MCACHE_LOCK_TRY(l) lck_mtx_try_lock(l)
103 static unsigned int cache_line_size
;
104 static lck_mtx_t
*mcache_llock
;
105 static struct thread
*mcache_llock_owner
;
106 static lck_attr_t
*mcache_llock_attr
;
107 static lck_grp_t
*mcache_llock_grp
;
108 static lck_grp_attr_t
*mcache_llock_grp_attr
;
109 static struct zone
*mcache_zone
;
110 static const uint32_t mcache_reap_interval
= 15;
111 static const uint32_t mcache_reap_interval_leeway
= 2;
112 static UInt32 mcache_reaping
;
113 static int mcache_ready
;
114 static int mcache_updating
;
116 static int mcache_bkt_contention
= 3;
118 static unsigned int mcache_flags
= MCF_DEBUG
;
120 static unsigned int mcache_flags
= 0;
123 int mca_trn_max
= MCA_TRN_MAX
;
125 #define DUMP_MCA_BUF_SIZE 512
126 static char *mca_dump_buf
;
128 static mcache_bkttype_t mcache_bkttype
[] = {
129 { 1, 4096, 32768, NULL
},
130 { 3, 2048, 16384, NULL
},
131 { 7, 1024, 12288, NULL
},
132 { 15, 256, 8192, NULL
},
133 { 31, 64, 4096, NULL
},
134 { 47, 0, 2048, NULL
},
135 { 63, 0, 1024, NULL
},
136 { 95, 0, 512, NULL
},
137 { 143, 0, 256, NULL
},
141 static mcache_t
*mcache_create_common(const char *, size_t, size_t,
142 mcache_allocfn_t
, mcache_freefn_t
, mcache_auditfn_t
, mcache_logfn_t
,
143 mcache_notifyfn_t
, void *, u_int32_t
, int, int);
144 static unsigned int mcache_slab_alloc(void *, mcache_obj_t
***,
146 static void mcache_slab_free(void *, mcache_obj_t
*, boolean_t
);
147 static void mcache_slab_audit(void *, mcache_obj_t
*, boolean_t
);
148 static void mcache_cpu_refill(mcache_cpu_t
*, mcache_bkt_t
*, int);
149 static mcache_bkt_t
*mcache_bkt_alloc(mcache_t
*, mcache_bktlist_t
*,
150 mcache_bkttype_t
**);
151 static void mcache_bkt_free(mcache_t
*, mcache_bktlist_t
*, mcache_bkt_t
*);
152 static void mcache_cache_bkt_enable(mcache_t
*);
153 static void mcache_bkt_purge(mcache_t
*);
154 static void mcache_bkt_destroy(mcache_t
*, mcache_bkttype_t
*,
155 mcache_bkt_t
*, int);
156 static void mcache_bkt_ws_update(mcache_t
*);
157 static void mcache_bkt_ws_zero(mcache_t
*);
158 static void mcache_bkt_ws_reap(mcache_t
*);
159 static void mcache_dispatch(void (*)(void *), void *);
160 static void mcache_cache_reap(mcache_t
*);
161 static void mcache_cache_update(mcache_t
*);
162 static void mcache_cache_bkt_resize(void *);
163 static void mcache_cache_enable(void *);
164 static void mcache_update(thread_call_param_t __unused
, thread_call_param_t __unused
);
165 static void mcache_update_timeout(void *);
166 static void mcache_applyall(void (*)(mcache_t
*));
167 static void mcache_reap_start(void *);
168 static void mcache_reap_done(void *);
169 static void mcache_reap_timeout(thread_call_param_t __unused
, thread_call_param_t
);
170 static void mcache_notify(mcache_t
*, u_int32_t
);
171 static void mcache_purge(void *);
173 static LIST_HEAD(, mcache
) mcache_head
;
174 mcache_t
*mcache_audit_cache
;
176 static thread_call_t mcache_reap_tcall
;
177 static thread_call_t mcache_update_tcall
;
180 * Initialize the framework; this is currently called as part of BSD init.
182 __private_extern__
void
185 mcache_bkttype_t
*btp
;
189 VERIFY(mca_trn_max
>= 2);
191 ncpu
= ml_get_max_cpus();
192 (void) mcache_cache_line_size(); /* prime it */
194 mcache_llock_grp_attr
= lck_grp_attr_alloc_init();
195 mcache_llock_grp
= lck_grp_alloc_init("mcache.list",
196 mcache_llock_grp_attr
);
197 mcache_llock_attr
= lck_attr_alloc_init();
198 mcache_llock
= lck_mtx_alloc_init(mcache_llock_grp
, mcache_llock_attr
);
200 mcache_reap_tcall
= thread_call_allocate(mcache_reap_timeout
, NULL
);
201 mcache_update_tcall
= thread_call_allocate(mcache_update
, NULL
);
202 if (mcache_reap_tcall
== NULL
|| mcache_update_tcall
== NULL
)
203 panic("mcache_init: thread_call_allocate failed");
205 mcache_zone
= zinit(MCACHE_ALLOC_SIZE
, 256 * MCACHE_ALLOC_SIZE
,
206 PAGE_SIZE
, "mcache");
207 if (mcache_zone
== NULL
)
208 panic("mcache_init: failed to allocate mcache zone\n");
209 zone_change(mcache_zone
, Z_CALLERACCT
, FALSE
);
211 LIST_INIT(&mcache_head
);
213 for (i
= 0; i
< sizeof (mcache_bkttype
) / sizeof (*btp
); i
++) {
214 btp
= &mcache_bkttype
[i
];
215 (void) snprintf(name
, sizeof (name
), "bkt_%d",
217 btp
->bt_cache
= mcache_create(name
,
218 (btp
->bt_bktsize
+ 1) * sizeof (void *), 0, 0, MCR_SLEEP
);
221 PE_parse_boot_argn("mcache_flags", &mcache_flags
, sizeof(mcache_flags
));
222 mcache_flags
&= MCF_FLAGS_MASK
;
224 mcache_audit_cache
= mcache_create("audit", sizeof (mcache_audit_t
),
227 mcache_applyall(mcache_cache_bkt_enable
);
230 printf("mcache: %d CPU(s), %d bytes CPU cache line size\n",
231 ncpu
, CPU_CACHE_LINE_SIZE
);
235 * Return the global mcache flags.
237 __private_extern__
unsigned int
238 mcache_getflags(void)
240 return (mcache_flags
);
244 * Return the CPU cache line size.
246 __private_extern__
unsigned int
247 mcache_cache_line_size(void)
249 if (cache_line_size
== 0) {
250 ml_cpu_info_t cpu_info
;
251 ml_cpu_get_info(&cpu_info
);
252 cache_line_size
= cpu_info
.cache_line_size
;
254 return (cache_line_size
);
258 * Create a cache using the zone allocator as the backend slab allocator.
259 * The caller may specify any alignment for the object; if it specifies 0
260 * the default alignment (MCACHE_ALIGN) will be used.
262 __private_extern__ mcache_t
*
263 mcache_create(const char *name
, size_t bufsize
, size_t align
,
264 u_int32_t flags
, int wait
)
266 return (mcache_create_common(name
, bufsize
, align
, mcache_slab_alloc
,
267 mcache_slab_free
, mcache_slab_audit
, NULL
, NULL
, NULL
, flags
, 1,
272 * Create a cache using a custom backend slab allocator. Since the caller
273 * is responsible for allocation, no alignment guarantee will be provided
276 __private_extern__ mcache_t
*
277 mcache_create_ext(const char *name
, size_t bufsize
,
278 mcache_allocfn_t allocfn
, mcache_freefn_t freefn
, mcache_auditfn_t auditfn
,
279 mcache_logfn_t logfn
, mcache_notifyfn_t notifyfn
, void *arg
,
280 u_int32_t flags
, int wait
)
282 return (mcache_create_common(name
, bufsize
, 0, allocfn
,
283 freefn
, auditfn
, logfn
, notifyfn
, arg
, flags
, 0, wait
));
287 * Common cache creation routine.
290 mcache_create_common(const char *name
, size_t bufsize
, size_t align
,
291 mcache_allocfn_t allocfn
, mcache_freefn_t freefn
, mcache_auditfn_t auditfn
,
292 mcache_logfn_t logfn
, mcache_notifyfn_t notifyfn
, void *arg
,
293 u_int32_t flags
, int need_zone
, int wait
)
295 mcache_bkttype_t
*btp
;
302 /* If auditing is on and print buffer is NULL, allocate it now */
303 if ((flags
& MCF_DEBUG
) && mca_dump_buf
== NULL
) {
304 int malloc_wait
= (wait
& MCR_NOSLEEP
) ? M_NOWAIT
: M_WAITOK
;
305 MALLOC(mca_dump_buf
, char *, DUMP_MCA_BUF_SIZE
, M_TEMP
,
306 malloc_wait
| M_ZERO
);
307 if (mca_dump_buf
== NULL
)
311 buf
= zalloc(mcache_zone
);
315 bzero(buf
, MCACHE_ALLOC_SIZE
);
318 * In case we didn't get a cache-aligned memory, round it up
319 * accordingly. This is needed in order to get the rest of
320 * structure members aligned properly. It also means that
321 * the memory span gets shifted due to the round up, but it
322 * is okay since we've allocated extra space for this.
325 P2ROUNDUP((intptr_t)buf
+ sizeof (void *), CPU_CACHE_LINE_SIZE
);
326 pbuf
= (void **)((intptr_t)cp
- sizeof (void *));
330 * Guaranteed alignment is valid only when we use the internal
331 * slab allocator (currently set to use the zone allocator).
336 /* Enforce 64-bit minimum alignment for zone-based buffers */
338 align
= MCACHE_ALIGN
;
339 align
= P2ROUNDUP(align
, MCACHE_ALIGN
);
342 if ((align
& (align
- 1)) != 0)
343 panic("mcache_create: bad alignment %lu", align
);
345 cp
->mc_align
= align
;
346 cp
->mc_slab_alloc
= allocfn
;
347 cp
->mc_slab_free
= freefn
;
348 cp
->mc_slab_audit
= auditfn
;
349 cp
->mc_slab_log
= logfn
;
350 cp
->mc_slab_notify
= notifyfn
;
351 cp
->mc_private
= need_zone
? cp
: arg
;
352 cp
->mc_bufsize
= bufsize
;
353 cp
->mc_flags
= (flags
& MCF_FLAGS_MASK
) | mcache_flags
;
355 (void) snprintf(cp
->mc_name
, sizeof (cp
->mc_name
), "mcache.%s", name
);
357 (void) snprintf(lck_name
, sizeof (lck_name
), "%s.cpu", cp
->mc_name
);
358 cp
->mc_cpu_lock_grp_attr
= lck_grp_attr_alloc_init();
359 cp
->mc_cpu_lock_grp
= lck_grp_alloc_init(lck_name
,
360 cp
->mc_cpu_lock_grp_attr
);
361 cp
->mc_cpu_lock_attr
= lck_attr_alloc_init();
364 * Allocation chunk size is the object's size plus any extra size
365 * needed to satisfy the object's alignment. It is enforced to be
366 * at least the size of an LP64 pointer to simplify auditing and to
367 * handle multiple-element allocation requests, where the elements
368 * returned are linked together in a list.
370 chunksize
= MAX(bufsize
, sizeof (u_int64_t
));
372 VERIFY(align
!= 0 && (align
% MCACHE_ALIGN
) == 0);
373 chunksize
+= sizeof (uint64_t) + align
;
374 chunksize
= P2ROUNDUP(chunksize
, align
);
375 if ((cp
->mc_slab_zone
= zinit(chunksize
, 64 * 1024 * ncpu
,
376 PAGE_SIZE
, cp
->mc_name
)) == NULL
)
378 zone_change(cp
->mc_slab_zone
, Z_EXPAND
, TRUE
);
380 cp
->mc_chunksize
= chunksize
;
383 * Initialize the bucket layer.
385 (void) snprintf(lck_name
, sizeof (lck_name
), "%s.bkt", cp
->mc_name
);
386 cp
->mc_bkt_lock_grp_attr
= lck_grp_attr_alloc_init();
387 cp
->mc_bkt_lock_grp
= lck_grp_alloc_init(lck_name
,
388 cp
->mc_bkt_lock_grp_attr
);
389 cp
->mc_bkt_lock_attr
= lck_attr_alloc_init();
390 lck_mtx_init(&cp
->mc_bkt_lock
, cp
->mc_bkt_lock_grp
,
391 cp
->mc_bkt_lock_attr
);
393 (void) snprintf(lck_name
, sizeof (lck_name
), "%s.sync", cp
->mc_name
);
394 cp
->mc_sync_lock_grp_attr
= lck_grp_attr_alloc_init();
395 cp
->mc_sync_lock_grp
= lck_grp_alloc_init(lck_name
,
396 cp
->mc_sync_lock_grp_attr
);
397 cp
->mc_sync_lock_attr
= lck_attr_alloc_init();
398 lck_mtx_init(&cp
->mc_sync_lock
, cp
->mc_sync_lock_grp
,
399 cp
->mc_sync_lock_attr
);
401 for (btp
= mcache_bkttype
; chunksize
<= btp
->bt_minbuf
; btp
++)
404 cp
->cache_bkttype
= btp
;
407 * Initialize the CPU layer. Each per-CPU structure is aligned
408 * on the CPU cache line boundary to prevent false sharing.
410 for (c
= 0; c
< ncpu
; c
++) {
411 mcache_cpu_t
*ccp
= &cp
->mc_cpu
[c
];
413 VERIFY(IS_P2ALIGNED(ccp
, CPU_CACHE_LINE_SIZE
));
414 lck_mtx_init(&ccp
->cc_lock
, cp
->mc_cpu_lock_grp
,
415 cp
->mc_cpu_lock_attr
);
421 mcache_cache_bkt_enable(cp
);
423 /* TODO: dynamically create sysctl for stats */
426 LIST_INSERT_HEAD(&mcache_head
, cp
, mc_list
);
427 MCACHE_LIST_UNLOCK();
430 * If cache buckets are enabled and this is the first cache
431 * created, start the periodic cache update.
433 if (!(mcache_flags
& MCF_NOCPUCACHE
) && !mcache_updating
) {
435 mcache_update_timeout(NULL
);
437 if (cp
->mc_flags
& MCF_DEBUG
) {
438 printf("mcache_create: %s (%s) arg %p bufsize %lu align %lu "
439 "chunksize %lu bktsize %d\n", name
, need_zone
? "i" : "e",
440 arg
, bufsize
, cp
->mc_align
, chunksize
, btp
->bt_bktsize
);
446 zfree(mcache_zone
, buf
);
451 * Allocate one or more objects from a cache.
453 __private_extern__
unsigned int
454 mcache_alloc_ext(mcache_t
*cp
, mcache_obj_t
**list
, unsigned int num
, int wait
)
457 mcache_obj_t
**top
= &(*list
);
459 unsigned int need
= num
;
460 boolean_t nwretry
= FALSE
;
462 /* MCR_NOSLEEP and MCR_FAILOK are mutually exclusive */
463 VERIFY((wait
& (MCR_NOSLEEP
|MCR_FAILOK
)) != (MCR_NOSLEEP
|MCR_FAILOK
));
465 ASSERT(list
!= NULL
);
472 /* We may not always be running in the same CPU in case of retries */
473 ccp
= MCACHE_CPU(cp
);
475 MCACHE_LOCK(&ccp
->cc_lock
);
478 * If we have an object in the current CPU's filled bucket,
479 * chain the object to any previous objects and return if
480 * we've satisfied the number of requested objects.
482 if (ccp
->cc_objs
> 0) {
487 * Objects in the bucket are already linked together
488 * with the most recently freed object at the head of
489 * the list; grab as many objects as we can.
491 objs
= MIN((unsigned int)ccp
->cc_objs
, need
);
492 *list
= ccp
->cc_filled
->bkt_obj
[ccp
->cc_objs
- 1];
493 ccp
->cc_objs
-= objs
;
494 ccp
->cc_alloc
+= objs
;
496 tail
= ccp
->cc_filled
->bkt_obj
[ccp
->cc_objs
];
497 list
= &tail
->obj_next
;
500 /* If we got them all, return to caller */
501 if ((need
-= objs
) == 0) {
502 MCACHE_UNLOCK(&ccp
->cc_lock
);
504 if (!(cp
->mc_flags
& MCF_NOLEAKLOG
) &&
505 cp
->mc_slab_log
!= NULL
)
506 (*cp
->mc_slab_log
)(num
, *top
, TRUE
);
508 if (cp
->mc_flags
& MCF_DEBUG
)
516 * The CPU's filled bucket is empty. If the previous filled
517 * bucket was full, exchange and try again.
519 if (ccp
->cc_pobjs
> 0) {
520 mcache_cpu_refill(ccp
, ccp
->cc_pfilled
, ccp
->cc_pobjs
);
525 * If the bucket layer is disabled, allocate from slab. This
526 * can happen either because MCF_NOCPUCACHE is set, or because
527 * the bucket layer is currently being resized.
529 if (ccp
->cc_bktsize
== 0)
533 * Both of the CPU's buckets are empty; try to get a full
534 * bucket from the bucket layer. Upon success, refill this
535 * CPU and place any empty bucket into the empty list.
537 bkt
= mcache_bkt_alloc(cp
, &cp
->mc_full
, NULL
);
539 if (ccp
->cc_pfilled
!= NULL
)
540 mcache_bkt_free(cp
, &cp
->mc_empty
,
542 mcache_cpu_refill(ccp
, bkt
, ccp
->cc_bktsize
);
547 * The bucket layer has no full buckets; allocate the
548 * object(s) directly from the slab layer.
552 MCACHE_UNLOCK(&ccp
->cc_lock
);
554 need
-= (*cp
->mc_slab_alloc
)(cp
->mc_private
, &list
, need
, wait
);
557 * If this is a blocking allocation, or if it is non-blocking and
558 * the cache's full bucket is non-empty, then retry the allocation.
561 if (!(wait
& MCR_NONBLOCKING
)) {
562 atomic_add_32(&cp
->mc_wretry_cnt
, 1);
564 } else if ((wait
& (MCR_NOSLEEP
| MCR_TRYHARD
)) &&
565 !mcache_bkt_isempty(cp
)) {
568 atomic_add_32(&cp
->mc_nwretry_cnt
, 1);
570 } else if (nwretry
) {
571 atomic_add_32(&cp
->mc_nwfail_cnt
, 1);
575 if (!(cp
->mc_flags
& MCF_NOLEAKLOG
) && cp
->mc_slab_log
!= NULL
)
576 (*cp
->mc_slab_log
)((num
- need
), *top
, TRUE
);
578 if (!(cp
->mc_flags
& MCF_DEBUG
))
582 if (cp
->mc_flags
& MCF_DEBUG
) {
583 mcache_obj_t
**o
= top
;
588 * Verify that the chain of objects have the same count as
589 * what we are about to report to the caller. Any mismatch
590 * here means that the object list is insanely broken and
591 * therefore we must panic.
597 if (n
!= (num
- need
)) {
598 panic("mcache_alloc_ext: %s cp %p corrupted list "
599 "(got %d actual %d)\n", cp
->mc_name
,
600 (void *)cp
, num
- need
, n
);
604 /* Invoke the slab layer audit callback if auditing is enabled */
605 if ((cp
->mc_flags
& MCF_DEBUG
) && cp
->mc_slab_audit
!= NULL
)
606 (*cp
->mc_slab_audit
)(cp
->mc_private
, *top
, TRUE
);
612 * Allocate a single object from a cache.
614 __private_extern__
void *
615 mcache_alloc(mcache_t
*cp
, int wait
)
619 (void) mcache_alloc_ext(cp
, &buf
, 1, wait
);
623 __private_extern__
void
624 mcache_waiter_inc(mcache_t
*cp
)
626 atomic_add_32(&cp
->mc_waiter_cnt
, 1);
629 __private_extern__
void
630 mcache_waiter_dec(mcache_t
*cp
)
632 atomic_add_32(&cp
->mc_waiter_cnt
, -1);
635 __private_extern__ boolean_t
636 mcache_bkt_isempty(mcache_t
*cp
)
639 * This isn't meant to accurately tell whether there are
640 * any full buckets in the cache; it is simply a way to
641 * obtain "hints" about the state of the cache.
643 return (cp
->mc_full
.bl_total
== 0);
647 * Notify the slab layer about an event.
650 mcache_notify(mcache_t
*cp
, u_int32_t event
)
652 if (cp
->mc_slab_notify
!= NULL
)
653 (*cp
->mc_slab_notify
)(cp
->mc_private
, event
);
657 * Purge the cache and disable its buckets.
660 mcache_purge(void *arg
)
664 mcache_bkt_purge(cp
);
666 * We cannot simply call mcache_cache_bkt_enable() from here as
667 * a bucket resize may be in flight and we would cause the CPU
668 * layers of the cache to point to different sizes. Therefore,
669 * we simply increment the enable count so that during the next
670 * periodic cache update the buckets can be reenabled.
672 lck_mtx_lock_spin(&cp
->mc_sync_lock
);
674 lck_mtx_unlock(&cp
->mc_sync_lock
);
677 __private_extern__ boolean_t
678 mcache_purge_cache(mcache_t
*cp
, boolean_t async
)
681 * Purging a cache that has no per-CPU caches or is already
682 * in the process of being purged is rather pointless.
684 if (cp
->mc_flags
& MCF_NOCPUCACHE
)
687 lck_mtx_lock_spin(&cp
->mc_sync_lock
);
688 if (cp
->mc_purge_cnt
> 0) {
689 lck_mtx_unlock(&cp
->mc_sync_lock
);
693 lck_mtx_unlock(&cp
->mc_sync_lock
);
696 mcache_dispatch(mcache_purge
, cp
);
704 * Free a single object to a cache.
706 __private_extern__
void
707 mcache_free(mcache_t
*cp
, void *buf
)
709 ((mcache_obj_t
*)buf
)->obj_next
= NULL
;
710 mcache_free_ext(cp
, (mcache_obj_t
*)buf
);
714 * Free one or more objects to a cache.
716 __private_extern__
void
717 mcache_free_ext(mcache_t
*cp
, mcache_obj_t
*list
)
719 mcache_cpu_t
*ccp
= MCACHE_CPU(cp
);
720 mcache_bkttype_t
*btp
;
724 if (!(cp
->mc_flags
& MCF_NOLEAKLOG
) && cp
->mc_slab_log
!= NULL
)
725 (*cp
->mc_slab_log
)(0, list
, FALSE
);
727 /* Invoke the slab layer audit callback if auditing is enabled */
728 if ((cp
->mc_flags
& MCF_DEBUG
) && cp
->mc_slab_audit
!= NULL
)
729 (*cp
->mc_slab_audit
)(cp
->mc_private
, list
, FALSE
);
731 MCACHE_LOCK(&ccp
->cc_lock
);
734 * If there is space in the current CPU's filled bucket, put
735 * the object there and return once all objects are freed.
736 * Note the cast to unsigned integer takes care of the case
737 * where the bucket layer is disabled (when cc_objs is -1).
739 if ((unsigned int)ccp
->cc_objs
<
740 (unsigned int)ccp
->cc_bktsize
) {
742 * Reverse the list while we place the object into the
743 * bucket; this effectively causes the most recently
744 * freed object(s) to be reused during allocation.
746 nlist
= list
->obj_next
;
747 list
->obj_next
= (ccp
->cc_objs
== 0) ? NULL
:
748 ccp
->cc_filled
->bkt_obj
[ccp
->cc_objs
- 1];
749 ccp
->cc_filled
->bkt_obj
[ccp
->cc_objs
++] = list
;
752 if ((list
= nlist
) != NULL
)
755 /* We are done; return to caller */
756 MCACHE_UNLOCK(&ccp
->cc_lock
);
758 /* If there is a waiter below, notify it */
759 if (cp
->mc_waiter_cnt
> 0)
760 mcache_notify(cp
, MCN_RETRYALLOC
);
765 * The CPU's filled bucket is full. If the previous filled
766 * bucket was empty, exchange and try again.
768 if (ccp
->cc_pobjs
== 0) {
769 mcache_cpu_refill(ccp
, ccp
->cc_pfilled
, ccp
->cc_pobjs
);
774 * If the bucket layer is disabled, free to slab. This can
775 * happen either because MCF_NOCPUCACHE is set, or because
776 * the bucket layer is currently being resized.
778 if (ccp
->cc_bktsize
== 0)
782 * Both of the CPU's buckets are full; try to get an empty
783 * bucket from the bucket layer. Upon success, empty this
784 * CPU and place any full bucket into the full list.
786 bkt
= mcache_bkt_alloc(cp
, &cp
->mc_empty
, &btp
);
788 if (ccp
->cc_pfilled
!= NULL
)
789 mcache_bkt_free(cp
, &cp
->mc_full
,
791 mcache_cpu_refill(ccp
, bkt
, 0);
796 * We need an empty bucket to put our freed objects into
797 * but couldn't get an empty bucket from the bucket layer;
798 * attempt to allocate one. We do not want to block for
799 * allocation here, and if the bucket allocation fails
800 * we will simply fall through to the slab layer.
802 MCACHE_UNLOCK(&ccp
->cc_lock
);
803 bkt
= mcache_alloc(btp
->bt_cache
, MCR_NOSLEEP
);
804 MCACHE_LOCK(&ccp
->cc_lock
);
808 * We have an empty bucket, but since we drop the
809 * CPU lock above, the cache's bucket size may have
810 * changed. If so, free the bucket and try again.
812 if (ccp
->cc_bktsize
!= btp
->bt_bktsize
) {
813 MCACHE_UNLOCK(&ccp
->cc_lock
);
814 mcache_free(btp
->bt_cache
, bkt
);
815 MCACHE_LOCK(&ccp
->cc_lock
);
820 * We have an empty bucket of the right size;
821 * add it to the bucket layer and try again.
823 mcache_bkt_free(cp
, &cp
->mc_empty
, bkt
);
828 * The bucket layer has no empty buckets; free the
829 * object(s) directly to the slab layer.
833 MCACHE_UNLOCK(&ccp
->cc_lock
);
835 /* If there is a waiter below, notify it */
836 if (cp
->mc_waiter_cnt
> 0)
837 mcache_notify(cp
, MCN_RETRYALLOC
);
839 /* Advise the slab layer to purge the object(s) */
840 (*cp
->mc_slab_free
)(cp
->mc_private
, list
,
841 (cp
->mc_flags
& MCF_DEBUG
) || cp
->mc_purge_cnt
);
845 * Cache destruction routine.
847 __private_extern__
void
848 mcache_destroy(mcache_t
*cp
)
853 LIST_REMOVE(cp
, mc_list
);
854 MCACHE_LIST_UNLOCK();
856 mcache_bkt_purge(cp
);
859 * This cache is dead; there should be no further transaction.
860 * If it's still invoked, make sure that it induces a fault.
862 cp
->mc_slab_alloc
= NULL
;
863 cp
->mc_slab_free
= NULL
;
864 cp
->mc_slab_audit
= NULL
;
866 lck_attr_free(cp
->mc_bkt_lock_attr
);
867 lck_grp_free(cp
->mc_bkt_lock_grp
);
868 lck_grp_attr_free(cp
->mc_bkt_lock_grp_attr
);
870 lck_attr_free(cp
->mc_cpu_lock_attr
);
871 lck_grp_free(cp
->mc_cpu_lock_grp
);
872 lck_grp_attr_free(cp
->mc_cpu_lock_grp_attr
);
874 lck_attr_free(cp
->mc_sync_lock_attr
);
875 lck_grp_free(cp
->mc_sync_lock_grp
);
876 lck_grp_attr_free(cp
->mc_sync_lock_grp_attr
);
879 * TODO: We need to destroy the zone here, but cannot do it
880 * because there is no such way to achieve that. Until then
881 * the memory allocated for the zone structure is leaked.
882 * Once it is achievable, uncomment these lines:
884 * if (cp->mc_slab_zone != NULL) {
885 * zdestroy(cp->mc_slab_zone);
886 * cp->mc_slab_zone = NULL;
890 /* Get the original address since we're about to free it */
891 pbuf
= (void **)((intptr_t)cp
- sizeof (void *));
893 zfree(mcache_zone
, *pbuf
);
897 * Internal slab allocator used as a backend for simple caches. The current
898 * implementation uses the zone allocator for simplicity reasons.
901 mcache_slab_alloc(void *arg
, mcache_obj_t
***plist
, unsigned int num
,
906 unsigned int need
= num
;
907 size_t rsize
= P2ROUNDUP(cp
->mc_bufsize
, sizeof (u_int64_t
));
908 u_int32_t flags
= cp
->mc_flags
;
909 void *buf
, *base
, **pbuf
;
910 mcache_obj_t
**list
= *plist
;
915 buf
= zalloc(cp
->mc_slab_zone
);
919 /* Get the aligned base address for this object */
920 base
= (void *)P2ROUNDUP((intptr_t)buf
+ sizeof (u_int64_t
),
924 * Wind back a pointer size from the aligned base and
925 * save the original address so we can free it later.
927 pbuf
= (void **)((intptr_t)base
- sizeof (void *));
930 VERIFY (((intptr_t)base
+ cp
->mc_bufsize
) <=
931 ((intptr_t)buf
+ cp
->mc_chunksize
));
934 * If auditing is enabled, patternize the contents of
935 * the buffer starting from the 64-bit aligned base to
936 * the end of the buffer; the length is rounded up to
937 * the nearest 64-bit multiply; this is because we use
938 * 64-bit memory access to set/check the pattern.
940 if (flags
& MCF_DEBUG
) {
941 VERIFY(((intptr_t)base
+ rsize
) <=
942 ((intptr_t)buf
+ cp
->mc_chunksize
));
943 mcache_set_pattern(MCACHE_FREE_PATTERN
, base
, rsize
);
946 VERIFY(IS_P2ALIGNED(base
, cp
->mc_align
));
947 *list
= (mcache_obj_t
*)base
;
949 (*list
)->obj_next
= NULL
;
950 list
= *plist
= &(*list
)->obj_next
;
952 /* If we got them all, return to mcache */
961 * Internal slab deallocator used as a backend for simple caches.
964 mcache_slab_free(void *arg
, mcache_obj_t
*list
, __unused boolean_t purged
)
968 size_t rsize
= P2ROUNDUP(cp
->mc_bufsize
, sizeof (u_int64_t
));
969 u_int32_t flags
= cp
->mc_flags
;
974 nlist
= list
->obj_next
;
975 list
->obj_next
= NULL
;
978 VERIFY(IS_P2ALIGNED(base
, cp
->mc_align
));
980 /* Get the original address since we're about to free it */
981 pbuf
= (void **)((intptr_t)base
- sizeof (void *));
983 VERIFY(((intptr_t)base
+ cp
->mc_bufsize
) <=
984 ((intptr_t)*pbuf
+ cp
->mc_chunksize
));
986 if (flags
& MCF_DEBUG
) {
987 VERIFY(((intptr_t)base
+ rsize
) <=
988 ((intptr_t)*pbuf
+ cp
->mc_chunksize
));
989 mcache_audit_free_verify(NULL
, base
, 0, rsize
);
992 /* Free it to zone */
993 zfree(cp
->mc_slab_zone
, *pbuf
);
995 /* No more objects to free; return to mcache */
996 if ((list
= nlist
) == NULL
)
1002 * Internal slab auditor for simple caches.
1005 mcache_slab_audit(void *arg
, mcache_obj_t
*list
, boolean_t alloc
)
1008 size_t rsize
= P2ROUNDUP(cp
->mc_bufsize
, sizeof (u_int64_t
));
1011 while (list
!= NULL
) {
1012 mcache_obj_t
*next
= list
->obj_next
;
1015 VERIFY(IS_P2ALIGNED(base
, cp
->mc_align
));
1017 /* Get the original address */
1018 pbuf
= (void **)((intptr_t)base
- sizeof (void *));
1020 VERIFY(((intptr_t)base
+ rsize
) <=
1021 ((intptr_t)*pbuf
+ cp
->mc_chunksize
));
1024 mcache_set_pattern(MCACHE_FREE_PATTERN
, base
, rsize
);
1026 mcache_audit_free_verify_set(NULL
, base
, 0, rsize
);
1028 list
= list
->obj_next
= next
;
1033 * Refill the CPU's filled bucket with bkt and save the previous one.
1036 mcache_cpu_refill(mcache_cpu_t
*ccp
, mcache_bkt_t
*bkt
, int objs
)
1038 ASSERT((ccp
->cc_filled
== NULL
&& ccp
->cc_objs
== -1) ||
1039 (ccp
->cc_filled
&& ccp
->cc_objs
+ objs
== ccp
->cc_bktsize
));
1040 ASSERT(ccp
->cc_bktsize
> 0);
1042 ccp
->cc_pfilled
= ccp
->cc_filled
;
1043 ccp
->cc_pobjs
= ccp
->cc_objs
;
1044 ccp
->cc_filled
= bkt
;
1045 ccp
->cc_objs
= objs
;
1049 * Allocate a bucket from the bucket layer.
1051 static mcache_bkt_t
*
1052 mcache_bkt_alloc(mcache_t
*cp
, mcache_bktlist_t
*blp
, mcache_bkttype_t
**btp
)
1056 if (!MCACHE_LOCK_TRY(&cp
->mc_bkt_lock
)) {
1058 * The bucket layer lock is held by another CPU; increase
1059 * the contention count so that we can later resize the
1060 * bucket size accordingly.
1062 MCACHE_LOCK(&cp
->mc_bkt_lock
);
1063 cp
->mc_bkt_contention
++;
1066 if ((bkt
= blp
->bl_list
) != NULL
) {
1067 blp
->bl_list
= bkt
->bkt_next
;
1068 if (--blp
->bl_total
< blp
->bl_min
)
1069 blp
->bl_min
= blp
->bl_total
;
1074 *btp
= cp
->cache_bkttype
;
1076 MCACHE_UNLOCK(&cp
->mc_bkt_lock
);
1082 * Free a bucket to the bucket layer.
1085 mcache_bkt_free(mcache_t
*cp
, mcache_bktlist_t
*blp
, mcache_bkt_t
*bkt
)
1087 MCACHE_LOCK(&cp
->mc_bkt_lock
);
1089 bkt
->bkt_next
= blp
->bl_list
;
1093 MCACHE_UNLOCK(&cp
->mc_bkt_lock
);
1097 * Enable the bucket layer of a cache.
1100 mcache_cache_bkt_enable(mcache_t
*cp
)
1105 if (cp
->mc_flags
& MCF_NOCPUCACHE
)
1108 for (cpu
= 0; cpu
< ncpu
; cpu
++) {
1109 ccp
= &cp
->mc_cpu
[cpu
];
1110 MCACHE_LOCK(&ccp
->cc_lock
);
1111 ccp
->cc_bktsize
= cp
->cache_bkttype
->bt_bktsize
;
1112 MCACHE_UNLOCK(&ccp
->cc_lock
);
1117 * Purge all buckets from a cache and disable its bucket layer.
1120 mcache_bkt_purge(mcache_t
*cp
)
1123 mcache_bkt_t
*bp
, *pbp
;
1124 mcache_bkttype_t
*btp
;
1125 int cpu
, objs
, pobjs
;
1127 for (cpu
= 0; cpu
< ncpu
; cpu
++) {
1128 ccp
= &cp
->mc_cpu
[cpu
];
1130 MCACHE_LOCK(&ccp
->cc_lock
);
1132 btp
= cp
->cache_bkttype
;
1133 bp
= ccp
->cc_filled
;
1134 pbp
= ccp
->cc_pfilled
;
1135 objs
= ccp
->cc_objs
;
1136 pobjs
= ccp
->cc_pobjs
;
1137 ccp
->cc_filled
= NULL
;
1138 ccp
->cc_pfilled
= NULL
;
1141 ccp
->cc_bktsize
= 0;
1143 MCACHE_UNLOCK(&ccp
->cc_lock
);
1146 mcache_bkt_destroy(cp
, btp
, bp
, objs
);
1148 mcache_bkt_destroy(cp
, btp
, pbp
, pobjs
);
1151 mcache_bkt_ws_zero(cp
);
1152 mcache_bkt_ws_reap(cp
);
1156 * Free one or more objects in the bucket to the slab layer,
1157 * and also free the bucket itself.
1160 mcache_bkt_destroy(mcache_t
*cp
, mcache_bkttype_t
*btp
, mcache_bkt_t
*bkt
,
1164 mcache_obj_t
*top
= bkt
->bkt_obj
[nobjs
- 1];
1166 if (cp
->mc_flags
& MCF_DEBUG
) {
1167 mcache_obj_t
*o
= top
;
1171 * Verify that the chain of objects in the bucket is
1172 * valid. Any mismatch here means a mistake when the
1173 * object(s) were freed to the CPU layer, so we panic.
1180 panic("mcache_bkt_destroy: %s cp %p corrupted "
1181 "list in bkt %p (nobjs %d actual %d)\n",
1182 cp
->mc_name
, (void *)cp
, (void *)bkt
,
1187 /* Advise the slab layer to purge the object(s) */
1188 (*cp
->mc_slab_free
)(cp
->mc_private
, top
,
1189 (cp
->mc_flags
& MCF_DEBUG
) || cp
->mc_purge_cnt
);
1191 mcache_free(btp
->bt_cache
, bkt
);
1195 * Update the bucket layer working set statistics.
1198 mcache_bkt_ws_update(mcache_t
*cp
)
1200 MCACHE_LOCK(&cp
->mc_bkt_lock
);
1202 cp
->mc_full
.bl_reaplimit
= cp
->mc_full
.bl_min
;
1203 cp
->mc_full
.bl_min
= cp
->mc_full
.bl_total
;
1204 cp
->mc_empty
.bl_reaplimit
= cp
->mc_empty
.bl_min
;
1205 cp
->mc_empty
.bl_min
= cp
->mc_empty
.bl_total
;
1207 MCACHE_UNLOCK(&cp
->mc_bkt_lock
);
1211 * Mark everything as eligible for reaping (working set is zero).
1214 mcache_bkt_ws_zero(mcache_t
*cp
)
1216 MCACHE_LOCK(&cp
->mc_bkt_lock
);
1218 cp
->mc_full
.bl_reaplimit
= cp
->mc_full
.bl_total
;
1219 cp
->mc_full
.bl_min
= cp
->mc_full
.bl_total
;
1220 cp
->mc_empty
.bl_reaplimit
= cp
->mc_empty
.bl_total
;
1221 cp
->mc_empty
.bl_min
= cp
->mc_empty
.bl_total
;
1223 MCACHE_UNLOCK(&cp
->mc_bkt_lock
);
1227 * Reap all buckets that are beyond the working set.
1230 mcache_bkt_ws_reap(mcache_t
*cp
)
1234 mcache_bkttype_t
*btp
;
1236 reap
= MIN(cp
->mc_full
.bl_reaplimit
, cp
->mc_full
.bl_min
);
1238 (bkt
= mcache_bkt_alloc(cp
, &cp
->mc_full
, &btp
)) != NULL
)
1239 mcache_bkt_destroy(cp
, btp
, bkt
, btp
->bt_bktsize
);
1241 reap
= MIN(cp
->mc_empty
.bl_reaplimit
, cp
->mc_empty
.bl_min
);
1243 (bkt
= mcache_bkt_alloc(cp
, &cp
->mc_empty
, &btp
)) != NULL
)
1244 mcache_bkt_destroy(cp
, btp
, bkt
, 0);
1248 mcache_reap_timeout(thread_call_param_t dummy __unused
,
1249 thread_call_param_t arg
)
1251 volatile UInt32
*flag
= arg
;
1253 ASSERT(flag
== &mcache_reaping
);
1259 mcache_reap_done(void *flag
)
1261 uint64_t deadline
, leeway
;
1263 clock_interval_to_deadline(mcache_reap_interval
, NSEC_PER_SEC
,
1265 clock_interval_to_absolutetime_interval(mcache_reap_interval_leeway
,
1266 NSEC_PER_SEC
, &leeway
);
1267 thread_call_enter_delayed_with_leeway(mcache_reap_tcall
, flag
,
1268 deadline
, leeway
, THREAD_CALL_DELAY_LEEWAY
);
1272 mcache_reap_start(void *arg
)
1276 ASSERT(flag
== &mcache_reaping
);
1278 mcache_applyall(mcache_cache_reap
);
1279 mcache_dispatch(mcache_reap_done
, flag
);
1282 __private_extern__
void
1285 UInt32
*flag
= &mcache_reaping
;
1287 if (mcache_llock_owner
== current_thread() ||
1288 !OSCompareAndSwap(0, 1, flag
))
1291 mcache_dispatch(mcache_reap_start
, flag
);
1294 __private_extern__
void
1295 mcache_reap_now(mcache_t
*cp
, boolean_t purge
)
1298 mcache_bkt_purge(cp
);
1299 mcache_cache_bkt_enable(cp
);
1301 mcache_bkt_ws_zero(cp
);
1302 mcache_bkt_ws_reap(cp
);
1307 mcache_cache_reap(mcache_t
*cp
)
1309 mcache_bkt_ws_reap(cp
);
1313 * Performs period maintenance on a cache.
1316 mcache_cache_update(mcache_t
*cp
)
1318 int need_bkt_resize
= 0;
1319 int need_bkt_reenable
= 0;
1321 lck_mtx_assert(mcache_llock
, LCK_MTX_ASSERT_OWNED
);
1323 mcache_bkt_ws_update(cp
);
1326 * Cache resize and post-purge reenable are mutually exclusive.
1327 * If the cache was previously purged, there is no point of
1328 * increasing the bucket size as there was an indication of
1329 * memory pressure on the system.
1331 lck_mtx_lock_spin(&cp
->mc_sync_lock
);
1332 if (!(cp
->mc_flags
& MCF_NOCPUCACHE
) && cp
->mc_enable_cnt
)
1333 need_bkt_reenable
= 1;
1334 lck_mtx_unlock(&cp
->mc_sync_lock
);
1336 MCACHE_LOCK(&cp
->mc_bkt_lock
);
1338 * If the contention count is greater than the threshold, and if
1339 * we are not already at the maximum bucket size, increase it.
1340 * Otherwise, if this cache was previously purged by the user
1341 * then we simply reenable it.
1343 if ((unsigned int)cp
->mc_chunksize
< cp
->cache_bkttype
->bt_maxbuf
&&
1344 (int)(cp
->mc_bkt_contention
- cp
->mc_bkt_contention_prev
) >
1345 mcache_bkt_contention
&& !need_bkt_reenable
)
1346 need_bkt_resize
= 1;
1348 cp
->mc_bkt_contention_prev
= cp
->mc_bkt_contention
;
1349 MCACHE_UNLOCK(&cp
->mc_bkt_lock
);
1351 if (need_bkt_resize
)
1352 mcache_dispatch(mcache_cache_bkt_resize
, cp
);
1353 else if (need_bkt_reenable
)
1354 mcache_dispatch(mcache_cache_enable
, cp
);
1358 * Recompute a cache's bucket size. This is an expensive operation
1359 * and should not be done frequently; larger buckets provide for a
1360 * higher transfer rate with the bucket while smaller buckets reduce
1361 * the memory consumption.
1364 mcache_cache_bkt_resize(void *arg
)
1367 mcache_bkttype_t
*btp
= cp
->cache_bkttype
;
1369 if ((unsigned int)cp
->mc_chunksize
< btp
->bt_maxbuf
) {
1370 mcache_bkt_purge(cp
);
1373 * Upgrade to the next bucket type with larger bucket size;
1374 * temporarily set the previous contention snapshot to a
1375 * negative number to prevent unnecessary resize request.
1377 MCACHE_LOCK(&cp
->mc_bkt_lock
);
1378 cp
->cache_bkttype
= ++btp
;
1379 cp
->mc_bkt_contention_prev
= cp
->mc_bkt_contention
+ INT_MAX
;
1380 MCACHE_UNLOCK(&cp
->mc_bkt_lock
);
1382 mcache_cache_enable(cp
);
1387 * Reenable a previously disabled cache due to purge.
1390 mcache_cache_enable(void *arg
)
1394 lck_mtx_lock_spin(&cp
->mc_sync_lock
);
1395 cp
->mc_purge_cnt
= 0;
1396 cp
->mc_enable_cnt
= 0;
1397 lck_mtx_unlock(&cp
->mc_sync_lock
);
1399 mcache_cache_bkt_enable(cp
);
1403 mcache_update_timeout(__unused
void *arg
)
1405 uint64_t deadline
, leeway
;
1407 clock_interval_to_deadline(mcache_reap_interval
, NSEC_PER_SEC
,
1409 clock_interval_to_absolutetime_interval(mcache_reap_interval_leeway
,
1410 NSEC_PER_SEC
, &leeway
);
1411 thread_call_enter_delayed_with_leeway(mcache_update_tcall
, NULL
,
1412 deadline
, leeway
, THREAD_CALL_DELAY_LEEWAY
);
1416 mcache_update(thread_call_param_t arg __unused
,
1417 thread_call_param_t dummy __unused
)
1419 mcache_applyall(mcache_cache_update
);
1420 mcache_update_timeout(NULL
);
1424 mcache_applyall(void (*func
)(mcache_t
*))
1429 LIST_FOREACH(cp
, &mcache_head
, mc_list
) {
1432 MCACHE_LIST_UNLOCK();
1436 mcache_dispatch(void (*func
)(void *), void *arg
)
1438 ASSERT(func
!= NULL
);
1439 timeout(func
, arg
, hz
/1000);
1442 __private_extern__
void
1443 mcache_buffer_log(mcache_audit_t
*mca
, void *addr
, mcache_t
*cp
,
1444 struct timeval
*base_ts
)
1446 struct timeval now
, base
= { 0, 0 };
1447 void *stack
[MCACHE_STACK_DEPTH
+ 1];
1448 struct mca_trn
*transaction
;
1450 transaction
= &mca
->mca_trns
[mca
->mca_next_trn
];
1452 mca
->mca_addr
= addr
;
1453 mca
->mca_cache
= cp
;
1455 transaction
->mca_thread
= current_thread();
1457 bzero(stack
, sizeof (stack
));
1458 transaction
->mca_depth
= OSBacktrace(stack
, MCACHE_STACK_DEPTH
+ 1) - 1;
1459 bcopy(&stack
[1], transaction
->mca_stack
,
1460 sizeof (transaction
->mca_stack
));
1463 if (base_ts
!= NULL
)
1465 /* tstamp is in ms relative to base_ts */
1466 transaction
->mca_tstamp
= ((now
.tv_usec
- base
.tv_usec
) / 1000);
1467 if ((now
.tv_sec
- base
.tv_sec
) > 0)
1468 transaction
->mca_tstamp
+= ((now
.tv_sec
- base
.tv_sec
) * 1000);
1471 (mca
->mca_next_trn
+ 1) % mca_trn_max
;
1474 __private_extern__
void
1475 mcache_set_pattern(u_int64_t pattern
, void *buf_arg
, size_t size
)
1477 u_int64_t
*buf_end
= (u_int64_t
*)((void *)((char *)buf_arg
+ size
));
1478 u_int64_t
*buf
= (u_int64_t
*)buf_arg
;
1480 VERIFY(IS_P2ALIGNED(buf_arg
, sizeof (u_int64_t
)));
1481 VERIFY(IS_P2ALIGNED(size
, sizeof (u_int64_t
)));
1483 while (buf
< buf_end
)
1487 __private_extern__
void *
1488 mcache_verify_pattern(u_int64_t pattern
, void *buf_arg
, size_t size
)
1490 u_int64_t
*buf_end
= (u_int64_t
*)((void *)((char *)buf_arg
+ size
));
1493 VERIFY(IS_P2ALIGNED(buf_arg
, sizeof (u_int64_t
)));
1494 VERIFY(IS_P2ALIGNED(size
, sizeof (u_int64_t
)));
1496 for (buf
= buf_arg
; buf
< buf_end
; buf
++) {
1497 if (*buf
!= pattern
)
1503 __private_extern__
void *
1504 mcache_verify_set_pattern(u_int64_t old
, u_int64_t
new, void *buf_arg
,
1507 u_int64_t
*buf_end
= (u_int64_t
*)((void *)((char *)buf_arg
+ size
));
1510 VERIFY(IS_P2ALIGNED(buf_arg
, sizeof (u_int64_t
)));
1511 VERIFY(IS_P2ALIGNED(size
, sizeof (u_int64_t
)));
1513 for (buf
= buf_arg
; buf
< buf_end
; buf
++) {
1515 mcache_set_pattern(old
, buf_arg
,
1516 (uintptr_t)buf
- (uintptr_t)buf_arg
);
1524 __private_extern__
void
1525 mcache_audit_free_verify(mcache_audit_t
*mca
, void *base
, size_t offset
,
1532 addr
= (void *)((uintptr_t)base
+ offset
);
1533 next
= ((mcache_obj_t
*)addr
)->obj_next
;
1535 /* For the "obj_next" pointer in the buffer */
1536 oaddr64
= (u_int64_t
*)P2ROUNDDOWN(addr
, sizeof (u_int64_t
));
1537 *oaddr64
= MCACHE_FREE_PATTERN
;
1539 if ((oaddr64
= mcache_verify_pattern(MCACHE_FREE_PATTERN
,
1540 (caddr_t
)base
, size
)) != NULL
) {
1541 mcache_audit_panic(mca
, addr
, (caddr_t
)oaddr64
- (caddr_t
)base
,
1542 (int64_t)MCACHE_FREE_PATTERN
, (int64_t)*oaddr64
);
1545 ((mcache_obj_t
*)addr
)->obj_next
= next
;
1548 __private_extern__
void
1549 mcache_audit_free_verify_set(mcache_audit_t
*mca
, void *base
, size_t offset
,
1556 addr
= (void *)((uintptr_t)base
+ offset
);
1557 next
= ((mcache_obj_t
*)addr
)->obj_next
;
1559 /* For the "obj_next" pointer in the buffer */
1560 oaddr64
= (u_int64_t
*)P2ROUNDDOWN(addr
, sizeof (u_int64_t
));
1561 *oaddr64
= MCACHE_FREE_PATTERN
;
1563 if ((oaddr64
= mcache_verify_set_pattern(MCACHE_FREE_PATTERN
,
1564 MCACHE_UNINITIALIZED_PATTERN
, (caddr_t
)base
, size
)) != NULL
) {
1565 mcache_audit_panic(mca
, addr
, (caddr_t
)oaddr64
- (caddr_t
)base
,
1566 (int64_t)MCACHE_FREE_PATTERN
, (int64_t)*oaddr64
);
1569 ((mcache_obj_t
*)addr
)->obj_next
= next
;
1574 #define DUMP_TRN_FMT() \
1575 "%s transaction thread %p saved PC stack (%d deep):\n" \
1576 "\t%p, %p, %p, %p, %p, %p, %p, %p\n" \
1577 "\t%p, %p, %p, %p, %p, %p, %p, %p\n"
1579 #define DUMP_TRN_FIELDS(s, x) \
1581 mca->mca_trns[x].mca_thread, mca->mca_trns[x].mca_depth, \
1582 mca->mca_trns[x].mca_stack[0], mca->mca_trns[x].mca_stack[1], \
1583 mca->mca_trns[x].mca_stack[2], mca->mca_trns[x].mca_stack[3], \
1584 mca->mca_trns[x].mca_stack[4], mca->mca_trns[x].mca_stack[5], \
1585 mca->mca_trns[x].mca_stack[6], mca->mca_trns[x].mca_stack[7], \
1586 mca->mca_trns[x].mca_stack[8], mca->mca_trns[x].mca_stack[9], \
1587 mca->mca_trns[x].mca_stack[10], mca->mca_trns[x].mca_stack[11], \
1588 mca->mca_trns[x].mca_stack[12], mca->mca_trns[x].mca_stack[13], \
1589 mca->mca_trns[x].mca_stack[14], mca->mca_trns[x].mca_stack[15]
1591 #define MCA_TRN_LAST ((mca->mca_next_trn + mca_trn_max) % mca_trn_max)
1592 #define MCA_TRN_PREV ((mca->mca_next_trn + mca_trn_max - 1) % mca_trn_max)
1594 __private_extern__
char *
1595 mcache_dump_mca(mcache_audit_t
*mca
)
1597 if (mca_dump_buf
== NULL
)
1600 snprintf(mca_dump_buf
, DUMP_MCA_BUF_SIZE
,
1601 "mca %p: addr %p, cache %p (%s) nxttrn %d\n"
1605 mca
, mca
->mca_addr
, mca
->mca_cache
,
1606 mca
->mca_cache
? mca
->mca_cache
->mc_name
: "?",
1609 DUMP_TRN_FIELDS("last", MCA_TRN_LAST
),
1610 DUMP_TRN_FIELDS("previous", MCA_TRN_PREV
));
1612 return (mca_dump_buf
);
1615 __private_extern__
void
1616 mcache_audit_panic(mcache_audit_t
*mca
, void *addr
, size_t offset
,
1617 int64_t expected
, int64_t got
)
1620 panic("mcache_audit: buffer %p modified after free at "
1621 "offset 0x%lx (0x%llx instead of 0x%llx)\n", addr
,
1622 offset
, got
, expected
);
1626 panic("mcache_audit: buffer %p modified after free at offset 0x%lx "
1627 "(0x%llx instead of 0x%llx)\n%s\n",
1628 addr
, offset
, got
, expected
, mcache_dump_mca(mca
));
1632 __private_extern__
int
1633 assfail(const char *a
, const char *f
, int l
)
1635 panic("assertion failed: %s, file: %s, line: %d", a
, f
, l
);