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)
52 * Cache magazines are an optimization designed to minimize the cost of
53 * allocating memory. They do this by keeping a per-cpu cache of recently
54 * freed objects, which can then be reallocated without taking a lock. This
55 * can improve performance on highly contended caches. However, because
56 * objects in magazines will prevent otherwise empty slabs from being
57 * immediately released this may not be ideal for low memory machines.
59 * For this reason spl_kmem_cache_magazine_size can be used to set a maximum
60 * magazine size. When this value is set to 0 the magazine size will be
61 * automatically determined based on the object size. Otherwise magazines
62 * will be limited to 2-256 objects per magazine (i.e per cpu). Magazines
63 * may never be entirely disabled in this implementation.
65 static unsigned int spl_kmem_cache_magazine_size
= 0;
66 module_param(spl_kmem_cache_magazine_size
, uint
, 0444);
67 MODULE_PARM_DESC(spl_kmem_cache_magazine_size
,
68 "Default magazine size (2-256), set automatically (0)");
70 static unsigned int spl_kmem_cache_obj_per_slab
= SPL_KMEM_CACHE_OBJ_PER_SLAB
;
71 module_param(spl_kmem_cache_obj_per_slab
, uint
, 0644);
72 MODULE_PARM_DESC(spl_kmem_cache_obj_per_slab
, "Number of objects per slab");
74 static unsigned int spl_kmem_cache_max_size
= SPL_KMEM_CACHE_MAX_SIZE
;
75 module_param(spl_kmem_cache_max_size
, uint
, 0644);
76 MODULE_PARM_DESC(spl_kmem_cache_max_size
, "Maximum size of slab in MB");
79 * For small objects the Linux slab allocator should be used to make the most
80 * efficient use of the memory. However, large objects are not supported by
81 * the Linux slab and therefore the SPL implementation is preferred. A cutoff
82 * of 16K was determined to be optimal for architectures using 4K pages and
83 * to also work well on architecutres using larger 64K page sizes.
85 static unsigned int spl_kmem_cache_slab_limit
=
86 SPL_MAX_KMEM_ORDER_NR_PAGES
* PAGE_SIZE
;
87 module_param(spl_kmem_cache_slab_limit
, uint
, 0644);
88 MODULE_PARM_DESC(spl_kmem_cache_slab_limit
,
89 "Objects less than N bytes use the Linux slab");
92 * The number of threads available to allocate new slabs for caches. This
93 * should not need to be tuned but it is available for performance analysis.
95 static unsigned int spl_kmem_cache_kmem_threads
= 4;
96 module_param(spl_kmem_cache_kmem_threads
, uint
, 0444);
97 MODULE_PARM_DESC(spl_kmem_cache_kmem_threads
,
98 "Number of spl_kmem_cache threads");
101 * Slab allocation interfaces
103 * While the Linux slab implementation was inspired by the Solaris
104 * implementation I cannot use it to emulate the Solaris APIs. I
105 * require two features which are not provided by the Linux slab.
107 * 1) Constructors AND destructors. Recent versions of the Linux
108 * kernel have removed support for destructors. This is a deal
109 * breaker for the SPL which contains particularly expensive
110 * initializers for mutex's, condition variables, etc. We also
111 * require a minimal level of cleanup for these data types unlike
112 * many Linux data types which do need to be explicitly destroyed.
114 * 2) Virtual address space backed slab. Callers of the Solaris slab
115 * expect it to work well for both small are very large allocations.
116 * Because of memory fragmentation the Linux slab which is backed
117 * by kmalloc'ed memory performs very badly when confronted with
118 * large numbers of large allocations. Basing the slab on the
119 * virtual address space removes the need for contiguous pages
120 * and greatly improve performance for large allocations.
122 * For these reasons, the SPL has its own slab implementation with
123 * the needed features. It is not as highly optimized as either the
124 * Solaris or Linux slabs, but it should get me most of what is
125 * needed until it can be optimized or obsoleted by another approach.
127 * One serious concern I do have about this method is the relatively
128 * small virtual address space on 32bit arches. This will seriously
129 * constrain the size of the slab caches and their performance.
132 struct list_head spl_kmem_cache_list
; /* List of caches */
133 struct rw_semaphore spl_kmem_cache_sem
; /* Cache list lock */
134 static taskq_t
*spl_kmem_cache_taskq
; /* Task queue for aging / reclaim */
136 static void spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
);
139 kv_alloc(spl_kmem_cache_t
*skc
, int size
, int flags
)
141 gfp_t lflags
= kmem_flags_convert(flags
);
144 if (skc
->skc_flags
& KMC_RECLAIMABLE
)
145 lflags
|= __GFP_RECLAIMABLE
;
146 ptr
= spl_vmalloc(size
, lflags
| __GFP_HIGHMEM
);
148 /* Resulting allocated memory will be page aligned */
149 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
155 kv_free(spl_kmem_cache_t
*skc
, void *ptr
, int size
)
157 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
160 * The Linux direct reclaim path uses this out of band value to
161 * determine if forward progress is being made. Normally this is
162 * incremented by kmem_freepages() which is part of the various
163 * Linux slab implementations. However, since we are using none
164 * of that infrastructure we are responsible for incrementing it.
166 if (current
->reclaim_state
)
167 #ifdef HAVE_RECLAIM_STATE_RECLAIMED
168 current
->reclaim_state
->reclaimed
+= size
>> PAGE_SHIFT
;
170 current
->reclaim_state
->reclaimed_slab
+= size
>> PAGE_SHIFT
;
176 * Required space for each aligned sks.
178 static inline uint32_t
179 spl_sks_size(spl_kmem_cache_t
*skc
)
181 return (P2ROUNDUP_TYPED(sizeof (spl_kmem_slab_t
),
182 skc
->skc_obj_align
, uint32_t));
186 * Required space for each aligned object.
188 static inline uint32_t
189 spl_obj_size(spl_kmem_cache_t
*skc
)
191 uint32_t align
= skc
->skc_obj_align
;
193 return (P2ROUNDUP_TYPED(skc
->skc_obj_size
, align
, uint32_t) +
194 P2ROUNDUP_TYPED(sizeof (spl_kmem_obj_t
), align
, uint32_t));
198 spl_kmem_cache_inuse(kmem_cache_t
*cache
)
200 return (cache
->skc_obj_total
);
202 EXPORT_SYMBOL(spl_kmem_cache_inuse
);
205 spl_kmem_cache_entry_size(kmem_cache_t
*cache
)
207 return (cache
->skc_obj_size
);
209 EXPORT_SYMBOL(spl_kmem_cache_entry_size
);
212 * Lookup the spl_kmem_object_t for an object given that object.
214 static inline spl_kmem_obj_t
*
215 spl_sko_from_obj(spl_kmem_cache_t
*skc
, void *obj
)
217 return (obj
+ P2ROUNDUP_TYPED(skc
->skc_obj_size
,
218 skc
->skc_obj_align
, uint32_t));
222 * It's important that we pack the spl_kmem_obj_t structure and the
223 * actual objects in to one large address space to minimize the number
224 * of calls to the allocator. It is far better to do a few large
225 * allocations and then subdivide it ourselves. Now which allocator
226 * we use requires balancing a few trade offs.
228 * For small objects we use kmem_alloc() because as long as you are
229 * only requesting a small number of pages (ideally just one) its cheap.
230 * However, when you start requesting multiple pages with kmem_alloc()
231 * it gets increasingly expensive since it requires contiguous pages.
232 * For this reason we shift to vmem_alloc() for slabs of large objects
233 * which removes the need for contiguous pages. We do not use
234 * vmem_alloc() in all cases because there is significant locking
235 * overhead in __get_vm_area_node(). This function takes a single
236 * global lock when acquiring an available virtual address range which
237 * serializes all vmem_alloc()'s for all slab caches. Using slightly
238 * different allocation functions for small and large objects should
239 * give us the best of both worlds.
241 * +------------------------+
242 * | spl_kmem_slab_t --+-+ |
243 * | skc_obj_size <-+ | |
244 * | spl_kmem_obj_t | |
245 * | skc_obj_size <---+ |
246 * | spl_kmem_obj_t | |
248 * +------------------------+
250 static spl_kmem_slab_t
*
251 spl_slab_alloc(spl_kmem_cache_t
*skc
, int flags
)
253 spl_kmem_slab_t
*sks
;
257 base
= kv_alloc(skc
, skc
->skc_slab_size
, flags
);
261 sks
= (spl_kmem_slab_t
*)base
;
262 sks
->sks_magic
= SKS_MAGIC
;
263 sks
->sks_objs
= skc
->skc_slab_objs
;
264 sks
->sks_age
= jiffies
;
265 sks
->sks_cache
= skc
;
266 INIT_LIST_HEAD(&sks
->sks_list
);
267 INIT_LIST_HEAD(&sks
->sks_free_list
);
269 obj_size
= spl_obj_size(skc
);
271 for (int i
= 0; i
< sks
->sks_objs
; i
++) {
272 void *obj
= base
+ spl_sks_size(skc
) + (i
* obj_size
);
274 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
275 spl_kmem_obj_t
*sko
= spl_sko_from_obj(skc
, obj
);
277 sko
->sko_magic
= SKO_MAGIC
;
279 INIT_LIST_HEAD(&sko
->sko_list
);
280 list_add_tail(&sko
->sko_list
, &sks
->sks_free_list
);
287 * Remove a slab from complete or partial list, it must be called with
288 * the 'skc->skc_lock' held but the actual free must be performed
289 * outside the lock to prevent deadlocking on vmem addresses.
292 spl_slab_free(spl_kmem_slab_t
*sks
,
293 struct list_head
*sks_list
, struct list_head
*sko_list
)
295 spl_kmem_cache_t
*skc
;
297 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
298 ASSERT(sks
->sks_ref
== 0);
300 skc
= sks
->sks_cache
;
301 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
304 * Update slab/objects counters in the cache, then remove the
305 * slab from the skc->skc_partial_list. Finally add the slab
306 * and all its objects in to the private work lists where the
307 * destructors will be called and the memory freed to the system.
309 skc
->skc_obj_total
-= sks
->sks_objs
;
310 skc
->skc_slab_total
--;
311 list_del(&sks
->sks_list
);
312 list_add(&sks
->sks_list
, sks_list
);
313 list_splice_init(&sks
->sks_free_list
, sko_list
);
317 * Reclaim empty slabs at the end of the partial list.
320 spl_slab_reclaim(spl_kmem_cache_t
*skc
)
322 spl_kmem_slab_t
*sks
= NULL
, *m
= NULL
;
323 spl_kmem_obj_t
*sko
= NULL
, *n
= NULL
;
328 * Empty slabs and objects must be moved to a private list so they
329 * can be safely freed outside the spin lock. All empty slabs are
330 * at the end of skc->skc_partial_list, therefore once a non-empty
331 * slab is found we can stop scanning.
333 spin_lock(&skc
->skc_lock
);
334 list_for_each_entry_safe_reverse(sks
, m
,
335 &skc
->skc_partial_list
, sks_list
) {
337 if (sks
->sks_ref
> 0)
340 spl_slab_free(sks
, &sks_list
, &sko_list
);
342 spin_unlock(&skc
->skc_lock
);
345 * The following two loops ensure all the object destructors are run,
346 * and the slabs themselves are freed. This is all done outside the
347 * skc->skc_lock since this allows the destructor to sleep, and
348 * allows us to perform a conditional reschedule when a freeing a
349 * large number of objects and slabs back to the system.
352 list_for_each_entry_safe(sko
, n
, &sko_list
, sko_list
) {
353 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
356 list_for_each_entry_safe(sks
, m
, &sks_list
, sks_list
) {
357 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
358 kv_free(skc
, sks
, skc
->skc_slab_size
);
362 static spl_kmem_emergency_t
*
363 spl_emergency_search(struct rb_root
*root
, void *obj
)
365 struct rb_node
*node
= root
->rb_node
;
366 spl_kmem_emergency_t
*ske
;
367 unsigned long address
= (unsigned long)obj
;
370 ske
= container_of(node
, spl_kmem_emergency_t
, ske_node
);
372 if (address
< ske
->ske_obj
)
373 node
= node
->rb_left
;
374 else if (address
> ske
->ske_obj
)
375 node
= node
->rb_right
;
384 spl_emergency_insert(struct rb_root
*root
, spl_kmem_emergency_t
*ske
)
386 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
387 spl_kmem_emergency_t
*ske_tmp
;
388 unsigned long address
= ske
->ske_obj
;
391 ske_tmp
= container_of(*new, spl_kmem_emergency_t
, ske_node
);
394 if (address
< ske_tmp
->ske_obj
)
395 new = &((*new)->rb_left
);
396 else if (address
> ske_tmp
->ske_obj
)
397 new = &((*new)->rb_right
);
402 rb_link_node(&ske
->ske_node
, parent
, new);
403 rb_insert_color(&ske
->ske_node
, root
);
409 * Allocate a single emergency object and track it in a red black tree.
412 spl_emergency_alloc(spl_kmem_cache_t
*skc
, int flags
, void **obj
)
414 gfp_t lflags
= kmem_flags_convert(flags
);
415 spl_kmem_emergency_t
*ske
;
416 int order
= get_order(skc
->skc_obj_size
);
419 /* Last chance use a partial slab if one now exists */
420 spin_lock(&skc
->skc_lock
);
421 empty
= list_empty(&skc
->skc_partial_list
);
422 spin_unlock(&skc
->skc_lock
);
426 if (skc
->skc_flags
& KMC_RECLAIMABLE
)
427 lflags
|= __GFP_RECLAIMABLE
;
428 ske
= kmalloc(sizeof (*ske
), lflags
);
432 ske
->ske_obj
= __get_free_pages(lflags
, order
);
433 if (ske
->ske_obj
== 0) {
438 spin_lock(&skc
->skc_lock
);
439 empty
= spl_emergency_insert(&skc
->skc_emergency_tree
, ske
);
441 skc
->skc_obj_total
++;
442 skc
->skc_obj_emergency
++;
443 if (skc
->skc_obj_emergency
> skc
->skc_obj_emergency_max
)
444 skc
->skc_obj_emergency_max
= skc
->skc_obj_emergency
;
446 spin_unlock(&skc
->skc_lock
);
448 if (unlikely(!empty
)) {
449 free_pages(ske
->ske_obj
, order
);
454 *obj
= (void *)ske
->ske_obj
;
460 * Locate the passed object in the red black tree and free it.
463 spl_emergency_free(spl_kmem_cache_t
*skc
, void *obj
)
465 spl_kmem_emergency_t
*ske
;
466 int order
= get_order(skc
->skc_obj_size
);
468 spin_lock(&skc
->skc_lock
);
469 ske
= spl_emergency_search(&skc
->skc_emergency_tree
, obj
);
471 rb_erase(&ske
->ske_node
, &skc
->skc_emergency_tree
);
472 skc
->skc_obj_emergency
--;
473 skc
->skc_obj_total
--;
475 spin_unlock(&skc
->skc_lock
);
480 free_pages(ske
->ske_obj
, order
);
487 * Release objects from the per-cpu magazine back to their slab. The flush
488 * argument contains the max number of entries to remove from the magazine.
491 spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
493 spin_lock(&skc
->skc_lock
);
495 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
496 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
498 int count
= MIN(flush
, skm
->skm_avail
);
499 for (int i
= 0; i
< count
; i
++)
500 spl_cache_shrink(skc
, skm
->skm_objs
[i
]);
502 skm
->skm_avail
-= count
;
503 memmove(skm
->skm_objs
, &(skm
->skm_objs
[count
]),
504 sizeof (void *) * skm
->skm_avail
);
506 spin_unlock(&skc
->skc_lock
);
510 * Size a slab based on the size of each aligned object plus spl_kmem_obj_t.
511 * When on-slab we want to target spl_kmem_cache_obj_per_slab. However,
512 * for very small objects we may end up with more than this so as not
513 * to waste space in the minimal allocation of a single page.
516 spl_slab_size(spl_kmem_cache_t
*skc
, uint32_t *objs
, uint32_t *size
)
518 uint32_t sks_size
, obj_size
, max_size
, tgt_size
, tgt_objs
;
520 sks_size
= spl_sks_size(skc
);
521 obj_size
= spl_obj_size(skc
);
522 max_size
= (spl_kmem_cache_max_size
* 1024 * 1024);
523 tgt_size
= (spl_kmem_cache_obj_per_slab
* obj_size
+ sks_size
);
525 if (tgt_size
<= max_size
) {
526 tgt_objs
= (tgt_size
- sks_size
) / obj_size
;
528 tgt_objs
= (max_size
- sks_size
) / obj_size
;
529 tgt_size
= (tgt_objs
* obj_size
) + sks_size
;
542 * Make a guess at reasonable per-cpu magazine size based on the size of
543 * each object and the cost of caching N of them in each magazine. Long
544 * term this should really adapt based on an observed usage heuristic.
547 spl_magazine_size(spl_kmem_cache_t
*skc
)
549 uint32_t obj_size
= spl_obj_size(skc
);
552 if (spl_kmem_cache_magazine_size
> 0)
553 return (MAX(MIN(spl_kmem_cache_magazine_size
, 256), 2));
555 /* Per-magazine sizes below assume a 4Kib page size */
556 if (obj_size
> (PAGE_SIZE
* 256))
557 size
= 4; /* Minimum 4Mib per-magazine */
558 else if (obj_size
> (PAGE_SIZE
* 32))
559 size
= 16; /* Minimum 2Mib per-magazine */
560 else if (obj_size
> (PAGE_SIZE
))
561 size
= 64; /* Minimum 256Kib per-magazine */
562 else if (obj_size
> (PAGE_SIZE
/ 4))
563 size
= 128; /* Minimum 128Kib per-magazine */
571 * Allocate a per-cpu magazine to associate with a specific core.
573 static spl_kmem_magazine_t
*
574 spl_magazine_alloc(spl_kmem_cache_t
*skc
, int cpu
)
576 spl_kmem_magazine_t
*skm
;
577 int size
= sizeof (spl_kmem_magazine_t
) +
578 sizeof (void *) * skc
->skc_mag_size
;
580 skm
= kmalloc_node(size
, GFP_KERNEL
, cpu_to_node(cpu
));
582 skm
->skm_magic
= SKM_MAGIC
;
584 skm
->skm_size
= skc
->skc_mag_size
;
585 skm
->skm_refill
= skc
->skc_mag_refill
;
586 skm
->skm_cache
= skc
;
594 * Free a per-cpu magazine associated with a specific core.
597 spl_magazine_free(spl_kmem_magazine_t
*skm
)
599 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
600 ASSERT(skm
->skm_avail
== 0);
605 * Create all pre-cpu magazines of reasonable sizes.
608 spl_magazine_create(spl_kmem_cache_t
*skc
)
612 ASSERT((skc
->skc_flags
& KMC_SLAB
) == 0);
614 skc
->skc_mag
= kzalloc(sizeof (spl_kmem_magazine_t
*) *
615 num_possible_cpus(), kmem_flags_convert(KM_SLEEP
));
616 skc
->skc_mag_size
= spl_magazine_size(skc
);
617 skc
->skc_mag_refill
= (skc
->skc_mag_size
+ 1) / 2;
619 for_each_possible_cpu(i
) {
620 skc
->skc_mag
[i
] = spl_magazine_alloc(skc
, i
);
621 if (!skc
->skc_mag
[i
]) {
622 for (i
--; i
>= 0; i
--)
623 spl_magazine_free(skc
->skc_mag
[i
]);
634 * Destroy all pre-cpu magazines.
637 spl_magazine_destroy(spl_kmem_cache_t
*skc
)
639 spl_kmem_magazine_t
*skm
;
642 ASSERT((skc
->skc_flags
& KMC_SLAB
) == 0);
644 for_each_possible_cpu(i
) {
645 skm
= skc
->skc_mag
[i
];
646 spl_cache_flush(skc
, skm
, skm
->skm_avail
);
647 spl_magazine_free(skm
);
654 * Create a object cache based on the following arguments:
656 * size cache object size
657 * align cache object alignment
658 * ctor cache object constructor
659 * dtor cache object destructor
660 * reclaim cache object reclaim
661 * priv cache private data for ctor/dtor/reclaim
662 * vmp unused must be NULL
664 * KMC_KVMEM Force kvmem backed SPL cache
665 * KMC_SLAB Force Linux slab backed cache
666 * KMC_NODEBUG Disable debugging (unsupported)
667 * KMC_RECLAIMABLE Memory can be freed under pressure
670 spl_kmem_cache_create(const char *name
, size_t size
, size_t align
,
671 spl_kmem_ctor_t ctor
, spl_kmem_dtor_t dtor
, void *reclaim
,
672 void *priv
, void *vmp
, int flags
)
674 gfp_t lflags
= kmem_flags_convert(KM_SLEEP
);
675 spl_kmem_cache_t
*skc
;
682 ASSERT(reclaim
== NULL
);
686 skc
= kzalloc(sizeof (*skc
), lflags
);
690 skc
->skc_magic
= SKC_MAGIC
;
691 skc
->skc_name_size
= strlen(name
) + 1;
692 skc
->skc_name
= kmalloc(skc
->skc_name_size
, lflags
);
693 if (skc
->skc_name
== NULL
) {
697 strlcpy(skc
->skc_name
, name
, skc
->skc_name_size
);
699 skc
->skc_ctor
= ctor
;
700 skc
->skc_dtor
= dtor
;
701 skc
->skc_private
= priv
;
703 skc
->skc_linux_cache
= NULL
;
704 skc
->skc_flags
= flags
;
705 skc
->skc_obj_size
= size
;
706 skc
->skc_obj_align
= SPL_KMEM_CACHE_ALIGN
;
707 atomic_set(&skc
->skc_ref
, 0);
709 INIT_LIST_HEAD(&skc
->skc_list
);
710 INIT_LIST_HEAD(&skc
->skc_complete_list
);
711 INIT_LIST_HEAD(&skc
->skc_partial_list
);
712 skc
->skc_emergency_tree
= RB_ROOT
;
713 spin_lock_init(&skc
->skc_lock
);
714 init_waitqueue_head(&skc
->skc_waitq
);
715 skc
->skc_slab_fail
= 0;
716 skc
->skc_slab_create
= 0;
717 skc
->skc_slab_destroy
= 0;
718 skc
->skc_slab_total
= 0;
719 skc
->skc_slab_alloc
= 0;
720 skc
->skc_slab_max
= 0;
721 skc
->skc_obj_total
= 0;
722 skc
->skc_obj_alloc
= 0;
723 skc
->skc_obj_max
= 0;
724 skc
->skc_obj_deadlock
= 0;
725 skc
->skc_obj_emergency
= 0;
726 skc
->skc_obj_emergency_max
= 0;
728 rc
= percpu_counter_init(&skc
->skc_linux_alloc
, 0, GFP_KERNEL
);
735 * Verify the requested alignment restriction is sane.
739 VERIFY3U(align
, >=, SPL_KMEM_CACHE_ALIGN
);
740 VERIFY3U(align
, <=, PAGE_SIZE
);
741 skc
->skc_obj_align
= align
;
745 * When no specific type of slab is requested (kmem, vmem, or
746 * linuxslab) then select a cache type based on the object size
747 * and default tunables.
749 if (!(skc
->skc_flags
& (KMC_SLAB
| KMC_KVMEM
))) {
750 if (spl_kmem_cache_slab_limit
&&
751 size
<= (size_t)spl_kmem_cache_slab_limit
) {
753 * Objects smaller than spl_kmem_cache_slab_limit can
754 * use the Linux slab for better space-efficiency.
756 skc
->skc_flags
|= KMC_SLAB
;
759 * All other objects are considered large and are
760 * placed on kvmem backed slabs.
762 skc
->skc_flags
|= KMC_KVMEM
;
767 * Given the type of slab allocate the required resources.
769 if (skc
->skc_flags
& KMC_KVMEM
) {
770 rc
= spl_slab_size(skc
,
771 &skc
->skc_slab_objs
, &skc
->skc_slab_size
);
775 rc
= spl_magazine_create(skc
);
779 unsigned long slabflags
= 0;
781 if (size
> spl_kmem_cache_slab_limit
)
784 if (skc
->skc_flags
& KMC_RECLAIMABLE
)
785 slabflags
|= SLAB_RECLAIM_ACCOUNT
;
787 skc
->skc_linux_cache
= kmem_cache_create_usercopy(
788 skc
->skc_name
, size
, align
, slabflags
, 0, size
, NULL
);
789 if (skc
->skc_linux_cache
== NULL
)
793 down_write(&spl_kmem_cache_sem
);
794 list_add_tail(&skc
->skc_list
, &spl_kmem_cache_list
);
795 up_write(&spl_kmem_cache_sem
);
799 kfree(skc
->skc_name
);
800 percpu_counter_destroy(&skc
->skc_linux_alloc
);
804 EXPORT_SYMBOL(spl_kmem_cache_create
);
807 * Register a move callback for cache defragmentation.
808 * XXX: Unimplemented but harmless to stub out for now.
811 spl_kmem_cache_set_move(spl_kmem_cache_t
*skc
,
812 kmem_cbrc_t (move
)(void *, void *, size_t, void *))
814 ASSERT(move
!= NULL
);
816 EXPORT_SYMBOL(spl_kmem_cache_set_move
);
819 * Destroy a cache and all objects associated with the cache.
822 spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
)
824 DECLARE_WAIT_QUEUE_HEAD(wq
);
827 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
828 ASSERT(skc
->skc_flags
& (KMC_KVMEM
| KMC_SLAB
));
830 down_write(&spl_kmem_cache_sem
);
831 list_del_init(&skc
->skc_list
);
832 up_write(&spl_kmem_cache_sem
);
834 /* Cancel any and wait for any pending delayed tasks */
835 VERIFY(!test_and_set_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
837 spin_lock(&skc
->skc_lock
);
838 id
= skc
->skc_taskqid
;
839 spin_unlock(&skc
->skc_lock
);
841 taskq_cancel_id(spl_kmem_cache_taskq
, id
);
844 * Wait until all current callers complete, this is mainly
845 * to catch the case where a low memory situation triggers a
846 * cache reaping action which races with this destroy.
848 wait_event(wq
, atomic_read(&skc
->skc_ref
) == 0);
850 if (skc
->skc_flags
& KMC_KVMEM
) {
851 spl_magazine_destroy(skc
);
852 spl_slab_reclaim(skc
);
854 ASSERT(skc
->skc_flags
& KMC_SLAB
);
855 kmem_cache_destroy(skc
->skc_linux_cache
);
858 spin_lock(&skc
->skc_lock
);
861 * Validate there are no objects in use and free all the
862 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers.
864 ASSERT3U(skc
->skc_slab_alloc
, ==, 0);
865 ASSERT3U(skc
->skc_obj_alloc
, ==, 0);
866 ASSERT3U(skc
->skc_slab_total
, ==, 0);
867 ASSERT3U(skc
->skc_obj_total
, ==, 0);
868 ASSERT3U(skc
->skc_obj_emergency
, ==, 0);
869 ASSERT(list_empty(&skc
->skc_complete_list
));
871 ASSERT3U(percpu_counter_sum(&skc
->skc_linux_alloc
), ==, 0);
872 percpu_counter_destroy(&skc
->skc_linux_alloc
);
874 spin_unlock(&skc
->skc_lock
);
876 kfree(skc
->skc_name
);
879 EXPORT_SYMBOL(spl_kmem_cache_destroy
);
882 * Allocate an object from a slab attached to the cache. This is used to
883 * repopulate the per-cpu magazine caches in batches when they run low.
886 spl_cache_obj(spl_kmem_cache_t
*skc
, spl_kmem_slab_t
*sks
)
890 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
891 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
893 sko
= list_entry(sks
->sks_free_list
.next
, spl_kmem_obj_t
, sko_list
);
894 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
895 ASSERT(sko
->sko_addr
!= NULL
);
897 /* Remove from sks_free_list */
898 list_del_init(&sko
->sko_list
);
900 sks
->sks_age
= jiffies
;
902 skc
->skc_obj_alloc
++;
904 /* Track max obj usage statistics */
905 if (skc
->skc_obj_alloc
> skc
->skc_obj_max
)
906 skc
->skc_obj_max
= skc
->skc_obj_alloc
;
908 /* Track max slab usage statistics */
909 if (sks
->sks_ref
== 1) {
910 skc
->skc_slab_alloc
++;
912 if (skc
->skc_slab_alloc
> skc
->skc_slab_max
)
913 skc
->skc_slab_max
= skc
->skc_slab_alloc
;
916 return (sko
->sko_addr
);
920 * Generic slab allocation function to run by the global work queues.
921 * It is responsible for allocating a new slab, linking it in to the list
922 * of partial slabs, and then waking any waiters.
925 __spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
)
927 spl_kmem_slab_t
*sks
;
929 fstrans_cookie_t cookie
= spl_fstrans_mark();
930 sks
= spl_slab_alloc(skc
, flags
);
931 spl_fstrans_unmark(cookie
);
933 spin_lock(&skc
->skc_lock
);
935 skc
->skc_slab_total
++;
936 skc
->skc_obj_total
+= sks
->sks_objs
;
937 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
939 smp_mb__before_atomic();
940 clear_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
);
941 smp_mb__after_atomic();
943 spin_unlock(&skc
->skc_lock
);
945 return (sks
== NULL
? -ENOMEM
: 0);
949 spl_cache_grow_work(void *data
)
951 spl_kmem_alloc_t
*ska
= (spl_kmem_alloc_t
*)data
;
952 spl_kmem_cache_t
*skc
= ska
->ska_cache
;
954 int error
= __spl_cache_grow(skc
, ska
->ska_flags
);
956 atomic_dec(&skc
->skc_ref
);
957 smp_mb__before_atomic();
958 clear_bit(KMC_BIT_GROWING
, &skc
->skc_flags
);
959 smp_mb__after_atomic();
961 wake_up_all(&skc
->skc_waitq
);
967 * Returns non-zero when a new slab should be available.
970 spl_cache_grow_wait(spl_kmem_cache_t
*skc
)
972 return (!test_bit(KMC_BIT_GROWING
, &skc
->skc_flags
));
976 * No available objects on any slabs, create a new slab. Note that this
977 * functionality is disabled for KMC_SLAB caches which are backed by the
981 spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
, void **obj
)
983 int remaining
, rc
= 0;
985 ASSERT0(flags
& ~KM_PUBLIC_MASK
);
986 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
987 ASSERT((skc
->skc_flags
& KMC_SLAB
) == 0);
992 * Since we can't sleep attempt an emergency allocation to satisfy
993 * the request. The only alterative is to fail the allocation but
994 * it's preferable try. The use of KM_NOSLEEP is expected to be rare.
996 if (flags
& KM_NOSLEEP
)
997 return (spl_emergency_alloc(skc
, flags
, obj
));
1002 * Before allocating a new slab wait for any reaping to complete and
1003 * then return so the local magazine can be rechecked for new objects.
1005 if (test_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1006 rc
= wait_on_bit(&skc
->skc_flags
, KMC_BIT_REAPING
,
1007 TASK_UNINTERRUPTIBLE
);
1008 return (rc
? rc
: -EAGAIN
);
1012 * Note: It would be nice to reduce the overhead of context switch
1013 * and improve NUMA locality, by trying to allocate a new slab in the
1014 * current process context with KM_NOSLEEP flag.
1016 * However, this can't be applied to vmem/kvmem due to a bug that
1017 * spl_vmalloc() doesn't honor gfp flags in page table allocation.
1021 * This is handled by dispatching a work request to the global work
1022 * queue. This allows us to asynchronously allocate a new slab while
1023 * retaining the ability to safely fall back to a smaller synchronous
1024 * allocations to ensure forward progress is always maintained.
1026 if (test_and_set_bit(KMC_BIT_GROWING
, &skc
->skc_flags
) == 0) {
1027 spl_kmem_alloc_t
*ska
;
1029 ska
= kmalloc(sizeof (*ska
), kmem_flags_convert(flags
));
1031 clear_bit_unlock(KMC_BIT_GROWING
, &skc
->skc_flags
);
1032 smp_mb__after_atomic();
1033 wake_up_all(&skc
->skc_waitq
);
1037 atomic_inc(&skc
->skc_ref
);
1038 ska
->ska_cache
= skc
;
1039 ska
->ska_flags
= flags
;
1040 taskq_init_ent(&ska
->ska_tqe
);
1041 taskq_dispatch_ent(spl_kmem_cache_taskq
,
1042 spl_cache_grow_work
, ska
, 0, &ska
->ska_tqe
);
1046 * The goal here is to only detect the rare case where a virtual slab
1047 * allocation has deadlocked. We must be careful to minimize the use
1048 * of emergency objects which are more expensive to track. Therefore,
1049 * we set a very long timeout for the asynchronous allocation and if
1050 * the timeout is reached the cache is flagged as deadlocked. From
1051 * this point only new emergency objects will be allocated until the
1052 * asynchronous allocation completes and clears the deadlocked flag.
1054 if (test_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
)) {
1055 rc
= spl_emergency_alloc(skc
, flags
, obj
);
1057 remaining
= wait_event_timeout(skc
->skc_waitq
,
1058 spl_cache_grow_wait(skc
), HZ
/ 10);
1061 spin_lock(&skc
->skc_lock
);
1062 if (test_bit(KMC_BIT_GROWING
, &skc
->skc_flags
)) {
1063 set_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
);
1064 skc
->skc_obj_deadlock
++;
1066 spin_unlock(&skc
->skc_lock
);
1076 * Refill a per-cpu magazine with objects from the slabs for this cache.
1077 * Ideally the magazine can be repopulated using existing objects which have
1078 * been released, however if we are unable to locate enough free objects new
1079 * slabs of objects will be created. On success NULL is returned, otherwise
1080 * the address of a single emergency object is returned for use by the caller.
1083 spl_cache_refill(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flags
)
1085 spl_kmem_slab_t
*sks
;
1086 int count
= 0, rc
, refill
;
1089 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1090 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1092 refill
= MIN(skm
->skm_refill
, skm
->skm_size
- skm
->skm_avail
);
1093 spin_lock(&skc
->skc_lock
);
1095 while (refill
> 0) {
1096 /* No slabs available we may need to grow the cache */
1097 if (list_empty(&skc
->skc_partial_list
)) {
1098 spin_unlock(&skc
->skc_lock
);
1101 rc
= spl_cache_grow(skc
, flags
, &obj
);
1102 local_irq_disable();
1104 /* Emergency object for immediate use by caller */
1105 if (rc
== 0 && obj
!= NULL
)
1111 /* Rescheduled to different CPU skm is not local */
1112 if (skm
!= skc
->skc_mag
[smp_processor_id()])
1116 * Potentially rescheduled to the same CPU but
1117 * allocations may have occurred from this CPU while
1118 * we were sleeping so recalculate max refill.
1120 refill
= MIN(refill
, skm
->skm_size
- skm
->skm_avail
);
1122 spin_lock(&skc
->skc_lock
);
1126 /* Grab the next available slab */
1127 sks
= list_entry((&skc
->skc_partial_list
)->next
,
1128 spl_kmem_slab_t
, sks_list
);
1129 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1130 ASSERT(sks
->sks_ref
< sks
->sks_objs
);
1131 ASSERT(!list_empty(&sks
->sks_free_list
));
1134 * Consume as many objects as needed to refill the requested
1135 * cache. We must also be careful not to overfill it.
1137 while (sks
->sks_ref
< sks
->sks_objs
&& refill
-- > 0 &&
1139 ASSERT(skm
->skm_avail
< skm
->skm_size
);
1140 ASSERT(count
< skm
->skm_size
);
1141 skm
->skm_objs
[skm
->skm_avail
++] =
1142 spl_cache_obj(skc
, sks
);
1145 /* Move slab to skc_complete_list when full */
1146 if (sks
->sks_ref
== sks
->sks_objs
) {
1147 list_del(&sks
->sks_list
);
1148 list_add(&sks
->sks_list
, &skc
->skc_complete_list
);
1152 spin_unlock(&skc
->skc_lock
);
1158 * Release an object back to the slab from which it came.
1161 spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
)
1163 spl_kmem_slab_t
*sks
= NULL
;
1164 spl_kmem_obj_t
*sko
= NULL
;
1166 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1168 sko
= spl_sko_from_obj(skc
, obj
);
1169 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1170 sks
= sko
->sko_slab
;
1171 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1172 ASSERT(sks
->sks_cache
== skc
);
1173 list_add(&sko
->sko_list
, &sks
->sks_free_list
);
1175 sks
->sks_age
= jiffies
;
1177 skc
->skc_obj_alloc
--;
1180 * Move slab to skc_partial_list when no longer full. Slabs
1181 * are added to the head to keep the partial list is quasi-full
1182 * sorted order. Fuller at the head, emptier at the tail.
1184 if (sks
->sks_ref
== (sks
->sks_objs
- 1)) {
1185 list_del(&sks
->sks_list
);
1186 list_add(&sks
->sks_list
, &skc
->skc_partial_list
);
1190 * Move empty slabs to the end of the partial list so
1191 * they can be easily found and freed during reclamation.
1193 if (sks
->sks_ref
== 0) {
1194 list_del(&sks
->sks_list
);
1195 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1196 skc
->skc_slab_alloc
--;
1201 * Allocate an object from the per-cpu magazine, or if the magazine
1202 * is empty directly allocate from a slab and repopulate the magazine.
1205 spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
)
1207 spl_kmem_magazine_t
*skm
;
1210 ASSERT0(flags
& ~KM_PUBLIC_MASK
);
1211 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1212 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1215 * Allocate directly from a Linux slab. All optimizations are left
1216 * to the underlying cache we only need to guarantee that KM_SLEEP
1217 * callers will never fail.
1219 if (skc
->skc_flags
& KMC_SLAB
) {
1220 struct kmem_cache
*slc
= skc
->skc_linux_cache
;
1222 obj
= kmem_cache_alloc(slc
, kmem_flags_convert(flags
));
1223 } while ((obj
== NULL
) && !(flags
& KM_NOSLEEP
));
1227 * Even though we leave everything up to the
1228 * underlying cache we still keep track of
1229 * how many objects we've allocated in it for
1230 * better debuggability.
1232 percpu_counter_inc(&skc
->skc_linux_alloc
);
1237 local_irq_disable();
1241 * Safe to update per-cpu structure without lock, but
1242 * in the restart case we must be careful to reacquire
1243 * the local magazine since this may have changed
1244 * when we need to grow the cache.
1246 skm
= skc
->skc_mag
[smp_processor_id()];
1247 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1249 if (likely(skm
->skm_avail
)) {
1250 /* Object available in CPU cache, use it */
1251 obj
= skm
->skm_objs
[--skm
->skm_avail
];
1253 obj
= spl_cache_refill(skc
, skm
, flags
);
1254 if ((obj
== NULL
) && !(flags
& KM_NOSLEEP
))
1263 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
1266 /* Pre-emptively migrate object to CPU L1 cache */
1268 if (obj
&& skc
->skc_ctor
)
1269 skc
->skc_ctor(obj
, skc
->skc_private
, flags
);
1276 EXPORT_SYMBOL(spl_kmem_cache_alloc
);
1279 * Free an object back to the local per-cpu magazine, there is no
1280 * guarantee that this is the same magazine the object was originally
1281 * allocated from. We may need to flush entire from the magazine
1282 * back to the slabs to make space.
1285 spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
)
1287 spl_kmem_magazine_t
*skm
;
1288 unsigned long flags
;
1290 int do_emergency
= 0;
1292 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1293 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1296 * Run the destructor
1299 skc
->skc_dtor(obj
, skc
->skc_private
);
1302 * Free the object from the Linux underlying Linux slab.
1304 if (skc
->skc_flags
& KMC_SLAB
) {
1305 kmem_cache_free(skc
->skc_linux_cache
, obj
);
1306 percpu_counter_dec(&skc
->skc_linux_alloc
);
1311 * While a cache has outstanding emergency objects all freed objects
1312 * must be checked. However, since emergency objects will never use
1313 * a virtual address these objects can be safely excluded as an
1316 if (!is_vmalloc_addr(obj
)) {
1317 spin_lock(&skc
->skc_lock
);
1318 do_emergency
= (skc
->skc_obj_emergency
> 0);
1319 spin_unlock(&skc
->skc_lock
);
1321 if (do_emergency
&& (spl_emergency_free(skc
, obj
) == 0))
1325 local_irq_save(flags
);
1328 * Safe to update per-cpu structure without lock, but
1329 * no remote memory allocation tracking is being performed
1330 * it is entirely possible to allocate an object from one
1331 * CPU cache and return it to another.
1333 skm
= skc
->skc_mag
[smp_processor_id()];
1334 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1337 * Per-CPU cache full, flush it to make space for this object,
1338 * this may result in an empty slab which can be reclaimed once
1339 * interrupts are re-enabled.
1341 if (unlikely(skm
->skm_avail
>= skm
->skm_size
)) {
1342 spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1346 /* Available space in cache, use it */
1347 skm
->skm_objs
[skm
->skm_avail
++] = obj
;
1349 local_irq_restore(flags
);
1352 spl_slab_reclaim(skc
);
1354 EXPORT_SYMBOL(spl_kmem_cache_free
);
1357 * Depending on how many and which objects are released it may simply
1358 * repopulate the local magazine which will then need to age-out. Objects
1359 * which cannot fit in the magazine will be released back to their slabs
1360 * which will also need to age out before being released. This is all just
1361 * best effort and we do not want to thrash creating and destroying slabs.
1364 spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
)
1366 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1367 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1369 if (skc
->skc_flags
& KMC_SLAB
)
1372 atomic_inc(&skc
->skc_ref
);
1375 * Prevent concurrent cache reaping when contended.
1377 if (test_and_set_bit(KMC_BIT_REAPING
, &skc
->skc_flags
))
1380 /* Reclaim from the magazine and free all now empty slabs. */
1381 unsigned long irq_flags
;
1382 local_irq_save(irq_flags
);
1383 spl_kmem_magazine_t
*skm
= skc
->skc_mag
[smp_processor_id()];
1384 spl_cache_flush(skc
, skm
, skm
->skm_avail
);
1385 local_irq_restore(irq_flags
);
1387 spl_slab_reclaim(skc
);
1388 clear_bit_unlock(KMC_BIT_REAPING
, &skc
->skc_flags
);
1389 smp_mb__after_atomic();
1390 wake_up_bit(&skc
->skc_flags
, KMC_BIT_REAPING
);
1392 atomic_dec(&skc
->skc_ref
);
1394 EXPORT_SYMBOL(spl_kmem_cache_reap_now
);
1397 * This is stubbed out for code consistency with other platforms. There
1398 * is existing logic to prevent concurrent reaping so while this is ugly
1399 * it should do no harm.
1402 spl_kmem_cache_reap_active(void)
1406 EXPORT_SYMBOL(spl_kmem_cache_reap_active
);
1409 * Reap all free slabs from all registered caches.
1414 spl_kmem_cache_t
*skc
= NULL
;
1416 down_read(&spl_kmem_cache_sem
);
1417 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
1418 spl_kmem_cache_reap_now(skc
);
1420 up_read(&spl_kmem_cache_sem
);
1422 EXPORT_SYMBOL(spl_kmem_reap
);
1425 spl_kmem_cache_init(void)
1427 init_rwsem(&spl_kmem_cache_sem
);
1428 INIT_LIST_HEAD(&spl_kmem_cache_list
);
1429 spl_kmem_cache_taskq
= taskq_create("spl_kmem_cache",
1430 spl_kmem_cache_kmem_threads
, maxclsyspri
,
1431 spl_kmem_cache_kmem_threads
* 8, INT_MAX
,
1432 TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
1434 if (spl_kmem_cache_taskq
== NULL
)
1441 spl_kmem_cache_fini(void)
1443 taskq_destroy(spl_kmem_cache_taskq
);