2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
8 * This file is part of the SPL, Solaris Porting Layer.
10 * The SPL is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
15 * The SPL is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * You should have received a copy of the GNU General Public License along
21 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
24 #define SPL_KMEM_CACHE_IMPLEMENTING
27 #include <sys/kmem_cache.h>
28 #include <sys/taskq.h>
29 #include <sys/timer.h>
32 #include <sys/string.h>
33 #include <linux/slab.h>
34 #include <linux/swap.h>
35 #include <linux/prefetch.h>
38 * Linux 3.16 replaced smp_mb__{before,after}_{atomic,clear}_{dec,inc,bit}()
39 * with smp_mb__{before,after}_atomic() because they were redundant. This is
40 * only used inside our SLAB allocator, so we implement an internal wrapper
41 * here to give us smp_mb__{before,after}_atomic() on older kernels.
43 #ifndef smp_mb__before_atomic
44 #define smp_mb__before_atomic(x) smp_mb__before_clear_bit(x)
47 #ifndef smp_mb__after_atomic
48 #define smp_mb__after_atomic(x) smp_mb__after_clear_bit(x)
53 * Cache magazines are an optimization designed to minimize the cost of
54 * allocating memory. They do this by keeping a per-cpu cache of recently
55 * freed objects, which can then be reallocated without taking a lock. This
56 * can improve performance on highly contended caches. However, because
57 * objects in magazines will prevent otherwise empty slabs from being
58 * immediately released this may not be ideal for low memory machines.
60 * For this reason spl_kmem_cache_magazine_size can be used to set a maximum
61 * magazine size. When this value is set to 0 the magazine size will be
62 * automatically determined based on the object size. Otherwise magazines
63 * will be limited to 2-256 objects per magazine (i.e per cpu). Magazines
64 * may never be entirely disabled in this implementation.
66 static unsigned int spl_kmem_cache_magazine_size
= 0;
67 module_param(spl_kmem_cache_magazine_size
, uint
, 0444);
68 MODULE_PARM_DESC(spl_kmem_cache_magazine_size
,
69 "Default magazine size (2-256), set automatically (0)");
71 static unsigned int spl_kmem_cache_obj_per_slab
= SPL_KMEM_CACHE_OBJ_PER_SLAB
;
72 module_param(spl_kmem_cache_obj_per_slab
, uint
, 0644);
73 MODULE_PARM_DESC(spl_kmem_cache_obj_per_slab
, "Number of objects per slab");
75 static unsigned int spl_kmem_cache_max_size
= SPL_KMEM_CACHE_MAX_SIZE
;
76 module_param(spl_kmem_cache_max_size
, uint
, 0644);
77 MODULE_PARM_DESC(spl_kmem_cache_max_size
, "Maximum size of slab in MB");
80 * For small objects the Linux slab allocator should be used to make the most
81 * efficient use of the memory. However, large objects are not supported by
82 * the Linux slab and therefore the SPL implementation is preferred. A cutoff
83 * of 16K was determined to be optimal for architectures using 4K pages and
84 * to also work well on architecutres using larger 64K page sizes.
86 static unsigned int spl_kmem_cache_slab_limit
=
87 SPL_MAX_KMEM_ORDER_NR_PAGES
* PAGE_SIZE
;
88 module_param(spl_kmem_cache_slab_limit
, uint
, 0644);
89 MODULE_PARM_DESC(spl_kmem_cache_slab_limit
,
90 "Objects less than N bytes use the Linux slab");
93 * The number of threads available to allocate new slabs for caches. This
94 * should not need to be tuned but it is available for performance analysis.
96 static unsigned int spl_kmem_cache_kmem_threads
= 4;
97 module_param(spl_kmem_cache_kmem_threads
, uint
, 0444);
98 MODULE_PARM_DESC(spl_kmem_cache_kmem_threads
,
99 "Number of spl_kmem_cache threads");
103 * Slab allocation interfaces
105 * While the Linux slab implementation was inspired by the Solaris
106 * implementation I cannot use it to emulate the Solaris APIs. I
107 * require two features which are not provided by the Linux slab.
109 * 1) Constructors AND destructors. Recent versions of the Linux
110 * kernel have removed support for destructors. This is a deal
111 * breaker for the SPL which contains particularly expensive
112 * initializers for mutex's, condition variables, etc. We also
113 * require a minimal level of cleanup for these data types unlike
114 * many Linux data types which do need to be explicitly destroyed.
116 * 2) Virtual address space backed slab. Callers of the Solaris slab
117 * expect it to work well for both small are very large allocations.
118 * Because of memory fragmentation the Linux slab which is backed
119 * by kmalloc'ed memory performs very badly when confronted with
120 * large numbers of large allocations. Basing the slab on the
121 * virtual address space removes the need for contiguous pages
122 * and greatly improve performance for large allocations.
124 * For these reasons, the SPL has its own slab implementation with
125 * the needed features. It is not as highly optimized as either the
126 * Solaris or Linux slabs, but it should get me most of what is
127 * needed until it can be optimized or obsoleted by another approach.
129 * One serious concern I do have about this method is the relatively
130 * small virtual address space on 32bit arches. This will seriously
131 * constrain the size of the slab caches and their performance.
134 struct list_head spl_kmem_cache_list
; /* List of caches */
135 struct rw_semaphore spl_kmem_cache_sem
; /* Cache list lock */
136 static taskq_t
*spl_kmem_cache_taskq
; /* Task queue for aging / reclaim */
138 static void spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
);
141 kv_alloc(spl_kmem_cache_t
*skc
, int size
, int flags
)
143 gfp_t lflags
= kmem_flags_convert(flags
);
146 if (skc
->skc_flags
& KMC_RECLAIMABLE
)
147 lflags
|= __GFP_RECLAIMABLE
;
148 ptr
= spl_vmalloc(size
, lflags
| __GFP_HIGHMEM
);
150 /* Resulting allocated memory will be page aligned */
151 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
157 kv_free(spl_kmem_cache_t
*skc
, void *ptr
, int size
)
159 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
162 * The Linux direct reclaim path uses this out of band value to
163 * determine if forward progress is being made. Normally this is
164 * incremented by kmem_freepages() which is part of the various
165 * Linux slab implementations. However, since we are using none
166 * of that infrastructure we are responsible for incrementing it.
168 if (current
->reclaim_state
)
169 #ifdef HAVE_RECLAIM_STATE_RECLAIMED
170 current
->reclaim_state
->reclaimed
+= size
>> PAGE_SHIFT
;
172 current
->reclaim_state
->reclaimed_slab
+= size
>> PAGE_SHIFT
;
178 * Required space for each aligned sks.
180 static inline uint32_t
181 spl_sks_size(spl_kmem_cache_t
*skc
)
183 return (P2ROUNDUP_TYPED(sizeof (spl_kmem_slab_t
),
184 skc
->skc_obj_align
, uint32_t));
188 * Required space for each aligned object.
190 static inline uint32_t
191 spl_obj_size(spl_kmem_cache_t
*skc
)
193 uint32_t align
= skc
->skc_obj_align
;
195 return (P2ROUNDUP_TYPED(skc
->skc_obj_size
, align
, uint32_t) +
196 P2ROUNDUP_TYPED(sizeof (spl_kmem_obj_t
), align
, uint32_t));
200 spl_kmem_cache_inuse(kmem_cache_t
*cache
)
202 return (cache
->skc_obj_total
);
204 EXPORT_SYMBOL(spl_kmem_cache_inuse
);
207 spl_kmem_cache_entry_size(kmem_cache_t
*cache
)
209 return (cache
->skc_obj_size
);
211 EXPORT_SYMBOL(spl_kmem_cache_entry_size
);
214 * Lookup the spl_kmem_object_t for an object given that object.
216 static inline spl_kmem_obj_t
*
217 spl_sko_from_obj(spl_kmem_cache_t
*skc
, void *obj
)
219 return (obj
+ P2ROUNDUP_TYPED(skc
->skc_obj_size
,
220 skc
->skc_obj_align
, uint32_t));
224 * It's important that we pack the spl_kmem_obj_t structure and the
225 * actual objects in to one large address space to minimize the number
226 * of calls to the allocator. It is far better to do a few large
227 * allocations and then subdivide it ourselves. Now which allocator
228 * we use requires balancing a few trade offs.
230 * For small objects we use kmem_alloc() because as long as you are
231 * only requesting a small number of pages (ideally just one) its cheap.
232 * However, when you start requesting multiple pages with kmem_alloc()
233 * it gets increasingly expensive since it requires contiguous pages.
234 * For this reason we shift to vmem_alloc() for slabs of large objects
235 * which removes the need for contiguous pages. We do not use
236 * vmem_alloc() in all cases because there is significant locking
237 * overhead in __get_vm_area_node(). This function takes a single
238 * global lock when acquiring an available virtual address range which
239 * serializes all vmem_alloc()'s for all slab caches. Using slightly
240 * different allocation functions for small and large objects should
241 * give us the best of both worlds.
243 * +------------------------+
244 * | spl_kmem_slab_t --+-+ |
245 * | skc_obj_size <-+ | |
246 * | spl_kmem_obj_t | |
247 * | skc_obj_size <---+ |
248 * | spl_kmem_obj_t | |
250 * +------------------------+
252 static spl_kmem_slab_t
*
253 spl_slab_alloc(spl_kmem_cache_t
*skc
, int flags
)
255 spl_kmem_slab_t
*sks
;
259 base
= kv_alloc(skc
, skc
->skc_slab_size
, flags
);
263 sks
= (spl_kmem_slab_t
*)base
;
264 sks
->sks_magic
= SKS_MAGIC
;
265 sks
->sks_objs
= skc
->skc_slab_objs
;
266 sks
->sks_age
= jiffies
;
267 sks
->sks_cache
= skc
;
268 INIT_LIST_HEAD(&sks
->sks_list
);
269 INIT_LIST_HEAD(&sks
->sks_free_list
);
271 obj_size
= spl_obj_size(skc
);
273 for (int i
= 0; i
< sks
->sks_objs
; i
++) {
274 void *obj
= base
+ spl_sks_size(skc
) + (i
* obj_size
);
276 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
277 spl_kmem_obj_t
*sko
= spl_sko_from_obj(skc
, obj
);
279 sko
->sko_magic
= SKO_MAGIC
;
281 INIT_LIST_HEAD(&sko
->sko_list
);
282 list_add_tail(&sko
->sko_list
, &sks
->sks_free_list
);
289 * Remove a slab from complete or partial list, it must be called with
290 * the 'skc->skc_lock' held but the actual free must be performed
291 * outside the lock to prevent deadlocking on vmem addresses.
294 spl_slab_free(spl_kmem_slab_t
*sks
,
295 struct list_head
*sks_list
, struct list_head
*sko_list
)
297 spl_kmem_cache_t
*skc
;
299 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
300 ASSERT(sks
->sks_ref
== 0);
302 skc
= sks
->sks_cache
;
303 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
306 * Update slab/objects counters in the cache, then remove the
307 * slab from the skc->skc_partial_list. Finally add the slab
308 * and all its objects in to the private work lists where the
309 * destructors will be called and the memory freed to the system.
311 skc
->skc_obj_total
-= sks
->sks_objs
;
312 skc
->skc_slab_total
--;
313 list_del(&sks
->sks_list
);
314 list_add(&sks
->sks_list
, sks_list
);
315 list_splice_init(&sks
->sks_free_list
, sko_list
);
319 * Reclaim empty slabs at the end of the partial list.
322 spl_slab_reclaim(spl_kmem_cache_t
*skc
)
324 spl_kmem_slab_t
*sks
= NULL
, *m
= NULL
;
325 spl_kmem_obj_t
*sko
= NULL
, *n
= NULL
;
330 * Empty slabs and objects must be moved to a private list so they
331 * can be safely freed outside the spin lock. All empty slabs are
332 * at the end of skc->skc_partial_list, therefore once a non-empty
333 * slab is found we can stop scanning.
335 spin_lock(&skc
->skc_lock
);
336 list_for_each_entry_safe_reverse(sks
, m
,
337 &skc
->skc_partial_list
, sks_list
) {
339 if (sks
->sks_ref
> 0)
342 spl_slab_free(sks
, &sks_list
, &sko_list
);
344 spin_unlock(&skc
->skc_lock
);
347 * The following two loops ensure all the object destructors are run,
348 * and the slabs themselves are freed. This is all done outside the
349 * skc->skc_lock since this allows the destructor to sleep, and
350 * allows us to perform a conditional reschedule when a freeing a
351 * large number of objects and slabs back to the system.
354 list_for_each_entry_safe(sko
, n
, &sko_list
, sko_list
) {
355 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
358 list_for_each_entry_safe(sks
, m
, &sks_list
, sks_list
) {
359 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
360 kv_free(skc
, sks
, skc
->skc_slab_size
);
364 static spl_kmem_emergency_t
*
365 spl_emergency_search(struct rb_root
*root
, void *obj
)
367 struct rb_node
*node
= root
->rb_node
;
368 spl_kmem_emergency_t
*ske
;
369 unsigned long address
= (unsigned long)obj
;
372 ske
= container_of(node
, spl_kmem_emergency_t
, ske_node
);
374 if (address
< ske
->ske_obj
)
375 node
= node
->rb_left
;
376 else if (address
> ske
->ske_obj
)
377 node
= node
->rb_right
;
386 spl_emergency_insert(struct rb_root
*root
, spl_kmem_emergency_t
*ske
)
388 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
389 spl_kmem_emergency_t
*ske_tmp
;
390 unsigned long address
= ske
->ske_obj
;
393 ske_tmp
= container_of(*new, spl_kmem_emergency_t
, ske_node
);
396 if (address
< ske_tmp
->ske_obj
)
397 new = &((*new)->rb_left
);
398 else if (address
> ske_tmp
->ske_obj
)
399 new = &((*new)->rb_right
);
404 rb_link_node(&ske
->ske_node
, parent
, new);
405 rb_insert_color(&ske
->ske_node
, root
);
411 * Allocate a single emergency object and track it in a red black tree.
414 spl_emergency_alloc(spl_kmem_cache_t
*skc
, int flags
, void **obj
)
416 gfp_t lflags
= kmem_flags_convert(flags
);
417 spl_kmem_emergency_t
*ske
;
418 int order
= get_order(skc
->skc_obj_size
);
421 /* Last chance use a partial slab if one now exists */
422 spin_lock(&skc
->skc_lock
);
423 empty
= list_empty(&skc
->skc_partial_list
);
424 spin_unlock(&skc
->skc_lock
);
428 if (skc
->skc_flags
& KMC_RECLAIMABLE
)
429 lflags
|= __GFP_RECLAIMABLE
;
430 ske
= kmalloc(sizeof (*ske
), lflags
);
434 ske
->ske_obj
= __get_free_pages(lflags
, order
);
435 if (ske
->ske_obj
== 0) {
440 spin_lock(&skc
->skc_lock
);
441 empty
= spl_emergency_insert(&skc
->skc_emergency_tree
, ske
);
443 skc
->skc_obj_total
++;
444 skc
->skc_obj_emergency
++;
445 if (skc
->skc_obj_emergency
> skc
->skc_obj_emergency_max
)
446 skc
->skc_obj_emergency_max
= skc
->skc_obj_emergency
;
448 spin_unlock(&skc
->skc_lock
);
450 if (unlikely(!empty
)) {
451 free_pages(ske
->ske_obj
, order
);
456 *obj
= (void *)ske
->ske_obj
;
462 * Locate the passed object in the red black tree and free it.
465 spl_emergency_free(spl_kmem_cache_t
*skc
, void *obj
)
467 spl_kmem_emergency_t
*ske
;
468 int order
= get_order(skc
->skc_obj_size
);
470 spin_lock(&skc
->skc_lock
);
471 ske
= spl_emergency_search(&skc
->skc_emergency_tree
, obj
);
473 rb_erase(&ske
->ske_node
, &skc
->skc_emergency_tree
);
474 skc
->skc_obj_emergency
--;
475 skc
->skc_obj_total
--;
477 spin_unlock(&skc
->skc_lock
);
482 free_pages(ske
->ske_obj
, order
);
489 * Release objects from the per-cpu magazine back to their slab. The flush
490 * argument contains the max number of entries to remove from the magazine.
493 spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
495 spin_lock(&skc
->skc_lock
);
497 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
498 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
500 int count
= MIN(flush
, skm
->skm_avail
);
501 for (int i
= 0; i
< count
; i
++)
502 spl_cache_shrink(skc
, skm
->skm_objs
[i
]);
504 skm
->skm_avail
-= count
;
505 memmove(skm
->skm_objs
, &(skm
->skm_objs
[count
]),
506 sizeof (void *) * skm
->skm_avail
);
508 spin_unlock(&skc
->skc_lock
);
512 * Size a slab based on the size of each aligned object plus spl_kmem_obj_t.
513 * When on-slab we want to target spl_kmem_cache_obj_per_slab. However,
514 * for very small objects we may end up with more than this so as not
515 * to waste space in the minimal allocation of a single page.
518 spl_slab_size(spl_kmem_cache_t
*skc
, uint32_t *objs
, uint32_t *size
)
520 uint32_t sks_size
, obj_size
, max_size
, tgt_size
, tgt_objs
;
522 sks_size
= spl_sks_size(skc
);
523 obj_size
= spl_obj_size(skc
);
524 max_size
= (spl_kmem_cache_max_size
* 1024 * 1024);
525 tgt_size
= (spl_kmem_cache_obj_per_slab
* obj_size
+ sks_size
);
527 if (tgt_size
<= max_size
) {
528 tgt_objs
= (tgt_size
- sks_size
) / obj_size
;
530 tgt_objs
= (max_size
- sks_size
) / obj_size
;
531 tgt_size
= (tgt_objs
* obj_size
) + sks_size
;
544 * Make a guess at reasonable per-cpu magazine size based on the size of
545 * each object and the cost of caching N of them in each magazine. Long
546 * term this should really adapt based on an observed usage heuristic.
549 spl_magazine_size(spl_kmem_cache_t
*skc
)
551 uint32_t obj_size
= spl_obj_size(skc
);
554 if (spl_kmem_cache_magazine_size
> 0)
555 return (MAX(MIN(spl_kmem_cache_magazine_size
, 256), 2));
557 /* Per-magazine sizes below assume a 4Kib page size */
558 if (obj_size
> (PAGE_SIZE
* 256))
559 size
= 4; /* Minimum 4Mib per-magazine */
560 else if (obj_size
> (PAGE_SIZE
* 32))
561 size
= 16; /* Minimum 2Mib per-magazine */
562 else if (obj_size
> (PAGE_SIZE
))
563 size
= 64; /* Minimum 256Kib per-magazine */
564 else if (obj_size
> (PAGE_SIZE
/ 4))
565 size
= 128; /* Minimum 128Kib per-magazine */
573 * Allocate a per-cpu magazine to associate with a specific core.
575 static spl_kmem_magazine_t
*
576 spl_magazine_alloc(spl_kmem_cache_t
*skc
, int cpu
)
578 spl_kmem_magazine_t
*skm
;
579 int size
= sizeof (spl_kmem_magazine_t
) +
580 sizeof (void *) * skc
->skc_mag_size
;
582 skm
= kmalloc_node(size
, GFP_KERNEL
, cpu_to_node(cpu
));
584 skm
->skm_magic
= SKM_MAGIC
;
586 skm
->skm_size
= skc
->skc_mag_size
;
587 skm
->skm_refill
= skc
->skc_mag_refill
;
588 skm
->skm_cache
= skc
;
596 * Free a per-cpu magazine associated with a specific core.
599 spl_magazine_free(spl_kmem_magazine_t
*skm
)
601 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
602 ASSERT(skm
->skm_avail
== 0);
607 * Create all pre-cpu magazines of reasonable sizes.
610 spl_magazine_create(spl_kmem_cache_t
*skc
)
614 ASSERT((skc
->skc_flags
& KMC_SLAB
) == 0);
616 skc
->skc_mag
= kzalloc(sizeof (spl_kmem_magazine_t
*) *
617 num_possible_cpus(), kmem_flags_convert(KM_SLEEP
));
618 skc
->skc_mag_size
= spl_magazine_size(skc
);
619 skc
->skc_mag_refill
= (skc
->skc_mag_size
+ 1) / 2;
621 for_each_possible_cpu(i
) {
622 skc
->skc_mag
[i
] = spl_magazine_alloc(skc
, i
);
623 if (!skc
->skc_mag
[i
]) {
624 for (i
--; i
>= 0; i
--)
625 spl_magazine_free(skc
->skc_mag
[i
]);
636 * Destroy all pre-cpu magazines.
639 spl_magazine_destroy(spl_kmem_cache_t
*skc
)
641 spl_kmem_magazine_t
*skm
;
644 ASSERT((skc
->skc_flags
& KMC_SLAB
) == 0);
646 for_each_possible_cpu(i
) {
647 skm
= skc
->skc_mag
[i
];
648 spl_cache_flush(skc
, skm
, skm
->skm_avail
);
649 spl_magazine_free(skm
);
656 * Create a object cache based on the following arguments:
658 * size cache object size
659 * align cache object alignment
660 * ctor cache object constructor
661 * dtor cache object destructor
662 * reclaim cache object reclaim
663 * priv cache private data for ctor/dtor/reclaim
664 * vmp unused must be NULL
666 * KMC_KVMEM Force kvmem backed SPL cache
667 * KMC_SLAB Force Linux slab backed cache
668 * KMC_NODEBUG Disable debugging (unsupported)
669 * KMC_RECLAIMABLE Memory can be freed under pressure
672 spl_kmem_cache_create(const char *name
, size_t size
, size_t align
,
673 spl_kmem_ctor_t ctor
, spl_kmem_dtor_t dtor
, void *reclaim
,
674 void *priv
, void *vmp
, int flags
)
676 gfp_t lflags
= kmem_flags_convert(KM_SLEEP
);
677 spl_kmem_cache_t
*skc
;
684 ASSERT(reclaim
== NULL
);
688 skc
= kzalloc(sizeof (*skc
), lflags
);
692 skc
->skc_magic
= SKC_MAGIC
;
693 skc
->skc_name_size
= strlen(name
) + 1;
694 skc
->skc_name
= kmalloc(skc
->skc_name_size
, lflags
);
695 if (skc
->skc_name
== NULL
) {
699 strlcpy(skc
->skc_name
, name
, skc
->skc_name_size
);
701 skc
->skc_ctor
= ctor
;
702 skc
->skc_dtor
= dtor
;
703 skc
->skc_private
= priv
;
705 skc
->skc_linux_cache
= NULL
;
706 skc
->skc_flags
= flags
;
707 skc
->skc_obj_size
= size
;
708 skc
->skc_obj_align
= SPL_KMEM_CACHE_ALIGN
;
709 atomic_set(&skc
->skc_ref
, 0);
711 INIT_LIST_HEAD(&skc
->skc_list
);
712 INIT_LIST_HEAD(&skc
->skc_complete_list
);
713 INIT_LIST_HEAD(&skc
->skc_partial_list
);
714 skc
->skc_emergency_tree
= RB_ROOT
;
715 spin_lock_init(&skc
->skc_lock
);
716 init_waitqueue_head(&skc
->skc_waitq
);
717 skc
->skc_slab_fail
= 0;
718 skc
->skc_slab_create
= 0;
719 skc
->skc_slab_destroy
= 0;
720 skc
->skc_slab_total
= 0;
721 skc
->skc_slab_alloc
= 0;
722 skc
->skc_slab_max
= 0;
723 skc
->skc_obj_total
= 0;
724 skc
->skc_obj_alloc
= 0;
725 skc
->skc_obj_max
= 0;
726 skc
->skc_obj_deadlock
= 0;
727 skc
->skc_obj_emergency
= 0;
728 skc
->skc_obj_emergency_max
= 0;
730 rc
= percpu_counter_init(&skc
->skc_linux_alloc
, 0, GFP_KERNEL
);
737 * Verify the requested alignment restriction is sane.
741 VERIFY3U(align
, >=, SPL_KMEM_CACHE_ALIGN
);
742 VERIFY3U(align
, <=, PAGE_SIZE
);
743 skc
->skc_obj_align
= align
;
747 * When no specific type of slab is requested (kmem, vmem, or
748 * linuxslab) then select a cache type based on the object size
749 * and default tunables.
751 if (!(skc
->skc_flags
& (KMC_SLAB
| KMC_KVMEM
))) {
752 if (spl_kmem_cache_slab_limit
&&
753 size
<= (size_t)spl_kmem_cache_slab_limit
) {
755 * Objects smaller than spl_kmem_cache_slab_limit can
756 * use the Linux slab for better space-efficiency.
758 skc
->skc_flags
|= KMC_SLAB
;
761 * All other objects are considered large and are
762 * placed on kvmem backed slabs.
764 skc
->skc_flags
|= KMC_KVMEM
;
769 * Given the type of slab allocate the required resources.
771 if (skc
->skc_flags
& KMC_KVMEM
) {
772 rc
= spl_slab_size(skc
,
773 &skc
->skc_slab_objs
, &skc
->skc_slab_size
);
777 rc
= spl_magazine_create(skc
);
781 unsigned long slabflags
= 0;
783 if (size
> spl_kmem_cache_slab_limit
)
786 if (skc
->skc_flags
& KMC_RECLAIMABLE
)
787 slabflags
|= SLAB_RECLAIM_ACCOUNT
;
789 skc
->skc_linux_cache
= kmem_cache_create_usercopy(
790 skc
->skc_name
, size
, align
, slabflags
, 0, size
, NULL
);
791 if (skc
->skc_linux_cache
== NULL
)
795 down_write(&spl_kmem_cache_sem
);
796 list_add_tail(&skc
->skc_list
, &spl_kmem_cache_list
);
797 up_write(&spl_kmem_cache_sem
);
801 kfree(skc
->skc_name
);
802 percpu_counter_destroy(&skc
->skc_linux_alloc
);
806 EXPORT_SYMBOL(spl_kmem_cache_create
);
809 * Register a move callback for cache defragmentation.
810 * XXX: Unimplemented but harmless to stub out for now.
813 spl_kmem_cache_set_move(spl_kmem_cache_t
*skc
,
814 kmem_cbrc_t (move
)(void *, void *, size_t, void *))
816 ASSERT(move
!= NULL
);
818 EXPORT_SYMBOL(spl_kmem_cache_set_move
);
821 * Destroy a cache and all objects associated with the cache.
824 spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
)
826 DECLARE_WAIT_QUEUE_HEAD(wq
);
829 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
830 ASSERT(skc
->skc_flags
& (KMC_KVMEM
| KMC_SLAB
));
832 down_write(&spl_kmem_cache_sem
);
833 list_del_init(&skc
->skc_list
);
834 up_write(&spl_kmem_cache_sem
);
836 /* Cancel any and wait for any pending delayed tasks */
837 VERIFY(!test_and_set_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
839 spin_lock(&skc
->skc_lock
);
840 id
= skc
->skc_taskqid
;
841 spin_unlock(&skc
->skc_lock
);
843 taskq_cancel_id(spl_kmem_cache_taskq
, id
);
846 * Wait until all current callers complete, this is mainly
847 * to catch the case where a low memory situation triggers a
848 * cache reaping action which races with this destroy.
850 wait_event(wq
, atomic_read(&skc
->skc_ref
) == 0);
852 if (skc
->skc_flags
& KMC_KVMEM
) {
853 spl_magazine_destroy(skc
);
854 spl_slab_reclaim(skc
);
856 ASSERT(skc
->skc_flags
& KMC_SLAB
);
857 kmem_cache_destroy(skc
->skc_linux_cache
);
860 spin_lock(&skc
->skc_lock
);
863 * Validate there are no objects in use and free all the
864 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers.
866 ASSERT3U(skc
->skc_slab_alloc
, ==, 0);
867 ASSERT3U(skc
->skc_obj_alloc
, ==, 0);
868 ASSERT3U(skc
->skc_slab_total
, ==, 0);
869 ASSERT3U(skc
->skc_obj_total
, ==, 0);
870 ASSERT3U(skc
->skc_obj_emergency
, ==, 0);
871 ASSERT(list_empty(&skc
->skc_complete_list
));
873 ASSERT3U(percpu_counter_sum(&skc
->skc_linux_alloc
), ==, 0);
874 percpu_counter_destroy(&skc
->skc_linux_alloc
);
876 spin_unlock(&skc
->skc_lock
);
878 kfree(skc
->skc_name
);
881 EXPORT_SYMBOL(spl_kmem_cache_destroy
);
884 * Allocate an object from a slab attached to the cache. This is used to
885 * repopulate the per-cpu magazine caches in batches when they run low.
888 spl_cache_obj(spl_kmem_cache_t
*skc
, spl_kmem_slab_t
*sks
)
892 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
893 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
895 sko
= list_entry(sks
->sks_free_list
.next
, spl_kmem_obj_t
, sko_list
);
896 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
897 ASSERT(sko
->sko_addr
!= NULL
);
899 /* Remove from sks_free_list */
900 list_del_init(&sko
->sko_list
);
902 sks
->sks_age
= jiffies
;
904 skc
->skc_obj_alloc
++;
906 /* Track max obj usage statistics */
907 if (skc
->skc_obj_alloc
> skc
->skc_obj_max
)
908 skc
->skc_obj_max
= skc
->skc_obj_alloc
;
910 /* Track max slab usage statistics */
911 if (sks
->sks_ref
== 1) {
912 skc
->skc_slab_alloc
++;
914 if (skc
->skc_slab_alloc
> skc
->skc_slab_max
)
915 skc
->skc_slab_max
= skc
->skc_slab_alloc
;
918 return (sko
->sko_addr
);
922 * Generic slab allocation function to run by the global work queues.
923 * It is responsible for allocating a new slab, linking it in to the list
924 * of partial slabs, and then waking any waiters.
927 __spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
)
929 spl_kmem_slab_t
*sks
;
931 fstrans_cookie_t cookie
= spl_fstrans_mark();
932 sks
= spl_slab_alloc(skc
, flags
);
933 spl_fstrans_unmark(cookie
);
935 spin_lock(&skc
->skc_lock
);
937 skc
->skc_slab_total
++;
938 skc
->skc_obj_total
+= sks
->sks_objs
;
939 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
941 smp_mb__before_atomic();
942 clear_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
);
943 smp_mb__after_atomic();
945 spin_unlock(&skc
->skc_lock
);
947 return (sks
== NULL
? -ENOMEM
: 0);
951 spl_cache_grow_work(void *data
)
953 spl_kmem_alloc_t
*ska
= (spl_kmem_alloc_t
*)data
;
954 spl_kmem_cache_t
*skc
= ska
->ska_cache
;
956 int error
= __spl_cache_grow(skc
, ska
->ska_flags
);
958 atomic_dec(&skc
->skc_ref
);
959 smp_mb__before_atomic();
960 clear_bit(KMC_BIT_GROWING
, &skc
->skc_flags
);
961 smp_mb__after_atomic();
963 wake_up_all(&skc
->skc_waitq
);
969 * Returns non-zero when a new slab should be available.
972 spl_cache_grow_wait(spl_kmem_cache_t
*skc
)
974 return (!test_bit(KMC_BIT_GROWING
, &skc
->skc_flags
));
978 * No available objects on any slabs, create a new slab. Note that this
979 * functionality is disabled for KMC_SLAB caches which are backed by the
983 spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
, void **obj
)
985 int remaining
, rc
= 0;
987 ASSERT0(flags
& ~KM_PUBLIC_MASK
);
988 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
989 ASSERT((skc
->skc_flags
& KMC_SLAB
) == 0);
994 * Since we can't sleep attempt an emergency allocation to satisfy
995 * the request. The only alterative is to fail the allocation but
996 * it's preferable try. The use of KM_NOSLEEP is expected to be rare.
998 if (flags
& KM_NOSLEEP
)
999 return (spl_emergency_alloc(skc
, flags
, obj
));
1004 * Before allocating a new slab wait for any reaping to complete and
1005 * then return so the local magazine can be rechecked for new objects.
1007 if (test_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1008 rc
= wait_on_bit(&skc
->skc_flags
, KMC_BIT_REAPING
,
1009 TASK_UNINTERRUPTIBLE
);
1010 return (rc
? rc
: -EAGAIN
);
1014 * Note: It would be nice to reduce the overhead of context switch
1015 * and improve NUMA locality, by trying to allocate a new slab in the
1016 * current process context with KM_NOSLEEP flag.
1018 * However, this can't be applied to vmem/kvmem due to a bug that
1019 * spl_vmalloc() doesn't honor gfp flags in page table allocation.
1023 * This is handled by dispatching a work request to the global work
1024 * queue. This allows us to asynchronously allocate a new slab while
1025 * retaining the ability to safely fall back to a smaller synchronous
1026 * allocations to ensure forward progress is always maintained.
1028 if (test_and_set_bit(KMC_BIT_GROWING
, &skc
->skc_flags
) == 0) {
1029 spl_kmem_alloc_t
*ska
;
1031 ska
= kmalloc(sizeof (*ska
), kmem_flags_convert(flags
));
1033 clear_bit_unlock(KMC_BIT_GROWING
, &skc
->skc_flags
);
1034 smp_mb__after_atomic();
1035 wake_up_all(&skc
->skc_waitq
);
1039 atomic_inc(&skc
->skc_ref
);
1040 ska
->ska_cache
= skc
;
1041 ska
->ska_flags
= flags
;
1042 taskq_init_ent(&ska
->ska_tqe
);
1043 taskq_dispatch_ent(spl_kmem_cache_taskq
,
1044 spl_cache_grow_work
, ska
, 0, &ska
->ska_tqe
);
1048 * The goal here is to only detect the rare case where a virtual slab
1049 * allocation has deadlocked. We must be careful to minimize the use
1050 * of emergency objects which are more expensive to track. Therefore,
1051 * we set a very long timeout for the asynchronous allocation and if
1052 * the timeout is reached the cache is flagged as deadlocked. From
1053 * this point only new emergency objects will be allocated until the
1054 * asynchronous allocation completes and clears the deadlocked flag.
1056 if (test_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
)) {
1057 rc
= spl_emergency_alloc(skc
, flags
, obj
);
1059 remaining
= wait_event_timeout(skc
->skc_waitq
,
1060 spl_cache_grow_wait(skc
), HZ
/ 10);
1063 spin_lock(&skc
->skc_lock
);
1064 if (test_bit(KMC_BIT_GROWING
, &skc
->skc_flags
)) {
1065 set_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
);
1066 skc
->skc_obj_deadlock
++;
1068 spin_unlock(&skc
->skc_lock
);
1078 * Refill a per-cpu magazine with objects from the slabs for this cache.
1079 * Ideally the magazine can be repopulated using existing objects which have
1080 * been released, however if we are unable to locate enough free objects new
1081 * slabs of objects will be created. On success NULL is returned, otherwise
1082 * the address of a single emergency object is returned for use by the caller.
1085 spl_cache_refill(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flags
)
1087 spl_kmem_slab_t
*sks
;
1088 int count
= 0, rc
, refill
;
1091 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1092 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1094 refill
= MIN(skm
->skm_refill
, skm
->skm_size
- skm
->skm_avail
);
1095 spin_lock(&skc
->skc_lock
);
1097 while (refill
> 0) {
1098 /* No slabs available we may need to grow the cache */
1099 if (list_empty(&skc
->skc_partial_list
)) {
1100 spin_unlock(&skc
->skc_lock
);
1103 rc
= spl_cache_grow(skc
, flags
, &obj
);
1104 local_irq_disable();
1106 /* Emergency object for immediate use by caller */
1107 if (rc
== 0 && obj
!= NULL
)
1113 /* Rescheduled to different CPU skm is not local */
1114 if (skm
!= skc
->skc_mag
[smp_processor_id()])
1118 * Potentially rescheduled to the same CPU but
1119 * allocations may have occurred from this CPU while
1120 * we were sleeping so recalculate max refill.
1122 refill
= MIN(refill
, skm
->skm_size
- skm
->skm_avail
);
1124 spin_lock(&skc
->skc_lock
);
1128 /* Grab the next available slab */
1129 sks
= list_entry((&skc
->skc_partial_list
)->next
,
1130 spl_kmem_slab_t
, sks_list
);
1131 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1132 ASSERT(sks
->sks_ref
< sks
->sks_objs
);
1133 ASSERT(!list_empty(&sks
->sks_free_list
));
1136 * Consume as many objects as needed to refill the requested
1137 * cache. We must also be careful not to overfill it.
1139 while (sks
->sks_ref
< sks
->sks_objs
&& refill
-- > 0 &&
1141 ASSERT(skm
->skm_avail
< skm
->skm_size
);
1142 ASSERT(count
< skm
->skm_size
);
1143 skm
->skm_objs
[skm
->skm_avail
++] =
1144 spl_cache_obj(skc
, sks
);
1147 /* Move slab to skc_complete_list when full */
1148 if (sks
->sks_ref
== sks
->sks_objs
) {
1149 list_del(&sks
->sks_list
);
1150 list_add(&sks
->sks_list
, &skc
->skc_complete_list
);
1154 spin_unlock(&skc
->skc_lock
);
1160 * Release an object back to the slab from which it came.
1163 spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
)
1165 spl_kmem_slab_t
*sks
= NULL
;
1166 spl_kmem_obj_t
*sko
= NULL
;
1168 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1170 sko
= spl_sko_from_obj(skc
, obj
);
1171 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1172 sks
= sko
->sko_slab
;
1173 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1174 ASSERT(sks
->sks_cache
== skc
);
1175 list_add(&sko
->sko_list
, &sks
->sks_free_list
);
1177 sks
->sks_age
= jiffies
;
1179 skc
->skc_obj_alloc
--;
1182 * Move slab to skc_partial_list when no longer full. Slabs
1183 * are added to the head to keep the partial list is quasi-full
1184 * sorted order. Fuller at the head, emptier at the tail.
1186 if (sks
->sks_ref
== (sks
->sks_objs
- 1)) {
1187 list_del(&sks
->sks_list
);
1188 list_add(&sks
->sks_list
, &skc
->skc_partial_list
);
1192 * Move empty slabs to the end of the partial list so
1193 * they can be easily found and freed during reclamation.
1195 if (sks
->sks_ref
== 0) {
1196 list_del(&sks
->sks_list
);
1197 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1198 skc
->skc_slab_alloc
--;
1203 * Allocate an object from the per-cpu magazine, or if the magazine
1204 * is empty directly allocate from a slab and repopulate the magazine.
1207 spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
)
1209 spl_kmem_magazine_t
*skm
;
1212 ASSERT0(flags
& ~KM_PUBLIC_MASK
);
1213 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1214 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1217 * Allocate directly from a Linux slab. All optimizations are left
1218 * to the underlying cache we only need to guarantee that KM_SLEEP
1219 * callers will never fail.
1221 if (skc
->skc_flags
& KMC_SLAB
) {
1222 struct kmem_cache
*slc
= skc
->skc_linux_cache
;
1224 obj
= kmem_cache_alloc(slc
, kmem_flags_convert(flags
));
1225 } while ((obj
== NULL
) && !(flags
& KM_NOSLEEP
));
1229 * Even though we leave everything up to the
1230 * underlying cache we still keep track of
1231 * how many objects we've allocated in it for
1232 * better debuggability.
1234 percpu_counter_inc(&skc
->skc_linux_alloc
);
1239 local_irq_disable();
1243 * Safe to update per-cpu structure without lock, but
1244 * in the restart case we must be careful to reacquire
1245 * the local magazine since this may have changed
1246 * when we need to grow the cache.
1248 skm
= skc
->skc_mag
[smp_processor_id()];
1249 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1251 if (likely(skm
->skm_avail
)) {
1252 /* Object available in CPU cache, use it */
1253 obj
= skm
->skm_objs
[--skm
->skm_avail
];
1255 obj
= spl_cache_refill(skc
, skm
, flags
);
1256 if ((obj
== NULL
) && !(flags
& KM_NOSLEEP
))
1265 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
1268 /* Pre-emptively migrate object to CPU L1 cache */
1270 if (obj
&& skc
->skc_ctor
)
1271 skc
->skc_ctor(obj
, skc
->skc_private
, flags
);
1278 EXPORT_SYMBOL(spl_kmem_cache_alloc
);
1281 * Free an object back to the local per-cpu magazine, there is no
1282 * guarantee that this is the same magazine the object was originally
1283 * allocated from. We may need to flush entire from the magazine
1284 * back to the slabs to make space.
1287 spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
)
1289 spl_kmem_magazine_t
*skm
;
1290 unsigned long flags
;
1292 int do_emergency
= 0;
1294 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1295 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1298 * Run the destructor
1301 skc
->skc_dtor(obj
, skc
->skc_private
);
1304 * Free the object from the Linux underlying Linux slab.
1306 if (skc
->skc_flags
& KMC_SLAB
) {
1307 kmem_cache_free(skc
->skc_linux_cache
, obj
);
1308 percpu_counter_dec(&skc
->skc_linux_alloc
);
1313 * While a cache has outstanding emergency objects all freed objects
1314 * must be checked. However, since emergency objects will never use
1315 * a virtual address these objects can be safely excluded as an
1318 if (!is_vmalloc_addr(obj
)) {
1319 spin_lock(&skc
->skc_lock
);
1320 do_emergency
= (skc
->skc_obj_emergency
> 0);
1321 spin_unlock(&skc
->skc_lock
);
1323 if (do_emergency
&& (spl_emergency_free(skc
, obj
) == 0))
1327 local_irq_save(flags
);
1330 * Safe to update per-cpu structure without lock, but
1331 * no remote memory allocation tracking is being performed
1332 * it is entirely possible to allocate an object from one
1333 * CPU cache and return it to another.
1335 skm
= skc
->skc_mag
[smp_processor_id()];
1336 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1339 * Per-CPU cache full, flush it to make space for this object,
1340 * this may result in an empty slab which can be reclaimed once
1341 * interrupts are re-enabled.
1343 if (unlikely(skm
->skm_avail
>= skm
->skm_size
)) {
1344 spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1348 /* Available space in cache, use it */
1349 skm
->skm_objs
[skm
->skm_avail
++] = obj
;
1351 local_irq_restore(flags
);
1354 spl_slab_reclaim(skc
);
1356 EXPORT_SYMBOL(spl_kmem_cache_free
);
1359 * Depending on how many and which objects are released it may simply
1360 * repopulate the local magazine which will then need to age-out. Objects
1361 * which cannot fit in the magazine will be released back to their slabs
1362 * which will also need to age out before being released. This is all just
1363 * best effort and we do not want to thrash creating and destroying slabs.
1366 spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
)
1368 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1369 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1371 if (skc
->skc_flags
& KMC_SLAB
)
1374 atomic_inc(&skc
->skc_ref
);
1377 * Prevent concurrent cache reaping when contended.
1379 if (test_and_set_bit(KMC_BIT_REAPING
, &skc
->skc_flags
))
1382 /* Reclaim from the magazine and free all now empty slabs. */
1383 unsigned long irq_flags
;
1384 local_irq_save(irq_flags
);
1385 spl_kmem_magazine_t
*skm
= skc
->skc_mag
[smp_processor_id()];
1386 spl_cache_flush(skc
, skm
, skm
->skm_avail
);
1387 local_irq_restore(irq_flags
);
1389 spl_slab_reclaim(skc
);
1390 clear_bit_unlock(KMC_BIT_REAPING
, &skc
->skc_flags
);
1391 smp_mb__after_atomic();
1392 wake_up_bit(&skc
->skc_flags
, KMC_BIT_REAPING
);
1394 atomic_dec(&skc
->skc_ref
);
1396 EXPORT_SYMBOL(spl_kmem_cache_reap_now
);
1399 * This is stubbed out for code consistency with other platforms. There
1400 * is existing logic to prevent concurrent reaping so while this is ugly
1401 * it should do no harm.
1404 spl_kmem_cache_reap_active(void)
1408 EXPORT_SYMBOL(spl_kmem_cache_reap_active
);
1411 * Reap all free slabs from all registered caches.
1416 spl_kmem_cache_t
*skc
= NULL
;
1418 down_read(&spl_kmem_cache_sem
);
1419 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
1420 spl_kmem_cache_reap_now(skc
);
1422 up_read(&spl_kmem_cache_sem
);
1424 EXPORT_SYMBOL(spl_kmem_reap
);
1427 spl_kmem_cache_init(void)
1429 init_rwsem(&spl_kmem_cache_sem
);
1430 INIT_LIST_HEAD(&spl_kmem_cache_list
);
1431 spl_kmem_cache_taskq
= taskq_create("spl_kmem_cache",
1432 spl_kmem_cache_kmem_threads
, maxclsyspri
,
1433 spl_kmem_cache_kmem_threads
* 8, INT_MAX
,
1434 TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
1436 if (spl_kmem_cache_taskq
== NULL
)
1443 spl_kmem_cache_fini(void)
1445 taskq_destroy(spl_kmem_cache_taskq
);