]> git.proxmox.com Git - mirror_spl-debian.git/blame - modules/spl/spl-kmem.c
Implement per-cpu local caches. This seems to have bough me another
[mirror_spl-debian.git] / modules / spl / spl-kmem.c
CommitLineData
715f6251 1/*
2 * This file is part of the SPL: Solaris Porting Layer.
3 *
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
6 * Written by:
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
10 * UCRL-CODE-235197
11 *
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
f4b37741 27#include <sys/kmem.h>
f1ca4da6 28
937879f1 29#ifdef DEBUG_SUBSYSTEM
30#undef DEBUG_SUBSYSTEM
31#endif
32
33#define DEBUG_SUBSYSTEM S_KMEM
34
f1ca4da6 35/*
2fb9b26a 36 * Memory allocation interfaces and debugging for basic kmem_*
37 * and vmem_* style memory allocation. When DEBUG_KMEM is enable
38 * all allocations will be tracked when they are allocated and
39 * freed. When the SPL module is unload a list of all leaked
40 * addresses and where they were allocated will be dumped to the
41 * console. Enabling this feature has a significant impant on
42 * performance but it makes finding memory leaks staight forward.
f1ca4da6 43 */
44#ifdef DEBUG_KMEM
45/* Shim layer memory accounting */
c19c06f3 46atomic64_t kmem_alloc_used;
47unsigned long kmem_alloc_max = 0;
48atomic64_t vmem_alloc_used;
49unsigned long vmem_alloc_max = 0;
50int kmem_warning_flag = 1;
5c2bb9b2 51atomic64_t kmem_cache_alloc_failed;
79b31f36 52
d6a26c6a 53spinlock_t kmem_lock;
54struct hlist_head kmem_table[KMEM_TABLE_SIZE];
55struct list_head kmem_list;
56
13cdca65 57spinlock_t vmem_lock;
58struct hlist_head vmem_table[VMEM_TABLE_SIZE];
59struct list_head vmem_list;
60
79b31f36 61EXPORT_SYMBOL(kmem_alloc_used);
62EXPORT_SYMBOL(kmem_alloc_max);
63EXPORT_SYMBOL(vmem_alloc_used);
64EXPORT_SYMBOL(vmem_alloc_max);
c19c06f3 65EXPORT_SYMBOL(kmem_warning_flag);
66
d6a26c6a 67EXPORT_SYMBOL(kmem_lock);
68EXPORT_SYMBOL(kmem_table);
69EXPORT_SYMBOL(kmem_list);
70
13cdca65 71EXPORT_SYMBOL(vmem_lock);
72EXPORT_SYMBOL(vmem_table);
73EXPORT_SYMBOL(vmem_list);
74
c19c06f3 75int kmem_set_warning(int flag) { return (kmem_warning_flag = !!flag); }
76#else
77int kmem_set_warning(int flag) { return 0; }
f1ca4da6 78#endif
c19c06f3 79EXPORT_SYMBOL(kmem_set_warning);
f1ca4da6 80
81/*
82 * Slab allocation interfaces
83 *
2fb9b26a 84 * While the Linux slab implementation was inspired by the Solaris
85 * implemenation I cannot use it to emulate the Solaris APIs. I
86 * require two features which are not provided by the Linux slab.
87 *
88 * 1) Constructors AND destructors. Recent versions of the Linux
89 * kernel have removed support for destructors. This is a deal
90 * breaker for the SPL which contains particularly expensive
91 * initializers for mutex's, condition variables, etc. We also
92 * require a minimal level of cleaner for these data types unlike
93 * may Linux data type which do need to be explicitly destroyed.
94 *
95 * 2) Virtual address backed slab. Callers of the Solaris slab
96 * expect it to work well for both small are very large allocations.
97 * Because of memory fragmentation the Linux slab which is backed
98 * by kmalloc'ed memory performs very badly when confronted with
99 * large numbers of large allocations. Basing the slab on the
100 * virtual address space removes the need for contigeous pages
101 * and greatly improve performance for large allocations.
102 *
103 * For these reasons, the SPL has its own slab implementation with
104 * the needed features. It is not as highly optimized as either the
105 * Solaris or Linux slabs, but it should get me most of what is
106 * needed until it can be optimized or obsoleted by another approach.
107 *
108 * One serious concern I do have about this method is the relatively
109 * small virtual address space on 32bit arches. This will seriously
110 * constrain the size of the slab caches and their performance.
111 *
2fb9b26a 112 * XXX: Implement SPL proc interface to export full per cache stats.
113 *
114 * XXX: Implement work requests to keep an eye on each cache and
4afaaefa 115 * shrink them via spl_slab_reclaim() when they are wasting lots
2fb9b26a 116 * of space. Currently this process is driven by the reapers.
117 *
118 * XXX: Implement proper small cache object support by embedding
119 * the spl_kmem_slab_t, spl_kmem_obj_t's, and objects in the
120 * allocated for a particular slab.
121 *
122 * XXX: Implement a resizable used object hash. Currently the hash
123 * is statically sized for thousands of objects but it should
124 * grow based on observed worst case slab depth.
125 *
126 * XXX: Improve the partial slab list by carefully maintaining a
127 * strict ordering of fullest to emptiest slabs based on
128 * the slab reference count. This gaurentees the when freeing
129 * slabs back to the system we need only linearly traverse the
130 * last N slabs in the list to discover all the freeable slabs.
131 *
132 * XXX: NUMA awareness for optionally allocating memory close to a
133 * particular core. This can be adventageous if you know the slab
134 * object will be short lived and primarily accessed from one core.
135 *
136 * XXX: Slab coloring may also yield performance improvements and would
137 * be desirable to implement.
4afaaefa 138 *
139 * XXX: Proper hardware cache alignment would be good too.
f1ca4da6 140 */
2fb9b26a 141
142/* Ensure the __kmem_cache_create/__kmem_cache_destroy macros are
143 * removed here to prevent a recursive substitution, we want to call
144 * the native linux version.
145 */
146#undef kmem_cache_t
147#undef kmem_cache_create
148#undef kmem_cache_destroy
149#undef kmem_cache_alloc
150#undef kmem_cache_free
151
152static struct list_head spl_kmem_cache_list; /* List of caches */
153static struct rw_semaphore spl_kmem_cache_sem; /* Cache list lock */
154static kmem_cache_t *spl_slab_cache; /* Cache for slab structs */
155static kmem_cache_t *spl_obj_cache; /* Cache for obj structs */
c30df9c8 156
4afaaefa 157static int spl_cache_flush(spl_kmem_cache_t *skc,
158 spl_kmem_magazine_t *skm, int flush);
159
57d86234 160#ifdef HAVE_SET_SHRINKER
2fb9b26a 161static struct shrinker *spl_kmem_cache_shrinker;
57d86234 162#else
4afaaefa 163static int spl_kmem_cache_generic_shrinker(int nr_to_scan,
164 unsigned int gfp_mask);
2fb9b26a 165static struct shrinker spl_kmem_cache_shrinker = {
4afaaefa 166 .shrink = spl_kmem_cache_generic_shrinker,
57d86234 167 .seeks = KMC_DEFAULT_SEEKS,
168};
169#endif
f1ca4da6 170
2fb9b26a 171static spl_kmem_slab_t *
4afaaefa 172spl_slab_alloc(spl_kmem_cache_t *skc, int flags) {
2fb9b26a 173 spl_kmem_slab_t *sks;
174 spl_kmem_obj_t *sko, *n;
175 int i;
176 ENTRY;
f1ca4da6 177
2fb9b26a 178 sks = kmem_cache_alloc(spl_slab_cache, flags);
179 if (sks == NULL)
180 RETURN(sks);
181
182 sks->sks_magic = SKS_MAGIC;
183 sks->sks_objs = SPL_KMEM_CACHE_OBJ_PER_SLAB;
184 sks->sks_age = jiffies;
185 sks->sks_cache = skc;
186 INIT_LIST_HEAD(&sks->sks_list);
187 INIT_LIST_HEAD(&sks->sks_free_list);
4afaaefa 188 sks->sks_ref = 0;
2fb9b26a 189
190 for (i = 0; i < sks->sks_objs; i++) {
191 sko = kmem_cache_alloc(spl_obj_cache, flags);
192 if (sko == NULL) {
193out_alloc:
194 /* Unable to fully construct slab, objects,
195 * and object data buffers unwind everything.
196 */
197 list_for_each_entry_safe(sko, n, &sks->sks_free_list,
198 sko_list) {
199 ASSERT(sko->sko_magic == SKO_MAGIC);
200 vmem_free(sko->sko_addr, skc->skc_obj_size);
201 list_del(&sko->sko_list);
202 kmem_cache_free(spl_obj_cache, sko);
203 }
204
205 kmem_cache_free(spl_slab_cache, sks);
206 GOTO(out, sks = NULL);
207 }
f1ca4da6 208
2fb9b26a 209 sko->sko_addr = vmem_alloc(skc->skc_obj_size, flags);
210 if (sko->sko_addr == NULL) {
211 kmem_cache_free(spl_obj_cache, sko);
212 GOTO(out_alloc, sks = NULL);
213 }
f1ca4da6 214
2fb9b26a 215 sko->sko_magic = SKO_MAGIC;
216 sko->sko_flags = 0;
217 sko->sko_slab = sks;
218 INIT_LIST_HEAD(&sko->sko_list);
219 INIT_HLIST_NODE(&sko->sko_hlist);
220 list_add(&sko->sko_list, &sks->sks_free_list);
d6a26c6a 221 }
2fb9b26a 222out:
223 RETURN(sks);
f1ca4da6 224}
225
2fb9b26a 226/* Removes slab from complete or partial list, so it must
d46630e0 227 * be called with the 'skc->skc_lock' held.
2fb9b26a 228 * */
f1ca4da6 229static void
4afaaefa 230spl_slab_free(spl_kmem_slab_t *sks) {
2fb9b26a 231 spl_kmem_cache_t *skc;
232 spl_kmem_obj_t *sko, *n;
233 int i = 0;
234 ENTRY;
57d86234 235
2fb9b26a 236 ASSERT(sks->sks_magic == SKS_MAGIC);
4afaaefa 237 ASSERT(sks->sks_ref == 0);
2fb9b26a 238 skc = sks->sks_cache;
239 skc->skc_obj_total -= sks->sks_objs;
240 skc->skc_slab_total--;
d6a26c6a 241
d46630e0 242 ASSERT(spin_is_locked(&skc->skc_lock));
f1ca4da6 243
2fb9b26a 244 list_for_each_entry_safe(sko, n, &sks->sks_free_list, sko_list) {
245 ASSERT(sko->sko_magic == SKO_MAGIC);
937879f1 246
2fb9b26a 247 /* Run destructors for being freed */
248 if (skc->skc_dtor)
249 skc->skc_dtor(sko->sko_addr, skc->skc_private);
0a6fd143 250
2fb9b26a 251 vmem_free(sko->sko_addr, skc->skc_obj_size);
252 list_del(&sko->sko_list);
253 kmem_cache_free(spl_obj_cache, sko);
254 i++;
255 }
d61e12af 256
2fb9b26a 257 ASSERT(sks->sks_objs == i);
258 list_del(&sks->sks_list);
259 kmem_cache_free(spl_slab_cache, sks);
d61e12af 260
2fb9b26a 261 EXIT;
262}
d6a26c6a 263
2fb9b26a 264static int
4afaaefa 265__spl_slab_reclaim(spl_kmem_cache_t *skc)
2fb9b26a 266{
267 spl_kmem_slab_t *sks, *m;
268 int rc = 0;
269 ENTRY;
270
d46630e0 271 ASSERT(spin_is_locked(&skc->skc_lock));
2fb9b26a 272 /*
273 * Free empty slabs which have not been touched in skc_delay
274 * seconds. This delay time is important to avoid thrashing.
275 * Empty slabs will be at the end of the skc_partial_list.
276 */
277 list_for_each_entry_safe_reverse(sks, m, &skc->skc_partial_list,
278 sks_list) {
4afaaefa 279 if (sks->sks_ref > 0)
2fb9b26a 280 break;
281
282 if (time_after(jiffies, sks->sks_age + skc->skc_delay * HZ)) {
4afaaefa 283 spl_slab_free(sks);
2fb9b26a 284 rc++;
285 }
286 }
287
288 /* Returns number of slabs reclaimed */
289 RETURN(rc);
f1ca4da6 290}
291
2fb9b26a 292static int
4afaaefa 293spl_slab_reclaim(spl_kmem_cache_t *skc)
f1ca4da6 294{
2fb9b26a 295 int rc;
296 ENTRY;
f1ca4da6 297
d46630e0 298 spin_lock(&skc->skc_lock);
4afaaefa 299 rc = __spl_slab_reclaim(skc);
d46630e0 300 spin_unlock(&skc->skc_lock);
4efd4118 301
2fb9b26a 302 RETURN(rc);
303}
f1ca4da6 304
4afaaefa 305static int
306spl_magazine_size(spl_kmem_cache_t *skc)
307{
308 int size;
309 ENTRY;
310
311 /* Guesses for reasonable magazine sizes, they
312 * should really adapt based on observed usage. */
313 if (skc->skc_obj_size > (PAGE_SIZE * 256))
314 size = 1;
315 else if (skc->skc_obj_size > (PAGE_SIZE * 32))
316 size = 4;
317 else if (skc->skc_obj_size > (PAGE_SIZE))
318 size = 16;
319 else if (skc->skc_obj_size > (PAGE_SIZE / 4))
320 size = 32;
321 else if (skc->skc_obj_size > (PAGE_SIZE / 16))
322 size = 64;
323 else
324 size = 128;
325
326 RETURN(size);
327}
328
329static spl_kmem_magazine_t *
330spl_magazine_alloc(spl_kmem_cache_t *skc, int node)
331{
332 spl_kmem_magazine_t *skm;
333 int size = sizeof(spl_kmem_magazine_t) +
334 sizeof(void *) * skc->skc_mag_size;
335 ENTRY;
336
337 skm = kmalloc_node(size, GFP_KERNEL, node);
338 if (skm) {
339 skm->skm_magic = SKM_MAGIC;
340 skm->skm_avail = 0;
341 skm->skm_size = skc->skc_mag_size;
342 skm->skm_refill = skc->skc_mag_refill;
343 skm->skm_age = jiffies;
344 }
345
346 RETURN(skm);
347}
348
349static void
350spl_magazine_free(spl_kmem_magazine_t *skm)
351{
352 ENTRY;
353 ASSERT(skm->skm_magic == SKM_MAGIC);
354 ASSERT(skm->skm_avail == 0);
355 kfree(skm);
356 EXIT;
357}
358
359static int
360spl_magazine_create(spl_kmem_cache_t *skc)
361{
362 int i;
363 ENTRY;
364
365 skc->skc_mag_size = spl_magazine_size(skc);
366 skc->skc_mag_refill = (skc->skc_mag_size + 1) / 2;
367
368 for_each_online_cpu(i) {
369 skc->skc_mag[i] = spl_magazine_alloc(skc, cpu_to_node(i));
370 if (!skc->skc_mag[i]) {
371 for (i--; i >= 0; i--)
372 spl_magazine_free(skc->skc_mag[i]);
373
374 RETURN(-ENOMEM);
375 }
376 }
377
378 RETURN(0);
379}
380
381static void
382spl_magazine_destroy(spl_kmem_cache_t *skc)
383{
384 spl_kmem_magazine_t *skm;
385 int i;
386 ENTRY;
387
388 for_each_online_cpu(i) {
389 skm = skc->skc_mag[i];
390 (void)spl_cache_flush(skc, skm, skm->skm_avail);
391 spl_magazine_free(skm);
392 }
393
394 EXIT;
395}
396
2fb9b26a 397spl_kmem_cache_t *
398spl_kmem_cache_create(char *name, size_t size, size_t align,
399 spl_kmem_ctor_t ctor,
400 spl_kmem_dtor_t dtor,
401 spl_kmem_reclaim_t reclaim,
402 void *priv, void *vmp, int flags)
403{
404 spl_kmem_cache_t *skc;
4afaaefa 405 int i, rc, kmem_flags = KM_SLEEP;
2fb9b26a 406 ENTRY;
937879f1 407
2fb9b26a 408 /* We may be called when there is a non-zero preempt_count or
409 * interrupts are disabled is which case we must not sleep.
410 */
411 if (current_thread_info()->preempt_count || irqs_disabled())
412 kmem_flags = KM_NOSLEEP;
0a6fd143 413
2fb9b26a 414 /* Allocate new cache memory and initialize. */
415 skc = (spl_kmem_cache_t *)kmem_alloc(sizeof(*skc), kmem_flags);
416 if (skc == NULL)
417 RETURN(NULL);
d61e12af 418
2fb9b26a 419 skc->skc_magic = SKC_MAGIC;
2fb9b26a 420 skc->skc_name_size = strlen(name) + 1;
421 skc->skc_name = (char *)kmem_alloc(skc->skc_name_size, kmem_flags);
422 if (skc->skc_name == NULL) {
423 kmem_free(skc, sizeof(*skc));
424 RETURN(NULL);
425 }
426 strncpy(skc->skc_name, name, skc->skc_name_size);
427
428 skc->skc_ctor = ctor;
429 skc->skc_dtor = dtor;
430 skc->skc_reclaim = reclaim;
431 skc->skc_private = priv;
432 skc->skc_vmp = vmp;
433 skc->skc_flags = flags;
434 skc->skc_obj_size = size;
435 skc->skc_chunk_size = 0; /* XXX: Needed only when implementing */
436 skc->skc_slab_size = 0; /* small slab object optimizations */
437 skc->skc_max_chunks = 0; /* which are yet supported. */
438 skc->skc_delay = SPL_KMEM_CACHE_DELAY;
439
440 skc->skc_hash_bits = SPL_KMEM_CACHE_HASH_BITS;
441 skc->skc_hash_size = SPL_KMEM_CACHE_HASH_SIZE;
442 skc->skc_hash_elts = SPL_KMEM_CACHE_HASH_ELTS;
443 skc->skc_hash = (struct hlist_head *)
444 kmem_alloc(skc->skc_hash_size, kmem_flags);
445 if (skc->skc_hash == NULL) {
446 kmem_free(skc->skc_name, skc->skc_name_size);
447 kmem_free(skc, sizeof(*skc));
4afaaefa 448 RETURN(NULL);
2fb9b26a 449 }
450
451 for (i = 0; i < skc->skc_hash_elts; i++)
452 INIT_HLIST_HEAD(&skc->skc_hash[i]);
453
454 INIT_LIST_HEAD(&skc->skc_list);
455 INIT_LIST_HEAD(&skc->skc_complete_list);
456 INIT_LIST_HEAD(&skc->skc_partial_list);
d46630e0 457 spin_lock_init(&skc->skc_lock);
2fb9b26a 458 skc->skc_slab_fail = 0;
459 skc->skc_slab_create = 0;
460 skc->skc_slab_destroy = 0;
461 skc->skc_slab_total = 0;
462 skc->skc_slab_alloc = 0;
463 skc->skc_slab_max = 0;
464 skc->skc_obj_total = 0;
465 skc->skc_obj_alloc = 0;
466 skc->skc_obj_max = 0;
467 skc->skc_hash_depth = 0;
4afaaefa 468 skc->skc_hash_count = 0;
469
470 rc = spl_magazine_create(skc);
471 if (rc) {
472 kmem_free(skc->skc_hash, skc->skc_hash_size);
473 kmem_free(skc->skc_name, skc->skc_name_size);
474 kmem_free(skc, sizeof(*skc));
475 RETURN(NULL);
476 }
2fb9b26a 477
478 down_write(&spl_kmem_cache_sem);
479 list_add_tail(&skc->skc_list, &spl_kmem_cache_list);
480 up_write(&spl_kmem_cache_sem);
481
482 RETURN(skc);
f1ca4da6 483}
2fb9b26a 484EXPORT_SYMBOL(spl_kmem_cache_create);
f1ca4da6 485
2fb9b26a 486/* The caller must ensure there are no racing calls to
4afaaefa 487 * spl_kmem_cache_alloc() for this spl_kmem_cache_t.
2fb9b26a 488 */
489void
490spl_kmem_cache_destroy(spl_kmem_cache_t *skc)
f1ca4da6 491{
2fb9b26a 492 spl_kmem_slab_t *sks, *m;
493 ENTRY;
f1ca4da6 494
2fb9b26a 495 down_write(&spl_kmem_cache_sem);
496 list_del_init(&skc->skc_list);
497 up_write(&spl_kmem_cache_sem);
498
4afaaefa 499 spl_magazine_destroy(skc);
d46630e0 500 spin_lock(&skc->skc_lock);
d6a26c6a 501
2fb9b26a 502 /* Validate there are no objects in use and free all the
4afaaefa 503 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
2fb9b26a 504 ASSERT(list_empty(&skc->skc_complete_list));
4afaaefa 505 ASSERTF(skc->skc_hash_count == 0, "skc->skc_hash_count=%d\n",
506 skc->skc_hash_count);
d6a26c6a 507
2fb9b26a 508 list_for_each_entry_safe(sks, m, &skc->skc_partial_list, sks_list)
4afaaefa 509 spl_slab_free(sks);
2fb9b26a 510
511 kmem_free(skc->skc_hash, skc->skc_hash_size);
512 kmem_free(skc->skc_name, skc->skc_name_size);
d46630e0 513 spin_unlock(&skc->skc_lock);
4afaaefa 514 kmem_free(skc, sizeof(*skc));
2fb9b26a 515
516 EXIT;
f1ca4da6 517}
2fb9b26a 518EXPORT_SYMBOL(spl_kmem_cache_destroy);
f1ca4da6 519
2fb9b26a 520/* The kernel provided hash_ptr() function behaves exceptionally badly
521 * when all the addresses are page aligned which is likely the case
522 * here. To avoid this issue shift off the low order non-random bits.
f1ca4da6 523 */
2fb9b26a 524static unsigned long
525spl_hash_ptr(void *ptr, unsigned int bits)
526{
527 return hash_long((unsigned long)ptr >> PAGE_SHIFT, bits);
528}
f1ca4da6 529
4afaaefa 530static spl_kmem_obj_t *
531spl_hash_obj(spl_kmem_cache_t *skc, void *obj)
532{
533 struct hlist_node *node;
534 spl_kmem_obj_t *sko = NULL;
535 unsigned long key = spl_hash_ptr(obj, skc->skc_hash_bits);
536 int i = 0;
5cbd57fa 537
4afaaefa 538 ASSERT(spin_is_locked(&skc->skc_lock));
539
540 hlist_for_each_entry(sko, node, &skc->skc_hash[key], sko_hlist) {
541
542 if (unlikely((++i) > skc->skc_hash_depth))
543 skc->skc_hash_depth = i;
544
545 if (sko->sko_addr == obj) {
546 ASSERT(sko->sko_magic == SKO_MAGIC);
547 RETURN(sko);
548 }
549 }
550
551 RETURN(NULL);
552}
553
554static void *
555spl_cache_obj(spl_kmem_cache_t *skc, spl_kmem_slab_t *sks)
f1ca4da6 556{
2fb9b26a 557 spl_kmem_obj_t *sko;
2fb9b26a 558 unsigned long key;
f1ca4da6 559
4afaaefa 560 ASSERT(spin_is_locked(&skc->skc_lock));
2fb9b26a 561
4afaaefa 562 sko = list_entry((&sks->sks_free_list)->next,spl_kmem_obj_t,sko_list);
563 ASSERT(sko->sko_magic == SKO_MAGIC);
564 ASSERT(sko->sko_addr != NULL);
2fb9b26a 565
4afaaefa 566 /* Remove from sks_free_list and add to used hash */
567 list_del_init(&sko->sko_list);
568 key = spl_hash_ptr(sko->sko_addr, skc->skc_hash_bits);
569 hlist_add_head(&sko->sko_hlist, &skc->skc_hash[key]);
2fb9b26a 570
4afaaefa 571 sks->sks_age = jiffies;
572 sks->sks_ref++;
573 skc->skc_obj_alloc++;
574 skc->skc_hash_count++;
2fb9b26a 575
4afaaefa 576 /* Track max obj usage statistics */
577 if (skc->skc_obj_alloc > skc->skc_obj_max)
578 skc->skc_obj_max = skc->skc_obj_alloc;
2fb9b26a 579
4afaaefa 580 /* Track max slab usage statistics */
581 if (sks->sks_ref == 1) {
582 skc->skc_slab_alloc++;
f1ca4da6 583
4afaaefa 584 if (skc->skc_slab_alloc > skc->skc_slab_max)
585 skc->skc_slab_max = skc->skc_slab_alloc;
2fb9b26a 586 }
587
4afaaefa 588 return sko->sko_addr;
589}
c30df9c8 590
4afaaefa 591/* No available objects create a new slab. Since this is an
592 * expensive operation we do it without holding the spinlock
593 * and only briefly aquire it when we link in the fully
594 * allocated and constructed slab.
595 */
596static spl_kmem_slab_t *
597spl_cache_grow(spl_kmem_cache_t *skc, int flags)
598{
599 spl_kmem_slab_t *sks;
600 spl_kmem_obj_t *sko;
601 ENTRY;
f1ca4da6 602
4afaaefa 603 if (flags & __GFP_WAIT) {
2fb9b26a 604 flags |= __GFP_NOFAIL;
4afaaefa 605 might_sleep();
606 local_irq_enable();
607 }
f1ca4da6 608
4afaaefa 609 sks = spl_slab_alloc(skc, flags);
610 if (sks == NULL) {
611 if (flags & __GFP_WAIT)
612 local_irq_disable();
613
614 RETURN(NULL);
615 }
2fb9b26a 616
617 /* Run all the constructors now that the slab is fully allocated */
618 list_for_each_entry(sko, &sks->sks_free_list, sko_list) {
619 ASSERT(sko->sko_magic == SKO_MAGIC);
f1ca4da6 620
2fb9b26a 621 if (skc->skc_ctor)
622 skc->skc_ctor(sko->sko_addr, skc->skc_private, flags);
623 }
f1ca4da6 624
4afaaefa 625 if (flags & __GFP_WAIT)
626 local_irq_disable();
627
628 /* Link the new empty slab in to the end of skc_partial_list */
d46630e0 629 spin_lock(&skc->skc_lock);
2fb9b26a 630 skc->skc_slab_total++;
631 skc->skc_obj_total += sks->sks_objs;
632 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
d46630e0 633 spin_unlock(&skc->skc_lock);
4afaaefa 634
635 RETURN(sks);
f1ca4da6 636}
637
4afaaefa 638static int
639spl_cache_refill(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flags)
f1ca4da6 640{
4afaaefa 641 spl_kmem_slab_t *sks;
642 int refill = skm->skm_refill;
937879f1 643 ENTRY;
f1ca4da6 644
4afaaefa 645 /* XXX: Check for refill bouncing by age perhaps */
646
d46630e0 647 spin_lock(&skc->skc_lock);
4afaaefa 648 while (refill > 0) {
649 /* No slabs available we must grow the cache */
650 if (list_empty(&skc->skc_partial_list)) {
651 spin_unlock(&skc->skc_lock);
652 sks = spl_cache_grow(skc, flags);
653 if (!sks)
654 GOTO(out, refill);
655
656 /* Rescheduled to different CPU skm is not local */
657 if (skm != skc->skc_mag[smp_processor_id()])
658 GOTO(out, refill);
659
660 spin_lock(&skc->skc_lock);
661 continue;
662 }
d46630e0 663
4afaaefa 664 /* Grab the next available slab */
665 sks = list_entry((&skc->skc_partial_list)->next,
666 spl_kmem_slab_t, sks_list);
667 ASSERT(sks->sks_magic == SKS_MAGIC);
668 ASSERT(sks->sks_ref < sks->sks_objs);
669 ASSERT(!list_empty(&sks->sks_free_list));
d46630e0 670
4afaaefa 671 /* Consume as many objects as needed to refill the requested
672 * cache. We must be careful to lock here because our local
673 * magazine may not be local anymore due to spl_cache_grow. */
674 while ((sks->sks_ref < sks->sks_objs) && (refill-- > 0))
675 skm->skm_objs[skm->skm_avail++]=spl_cache_obj(skc,sks);
f1ca4da6 676
4afaaefa 677 /* Move slab to skc_complete_list when full */
678 if (sks->sks_ref == sks->sks_objs) {
679 list_del(&sks->sks_list);
680 list_add(&sks->sks_list, &skc->skc_complete_list);
2fb9b26a 681 }
682 }
57d86234 683
4afaaefa 684 spin_unlock(&skc->skc_lock);
685out:
686 /* Returns the number of entries added to cache */
687 RETURN(skm->skm_refill - refill);
688}
689
690static void
691spl_cache_shrink(spl_kmem_cache_t *skc, void *obj)
692{
693 spl_kmem_slab_t *sks = NULL;
694 spl_kmem_obj_t *sko = NULL;
695 ENTRY;
696
697 ASSERT(spin_is_locked(&skc->skc_lock));
698
699 sko = spl_hash_obj(skc, obj);
700 ASSERTF(sko, "Obj %p missing from in-use hash (%d) for cache %s\n",
701 obj, skc->skc_hash_count, skc->skc_name);
702
703 sks = sko->sko_slab;
704 ASSERTF(sks, "Obj %p/%p linked to invalid slab for cache %s\n",
705 obj, sko, skc->skc_name);
706
2fb9b26a 707 ASSERT(sks->sks_cache == skc);
708 hlist_del_init(&sko->sko_hlist);
709 list_add(&sko->sko_list, &sks->sks_free_list);
d6a26c6a 710
2fb9b26a 711 sks->sks_age = jiffies;
4afaaefa 712 sks->sks_ref--;
2fb9b26a 713 skc->skc_obj_alloc--;
4afaaefa 714 skc->skc_hash_count--;
f1ca4da6 715
2fb9b26a 716 /* Move slab to skc_partial_list when no longer full. Slabs
4afaaefa 717 * are added to the head to keep the partial list is quasi-full
718 * sorted order. Fuller at the head, emptier at the tail. */
719 if (sks->sks_ref == (sks->sks_objs - 1)) {
2fb9b26a 720 list_del(&sks->sks_list);
721 list_add(&sks->sks_list, &skc->skc_partial_list);
722 }
f1ca4da6 723
2fb9b26a 724 /* Move emply slabs to the end of the partial list so
4afaaefa 725 * they can be easily found and freed during reclamation. */
726 if (sks->sks_ref == 0) {
2fb9b26a 727 list_del(&sks->sks_list);
728 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
729 skc->skc_slab_alloc--;
730 }
731
4afaaefa 732 EXIT;
733}
734
735static int
736spl_cache_flush(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flush)
737{
738 int i, count = MIN(flush, skm->skm_avail);
739 ENTRY;
740
741
742 spin_lock(&skc->skc_lock);
743 for (i = 0; i < count; i++)
744 spl_cache_shrink(skc, skm->skm_objs[i]);
745
746 __spl_slab_reclaim(skc);
747 skm->skm_avail -= count;
748 memmove(skm->skm_objs, &(skm->skm_objs[count]),
749 sizeof(void *) * skm->skm_avail);
750
d46630e0 751 spin_unlock(&skc->skc_lock);
4afaaefa 752
753 RETURN(count);
754}
755
756void *
757spl_kmem_cache_alloc(spl_kmem_cache_t *skc, int flags)
758{
759 spl_kmem_magazine_t *skm;
760 unsigned long irq_flags;
761 void *obj = NULL;
762 ENTRY;
763
764 ASSERT(flags & KM_SLEEP);
765 local_irq_save(irq_flags);
766
767restart:
768 /* Safe to update per-cpu structure without lock, but
769 * in the restart case we must be careful to reaquire
770 * the local magazine since this may have changed
771 * when we need to grow the cache. */
772 skm = skc->skc_mag[smp_processor_id()];
773
774 if (likely(skm->skm_avail)) {
775 /* Object available in CPU cache, use it */
776 obj = skm->skm_objs[--skm->skm_avail];
777 skm->skm_age = jiffies;
778 } else {
779 /* Per-CPU cache empty, directly allocate from
780 * the slab and refill the per-CPU cache. */
781 (void)spl_cache_refill(skc, skm, flags);
782 GOTO(restart, obj = NULL);
783 }
784
785 local_irq_restore(irq_flags);
786
787 /* Pre-emptively migrate object to CPU L1 cache */
788 prefetchw(obj);
789
790 RETURN(obj);
791}
792EXPORT_SYMBOL(spl_kmem_cache_alloc);
793
794void
795spl_kmem_cache_free(spl_kmem_cache_t *skc, void *obj)
796{
797 spl_kmem_magazine_t *skm;
798 unsigned long flags;
799 ENTRY;
800
801 local_irq_save(flags);
802
803 /* Safe to update per-cpu structure without lock, but
804 * no remote memory allocation tracking is being performed
805 * it is entirely possible to allocate an object from one
806 * CPU cache and return it to another. */
807 skm = skc->skc_mag[smp_processor_id()];
808
809 /* Per-CPU cache full, flush it to make space */
810 if (unlikely(skm->skm_avail >= skm->skm_size))
811 (void)spl_cache_flush(skc, skm, skm->skm_refill);
812
813 /* Available space in cache, use it */
814 skm->skm_objs[skm->skm_avail++] = obj;
815
816 local_irq_restore(flags);
817
818 EXIT;
f1ca4da6 819}
2fb9b26a 820EXPORT_SYMBOL(spl_kmem_cache_free);
5c2bb9b2 821
2fb9b26a 822static int
4afaaefa 823spl_kmem_cache_generic_shrinker(int nr_to_scan, unsigned int gfp_mask)
2fb9b26a 824{
825 spl_kmem_cache_t *skc;
5c2bb9b2 826
2fb9b26a 827 /* Under linux a shrinker is not tightly coupled with a slab
828 * cache. In fact linux always systematically trys calling all
829 * registered shrinker callbacks until its target reclamation level
830 * is reached. Because of this we only register one shrinker
831 * function in the shim layer for all slab caches. And we always
832 * attempt to shrink all caches when this generic shrinker is called.
c30df9c8 833 */
2fb9b26a 834 down_read(&spl_kmem_cache_sem);
57d86234 835
2fb9b26a 836 list_for_each_entry(skc, &spl_kmem_cache_list, skc_list)
837 spl_kmem_cache_reap_now(skc);
838
839 up_read(&spl_kmem_cache_sem);
840
841 /* XXX: Under linux we should return the remaining number of
842 * entries in the cache. We should do this as well.
843 */
844 return 1;
5c2bb9b2 845}
5c2bb9b2 846
57d86234 847void
2fb9b26a 848spl_kmem_cache_reap_now(spl_kmem_cache_t *skc)
57d86234 849{
4afaaefa 850 spl_kmem_magazine_t *skm;
851 int i;
2fb9b26a 852 ENTRY;
853 ASSERT(skc && skc->skc_magic == SKC_MAGIC);
854
855 if (skc->skc_reclaim)
856 skc->skc_reclaim(skc->skc_private);
857
4afaaefa 858 /* Ensure per-CPU caches which are idle gradually flush */
859 for_each_online_cpu(i) {
860 skm = skc->skc_mag[i];
861
862 if (time_after(jiffies, skm->skm_age + skc->skc_delay * HZ))
863 (void)spl_cache_flush(skc, skm, skm->skm_refill);
864 }
865
866 spl_slab_reclaim(skc);
867
2fb9b26a 868 EXIT;
57d86234 869}
2fb9b26a 870EXPORT_SYMBOL(spl_kmem_cache_reap_now);
57d86234 871
f1b59d26 872void
2fb9b26a 873spl_kmem_reap(void)
937879f1 874{
4afaaefa 875 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK, GFP_KERNEL);
f1ca4da6 876}
2fb9b26a 877EXPORT_SYMBOL(spl_kmem_reap);
5d86345d 878
879int
2fb9b26a 880spl_kmem_init(void)
5d86345d 881{
2fb9b26a 882 int rc = 0;
937879f1 883 ENTRY;
d6a26c6a 884
2fb9b26a 885 init_rwsem(&spl_kmem_cache_sem);
886 INIT_LIST_HEAD(&spl_kmem_cache_list);
887
888 spl_slab_cache = NULL;
889 spl_obj_cache = NULL;
890
5cbd57fa 891 spl_slab_cache = __kmem_cache_create("spl_slab_cache",
892 sizeof(spl_kmem_slab_t),
893 0, 0, NULL, NULL);
2fb9b26a 894 if (spl_slab_cache == NULL)
895 GOTO(out_cache, rc = -ENOMEM);
c30df9c8 896
5cbd57fa 897 spl_obj_cache = __kmem_cache_create("spl_obj_cache",
898 sizeof(spl_kmem_obj_t),
899 0, 0, NULL, NULL);
2fb9b26a 900 if (spl_obj_cache == NULL)
901 GOTO(out_cache, rc = -ENOMEM);
902
903#ifdef HAVE_SET_SHRINKER
5cbd57fa 904 spl_kmem_cache_shrinker = set_shrinker(KMC_DEFAULT_SEEKS,
4afaaefa 905 spl_kmem_cache_generic_shrinker);
2fb9b26a 906 if (spl_kmem_cache_shrinker == NULL)
907 GOTO(out_cache, rc = -ENOMEM);
908#else
909 register_shrinker(&spl_kmem_cache_shrinker);
910#endif
c30df9c8 911
5d86345d 912#ifdef DEBUG_KMEM
2fb9b26a 913 { int i;
c30df9c8 914 atomic64_set(&kmem_alloc_used, 0);
915 atomic64_set(&vmem_alloc_used, 0);
2fb9b26a 916 atomic64_set(&kmem_cache_alloc_failed, 0);
d6a26c6a 917
c30df9c8 918 spin_lock_init(&kmem_lock);
919 INIT_LIST_HEAD(&kmem_list);
d6a26c6a 920
c30df9c8 921 for (i = 0; i < KMEM_TABLE_SIZE; i++)
922 INIT_HLIST_HEAD(&kmem_table[i]);
13cdca65 923
c30df9c8 924 spin_lock_init(&vmem_lock);
925 INIT_LIST_HEAD(&vmem_list);
13cdca65 926
c30df9c8 927 for (i = 0; i < VMEM_TABLE_SIZE; i++)
928 INIT_HLIST_HEAD(&vmem_table[i]);
2fb9b26a 929 }
5d86345d 930#endif
2fb9b26a 931 RETURN(rc);
932
933out_cache:
934 if (spl_obj_cache)
935 (void)kmem_cache_destroy(spl_obj_cache);
936
937 if (spl_slab_cache)
938 (void)kmem_cache_destroy(spl_slab_cache);
939
940 RETURN(rc);
5d86345d 941}
942
c6dc93d6 943#ifdef DEBUG_KMEM
944static char *
4afaaefa 945spl_sprintf_addr(kmem_debug_t *kd, char *str, int len, int min)
d6a26c6a 946{
947 int size = ((len - 1) < kd->kd_size) ? (len - 1) : kd->kd_size;
948 int i, flag = 1;
949
950 ASSERT(str != NULL && len >= 17);
951 memset(str, 0, len);
952
953 /* Check for a fully printable string, and while we are at
954 * it place the printable characters in the passed buffer. */
955 for (i = 0; i < size; i++) {
956 str[i] = ((char *)(kd->kd_addr))[i];
957 if (isprint(str[i])) {
958 continue;
959 } else {
960 /* Minimum number of printable characters found
961 * to make it worthwhile to print this as ascii. */
962 if (i > min)
963 break;
964
965 flag = 0;
966 break;
967 }
968
969 }
970
971 if (!flag) {
972 sprintf(str, "%02x%02x%02x%02x%02x%02x%02x%02x",
973 *((uint8_t *)kd->kd_addr),
974 *((uint8_t *)kd->kd_addr + 2),
975 *((uint8_t *)kd->kd_addr + 4),
976 *((uint8_t *)kd->kd_addr + 6),
977 *((uint8_t *)kd->kd_addr + 8),
978 *((uint8_t *)kd->kd_addr + 10),
979 *((uint8_t *)kd->kd_addr + 12),
980 *((uint8_t *)kd->kd_addr + 14));
981 }
982
983 return str;
984}
c6dc93d6 985#endif /* DEBUG_KMEM */
d6a26c6a 986
5d86345d 987void
2fb9b26a 988spl_kmem_fini(void)
5d86345d 989{
990#ifdef DEBUG_KMEM
2fb9b26a 991 unsigned long flags;
992 kmem_debug_t *kd;
993 char str[17];
994
995 /* Display all unreclaimed memory addresses, including the
996 * allocation size and the first few bytes of what's located
997 * at that address to aid in debugging. Performance is not
998 * a serious concern here since it is module unload time. */
999 if (atomic64_read(&kmem_alloc_used) != 0)
1000 CWARN("kmem leaked %ld/%ld bytes\n",
1001 atomic_read(&kmem_alloc_used), kmem_alloc_max);
1002
1003 spin_lock_irqsave(&kmem_lock, flags);
1004 if (!list_empty(&kmem_list))
1005 CDEBUG(D_WARNING, "%-16s %-5s %-16s %s:%s\n",
1006 "address", "size", "data", "func", "line");
1007
1008 list_for_each_entry(kd, &kmem_list, kd_list)
1009 CDEBUG(D_WARNING, "%p %-5d %-16s %s:%d\n",
1010 kd->kd_addr, kd->kd_size,
4afaaefa 1011 spl_sprintf_addr(kd, str, 17, 8),
2fb9b26a 1012 kd->kd_func, kd->kd_line);
1013
1014 spin_unlock_irqrestore(&kmem_lock, flags);
1015
1016 if (atomic64_read(&vmem_alloc_used) != 0)
1017 CWARN("vmem leaked %ld/%ld bytes\n",
1018 atomic_read(&vmem_alloc_used), vmem_alloc_max);
1019
1020 spin_lock_irqsave(&vmem_lock, flags);
1021 if (!list_empty(&vmem_list))
1022 CDEBUG(D_WARNING, "%-16s %-5s %-16s %s:%s\n",
1023 "address", "size", "data", "func", "line");
1024
1025 list_for_each_entry(kd, &vmem_list, kd_list)
1026 CDEBUG(D_WARNING, "%p %-5d %-16s %s:%d\n",
1027 kd->kd_addr, kd->kd_size,
4afaaefa 1028 spl_sprintf_addr(kd, str, 17, 8),
2fb9b26a 1029 kd->kd_func, kd->kd_line);
1030
1031 spin_unlock_irqrestore(&vmem_lock, flags);
1032#endif
1033 ENTRY;
1034
1035#ifdef HAVE_SET_SHRINKER
1036 remove_shrinker(spl_kmem_cache_shrinker);
1037#else
1038 unregister_shrinker(&spl_kmem_cache_shrinker);
5d86345d 1039#endif
2fb9b26a 1040
1041 (void)kmem_cache_destroy(spl_obj_cache);
1042 (void)kmem_cache_destroy(spl_slab_cache);
1043
937879f1 1044 EXIT;
5d86345d 1045}