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.
9 * For details, see <http://zfsonlinux.org/>.
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/kmem_cache.h>
27 #include <sys/taskq.h>
28 #include <sys/timer.h>
30 #include <linux/slab.h>
31 #include <linux/swap.h>
32 #include <linux/mm_compat.h>
33 #include <linux/wait_compat.h>
36 * Within the scope of spl-kmem.c file the kmem_cache_* definitions
37 * are removed to allow access to the real Linux slab allocator.
39 #undef kmem_cache_destroy
40 #undef kmem_cache_create
41 #undef kmem_cache_alloc
42 #undef kmem_cache_free
46 * Linux 3.16 replaced smp_mb__{before,after}_{atomic,clear}_{dec,inc,bit}()
47 * with smp_mb__{before,after}_atomic() because they were redundant. This is
48 * only used inside our SLAB allocator, so we implement an internal wrapper
49 * here to give us smp_mb__{before,after}_atomic() on older kernels.
51 #ifndef smp_mb__before_atomic
52 #define smp_mb__before_atomic(x) smp_mb__before_clear_bit(x)
55 #ifndef smp_mb__after_atomic
56 #define smp_mb__after_atomic(x) smp_mb__after_clear_bit(x)
60 * Cache expiration was implemented because it was part of the default Solaris
61 * kmem_cache behavior. The idea is that per-cpu objects which haven't been
62 * accessed in several seconds should be returned to the cache. On the other
63 * hand Linux slabs never move objects back to the slabs unless there is
64 * memory pressure on the system. By default the Linux method is enabled
65 * because it has been shown to improve responsiveness on low memory systems.
66 * This policy may be changed by setting KMC_EXPIRE_AGE or KMC_EXPIRE_MEM.
68 unsigned int spl_kmem_cache_expire
= KMC_EXPIRE_MEM
;
69 EXPORT_SYMBOL(spl_kmem_cache_expire
);
70 module_param(spl_kmem_cache_expire
, uint
, 0644);
71 MODULE_PARM_DESC(spl_kmem_cache_expire
, "By age (0x1) or low memory (0x2)");
74 * Cache magazines are an optimization designed to minimize the cost of
75 * allocating memory. They do this by keeping a per-cpu cache of recently
76 * freed objects, which can then be reallocated without taking a lock. This
77 * can improve performance on highly contended caches. However, because
78 * objects in magazines will prevent otherwise empty slabs from being
79 * immediately released this may not be ideal for low memory machines.
81 * For this reason spl_kmem_cache_magazine_size can be used to set a maximum
82 * magazine size. When this value is set to 0 the magazine size will be
83 * automatically determined based on the object size. Otherwise magazines
84 * will be limited to 2-256 objects per magazine (i.e per cpu). Magazines
85 * may never be entirely disabled in this implementation.
87 unsigned int spl_kmem_cache_magazine_size
= 0;
88 module_param(spl_kmem_cache_magazine_size
, uint
, 0444);
89 MODULE_PARM_DESC(spl_kmem_cache_magazine_size
,
90 "Default magazine size (2-256), set automatically (0)\n");
93 * The default behavior is to report the number of objects remaining in the
94 * cache. This allows the Linux VM to repeatedly reclaim objects from the
95 * cache when memory is low satisfy other memory allocations. Alternately,
96 * setting this value to KMC_RECLAIM_ONCE limits how aggressively the cache
97 * is reclaimed. This may increase the likelihood of out of memory events.
99 unsigned int spl_kmem_cache_reclaim
= 0 /* KMC_RECLAIM_ONCE */;
100 module_param(spl_kmem_cache_reclaim
, uint
, 0644);
101 MODULE_PARM_DESC(spl_kmem_cache_reclaim
, "Single reclaim pass (0x1)");
103 unsigned int spl_kmem_cache_obj_per_slab
= SPL_KMEM_CACHE_OBJ_PER_SLAB
;
104 module_param(spl_kmem_cache_obj_per_slab
, uint
, 0644);
105 MODULE_PARM_DESC(spl_kmem_cache_obj_per_slab
, "Number of objects per slab");
107 unsigned int spl_kmem_cache_obj_per_slab_min
= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN
;
108 module_param(spl_kmem_cache_obj_per_slab_min
, uint
, 0644);
109 MODULE_PARM_DESC(spl_kmem_cache_obj_per_slab_min
,
110 "Minimal number of objects per slab");
112 unsigned int spl_kmem_cache_max_size
= SPL_KMEM_CACHE_MAX_SIZE
;
113 module_param(spl_kmem_cache_max_size
, uint
, 0644);
114 MODULE_PARM_DESC(spl_kmem_cache_max_size
, "Maximum size of slab in MB");
117 * For small objects the Linux slab allocator should be used to make the most
118 * efficient use of the memory. However, large objects are not supported by
119 * the Linux slab and therefore the SPL implementation is preferred. A cutoff
120 * of 16K was determined to be optimal for architectures using 4K pages.
122 #if PAGE_SIZE == 4096
123 unsigned int spl_kmem_cache_slab_limit
= 16384;
125 unsigned int spl_kmem_cache_slab_limit
= 0;
127 module_param(spl_kmem_cache_slab_limit
, uint
, 0644);
128 MODULE_PARM_DESC(spl_kmem_cache_slab_limit
,
129 "Objects less than N bytes use the Linux slab");
132 * This value defaults to a threshold designed to avoid allocations which
133 * have been deemed costly by the kernel.
135 unsigned int spl_kmem_cache_kmem_limit
=
136 ((1 << (PAGE_ALLOC_COSTLY_ORDER
- 1)) * PAGE_SIZE
) /
137 SPL_KMEM_CACHE_OBJ_PER_SLAB
;
138 module_param(spl_kmem_cache_kmem_limit
, uint
, 0644);
139 MODULE_PARM_DESC(spl_kmem_cache_kmem_limit
,
140 "Objects less than N bytes use the kmalloc");
143 * The number of threads available to allocate new slabs for caches. This
144 * should not need to be tuned but it is available for performance analysis.
146 unsigned int spl_kmem_cache_kmem_threads
= 4;
147 module_param(spl_kmem_cache_kmem_threads
, uint
, 0444);
148 MODULE_PARM_DESC(spl_kmem_cache_kmem_threads
,
149 "Number of spl_kmem_cache threads");
152 * Slab allocation interfaces
154 * While the Linux slab implementation was inspired by the Solaris
155 * implementation I cannot use it to emulate the Solaris APIs. I
156 * require two features which are not provided by the Linux slab.
158 * 1) Constructors AND destructors. Recent versions of the Linux
159 * kernel have removed support for destructors. This is a deal
160 * breaker for the SPL which contains particularly expensive
161 * initializers for mutex's, condition variables, etc. We also
162 * require a minimal level of cleanup for these data types unlike
163 * many Linux data types which do need to be explicitly destroyed.
165 * 2) Virtual address space backed slab. Callers of the Solaris slab
166 * expect it to work well for both small are very large allocations.
167 * Because of memory fragmentation the Linux slab which is backed
168 * by kmalloc'ed memory performs very badly when confronted with
169 * large numbers of large allocations. Basing the slab on the
170 * virtual address space removes the need for contiguous pages
171 * and greatly improve performance for large allocations.
173 * For these reasons, the SPL has its own slab implementation with
174 * the needed features. It is not as highly optimized as either the
175 * Solaris or Linux slabs, but it should get me most of what is
176 * needed until it can be optimized or obsoleted by another approach.
178 * One serious concern I do have about this method is the relatively
179 * small virtual address space on 32bit arches. This will seriously
180 * constrain the size of the slab caches and their performance.
183 struct list_head spl_kmem_cache_list
; /* List of caches */
184 struct rw_semaphore spl_kmem_cache_sem
; /* Cache list lock */
185 taskq_t
*spl_kmem_cache_taskq
; /* Task queue for ageing / reclaim */
187 static void spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
);
189 SPL_SHRINKER_CALLBACK_FWD_DECLARE(spl_kmem_cache_generic_shrinker
);
190 SPL_SHRINKER_DECLARE(spl_kmem_cache_shrinker
,
191 spl_kmem_cache_generic_shrinker
, KMC_DEFAULT_SEEKS
);
194 kv_alloc(spl_kmem_cache_t
*skc
, int size
, int flags
)
196 gfp_t lflags
= kmem_flags_convert(flags
);
199 if (skc
->skc_flags
& KMC_KMEM
) {
201 ptr
= (void *)__get_free_pages(lflags
, get_order(size
));
203 ptr
= spl_vmalloc(size
, lflags
| __GFP_HIGHMEM
, PAGE_KERNEL
);
206 /* Resulting allocated memory will be page aligned */
207 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
213 kv_free(spl_kmem_cache_t
*skc
, void *ptr
, int size
)
215 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
218 * The Linux direct reclaim path uses this out of band value to
219 * determine if forward progress is being made. Normally this is
220 * incremented by kmem_freepages() which is part of the various
221 * Linux slab implementations. However, since we are using none
222 * of that infrastructure we are responsible for incrementing it.
224 if (current
->reclaim_state
)
225 current
->reclaim_state
->reclaimed_slab
+= size
>> PAGE_SHIFT
;
227 if (skc
->skc_flags
& KMC_KMEM
) {
229 free_pages((unsigned long)ptr
, get_order(size
));
236 * Required space for each aligned sks.
238 static inline uint32_t
239 spl_sks_size(spl_kmem_cache_t
*skc
)
241 return (P2ROUNDUP_TYPED(sizeof (spl_kmem_slab_t
),
242 skc
->skc_obj_align
, uint32_t));
246 * Required space for each aligned object.
248 static inline uint32_t
249 spl_obj_size(spl_kmem_cache_t
*skc
)
251 uint32_t align
= skc
->skc_obj_align
;
253 return (P2ROUNDUP_TYPED(skc
->skc_obj_size
, align
, uint32_t) +
254 P2ROUNDUP_TYPED(sizeof (spl_kmem_obj_t
), align
, uint32_t));
258 * Lookup the spl_kmem_object_t for an object given that object.
260 static inline spl_kmem_obj_t
*
261 spl_sko_from_obj(spl_kmem_cache_t
*skc
, void *obj
)
263 return (obj
+ P2ROUNDUP_TYPED(skc
->skc_obj_size
,
264 skc
->skc_obj_align
, uint32_t));
268 * Required space for each offslab object taking in to account alignment
269 * restrictions and the power-of-two requirement of kv_alloc().
271 static inline uint32_t
272 spl_offslab_size(spl_kmem_cache_t
*skc
)
274 return (1UL << (fls64(spl_obj_size(skc
)) + 1));
278 * It's important that we pack the spl_kmem_obj_t structure and the
279 * actual objects in to one large address space to minimize the number
280 * of calls to the allocator. It is far better to do a few large
281 * allocations and then subdivide it ourselves. Now which allocator
282 * we use requires balancing a few trade offs.
284 * For small objects we use kmem_alloc() because as long as you are
285 * only requesting a small number of pages (ideally just one) its cheap.
286 * However, when you start requesting multiple pages with kmem_alloc()
287 * it gets increasingly expensive since it requires contiguous pages.
288 * For this reason we shift to vmem_alloc() for slabs of large objects
289 * which removes the need for contiguous pages. We do not use
290 * vmem_alloc() in all cases because there is significant locking
291 * overhead in __get_vm_area_node(). This function takes a single
292 * global lock when acquiring an available virtual address range which
293 * serializes all vmem_alloc()'s for all slab caches. Using slightly
294 * different allocation functions for small and large objects should
295 * give us the best of both worlds.
297 * KMC_ONSLAB KMC_OFFSLAB
299 * +------------------------+ +-----------------+
300 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
301 * | skc_obj_size <-+ | | +-----------------+ | |
302 * | spl_kmem_obj_t | | | |
303 * | skc_obj_size <---+ | +-----------------+ | |
304 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
305 * | ... v | | spl_kmem_obj_t | |
306 * +------------------------+ +-----------------+ v
308 static spl_kmem_slab_t
*
309 spl_slab_alloc(spl_kmem_cache_t
*skc
, int flags
)
311 spl_kmem_slab_t
*sks
;
312 spl_kmem_obj_t
*sko
, *n
;
314 uint32_t obj_size
, offslab_size
= 0;
317 base
= kv_alloc(skc
, skc
->skc_slab_size
, flags
);
321 sks
= (spl_kmem_slab_t
*)base
;
322 sks
->sks_magic
= SKS_MAGIC
;
323 sks
->sks_objs
= skc
->skc_slab_objs
;
324 sks
->sks_age
= jiffies
;
325 sks
->sks_cache
= skc
;
326 INIT_LIST_HEAD(&sks
->sks_list
);
327 INIT_LIST_HEAD(&sks
->sks_free_list
);
329 obj_size
= spl_obj_size(skc
);
331 if (skc
->skc_flags
& KMC_OFFSLAB
)
332 offslab_size
= spl_offslab_size(skc
);
334 for (i
= 0; i
< sks
->sks_objs
; i
++) {
335 if (skc
->skc_flags
& KMC_OFFSLAB
) {
336 obj
= kv_alloc(skc
, offslab_size
, flags
);
342 obj
= base
+ spl_sks_size(skc
) + (i
* obj_size
);
345 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
346 sko
= spl_sko_from_obj(skc
, obj
);
348 sko
->sko_magic
= SKO_MAGIC
;
350 INIT_LIST_HEAD(&sko
->sko_list
);
351 list_add_tail(&sko
->sko_list
, &sks
->sks_free_list
);
356 if (skc
->skc_flags
& KMC_OFFSLAB
)
357 list_for_each_entry_safe(sko
,
358 n
, &sks
->sks_free_list
, sko_list
)
359 kv_free(skc
, sko
->sko_addr
, offslab_size
);
361 kv_free(skc
, base
, skc
->skc_slab_size
);
369 * Remove a slab from complete or partial list, it must be called with
370 * the 'skc->skc_lock' held but the actual free must be performed
371 * outside the lock to prevent deadlocking on vmem addresses.
374 spl_slab_free(spl_kmem_slab_t
*sks
,
375 struct list_head
*sks_list
, struct list_head
*sko_list
)
377 spl_kmem_cache_t
*skc
;
379 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
380 ASSERT(sks
->sks_ref
== 0);
382 skc
= sks
->sks_cache
;
383 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
384 ASSERT(spin_is_locked(&skc
->skc_lock
));
387 * Update slab/objects counters in the cache, then remove the
388 * slab from the skc->skc_partial_list. Finally add the slab
389 * and all its objects in to the private work lists where the
390 * destructors will be called and the memory freed to the system.
392 skc
->skc_obj_total
-= sks
->sks_objs
;
393 skc
->skc_slab_total
--;
394 list_del(&sks
->sks_list
);
395 list_add(&sks
->sks_list
, sks_list
);
396 list_splice_init(&sks
->sks_free_list
, sko_list
);
400 * Reclaim empty slabs at the end of the partial list.
403 spl_slab_reclaim(spl_kmem_cache_t
*skc
)
405 spl_kmem_slab_t
*sks
, *m
;
406 spl_kmem_obj_t
*sko
, *n
;
412 * Empty slabs and objects must be moved to a private list so they
413 * can be safely freed outside the spin lock. All empty slabs are
414 * at the end of skc->skc_partial_list, therefore once a non-empty
415 * slab is found we can stop scanning.
417 spin_lock(&skc
->skc_lock
);
418 list_for_each_entry_safe_reverse(sks
, m
,
419 &skc
->skc_partial_list
, sks_list
) {
421 if (sks
->sks_ref
> 0)
424 spl_slab_free(sks
, &sks_list
, &sko_list
);
426 spin_unlock(&skc
->skc_lock
);
429 * The following two loops ensure all the object destructors are
430 * run, any offslab objects are freed, and the slabs themselves
431 * are freed. This is all done outside the skc->skc_lock since
432 * this allows the destructor to sleep, and allows us to perform
433 * a conditional reschedule when a freeing a large number of
434 * objects and slabs back to the system.
436 if (skc
->skc_flags
& KMC_OFFSLAB
)
437 size
= spl_offslab_size(skc
);
439 list_for_each_entry_safe(sko
, n
, &sko_list
, sko_list
) {
440 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
442 if (skc
->skc_flags
& KMC_OFFSLAB
)
443 kv_free(skc
, sko
->sko_addr
, size
);
446 list_for_each_entry_safe(sks
, m
, &sks_list
, sks_list
) {
447 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
448 kv_free(skc
, sks
, skc
->skc_slab_size
);
452 static spl_kmem_emergency_t
*
453 spl_emergency_search(struct rb_root
*root
, void *obj
)
455 struct rb_node
*node
= root
->rb_node
;
456 spl_kmem_emergency_t
*ske
;
457 unsigned long address
= (unsigned long)obj
;
460 ske
= container_of(node
, spl_kmem_emergency_t
, ske_node
);
462 if (address
< ske
->ske_obj
)
463 node
= node
->rb_left
;
464 else if (address
> ske
->ske_obj
)
465 node
= node
->rb_right
;
474 spl_emergency_insert(struct rb_root
*root
, spl_kmem_emergency_t
*ske
)
476 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
477 spl_kmem_emergency_t
*ske_tmp
;
478 unsigned long address
= ske
->ske_obj
;
481 ske_tmp
= container_of(*new, spl_kmem_emergency_t
, ske_node
);
484 if (address
< ske_tmp
->ske_obj
)
485 new = &((*new)->rb_left
);
486 else if (address
> ske_tmp
->ske_obj
)
487 new = &((*new)->rb_right
);
492 rb_link_node(&ske
->ske_node
, parent
, new);
493 rb_insert_color(&ske
->ske_node
, root
);
499 * Allocate a single emergency object and track it in a red black tree.
502 spl_emergency_alloc(spl_kmem_cache_t
*skc
, int flags
, void **obj
)
504 gfp_t lflags
= kmem_flags_convert(flags
);
505 spl_kmem_emergency_t
*ske
;
506 int order
= get_order(skc
->skc_obj_size
);
509 /* Last chance use a partial slab if one now exists */
510 spin_lock(&skc
->skc_lock
);
511 empty
= list_empty(&skc
->skc_partial_list
);
512 spin_unlock(&skc
->skc_lock
);
516 ske
= kmalloc(sizeof (*ske
), lflags
);
520 ske
->ske_obj
= __get_free_pages(lflags
, order
);
521 if (ske
->ske_obj
== 0) {
526 spin_lock(&skc
->skc_lock
);
527 empty
= spl_emergency_insert(&skc
->skc_emergency_tree
, ske
);
529 skc
->skc_obj_total
++;
530 skc
->skc_obj_emergency
++;
531 if (skc
->skc_obj_emergency
> skc
->skc_obj_emergency_max
)
532 skc
->skc_obj_emergency_max
= skc
->skc_obj_emergency
;
534 spin_unlock(&skc
->skc_lock
);
536 if (unlikely(!empty
)) {
537 free_pages(ske
->ske_obj
, order
);
542 *obj
= (void *)ske
->ske_obj
;
548 * Locate the passed object in the red black tree and free it.
551 spl_emergency_free(spl_kmem_cache_t
*skc
, void *obj
)
553 spl_kmem_emergency_t
*ske
;
554 int order
= get_order(skc
->skc_obj_size
);
556 spin_lock(&skc
->skc_lock
);
557 ske
= spl_emergency_search(&skc
->skc_emergency_tree
, obj
);
559 rb_erase(&ske
->ske_node
, &skc
->skc_emergency_tree
);
560 skc
->skc_obj_emergency
--;
561 skc
->skc_obj_total
--;
563 spin_unlock(&skc
->skc_lock
);
568 free_pages(ske
->ske_obj
, order
);
575 * Release objects from the per-cpu magazine back to their slab. The flush
576 * argument contains the max number of entries to remove from the magazine.
579 __spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
581 int i
, count
= MIN(flush
, skm
->skm_avail
);
583 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
584 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
585 ASSERT(spin_is_locked(&skc
->skc_lock
));
587 for (i
= 0; i
< count
; i
++)
588 spl_cache_shrink(skc
, skm
->skm_objs
[i
]);
590 skm
->skm_avail
-= count
;
591 memmove(skm
->skm_objs
, &(skm
->skm_objs
[count
]),
592 sizeof (void *) * skm
->skm_avail
);
596 spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
598 spin_lock(&skc
->skc_lock
);
599 __spl_cache_flush(skc
, skm
, flush
);
600 spin_unlock(&skc
->skc_lock
);
604 spl_magazine_age(void *data
)
606 spl_kmem_cache_t
*skc
= (spl_kmem_cache_t
*)data
;
607 spl_kmem_magazine_t
*skm
= skc
->skc_mag
[smp_processor_id()];
609 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
610 ASSERT(skm
->skm_cpu
== smp_processor_id());
611 ASSERT(irqs_disabled());
613 /* There are no available objects or they are too young to age out */
614 if ((skm
->skm_avail
== 0) ||
615 time_before(jiffies
, skm
->skm_age
+ skc
->skc_delay
* HZ
))
619 * Because we're executing in interrupt context we may have
620 * interrupted the holder of this lock. To avoid a potential
621 * deadlock return if the lock is contended.
623 if (!spin_trylock(&skc
->skc_lock
))
626 __spl_cache_flush(skc
, skm
, skm
->skm_refill
);
627 spin_unlock(&skc
->skc_lock
);
631 * Called regularly to keep a downward pressure on the cache.
633 * Objects older than skc->skc_delay seconds in the per-cpu magazines will
634 * be returned to the caches. This is done to prevent idle magazines from
635 * holding memory which could be better used elsewhere. The delay is
636 * present to prevent thrashing the magazine.
638 * The newly released objects may result in empty partial slabs. Those
639 * slabs should be released to the system. Otherwise moving the objects
640 * out of the magazines is just wasted work.
643 spl_cache_age(void *data
)
645 spl_kmem_cache_t
*skc
= (spl_kmem_cache_t
*)data
;
648 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
650 /* Dynamically disabled at run time */
651 if (!(spl_kmem_cache_expire
& KMC_EXPIRE_AGE
))
654 atomic_inc(&skc
->skc_ref
);
656 if (!(skc
->skc_flags
& KMC_NOMAGAZINE
))
657 on_each_cpu(spl_magazine_age
, skc
, 1);
659 spl_slab_reclaim(skc
);
661 while (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
) && !id
) {
662 id
= taskq_dispatch_delay(
663 spl_kmem_cache_taskq
, spl_cache_age
, skc
, TQ_SLEEP
,
664 ddi_get_lbolt() + skc
->skc_delay
/ 3 * HZ
);
666 /* Destroy issued after dispatch immediately cancel it */
667 if (test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
) && id
)
668 taskq_cancel_id(spl_kmem_cache_taskq
, id
);
671 spin_lock(&skc
->skc_lock
);
672 skc
->skc_taskqid
= id
;
673 spin_unlock(&skc
->skc_lock
);
675 atomic_dec(&skc
->skc_ref
);
679 * Size a slab based on the size of each aligned object plus spl_kmem_obj_t.
680 * When on-slab we want to target spl_kmem_cache_obj_per_slab. However,
681 * for very small objects we may end up with more than this so as not
682 * to waste space in the minimal allocation of a single page. Also for
683 * very large objects we may use as few as spl_kmem_cache_obj_per_slab_min,
684 * lower than this and we will fail.
687 spl_slab_size(spl_kmem_cache_t
*skc
, uint32_t *objs
, uint32_t *size
)
689 uint32_t sks_size
, obj_size
, max_size
, tgt_size
, tgt_objs
;
691 if (skc
->skc_flags
& KMC_OFFSLAB
) {
692 tgt_objs
= spl_kmem_cache_obj_per_slab
;
693 tgt_size
= P2ROUNDUP(sizeof (spl_kmem_slab_t
), PAGE_SIZE
);
695 if ((skc
->skc_flags
& KMC_KMEM
) &&
696 (spl_obj_size(skc
) > (SPL_MAX_ORDER_NR_PAGES
* PAGE_SIZE
)))
699 sks_size
= spl_sks_size(skc
);
700 obj_size
= spl_obj_size(skc
);
701 max_size
= (spl_kmem_cache_max_size
* 1024 * 1024);
702 tgt_size
= (spl_kmem_cache_obj_per_slab
* obj_size
+ sks_size
);
705 * KMC_KMEM slabs are allocated by __get_free_pages() which
706 * rounds up to the nearest order. Knowing this the size
707 * should be rounded up to the next power of two with a hard
708 * maximum defined by the maximum allowed allocation order.
710 if (skc
->skc_flags
& KMC_KMEM
) {
711 max_size
= SPL_MAX_ORDER_NR_PAGES
* PAGE_SIZE
;
712 tgt_size
= MIN(max_size
,
713 PAGE_SIZE
* (1 << MAX(get_order(tgt_size
) - 1, 1)));
716 if (tgt_size
<= max_size
) {
717 tgt_objs
= (tgt_size
- sks_size
) / obj_size
;
719 tgt_objs
= (max_size
- sks_size
) / obj_size
;
720 tgt_size
= (tgt_objs
* obj_size
) + sks_size
;
734 * Make a guess at reasonable per-cpu magazine size based on the size of
735 * each object and the cost of caching N of them in each magazine. Long
736 * term this should really adapt based on an observed usage heuristic.
739 spl_magazine_size(spl_kmem_cache_t
*skc
)
741 uint32_t obj_size
= spl_obj_size(skc
);
744 if (spl_kmem_cache_magazine_size
> 0)
745 return (MAX(MIN(spl_kmem_cache_magazine_size
, 256), 2));
747 /* Per-magazine sizes below assume a 4Kib page size */
748 if (obj_size
> (PAGE_SIZE
* 256))
749 size
= 4; /* Minimum 4Mib per-magazine */
750 else if (obj_size
> (PAGE_SIZE
* 32))
751 size
= 16; /* Minimum 2Mib per-magazine */
752 else if (obj_size
> (PAGE_SIZE
))
753 size
= 64; /* Minimum 256Kib per-magazine */
754 else if (obj_size
> (PAGE_SIZE
/ 4))
755 size
= 128; /* Minimum 128Kib per-magazine */
763 * Allocate a per-cpu magazine to associate with a specific core.
765 static spl_kmem_magazine_t
*
766 spl_magazine_alloc(spl_kmem_cache_t
*skc
, int cpu
)
768 spl_kmem_magazine_t
*skm
;
769 int size
= sizeof (spl_kmem_magazine_t
) +
770 sizeof (void *) * skc
->skc_mag_size
;
772 skm
= kmalloc_node(size
, GFP_KERNEL
, cpu_to_node(cpu
));
774 skm
->skm_magic
= SKM_MAGIC
;
776 skm
->skm_size
= skc
->skc_mag_size
;
777 skm
->skm_refill
= skc
->skc_mag_refill
;
778 skm
->skm_cache
= skc
;
779 skm
->skm_age
= jiffies
;
787 * Free a per-cpu magazine associated with a specific core.
790 spl_magazine_free(spl_kmem_magazine_t
*skm
)
792 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
793 ASSERT(skm
->skm_avail
== 0);
798 * Create all pre-cpu magazines of reasonable sizes.
801 spl_magazine_create(spl_kmem_cache_t
*skc
)
805 if (skc
->skc_flags
& KMC_NOMAGAZINE
)
808 skc
->skc_mag_size
= spl_magazine_size(skc
);
809 skc
->skc_mag_refill
= (skc
->skc_mag_size
+ 1) / 2;
811 for_each_online_cpu(i
) {
812 skc
->skc_mag
[i
] = spl_magazine_alloc(skc
, i
);
813 if (!skc
->skc_mag
[i
]) {
814 for (i
--; i
>= 0; i
--)
815 spl_magazine_free(skc
->skc_mag
[i
]);
825 * Destroy all pre-cpu magazines.
828 spl_magazine_destroy(spl_kmem_cache_t
*skc
)
830 spl_kmem_magazine_t
*skm
;
833 if (skc
->skc_flags
& KMC_NOMAGAZINE
)
836 for_each_online_cpu(i
) {
837 skm
= skc
->skc_mag
[i
];
838 spl_cache_flush(skc
, skm
, skm
->skm_avail
);
839 spl_magazine_free(skm
);
844 * Create a object cache based on the following arguments:
846 * size cache object size
847 * align cache object alignment
848 * ctor cache object constructor
849 * dtor cache object destructor
850 * reclaim cache object reclaim
851 * priv cache private data for ctor/dtor/reclaim
852 * vmp unused must be NULL
854 * KMC_NOTOUCH Disable cache object aging (unsupported)
855 * KMC_NODEBUG Disable debugging (unsupported)
856 * KMC_NOHASH Disable hashing (unsupported)
857 * KMC_QCACHE Disable qcache (unsupported)
858 * KMC_NOMAGAZINE Enabled for kmem/vmem, Disabled for Linux slab
859 * KMC_KMEM Force kmem backed cache
860 * KMC_VMEM Force vmem backed cache
861 * KMC_SLAB Force Linux slab backed cache
862 * KMC_OFFSLAB Locate objects off the slab
865 spl_kmem_cache_create(char *name
, size_t size
, size_t align
,
866 spl_kmem_ctor_t ctor
, spl_kmem_dtor_t dtor
, spl_kmem_reclaim_t reclaim
,
867 void *priv
, void *vmp
, int flags
)
869 gfp_t lflags
= kmem_flags_convert(KM_SLEEP
);
870 spl_kmem_cache_t
*skc
;
876 ASSERT0(flags
& KMC_NOMAGAZINE
);
877 ASSERT0(flags
& KMC_NOHASH
);
878 ASSERT0(flags
& KMC_QCACHE
);
884 * Allocate memory for a new cache and initialize it. Unfortunately,
885 * this usually ends up being a large allocation of ~32k because
886 * we need to allocate enough memory for the worst case number of
887 * cpus in the magazine, skc_mag[NR_CPUS].
889 skc
= kzalloc(sizeof (*skc
), lflags
);
893 skc
->skc_magic
= SKC_MAGIC
;
894 skc
->skc_name_size
= strlen(name
) + 1;
895 skc
->skc_name
= (char *)kmalloc(skc
->skc_name_size
, lflags
);
896 if (skc
->skc_name
== NULL
) {
900 strncpy(skc
->skc_name
, name
, skc
->skc_name_size
);
902 skc
->skc_ctor
= ctor
;
903 skc
->skc_dtor
= dtor
;
904 skc
->skc_reclaim
= reclaim
;
905 skc
->skc_private
= priv
;
907 skc
->skc_linux_cache
= NULL
;
908 skc
->skc_flags
= flags
;
909 skc
->skc_obj_size
= size
;
910 skc
->skc_obj_align
= SPL_KMEM_CACHE_ALIGN
;
911 skc
->skc_delay
= SPL_KMEM_CACHE_DELAY
;
912 skc
->skc_reap
= SPL_KMEM_CACHE_REAP
;
913 atomic_set(&skc
->skc_ref
, 0);
915 INIT_LIST_HEAD(&skc
->skc_list
);
916 INIT_LIST_HEAD(&skc
->skc_complete_list
);
917 INIT_LIST_HEAD(&skc
->skc_partial_list
);
918 skc
->skc_emergency_tree
= RB_ROOT
;
919 spin_lock_init(&skc
->skc_lock
);
920 init_waitqueue_head(&skc
->skc_waitq
);
921 skc
->skc_slab_fail
= 0;
922 skc
->skc_slab_create
= 0;
923 skc
->skc_slab_destroy
= 0;
924 skc
->skc_slab_total
= 0;
925 skc
->skc_slab_alloc
= 0;
926 skc
->skc_slab_max
= 0;
927 skc
->skc_obj_total
= 0;
928 skc
->skc_obj_alloc
= 0;
929 skc
->skc_obj_max
= 0;
930 skc
->skc_obj_deadlock
= 0;
931 skc
->skc_obj_emergency
= 0;
932 skc
->skc_obj_emergency_max
= 0;
935 * Verify the requested alignment restriction is sane.
939 VERIFY3U(align
, >=, SPL_KMEM_CACHE_ALIGN
);
940 VERIFY3U(align
, <=, PAGE_SIZE
);
941 skc
->skc_obj_align
= align
;
945 * When no specific type of slab is requested (kmem, vmem, or
946 * linuxslab) then select a cache type based on the object size
947 * and default tunables.
949 if (!(skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
| KMC_SLAB
))) {
952 * Objects smaller than spl_kmem_cache_slab_limit can
953 * use the Linux slab for better space-efficiency. By
954 * default this functionality is disabled until its
955 * performance characteristics are fully understood.
957 if (spl_kmem_cache_slab_limit
&&
958 size
<= (size_t)spl_kmem_cache_slab_limit
)
959 skc
->skc_flags
|= KMC_SLAB
;
962 * Small objects, less than spl_kmem_cache_kmem_limit per
963 * object should use kmem because their slabs are small.
965 else if (spl_obj_size(skc
) <= spl_kmem_cache_kmem_limit
)
966 skc
->skc_flags
|= KMC_KMEM
;
969 * All other objects are considered large and are placed
970 * on vmem backed slabs.
973 skc
->skc_flags
|= KMC_VMEM
;
977 * Given the type of slab allocate the required resources.
979 if (skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
)) {
980 rc
= spl_slab_size(skc
,
981 &skc
->skc_slab_objs
, &skc
->skc_slab_size
);
985 rc
= spl_magazine_create(skc
);
989 if (size
> (SPL_MAX_KMEM_ORDER_NR_PAGES
* PAGE_SIZE
)) {
994 skc
->skc_linux_cache
= kmem_cache_create(
995 skc
->skc_name
, size
, align
, 0, NULL
);
996 if (skc
->skc_linux_cache
== NULL
) {
1001 #if defined(HAVE_KMEM_CACHE_ALLOCFLAGS)
1002 skc
->skc_linux_cache
->allocflags
|= __GFP_COMP
;
1003 #elif defined(HAVE_KMEM_CACHE_GFPFLAGS)
1004 skc
->skc_linux_cache
->gfpflags
|= __GFP_COMP
;
1006 skc
->skc_flags
|= KMC_NOMAGAZINE
;
1009 if (spl_kmem_cache_expire
& KMC_EXPIRE_AGE
)
1010 skc
->skc_taskqid
= taskq_dispatch_delay(spl_kmem_cache_taskq
,
1011 spl_cache_age
, skc
, TQ_SLEEP
,
1012 ddi_get_lbolt() + skc
->skc_delay
/ 3 * HZ
);
1014 down_write(&spl_kmem_cache_sem
);
1015 list_add_tail(&skc
->skc_list
, &spl_kmem_cache_list
);
1016 up_write(&spl_kmem_cache_sem
);
1020 kfree(skc
->skc_name
);
1024 EXPORT_SYMBOL(spl_kmem_cache_create
);
1027 * Register a move callback for cache defragmentation.
1028 * XXX: Unimplemented but harmless to stub out for now.
1031 spl_kmem_cache_set_move(spl_kmem_cache_t
*skc
,
1032 kmem_cbrc_t (move
)(void *, void *, size_t, void *))
1034 ASSERT(move
!= NULL
);
1036 EXPORT_SYMBOL(spl_kmem_cache_set_move
);
1039 * Destroy a cache and all objects associated with the cache.
1042 spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
)
1044 DECLARE_WAIT_QUEUE_HEAD(wq
);
1047 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1048 ASSERT(skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
| KMC_SLAB
));
1050 down_write(&spl_kmem_cache_sem
);
1051 list_del_init(&skc
->skc_list
);
1052 up_write(&spl_kmem_cache_sem
);
1054 /* Cancel any and wait for any pending delayed tasks */
1055 VERIFY(!test_and_set_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1057 spin_lock(&skc
->skc_lock
);
1058 id
= skc
->skc_taskqid
;
1059 spin_unlock(&skc
->skc_lock
);
1061 taskq_cancel_id(spl_kmem_cache_taskq
, id
);
1064 * Wait until all current callers complete, this is mainly
1065 * to catch the case where a low memory situation triggers a
1066 * cache reaping action which races with this destroy.
1068 wait_event(wq
, atomic_read(&skc
->skc_ref
) == 0);
1070 if (skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
)) {
1071 spl_magazine_destroy(skc
);
1072 spl_slab_reclaim(skc
);
1074 ASSERT(skc
->skc_flags
& KMC_SLAB
);
1075 kmem_cache_destroy(skc
->skc_linux_cache
);
1078 spin_lock(&skc
->skc_lock
);
1081 * Validate there are no objects in use and free all the
1082 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers.
1084 ASSERT3U(skc
->skc_slab_alloc
, ==, 0);
1085 ASSERT3U(skc
->skc_obj_alloc
, ==, 0);
1086 ASSERT3U(skc
->skc_slab_total
, ==, 0);
1087 ASSERT3U(skc
->skc_obj_total
, ==, 0);
1088 ASSERT3U(skc
->skc_obj_emergency
, ==, 0);
1089 ASSERT(list_empty(&skc
->skc_complete_list
));
1091 spin_unlock(&skc
->skc_lock
);
1093 kfree(skc
->skc_name
);
1096 EXPORT_SYMBOL(spl_kmem_cache_destroy
);
1099 * Allocate an object from a slab attached to the cache. This is used to
1100 * repopulate the per-cpu magazine caches in batches when they run low.
1103 spl_cache_obj(spl_kmem_cache_t
*skc
, spl_kmem_slab_t
*sks
)
1105 spl_kmem_obj_t
*sko
;
1107 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1108 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1109 ASSERT(spin_is_locked(&skc
->skc_lock
));
1111 sko
= list_entry(sks
->sks_free_list
.next
, spl_kmem_obj_t
, sko_list
);
1112 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1113 ASSERT(sko
->sko_addr
!= NULL
);
1115 /* Remove from sks_free_list */
1116 list_del_init(&sko
->sko_list
);
1118 sks
->sks_age
= jiffies
;
1120 skc
->skc_obj_alloc
++;
1122 /* Track max obj usage statistics */
1123 if (skc
->skc_obj_alloc
> skc
->skc_obj_max
)
1124 skc
->skc_obj_max
= skc
->skc_obj_alloc
;
1126 /* Track max slab usage statistics */
1127 if (sks
->sks_ref
== 1) {
1128 skc
->skc_slab_alloc
++;
1130 if (skc
->skc_slab_alloc
> skc
->skc_slab_max
)
1131 skc
->skc_slab_max
= skc
->skc_slab_alloc
;
1134 return (sko
->sko_addr
);
1138 * Generic slab allocation function to run by the global work queues.
1139 * It is responsible for allocating a new slab, linking it in to the list
1140 * of partial slabs, and then waking any waiters.
1143 spl_cache_grow_work(void *data
)
1145 spl_kmem_alloc_t
*ska
= (spl_kmem_alloc_t
*)data
;
1146 spl_kmem_cache_t
*skc
= ska
->ska_cache
;
1147 spl_kmem_slab_t
*sks
;
1149 #if defined(PF_MEMALLOC_NOIO)
1150 unsigned noio_flag
= memalloc_noio_save();
1151 sks
= spl_slab_alloc(skc
, ska
->ska_flags
);
1152 memalloc_noio_restore(noio_flag
);
1154 fstrans_cookie_t cookie
= spl_fstrans_mark();
1155 sks
= spl_slab_alloc(skc
, ska
->ska_flags
);
1156 spl_fstrans_unmark(cookie
);
1158 spin_lock(&skc
->skc_lock
);
1160 skc
->skc_slab_total
++;
1161 skc
->skc_obj_total
+= sks
->sks_objs
;
1162 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1165 atomic_dec(&skc
->skc_ref
);
1166 smp_mb__before_atomic();
1167 clear_bit(KMC_BIT_GROWING
, &skc
->skc_flags
);
1168 clear_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
);
1169 smp_mb__after_atomic();
1170 wake_up_all(&skc
->skc_waitq
);
1171 spin_unlock(&skc
->skc_lock
);
1177 * Returns non-zero when a new slab should be available.
1180 spl_cache_grow_wait(spl_kmem_cache_t
*skc
)
1182 return (!test_bit(KMC_BIT_GROWING
, &skc
->skc_flags
));
1186 * No available objects on any slabs, create a new slab. Note that this
1187 * functionality is disabled for KMC_SLAB caches which are backed by the
1191 spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
, void **obj
)
1193 int remaining
, rc
= 0;
1195 ASSERT0(flags
& ~KM_PUBLIC_MASK
);
1196 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1197 ASSERT((skc
->skc_flags
& KMC_SLAB
) == 0);
1202 * Before allocating a new slab wait for any reaping to complete and
1203 * then return so the local magazine can be rechecked for new objects.
1205 if (test_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1206 rc
= spl_wait_on_bit(&skc
->skc_flags
, KMC_BIT_REAPING
,
1207 TASK_UNINTERRUPTIBLE
);
1208 return (rc
? rc
: -EAGAIN
);
1212 * This is handled by dispatching a work request to the global work
1213 * queue. This allows us to asynchronously allocate a new slab while
1214 * retaining the ability to safely fall back to a smaller synchronous
1215 * allocations to ensure forward progress is always maintained.
1217 if (test_and_set_bit(KMC_BIT_GROWING
, &skc
->skc_flags
) == 0) {
1218 spl_kmem_alloc_t
*ska
;
1220 ska
= kmalloc(sizeof (*ska
), kmem_flags_convert(flags
));
1222 clear_bit_unlock(KMC_BIT_GROWING
, &skc
->skc_flags
);
1223 smp_mb__after_atomic();
1224 wake_up_all(&skc
->skc_waitq
);
1228 atomic_inc(&skc
->skc_ref
);
1229 ska
->ska_cache
= skc
;
1230 ska
->ska_flags
= flags
;
1231 taskq_init_ent(&ska
->ska_tqe
);
1232 taskq_dispatch_ent(spl_kmem_cache_taskq
,
1233 spl_cache_grow_work
, ska
, 0, &ska
->ska_tqe
);
1237 * The goal here is to only detect the rare case where a virtual slab
1238 * allocation has deadlocked. We must be careful to minimize the use
1239 * of emergency objects which are more expensive to track. Therefore,
1240 * we set a very long timeout for the asynchronous allocation and if
1241 * the timeout is reached the cache is flagged as deadlocked. From
1242 * this point only new emergency objects will be allocated until the
1243 * asynchronous allocation completes and clears the deadlocked flag.
1245 if (test_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
)) {
1246 rc
= spl_emergency_alloc(skc
, flags
, obj
);
1248 remaining
= wait_event_timeout(skc
->skc_waitq
,
1249 spl_cache_grow_wait(skc
), HZ
/ 10);
1252 spin_lock(&skc
->skc_lock
);
1253 if (test_bit(KMC_BIT_GROWING
, &skc
->skc_flags
)) {
1254 set_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
);
1255 skc
->skc_obj_deadlock
++;
1257 spin_unlock(&skc
->skc_lock
);
1267 * Refill a per-cpu magazine with objects from the slabs for this cache.
1268 * Ideally the magazine can be repopulated using existing objects which have
1269 * been released, however if we are unable to locate enough free objects new
1270 * slabs of objects will be created. On success NULL is returned, otherwise
1271 * the address of a single emergency object is returned for use by the caller.
1274 spl_cache_refill(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flags
)
1276 spl_kmem_slab_t
*sks
;
1277 int count
= 0, rc
, refill
;
1280 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1281 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1283 refill
= MIN(skm
->skm_refill
, skm
->skm_size
- skm
->skm_avail
);
1284 spin_lock(&skc
->skc_lock
);
1286 while (refill
> 0) {
1287 /* No slabs available we may need to grow the cache */
1288 if (list_empty(&skc
->skc_partial_list
)) {
1289 spin_unlock(&skc
->skc_lock
);
1292 rc
= spl_cache_grow(skc
, flags
, &obj
);
1293 local_irq_disable();
1295 /* Emergency object for immediate use by caller */
1296 if (rc
== 0 && obj
!= NULL
)
1302 /* Rescheduled to different CPU skm is not local */
1303 if (skm
!= skc
->skc_mag
[smp_processor_id()])
1307 * Potentially rescheduled to the same CPU but
1308 * allocations may have occurred from this CPU while
1309 * we were sleeping so recalculate max refill.
1311 refill
= MIN(refill
, skm
->skm_size
- skm
->skm_avail
);
1313 spin_lock(&skc
->skc_lock
);
1317 /* Grab the next available slab */
1318 sks
= list_entry((&skc
->skc_partial_list
)->next
,
1319 spl_kmem_slab_t
, sks_list
);
1320 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1321 ASSERT(sks
->sks_ref
< sks
->sks_objs
);
1322 ASSERT(!list_empty(&sks
->sks_free_list
));
1325 * Consume as many objects as needed to refill the requested
1326 * cache. We must also be careful not to overfill it.
1328 while (sks
->sks_ref
< sks
->sks_objs
&& refill
-- > 0 &&
1330 ASSERT(skm
->skm_avail
< skm
->skm_size
);
1331 ASSERT(count
< skm
->skm_size
);
1332 skm
->skm_objs
[skm
->skm_avail
++] =
1333 spl_cache_obj(skc
, sks
);
1336 /* Move slab to skc_complete_list when full */
1337 if (sks
->sks_ref
== sks
->sks_objs
) {
1338 list_del(&sks
->sks_list
);
1339 list_add(&sks
->sks_list
, &skc
->skc_complete_list
);
1343 spin_unlock(&skc
->skc_lock
);
1349 * Release an object back to the slab from which it came.
1352 spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
)
1354 spl_kmem_slab_t
*sks
= NULL
;
1355 spl_kmem_obj_t
*sko
= NULL
;
1357 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1358 ASSERT(spin_is_locked(&skc
->skc_lock
));
1360 sko
= spl_sko_from_obj(skc
, obj
);
1361 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1362 sks
= sko
->sko_slab
;
1363 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1364 ASSERT(sks
->sks_cache
== skc
);
1365 list_add(&sko
->sko_list
, &sks
->sks_free_list
);
1367 sks
->sks_age
= jiffies
;
1369 skc
->skc_obj_alloc
--;
1372 * Move slab to skc_partial_list when no longer full. Slabs
1373 * are added to the head to keep the partial list is quasi-full
1374 * sorted order. Fuller at the head, emptier at the tail.
1376 if (sks
->sks_ref
== (sks
->sks_objs
- 1)) {
1377 list_del(&sks
->sks_list
);
1378 list_add(&sks
->sks_list
, &skc
->skc_partial_list
);
1382 * Move empty slabs to the end of the partial list so
1383 * they can be easily found and freed during reclamation.
1385 if (sks
->sks_ref
== 0) {
1386 list_del(&sks
->sks_list
);
1387 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1388 skc
->skc_slab_alloc
--;
1393 * Allocate an object from the per-cpu magazine, or if the magazine
1394 * is empty directly allocate from a slab and repopulate the magazine.
1397 spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
)
1399 spl_kmem_magazine_t
*skm
;
1402 ASSERT0(flags
& ~KM_PUBLIC_MASK
);
1403 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1404 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1407 * Allocate directly from a Linux slab. All optimizations are left
1408 * to the underlying cache we only need to guarantee that KM_SLEEP
1409 * callers will never fail.
1411 if (skc
->skc_flags
& KMC_SLAB
) {
1412 struct kmem_cache
*slc
= skc
->skc_linux_cache
;
1414 obj
= kmem_cache_alloc(slc
, kmem_flags_convert(flags
));
1415 } while ((obj
== NULL
) && !(flags
& KM_NOSLEEP
));
1420 local_irq_disable();
1424 * Safe to update per-cpu structure without lock, but
1425 * in the restart case we must be careful to reacquire
1426 * the local magazine since this may have changed
1427 * when we need to grow the cache.
1429 skm
= skc
->skc_mag
[smp_processor_id()];
1430 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1432 if (likely(skm
->skm_avail
)) {
1433 /* Object available in CPU cache, use it */
1434 obj
= skm
->skm_objs
[--skm
->skm_avail
];
1435 skm
->skm_age
= jiffies
;
1437 obj
= spl_cache_refill(skc
, skm
, flags
);
1438 if ((obj
== NULL
) && !(flags
& KM_NOSLEEP
))
1447 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
1450 /* Pre-emptively migrate object to CPU L1 cache */
1452 if (obj
&& skc
->skc_ctor
)
1453 skc
->skc_ctor(obj
, skc
->skc_private
, flags
);
1460 EXPORT_SYMBOL(spl_kmem_cache_alloc
);
1463 * Free an object back to the local per-cpu magazine, there is no
1464 * guarantee that this is the same magazine the object was originally
1465 * allocated from. We may need to flush entire from the magazine
1466 * back to the slabs to make space.
1469 spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
)
1471 spl_kmem_magazine_t
*skm
;
1472 unsigned long flags
;
1474 int do_emergency
= 0;
1476 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1477 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1480 * Run the destructor
1483 skc
->skc_dtor(obj
, skc
->skc_private
);
1486 * Free the object from the Linux underlying Linux slab.
1488 if (skc
->skc_flags
& KMC_SLAB
) {
1489 kmem_cache_free(skc
->skc_linux_cache
, obj
);
1494 * While a cache has outstanding emergency objects all freed objects
1495 * must be checked. However, since emergency objects will never use
1496 * a virtual address these objects can be safely excluded as an
1499 if (!is_vmalloc_addr(obj
)) {
1500 spin_lock(&skc
->skc_lock
);
1501 do_emergency
= (skc
->skc_obj_emergency
> 0);
1502 spin_unlock(&skc
->skc_lock
);
1504 if (do_emergency
&& (spl_emergency_free(skc
, obj
) == 0))
1508 local_irq_save(flags
);
1511 * Safe to update per-cpu structure without lock, but
1512 * no remote memory allocation tracking is being performed
1513 * it is entirely possible to allocate an object from one
1514 * CPU cache and return it to another.
1516 skm
= skc
->skc_mag
[smp_processor_id()];
1517 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1520 * Per-CPU cache full, flush it to make space for this object,
1521 * this may result in an empty slab which can be reclaimed once
1522 * interrupts are re-enabled.
1524 if (unlikely(skm
->skm_avail
>= skm
->skm_size
)) {
1525 spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1529 /* Available space in cache, use it */
1530 skm
->skm_objs
[skm
->skm_avail
++] = obj
;
1532 local_irq_restore(flags
);
1535 spl_slab_reclaim(skc
);
1537 EXPORT_SYMBOL(spl_kmem_cache_free
);
1540 * The generic shrinker function for all caches. Under Linux a shrinker
1541 * may not be tightly coupled with a slab cache. In fact Linux always
1542 * systematically tries calling all registered shrinker callbacks which
1543 * report that they contain unused objects. Because of this we only
1544 * register one shrinker function in the shim layer for all slab caches.
1545 * We always attempt to shrink all caches when this generic shrinker
1548 * If sc->nr_to_scan is zero, the caller is requesting a query of the
1549 * number of objects which can potentially be freed. If it is nonzero,
1550 * the request is to free that many objects.
1552 * Linux kernels >= 3.12 have the count_objects and scan_objects callbacks
1553 * in struct shrinker and also require the shrinker to return the number
1556 * Older kernels require the shrinker to return the number of freeable
1557 * objects following the freeing of nr_to_free.
1559 * Linux semantics differ from those under Solaris, which are to
1560 * free all available objects which may (and probably will) be more
1561 * objects than the requested nr_to_scan.
1563 static spl_shrinker_t
1564 __spl_kmem_cache_generic_shrinker(struct shrinker
*shrink
,
1565 struct shrink_control
*sc
)
1567 spl_kmem_cache_t
*skc
;
1571 * No shrinking in a transaction context. Can cause deadlocks.
1573 if (sc
->nr_to_scan
&& spl_fstrans_check())
1574 return (SHRINK_STOP
);
1576 down_read(&spl_kmem_cache_sem
);
1577 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
1578 if (sc
->nr_to_scan
) {
1579 #ifdef HAVE_SPLIT_SHRINKER_CALLBACK
1580 uint64_t oldalloc
= skc
->skc_obj_alloc
;
1581 spl_kmem_cache_reap_now(skc
,
1582 MAX(sc
->nr_to_scan
>>fls64(skc
->skc_slab_objs
), 1));
1583 if (oldalloc
> skc
->skc_obj_alloc
)
1584 alloc
+= oldalloc
- skc
->skc_obj_alloc
;
1586 spl_kmem_cache_reap_now(skc
,
1587 MAX(sc
->nr_to_scan
>>fls64(skc
->skc_slab_objs
), 1));
1588 alloc
+= skc
->skc_obj_alloc
;
1589 #endif /* HAVE_SPLIT_SHRINKER_CALLBACK */
1591 /* Request to query number of freeable objects */
1592 alloc
+= skc
->skc_obj_alloc
;
1595 up_read(&spl_kmem_cache_sem
);
1598 * When KMC_RECLAIM_ONCE is set allow only a single reclaim pass.
1599 * This functionality only exists to work around a rare issue where
1600 * shrink_slabs() is repeatedly invoked by many cores causing the
1603 if ((spl_kmem_cache_reclaim
& KMC_RECLAIM_ONCE
) && sc
->nr_to_scan
)
1604 return (SHRINK_STOP
);
1606 return (MAX(alloc
, 0));
1609 SPL_SHRINKER_CALLBACK_WRAPPER(spl_kmem_cache_generic_shrinker
);
1612 * Call the registered reclaim function for a cache. Depending on how
1613 * many and which objects are released it may simply repopulate the
1614 * local magazine which will then need to age-out. Objects which cannot
1615 * fit in the magazine we will be released back to their slabs which will
1616 * also need to age out before being release. This is all just best
1617 * effort and we do not want to thrash creating and destroying slabs.
1620 spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
, int count
)
1622 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1623 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1625 atomic_inc(&skc
->skc_ref
);
1628 * Execute the registered reclaim callback if it exists. The
1629 * per-cpu caches will be drained when is set KMC_EXPIRE_MEM.
1631 if (skc
->skc_flags
& KMC_SLAB
) {
1632 if (skc
->skc_reclaim
)
1633 skc
->skc_reclaim(skc
->skc_private
);
1635 if (spl_kmem_cache_expire
& KMC_EXPIRE_MEM
)
1636 kmem_cache_shrink(skc
->skc_linux_cache
);
1642 * Prevent concurrent cache reaping when contended.
1644 if (test_and_set_bit(KMC_BIT_REAPING
, &skc
->skc_flags
))
1648 * When a reclaim function is available it may be invoked repeatedly
1649 * until at least a single slab can be freed. This ensures that we
1650 * do free memory back to the system. This helps minimize the chance
1651 * of an OOM event when the bulk of memory is used by the slab.
1653 * When free slabs are already available the reclaim callback will be
1654 * skipped. Additionally, if no forward progress is detected despite
1655 * a reclaim function the cache will be skipped to avoid deadlock.
1657 * Longer term this would be the correct place to add the code which
1658 * repacks the slabs in order minimize fragmentation.
1660 if (skc
->skc_reclaim
) {
1661 uint64_t objects
= UINT64_MAX
;
1665 spin_lock(&skc
->skc_lock
);
1667 (skc
->skc_slab_total
> 0) &&
1668 ((skc
->skc_slab_total
-skc
->skc_slab_alloc
) == 0) &&
1669 (skc
->skc_obj_alloc
< objects
);
1671 objects
= skc
->skc_obj_alloc
;
1672 spin_unlock(&skc
->skc_lock
);
1675 skc
->skc_reclaim(skc
->skc_private
);
1677 } while (do_reclaim
);
1680 /* Reclaim from the magazine and free all now empty slabs. */
1681 if (spl_kmem_cache_expire
& KMC_EXPIRE_MEM
) {
1682 spl_kmem_magazine_t
*skm
;
1683 unsigned long irq_flags
;
1685 local_irq_save(irq_flags
);
1686 skm
= skc
->skc_mag
[smp_processor_id()];
1687 spl_cache_flush(skc
, skm
, skm
->skm_avail
);
1688 local_irq_restore(irq_flags
);
1691 spl_slab_reclaim(skc
);
1692 clear_bit_unlock(KMC_BIT_REAPING
, &skc
->skc_flags
);
1693 smp_mb__after_atomic();
1694 wake_up_bit(&skc
->skc_flags
, KMC_BIT_REAPING
);
1696 atomic_dec(&skc
->skc_ref
);
1698 EXPORT_SYMBOL(spl_kmem_cache_reap_now
);
1701 * Reap all free slabs from all registered caches.
1706 struct shrink_control sc
;
1708 sc
.nr_to_scan
= KMC_REAP_CHUNK
;
1709 sc
.gfp_mask
= GFP_KERNEL
;
1711 (void) __spl_kmem_cache_generic_shrinker(NULL
, &sc
);
1713 EXPORT_SYMBOL(spl_kmem_reap
);
1716 spl_kmem_cache_init(void)
1718 init_rwsem(&spl_kmem_cache_sem
);
1719 INIT_LIST_HEAD(&spl_kmem_cache_list
);
1720 spl_kmem_cache_taskq
= taskq_create("spl_kmem_cache",
1721 spl_kmem_cache_kmem_threads
, defclsyspri
,
1722 spl_kmem_cache_kmem_threads
* 8, INT_MAX
,
1723 TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
1724 spl_register_shrinker(&spl_kmem_cache_shrinker
);
1730 spl_kmem_cache_fini(void)
1732 spl_unregister_shrinker(&spl_kmem_cache_shrinker
);
1733 taskq_destroy(spl_kmem_cache_taskq
);