]> git.proxmox.com Git - mirror_zfs.git/blame - module/spl/spl-kmem-cache.c
Fix style of spl_kmem_cache_create()
[mirror_zfs.git] / module / spl / spl-kmem-cache.c
CommitLineData
b34b9563 1/*
e5b9b344
BB
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>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
10 *
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.
15 *
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
19 * for more details.
20 *
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/>.
b34b9563 23 */
e5b9b344
BB
24
25#include <sys/kmem.h>
26#include <sys/kmem_cache.h>
a9125891 27#include <sys/shrinker.h>
e5b9b344
BB
28#include <sys/taskq.h>
29#include <sys/timer.h>
30#include <sys/vmem.h>
a9125891 31#include <sys/wait.h>
e5b9b344
BB
32#include <linux/slab.h>
33#include <linux/swap.h>
9f456111 34#include <linux/prefetch.h>
e5b9b344
BB
35
36/*
37 * Within the scope of spl-kmem.c file the kmem_cache_* definitions
38 * are removed to allow access to the real Linux slab allocator.
39 */
40#undef kmem_cache_destroy
41#undef kmem_cache_create
42#undef kmem_cache_alloc
43#undef kmem_cache_free
44
45
a988a35a
RY
46/*
47 * Linux 3.16 replaced smp_mb__{before,after}_{atomic,clear}_{dec,inc,bit}()
48 * with smp_mb__{before,after}_atomic() because they were redundant. This is
49 * only used inside our SLAB allocator, so we implement an internal wrapper
50 * here to give us smp_mb__{before,after}_atomic() on older kernels.
51 */
52#ifndef smp_mb__before_atomic
53#define smp_mb__before_atomic(x) smp_mb__before_clear_bit(x)
54#endif
55
56#ifndef smp_mb__after_atomic
57#define smp_mb__after_atomic(x) smp_mb__after_clear_bit(x)
58#endif
59
e5b9b344
BB
60/*
61 * Cache expiration was implemented because it was part of the default Solaris
62 * kmem_cache behavior. The idea is that per-cpu objects which haven't been
63 * accessed in several seconds should be returned to the cache. On the other
64 * hand Linux slabs never move objects back to the slabs unless there is
65 * memory pressure on the system. By default the Linux method is enabled
66 * because it has been shown to improve responsiveness on low memory systems.
67 * This policy may be changed by setting KMC_EXPIRE_AGE or KMC_EXPIRE_MEM.
68 */
3673d032 69/* BEGIN CSTYLED */
e5b9b344
BB
70unsigned int spl_kmem_cache_expire = KMC_EXPIRE_MEM;
71EXPORT_SYMBOL(spl_kmem_cache_expire);
72module_param(spl_kmem_cache_expire, uint, 0644);
73MODULE_PARM_DESC(spl_kmem_cache_expire, "By age (0x1) or low memory (0x2)");
74
1a204968
BB
75/*
76 * Cache magazines are an optimization designed to minimize the cost of
77 * allocating memory. They do this by keeping a per-cpu cache of recently
78 * freed objects, which can then be reallocated without taking a lock. This
79 * can improve performance on highly contended caches. However, because
80 * objects in magazines will prevent otherwise empty slabs from being
81 * immediately released this may not be ideal for low memory machines.
82 *
83 * For this reason spl_kmem_cache_magazine_size can be used to set a maximum
84 * magazine size. When this value is set to 0 the magazine size will be
85 * automatically determined based on the object size. Otherwise magazines
86 * will be limited to 2-256 objects per magazine (i.e per cpu). Magazines
87 * may never be entirely disabled in this implementation.
88 */
89unsigned int spl_kmem_cache_magazine_size = 0;
90module_param(spl_kmem_cache_magazine_size, uint, 0444);
91MODULE_PARM_DESC(spl_kmem_cache_magazine_size,
31f24932 92 "Default magazine size (2-256), set automatically (0)");
1a204968 93
e5b9b344
BB
94/*
95 * The default behavior is to report the number of objects remaining in the
96 * cache. This allows the Linux VM to repeatedly reclaim objects from the
97 * cache when memory is low satisfy other memory allocations. Alternately,
98 * setting this value to KMC_RECLAIM_ONCE limits how aggressively the cache
99 * is reclaimed. This may increase the likelihood of out of memory events.
100 */
101unsigned int spl_kmem_cache_reclaim = 0 /* KMC_RECLAIM_ONCE */;
102module_param(spl_kmem_cache_reclaim, uint, 0644);
103MODULE_PARM_DESC(spl_kmem_cache_reclaim, "Single reclaim pass (0x1)");
104
105unsigned int spl_kmem_cache_obj_per_slab = SPL_KMEM_CACHE_OBJ_PER_SLAB;
106module_param(spl_kmem_cache_obj_per_slab, uint, 0644);
107MODULE_PARM_DESC(spl_kmem_cache_obj_per_slab, "Number of objects per slab");
108
109unsigned int spl_kmem_cache_obj_per_slab_min = SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN;
110module_param(spl_kmem_cache_obj_per_slab_min, uint, 0644);
111MODULE_PARM_DESC(spl_kmem_cache_obj_per_slab_min,
b34b9563 112 "Minimal number of objects per slab");
e5b9b344 113
3018bffa 114unsigned int spl_kmem_cache_max_size = SPL_KMEM_CACHE_MAX_SIZE;
e5b9b344
BB
115module_param(spl_kmem_cache_max_size, uint, 0644);
116MODULE_PARM_DESC(spl_kmem_cache_max_size, "Maximum size of slab in MB");
117
118/*
119 * For small objects the Linux slab allocator should be used to make the most
120 * efficient use of the memory. However, large objects are not supported by
121 * the Linux slab and therefore the SPL implementation is preferred. A cutoff
122 * of 16K was determined to be optimal for architectures using 4K pages.
123 */
124#if PAGE_SIZE == 4096
125unsigned int spl_kmem_cache_slab_limit = 16384;
126#else
127unsigned int spl_kmem_cache_slab_limit = 0;
128#endif
129module_param(spl_kmem_cache_slab_limit, uint, 0644);
130MODULE_PARM_DESC(spl_kmem_cache_slab_limit,
b34b9563 131 "Objects less than N bytes use the Linux slab");
e5b9b344 132
3018bffa
BB
133/*
134 * This value defaults to a threshold designed to avoid allocations which
135 * have been deemed costly by the kernel.
136 */
137unsigned int spl_kmem_cache_kmem_limit =
5461eefe
BB
138 ((1 << (PAGE_ALLOC_COSTLY_ORDER - 1)) * PAGE_SIZE) /
139 SPL_KMEM_CACHE_OBJ_PER_SLAB;
e5b9b344
BB
140module_param(spl_kmem_cache_kmem_limit, uint, 0644);
141MODULE_PARM_DESC(spl_kmem_cache_kmem_limit,
b34b9563 142 "Objects less than N bytes use the kmalloc");
e5b9b344 143
436ad60f
BB
144/*
145 * The number of threads available to allocate new slabs for caches. This
146 * should not need to be tuned but it is available for performance analysis.
147 */
148unsigned int spl_kmem_cache_kmem_threads = 4;
149module_param(spl_kmem_cache_kmem_threads, uint, 0444);
150MODULE_PARM_DESC(spl_kmem_cache_kmem_threads,
151 "Number of spl_kmem_cache threads");
3673d032 152/* END CSTYLED */
436ad60f 153
e5b9b344
BB
154/*
155 * Slab allocation interfaces
156 *
157 * While the Linux slab implementation was inspired by the Solaris
158 * implementation I cannot use it to emulate the Solaris APIs. I
159 * require two features which are not provided by the Linux slab.
160 *
161 * 1) Constructors AND destructors. Recent versions of the Linux
162 * kernel have removed support for destructors. This is a deal
163 * breaker for the SPL which contains particularly expensive
164 * initializers for mutex's, condition variables, etc. We also
165 * require a minimal level of cleanup for these data types unlike
b34b9563 166 * many Linux data types which do need to be explicitly destroyed.
e5b9b344
BB
167 *
168 * 2) Virtual address space backed slab. Callers of the Solaris slab
169 * expect it to work well for both small are very large allocations.
170 * Because of memory fragmentation the Linux slab which is backed
171 * by kmalloc'ed memory performs very badly when confronted with
172 * large numbers of large allocations. Basing the slab on the
173 * virtual address space removes the need for contiguous pages
174 * and greatly improve performance for large allocations.
175 *
176 * For these reasons, the SPL has its own slab implementation with
177 * the needed features. It is not as highly optimized as either the
178 * Solaris or Linux slabs, but it should get me most of what is
179 * needed until it can be optimized or obsoleted by another approach.
180 *
181 * One serious concern I do have about this method is the relatively
182 * small virtual address space on 32bit arches. This will seriously
183 * constrain the size of the slab caches and their performance.
e5b9b344
BB
184 */
185
186struct list_head spl_kmem_cache_list; /* List of caches */
187struct rw_semaphore spl_kmem_cache_sem; /* Cache list lock */
b34b9563 188taskq_t *spl_kmem_cache_taskq; /* Task queue for ageing / reclaim */
e5b9b344
BB
189
190static void spl_cache_shrink(spl_kmem_cache_t *skc, void *obj);
191
192SPL_SHRINKER_CALLBACK_FWD_DECLARE(spl_kmem_cache_generic_shrinker);
193SPL_SHRINKER_DECLARE(spl_kmem_cache_shrinker,
194 spl_kmem_cache_generic_shrinker, KMC_DEFAULT_SEEKS);
195
196static void *
197kv_alloc(spl_kmem_cache_t *skc, int size, int flags)
198{
c3eabc75 199 gfp_t lflags = kmem_flags_convert(flags);
e5b9b344
BB
200 void *ptr;
201
3018bffa
BB
202 if (skc->skc_flags & KMC_KMEM) {
203 ASSERT(ISP2(size));
c3eabc75 204 ptr = (void *)__get_free_pages(lflags, get_order(size));
3018bffa 205 } else {
b4ad50ac 206 ptr = __vmalloc(size, lflags | __GFP_HIGHMEM, PAGE_KERNEL);
3018bffa 207 }
e5b9b344
BB
208
209 /* Resulting allocated memory will be page aligned */
210 ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE));
211
b34b9563 212 return (ptr);
e5b9b344
BB
213}
214
215static void
216kv_free(spl_kmem_cache_t *skc, void *ptr, int size)
217{
218 ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE));
e5b9b344
BB
219
220 /*
221 * The Linux direct reclaim path uses this out of band value to
222 * determine if forward progress is being made. Normally this is
223 * incremented by kmem_freepages() which is part of the various
224 * Linux slab implementations. However, since we are using none
225 * of that infrastructure we are responsible for incrementing it.
226 */
227 if (current->reclaim_state)
228 current->reclaim_state->reclaimed_slab += size >> PAGE_SHIFT;
229
3018bffa
BB
230 if (skc->skc_flags & KMC_KMEM) {
231 ASSERT(ISP2(size));
e5b9b344 232 free_pages((unsigned long)ptr, get_order(size));
3018bffa 233 } else {
e5b9b344 234 vfree(ptr);
3018bffa 235 }
e5b9b344
BB
236}
237
238/*
239 * Required space for each aligned sks.
240 */
241static inline uint32_t
242spl_sks_size(spl_kmem_cache_t *skc)
243{
b34b9563
BB
244 return (P2ROUNDUP_TYPED(sizeof (spl_kmem_slab_t),
245 skc->skc_obj_align, uint32_t));
e5b9b344
BB
246}
247
248/*
249 * Required space for each aligned object.
250 */
251static inline uint32_t
252spl_obj_size(spl_kmem_cache_t *skc)
253{
254 uint32_t align = skc->skc_obj_align;
255
b34b9563
BB
256 return (P2ROUNDUP_TYPED(skc->skc_obj_size, align, uint32_t) +
257 P2ROUNDUP_TYPED(sizeof (spl_kmem_obj_t), align, uint32_t));
e5b9b344
BB
258}
259
260/*
261 * Lookup the spl_kmem_object_t for an object given that object.
262 */
263static inline spl_kmem_obj_t *
264spl_sko_from_obj(spl_kmem_cache_t *skc, void *obj)
265{
b34b9563
BB
266 return (obj + P2ROUNDUP_TYPED(skc->skc_obj_size,
267 skc->skc_obj_align, uint32_t));
e5b9b344
BB
268}
269
270/*
271 * Required space for each offslab object taking in to account alignment
272 * restrictions and the power-of-two requirement of kv_alloc().
273 */
274static inline uint32_t
275spl_offslab_size(spl_kmem_cache_t *skc)
276{
b34b9563 277 return (1UL << (fls64(spl_obj_size(skc)) + 1));
e5b9b344
BB
278}
279
280/*
281 * It's important that we pack the spl_kmem_obj_t structure and the
282 * actual objects in to one large address space to minimize the number
283 * of calls to the allocator. It is far better to do a few large
284 * allocations and then subdivide it ourselves. Now which allocator
285 * we use requires balancing a few trade offs.
286 *
287 * For small objects we use kmem_alloc() because as long as you are
288 * only requesting a small number of pages (ideally just one) its cheap.
289 * However, when you start requesting multiple pages with kmem_alloc()
290 * it gets increasingly expensive since it requires contiguous pages.
291 * For this reason we shift to vmem_alloc() for slabs of large objects
292 * which removes the need for contiguous pages. We do not use
293 * vmem_alloc() in all cases because there is significant locking
294 * overhead in __get_vm_area_node(). This function takes a single
295 * global lock when acquiring an available virtual address range which
296 * serializes all vmem_alloc()'s for all slab caches. Using slightly
297 * different allocation functions for small and large objects should
298 * give us the best of both worlds.
299 *
300 * KMC_ONSLAB KMC_OFFSLAB
301 *
302 * +------------------------+ +-----------------+
303 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
304 * | skc_obj_size <-+ | | +-----------------+ | |
305 * | spl_kmem_obj_t | | | |
306 * | skc_obj_size <---+ | +-----------------+ | |
307 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
308 * | ... v | | spl_kmem_obj_t | |
309 * +------------------------+ +-----------------+ v
310 */
311static spl_kmem_slab_t *
312spl_slab_alloc(spl_kmem_cache_t *skc, int flags)
313{
314 spl_kmem_slab_t *sks;
315 spl_kmem_obj_t *sko, *n;
316 void *base, *obj;
317 uint32_t obj_size, offslab_size = 0;
318 int i, rc = 0;
319
320 base = kv_alloc(skc, skc->skc_slab_size, flags);
321 if (base == NULL)
322 return (NULL);
323
324 sks = (spl_kmem_slab_t *)base;
325 sks->sks_magic = SKS_MAGIC;
326 sks->sks_objs = skc->skc_slab_objs;
327 sks->sks_age = jiffies;
328 sks->sks_cache = skc;
329 INIT_LIST_HEAD(&sks->sks_list);
330 INIT_LIST_HEAD(&sks->sks_free_list);
331 sks->sks_ref = 0;
332 obj_size = spl_obj_size(skc);
333
334 if (skc->skc_flags & KMC_OFFSLAB)
335 offslab_size = spl_offslab_size(skc);
336
337 for (i = 0; i < sks->sks_objs; i++) {
338 if (skc->skc_flags & KMC_OFFSLAB) {
339 obj = kv_alloc(skc, offslab_size, flags);
340 if (!obj) {
341 rc = -ENOMEM;
342 goto out;
343 }
344 } else {
345 obj = base + spl_sks_size(skc) + (i * obj_size);
346 }
347
348 ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align));
349 sko = spl_sko_from_obj(skc, obj);
350 sko->sko_addr = obj;
351 sko->sko_magic = SKO_MAGIC;
352 sko->sko_slab = sks;
353 INIT_LIST_HEAD(&sko->sko_list);
354 list_add_tail(&sko->sko_list, &sks->sks_free_list);
355 }
356
357out:
358 if (rc) {
359 if (skc->skc_flags & KMC_OFFSLAB)
b34b9563 360 list_for_each_entry_safe(sko,
3673d032 361 n, &sks->sks_free_list, sko_list) {
e5b9b344 362 kv_free(skc, sko->sko_addr, offslab_size);
3673d032 363 }
e5b9b344
BB
364
365 kv_free(skc, base, skc->skc_slab_size);
366 sks = NULL;
367 }
368
369 return (sks);
370}
371
372/*
373 * Remove a slab from complete or partial list, it must be called with
374 * the 'skc->skc_lock' held but the actual free must be performed
375 * outside the lock to prevent deadlocking on vmem addresses.
376 */
377static void
378spl_slab_free(spl_kmem_slab_t *sks,
b34b9563 379 struct list_head *sks_list, struct list_head *sko_list)
e5b9b344
BB
380{
381 spl_kmem_cache_t *skc;
382
383 ASSERT(sks->sks_magic == SKS_MAGIC);
384 ASSERT(sks->sks_ref == 0);
385
386 skc = sks->sks_cache;
387 ASSERT(skc->skc_magic == SKC_MAGIC);
e5b9b344
BB
388
389 /*
390 * Update slab/objects counters in the cache, then remove the
391 * slab from the skc->skc_partial_list. Finally add the slab
392 * and all its objects in to the private work lists where the
393 * destructors will be called and the memory freed to the system.
394 */
395 skc->skc_obj_total -= sks->sks_objs;
396 skc->skc_slab_total--;
397 list_del(&sks->sks_list);
398 list_add(&sks->sks_list, sks_list);
399 list_splice_init(&sks->sks_free_list, sko_list);
400}
401
402/*
1a204968 403 * Reclaim empty slabs at the end of the partial list.
e5b9b344
BB
404 */
405static void
1a204968 406spl_slab_reclaim(spl_kmem_cache_t *skc)
e5b9b344
BB
407{
408 spl_kmem_slab_t *sks, *m;
409 spl_kmem_obj_t *sko, *n;
410 LIST_HEAD(sks_list);
411 LIST_HEAD(sko_list);
412 uint32_t size = 0;
e5b9b344
BB
413
414 /*
1a204968
BB
415 * Empty slabs and objects must be moved to a private list so they
416 * can be safely freed outside the spin lock. All empty slabs are
417 * at the end of skc->skc_partial_list, therefore once a non-empty
418 * slab is found we can stop scanning.
e5b9b344
BB
419 */
420 spin_lock(&skc->skc_lock);
b34b9563
BB
421 list_for_each_entry_safe_reverse(sks, m,
422 &skc->skc_partial_list, sks_list) {
1a204968
BB
423
424 if (sks->sks_ref > 0)
e5b9b344
BB
425 break;
426
1a204968 427 spl_slab_free(sks, &sks_list, &sko_list);
e5b9b344
BB
428 }
429 spin_unlock(&skc->skc_lock);
430
431 /*
432 * The following two loops ensure all the object destructors are
433 * run, any offslab objects are freed, and the slabs themselves
434 * are freed. This is all done outside the skc->skc_lock since
435 * this allows the destructor to sleep, and allows us to perform
436 * a conditional reschedule when a freeing a large number of
437 * objects and slabs back to the system.
438 */
439 if (skc->skc_flags & KMC_OFFSLAB)
440 size = spl_offslab_size(skc);
441
442 list_for_each_entry_safe(sko, n, &sko_list, sko_list) {
443 ASSERT(sko->sko_magic == SKO_MAGIC);
444
445 if (skc->skc_flags & KMC_OFFSLAB)
446 kv_free(skc, sko->sko_addr, size);
447 }
448
449 list_for_each_entry_safe(sks, m, &sks_list, sks_list) {
450 ASSERT(sks->sks_magic == SKS_MAGIC);
451 kv_free(skc, sks, skc->skc_slab_size);
452 }
453}
454
455static spl_kmem_emergency_t *
456spl_emergency_search(struct rb_root *root, void *obj)
457{
458 struct rb_node *node = root->rb_node;
459 spl_kmem_emergency_t *ske;
460 unsigned long address = (unsigned long)obj;
461
462 while (node) {
463 ske = container_of(node, spl_kmem_emergency_t, ske_node);
464
ee335174 465 if (address < ske->ske_obj)
e5b9b344 466 node = node->rb_left;
ee335174 467 else if (address > ske->ske_obj)
e5b9b344
BB
468 node = node->rb_right;
469 else
b34b9563 470 return (ske);
e5b9b344
BB
471 }
472
b34b9563 473 return (NULL);
e5b9b344
BB
474}
475
476static int
477spl_emergency_insert(struct rb_root *root, spl_kmem_emergency_t *ske)
478{
479 struct rb_node **new = &(root->rb_node), *parent = NULL;
480 spl_kmem_emergency_t *ske_tmp;
ee335174 481 unsigned long address = ske->ske_obj;
e5b9b344
BB
482
483 while (*new) {
484 ske_tmp = container_of(*new, spl_kmem_emergency_t, ske_node);
485
486 parent = *new;
ee335174 487 if (address < ske_tmp->ske_obj)
e5b9b344 488 new = &((*new)->rb_left);
ee335174 489 else if (address > ske_tmp->ske_obj)
e5b9b344
BB
490 new = &((*new)->rb_right);
491 else
b34b9563 492 return (0);
e5b9b344
BB
493 }
494
495 rb_link_node(&ske->ske_node, parent, new);
496 rb_insert_color(&ske->ske_node, root);
497
b34b9563 498 return (1);
e5b9b344
BB
499}
500
501/*
502 * Allocate a single emergency object and track it in a red black tree.
503 */
504static int
505spl_emergency_alloc(spl_kmem_cache_t *skc, int flags, void **obj)
506{
c3eabc75 507 gfp_t lflags = kmem_flags_convert(flags);
e5b9b344 508 spl_kmem_emergency_t *ske;
ee335174 509 int order = get_order(skc->skc_obj_size);
e5b9b344
BB
510 int empty;
511
512 /* Last chance use a partial slab if one now exists */
513 spin_lock(&skc->skc_lock);
514 empty = list_empty(&skc->skc_partial_list);
515 spin_unlock(&skc->skc_lock);
516 if (!empty)
517 return (-EEXIST);
518
c3eabc75 519 ske = kmalloc(sizeof (*ske), lflags);
e5b9b344
BB
520 if (ske == NULL)
521 return (-ENOMEM);
522
ee335174
BB
523 ske->ske_obj = __get_free_pages(lflags, order);
524 if (ske->ske_obj == 0) {
e5b9b344
BB
525 kfree(ske);
526 return (-ENOMEM);
527 }
528
529 spin_lock(&skc->skc_lock);
530 empty = spl_emergency_insert(&skc->skc_emergency_tree, ske);
531 if (likely(empty)) {
532 skc->skc_obj_total++;
533 skc->skc_obj_emergency++;
534 if (skc->skc_obj_emergency > skc->skc_obj_emergency_max)
535 skc->skc_obj_emergency_max = skc->skc_obj_emergency;
536 }
537 spin_unlock(&skc->skc_lock);
538
539 if (unlikely(!empty)) {
ee335174 540 free_pages(ske->ske_obj, order);
e5b9b344
BB
541 kfree(ske);
542 return (-EINVAL);
543 }
544
ee335174 545 *obj = (void *)ske->ske_obj;
e5b9b344
BB
546
547 return (0);
548}
549
550/*
551 * Locate the passed object in the red black tree and free it.
552 */
553static int
554spl_emergency_free(spl_kmem_cache_t *skc, void *obj)
555{
556 spl_kmem_emergency_t *ske;
ee335174 557 int order = get_order(skc->skc_obj_size);
e5b9b344
BB
558
559 spin_lock(&skc->skc_lock);
560 ske = spl_emergency_search(&skc->skc_emergency_tree, obj);
436ad60f 561 if (ske) {
e5b9b344
BB
562 rb_erase(&ske->ske_node, &skc->skc_emergency_tree);
563 skc->skc_obj_emergency--;
564 skc->skc_obj_total--;
565 }
566 spin_unlock(&skc->skc_lock);
567
436ad60f 568 if (ske == NULL)
e5b9b344
BB
569 return (-ENOENT);
570
ee335174 571 free_pages(ske->ske_obj, order);
e5b9b344
BB
572 kfree(ske);
573
574 return (0);
575}
576
577/*
578 * Release objects from the per-cpu magazine back to their slab. The flush
579 * argument contains the max number of entries to remove from the magazine.
580 */
581static void
582__spl_cache_flush(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flush)
583{
584 int i, count = MIN(flush, skm->skm_avail);
585
586 ASSERT(skc->skc_magic == SKC_MAGIC);
587 ASSERT(skm->skm_magic == SKM_MAGIC);
e5b9b344
BB
588
589 for (i = 0; i < count; i++)
590 spl_cache_shrink(skc, skm->skm_objs[i]);
591
592 skm->skm_avail -= count;
593 memmove(skm->skm_objs, &(skm->skm_objs[count]),
b34b9563 594 sizeof (void *) * skm->skm_avail);
e5b9b344
BB
595}
596
597static void
598spl_cache_flush(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flush)
599{
600 spin_lock(&skc->skc_lock);
601 __spl_cache_flush(skc, skm, flush);
602 spin_unlock(&skc->skc_lock);
603}
604
605static void
606spl_magazine_age(void *data)
607{
608 spl_kmem_cache_t *skc = (spl_kmem_cache_t *)data;
609 spl_kmem_magazine_t *skm = skc->skc_mag[smp_processor_id()];
610
611 ASSERT(skm->skm_magic == SKM_MAGIC);
612 ASSERT(skm->skm_cpu == smp_processor_id());
613 ASSERT(irqs_disabled());
614
615 /* There are no available objects or they are too young to age out */
616 if ((skm->skm_avail == 0) ||
617 time_before(jiffies, skm->skm_age + skc->skc_delay * HZ))
618 return;
619
620 /*
621 * Because we're executing in interrupt context we may have
622 * interrupted the holder of this lock. To avoid a potential
623 * deadlock return if the lock is contended.
624 */
625 if (!spin_trylock(&skc->skc_lock))
626 return;
627
628 __spl_cache_flush(skc, skm, skm->skm_refill);
629 spin_unlock(&skc->skc_lock);
630}
631
632/*
633 * Called regularly to keep a downward pressure on the cache.
634 *
635 * Objects older than skc->skc_delay seconds in the per-cpu magazines will
636 * be returned to the caches. This is done to prevent idle magazines from
637 * holding memory which could be better used elsewhere. The delay is
638 * present to prevent thrashing the magazine.
639 *
640 * The newly released objects may result in empty partial slabs. Those
641 * slabs should be released to the system. Otherwise moving the objects
642 * out of the magazines is just wasted work.
643 */
644static void
645spl_cache_age(void *data)
646{
647 spl_kmem_cache_t *skc = (spl_kmem_cache_t *)data;
648 taskqid_t id = 0;
649
650 ASSERT(skc->skc_magic == SKC_MAGIC);
651
652 /* Dynamically disabled at run time */
653 if (!(spl_kmem_cache_expire & KMC_EXPIRE_AGE))
654 return;
655
656 atomic_inc(&skc->skc_ref);
657
658 if (!(skc->skc_flags & KMC_NOMAGAZINE))
659 on_each_cpu(spl_magazine_age, skc, 1);
660
1a204968 661 spl_slab_reclaim(skc);
e5b9b344
BB
662
663 while (!test_bit(KMC_BIT_DESTROY, &skc->skc_flags) && !id) {
664 id = taskq_dispatch_delay(
665 spl_kmem_cache_taskq, spl_cache_age, skc, TQ_SLEEP,
666 ddi_get_lbolt() + skc->skc_delay / 3 * HZ);
667
668 /* Destroy issued after dispatch immediately cancel it */
669 if (test_bit(KMC_BIT_DESTROY, &skc->skc_flags) && id)
670 taskq_cancel_id(spl_kmem_cache_taskq, id);
671 }
672
673 spin_lock(&skc->skc_lock);
674 skc->skc_taskqid = id;
675 spin_unlock(&skc->skc_lock);
676
677 atomic_dec(&skc->skc_ref);
678}
679
680/*
681 * Size a slab based on the size of each aligned object plus spl_kmem_obj_t.
682 * When on-slab we want to target spl_kmem_cache_obj_per_slab. However,
683 * for very small objects we may end up with more than this so as not
684 * to waste space in the minimal allocation of a single page. Also for
685 * very large objects we may use as few as spl_kmem_cache_obj_per_slab_min,
686 * lower than this and we will fail.
687 */
688static int
689spl_slab_size(spl_kmem_cache_t *skc, uint32_t *objs, uint32_t *size)
690{
3018bffa 691 uint32_t sks_size, obj_size, max_size, tgt_size, tgt_objs;
e5b9b344
BB
692
693 if (skc->skc_flags & KMC_OFFSLAB) {
3018bffa
BB
694 tgt_objs = spl_kmem_cache_obj_per_slab;
695 tgt_size = P2ROUNDUP(sizeof (spl_kmem_slab_t), PAGE_SIZE);
696
697 if ((skc->skc_flags & KMC_KMEM) &&
698 (spl_obj_size(skc) > (SPL_MAX_ORDER_NR_PAGES * PAGE_SIZE)))
699 return (-ENOSPC);
e5b9b344
BB
700 } else {
701 sks_size = spl_sks_size(skc);
702 obj_size = spl_obj_size(skc);
3018bffa
BB
703 max_size = (spl_kmem_cache_max_size * 1024 * 1024);
704 tgt_size = (spl_kmem_cache_obj_per_slab * obj_size + sks_size);
e5b9b344
BB
705
706 /*
3018bffa
BB
707 * KMC_KMEM slabs are allocated by __get_free_pages() which
708 * rounds up to the nearest order. Knowing this the size
709 * should be rounded up to the next power of two with a hard
710 * maximum defined by the maximum allowed allocation order.
e5b9b344 711 */
3018bffa
BB
712 if (skc->skc_flags & KMC_KMEM) {
713 max_size = SPL_MAX_ORDER_NR_PAGES * PAGE_SIZE;
714 tgt_size = MIN(max_size,
715 PAGE_SIZE * (1 << MAX(get_order(tgt_size) - 1, 1)));
716 }
717
718 if (tgt_size <= max_size) {
719 tgt_objs = (tgt_size - sks_size) / obj_size;
720 } else {
721 tgt_objs = (max_size - sks_size) / obj_size;
722 tgt_size = (tgt_objs * obj_size) + sks_size;
723 }
e5b9b344
BB
724 }
725
3018bffa
BB
726 if (tgt_objs == 0)
727 return (-ENOSPC);
728
729 *objs = tgt_objs;
730 *size = tgt_size;
731
732 return (0);
e5b9b344
BB
733}
734
735/*
736 * Make a guess at reasonable per-cpu magazine size based on the size of
737 * each object and the cost of caching N of them in each magazine. Long
738 * term this should really adapt based on an observed usage heuristic.
739 */
740static int
741spl_magazine_size(spl_kmem_cache_t *skc)
742{
743 uint32_t obj_size = spl_obj_size(skc);
744 int size;
745
1a204968
BB
746 if (spl_kmem_cache_magazine_size > 0)
747 return (MAX(MIN(spl_kmem_cache_magazine_size, 256), 2));
748
e5b9b344
BB
749 /* Per-magazine sizes below assume a 4Kib page size */
750 if (obj_size > (PAGE_SIZE * 256))
751 size = 4; /* Minimum 4Mib per-magazine */
752 else if (obj_size > (PAGE_SIZE * 32))
753 size = 16; /* Minimum 2Mib per-magazine */
754 else if (obj_size > (PAGE_SIZE))
755 size = 64; /* Minimum 256Kib per-magazine */
756 else if (obj_size > (PAGE_SIZE / 4))
757 size = 128; /* Minimum 128Kib per-magazine */
758 else
759 size = 256;
760
761 return (size);
762}
763
764/*
765 * Allocate a per-cpu magazine to associate with a specific core.
766 */
767static spl_kmem_magazine_t *
768spl_magazine_alloc(spl_kmem_cache_t *skc, int cpu)
769{
770 spl_kmem_magazine_t *skm;
b34b9563
BB
771 int size = sizeof (spl_kmem_magazine_t) +
772 sizeof (void *) * skc->skc_mag_size;
e5b9b344 773
c3eabc75 774 skm = kmalloc_node(size, GFP_KERNEL, cpu_to_node(cpu));
e5b9b344
BB
775 if (skm) {
776 skm->skm_magic = SKM_MAGIC;
777 skm->skm_avail = 0;
778 skm->skm_size = skc->skc_mag_size;
779 skm->skm_refill = skc->skc_mag_refill;
780 skm->skm_cache = skc;
781 skm->skm_age = jiffies;
782 skm->skm_cpu = cpu;
783 }
784
785 return (skm);
786}
787
788/*
789 * Free a per-cpu magazine associated with a specific core.
790 */
791static void
792spl_magazine_free(spl_kmem_magazine_t *skm)
793{
e5b9b344
BB
794 ASSERT(skm->skm_magic == SKM_MAGIC);
795 ASSERT(skm->skm_avail == 0);
c3eabc75 796 kfree(skm);
e5b9b344
BB
797}
798
799/*
800 * Create all pre-cpu magazines of reasonable sizes.
801 */
802static int
803spl_magazine_create(spl_kmem_cache_t *skc)
804{
805 int i;
806
807 if (skc->skc_flags & KMC_NOMAGAZINE)
808 return (0);
809
9b13f65d
BB
810 skc->skc_mag = kzalloc(sizeof (spl_kmem_magazine_t *) *
811 num_possible_cpus(), kmem_flags_convert(KM_SLEEP));
e5b9b344
BB
812 skc->skc_mag_size = spl_magazine_size(skc);
813 skc->skc_mag_refill = (skc->skc_mag_size + 1) / 2;
814
9b13f65d 815 for_each_possible_cpu(i) {
e5b9b344
BB
816 skc->skc_mag[i] = spl_magazine_alloc(skc, i);
817 if (!skc->skc_mag[i]) {
818 for (i--; i >= 0; i--)
819 spl_magazine_free(skc->skc_mag[i]);
820
9b13f65d 821 kfree(skc->skc_mag);
e5b9b344
BB
822 return (-ENOMEM);
823 }
824 }
825
826 return (0);
827}
828
829/*
830 * Destroy all pre-cpu magazines.
831 */
832static void
833spl_magazine_destroy(spl_kmem_cache_t *skc)
834{
835 spl_kmem_magazine_t *skm;
836 int i;
837
838 if (skc->skc_flags & KMC_NOMAGAZINE)
839 return;
840
9b13f65d 841 for_each_possible_cpu(i) {
e5b9b344
BB
842 skm = skc->skc_mag[i];
843 spl_cache_flush(skc, skm, skm->skm_avail);
844 spl_magazine_free(skm);
b34b9563 845 }
9b13f65d
BB
846
847 kfree(skc->skc_mag);
e5b9b344
BB
848}
849
850/*
851 * Create a object cache based on the following arguments:
852 * name cache name
853 * size cache object size
854 * align cache object alignment
855 * ctor cache object constructor
856 * dtor cache object destructor
857 * reclaim cache object reclaim
858 * priv cache private data for ctor/dtor/reclaim
859 * vmp unused must be NULL
860 * flags
0409679d
MA
861 * KMC_KMEM Force SPL kmem backed cache
862 * KMC_VMEM Force SPL vmem backed cache
e5b9b344
BB
863 * KMC_SLAB Force Linux slab backed cache
864 * KMC_OFFSLAB Locate objects off the slab
0409679d
MA
865 * KMC_NOTOUCH unsupported
866 * KMC_NODEBUG unsupported
867 * KMC_NOHASH unsupported
868 * KMC_QCACHE unsupported
869 * KMC_NOMAGAZINE unsupported
e5b9b344
BB
870 */
871spl_kmem_cache_t *
872spl_kmem_cache_create(char *name, size_t size, size_t align,
b34b9563
BB
873 spl_kmem_ctor_t ctor, spl_kmem_dtor_t dtor, spl_kmem_reclaim_t reclaim,
874 void *priv, void *vmp, int flags)
e5b9b344 875{
c3eabc75 876 gfp_t lflags = kmem_flags_convert(KM_SLEEP);
b34b9563 877 spl_kmem_cache_t *skc;
e5b9b344
BB
878 int rc;
879
880 /*
881 * Unsupported flags
882 */
883 ASSERT0(flags & KMC_NOMAGAZINE);
884 ASSERT0(flags & KMC_NOHASH);
885 ASSERT0(flags & KMC_QCACHE);
886 ASSERT(vmp == NULL);
887
888 might_sleep();
889
c3eabc75 890 skc = kzalloc(sizeof (*skc), lflags);
e5b9b344
BB
891 if (skc == NULL)
892 return (NULL);
893
894 skc->skc_magic = SKC_MAGIC;
895 skc->skc_name_size = strlen(name) + 1;
c3eabc75 896 skc->skc_name = (char *)kmalloc(skc->skc_name_size, lflags);
e5b9b344 897 if (skc->skc_name == NULL) {
c3eabc75 898 kfree(skc);
e5b9b344
BB
899 return (NULL);
900 }
901 strncpy(skc->skc_name, name, skc->skc_name_size);
902
903 skc->skc_ctor = ctor;
904 skc->skc_dtor = dtor;
905 skc->skc_reclaim = reclaim;
906 skc->skc_private = priv;
907 skc->skc_vmp = vmp;
908 skc->skc_linux_cache = NULL;
909 skc->skc_flags = flags;
910 skc->skc_obj_size = size;
911 skc->skc_obj_align = SPL_KMEM_CACHE_ALIGN;
912 skc->skc_delay = SPL_KMEM_CACHE_DELAY;
913 skc->skc_reap = SPL_KMEM_CACHE_REAP;
914 atomic_set(&skc->skc_ref, 0);
915
916 INIT_LIST_HEAD(&skc->skc_list);
917 INIT_LIST_HEAD(&skc->skc_complete_list);
918 INIT_LIST_HEAD(&skc->skc_partial_list);
919 skc->skc_emergency_tree = RB_ROOT;
920 spin_lock_init(&skc->skc_lock);
921 init_waitqueue_head(&skc->skc_waitq);
922 skc->skc_slab_fail = 0;
923 skc->skc_slab_create = 0;
924 skc->skc_slab_destroy = 0;
925 skc->skc_slab_total = 0;
926 skc->skc_slab_alloc = 0;
927 skc->skc_slab_max = 0;
928 skc->skc_obj_total = 0;
929 skc->skc_obj_alloc = 0;
930 skc->skc_obj_max = 0;
931 skc->skc_obj_deadlock = 0;
932 skc->skc_obj_emergency = 0;
933 skc->skc_obj_emergency_max = 0;
934
935 /*
936 * Verify the requested alignment restriction is sane.
937 */
938 if (align) {
939 VERIFY(ISP2(align));
940 VERIFY3U(align, >=, SPL_KMEM_CACHE_ALIGN);
941 VERIFY3U(align, <=, PAGE_SIZE);
942 skc->skc_obj_align = align;
943 }
944
945 /*
946 * When no specific type of slab is requested (kmem, vmem, or
947 * linuxslab) then select a cache type based on the object size
948 * and default tunables.
949 */
950 if (!(skc->skc_flags & (KMC_KMEM | KMC_VMEM | KMC_SLAB))) {
951
e5b9b344 952 if (spl_kmem_cache_slab_limit &&
0409679d
MA
953 size <= (size_t)spl_kmem_cache_slab_limit) {
954 /*
955 * Objects smaller than spl_kmem_cache_slab_limit can
956 * use the Linux slab for better space-efficiency.
957 */
e5b9b344 958 skc->skc_flags |= KMC_SLAB;
0409679d
MA
959 } else if (spl_obj_size(skc) <= spl_kmem_cache_kmem_limit) {
960 /*
961 * Small objects, less than spl_kmem_cache_kmem_limit
962 * per object should use kmem because their slabs are
963 * small.
964 */
e5b9b344 965 skc->skc_flags |= KMC_KMEM;
0409679d
MA
966 } else {
967 /*
968 * All other objects are considered large and are
969 * placed on vmem backed slabs.
970 */
e5b9b344 971 skc->skc_flags |= KMC_VMEM;
0409679d 972 }
e5b9b344
BB
973 }
974
975 /*
976 * Given the type of slab allocate the required resources.
977 */
978 if (skc->skc_flags & (KMC_KMEM | KMC_VMEM)) {
979 rc = spl_slab_size(skc,
980 &skc->skc_slab_objs, &skc->skc_slab_size);
981 if (rc)
982 goto out;
983
984 rc = spl_magazine_create(skc);
985 if (rc)
986 goto out;
987 } else {
2ebe3960
BB
988 unsigned long slabflags = 0;
989
3018bffa
BB
990 if (size > (SPL_MAX_KMEM_ORDER_NR_PAGES * PAGE_SIZE)) {
991 rc = EINVAL;
992 goto out;
993 }
994
2ebe3960
BB
995#if defined(SLAB_USERCOPY)
996 /*
997 * Required for PAX-enabled kernels if the slab is to be
998 * used for coping between user and kernel space.
999 */
1000 slabflags |= SLAB_USERCOPY;
1001#endif
1002
0194e4a0 1003#if defined(HAVE_KMEM_CACHE_CREATE_USERCOPY)
0409679d
MA
1004 /*
1005 * Newer grsec patchset uses kmem_cache_create_usercopy()
1006 * instead of SLAB_USERCOPY flag
1007 */
1008 skc->skc_linux_cache = kmem_cache_create_usercopy(
1009 skc->skc_name, size, align, slabflags, 0, size, NULL);
0194e4a0 1010#else
0409679d
MA
1011 skc->skc_linux_cache = kmem_cache_create(
1012 skc->skc_name, size, align, slabflags, NULL);
0194e4a0 1013#endif
e5b9b344
BB
1014 if (skc->skc_linux_cache == NULL) {
1015 rc = ENOMEM;
1016 goto out;
1017 }
1018
c3eabc75
BB
1019#if defined(HAVE_KMEM_CACHE_ALLOCFLAGS)
1020 skc->skc_linux_cache->allocflags |= __GFP_COMP;
1021#elif defined(HAVE_KMEM_CACHE_GFPFLAGS)
1022 skc->skc_linux_cache->gfpflags |= __GFP_COMP;
1023#endif
e5b9b344
BB
1024 skc->skc_flags |= KMC_NOMAGAZINE;
1025 }
1026
0409679d 1027 if (spl_kmem_cache_expire & KMC_EXPIRE_AGE) {
e5b9b344
BB
1028 skc->skc_taskqid = taskq_dispatch_delay(spl_kmem_cache_taskq,
1029 spl_cache_age, skc, TQ_SLEEP,
1030 ddi_get_lbolt() + skc->skc_delay / 3 * HZ);
0409679d 1031 }
e5b9b344
BB
1032
1033 down_write(&spl_kmem_cache_sem);
1034 list_add_tail(&skc->skc_list, &spl_kmem_cache_list);
1035 up_write(&spl_kmem_cache_sem);
1036
1037 return (skc);
1038out:
c3eabc75
BB
1039 kfree(skc->skc_name);
1040 kfree(skc);
e5b9b344
BB
1041 return (NULL);
1042}
1043EXPORT_SYMBOL(spl_kmem_cache_create);
1044
1045/*
b34b9563 1046 * Register a move callback for cache defragmentation.
e5b9b344
BB
1047 * XXX: Unimplemented but harmless to stub out for now.
1048 */
1049void
1050spl_kmem_cache_set_move(spl_kmem_cache_t *skc,
1051 kmem_cbrc_t (move)(void *, void *, size_t, void *))
1052{
b34b9563 1053 ASSERT(move != NULL);
e5b9b344
BB
1054}
1055EXPORT_SYMBOL(spl_kmem_cache_set_move);
1056
1057/*
1058 * Destroy a cache and all objects associated with the cache.
1059 */
1060void
1061spl_kmem_cache_destroy(spl_kmem_cache_t *skc)
1062{
1063 DECLARE_WAIT_QUEUE_HEAD(wq);
1064 taskqid_t id;
1065
1066 ASSERT(skc->skc_magic == SKC_MAGIC);
1067 ASSERT(skc->skc_flags & (KMC_KMEM | KMC_VMEM | KMC_SLAB));
1068
1069 down_write(&spl_kmem_cache_sem);
1070 list_del_init(&skc->skc_list);
1071 up_write(&spl_kmem_cache_sem);
1072
1073 /* Cancel any and wait for any pending delayed tasks */
1074 VERIFY(!test_and_set_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1075
1076 spin_lock(&skc->skc_lock);
1077 id = skc->skc_taskqid;
1078 spin_unlock(&skc->skc_lock);
1079
1080 taskq_cancel_id(spl_kmem_cache_taskq, id);
1081
b34b9563
BB
1082 /*
1083 * Wait until all current callers complete, this is mainly
e5b9b344 1084 * to catch the case where a low memory situation triggers a
b34b9563
BB
1085 * cache reaping action which races with this destroy.
1086 */
e5b9b344
BB
1087 wait_event(wq, atomic_read(&skc->skc_ref) == 0);
1088
1089 if (skc->skc_flags & (KMC_KMEM | KMC_VMEM)) {
1090 spl_magazine_destroy(skc);
1a204968 1091 spl_slab_reclaim(skc);
e5b9b344
BB
1092 } else {
1093 ASSERT(skc->skc_flags & KMC_SLAB);
1094 kmem_cache_destroy(skc->skc_linux_cache);
1095 }
1096
1097 spin_lock(&skc->skc_lock);
1098
b34b9563
BB
1099 /*
1100 * Validate there are no objects in use and free all the
1101 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers.
1102 */
e5b9b344
BB
1103 ASSERT3U(skc->skc_slab_alloc, ==, 0);
1104 ASSERT3U(skc->skc_obj_alloc, ==, 0);
1105 ASSERT3U(skc->skc_slab_total, ==, 0);
1106 ASSERT3U(skc->skc_obj_total, ==, 0);
1107 ASSERT3U(skc->skc_obj_emergency, ==, 0);
1108 ASSERT(list_empty(&skc->skc_complete_list));
1109
e5b9b344
BB
1110 spin_unlock(&skc->skc_lock);
1111
c3eabc75
BB
1112 kfree(skc->skc_name);
1113 kfree(skc);
e5b9b344
BB
1114}
1115EXPORT_SYMBOL(spl_kmem_cache_destroy);
1116
1117/*
1118 * Allocate an object from a slab attached to the cache. This is used to
1119 * repopulate the per-cpu magazine caches in batches when they run low.
1120 */
1121static void *
1122spl_cache_obj(spl_kmem_cache_t *skc, spl_kmem_slab_t *sks)
1123{
1124 spl_kmem_obj_t *sko;
1125
1126 ASSERT(skc->skc_magic == SKC_MAGIC);
1127 ASSERT(sks->sks_magic == SKS_MAGIC);
e5b9b344
BB
1128
1129 sko = list_entry(sks->sks_free_list.next, spl_kmem_obj_t, sko_list);
1130 ASSERT(sko->sko_magic == SKO_MAGIC);
1131 ASSERT(sko->sko_addr != NULL);
1132
1133 /* Remove from sks_free_list */
1134 list_del_init(&sko->sko_list);
1135
1136 sks->sks_age = jiffies;
1137 sks->sks_ref++;
1138 skc->skc_obj_alloc++;
1139
1140 /* Track max obj usage statistics */
1141 if (skc->skc_obj_alloc > skc->skc_obj_max)
1142 skc->skc_obj_max = skc->skc_obj_alloc;
1143
1144 /* Track max slab usage statistics */
1145 if (sks->sks_ref == 1) {
1146 skc->skc_slab_alloc++;
1147
1148 if (skc->skc_slab_alloc > skc->skc_slab_max)
1149 skc->skc_slab_max = skc->skc_slab_alloc;
1150 }
1151
b34b9563 1152 return (sko->sko_addr);
e5b9b344
BB
1153}
1154
1155/*
1156 * Generic slab allocation function to run by the global work queues.
1157 * It is responsible for allocating a new slab, linking it in to the list
1158 * of partial slabs, and then waking any waiters.
1159 */
16fc1ec3
JX
1160static int
1161__spl_cache_grow(spl_kmem_cache_t *skc, int flags)
e5b9b344 1162{
e5b9b344
BB
1163 spl_kmem_slab_t *sks;
1164
c2fa0945 1165 fstrans_cookie_t cookie = spl_fstrans_mark();
16fc1ec3 1166 sks = spl_slab_alloc(skc, flags);
c2fa0945 1167 spl_fstrans_unmark(cookie);
b4ad50ac 1168
e5b9b344
BB
1169 spin_lock(&skc->skc_lock);
1170 if (sks) {
1171 skc->skc_slab_total++;
1172 skc->skc_obj_total += sks->sks_objs;
1173 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
16fc1ec3
JX
1174
1175 smp_mb__before_atomic();
1176 clear_bit(KMC_BIT_DEADLOCKED, &skc->skc_flags);
1177 smp_mb__after_atomic();
1178 wake_up_all(&skc->skc_waitq);
e5b9b344 1179 }
16fc1ec3
JX
1180 spin_unlock(&skc->skc_lock);
1181
1182 return (sks == NULL ? -ENOMEM : 0);
1183}
1184
1185static void
1186spl_cache_grow_work(void *data)
1187{
1188 spl_kmem_alloc_t *ska = (spl_kmem_alloc_t *)data;
1189 spl_kmem_cache_t *skc = ska->ska_cache;
1190
5461eefe 1191 (void) __spl_cache_grow(skc, ska->ska_flags);
e5b9b344
BB
1192
1193 atomic_dec(&skc->skc_ref);
a988a35a 1194 smp_mb__before_atomic();
e5b9b344 1195 clear_bit(KMC_BIT_GROWING, &skc->skc_flags);
a988a35a 1196 smp_mb__after_atomic();
e5b9b344
BB
1197
1198 kfree(ska);
1199}
1200
1201/*
1202 * Returns non-zero when a new slab should be available.
1203 */
1204static int
1205spl_cache_grow_wait(spl_kmem_cache_t *skc)
1206{
b34b9563 1207 return (!test_bit(KMC_BIT_GROWING, &skc->skc_flags));
e5b9b344
BB
1208}
1209
1210/*
1211 * No available objects on any slabs, create a new slab. Note that this
1212 * functionality is disabled for KMC_SLAB caches which are backed by the
1213 * Linux slab.
1214 */
1215static int
1216spl_cache_grow(spl_kmem_cache_t *skc, int flags, void **obj)
1217{
c3eabc75 1218 int remaining, rc = 0;
e5b9b344 1219
c3eabc75 1220 ASSERT0(flags & ~KM_PUBLIC_MASK);
e5b9b344
BB
1221 ASSERT(skc->skc_magic == SKC_MAGIC);
1222 ASSERT((skc->skc_flags & KMC_SLAB) == 0);
1223 might_sleep();
1224 *obj = NULL;
1225
1226 /*
1227 * Before allocating a new slab wait for any reaping to complete and
1228 * then return so the local magazine can be rechecked for new objects.
1229 */
1230 if (test_bit(KMC_BIT_REAPING, &skc->skc_flags)) {
1231 rc = spl_wait_on_bit(&skc->skc_flags, KMC_BIT_REAPING,
1232 TASK_UNINTERRUPTIBLE);
1233 return (rc ? rc : -EAGAIN);
1234 }
1235
16fc1ec3
JX
1236 /*
1237 * To reduce the overhead of context switch and improve NUMA locality,
1238 * it tries to allocate a new slab in the current process context with
1239 * KM_NOSLEEP flag. If it fails, it will launch a new taskq to do the
1240 * allocation.
1241 *
1242 * However, this can't be applied to KVM_VMEM due to a bug that
1243 * __vmalloc() doesn't honor gfp flags in page table allocation.
1244 */
1245 if (!(skc->skc_flags & KMC_VMEM)) {
1246 rc = __spl_cache_grow(skc, flags | KM_NOSLEEP);
1247 if (rc == 0)
1248 return (0);
1249 }
1250
e5b9b344
BB
1251 /*
1252 * This is handled by dispatching a work request to the global work
1253 * queue. This allows us to asynchronously allocate a new slab while
1254 * retaining the ability to safely fall back to a smaller synchronous
1255 * allocations to ensure forward progress is always maintained.
1256 */
1257 if (test_and_set_bit(KMC_BIT_GROWING, &skc->skc_flags) == 0) {
1258 spl_kmem_alloc_t *ska;
1259
c3eabc75 1260 ska = kmalloc(sizeof (*ska), kmem_flags_convert(flags));
e5b9b344 1261 if (ska == NULL) {
a988a35a
RY
1262 clear_bit_unlock(KMC_BIT_GROWING, &skc->skc_flags);
1263 smp_mb__after_atomic();
e5b9b344
BB
1264 wake_up_all(&skc->skc_waitq);
1265 return (-ENOMEM);
1266 }
1267
1268 atomic_inc(&skc->skc_ref);
1269 ska->ska_cache = skc;
c3eabc75 1270 ska->ska_flags = flags;
e5b9b344
BB
1271 taskq_init_ent(&ska->ska_tqe);
1272 taskq_dispatch_ent(spl_kmem_cache_taskq,
1273 spl_cache_grow_work, ska, 0, &ska->ska_tqe);
1274 }
1275
1276 /*
1277 * The goal here is to only detect the rare case where a virtual slab
1278 * allocation has deadlocked. We must be careful to minimize the use
1279 * of emergency objects which are more expensive to track. Therefore,
1280 * we set a very long timeout for the asynchronous allocation and if
1281 * the timeout is reached the cache is flagged as deadlocked. From
1282 * this point only new emergency objects will be allocated until the
1283 * asynchronous allocation completes and clears the deadlocked flag.
1284 */
1285 if (test_bit(KMC_BIT_DEADLOCKED, &skc->skc_flags)) {
1286 rc = spl_emergency_alloc(skc, flags, obj);
1287 } else {
1288 remaining = wait_event_timeout(skc->skc_waitq,
e50e6cc9 1289 spl_cache_grow_wait(skc), HZ / 10);
e5b9b344 1290
436ad60f 1291 if (!remaining) {
e5b9b344
BB
1292 spin_lock(&skc->skc_lock);
1293 if (test_bit(KMC_BIT_GROWING, &skc->skc_flags)) {
1294 set_bit(KMC_BIT_DEADLOCKED, &skc->skc_flags);
1295 skc->skc_obj_deadlock++;
1296 }
1297 spin_unlock(&skc->skc_lock);
1298 }
1299
1300 rc = -ENOMEM;
1301 }
1302
1303 return (rc);
1304}
1305
1306/*
1307 * Refill a per-cpu magazine with objects from the slabs for this cache.
1308 * Ideally the magazine can be repopulated using existing objects which have
1309 * been released, however if we are unable to locate enough free objects new
1310 * slabs of objects will be created. On success NULL is returned, otherwise
1311 * the address of a single emergency object is returned for use by the caller.
1312 */
1313static void *
1314spl_cache_refill(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flags)
1315{
1316 spl_kmem_slab_t *sks;
1317 int count = 0, rc, refill;
1318 void *obj = NULL;
1319
1320 ASSERT(skc->skc_magic == SKC_MAGIC);
1321 ASSERT(skm->skm_magic == SKM_MAGIC);
1322
1323 refill = MIN(skm->skm_refill, skm->skm_size - skm->skm_avail);
1324 spin_lock(&skc->skc_lock);
1325
1326 while (refill > 0) {
1327 /* No slabs available we may need to grow the cache */
1328 if (list_empty(&skc->skc_partial_list)) {
1329 spin_unlock(&skc->skc_lock);
1330
1331 local_irq_enable();
1332 rc = spl_cache_grow(skc, flags, &obj);
1333 local_irq_disable();
1334
1335 /* Emergency object for immediate use by caller */
1336 if (rc == 0 && obj != NULL)
1337 return (obj);
1338
1339 if (rc)
1340 goto out;
1341
1342 /* Rescheduled to different CPU skm is not local */
1343 if (skm != skc->skc_mag[smp_processor_id()])
1344 goto out;
1345
b34b9563
BB
1346 /*
1347 * Potentially rescheduled to the same CPU but
e5b9b344 1348 * allocations may have occurred from this CPU while
b34b9563
BB
1349 * we were sleeping so recalculate max refill.
1350 */
e5b9b344
BB
1351 refill = MIN(refill, skm->skm_size - skm->skm_avail);
1352
1353 spin_lock(&skc->skc_lock);
1354 continue;
1355 }
1356
1357 /* Grab the next available slab */
1358 sks = list_entry((&skc->skc_partial_list)->next,
b34b9563 1359 spl_kmem_slab_t, sks_list);
e5b9b344
BB
1360 ASSERT(sks->sks_magic == SKS_MAGIC);
1361 ASSERT(sks->sks_ref < sks->sks_objs);
1362 ASSERT(!list_empty(&sks->sks_free_list));
1363
b34b9563
BB
1364 /*
1365 * Consume as many objects as needed to refill the requested
1366 * cache. We must also be careful not to overfill it.
1367 */
1368 while (sks->sks_ref < sks->sks_objs && refill-- > 0 &&
1369 ++count) {
e5b9b344
BB
1370 ASSERT(skm->skm_avail < skm->skm_size);
1371 ASSERT(count < skm->skm_size);
b34b9563
BB
1372 skm->skm_objs[skm->skm_avail++] =
1373 spl_cache_obj(skc, sks);
e5b9b344
BB
1374 }
1375
1376 /* Move slab to skc_complete_list when full */
1377 if (sks->sks_ref == sks->sks_objs) {
1378 list_del(&sks->sks_list);
1379 list_add(&sks->sks_list, &skc->skc_complete_list);
1380 }
1381 }
1382
1383 spin_unlock(&skc->skc_lock);
1384out:
1385 return (NULL);
1386}
1387
1388/*
1389 * Release an object back to the slab from which it came.
1390 */
1391static void
1392spl_cache_shrink(spl_kmem_cache_t *skc, void *obj)
1393{
1394 spl_kmem_slab_t *sks = NULL;
1395 spl_kmem_obj_t *sko = NULL;
1396
1397 ASSERT(skc->skc_magic == SKC_MAGIC);
e5b9b344
BB
1398
1399 sko = spl_sko_from_obj(skc, obj);
1400 ASSERT(sko->sko_magic == SKO_MAGIC);
1401 sks = sko->sko_slab;
1402 ASSERT(sks->sks_magic == SKS_MAGIC);
1403 ASSERT(sks->sks_cache == skc);
1404 list_add(&sko->sko_list, &sks->sks_free_list);
1405
1406 sks->sks_age = jiffies;
1407 sks->sks_ref--;
1408 skc->skc_obj_alloc--;
1409
b34b9563
BB
1410 /*
1411 * Move slab to skc_partial_list when no longer full. Slabs
e5b9b344 1412 * are added to the head to keep the partial list is quasi-full
b34b9563
BB
1413 * sorted order. Fuller at the head, emptier at the tail.
1414 */
e5b9b344
BB
1415 if (sks->sks_ref == (sks->sks_objs - 1)) {
1416 list_del(&sks->sks_list);
1417 list_add(&sks->sks_list, &skc->skc_partial_list);
1418 }
1419
b34b9563
BB
1420 /*
1421 * Move empty slabs to the end of the partial list so
1422 * they can be easily found and freed during reclamation.
1423 */
e5b9b344
BB
1424 if (sks->sks_ref == 0) {
1425 list_del(&sks->sks_list);
1426 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
1427 skc->skc_slab_alloc--;
1428 }
1429}
1430
1431/*
1432 * Allocate an object from the per-cpu magazine, or if the magazine
1433 * is empty directly allocate from a slab and repopulate the magazine.
1434 */
1435void *
1436spl_kmem_cache_alloc(spl_kmem_cache_t *skc, int flags)
1437{
1438 spl_kmem_magazine_t *skm;
1439 void *obj = NULL;
1440
c3eabc75 1441 ASSERT0(flags & ~KM_PUBLIC_MASK);
e5b9b344
BB
1442 ASSERT(skc->skc_magic == SKC_MAGIC);
1443 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
e5b9b344 1444
e5b9b344
BB
1445 /*
1446 * Allocate directly from a Linux slab. All optimizations are left
1447 * to the underlying cache we only need to guarantee that KM_SLEEP
1448 * callers will never fail.
1449 */
1450 if (skc->skc_flags & KMC_SLAB) {
1451 struct kmem_cache *slc = skc->skc_linux_cache;
e5b9b344 1452 do {
c3eabc75 1453 obj = kmem_cache_alloc(slc, kmem_flags_convert(flags));
e5b9b344
BB
1454 } while ((obj == NULL) && !(flags & KM_NOSLEEP));
1455
1456 goto ret;
1457 }
1458
1459 local_irq_disable();
1460
1461restart:
b34b9563
BB
1462 /*
1463 * Safe to update per-cpu structure without lock, but
e5b9b344
BB
1464 * in the restart case we must be careful to reacquire
1465 * the local magazine since this may have changed
b34b9563
BB
1466 * when we need to grow the cache.
1467 */
e5b9b344
BB
1468 skm = skc->skc_mag[smp_processor_id()];
1469 ASSERT(skm->skm_magic == SKM_MAGIC);
1470
1471 if (likely(skm->skm_avail)) {
1472 /* Object available in CPU cache, use it */
1473 obj = skm->skm_objs[--skm->skm_avail];
1474 skm->skm_age = jiffies;
1475 } else {
1476 obj = spl_cache_refill(skc, skm, flags);
3018bffa 1477 if ((obj == NULL) && !(flags & KM_NOSLEEP))
e5b9b344 1478 goto restart;
3018bffa
BB
1479
1480 local_irq_enable();
1481 goto ret;
e5b9b344
BB
1482 }
1483
1484 local_irq_enable();
1485 ASSERT(obj);
1486 ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align));
1487
1488ret:
1489 /* Pre-emptively migrate object to CPU L1 cache */
1490 if (obj) {
1491 if (obj && skc->skc_ctor)
1492 skc->skc_ctor(obj, skc->skc_private, flags);
1493 else
1494 prefetchw(obj);
1495 }
1496
e5b9b344
BB
1497 return (obj);
1498}
e5b9b344
BB
1499EXPORT_SYMBOL(spl_kmem_cache_alloc);
1500
1501/*
1502 * Free an object back to the local per-cpu magazine, there is no
1503 * guarantee that this is the same magazine the object was originally
1504 * allocated from. We may need to flush entire from the magazine
1505 * back to the slabs to make space.
1506 */
1507void
1508spl_kmem_cache_free(spl_kmem_cache_t *skc, void *obj)
1509{
1510 spl_kmem_magazine_t *skm;
1511 unsigned long flags;
1a204968 1512 int do_reclaim = 0;
436ad60f 1513 int do_emergency = 0;
e5b9b344
BB
1514
1515 ASSERT(skc->skc_magic == SKC_MAGIC);
1516 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
e5b9b344
BB
1517
1518 /*
1519 * Run the destructor
1520 */
1521 if (skc->skc_dtor)
1522 skc->skc_dtor(obj, skc->skc_private);
1523
1524 /*
1525 * Free the object from the Linux underlying Linux slab.
1526 */
1527 if (skc->skc_flags & KMC_SLAB) {
1528 kmem_cache_free(skc->skc_linux_cache, obj);
4699d76d 1529 return;
e5b9b344
BB
1530 }
1531
1532 /*
436ad60f
BB
1533 * While a cache has outstanding emergency objects all freed objects
1534 * must be checked. However, since emergency objects will never use
1535 * a virtual address these objects can be safely excluded as an
1536 * optimization.
e5b9b344 1537 */
436ad60f
BB
1538 if (!is_vmalloc_addr(obj)) {
1539 spin_lock(&skc->skc_lock);
1540 do_emergency = (skc->skc_obj_emergency > 0);
1541 spin_unlock(&skc->skc_lock);
1542
1543 if (do_emergency && (spl_emergency_free(skc, obj) == 0))
4699d76d 1544 return;
e5b9b344
BB
1545 }
1546
1547 local_irq_save(flags);
1548
b34b9563
BB
1549 /*
1550 * Safe to update per-cpu structure without lock, but
e5b9b344
BB
1551 * no remote memory allocation tracking is being performed
1552 * it is entirely possible to allocate an object from one
b34b9563
BB
1553 * CPU cache and return it to another.
1554 */
e5b9b344
BB
1555 skm = skc->skc_mag[smp_processor_id()];
1556 ASSERT(skm->skm_magic == SKM_MAGIC);
1557
1a204968
BB
1558 /*
1559 * Per-CPU cache full, flush it to make space for this object,
1560 * this may result in an empty slab which can be reclaimed once
1561 * interrupts are re-enabled.
1562 */
1563 if (unlikely(skm->skm_avail >= skm->skm_size)) {
e5b9b344 1564 spl_cache_flush(skc, skm, skm->skm_refill);
1a204968
BB
1565 do_reclaim = 1;
1566 }
e5b9b344
BB
1567
1568 /* Available space in cache, use it */
1569 skm->skm_objs[skm->skm_avail++] = obj;
1570
1571 local_irq_restore(flags);
1a204968
BB
1572
1573 if (do_reclaim)
1574 spl_slab_reclaim(skc);
e5b9b344
BB
1575}
1576EXPORT_SYMBOL(spl_kmem_cache_free);
1577
1578/*
1579 * The generic shrinker function for all caches. Under Linux a shrinker
1580 * may not be tightly coupled with a slab cache. In fact Linux always
1581 * systematically tries calling all registered shrinker callbacks which
1582 * report that they contain unused objects. Because of this we only
1583 * register one shrinker function in the shim layer for all slab caches.
1584 * We always attempt to shrink all caches when this generic shrinker
1585 * is called.
1586 *
1587 * If sc->nr_to_scan is zero, the caller is requesting a query of the
1588 * number of objects which can potentially be freed. If it is nonzero,
1589 * the request is to free that many objects.
1590 *
1591 * Linux kernels >= 3.12 have the count_objects and scan_objects callbacks
1592 * in struct shrinker and also require the shrinker to return the number
1593 * of objects freed.
1594 *
1595 * Older kernels require the shrinker to return the number of freeable
1596 * objects following the freeing of nr_to_free.
1597 *
1598 * Linux semantics differ from those under Solaris, which are to
1599 * free all available objects which may (and probably will) be more
1600 * objects than the requested nr_to_scan.
1601 */
1602static spl_shrinker_t
1603__spl_kmem_cache_generic_shrinker(struct shrinker *shrink,
1604 struct shrink_control *sc)
1605{
1606 spl_kmem_cache_t *skc;
1607 int alloc = 0;
1608
ae26dd00
TC
1609 /*
1610 * No shrinking in a transaction context. Can cause deadlocks.
1611 */
1612 if (sc->nr_to_scan && spl_fstrans_check())
1613 return (SHRINK_STOP);
1614
e5b9b344
BB
1615 down_read(&spl_kmem_cache_sem);
1616 list_for_each_entry(skc, &spl_kmem_cache_list, skc_list) {
1617 if (sc->nr_to_scan) {
1618#ifdef HAVE_SPLIT_SHRINKER_CALLBACK
1619 uint64_t oldalloc = skc->skc_obj_alloc;
1620 spl_kmem_cache_reap_now(skc,
b34b9563 1621 MAX(sc->nr_to_scan>>fls64(skc->skc_slab_objs), 1));
e5b9b344
BB
1622 if (oldalloc > skc->skc_obj_alloc)
1623 alloc += oldalloc - skc->skc_obj_alloc;
1624#else
1625 spl_kmem_cache_reap_now(skc,
b34b9563 1626 MAX(sc->nr_to_scan>>fls64(skc->skc_slab_objs), 1));
e5b9b344
BB
1627 alloc += skc->skc_obj_alloc;
1628#endif /* HAVE_SPLIT_SHRINKER_CALLBACK */
1629 } else {
1630 /* Request to query number of freeable objects */
1631 alloc += skc->skc_obj_alloc;
1632 }
1633 }
1634 up_read(&spl_kmem_cache_sem);
1635
1636 /*
1637 * When KMC_RECLAIM_ONCE is set allow only a single reclaim pass.
1638 * This functionality only exists to work around a rare issue where
1639 * shrink_slabs() is repeatedly invoked by many cores causing the
1640 * system to thrash.
1641 */
1642 if ((spl_kmem_cache_reclaim & KMC_RECLAIM_ONCE) && sc->nr_to_scan)
1643 return (SHRINK_STOP);
1644
1645 return (MAX(alloc, 0));
1646}
1647
1648SPL_SHRINKER_CALLBACK_WRAPPER(spl_kmem_cache_generic_shrinker);
1649
1650/*
1651 * Call the registered reclaim function for a cache. Depending on how
1652 * many and which objects are released it may simply repopulate the
1653 * local magazine which will then need to age-out. Objects which cannot
1654 * fit in the magazine we will be released back to their slabs which will
1655 * also need to age out before being release. This is all just best
1656 * effort and we do not want to thrash creating and destroying slabs.
1657 */
1658void
1659spl_kmem_cache_reap_now(spl_kmem_cache_t *skc, int count)
1660{
1661 ASSERT(skc->skc_magic == SKC_MAGIC);
1662 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1663
1664 atomic_inc(&skc->skc_ref);
1665
1666 /*
3e7e6f34 1667 * Execute the registered reclaim callback if it exists.
e5b9b344
BB
1668 */
1669 if (skc->skc_flags & KMC_SLAB) {
1670 if (skc->skc_reclaim)
1671 skc->skc_reclaim(skc->skc_private);
e5b9b344
BB
1672 goto out;
1673 }
1674
1675 /*
1676 * Prevent concurrent cache reaping when contended.
1677 */
1678 if (test_and_set_bit(KMC_BIT_REAPING, &skc->skc_flags))
1679 goto out;
1680
1681 /*
1682 * When a reclaim function is available it may be invoked repeatedly
1683 * until at least a single slab can be freed. This ensures that we
1684 * do free memory back to the system. This helps minimize the chance
1685 * of an OOM event when the bulk of memory is used by the slab.
1686 *
1687 * When free slabs are already available the reclaim callback will be
1688 * skipped. Additionally, if no forward progress is detected despite
1689 * a reclaim function the cache will be skipped to avoid deadlock.
1690 *
1691 * Longer term this would be the correct place to add the code which
1692 * repacks the slabs in order minimize fragmentation.
1693 */
1694 if (skc->skc_reclaim) {
1695 uint64_t objects = UINT64_MAX;
1696 int do_reclaim;
1697
1698 do {
1699 spin_lock(&skc->skc_lock);
1700 do_reclaim =
1701 (skc->skc_slab_total > 0) &&
b34b9563 1702 ((skc->skc_slab_total-skc->skc_slab_alloc) == 0) &&
e5b9b344
BB
1703 (skc->skc_obj_alloc < objects);
1704
1705 objects = skc->skc_obj_alloc;
1706 spin_unlock(&skc->skc_lock);
1707
1708 if (do_reclaim)
1709 skc->skc_reclaim(skc->skc_private);
1710
1711 } while (do_reclaim);
1712 }
1713
1a204968 1714 /* Reclaim from the magazine and free all now empty slabs. */
e5b9b344
BB
1715 if (spl_kmem_cache_expire & KMC_EXPIRE_MEM) {
1716 spl_kmem_magazine_t *skm;
1717 unsigned long irq_flags;
1718
1719 local_irq_save(irq_flags);
1720 skm = skc->skc_mag[smp_processor_id()];
1721 spl_cache_flush(skc, skm, skm->skm_avail);
1722 local_irq_restore(irq_flags);
1723 }
1724
1a204968 1725 spl_slab_reclaim(skc);
a988a35a
RY
1726 clear_bit_unlock(KMC_BIT_REAPING, &skc->skc_flags);
1727 smp_mb__after_atomic();
e5b9b344
BB
1728 wake_up_bit(&skc->skc_flags, KMC_BIT_REAPING);
1729out:
1730 atomic_dec(&skc->skc_ref);
1731}
1732EXPORT_SYMBOL(spl_kmem_cache_reap_now);
1733
3ec34e55
BL
1734/*
1735 * This is stubbed out for code consistency with other platforms. There
1736 * is existing logic to prevent concurrent reaping so while this is ugly
1737 * it should do no harm.
1738 */
1739int
1740spl_kmem_cache_reap_active()
1741{
1742 return (0);
1743}
1744EXPORT_SYMBOL(spl_kmem_cache_reap_active);
1745
e5b9b344
BB
1746/*
1747 * Reap all free slabs from all registered caches.
1748 */
1749void
1750spl_kmem_reap(void)
1751{
1752 struct shrink_control sc;
1753
1754 sc.nr_to_scan = KMC_REAP_CHUNK;
1755 sc.gfp_mask = GFP_KERNEL;
1756
1757 (void) __spl_kmem_cache_generic_shrinker(NULL, &sc);
1758}
1759EXPORT_SYMBOL(spl_kmem_reap);
1760
1761int
1762spl_kmem_cache_init(void)
1763{
1764 init_rwsem(&spl_kmem_cache_sem);
1765 INIT_LIST_HEAD(&spl_kmem_cache_list);
1766 spl_kmem_cache_taskq = taskq_create("spl_kmem_cache",
9dc5ffbe 1767 spl_kmem_cache_kmem_threads, maxclsyspri,
3c82160f
BB
1768 spl_kmem_cache_kmem_threads * 8, INT_MAX,
1769 TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
e5b9b344
BB
1770 spl_register_shrinker(&spl_kmem_cache_shrinker);
1771
1772 return (0);
1773}
1774
1775void
1776spl_kmem_cache_fini(void)
1777{
1778 spl_unregister_shrinker(&spl_kmem_cache_shrinker);
1779 taskq_destroy(spl_kmem_cache_taskq);
1780}