]> git.proxmox.com Git - mirror_spl.git/blame - module/spl/spl-kmem.c
Ensure kmem_alloc() and vmem_alloc() never fail
[mirror_spl.git] / module / spl / spl-kmem.c
CommitLineData
716154c5
BB
1/*****************************************************************************\
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>.
715f6251 6 * UCRL-CODE-235197
7 *
716154c5
BB
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
715f6251 10 *
716154c5
BB
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
715f6251 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
716154c5
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Kmem Implementation.
25\*****************************************************************************/
715f6251 26
f4b37741 27#include <sys/kmem.h>
55abb092 28#include <spl-debug.h>
f1ca4da6 29
b17edc10
BB
30#ifdef SS_DEBUG_SUBSYS
31#undef SS_DEBUG_SUBSYS
937879f1 32#endif
33
b17edc10 34#define SS_DEBUG_SUBSYS SS_KMEM
937879f1 35
36b313da
BB
36/*
37 * The minimum amount of memory measured in pages to be free at all
38 * times on the system. This is similar to Linux's zone->pages_min
39 * multipled by the number of zones and is sized based on that.
40 */
41pgcnt_t minfree = 0;
42EXPORT_SYMBOL(minfree);
43
44/*
45 * The desired amount of memory measured in pages to be free at all
46 * times on the system. This is similar to Linux's zone->pages_low
47 * multipled by the number of zones and is sized based on that.
48 * Assuming all zones are being used roughly equally, when we drop
49 * below this threshold async page reclamation is triggered.
50 */
51pgcnt_t desfree = 0;
52EXPORT_SYMBOL(desfree);
53
54/*
55 * When above this amount of memory measures in pages the system is
56 * determined to have enough free memory. This is similar to Linux's
57 * zone->pages_high multipled by the number of zones and is sized based
58 * on that. Assuming all zones are being used roughly equally, when
59 * async page reclamation reaches this threshold it stops.
60 */
61pgcnt_t lotsfree = 0;
62EXPORT_SYMBOL(lotsfree);
63
64/* Unused always 0 in this implementation */
65pgcnt_t needfree = 0;
66EXPORT_SYMBOL(needfree);
67
36b313da
BB
68pgcnt_t swapfs_minfree = 0;
69EXPORT_SYMBOL(swapfs_minfree);
70
71pgcnt_t swapfs_reserve = 0;
72EXPORT_SYMBOL(swapfs_reserve);
73
36b313da
BB
74vmem_t *heap_arena = NULL;
75EXPORT_SYMBOL(heap_arena);
76
77vmem_t *zio_alloc_arena = NULL;
78EXPORT_SYMBOL(zio_alloc_arena);
79
80vmem_t *zio_arena = NULL;
81EXPORT_SYMBOL(zio_arena);
82
d1ff2312 83#ifndef HAVE_GET_VMALLOC_INFO
96dded38 84get_vmalloc_info_t get_vmalloc_info_fn = SYMBOL_POISON;
d1ff2312
BB
85EXPORT_SYMBOL(get_vmalloc_info_fn);
86#endif /* HAVE_GET_VMALLOC_INFO */
87
5232d256
BB
88#ifdef HAVE_PGDAT_HELPERS
89# ifndef HAVE_FIRST_ONLINE_PGDAT
96dded38 90first_online_pgdat_t first_online_pgdat_fn = SYMBOL_POISON;
d1ff2312 91EXPORT_SYMBOL(first_online_pgdat_fn);
5232d256 92# endif /* HAVE_FIRST_ONLINE_PGDAT */
36b313da 93
5232d256 94# ifndef HAVE_NEXT_ONLINE_PGDAT
96dded38 95next_online_pgdat_t next_online_pgdat_fn = SYMBOL_POISON;
d1ff2312 96EXPORT_SYMBOL(next_online_pgdat_fn);
5232d256 97# endif /* HAVE_NEXT_ONLINE_PGDAT */
36b313da 98
5232d256 99# ifndef HAVE_NEXT_ZONE
96dded38 100next_zone_t next_zone_fn = SYMBOL_POISON;
d1ff2312 101EXPORT_SYMBOL(next_zone_fn);
5232d256
BB
102# endif /* HAVE_NEXT_ZONE */
103
104#else /* HAVE_PGDAT_HELPERS */
105
106# ifndef HAVE_PGDAT_LIST
107struct pglist_data *pgdat_list_addr = SYMBOL_POISON;
108EXPORT_SYMBOL(pgdat_list_addr);
109# endif /* HAVE_PGDAT_LIST */
110
111#endif /* HAVE_PGDAT_HELPERS */
36b313da 112
6ae7fef5 113#ifdef NEED_GET_ZONE_COUNTS
e11d6c5f 114# ifndef HAVE_GET_ZONE_COUNTS
96dded38 115get_zone_counts_t get_zone_counts_fn = SYMBOL_POISON;
d1ff2312 116EXPORT_SYMBOL(get_zone_counts_fn);
96dded38 117# endif /* HAVE_GET_ZONE_COUNTS */
4ab13d3b 118
e11d6c5f 119unsigned long
6ae7fef5 120spl_global_page_state(spl_zone_stat_item_t item)
4ab13d3b
BB
121{
122 unsigned long active;
123 unsigned long inactive;
124 unsigned long free;
125
6ae7fef5
BB
126 get_zone_counts(&active, &inactive, &free);
127 switch (item) {
128 case SPL_NR_FREE_PAGES: return free;
129 case SPL_NR_INACTIVE: return inactive;
130 case SPL_NR_ACTIVE: return active;
131 default: ASSERT(0); /* Unsupported */
e11d6c5f
BB
132 }
133
6ae7fef5
BB
134 return 0;
135}
136#else
137# ifdef HAVE_GLOBAL_PAGE_STATE
138unsigned long
139spl_global_page_state(spl_zone_stat_item_t item)
140{
141 unsigned long pages = 0;
142
143 switch (item) {
144 case SPL_NR_FREE_PAGES:
145# ifdef HAVE_ZONE_STAT_ITEM_NR_FREE_PAGES
146 pages += global_page_state(NR_FREE_PAGES);
147# endif
148 break;
149 case SPL_NR_INACTIVE:
150# ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE
151 pages += global_page_state(NR_INACTIVE);
152# endif
153# ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_ANON
154 pages += global_page_state(NR_INACTIVE_ANON);
155# endif
156# ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_FILE
157 pages += global_page_state(NR_INACTIVE_FILE);
158# endif
159 break;
160 case SPL_NR_ACTIVE:
161# ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE
162 pages += global_page_state(NR_ACTIVE);
163# endif
164# ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_ANON
165 pages += global_page_state(NR_ACTIVE_ANON);
166# endif
167# ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_FILE
168 pages += global_page_state(NR_ACTIVE_FILE);
169# endif
170 break;
171 default:
172 ASSERT(0); /* Unsupported */
e11d6c5f
BB
173 }
174
6ae7fef5
BB
175 return pages;
176}
96dded38 177# else
6ae7fef5 178# error "Both global_page_state() and get_zone_counts() unavailable"
96dded38 179# endif /* HAVE_GLOBAL_PAGE_STATE */
6ae7fef5 180#endif /* NEED_GET_ZONE_COUNTS */
e11d6c5f 181EXPORT_SYMBOL(spl_global_page_state);
4ab13d3b 182
e11d6c5f
BB
183pgcnt_t
184spl_kmem_availrmem(void)
185{
4ab13d3b 186 /* The amount of easily available memory */
6ae7fef5
BB
187 return (spl_global_page_state(SPL_NR_FREE_PAGES) +
188 spl_global_page_state(SPL_NR_INACTIVE));
4ab13d3b
BB
189}
190EXPORT_SYMBOL(spl_kmem_availrmem);
191
192size_t
193vmem_size(vmem_t *vmp, int typemask)
194{
d1ff2312
BB
195 struct vmalloc_info vmi;
196 size_t size = 0;
197
4ab13d3b
BB
198 ASSERT(vmp == NULL);
199 ASSERT(typemask & (VMEM_ALLOC | VMEM_FREE));
200
d1ff2312
BB
201 get_vmalloc_info(&vmi);
202 if (typemask & VMEM_ALLOC)
203 size += (size_t)vmi.used;
204
205 if (typemask & VMEM_FREE)
206 size += (size_t)(VMALLOC_TOTAL - vmi.used);
207
208 return size;
4ab13d3b
BB
209}
210EXPORT_SYMBOL(vmem_size);
4ab13d3b 211
b868e22f
BB
212int
213kmem_debugging(void)
214{
215 return 0;
216}
217EXPORT_SYMBOL(kmem_debugging);
218
219#ifndef HAVE_KVASPRINTF
220/* Simplified asprintf. */
221char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
222{
223 unsigned int len;
224 char *p;
225 va_list aq;
226
227 va_copy(aq, ap);
228 len = vsnprintf(NULL, 0, fmt, aq);
229 va_end(aq);
230
231 p = kmalloc(len+1, gfp);
232 if (!p)
233 return NULL;
234
235 vsnprintf(p, len+1, fmt, ap);
236
237 return p;
238}
239EXPORT_SYMBOL(kvasprintf);
240#endif /* HAVE_KVASPRINTF */
241
e6de04b7
BB
242char *
243kmem_vasprintf(const char *fmt, va_list ap)
244{
245 va_list aq;
246 char *ptr;
247
e6de04b7 248 do {
2c762de8 249 va_copy(aq, ap);
e6de04b7 250 ptr = kvasprintf(GFP_KERNEL, fmt, aq);
2c762de8 251 va_end(aq);
e6de04b7 252 } while (ptr == NULL);
e6de04b7
BB
253
254 return ptr;
255}
256EXPORT_SYMBOL(kmem_vasprintf);
257
b868e22f
BB
258char *
259kmem_asprintf(const char *fmt, ...)
260{
e6de04b7 261 va_list ap;
b868e22f
BB
262 char *ptr;
263
b868e22f 264 do {
2c762de8 265 va_start(ap, fmt);
e6de04b7 266 ptr = kvasprintf(GFP_KERNEL, fmt, ap);
2c762de8 267 va_end(ap);
b868e22f 268 } while (ptr == NULL);
b868e22f
BB
269
270 return ptr;
271}
272EXPORT_SYMBOL(kmem_asprintf);
273
10129680
BB
274static char *
275__strdup(const char *str, int flags)
276{
277 char *ptr;
278 int n;
279
280 n = strlen(str);
281 ptr = kmalloc_nofail(n + 1, flags);
282 if (ptr)
283 memcpy(ptr, str, n + 1);
284
285 return ptr;
286}
287
288char *
289strdup(const char *str)
290{
291 return __strdup(str, KM_SLEEP);
292}
293EXPORT_SYMBOL(strdup);
294
295void
296strfree(char *str)
297{
298 kmem_free(str, strlen(str) + 1);
299}
300EXPORT_SYMBOL(strfree);
301
f1ca4da6 302/*
2fb9b26a 303 * Memory allocation interfaces and debugging for basic kmem_*
055ffd98
BB
304 * and vmem_* style memory allocation. When DEBUG_KMEM is enabled
305 * the SPL will keep track of the total memory allocated, and
306 * report any memory leaked when the module is unloaded.
f1ca4da6 307 */
308#ifdef DEBUG_KMEM
d04c8a56 309
f1ca4da6 310/* Shim layer memory accounting */
d04c8a56 311# ifdef HAVE_ATOMIC64_T
550f1705 312atomic64_t kmem_alloc_used = ATOMIC64_INIT(0);
a0f6da3d 313unsigned long long kmem_alloc_max = 0;
550f1705 314atomic64_t vmem_alloc_used = ATOMIC64_INIT(0);
a0f6da3d 315unsigned long long vmem_alloc_max = 0;
10129680 316# else /* HAVE_ATOMIC64_T */
d04c8a56
BB
317atomic_t kmem_alloc_used = ATOMIC_INIT(0);
318unsigned long long kmem_alloc_max = 0;
319atomic_t vmem_alloc_used = ATOMIC_INIT(0);
320unsigned long long vmem_alloc_max = 0;
10129680 321# endif /* HAVE_ATOMIC64_T */
79b31f36 322
ff449ac4 323EXPORT_SYMBOL(kmem_alloc_used);
324EXPORT_SYMBOL(kmem_alloc_max);
325EXPORT_SYMBOL(vmem_alloc_used);
326EXPORT_SYMBOL(vmem_alloc_max);
ff449ac4 327
055ffd98
BB
328/* When DEBUG_KMEM_TRACKING is enabled not only will total bytes be tracked
329 * but also the location of every alloc and free. When the SPL module is
330 * unloaded a list of all leaked addresses and where they were allocated
331 * will be dumped to the console. Enabling this feature has a significant
332 * impact on performance but it makes finding memory leaks straight forward.
333 *
334 * Not surprisingly with debugging enabled the xmem_locks are very highly
335 * contended particularly on xfree(). If we want to run with this detailed
336 * debugging enabled for anything other than debugging we need to minimize
337 * the contention by moving to a lock per xmem_table entry model.
a0f6da3d 338 */
055ffd98 339# ifdef DEBUG_KMEM_TRACKING
a0f6da3d 340
341# define KMEM_HASH_BITS 10
342# define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
343
344# define VMEM_HASH_BITS 10
345# define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS)
346
347typedef struct kmem_debug {
348 struct hlist_node kd_hlist; /* Hash node linkage */
349 struct list_head kd_list; /* List of all allocations */
350 void *kd_addr; /* Allocation pointer */
351 size_t kd_size; /* Allocation size */
352 const char *kd_func; /* Allocation function */
353 int kd_line; /* Allocation line */
354} kmem_debug_t;
355
d6a26c6a 356spinlock_t kmem_lock;
357struct hlist_head kmem_table[KMEM_TABLE_SIZE];
358struct list_head kmem_list;
359
13cdca65 360spinlock_t vmem_lock;
361struct hlist_head vmem_table[VMEM_TABLE_SIZE];
362struct list_head vmem_list;
363
d6a26c6a 364EXPORT_SYMBOL(kmem_lock);
365EXPORT_SYMBOL(kmem_table);
366EXPORT_SYMBOL(kmem_list);
367
13cdca65 368EXPORT_SYMBOL(vmem_lock);
369EXPORT_SYMBOL(vmem_table);
370EXPORT_SYMBOL(vmem_list);
a0f6da3d 371
372static kmem_debug_t *
10129680 373kmem_del_init(spinlock_t *lock, struct hlist_head *table, int bits, void *addr)
a0f6da3d 374{
375 struct hlist_head *head;
376 struct hlist_node *node;
377 struct kmem_debug *p;
378 unsigned long flags;
b17edc10 379 SENTRY;
a0f6da3d 380
381 spin_lock_irqsave(lock, flags);
382
383 head = &table[hash_ptr(addr, bits)];
384 hlist_for_each_entry_rcu(p, node, head, kd_hlist) {
385 if (p->kd_addr == addr) {
386 hlist_del_init(&p->kd_hlist);
387 list_del_init(&p->kd_list);
388 spin_unlock_irqrestore(lock, flags);
389 return p;
390 }
391 }
392
393 spin_unlock_irqrestore(lock, flags);
394
b17edc10 395 SRETURN(NULL);
a0f6da3d 396}
397
398void *
399kmem_alloc_track(size_t size, int flags, const char *func, int line,
400 int node_alloc, int node)
401{
402 void *ptr = NULL;
403 kmem_debug_t *dptr;
404 unsigned long irq_flags;
b17edc10 405 SENTRY;
a0f6da3d 406
10129680 407 /* Function may be called with KM_NOSLEEP so failure is possible */
c89fdee4 408 dptr = (kmem_debug_t *) kmalloc_nofail(sizeof(kmem_debug_t),
a0f6da3d 409 flags & ~__GFP_ZERO);
410
10129680 411 if (unlikely(dptr == NULL)) {
b17edc10 412 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "debug "
3cb77549
BB
413 "kmem_alloc(%ld, 0x%x) at %s:%d failed (%lld/%llu)\n",
414 sizeof(kmem_debug_t), flags, func, line,
415 kmem_alloc_used_read(), kmem_alloc_max);
a0f6da3d 416 } else {
10129680
BB
417 /*
418 * Marked unlikely because we should never be doing this,
419 * we tolerate to up 2 pages but a single page is best.
420 */
23d91792 421 if (unlikely((size > PAGE_SIZE*2) && !(flags & KM_NODEBUG))) {
b17edc10 422 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "large "
3cb77549
BB
423 "kmem_alloc(%llu, 0x%x) at %s:%d (%lld/%llu)\n",
424 (unsigned long long) size, flags, func, line,
d04c8a56 425 kmem_alloc_used_read(), kmem_alloc_max);
5198ea0e
BB
426 spl_debug_dumpstack(NULL);
427 }
a0f6da3d 428
10129680
BB
429 /*
430 * We use __strdup() below because the string pointed to by
c8e60837 431 * __FUNCTION__ might not be available by the time we want
10129680
BB
432 * to print it since the module might have been unloaded.
433 * This can only fail in the KM_NOSLEEP case.
434 */
435 dptr->kd_func = __strdup(func, flags & ~__GFP_ZERO);
c8e60837 436 if (unlikely(dptr->kd_func == NULL)) {
437 kfree(dptr);
b17edc10 438 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
10129680 439 "debug __strdup() at %s:%d failed (%lld/%llu)\n",
3cb77549 440 func, line, kmem_alloc_used_read(), kmem_alloc_max);
c8e60837 441 goto out;
442 }
443
a0f6da3d 444 /* Use the correct allocator */
445 if (node_alloc) {
446 ASSERT(!(flags & __GFP_ZERO));
c89fdee4 447 ptr = kmalloc_node_nofail(size, flags, node);
a0f6da3d 448 } else if (flags & __GFP_ZERO) {
c89fdee4 449 ptr = kzalloc_nofail(size, flags & ~__GFP_ZERO);
a0f6da3d 450 } else {
c89fdee4 451 ptr = kmalloc_nofail(size, flags);
a0f6da3d 452 }
453
454 if (unlikely(ptr == NULL)) {
c8e60837 455 kfree(dptr->kd_func);
a0f6da3d 456 kfree(dptr);
b17edc10 457 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "kmem_alloc"
3cb77549
BB
458 "(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
459 (unsigned long long) size, flags, func, line,
d04c8a56 460 kmem_alloc_used_read(), kmem_alloc_max);
a0f6da3d 461 goto out;
462 }
463
d04c8a56
BB
464 kmem_alloc_used_add(size);
465 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max))
466 kmem_alloc_max = kmem_alloc_used_read();
a0f6da3d 467
468 INIT_HLIST_NODE(&dptr->kd_hlist);
469 INIT_LIST_HEAD(&dptr->kd_list);
470
471 dptr->kd_addr = ptr;
472 dptr->kd_size = size;
a0f6da3d 473 dptr->kd_line = line;
474
475 spin_lock_irqsave(&kmem_lock, irq_flags);
476 hlist_add_head_rcu(&dptr->kd_hlist,
477 &kmem_table[hash_ptr(ptr, KMEM_HASH_BITS)]);
478 list_add_tail(&dptr->kd_list, &kmem_list);
479 spin_unlock_irqrestore(&kmem_lock, irq_flags);
480
b17edc10 481 SDEBUG_LIMIT(SD_INFO,
3cb77549
BB
482 "kmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
483 (unsigned long long) size, flags, func, line, ptr,
484 kmem_alloc_used_read(), kmem_alloc_max);
a0f6da3d 485 }
486out:
b17edc10 487 SRETURN(ptr);
a0f6da3d 488}
489EXPORT_SYMBOL(kmem_alloc_track);
490
491void
492kmem_free_track(void *ptr, size_t size)
493{
494 kmem_debug_t *dptr;
b17edc10 495 SENTRY;
a0f6da3d 496
497 ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr,
498 (unsigned long long) size);
499
500 dptr = kmem_del_init(&kmem_lock, kmem_table, KMEM_HASH_BITS, ptr);
501
10129680
BB
502 /* Must exist in hash due to kmem_alloc() */
503 ASSERT(dptr);
a0f6da3d 504
505 /* Size must match */
506 ASSERTF(dptr->kd_size == size, "kd_size (%llu) != size (%llu), "
507 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr->kd_size,
508 (unsigned long long) size, dptr->kd_func, dptr->kd_line);
509
d04c8a56 510 kmem_alloc_used_sub(size);
b17edc10 511 SDEBUG_LIMIT(SD_INFO, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr,
d04c8a56 512 (unsigned long long) size, kmem_alloc_used_read(),
a0f6da3d 513 kmem_alloc_max);
514
c8e60837 515 kfree(dptr->kd_func);
516
a0f6da3d 517 memset(dptr, 0x5a, sizeof(kmem_debug_t));
518 kfree(dptr);
519
520 memset(ptr, 0x5a, size);
521 kfree(ptr);
522
b17edc10 523 SEXIT;
a0f6da3d 524}
525EXPORT_SYMBOL(kmem_free_track);
526
527void *
528vmem_alloc_track(size_t size, int flags, const char *func, int line)
529{
530 void *ptr = NULL;
531 kmem_debug_t *dptr;
532 unsigned long irq_flags;
b17edc10 533 SENTRY;
a0f6da3d 534
535 ASSERT(flags & KM_SLEEP);
536
10129680 537 /* Function may be called with KM_NOSLEEP so failure is possible */
ef1c7a06
BB
538 dptr = (kmem_debug_t *) kmalloc_nofail(sizeof(kmem_debug_t),
539 flags & ~__GFP_ZERO);
10129680 540 if (unlikely(dptr == NULL)) {
b17edc10 541 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "debug "
3cb77549
BB
542 "vmem_alloc(%ld, 0x%x) at %s:%d failed (%lld/%llu)\n",
543 sizeof(kmem_debug_t), flags, func, line,
544 vmem_alloc_used_read(), vmem_alloc_max);
a0f6da3d 545 } else {
10129680
BB
546 /*
547 * We use __strdup() below because the string pointed to by
c8e60837 548 * __FUNCTION__ might not be available by the time we want
10129680
BB
549 * to print it, since the module might have been unloaded.
550 * This can never fail because we have already asserted
551 * that flags is KM_SLEEP.
552 */
553 dptr->kd_func = __strdup(func, flags & ~__GFP_ZERO);
c8e60837 554 if (unlikely(dptr->kd_func == NULL)) {
555 kfree(dptr);
b17edc10 556 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
10129680 557 "debug __strdup() at %s:%d failed (%lld/%llu)\n",
3cb77549 558 func, line, vmem_alloc_used_read(), vmem_alloc_max);
c8e60837 559 goto out;
560 }
561
10129680
BB
562 /* Use the correct allocator */
563 if (flags & __GFP_ZERO) {
564 ptr = vzalloc_nofail(size, flags & ~__GFP_ZERO);
565 } else {
566 ptr = vmalloc_nofail(size, flags);
567 }
a0f6da3d 568
569 if (unlikely(ptr == NULL)) {
c8e60837 570 kfree(dptr->kd_func);
a0f6da3d 571 kfree(dptr);
b17edc10 572 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "vmem_alloc"
3cb77549
BB
573 "(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
574 (unsigned long long) size, flags, func, line,
d04c8a56 575 vmem_alloc_used_read(), vmem_alloc_max);
a0f6da3d 576 goto out;
577 }
578
d04c8a56
BB
579 vmem_alloc_used_add(size);
580 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max))
581 vmem_alloc_max = vmem_alloc_used_read();
a0f6da3d 582
583 INIT_HLIST_NODE(&dptr->kd_hlist);
584 INIT_LIST_HEAD(&dptr->kd_list);
585
586 dptr->kd_addr = ptr;
587 dptr->kd_size = size;
a0f6da3d 588 dptr->kd_line = line;
589
590 spin_lock_irqsave(&vmem_lock, irq_flags);
591 hlist_add_head_rcu(&dptr->kd_hlist,
592 &vmem_table[hash_ptr(ptr, VMEM_HASH_BITS)]);
593 list_add_tail(&dptr->kd_list, &vmem_list);
594 spin_unlock_irqrestore(&vmem_lock, irq_flags);
595
b17edc10 596 SDEBUG_LIMIT(SD_INFO,
3cb77549
BB
597 "vmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
598 (unsigned long long) size, flags, func, line,
599 ptr, vmem_alloc_used_read(), vmem_alloc_max);
a0f6da3d 600 }
601out:
b17edc10 602 SRETURN(ptr);
a0f6da3d 603}
604EXPORT_SYMBOL(vmem_alloc_track);
605
606void
607vmem_free_track(void *ptr, size_t size)
608{
609 kmem_debug_t *dptr;
b17edc10 610 SENTRY;
a0f6da3d 611
612 ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr,
613 (unsigned long long) size);
614
615 dptr = kmem_del_init(&vmem_lock, vmem_table, VMEM_HASH_BITS, ptr);
10129680
BB
616
617 /* Must exist in hash due to vmem_alloc() */
618 ASSERT(dptr);
a0f6da3d 619
620 /* Size must match */
621 ASSERTF(dptr->kd_size == size, "kd_size (%llu) != size (%llu), "
622 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr->kd_size,
623 (unsigned long long) size, dptr->kd_func, dptr->kd_line);
624
d04c8a56 625 vmem_alloc_used_sub(size);
b17edc10 626 SDEBUG_LIMIT(SD_INFO, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr,
d04c8a56 627 (unsigned long long) size, vmem_alloc_used_read(),
a0f6da3d 628 vmem_alloc_max);
629
c8e60837 630 kfree(dptr->kd_func);
631
a0f6da3d 632 memset(dptr, 0x5a, sizeof(kmem_debug_t));
633 kfree(dptr);
634
635 memset(ptr, 0x5a, size);
636 vfree(ptr);
637
b17edc10 638 SEXIT;
a0f6da3d 639}
640EXPORT_SYMBOL(vmem_free_track);
641
642# else /* DEBUG_KMEM_TRACKING */
643
644void *
645kmem_alloc_debug(size_t size, int flags, const char *func, int line,
646 int node_alloc, int node)
647{
648 void *ptr;
b17edc10 649 SENTRY;
a0f6da3d 650
10129680
BB
651 /*
652 * Marked unlikely because we should never be doing this,
653 * we tolerate to up 2 pages but a single page is best.
654 */
23d91792 655 if (unlikely((size > PAGE_SIZE * 2) && !(flags & KM_NODEBUG))) {
b17edc10 656 SDEBUG(SD_CONSOLE | SD_WARNING,
10129680 657 "large kmem_alloc(%llu, 0x%x) at %s:%d (%lld/%llu)\n",
3cb77549 658 (unsigned long long) size, flags, func, line,
d04c8a56 659 kmem_alloc_used_read(), kmem_alloc_max);
5198ea0e
BB
660 spl_debug_dumpstack(NULL);
661 }
a0f6da3d 662
663 /* Use the correct allocator */
664 if (node_alloc) {
665 ASSERT(!(flags & __GFP_ZERO));
c89fdee4 666 ptr = kmalloc_node_nofail(size, flags, node);
a0f6da3d 667 } else if (flags & __GFP_ZERO) {
c89fdee4 668 ptr = kzalloc_nofail(size, flags & (~__GFP_ZERO));
a0f6da3d 669 } else {
c89fdee4 670 ptr = kmalloc_nofail(size, flags);
a0f6da3d 671 }
672
10129680 673 if (unlikely(ptr == NULL)) {
b17edc10 674 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
3cb77549
BB
675 "kmem_alloc(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
676 (unsigned long long) size, flags, func, line,
d04c8a56 677 kmem_alloc_used_read(), kmem_alloc_max);
a0f6da3d 678 } else {
d04c8a56
BB
679 kmem_alloc_used_add(size);
680 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max))
681 kmem_alloc_max = kmem_alloc_used_read();
a0f6da3d 682
b17edc10 683 SDEBUG_LIMIT(SD_INFO,
3cb77549
BB
684 "kmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
685 (unsigned long long) size, flags, func, line, ptr,
10129680 686 kmem_alloc_used_read(), kmem_alloc_max);
a0f6da3d 687 }
10129680 688
b17edc10 689 SRETURN(ptr);
a0f6da3d 690}
691EXPORT_SYMBOL(kmem_alloc_debug);
692
693void
694kmem_free_debug(void *ptr, size_t size)
695{
b17edc10 696 SENTRY;
a0f6da3d 697
698 ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr,
699 (unsigned long long) size);
700
d04c8a56 701 kmem_alloc_used_sub(size);
b17edc10 702 SDEBUG_LIMIT(SD_INFO, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr,
d04c8a56 703 (unsigned long long) size, kmem_alloc_used_read(),
a0f6da3d 704 kmem_alloc_max);
a0f6da3d 705 kfree(ptr);
706
b17edc10 707 SEXIT;
a0f6da3d 708}
709EXPORT_SYMBOL(kmem_free_debug);
710
711void *
712vmem_alloc_debug(size_t size, int flags, const char *func, int line)
713{
714 void *ptr;
b17edc10 715 SENTRY;
a0f6da3d 716
717 ASSERT(flags & KM_SLEEP);
718
10129680
BB
719 /* Use the correct allocator */
720 if (flags & __GFP_ZERO) {
721 ptr = vzalloc_nofail(size, flags & (~__GFP_ZERO));
722 } else {
723 ptr = vmalloc_nofail(size, flags);
724 }
725
726 if (unlikely(ptr == NULL)) {
b17edc10 727 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
3cb77549
BB
728 "vmem_alloc(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
729 (unsigned long long) size, flags, func, line,
d04c8a56 730 vmem_alloc_used_read(), vmem_alloc_max);
a0f6da3d 731 } else {
d04c8a56
BB
732 vmem_alloc_used_add(size);
733 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max))
734 vmem_alloc_max = vmem_alloc_used_read();
a0f6da3d 735
b17edc10 736 SDEBUG_LIMIT(SD_INFO, "vmem_alloc(%llu, 0x%x) = %p "
a0f6da3d 737 "(%lld/%llu)\n", (unsigned long long) size, flags, ptr,
d04c8a56 738 vmem_alloc_used_read(), vmem_alloc_max);
a0f6da3d 739 }
740
b17edc10 741 SRETURN(ptr);
a0f6da3d 742}
743EXPORT_SYMBOL(vmem_alloc_debug);
744
745void
746vmem_free_debug(void *ptr, size_t size)
747{
b17edc10 748 SENTRY;
a0f6da3d 749
750 ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr,
751 (unsigned long long) size);
752
d04c8a56 753 vmem_alloc_used_sub(size);
b17edc10 754 SDEBUG_LIMIT(SD_INFO, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr,
d04c8a56 755 (unsigned long long) size, vmem_alloc_used_read(),
a0f6da3d 756 vmem_alloc_max);
a0f6da3d 757 vfree(ptr);
758
b17edc10 759 SEXIT;
a0f6da3d 760}
761EXPORT_SYMBOL(vmem_free_debug);
762
763# endif /* DEBUG_KMEM_TRACKING */
764#endif /* DEBUG_KMEM */
765
10129680
BB
766/*
767 * Slab allocation interfaces
768 *
769 * While the Linux slab implementation was inspired by the Solaris
770 * implemenation I cannot use it to emulate the Solaris APIs. I
771 * require two features which are not provided by the Linux slab.
772 *
773 * 1) Constructors AND destructors. Recent versions of the Linux
774 * kernel have removed support for destructors. This is a deal
775 * breaker for the SPL which contains particularly expensive
776 * initializers for mutex's, condition variables, etc. We also
777 * require a minimal level of cleanup for these data types unlike
778 * many Linux data type which do need to be explicitly destroyed.
779 *
780 * 2) Virtual address space backed slab. Callers of the Solaris slab
781 * expect it to work well for both small are very large allocations.
782 * Because of memory fragmentation the Linux slab which is backed
783 * by kmalloc'ed memory performs very badly when confronted with
784 * large numbers of large allocations. Basing the slab on the
785 * virtual address space removes the need for contigeous pages
786 * and greatly improve performance for large allocations.
787 *
788 * For these reasons, the SPL has its own slab implementation with
789 * the needed features. It is not as highly optimized as either the
790 * Solaris or Linux slabs, but it should get me most of what is
791 * needed until it can be optimized or obsoleted by another approach.
792 *
793 * One serious concern I do have about this method is the relatively
794 * small virtual address space on 32bit arches. This will seriously
795 * constrain the size of the slab caches and their performance.
796 *
797 * XXX: Improve the partial slab list by carefully maintaining a
798 * strict ordering of fullest to emptiest slabs based on
799 * the slab reference count. This gaurentees the when freeing
800 * slabs back to the system we need only linearly traverse the
801 * last N slabs in the list to discover all the freeable slabs.
802 *
803 * XXX: NUMA awareness for optionally allocating memory close to a
804 * particular core. This can be adventageous if you know the slab
805 * object will be short lived and primarily accessed from one core.
806 *
807 * XXX: Slab coloring may also yield performance improvements and would
808 * be desirable to implement.
809 */
810
811struct list_head spl_kmem_cache_list; /* List of caches */
812struct rw_semaphore spl_kmem_cache_sem; /* Cache list lock */
813
814static int spl_cache_flush(spl_kmem_cache_t *skc,
815 spl_kmem_magazine_t *skm, int flush);
816
817#ifdef HAVE_SET_SHRINKER
818static struct shrinker *spl_kmem_cache_shrinker;
819#else
820static int spl_kmem_cache_generic_shrinker(int nr_to_scan,
821 unsigned int gfp_mask);
822static struct shrinker spl_kmem_cache_shrinker = {
823 .shrink = spl_kmem_cache_generic_shrinker,
824 .seeks = KMC_DEFAULT_SEEKS,
825};
826#endif
827
a1502d76 828static void *
829kv_alloc(spl_kmem_cache_t *skc, int size, int flags)
fece7c99 830{
a1502d76 831 void *ptr;
f1ca4da6 832
8b45dda2
BB
833 ASSERT(ISP2(size));
834
835 if (skc->skc_flags & KMC_KMEM)
836 ptr = (void *)__get_free_pages(flags, get_order(size));
837 else
838 ptr = __vmalloc(size, flags | __GFP_HIGHMEM, PAGE_KERNEL);
839
840 /* Resulting allocated memory will be page aligned */
841 ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE));
fece7c99 842
a1502d76 843 return ptr;
844}
fece7c99 845
a1502d76 846static void
847kv_free(spl_kmem_cache_t *skc, void *ptr, int size)
848{
8b45dda2
BB
849 ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE));
850 ASSERT(ISP2(size));
851
852 if (skc->skc_flags & KMC_KMEM)
853 free_pages((unsigned long)ptr, get_order(size));
854 else
855 vfree(ptr);
856}
857
858/*
859 * Required space for each aligned sks.
860 */
861static inline uint32_t
862spl_sks_size(spl_kmem_cache_t *skc)
863{
864 return P2ROUNDUP_TYPED(sizeof(spl_kmem_slab_t),
865 skc->skc_obj_align, uint32_t);
866}
867
868/*
869 * Required space for each aligned object.
870 */
871static inline uint32_t
872spl_obj_size(spl_kmem_cache_t *skc)
873{
874 uint32_t align = skc->skc_obj_align;
875
876 return P2ROUNDUP_TYPED(skc->skc_obj_size, align, uint32_t) +
877 P2ROUNDUP_TYPED(sizeof(spl_kmem_obj_t), align, uint32_t);
878}
879
880/*
881 * Lookup the spl_kmem_object_t for an object given that object.
882 */
883static inline spl_kmem_obj_t *
884spl_sko_from_obj(spl_kmem_cache_t *skc, void *obj)
885{
886 return obj + P2ROUNDUP_TYPED(skc->skc_obj_size,
887 skc->skc_obj_align, uint32_t);
888}
889
890/*
891 * Required space for each offslab object taking in to account alignment
892 * restrictions and the power-of-two requirement of kv_alloc().
893 */
894static inline uint32_t
895spl_offslab_size(spl_kmem_cache_t *skc)
896{
897 return 1UL << (highbit(spl_obj_size(skc)) + 1);
fece7c99 898}
899
ea3e6ca9
BB
900/*
901 * It's important that we pack the spl_kmem_obj_t structure and the
48e0606a
BB
902 * actual objects in to one large address space to minimize the number
903 * of calls to the allocator. It is far better to do a few large
904 * allocations and then subdivide it ourselves. Now which allocator
905 * we use requires balancing a few trade offs.
906 *
907 * For small objects we use kmem_alloc() because as long as you are
908 * only requesting a small number of pages (ideally just one) its cheap.
909 * However, when you start requesting multiple pages with kmem_alloc()
910 * it gets increasingly expensive since it requires contigeous pages.
911 * For this reason we shift to vmem_alloc() for slabs of large objects
912 * which removes the need for contigeous pages. We do not use
913 * vmem_alloc() in all cases because there is significant locking
914 * overhead in __get_vm_area_node(). This function takes a single
915 * global lock when aquiring an available virtual address range which
916 * serializes all vmem_alloc()'s for all slab caches. Using slightly
917 * different allocation functions for small and large objects should
918 * give us the best of both worlds.
919 *
920 * KMC_ONSLAB KMC_OFFSLAB
921 *
922 * +------------------------+ +-----------------+
923 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
924 * | skc_obj_size <-+ | | +-----------------+ | |
925 * | spl_kmem_obj_t | | | |
926 * | skc_obj_size <---+ | +-----------------+ | |
927 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
928 * | ... v | | spl_kmem_obj_t | |
929 * +------------------------+ +-----------------+ v
930 */
fece7c99 931static spl_kmem_slab_t *
a1502d76 932spl_slab_alloc(spl_kmem_cache_t *skc, int flags)
fece7c99 933{
934 spl_kmem_slab_t *sks;
a1502d76 935 spl_kmem_obj_t *sko, *n;
936 void *base, *obj;
8b45dda2
BB
937 uint32_t obj_size, offslab_size = 0;
938 int i, rc = 0;
48e0606a 939
a1502d76 940 base = kv_alloc(skc, skc->skc_slab_size, flags);
941 if (base == NULL)
b17edc10 942 SRETURN(NULL);
fece7c99 943
a1502d76 944 sks = (spl_kmem_slab_t *)base;
945 sks->sks_magic = SKS_MAGIC;
946 sks->sks_objs = skc->skc_slab_objs;
947 sks->sks_age = jiffies;
948 sks->sks_cache = skc;
949 INIT_LIST_HEAD(&sks->sks_list);
950 INIT_LIST_HEAD(&sks->sks_free_list);
951 sks->sks_ref = 0;
8b45dda2 952 obj_size = spl_obj_size(skc);
48e0606a 953
8b45dda2
BB
954 if (skc->skc_flags * KMC_OFFSLAB)
955 offslab_size = spl_offslab_size(skc);
fece7c99 956
957 for (i = 0; i < sks->sks_objs; i++) {
a1502d76 958 if (skc->skc_flags & KMC_OFFSLAB) {
8b45dda2 959 obj = kv_alloc(skc, offslab_size, flags);
a1502d76 960 if (!obj)
b17edc10 961 SGOTO(out, rc = -ENOMEM);
a1502d76 962 } else {
8b45dda2 963 obj = base + spl_sks_size(skc) + (i * obj_size);
a1502d76 964 }
965
8b45dda2
BB
966 ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align));
967 sko = spl_sko_from_obj(skc, obj);
fece7c99 968 sko->sko_addr = obj;
969 sko->sko_magic = SKO_MAGIC;
970 sko->sko_slab = sks;
971 INIT_LIST_HEAD(&sko->sko_list);
fece7c99 972 list_add_tail(&sko->sko_list, &sks->sks_free_list);
973 }
974
fece7c99 975 list_for_each_entry(sko, &sks->sks_free_list, sko_list)
976 if (skc->skc_ctor)
977 skc->skc_ctor(sko->sko_addr, skc->skc_private, flags);
2fb9b26a 978out:
a1502d76 979 if (rc) {
980 if (skc->skc_flags & KMC_OFFSLAB)
48e0606a
BB
981 list_for_each_entry_safe(sko, n, &sks->sks_free_list,
982 sko_list)
8b45dda2 983 kv_free(skc, sko->sko_addr, offslab_size);
fece7c99 984
a1502d76 985 kv_free(skc, base, skc->skc_slab_size);
986 sks = NULL;
fece7c99 987 }
988
b17edc10 989 SRETURN(sks);
fece7c99 990}
991
ea3e6ca9
BB
992/*
993 * Remove a slab from complete or partial list, it must be called with
994 * the 'skc->skc_lock' held but the actual free must be performed
995 * outside the lock to prevent deadlocking on vmem addresses.
fece7c99 996 */
f1ca4da6 997static void
ea3e6ca9
BB
998spl_slab_free(spl_kmem_slab_t *sks,
999 struct list_head *sks_list, struct list_head *sko_list)
1000{
2fb9b26a 1001 spl_kmem_cache_t *skc;
b17edc10 1002 SENTRY;
57d86234 1003
2fb9b26a 1004 ASSERT(sks->sks_magic == SKS_MAGIC);
4afaaefa 1005 ASSERT(sks->sks_ref == 0);
d6a26c6a 1006
fece7c99 1007 skc = sks->sks_cache;
1008 ASSERT(skc->skc_magic == SKC_MAGIC);
d46630e0 1009 ASSERT(spin_is_locked(&skc->skc_lock));
f1ca4da6 1010
1a944a7d
BB
1011 /*
1012 * Update slab/objects counters in the cache, then remove the
1013 * slab from the skc->skc_partial_list. Finally add the slab
1014 * and all its objects in to the private work lists where the
1015 * destructors will be called and the memory freed to the system.
1016 */
fece7c99 1017 skc->skc_obj_total -= sks->sks_objs;
1018 skc->skc_slab_total--;
1019 list_del(&sks->sks_list);
ea3e6ca9 1020 list_add(&sks->sks_list, sks_list);
1a944a7d
BB
1021 list_splice_init(&sks->sks_free_list, sko_list);
1022
b17edc10 1023 SEXIT;
2fb9b26a 1024}
d6a26c6a 1025
ea3e6ca9
BB
1026/*
1027 * Traverses all the partial slabs attached to a cache and free those
1028 * which which are currently empty, and have not been touched for
37db7d8c
BB
1029 * skc_delay seconds to avoid thrashing. The count argument is
1030 * passed to optionally cap the number of slabs reclaimed, a count
1031 * of zero means try and reclaim everything. When flag is set we
1032 * always free an available slab regardless of age.
ea3e6ca9
BB
1033 */
1034static void
37db7d8c 1035spl_slab_reclaim(spl_kmem_cache_t *skc, int count, int flag)
2fb9b26a 1036{
1037 spl_kmem_slab_t *sks, *m;
ea3e6ca9
BB
1038 spl_kmem_obj_t *sko, *n;
1039 LIST_HEAD(sks_list);
1040 LIST_HEAD(sko_list);
8b45dda2
BB
1041 uint32_t size = 0;
1042 int i = 0;
b17edc10 1043 SENTRY;
2fb9b26a 1044
2fb9b26a 1045 /*
ea3e6ca9
BB
1046 * Move empty slabs and objects which have not been touched in
1047 * skc_delay seconds on to private lists to be freed outside
1a944a7d
BB
1048 * the spin lock. This delay time is important to avoid thrashing
1049 * however when flag is set the delay will not be used.
2fb9b26a 1050 */
ea3e6ca9 1051 spin_lock(&skc->skc_lock);
1a944a7d
BB
1052 list_for_each_entry_safe_reverse(sks,m,&skc->skc_partial_list,sks_list){
1053 /*
1054 * All empty slabs are at the end of skc->skc_partial_list,
1055 * therefore once a non-empty slab is found we can stop
1056 * scanning. Additionally, stop when reaching the target
1057 * reclaim 'count' if a non-zero threshhold is given.
1058 */
1059 if ((sks->sks_ref > 0) || (count && i > count))
37db7d8c
BB
1060 break;
1061
37db7d8c 1062 if (time_after(jiffies,sks->sks_age+skc->skc_delay*HZ)||flag) {
ea3e6ca9 1063 spl_slab_free(sks, &sks_list, &sko_list);
37db7d8c
BB
1064 i++;
1065 }
ea3e6ca9
BB
1066 }
1067 spin_unlock(&skc->skc_lock);
1068
1069 /*
1a944a7d
BB
1070 * The following two loops ensure all the object destructors are
1071 * run, any offslab objects are freed, and the slabs themselves
1072 * are freed. This is all done outside the skc->skc_lock since
1073 * this allows the destructor to sleep, and allows us to perform
1074 * a conditional reschedule when a freeing a large number of
1075 * objects and slabs back to the system.
ea3e6ca9 1076 */
1a944a7d 1077 if (skc->skc_flags & KMC_OFFSLAB)
8b45dda2 1078 size = spl_offslab_size(skc);
ea3e6ca9 1079
1a944a7d
BB
1080 list_for_each_entry_safe(sko, n, &sko_list, sko_list) {
1081 ASSERT(sko->sko_magic == SKO_MAGIC);
1082
1083 if (skc->skc_dtor)
1084 skc->skc_dtor(sko->sko_addr, skc->skc_private);
1085
1086 if (skc->skc_flags & KMC_OFFSLAB)
ea3e6ca9 1087 kv_free(skc, sko->sko_addr, size);
1a944a7d
BB
1088
1089 cond_resched();
2fb9b26a 1090 }
1091
37db7d8c 1092 list_for_each_entry_safe(sks, m, &sks_list, sks_list) {
1a944a7d 1093 ASSERT(sks->sks_magic == SKS_MAGIC);
ea3e6ca9 1094 kv_free(skc, sks, skc->skc_slab_size);
37db7d8c
BB
1095 cond_resched();
1096 }
ea3e6ca9 1097
b17edc10 1098 SEXIT;
f1ca4da6 1099}
1100
ea3e6ca9
BB
1101/*
1102 * Called regularly on all caches to age objects out of the magazines
1103 * which have not been access in skc->skc_delay seconds. This prevents
1104 * idle magazines from holding memory which might be better used by
1105 * other caches or parts of the system. The delay is present to
1106 * prevent thrashing the magazine.
1107 */
1108static void
1109spl_magazine_age(void *data)
f1ca4da6 1110{
9b1b8e4c
BB
1111 spl_kmem_magazine_t *skm =
1112 spl_get_work_data(data, spl_kmem_magazine_t, skm_work.work);
1113 spl_kmem_cache_t *skc = skm->skm_cache;
1114 int i = smp_processor_id();
1115
1116 ASSERT(skm->skm_magic == SKM_MAGIC);
1117 ASSERT(skc->skc_magic == SKC_MAGIC);
1118 ASSERT(skc->skc_mag[i] == skm);
f1ca4da6 1119
ea3e6ca9
BB
1120 if (skm->skm_avail > 0 &&
1121 time_after(jiffies, skm->skm_age + skc->skc_delay * HZ))
1122 (void)spl_cache_flush(skc, skm, skm->skm_refill);
9b1b8e4c
BB
1123
1124 if (!test_bit(KMC_BIT_DESTROY, &skc->skc_flags))
1125 schedule_delayed_work_on(i, &skm->skm_work,
1126 skc->skc_delay / 3 * HZ);
ea3e6ca9 1127}
4efd4118 1128
ea3e6ca9
BB
1129/*
1130 * Called regularly to keep a downward pressure on the size of idle
1131 * magazines and to release free slabs from the cache. This function
1132 * never calls the registered reclaim function, that only occures
1133 * under memory pressure or with a direct call to spl_kmem_reap().
1134 */
1135static void
1136spl_cache_age(void *data)
1137{
9b1b8e4c 1138 spl_kmem_cache_t *skc =
ea3e6ca9
BB
1139 spl_get_work_data(data, spl_kmem_cache_t, skc_work.work);
1140
1141 ASSERT(skc->skc_magic == SKC_MAGIC);
37db7d8c 1142 spl_slab_reclaim(skc, skc->skc_reap, 0);
ea3e6ca9
BB
1143
1144 if (!test_bit(KMC_BIT_DESTROY, &skc->skc_flags))
37db7d8c 1145 schedule_delayed_work(&skc->skc_work, skc->skc_delay / 3 * HZ);
2fb9b26a 1146}
f1ca4da6 1147
ea3e6ca9 1148/*
8b45dda2 1149 * Size a slab based on the size of each aligned object plus spl_kmem_obj_t.
ea3e6ca9
BB
1150 * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However,
1151 * for very small objects we may end up with more than this so as not
1152 * to waste space in the minimal allocation of a single page. Also for
1153 * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN,
1154 * lower than this and we will fail.
1155 */
48e0606a
BB
1156static int
1157spl_slab_size(spl_kmem_cache_t *skc, uint32_t *objs, uint32_t *size)
1158{
8b45dda2 1159 uint32_t sks_size, obj_size, max_size;
48e0606a
BB
1160
1161 if (skc->skc_flags & KMC_OFFSLAB) {
ea3e6ca9 1162 *objs = SPL_KMEM_CACHE_OBJ_PER_SLAB;
48e0606a
BB
1163 *size = sizeof(spl_kmem_slab_t);
1164 } else {
8b45dda2
BB
1165 sks_size = spl_sks_size(skc);
1166 obj_size = spl_obj_size(skc);
ea3e6ca9
BB
1167
1168 if (skc->skc_flags & KMC_KMEM)
aa600d8a 1169 max_size = ((uint32_t)1 << (MAX_ORDER-3)) * PAGE_SIZE;
ea3e6ca9
BB
1170 else
1171 max_size = (32 * 1024 * 1024);
48e0606a 1172
8b45dda2
BB
1173 /* Power of two sized slab */
1174 for (*size = PAGE_SIZE; *size <= max_size; *size *= 2) {
ea3e6ca9
BB
1175 *objs = (*size - sks_size) / obj_size;
1176 if (*objs >= SPL_KMEM_CACHE_OBJ_PER_SLAB)
b17edc10 1177 SRETURN(0);
ea3e6ca9 1178 }
48e0606a 1179
ea3e6ca9 1180 /*
8b45dda2 1181 * Unable to satisfy target objects per slab, fall back to
ea3e6ca9
BB
1182 * allocating a maximally sized slab and assuming it can
1183 * contain the minimum objects count use it. If not fail.
1184 */
1185 *size = max_size;
1186 *objs = (*size - sks_size) / obj_size;
1187 if (*objs >= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN)
b17edc10 1188 SRETURN(0);
48e0606a
BB
1189 }
1190
b17edc10 1191 SRETURN(-ENOSPC);
48e0606a
BB
1192}
1193
ea3e6ca9
BB
1194/*
1195 * Make a guess at reasonable per-cpu magazine size based on the size of
1196 * each object and the cost of caching N of them in each magazine. Long
1197 * term this should really adapt based on an observed usage heuristic.
1198 */
4afaaefa 1199static int
1200spl_magazine_size(spl_kmem_cache_t *skc)
1201{
8b45dda2
BB
1202 uint32_t obj_size = spl_obj_size(skc);
1203 int size;
b17edc10 1204 SENTRY;
4afaaefa 1205
ea3e6ca9 1206 /* Per-magazine sizes below assume a 4Kib page size */
8b45dda2 1207 if (obj_size > (PAGE_SIZE * 256))
ea3e6ca9 1208 size = 4; /* Minimum 4Mib per-magazine */
8b45dda2 1209 else if (obj_size > (PAGE_SIZE * 32))
ea3e6ca9 1210 size = 16; /* Minimum 2Mib per-magazine */
8b45dda2 1211 else if (obj_size > (PAGE_SIZE))
ea3e6ca9 1212 size = 64; /* Minimum 256Kib per-magazine */
8b45dda2 1213 else if (obj_size > (PAGE_SIZE / 4))
ea3e6ca9 1214 size = 128; /* Minimum 128Kib per-magazine */
4afaaefa 1215 else
ea3e6ca9 1216 size = 256;
4afaaefa 1217
b17edc10 1218 SRETURN(size);
4afaaefa 1219}
1220
ea3e6ca9
BB
1221/*
1222 * Allocate a per-cpu magazine to assoicate with a specific core.
1223 */
4afaaefa 1224static spl_kmem_magazine_t *
1225spl_magazine_alloc(spl_kmem_cache_t *skc, int node)
1226{
1227 spl_kmem_magazine_t *skm;
1228 int size = sizeof(spl_kmem_magazine_t) +
1229 sizeof(void *) * skc->skc_mag_size;
b17edc10 1230 SENTRY;
4afaaefa 1231
c89fdee4 1232 skm = kmem_alloc_node(size, KM_SLEEP, node);
4afaaefa 1233 if (skm) {
1234 skm->skm_magic = SKM_MAGIC;
1235 skm->skm_avail = 0;
1236 skm->skm_size = skc->skc_mag_size;
1237 skm->skm_refill = skc->skc_mag_refill;
9b1b8e4c
BB
1238 skm->skm_cache = skc;
1239 spl_init_delayed_work(&skm->skm_work, spl_magazine_age, skm);
ea3e6ca9 1240 skm->skm_age = jiffies;
4afaaefa 1241 }
1242
b17edc10 1243 SRETURN(skm);
4afaaefa 1244}
1245
ea3e6ca9
BB
1246/*
1247 * Free a per-cpu magazine assoicated with a specific core.
1248 */
4afaaefa 1249static void
1250spl_magazine_free(spl_kmem_magazine_t *skm)
1251{
a0f6da3d 1252 int size = sizeof(spl_kmem_magazine_t) +
1253 sizeof(void *) * skm->skm_size;
1254
b17edc10 1255 SENTRY;
4afaaefa 1256 ASSERT(skm->skm_magic == SKM_MAGIC);
1257 ASSERT(skm->skm_avail == 0);
a0f6da3d 1258
1259 kmem_free(skm, size);
b17edc10 1260 SEXIT;
4afaaefa 1261}
1262
ea3e6ca9
BB
1263/*
1264 * Create all pre-cpu magazines of reasonable sizes.
1265 */
4afaaefa 1266static int
1267spl_magazine_create(spl_kmem_cache_t *skc)
1268{
37db7d8c 1269 int i;
b17edc10 1270 SENTRY;
4afaaefa 1271
1272 skc->skc_mag_size = spl_magazine_size(skc);
ea3e6ca9 1273 skc->skc_mag_refill = (skc->skc_mag_size + 1) / 2;
4afaaefa 1274
37db7d8c
BB
1275 for_each_online_cpu(i) {
1276 skc->skc_mag[i] = spl_magazine_alloc(skc, cpu_to_node(i));
1277 if (!skc->skc_mag[i]) {
1278 for (i--; i >= 0; i--)
1279 spl_magazine_free(skc->skc_mag[i]);
4afaaefa 1280
b17edc10 1281 SRETURN(-ENOMEM);
37db7d8c
BB
1282 }
1283 }
4afaaefa 1284
9b1b8e4c
BB
1285 /* Only after everything is allocated schedule magazine work */
1286 for_each_online_cpu(i)
1287 schedule_delayed_work_on(i, &skc->skc_mag[i]->skm_work,
1288 skc->skc_delay / 3 * HZ);
1289
b17edc10 1290 SRETURN(0);
4afaaefa 1291}
1292
ea3e6ca9
BB
1293/*
1294 * Destroy all pre-cpu magazines.
1295 */
4afaaefa 1296static void
1297spl_magazine_destroy(spl_kmem_cache_t *skc)
1298{
37db7d8c
BB
1299 spl_kmem_magazine_t *skm;
1300 int i;
b17edc10 1301 SENTRY;
37db7d8c
BB
1302
1303 for_each_online_cpu(i) {
1304 skm = skc->skc_mag[i];
1305 (void)spl_cache_flush(skc, skm, skm->skm_avail);
1306 spl_magazine_free(skm);
1307 }
1308
b17edc10 1309 SEXIT;
4afaaefa 1310}
1311
ea3e6ca9
BB
1312/*
1313 * Create a object cache based on the following arguments:
1314 * name cache name
1315 * size cache object size
1316 * align cache object alignment
1317 * ctor cache object constructor
1318 * dtor cache object destructor
1319 * reclaim cache object reclaim
1320 * priv cache private data for ctor/dtor/reclaim
1321 * vmp unused must be NULL
1322 * flags
1323 * KMC_NOTOUCH Disable cache object aging (unsupported)
1324 * KMC_NODEBUG Disable debugging (unsupported)
1325 * KMC_NOMAGAZINE Disable magazine (unsupported)
1326 * KMC_NOHASH Disable hashing (unsupported)
1327 * KMC_QCACHE Disable qcache (unsupported)
1328 * KMC_KMEM Force kmem backed cache
1329 * KMC_VMEM Force vmem backed cache
1330 * KMC_OFFSLAB Locate objects off the slab
1331 */
2fb9b26a 1332spl_kmem_cache_t *
1333spl_kmem_cache_create(char *name, size_t size, size_t align,
1334 spl_kmem_ctor_t ctor,
1335 spl_kmem_dtor_t dtor,
1336 spl_kmem_reclaim_t reclaim,
1337 void *priv, void *vmp, int flags)
1338{
1339 spl_kmem_cache_t *skc;
a1502d76 1340 int rc, kmem_flags = KM_SLEEP;
b17edc10 1341 SENTRY;
937879f1 1342
a1502d76 1343 ASSERTF(!(flags & KMC_NOMAGAZINE), "Bad KMC_NOMAGAZINE (%x)\n", flags);
1344 ASSERTF(!(flags & KMC_NOHASH), "Bad KMC_NOHASH (%x)\n", flags);
1345 ASSERTF(!(flags & KMC_QCACHE), "Bad KMC_QCACHE (%x)\n", flags);
48e0606a 1346 ASSERT(vmp == NULL);
a1502d76 1347
2fb9b26a 1348 /* We may be called when there is a non-zero preempt_count or
1349 * interrupts are disabled is which case we must not sleep.
1350 */
e9d7a2be 1351 if (current_thread_info()->preempt_count || irqs_disabled())
2fb9b26a 1352 kmem_flags = KM_NOSLEEP;
0a6fd143 1353
5198ea0e
BB
1354 /* Allocate memry for a new cache an initialize it. Unfortunately,
1355 * this usually ends up being a large allocation of ~32k because
1356 * we need to allocate enough memory for the worst case number of
1357 * cpus in the magazine, skc_mag[NR_CPUS]. Because of this we
23d91792 1358 * explicitly pass KM_NODEBUG to suppress the kmem warning */
5198ea0e 1359 skc = (spl_kmem_cache_t *)kmem_zalloc(sizeof(*skc),
23d91792 1360 kmem_flags | KM_NODEBUG);
e9d7a2be 1361 if (skc == NULL)
b17edc10 1362 SRETURN(NULL);
d61e12af 1363
2fb9b26a 1364 skc->skc_magic = SKC_MAGIC;
2fb9b26a 1365 skc->skc_name_size = strlen(name) + 1;
1366 skc->skc_name = (char *)kmem_alloc(skc->skc_name_size, kmem_flags);
1367 if (skc->skc_name == NULL) {
1368 kmem_free(skc, sizeof(*skc));
b17edc10 1369 SRETURN(NULL);
2fb9b26a 1370 }
1371 strncpy(skc->skc_name, name, skc->skc_name_size);
1372
e9d7a2be 1373 skc->skc_ctor = ctor;
1374 skc->skc_dtor = dtor;
1375 skc->skc_reclaim = reclaim;
2fb9b26a 1376 skc->skc_private = priv;
1377 skc->skc_vmp = vmp;
1378 skc->skc_flags = flags;
1379 skc->skc_obj_size = size;
48e0606a 1380 skc->skc_obj_align = SPL_KMEM_CACHE_ALIGN;
2fb9b26a 1381 skc->skc_delay = SPL_KMEM_CACHE_DELAY;
37db7d8c 1382 skc->skc_reap = SPL_KMEM_CACHE_REAP;
ea3e6ca9 1383 atomic_set(&skc->skc_ref, 0);
2fb9b26a 1384
2fb9b26a 1385 INIT_LIST_HEAD(&skc->skc_list);
1386 INIT_LIST_HEAD(&skc->skc_complete_list);
1387 INIT_LIST_HEAD(&skc->skc_partial_list);
d46630e0 1388 spin_lock_init(&skc->skc_lock);
e9d7a2be 1389 skc->skc_slab_fail = 0;
1390 skc->skc_slab_create = 0;
1391 skc->skc_slab_destroy = 0;
2fb9b26a 1392 skc->skc_slab_total = 0;
1393 skc->skc_slab_alloc = 0;
1394 skc->skc_slab_max = 0;
1395 skc->skc_obj_total = 0;
1396 skc->skc_obj_alloc = 0;
1397 skc->skc_obj_max = 0;
a1502d76 1398
48e0606a 1399 if (align) {
8b45dda2
BB
1400 VERIFY(ISP2(align));
1401 VERIFY3U(align, >=, SPL_KMEM_CACHE_ALIGN); /* Min alignment */
1402 VERIFY3U(align, <=, PAGE_SIZE); /* Max alignment */
48e0606a
BB
1403 skc->skc_obj_align = align;
1404 }
1405
a1502d76 1406 /* If none passed select a cache type based on object size */
1407 if (!(skc->skc_flags & (KMC_KMEM | KMC_VMEM))) {
8b45dda2 1408 if (spl_obj_size(skc) < (PAGE_SIZE / 8))
a1502d76 1409 skc->skc_flags |= KMC_KMEM;
8b45dda2 1410 else
a1502d76 1411 skc->skc_flags |= KMC_VMEM;
a1502d76 1412 }
1413
48e0606a
BB
1414 rc = spl_slab_size(skc, &skc->skc_slab_objs, &skc->skc_slab_size);
1415 if (rc)
b17edc10 1416 SGOTO(out, rc);
4afaaefa 1417
1418 rc = spl_magazine_create(skc);
48e0606a 1419 if (rc)
b17edc10 1420 SGOTO(out, rc);
2fb9b26a 1421
ea3e6ca9 1422 spl_init_delayed_work(&skc->skc_work, spl_cache_age, skc);
37db7d8c 1423 schedule_delayed_work(&skc->skc_work, skc->skc_delay / 3 * HZ);
ea3e6ca9 1424
2fb9b26a 1425 down_write(&spl_kmem_cache_sem);
e9d7a2be 1426 list_add_tail(&skc->skc_list, &spl_kmem_cache_list);
2fb9b26a 1427 up_write(&spl_kmem_cache_sem);
1428
b17edc10 1429 SRETURN(skc);
48e0606a
BB
1430out:
1431 kmem_free(skc->skc_name, skc->skc_name_size);
1432 kmem_free(skc, sizeof(*skc));
b17edc10 1433 SRETURN(NULL);
f1ca4da6 1434}
2fb9b26a 1435EXPORT_SYMBOL(spl_kmem_cache_create);
f1ca4da6 1436
ea3e6ca9
BB
1437/*
1438 * Destroy a cache and all objects assoicated with the cache.
1439 */
2fb9b26a 1440void
1441spl_kmem_cache_destroy(spl_kmem_cache_t *skc)
f1ca4da6 1442{
ea3e6ca9 1443 DECLARE_WAIT_QUEUE_HEAD(wq);
9b1b8e4c 1444 int i;
b17edc10 1445 SENTRY;
f1ca4da6 1446
e9d7a2be 1447 ASSERT(skc->skc_magic == SKC_MAGIC);
1448
1449 down_write(&spl_kmem_cache_sem);
1450 list_del_init(&skc->skc_list);
1451 up_write(&spl_kmem_cache_sem);
2fb9b26a 1452
ea3e6ca9
BB
1453 /* Cancel any and wait for any pending delayed work */
1454 ASSERT(!test_and_set_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1455 cancel_delayed_work(&skc->skc_work);
9b1b8e4c
BB
1456 for_each_online_cpu(i)
1457 cancel_delayed_work(&skc->skc_mag[i]->skm_work);
1458
ea3e6ca9
BB
1459 flush_scheduled_work();
1460
1461 /* Wait until all current callers complete, this is mainly
1462 * to catch the case where a low memory situation triggers a
1463 * cache reaping action which races with this destroy. */
1464 wait_event(wq, atomic_read(&skc->skc_ref) == 0);
1465
4afaaefa 1466 spl_magazine_destroy(skc);
37db7d8c 1467 spl_slab_reclaim(skc, 0, 1);
d46630e0 1468 spin_lock(&skc->skc_lock);
d6a26c6a 1469
2fb9b26a 1470 /* Validate there are no objects in use and free all the
4afaaefa 1471 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
ea3e6ca9
BB
1472 ASSERT3U(skc->skc_slab_alloc, ==, 0);
1473 ASSERT3U(skc->skc_obj_alloc, ==, 0);
1474 ASSERT3U(skc->skc_slab_total, ==, 0);
1475 ASSERT3U(skc->skc_obj_total, ==, 0);
2fb9b26a 1476 ASSERT(list_empty(&skc->skc_complete_list));
a1502d76 1477
2fb9b26a 1478 kmem_free(skc->skc_name, skc->skc_name_size);
d46630e0 1479 spin_unlock(&skc->skc_lock);
ff449ac4 1480
4afaaefa 1481 kmem_free(skc, sizeof(*skc));
2fb9b26a 1482
b17edc10 1483 SEXIT;
f1ca4da6 1484}
2fb9b26a 1485EXPORT_SYMBOL(spl_kmem_cache_destroy);
f1ca4da6 1486
ea3e6ca9
BB
1487/*
1488 * Allocate an object from a slab attached to the cache. This is used to
1489 * repopulate the per-cpu magazine caches in batches when they run low.
1490 */
4afaaefa 1491static void *
1492spl_cache_obj(spl_kmem_cache_t *skc, spl_kmem_slab_t *sks)
f1ca4da6 1493{
2fb9b26a 1494 spl_kmem_obj_t *sko;
f1ca4da6 1495
e9d7a2be 1496 ASSERT(skc->skc_magic == SKC_MAGIC);
1497 ASSERT(sks->sks_magic == SKS_MAGIC);
4afaaefa 1498 ASSERT(spin_is_locked(&skc->skc_lock));
2fb9b26a 1499
a1502d76 1500 sko = list_entry(sks->sks_free_list.next, spl_kmem_obj_t, sko_list);
4afaaefa 1501 ASSERT(sko->sko_magic == SKO_MAGIC);
1502 ASSERT(sko->sko_addr != NULL);
2fb9b26a 1503
a1502d76 1504 /* Remove from sks_free_list */
4afaaefa 1505 list_del_init(&sko->sko_list);
2fb9b26a 1506
4afaaefa 1507 sks->sks_age = jiffies;
1508 sks->sks_ref++;
1509 skc->skc_obj_alloc++;
2fb9b26a 1510
4afaaefa 1511 /* Track max obj usage statistics */
1512 if (skc->skc_obj_alloc > skc->skc_obj_max)
1513 skc->skc_obj_max = skc->skc_obj_alloc;
2fb9b26a 1514
4afaaefa 1515 /* Track max slab usage statistics */
1516 if (sks->sks_ref == 1) {
1517 skc->skc_slab_alloc++;
f1ca4da6 1518
4afaaefa 1519 if (skc->skc_slab_alloc > skc->skc_slab_max)
1520 skc->skc_slab_max = skc->skc_slab_alloc;
2fb9b26a 1521 }
1522
4afaaefa 1523 return sko->sko_addr;
1524}
c30df9c8 1525
ea3e6ca9
BB
1526/*
1527 * No available objects on any slabsi, create a new slab. Since this
1528 * is an expensive operation we do it without holding the spinlock and
1529 * only briefly aquire it when we link in the fully allocated and
1530 * constructed slab.
4afaaefa 1531 */
1532static spl_kmem_slab_t *
1533spl_cache_grow(spl_kmem_cache_t *skc, int flags)
1534{
e9d7a2be 1535 spl_kmem_slab_t *sks;
b17edc10 1536 SENTRY;
f1ca4da6 1537
e9d7a2be 1538 ASSERT(skc->skc_magic == SKC_MAGIC);
ea3e6ca9
BB
1539 local_irq_enable();
1540 might_sleep();
e9d7a2be 1541
ea3e6ca9
BB
1542 /*
1543 * Before allocating a new slab check if the slab is being reaped.
1544 * If it is there is a good chance we can wait until it finishes
1545 * and then use one of the newly freed but not aged-out slabs.
1546 */
1547 if (test_bit(KMC_BIT_REAPING, &skc->skc_flags)) {
1548 schedule();
b17edc10 1549 SGOTO(out, sks= NULL);
4afaaefa 1550 }
2fb9b26a 1551
ea3e6ca9 1552 /* Allocate a new slab for the cache */
23d91792 1553 sks = spl_slab_alloc(skc, flags | __GFP_NORETRY | KM_NODEBUG);
ea3e6ca9 1554 if (sks == NULL)
b17edc10 1555 SGOTO(out, sks = NULL);
4afaaefa 1556
ea3e6ca9 1557 /* Link the new empty slab in to the end of skc_partial_list. */
d46630e0 1558 spin_lock(&skc->skc_lock);
2fb9b26a 1559 skc->skc_slab_total++;
1560 skc->skc_obj_total += sks->sks_objs;
1561 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
d46630e0 1562 spin_unlock(&skc->skc_lock);
ea3e6ca9
BB
1563out:
1564 local_irq_disable();
4afaaefa 1565
b17edc10 1566 SRETURN(sks);
f1ca4da6 1567}
1568
ea3e6ca9
BB
1569/*
1570 * Refill a per-cpu magazine with objects from the slabs for this
1571 * cache. Ideally the magazine can be repopulated using existing
1572 * objects which have been released, however if we are unable to
1573 * locate enough free objects new slabs of objects will be created.
1574 */
4afaaefa 1575static int
1576spl_cache_refill(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flags)
f1ca4da6 1577{
e9d7a2be 1578 spl_kmem_slab_t *sks;
1579 int rc = 0, refill;
b17edc10 1580 SENTRY;
f1ca4da6 1581
e9d7a2be 1582 ASSERT(skc->skc_magic == SKC_MAGIC);
1583 ASSERT(skm->skm_magic == SKM_MAGIC);
1584
e9d7a2be 1585 refill = MIN(skm->skm_refill, skm->skm_size - skm->skm_avail);
d46630e0 1586 spin_lock(&skc->skc_lock);
ff449ac4 1587
4afaaefa 1588 while (refill > 0) {
ea3e6ca9 1589 /* No slabs available we may need to grow the cache */
4afaaefa 1590 if (list_empty(&skc->skc_partial_list)) {
1591 spin_unlock(&skc->skc_lock);
ff449ac4 1592
4afaaefa 1593 sks = spl_cache_grow(skc, flags);
1594 if (!sks)
b17edc10 1595 SGOTO(out, rc);
4afaaefa 1596
1597 /* Rescheduled to different CPU skm is not local */
1598 if (skm != skc->skc_mag[smp_processor_id()])
b17edc10 1599 SGOTO(out, rc);
e9d7a2be 1600
1601 /* Potentially rescheduled to the same CPU but
1602 * allocations may have occured from this CPU while
1603 * we were sleeping so recalculate max refill. */
1604 refill = MIN(refill, skm->skm_size - skm->skm_avail);
4afaaefa 1605
1606 spin_lock(&skc->skc_lock);
1607 continue;
1608 }
d46630e0 1609
4afaaefa 1610 /* Grab the next available slab */
1611 sks = list_entry((&skc->skc_partial_list)->next,
1612 spl_kmem_slab_t, sks_list);
1613 ASSERT(sks->sks_magic == SKS_MAGIC);
1614 ASSERT(sks->sks_ref < sks->sks_objs);
1615 ASSERT(!list_empty(&sks->sks_free_list));
d46630e0 1616
4afaaefa 1617 /* Consume as many objects as needed to refill the requested
e9d7a2be 1618 * cache. We must also be careful not to overfill it. */
1619 while (sks->sks_ref < sks->sks_objs && refill-- > 0 && ++rc) {
1620 ASSERT(skm->skm_avail < skm->skm_size);
1621 ASSERT(rc < skm->skm_size);
4afaaefa 1622 skm->skm_objs[skm->skm_avail++]=spl_cache_obj(skc,sks);
e9d7a2be 1623 }
f1ca4da6 1624
4afaaefa 1625 /* Move slab to skc_complete_list when full */
1626 if (sks->sks_ref == sks->sks_objs) {
1627 list_del(&sks->sks_list);
1628 list_add(&sks->sks_list, &skc->skc_complete_list);
2fb9b26a 1629 }
1630 }
57d86234 1631
4afaaefa 1632 spin_unlock(&skc->skc_lock);
1633out:
1634 /* Returns the number of entries added to cache */
b17edc10 1635 SRETURN(rc);
4afaaefa 1636}
1637
ea3e6ca9
BB
1638/*
1639 * Release an object back to the slab from which it came.
1640 */
4afaaefa 1641static void
1642spl_cache_shrink(spl_kmem_cache_t *skc, void *obj)
1643{
e9d7a2be 1644 spl_kmem_slab_t *sks = NULL;
4afaaefa 1645 spl_kmem_obj_t *sko = NULL;
b17edc10 1646 SENTRY;
4afaaefa 1647
e9d7a2be 1648 ASSERT(skc->skc_magic == SKC_MAGIC);
4afaaefa 1649 ASSERT(spin_is_locked(&skc->skc_lock));
1650
8b45dda2 1651 sko = spl_sko_from_obj(skc, obj);
a1502d76 1652 ASSERT(sko->sko_magic == SKO_MAGIC);
4afaaefa 1653 sks = sko->sko_slab;
a1502d76 1654 ASSERT(sks->sks_magic == SKS_MAGIC);
2fb9b26a 1655 ASSERT(sks->sks_cache == skc);
2fb9b26a 1656 list_add(&sko->sko_list, &sks->sks_free_list);
d6a26c6a 1657
2fb9b26a 1658 sks->sks_age = jiffies;
4afaaefa 1659 sks->sks_ref--;
2fb9b26a 1660 skc->skc_obj_alloc--;
f1ca4da6 1661
2fb9b26a 1662 /* Move slab to skc_partial_list when no longer full. Slabs
4afaaefa 1663 * are added to the head to keep the partial list is quasi-full
1664 * sorted order. Fuller at the head, emptier at the tail. */
1665 if (sks->sks_ref == (sks->sks_objs - 1)) {
2fb9b26a 1666 list_del(&sks->sks_list);
1667 list_add(&sks->sks_list, &skc->skc_partial_list);
1668 }
f1ca4da6 1669
2fb9b26a 1670 /* Move emply slabs to the end of the partial list so
4afaaefa 1671 * they can be easily found and freed during reclamation. */
1672 if (sks->sks_ref == 0) {
2fb9b26a 1673 list_del(&sks->sks_list);
1674 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
1675 skc->skc_slab_alloc--;
1676 }
1677
b17edc10 1678 SEXIT;
4afaaefa 1679}
1680
ea3e6ca9
BB
1681/*
1682 * Release a batch of objects from a per-cpu magazine back to their
1683 * respective slabs. This occurs when we exceed the magazine size,
1684 * are under memory pressure, when the cache is idle, or during
1685 * cache cleanup. The flush argument contains the number of entries
1686 * to remove from the magazine.
1687 */
4afaaefa 1688static int
1689spl_cache_flush(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flush)
1690{
1691 int i, count = MIN(flush, skm->skm_avail);
b17edc10 1692 SENTRY;
4afaaefa 1693
e9d7a2be 1694 ASSERT(skc->skc_magic == SKC_MAGIC);
1695 ASSERT(skm->skm_magic == SKM_MAGIC);
4afaaefa 1696
ea3e6ca9
BB
1697 /*
1698 * XXX: Currently we simply return objects from the magazine to
1699 * the slabs in fifo order. The ideal thing to do from a memory
1700 * fragmentation standpoint is to cheaply determine the set of
1701 * objects in the magazine which will result in the largest
1702 * number of free slabs if released from the magazine.
1703 */
4afaaefa 1704 spin_lock(&skc->skc_lock);
1705 for (i = 0; i < count; i++)
1706 spl_cache_shrink(skc, skm->skm_objs[i]);
1707
e9d7a2be 1708 skm->skm_avail -= count;
1709 memmove(skm->skm_objs, &(skm->skm_objs[count]),
4afaaefa 1710 sizeof(void *) * skm->skm_avail);
1711
d46630e0 1712 spin_unlock(&skc->skc_lock);
4afaaefa 1713
b17edc10 1714 SRETURN(count);
4afaaefa 1715}
1716
ea3e6ca9
BB
1717/*
1718 * Allocate an object from the per-cpu magazine, or if the magazine
1719 * is empty directly allocate from a slab and repopulate the magazine.
1720 */
4afaaefa 1721void *
1722spl_kmem_cache_alloc(spl_kmem_cache_t *skc, int flags)
1723{
1724 spl_kmem_magazine_t *skm;
1725 unsigned long irq_flags;
1726 void *obj = NULL;
b17edc10 1727 SENTRY;
4afaaefa 1728
e9d7a2be 1729 ASSERT(skc->skc_magic == SKC_MAGIC);
ea3e6ca9
BB
1730 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1731 ASSERT(flags & KM_SLEEP);
1732 atomic_inc(&skc->skc_ref);
4afaaefa 1733 local_irq_save(irq_flags);
1734
1735restart:
1736 /* Safe to update per-cpu structure without lock, but
1737 * in the restart case we must be careful to reaquire
1738 * the local magazine since this may have changed
1739 * when we need to grow the cache. */
1740 skm = skc->skc_mag[smp_processor_id()];
e9d7a2be 1741 ASSERTF(skm->skm_magic == SKM_MAGIC, "%x != %x: %s/%p/%p %x/%x/%x\n",
1742 skm->skm_magic, SKM_MAGIC, skc->skc_name, skc, skm,
1743 skm->skm_size, skm->skm_refill, skm->skm_avail);
4afaaefa 1744
1745 if (likely(skm->skm_avail)) {
1746 /* Object available in CPU cache, use it */
1747 obj = skm->skm_objs[--skm->skm_avail];
ea3e6ca9 1748 skm->skm_age = jiffies;
4afaaefa 1749 } else {
1750 /* Per-CPU cache empty, directly allocate from
1751 * the slab and refill the per-CPU cache. */
1752 (void)spl_cache_refill(skc, skm, flags);
b17edc10 1753 SGOTO(restart, obj = NULL);
4afaaefa 1754 }
1755
1756 local_irq_restore(irq_flags);
fece7c99 1757 ASSERT(obj);
8b45dda2 1758 ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align));
4afaaefa 1759
1760 /* Pre-emptively migrate object to CPU L1 cache */
1761 prefetchw(obj);
ea3e6ca9 1762 atomic_dec(&skc->skc_ref);
4afaaefa 1763
b17edc10 1764 SRETURN(obj);
4afaaefa 1765}
1766EXPORT_SYMBOL(spl_kmem_cache_alloc);
1767
ea3e6ca9
BB
1768/*
1769 * Free an object back to the local per-cpu magazine, there is no
1770 * guarantee that this is the same magazine the object was originally
1771 * allocated from. We may need to flush entire from the magazine
1772 * back to the slabs to make space.
1773 */
4afaaefa 1774void
1775spl_kmem_cache_free(spl_kmem_cache_t *skc, void *obj)
1776{
1777 spl_kmem_magazine_t *skm;
1778 unsigned long flags;
b17edc10 1779 SENTRY;
4afaaefa 1780
e9d7a2be 1781 ASSERT(skc->skc_magic == SKC_MAGIC);
ea3e6ca9
BB
1782 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1783 atomic_inc(&skc->skc_ref);
4afaaefa 1784 local_irq_save(flags);
1785
1786 /* Safe to update per-cpu structure without lock, but
1787 * no remote memory allocation tracking is being performed
1788 * it is entirely possible to allocate an object from one
1789 * CPU cache and return it to another. */
1790 skm = skc->skc_mag[smp_processor_id()];
e9d7a2be 1791 ASSERT(skm->skm_magic == SKM_MAGIC);
4afaaefa 1792
1793 /* Per-CPU cache full, flush it to make space */
1794 if (unlikely(skm->skm_avail >= skm->skm_size))
1795 (void)spl_cache_flush(skc, skm, skm->skm_refill);
1796
1797 /* Available space in cache, use it */
1798 skm->skm_objs[skm->skm_avail++] = obj;
1799
1800 local_irq_restore(flags);
ea3e6ca9 1801 atomic_dec(&skc->skc_ref);
4afaaefa 1802
b17edc10 1803 SEXIT;
f1ca4da6 1804}
2fb9b26a 1805EXPORT_SYMBOL(spl_kmem_cache_free);
5c2bb9b2 1806
ea3e6ca9
BB
1807/*
1808 * The generic shrinker function for all caches. Under linux a shrinker
1809 * may not be tightly coupled with a slab cache. In fact linux always
1810 * systematically trys calling all registered shrinker callbacks which
1811 * report that they contain unused objects. Because of this we only
1812 * register one shrinker function in the shim layer for all slab caches.
1813 * We always attempt to shrink all caches when this generic shrinker
1814 * is called. The shrinker should return the number of free objects
1815 * in the cache when called with nr_to_scan == 0 but not attempt to
1816 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
1817 * objects should be freed, because Solaris semantics are to free
1818 * all available objects we may free more objects than requested.
1819 */
2fb9b26a 1820static int
4afaaefa 1821spl_kmem_cache_generic_shrinker(int nr_to_scan, unsigned int gfp_mask)
2fb9b26a 1822{
e9d7a2be 1823 spl_kmem_cache_t *skc;
ea3e6ca9 1824 int unused = 0;
5c2bb9b2 1825
e9d7a2be 1826 down_read(&spl_kmem_cache_sem);
ea3e6ca9
BB
1827 list_for_each_entry(skc, &spl_kmem_cache_list, skc_list) {
1828 if (nr_to_scan)
1829 spl_kmem_cache_reap_now(skc);
1830
1831 /*
1832 * Presume everything alloc'ed in reclaimable, this ensures
1833 * we are called again with nr_to_scan > 0 so can try and
1834 * reclaim. The exact number is not important either so
1835 * we forgo taking this already highly contented lock.
1836 */
1837 unused += skc->skc_obj_alloc;
1838 }
e9d7a2be 1839 up_read(&spl_kmem_cache_sem);
2fb9b26a 1840
ea3e6ca9 1841 return (unused * sysctl_vfs_cache_pressure) / 100;
5c2bb9b2 1842}
5c2bb9b2 1843
ea3e6ca9
BB
1844/*
1845 * Call the registered reclaim function for a cache. Depending on how
1846 * many and which objects are released it may simply repopulate the
1847 * local magazine which will then need to age-out. Objects which cannot
1848 * fit in the magazine we will be released back to their slabs which will
1849 * also need to age out before being release. This is all just best
1850 * effort and we do not want to thrash creating and destroying slabs.
1851 */
57d86234 1852void
2fb9b26a 1853spl_kmem_cache_reap_now(spl_kmem_cache_t *skc)
57d86234 1854{
b17edc10 1855 SENTRY;
e9d7a2be 1856
1857 ASSERT(skc->skc_magic == SKC_MAGIC);
ea3e6ca9 1858 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
2fb9b26a 1859
ea3e6ca9
BB
1860 /* Prevent concurrent cache reaping when contended */
1861 if (test_and_set_bit(KMC_BIT_REAPING, &skc->skc_flags)) {
b17edc10 1862 SEXIT;
ea3e6ca9
BB
1863 return;
1864 }
2fb9b26a 1865
ea3e6ca9 1866 atomic_inc(&skc->skc_ref);
4afaaefa 1867
ea3e6ca9
BB
1868 if (skc->skc_reclaim)
1869 skc->skc_reclaim(skc->skc_private);
4afaaefa 1870
37db7d8c 1871 spl_slab_reclaim(skc, skc->skc_reap, 0);
ea3e6ca9
BB
1872 clear_bit(KMC_BIT_REAPING, &skc->skc_flags);
1873 atomic_dec(&skc->skc_ref);
4afaaefa 1874
b17edc10 1875 SEXIT;
57d86234 1876}
2fb9b26a 1877EXPORT_SYMBOL(spl_kmem_cache_reap_now);
57d86234 1878
ea3e6ca9
BB
1879/*
1880 * Reap all free slabs from all registered caches.
1881 */
f1b59d26 1882void
2fb9b26a 1883spl_kmem_reap(void)
937879f1 1884{
4afaaefa 1885 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK, GFP_KERNEL);
f1ca4da6 1886}
2fb9b26a 1887EXPORT_SYMBOL(spl_kmem_reap);
5d86345d 1888
ff449ac4 1889#if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
c6dc93d6 1890static char *
4afaaefa 1891spl_sprintf_addr(kmem_debug_t *kd, char *str, int len, int min)
d6a26c6a 1892{
e9d7a2be 1893 int size = ((len - 1) < kd->kd_size) ? (len - 1) : kd->kd_size;
d6a26c6a 1894 int i, flag = 1;
1895
1896 ASSERT(str != NULL && len >= 17);
e9d7a2be 1897 memset(str, 0, len);
d6a26c6a 1898
1899 /* Check for a fully printable string, and while we are at
1900 * it place the printable characters in the passed buffer. */
1901 for (i = 0; i < size; i++) {
e9d7a2be 1902 str[i] = ((char *)(kd->kd_addr))[i];
1903 if (isprint(str[i])) {
1904 continue;
1905 } else {
1906 /* Minimum number of printable characters found
1907 * to make it worthwhile to print this as ascii. */
1908 if (i > min)
1909 break;
1910
1911 flag = 0;
1912 break;
1913 }
d6a26c6a 1914 }
1915
1916 if (!flag) {
1917 sprintf(str, "%02x%02x%02x%02x%02x%02x%02x%02x",
1918 *((uint8_t *)kd->kd_addr),
1919 *((uint8_t *)kd->kd_addr + 2),
1920 *((uint8_t *)kd->kd_addr + 4),
1921 *((uint8_t *)kd->kd_addr + 6),
1922 *((uint8_t *)kd->kd_addr + 8),
1923 *((uint8_t *)kd->kd_addr + 10),
1924 *((uint8_t *)kd->kd_addr + 12),
1925 *((uint8_t *)kd->kd_addr + 14));
1926 }
1927
1928 return str;
1929}
1930
a1502d76 1931static int
1932spl_kmem_init_tracking(struct list_head *list, spinlock_t *lock, int size)
1933{
1934 int i;
b17edc10 1935 SENTRY;
a1502d76 1936
1937 spin_lock_init(lock);
1938 INIT_LIST_HEAD(list);
1939
1940 for (i = 0; i < size; i++)
1941 INIT_HLIST_HEAD(&kmem_table[i]);
1942
b17edc10 1943 SRETURN(0);
a1502d76 1944}
1945
ff449ac4 1946static void
1947spl_kmem_fini_tracking(struct list_head *list, spinlock_t *lock)
5d86345d 1948{
2fb9b26a 1949 unsigned long flags;
1950 kmem_debug_t *kd;
1951 char str[17];
b17edc10 1952 SENTRY;
2fb9b26a 1953
ff449ac4 1954 spin_lock_irqsave(lock, flags);
1955 if (!list_empty(list))
a0f6da3d 1956 printk(KERN_WARNING "%-16s %-5s %-16s %s:%s\n", "address",
1957 "size", "data", "func", "line");
2fb9b26a 1958
ff449ac4 1959 list_for_each_entry(kd, list, kd_list)
a0f6da3d 1960 printk(KERN_WARNING "%p %-5d %-16s %s:%d\n", kd->kd_addr,
b6b2acc6 1961 (int)kd->kd_size, spl_sprintf_addr(kd, str, 17, 8),
2fb9b26a 1962 kd->kd_func, kd->kd_line);
1963
ff449ac4 1964 spin_unlock_irqrestore(lock, flags);
b17edc10 1965 SEXIT;
ff449ac4 1966}
1967#else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
a1502d76 1968#define spl_kmem_init_tracking(list, lock, size)
ff449ac4 1969#define spl_kmem_fini_tracking(list, lock)
1970#endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1971
36b313da
BB
1972static void
1973spl_kmem_init_globals(void)
1974{
1975 struct zone *zone;
1976
1977 /* For now all zones are includes, it may be wise to restrict
1978 * this to normal and highmem zones if we see problems. */
1979 for_each_zone(zone) {
1980
1981 if (!populated_zone(zone))
1982 continue;
1983
baf2979e
BB
1984 minfree += min_wmark_pages(zone);
1985 desfree += low_wmark_pages(zone);
1986 lotsfree += high_wmark_pages(zone);
36b313da 1987 }
4ab13d3b
BB
1988
1989 /* Solaris default values */
96dded38
BB
1990 swapfs_minfree = MAX(2*1024*1024 >> PAGE_SHIFT, physmem >> 3);
1991 swapfs_reserve = MIN(4*1024*1024 >> PAGE_SHIFT, physmem >> 4);
36b313da
BB
1992}
1993
d1ff2312
BB
1994/*
1995 * Called at module init when it is safe to use spl_kallsyms_lookup_name()
1996 */
1997int
1998spl_kmem_init_kallsyms_lookup(void)
1999{
2000#ifndef HAVE_GET_VMALLOC_INFO
2001 get_vmalloc_info_fn = (get_vmalloc_info_t)
2002 spl_kallsyms_lookup_name("get_vmalloc_info");
e11d6c5f
BB
2003 if (!get_vmalloc_info_fn) {
2004 printk(KERN_ERR "Error: Unknown symbol get_vmalloc_info\n");
d1ff2312 2005 return -EFAULT;
e11d6c5f 2006 }
d1ff2312
BB
2007#endif /* HAVE_GET_VMALLOC_INFO */
2008
5232d256
BB
2009#ifdef HAVE_PGDAT_HELPERS
2010# ifndef HAVE_FIRST_ONLINE_PGDAT
d1ff2312
BB
2011 first_online_pgdat_fn = (first_online_pgdat_t)
2012 spl_kallsyms_lookup_name("first_online_pgdat");
e11d6c5f
BB
2013 if (!first_online_pgdat_fn) {
2014 printk(KERN_ERR "Error: Unknown symbol first_online_pgdat\n");
d1ff2312 2015 return -EFAULT;
e11d6c5f 2016 }
5232d256 2017# endif /* HAVE_FIRST_ONLINE_PGDAT */
d1ff2312 2018
5232d256 2019# ifndef HAVE_NEXT_ONLINE_PGDAT
d1ff2312
BB
2020 next_online_pgdat_fn = (next_online_pgdat_t)
2021 spl_kallsyms_lookup_name("next_online_pgdat");
e11d6c5f
BB
2022 if (!next_online_pgdat_fn) {
2023 printk(KERN_ERR "Error: Unknown symbol next_online_pgdat\n");
d1ff2312 2024 return -EFAULT;
e11d6c5f 2025 }
5232d256 2026# endif /* HAVE_NEXT_ONLINE_PGDAT */
d1ff2312 2027
5232d256 2028# ifndef HAVE_NEXT_ZONE
d1ff2312
BB
2029 next_zone_fn = (next_zone_t)
2030 spl_kallsyms_lookup_name("next_zone");
e11d6c5f
BB
2031 if (!next_zone_fn) {
2032 printk(KERN_ERR "Error: Unknown symbol next_zone\n");
d1ff2312 2033 return -EFAULT;
e11d6c5f 2034 }
5232d256
BB
2035# endif /* HAVE_NEXT_ZONE */
2036
2037#else /* HAVE_PGDAT_HELPERS */
2038
2039# ifndef HAVE_PGDAT_LIST
124ca8a5 2040 pgdat_list_addr = *(struct pglist_data **)
5232d256
BB
2041 spl_kallsyms_lookup_name("pgdat_list");
2042 if (!pgdat_list_addr) {
2043 printk(KERN_ERR "Error: Unknown symbol pgdat_list\n");
2044 return -EFAULT;
2045 }
2046# endif /* HAVE_PGDAT_LIST */
2047#endif /* HAVE_PGDAT_HELPERS */
d1ff2312 2048
6ae7fef5 2049#if defined(NEED_GET_ZONE_COUNTS) && !defined(HAVE_GET_ZONE_COUNTS)
d1ff2312
BB
2050 get_zone_counts_fn = (get_zone_counts_t)
2051 spl_kallsyms_lookup_name("get_zone_counts");
e11d6c5f
BB
2052 if (!get_zone_counts_fn) {
2053 printk(KERN_ERR "Error: Unknown symbol get_zone_counts\n");
d1ff2312 2054 return -EFAULT;
e11d6c5f 2055 }
6ae7fef5 2056#endif /* NEED_GET_ZONE_COUNTS && !HAVE_GET_ZONE_COUNTS */
d1ff2312
BB
2057
2058 /*
2059 * It is now safe to initialize the global tunings which rely on
2060 * the use of the for_each_zone() macro. This macro in turns
2061 * depends on the *_pgdat symbols which are now available.
2062 */
2063 spl_kmem_init_globals();
2064
2065 return 0;
2066}
2067
a1502d76 2068int
2069spl_kmem_init(void)
2070{
2071 int rc = 0;
b17edc10 2072 SENTRY;
a1502d76 2073
2074 init_rwsem(&spl_kmem_cache_sem);
2075 INIT_LIST_HEAD(&spl_kmem_cache_list);
2076
2077#ifdef HAVE_SET_SHRINKER
2078 spl_kmem_cache_shrinker = set_shrinker(KMC_DEFAULT_SEEKS,
2079 spl_kmem_cache_generic_shrinker);
2080 if (spl_kmem_cache_shrinker == NULL)
b17edc10 2081 SRETURN(rc = -ENOMEM);
a1502d76 2082#else
2083 register_shrinker(&spl_kmem_cache_shrinker);
2084#endif
2085
2086#ifdef DEBUG_KMEM
d04c8a56
BB
2087 kmem_alloc_used_set(0);
2088 vmem_alloc_used_set(0);
a1502d76 2089
2090 spl_kmem_init_tracking(&kmem_list, &kmem_lock, KMEM_TABLE_SIZE);
2091 spl_kmem_init_tracking(&vmem_list, &vmem_lock, VMEM_TABLE_SIZE);
2092#endif
b17edc10 2093 SRETURN(rc);
a1502d76 2094}
2095
ff449ac4 2096void
2097spl_kmem_fini(void)
2098{
2099#ifdef DEBUG_KMEM
2100 /* Display all unreclaimed memory addresses, including the
2101 * allocation size and the first few bytes of what's located
2102 * at that address to aid in debugging. Performance is not
2103 * a serious concern here since it is module unload time. */
d04c8a56 2104 if (kmem_alloc_used_read() != 0)
b17edc10 2105 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
3cb77549
BB
2106 "kmem leaked %ld/%ld bytes\n",
2107 kmem_alloc_used_read(), kmem_alloc_max);
ff449ac4 2108
2fb9b26a 2109
d04c8a56 2110 if (vmem_alloc_used_read() != 0)
b17edc10 2111 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING,
3cb77549
BB
2112 "vmem leaked %ld/%ld bytes\n",
2113 vmem_alloc_used_read(), vmem_alloc_max);
2fb9b26a 2114
ff449ac4 2115 spl_kmem_fini_tracking(&kmem_list, &kmem_lock);
2116 spl_kmem_fini_tracking(&vmem_list, &vmem_lock);
2117#endif /* DEBUG_KMEM */
b17edc10 2118 SENTRY;
2fb9b26a 2119
2120#ifdef HAVE_SET_SHRINKER
2121 remove_shrinker(spl_kmem_cache_shrinker);
2122#else
2123 unregister_shrinker(&spl_kmem_cache_shrinker);
5d86345d 2124#endif
2fb9b26a 2125
b17edc10 2126 SEXIT;
5d86345d 2127}