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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Kmem Implementation.
25 \*****************************************************************************/
28 #include <spl-debug.h>
30 #ifdef SS_DEBUG_SUBSYS
31 #undef SS_DEBUG_SUBSYS
34 #define SS_DEBUG_SUBSYS SS_KMEM
37 * Within the scope of spl-kmem.c file the kmem_cache_* definitions
38 * are removed to allow access to the real Linux slab allocator.
40 #undef kmem_cache_destroy
41 #undef kmem_cache_create
42 #undef kmem_cache_alloc
43 #undef kmem_cache_free
47 * Cache expiration was implemented because it was part of the default Solaris
48 * kmem_cache behavior. The idea is that per-cpu objects which haven't been
49 * accessed in several seconds should be returned to the cache. On the other
50 * hand Linux slabs never move objects back to the slabs unless there is
51 * memory pressure on the system. By default the Linux method is enabled
52 * because it has been shown to improve responsiveness on low memory systems.
53 * This policy may be changed by setting KMC_EXPIRE_AGE or KMC_EXPIRE_MEM.
55 unsigned int spl_kmem_cache_expire
= KMC_EXPIRE_MEM
;
56 EXPORT_SYMBOL(spl_kmem_cache_expire
);
57 module_param(spl_kmem_cache_expire
, uint
, 0644);
58 MODULE_PARM_DESC(spl_kmem_cache_expire
, "By age (0x1) or low memory (0x2)");
61 * The default behavior is to report the number of objects remaining in the
62 * cache. This allows the Linux VM to repeatedly reclaim objects from the
63 * cache when memory is low satisfy other memory allocations. Alternately,
64 * setting this value to KMC_RECLAIM_ONCE limits how aggressively the cache
65 * is reclaimed. This may increase the likelihood of out of memory events.
67 unsigned int spl_kmem_cache_reclaim
= 0;
68 module_param(spl_kmem_cache_reclaim
, uint
, 0644);
69 MODULE_PARM_DESC(spl_kmem_cache_reclaim
, "Single reclaim pass (0x1)");
71 unsigned int spl_kmem_cache_obj_per_slab
= SPL_KMEM_CACHE_OBJ_PER_SLAB
;
72 module_param(spl_kmem_cache_obj_per_slab
, uint
, 0644);
73 MODULE_PARM_DESC(spl_kmem_cache_obj_per_slab
, "Number of objects per slab");
75 unsigned int spl_kmem_cache_obj_per_slab_min
= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN
;
76 module_param(spl_kmem_cache_obj_per_slab_min
, uint
, 0644);
77 MODULE_PARM_DESC(spl_kmem_cache_obj_per_slab_min
,
78 "Minimal number of objects per slab");
80 unsigned int spl_kmem_cache_max_size
= 32;
81 module_param(spl_kmem_cache_max_size
, uint
, 0644);
82 MODULE_PARM_DESC(spl_kmem_cache_max_size
, "Maximum size of slab in MB");
85 * For small objects the Linux slab allocator should be used to make the most
86 * efficient use of the memory. However, large objects are not supported by
87 * the Linux slab and therefore the SPL implementation is preferred. A cutoff
88 * of 16K was determined to be optimal for architectures using 4K pages.
91 unsigned int spl_kmem_cache_slab_limit
= 16384;
93 unsigned int spl_kmem_cache_slab_limit
= 0;
95 module_param(spl_kmem_cache_slab_limit
, uint
, 0644);
96 MODULE_PARM_DESC(spl_kmem_cache_slab_limit
,
97 "Objects less than N bytes use the Linux slab");
99 unsigned int spl_kmem_cache_kmem_limit
= (PAGE_SIZE
/ 4);
100 module_param(spl_kmem_cache_kmem_limit
, uint
, 0644);
101 MODULE_PARM_DESC(spl_kmem_cache_kmem_limit
,
102 "Objects less than N bytes use the kmalloc");
105 * The minimum amount of memory measured in pages to be free at all
106 * times on the system. This is similar to Linux's zone->pages_min
107 * multiplied by the number of zones and is sized based on that.
110 EXPORT_SYMBOL(minfree
);
113 * The desired amount of memory measured in pages to be free at all
114 * times on the system. This is similar to Linux's zone->pages_low
115 * multiplied by the number of zones and is sized based on that.
116 * Assuming all zones are being used roughly equally, when we drop
117 * below this threshold asynchronous page reclamation is triggered.
120 EXPORT_SYMBOL(desfree
);
123 * When above this amount of memory measures in pages the system is
124 * determined to have enough free memory. This is similar to Linux's
125 * zone->pages_high multiplied by the number of zones and is sized based
126 * on that. Assuming all zones are being used roughly equally, when
127 * asynchronous page reclamation reaches this threshold it stops.
129 pgcnt_t lotsfree
= 0;
130 EXPORT_SYMBOL(lotsfree
);
132 /* Unused always 0 in this implementation */
133 pgcnt_t needfree
= 0;
134 EXPORT_SYMBOL(needfree
);
136 pgcnt_t swapfs_minfree
= 0;
137 EXPORT_SYMBOL(swapfs_minfree
);
139 pgcnt_t swapfs_reserve
= 0;
140 EXPORT_SYMBOL(swapfs_reserve
);
142 vmem_t
*heap_arena
= NULL
;
143 EXPORT_SYMBOL(heap_arena
);
145 vmem_t
*zio_alloc_arena
= NULL
;
146 EXPORT_SYMBOL(zio_alloc_arena
);
148 vmem_t
*zio_arena
= NULL
;
149 EXPORT_SYMBOL(zio_arena
);
151 #ifndef HAVE_GET_VMALLOC_INFO
152 get_vmalloc_info_t get_vmalloc_info_fn
= SYMBOL_POISON
;
153 EXPORT_SYMBOL(get_vmalloc_info_fn
);
154 #endif /* HAVE_GET_VMALLOC_INFO */
156 #ifdef HAVE_PGDAT_HELPERS
157 # ifndef HAVE_FIRST_ONLINE_PGDAT
158 first_online_pgdat_t first_online_pgdat_fn
= SYMBOL_POISON
;
159 EXPORT_SYMBOL(first_online_pgdat_fn
);
160 # endif /* HAVE_FIRST_ONLINE_PGDAT */
162 # ifndef HAVE_NEXT_ONLINE_PGDAT
163 next_online_pgdat_t next_online_pgdat_fn
= SYMBOL_POISON
;
164 EXPORT_SYMBOL(next_online_pgdat_fn
);
165 # endif /* HAVE_NEXT_ONLINE_PGDAT */
167 # ifndef HAVE_NEXT_ZONE
168 next_zone_t next_zone_fn
= SYMBOL_POISON
;
169 EXPORT_SYMBOL(next_zone_fn
);
170 # endif /* HAVE_NEXT_ZONE */
172 #else /* HAVE_PGDAT_HELPERS */
174 # ifndef HAVE_PGDAT_LIST
175 struct pglist_data
*pgdat_list_addr
= SYMBOL_POISON
;
176 EXPORT_SYMBOL(pgdat_list_addr
);
177 # endif /* HAVE_PGDAT_LIST */
179 #endif /* HAVE_PGDAT_HELPERS */
181 #ifdef NEED_GET_ZONE_COUNTS
182 # ifndef HAVE_GET_ZONE_COUNTS
183 get_zone_counts_t get_zone_counts_fn
= SYMBOL_POISON
;
184 EXPORT_SYMBOL(get_zone_counts_fn
);
185 # endif /* HAVE_GET_ZONE_COUNTS */
188 spl_global_page_state(spl_zone_stat_item_t item
)
190 unsigned long active
;
191 unsigned long inactive
;
194 get_zone_counts(&active
, &inactive
, &free
);
196 case SPL_NR_FREE_PAGES
: return free
;
197 case SPL_NR_INACTIVE
: return inactive
;
198 case SPL_NR_ACTIVE
: return active
;
199 default: ASSERT(0); /* Unsupported */
205 # ifdef HAVE_GLOBAL_PAGE_STATE
207 spl_global_page_state(spl_zone_stat_item_t item
)
209 unsigned long pages
= 0;
212 case SPL_NR_FREE_PAGES
:
213 # ifdef HAVE_ZONE_STAT_ITEM_NR_FREE_PAGES
214 pages
+= global_page_state(NR_FREE_PAGES
);
217 case SPL_NR_INACTIVE
:
218 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE
219 pages
+= global_page_state(NR_INACTIVE
);
221 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_ANON
222 pages
+= global_page_state(NR_INACTIVE_ANON
);
224 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_FILE
225 pages
+= global_page_state(NR_INACTIVE_FILE
);
229 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE
230 pages
+= global_page_state(NR_ACTIVE
);
232 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_ANON
233 pages
+= global_page_state(NR_ACTIVE_ANON
);
235 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_FILE
236 pages
+= global_page_state(NR_ACTIVE_FILE
);
240 ASSERT(0); /* Unsupported */
246 # error "Both global_page_state() and get_zone_counts() unavailable"
247 # endif /* HAVE_GLOBAL_PAGE_STATE */
248 #endif /* NEED_GET_ZONE_COUNTS */
249 EXPORT_SYMBOL(spl_global_page_state
);
251 #ifndef HAVE_SHRINK_DCACHE_MEMORY
252 shrink_dcache_memory_t shrink_dcache_memory_fn
= SYMBOL_POISON
;
253 EXPORT_SYMBOL(shrink_dcache_memory_fn
);
254 #endif /* HAVE_SHRINK_DCACHE_MEMORY */
256 #ifndef HAVE_SHRINK_ICACHE_MEMORY
257 shrink_icache_memory_t shrink_icache_memory_fn
= SYMBOL_POISON
;
258 EXPORT_SYMBOL(shrink_icache_memory_fn
);
259 #endif /* HAVE_SHRINK_ICACHE_MEMORY */
262 spl_kmem_availrmem(void)
264 /* The amount of easily available memory */
265 return (spl_global_page_state(SPL_NR_FREE_PAGES
) +
266 spl_global_page_state(SPL_NR_INACTIVE
));
268 EXPORT_SYMBOL(spl_kmem_availrmem
);
271 vmem_size(vmem_t
*vmp
, int typemask
)
273 struct vmalloc_info vmi
;
277 ASSERT(typemask
& (VMEM_ALLOC
| VMEM_FREE
));
279 get_vmalloc_info(&vmi
);
280 if (typemask
& VMEM_ALLOC
)
281 size
+= (size_t)vmi
.used
;
283 if (typemask
& VMEM_FREE
)
284 size
+= (size_t)(VMALLOC_TOTAL
- vmi
.used
);
288 EXPORT_SYMBOL(vmem_size
);
295 EXPORT_SYMBOL(kmem_debugging
);
297 #ifndef HAVE_KVASPRINTF
298 /* Simplified asprintf. */
299 char *kvasprintf(gfp_t gfp
, const char *fmt
, va_list ap
)
306 len
= vsnprintf(NULL
, 0, fmt
, aq
);
309 p
= kmalloc(len
+1, gfp
);
313 vsnprintf(p
, len
+1, fmt
, ap
);
317 EXPORT_SYMBOL(kvasprintf
);
318 #endif /* HAVE_KVASPRINTF */
321 kmem_vasprintf(const char *fmt
, va_list ap
)
328 ptr
= kvasprintf(GFP_KERNEL
, fmt
, aq
);
330 } while (ptr
== NULL
);
334 EXPORT_SYMBOL(kmem_vasprintf
);
337 kmem_asprintf(const char *fmt
, ...)
344 ptr
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
346 } while (ptr
== NULL
);
350 EXPORT_SYMBOL(kmem_asprintf
);
353 __strdup(const char *str
, int flags
)
359 ptr
= kmalloc_nofail(n
+ 1, flags
);
361 memcpy(ptr
, str
, n
+ 1);
367 strdup(const char *str
)
369 return __strdup(str
, KM_SLEEP
);
371 EXPORT_SYMBOL(strdup
);
378 EXPORT_SYMBOL(strfree
);
381 * Memory allocation interfaces and debugging for basic kmem_*
382 * and vmem_* style memory allocation. When DEBUG_KMEM is enabled
383 * the SPL will keep track of the total memory allocated, and
384 * report any memory leaked when the module is unloaded.
388 /* Shim layer memory accounting */
389 # ifdef HAVE_ATOMIC64_T
390 atomic64_t kmem_alloc_used
= ATOMIC64_INIT(0);
391 unsigned long long kmem_alloc_max
= 0;
392 atomic64_t vmem_alloc_used
= ATOMIC64_INIT(0);
393 unsigned long long vmem_alloc_max
= 0;
394 # else /* HAVE_ATOMIC64_T */
395 atomic_t kmem_alloc_used
= ATOMIC_INIT(0);
396 unsigned long long kmem_alloc_max
= 0;
397 atomic_t vmem_alloc_used
= ATOMIC_INIT(0);
398 unsigned long long vmem_alloc_max
= 0;
399 # endif /* HAVE_ATOMIC64_T */
401 EXPORT_SYMBOL(kmem_alloc_used
);
402 EXPORT_SYMBOL(kmem_alloc_max
);
403 EXPORT_SYMBOL(vmem_alloc_used
);
404 EXPORT_SYMBOL(vmem_alloc_max
);
406 /* When DEBUG_KMEM_TRACKING is enabled not only will total bytes be tracked
407 * but also the location of every alloc and free. When the SPL module is
408 * unloaded a list of all leaked addresses and where they were allocated
409 * will be dumped to the console. Enabling this feature has a significant
410 * impact on performance but it makes finding memory leaks straight forward.
412 * Not surprisingly with debugging enabled the xmem_locks are very highly
413 * contended particularly on xfree(). If we want to run with this detailed
414 * debugging enabled for anything other than debugging we need to minimize
415 * the contention by moving to a lock per xmem_table entry model.
417 # ifdef DEBUG_KMEM_TRACKING
419 # define KMEM_HASH_BITS 10
420 # define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
422 # define VMEM_HASH_BITS 10
423 # define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS)
425 typedef struct kmem_debug
{
426 struct hlist_node kd_hlist
; /* Hash node linkage */
427 struct list_head kd_list
; /* List of all allocations */
428 void *kd_addr
; /* Allocation pointer */
429 size_t kd_size
; /* Allocation size */
430 const char *kd_func
; /* Allocation function */
431 int kd_line
; /* Allocation line */
434 spinlock_t kmem_lock
;
435 struct hlist_head kmem_table
[KMEM_TABLE_SIZE
];
436 struct list_head kmem_list
;
438 spinlock_t vmem_lock
;
439 struct hlist_head vmem_table
[VMEM_TABLE_SIZE
];
440 struct list_head vmem_list
;
442 EXPORT_SYMBOL(kmem_lock
);
443 EXPORT_SYMBOL(kmem_table
);
444 EXPORT_SYMBOL(kmem_list
);
446 EXPORT_SYMBOL(vmem_lock
);
447 EXPORT_SYMBOL(vmem_table
);
448 EXPORT_SYMBOL(vmem_list
);
450 static kmem_debug_t
*
451 kmem_del_init(spinlock_t
*lock
, struct hlist_head
*table
, int bits
, const void *addr
)
453 struct hlist_head
*head
;
454 struct hlist_node
*node
;
455 struct kmem_debug
*p
;
459 spin_lock_irqsave(lock
, flags
);
461 head
= &table
[hash_ptr((void *)addr
, bits
)];
462 hlist_for_each(node
, head
) {
463 p
= list_entry(node
, struct kmem_debug
, kd_hlist
);
464 if (p
->kd_addr
== addr
) {
465 hlist_del_init(&p
->kd_hlist
);
466 list_del_init(&p
->kd_list
);
467 spin_unlock_irqrestore(lock
, flags
);
472 spin_unlock_irqrestore(lock
, flags
);
478 kmem_alloc_track(size_t size
, int flags
, const char *func
, int line
,
479 int node_alloc
, int node
)
483 unsigned long irq_flags
;
486 /* Function may be called with KM_NOSLEEP so failure is possible */
487 dptr
= (kmem_debug_t
*) kmalloc_nofail(sizeof(kmem_debug_t
),
488 flags
& ~__GFP_ZERO
);
490 if (unlikely(dptr
== NULL
)) {
491 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "debug "
492 "kmem_alloc(%ld, 0x%x) at %s:%d failed (%lld/%llu)\n",
493 sizeof(kmem_debug_t
), flags
, func
, line
,
494 kmem_alloc_used_read(), kmem_alloc_max
);
497 * Marked unlikely because we should never be doing this,
498 * we tolerate to up 2 pages but a single page is best.
500 if (unlikely((size
> PAGE_SIZE
*2) && !(flags
& KM_NODEBUG
))) {
501 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "large "
502 "kmem_alloc(%llu, 0x%x) at %s:%d (%lld/%llu)\n",
503 (unsigned long long) size
, flags
, func
, line
,
504 kmem_alloc_used_read(), kmem_alloc_max
);
505 spl_debug_dumpstack(NULL
);
509 * We use __strdup() below because the string pointed to by
510 * __FUNCTION__ might not be available by the time we want
511 * to print it since the module might have been unloaded.
512 * This can only fail in the KM_NOSLEEP case.
514 dptr
->kd_func
= __strdup(func
, flags
& ~__GFP_ZERO
);
515 if (unlikely(dptr
->kd_func
== NULL
)) {
517 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
518 "debug __strdup() at %s:%d failed (%lld/%llu)\n",
519 func
, line
, kmem_alloc_used_read(), kmem_alloc_max
);
523 /* Use the correct allocator */
525 ASSERT(!(flags
& __GFP_ZERO
));
526 ptr
= kmalloc_node_nofail(size
, flags
, node
);
527 } else if (flags
& __GFP_ZERO
) {
528 ptr
= kzalloc_nofail(size
, flags
& ~__GFP_ZERO
);
530 ptr
= kmalloc_nofail(size
, flags
);
533 if (unlikely(ptr
== NULL
)) {
534 kfree(dptr
->kd_func
);
536 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "kmem_alloc"
537 "(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
538 (unsigned long long) size
, flags
, func
, line
,
539 kmem_alloc_used_read(), kmem_alloc_max
);
543 kmem_alloc_used_add(size
);
544 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max
))
545 kmem_alloc_max
= kmem_alloc_used_read();
547 INIT_HLIST_NODE(&dptr
->kd_hlist
);
548 INIT_LIST_HEAD(&dptr
->kd_list
);
551 dptr
->kd_size
= size
;
552 dptr
->kd_line
= line
;
554 spin_lock_irqsave(&kmem_lock
, irq_flags
);
555 hlist_add_head(&dptr
->kd_hlist
,
556 &kmem_table
[hash_ptr(ptr
, KMEM_HASH_BITS
)]);
557 list_add_tail(&dptr
->kd_list
, &kmem_list
);
558 spin_unlock_irqrestore(&kmem_lock
, irq_flags
);
560 SDEBUG_LIMIT(SD_INFO
,
561 "kmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
562 (unsigned long long) size
, flags
, func
, line
, ptr
,
563 kmem_alloc_used_read(), kmem_alloc_max
);
568 EXPORT_SYMBOL(kmem_alloc_track
);
571 kmem_free_track(const void *ptr
, size_t size
)
576 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
577 (unsigned long long) size
);
579 dptr
= kmem_del_init(&kmem_lock
, kmem_table
, KMEM_HASH_BITS
, ptr
);
581 /* Must exist in hash due to kmem_alloc() */
584 /* Size must match */
585 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
586 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
587 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
589 kmem_alloc_used_sub(size
);
590 SDEBUG_LIMIT(SD_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
591 (unsigned long long) size
, kmem_alloc_used_read(),
594 kfree(dptr
->kd_func
);
596 memset((void *)dptr
, 0x5a, sizeof(kmem_debug_t
));
599 memset((void *)ptr
, 0x5a, size
);
604 EXPORT_SYMBOL(kmem_free_track
);
607 vmem_alloc_track(size_t size
, int flags
, const char *func
, int line
)
611 unsigned long irq_flags
;
614 ASSERT(flags
& KM_SLEEP
);
616 /* Function may be called with KM_NOSLEEP so failure is possible */
617 dptr
= (kmem_debug_t
*) kmalloc_nofail(sizeof(kmem_debug_t
),
618 flags
& ~__GFP_ZERO
);
619 if (unlikely(dptr
== NULL
)) {
620 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "debug "
621 "vmem_alloc(%ld, 0x%x) at %s:%d failed (%lld/%llu)\n",
622 sizeof(kmem_debug_t
), flags
, func
, line
,
623 vmem_alloc_used_read(), vmem_alloc_max
);
626 * We use __strdup() below because the string pointed to by
627 * __FUNCTION__ might not be available by the time we want
628 * to print it, since the module might have been unloaded.
629 * This can never fail because we have already asserted
630 * that flags is KM_SLEEP.
632 dptr
->kd_func
= __strdup(func
, flags
& ~__GFP_ZERO
);
633 if (unlikely(dptr
->kd_func
== NULL
)) {
635 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
636 "debug __strdup() at %s:%d failed (%lld/%llu)\n",
637 func
, line
, vmem_alloc_used_read(), vmem_alloc_max
);
641 /* Use the correct allocator */
642 if (flags
& __GFP_ZERO
) {
643 ptr
= vzalloc_nofail(size
, flags
& ~__GFP_ZERO
);
645 ptr
= vmalloc_nofail(size
, flags
);
648 if (unlikely(ptr
== NULL
)) {
649 kfree(dptr
->kd_func
);
651 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "vmem_alloc"
652 "(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
653 (unsigned long long) size
, flags
, func
, line
,
654 vmem_alloc_used_read(), vmem_alloc_max
);
658 vmem_alloc_used_add(size
);
659 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max
))
660 vmem_alloc_max
= vmem_alloc_used_read();
662 INIT_HLIST_NODE(&dptr
->kd_hlist
);
663 INIT_LIST_HEAD(&dptr
->kd_list
);
666 dptr
->kd_size
= size
;
667 dptr
->kd_line
= line
;
669 spin_lock_irqsave(&vmem_lock
, irq_flags
);
670 hlist_add_head(&dptr
->kd_hlist
,
671 &vmem_table
[hash_ptr(ptr
, VMEM_HASH_BITS
)]);
672 list_add_tail(&dptr
->kd_list
, &vmem_list
);
673 spin_unlock_irqrestore(&vmem_lock
, irq_flags
);
675 SDEBUG_LIMIT(SD_INFO
,
676 "vmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
677 (unsigned long long) size
, flags
, func
, line
,
678 ptr
, vmem_alloc_used_read(), vmem_alloc_max
);
683 EXPORT_SYMBOL(vmem_alloc_track
);
686 vmem_free_track(const void *ptr
, size_t size
)
691 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
692 (unsigned long long) size
);
694 dptr
= kmem_del_init(&vmem_lock
, vmem_table
, VMEM_HASH_BITS
, ptr
);
696 /* Must exist in hash due to vmem_alloc() */
699 /* Size must match */
700 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
701 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
702 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
704 vmem_alloc_used_sub(size
);
705 SDEBUG_LIMIT(SD_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
706 (unsigned long long) size
, vmem_alloc_used_read(),
709 kfree(dptr
->kd_func
);
711 memset((void *)dptr
, 0x5a, sizeof(kmem_debug_t
));
714 memset((void *)ptr
, 0x5a, size
);
719 EXPORT_SYMBOL(vmem_free_track
);
721 # else /* DEBUG_KMEM_TRACKING */
724 kmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
,
725 int node_alloc
, int node
)
731 * Marked unlikely because we should never be doing this,
732 * we tolerate to up 2 pages but a single page is best.
734 if (unlikely((size
> PAGE_SIZE
* 2) && !(flags
& KM_NODEBUG
))) {
735 SDEBUG(SD_CONSOLE
| SD_WARNING
,
736 "large kmem_alloc(%llu, 0x%x) at %s:%d (%lld/%llu)\n",
737 (unsigned long long) size
, flags
, func
, line
,
738 kmem_alloc_used_read(), kmem_alloc_max
);
739 spl_debug_dumpstack(NULL
);
742 /* Use the correct allocator */
744 ASSERT(!(flags
& __GFP_ZERO
));
745 ptr
= kmalloc_node_nofail(size
, flags
, node
);
746 } else if (flags
& __GFP_ZERO
) {
747 ptr
= kzalloc_nofail(size
, flags
& (~__GFP_ZERO
));
749 ptr
= kmalloc_nofail(size
, flags
);
752 if (unlikely(ptr
== NULL
)) {
753 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
754 "kmem_alloc(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
755 (unsigned long long) size
, flags
, func
, line
,
756 kmem_alloc_used_read(), kmem_alloc_max
);
758 kmem_alloc_used_add(size
);
759 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max
))
760 kmem_alloc_max
= kmem_alloc_used_read();
762 SDEBUG_LIMIT(SD_INFO
,
763 "kmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
764 (unsigned long long) size
, flags
, func
, line
, ptr
,
765 kmem_alloc_used_read(), kmem_alloc_max
);
770 EXPORT_SYMBOL(kmem_alloc_debug
);
773 kmem_free_debug(const void *ptr
, size_t size
)
777 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
778 (unsigned long long) size
);
780 kmem_alloc_used_sub(size
);
781 SDEBUG_LIMIT(SD_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
782 (unsigned long long) size
, kmem_alloc_used_read(),
788 EXPORT_SYMBOL(kmem_free_debug
);
791 vmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
)
796 ASSERT(flags
& KM_SLEEP
);
798 /* Use the correct allocator */
799 if (flags
& __GFP_ZERO
) {
800 ptr
= vzalloc_nofail(size
, flags
& (~__GFP_ZERO
));
802 ptr
= vmalloc_nofail(size
, flags
);
805 if (unlikely(ptr
== NULL
)) {
806 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
807 "vmem_alloc(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
808 (unsigned long long) size
, flags
, func
, line
,
809 vmem_alloc_used_read(), vmem_alloc_max
);
811 vmem_alloc_used_add(size
);
812 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max
))
813 vmem_alloc_max
= vmem_alloc_used_read();
815 SDEBUG_LIMIT(SD_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
816 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
817 vmem_alloc_used_read(), vmem_alloc_max
);
822 EXPORT_SYMBOL(vmem_alloc_debug
);
825 vmem_free_debug(const void *ptr
, size_t size
)
829 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
830 (unsigned long long) size
);
832 vmem_alloc_used_sub(size
);
833 SDEBUG_LIMIT(SD_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
834 (unsigned long long) size
, vmem_alloc_used_read(),
840 EXPORT_SYMBOL(vmem_free_debug
);
842 # endif /* DEBUG_KMEM_TRACKING */
843 #endif /* DEBUG_KMEM */
846 * Slab allocation interfaces
848 * While the Linux slab implementation was inspired by the Solaris
849 * implementation I cannot use it to emulate the Solaris APIs. I
850 * require two features which are not provided by the Linux slab.
852 * 1) Constructors AND destructors. Recent versions of the Linux
853 * kernel have removed support for destructors. This is a deal
854 * breaker for the SPL which contains particularly expensive
855 * initializers for mutex's, condition variables, etc. We also
856 * require a minimal level of cleanup for these data types unlike
857 * many Linux data type which do need to be explicitly destroyed.
859 * 2) Virtual address space backed slab. Callers of the Solaris slab
860 * expect it to work well for both small are very large allocations.
861 * Because of memory fragmentation the Linux slab which is backed
862 * by kmalloc'ed memory performs very badly when confronted with
863 * large numbers of large allocations. Basing the slab on the
864 * virtual address space removes the need for contiguous pages
865 * and greatly improve performance for large allocations.
867 * For these reasons, the SPL has its own slab implementation with
868 * the needed features. It is not as highly optimized as either the
869 * Solaris or Linux slabs, but it should get me most of what is
870 * needed until it can be optimized or obsoleted by another approach.
872 * One serious concern I do have about this method is the relatively
873 * small virtual address space on 32bit arches. This will seriously
874 * constrain the size of the slab caches and their performance.
876 * XXX: Improve the partial slab list by carefully maintaining a
877 * strict ordering of fullest to emptiest slabs based on
878 * the slab reference count. This guarantees the when freeing
879 * slabs back to the system we need only linearly traverse the
880 * last N slabs in the list to discover all the freeable slabs.
882 * XXX: NUMA awareness for optionally allocating memory close to a
883 * particular core. This can be advantageous if you know the slab
884 * object will be short lived and primarily accessed from one core.
886 * XXX: Slab coloring may also yield performance improvements and would
887 * be desirable to implement.
890 struct list_head spl_kmem_cache_list
; /* List of caches */
891 struct rw_semaphore spl_kmem_cache_sem
; /* Cache list lock */
892 taskq_t
*spl_kmem_cache_taskq
; /* Task queue for ageing / reclaim */
894 static void spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
);
896 SPL_SHRINKER_CALLBACK_FWD_DECLARE(spl_kmem_cache_generic_shrinker
);
897 SPL_SHRINKER_DECLARE(spl_kmem_cache_shrinker
,
898 spl_kmem_cache_generic_shrinker
, KMC_DEFAULT_SEEKS
);
901 kv_alloc(spl_kmem_cache_t
*skc
, int size
, int flags
)
907 if (skc
->skc_flags
& KMC_KMEM
)
908 ptr
= (void *)__get_free_pages(flags
| __GFP_COMP
,
911 ptr
= __vmalloc(size
, flags
| __GFP_HIGHMEM
, PAGE_KERNEL
);
913 /* Resulting allocated memory will be page aligned */
914 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
920 kv_free(spl_kmem_cache_t
*skc
, void *ptr
, int size
)
922 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
926 * The Linux direct reclaim path uses this out of band value to
927 * determine if forward progress is being made. Normally this is
928 * incremented by kmem_freepages() which is part of the various
929 * Linux slab implementations. However, since we are using none
930 * of that infrastructure we are responsible for incrementing it.
932 if (current
->reclaim_state
)
933 current
->reclaim_state
->reclaimed_slab
+= size
>> PAGE_SHIFT
;
935 if (skc
->skc_flags
& KMC_KMEM
)
936 free_pages((unsigned long)ptr
, get_order(size
));
942 * Required space for each aligned sks.
944 static inline uint32_t
945 spl_sks_size(spl_kmem_cache_t
*skc
)
947 return P2ROUNDUP_TYPED(sizeof(spl_kmem_slab_t
),
948 skc
->skc_obj_align
, uint32_t);
952 * Required space for each aligned object.
954 static inline uint32_t
955 spl_obj_size(spl_kmem_cache_t
*skc
)
957 uint32_t align
= skc
->skc_obj_align
;
959 return P2ROUNDUP_TYPED(skc
->skc_obj_size
, align
, uint32_t) +
960 P2ROUNDUP_TYPED(sizeof(spl_kmem_obj_t
), align
, uint32_t);
964 * Lookup the spl_kmem_object_t for an object given that object.
966 static inline spl_kmem_obj_t
*
967 spl_sko_from_obj(spl_kmem_cache_t
*skc
, void *obj
)
969 return obj
+ P2ROUNDUP_TYPED(skc
->skc_obj_size
,
970 skc
->skc_obj_align
, uint32_t);
974 * Required space for each offslab object taking in to account alignment
975 * restrictions and the power-of-two requirement of kv_alloc().
977 static inline uint32_t
978 spl_offslab_size(spl_kmem_cache_t
*skc
)
980 return 1UL << (highbit(spl_obj_size(skc
)) + 1);
984 * It's important that we pack the spl_kmem_obj_t structure and the
985 * actual objects in to one large address space to minimize the number
986 * of calls to the allocator. It is far better to do a few large
987 * allocations and then subdivide it ourselves. Now which allocator
988 * we use requires balancing a few trade offs.
990 * For small objects we use kmem_alloc() because as long as you are
991 * only requesting a small number of pages (ideally just one) its cheap.
992 * However, when you start requesting multiple pages with kmem_alloc()
993 * it gets increasingly expensive since it requires contiguous pages.
994 * For this reason we shift to vmem_alloc() for slabs of large objects
995 * which removes the need for contiguous pages. We do not use
996 * vmem_alloc() in all cases because there is significant locking
997 * overhead in __get_vm_area_node(). This function takes a single
998 * global lock when acquiring an available virtual address range which
999 * serializes all vmem_alloc()'s for all slab caches. Using slightly
1000 * different allocation functions for small and large objects should
1001 * give us the best of both worlds.
1003 * KMC_ONSLAB KMC_OFFSLAB
1005 * +------------------------+ +-----------------+
1006 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
1007 * | skc_obj_size <-+ | | +-----------------+ | |
1008 * | spl_kmem_obj_t | | | |
1009 * | skc_obj_size <---+ | +-----------------+ | |
1010 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
1011 * | ... v | | spl_kmem_obj_t | |
1012 * +------------------------+ +-----------------+ v
1014 static spl_kmem_slab_t
*
1015 spl_slab_alloc(spl_kmem_cache_t
*skc
, int flags
)
1017 spl_kmem_slab_t
*sks
;
1018 spl_kmem_obj_t
*sko
, *n
;
1020 uint32_t obj_size
, offslab_size
= 0;
1023 base
= kv_alloc(skc
, skc
->skc_slab_size
, flags
);
1027 sks
= (spl_kmem_slab_t
*)base
;
1028 sks
->sks_magic
= SKS_MAGIC
;
1029 sks
->sks_objs
= skc
->skc_slab_objs
;
1030 sks
->sks_age
= jiffies
;
1031 sks
->sks_cache
= skc
;
1032 INIT_LIST_HEAD(&sks
->sks_list
);
1033 INIT_LIST_HEAD(&sks
->sks_free_list
);
1035 obj_size
= spl_obj_size(skc
);
1037 if (skc
->skc_flags
& KMC_OFFSLAB
)
1038 offslab_size
= spl_offslab_size(skc
);
1040 for (i
= 0; i
< sks
->sks_objs
; i
++) {
1041 if (skc
->skc_flags
& KMC_OFFSLAB
) {
1042 obj
= kv_alloc(skc
, offslab_size
, flags
);
1044 SGOTO(out
, rc
= -ENOMEM
);
1046 obj
= base
+ spl_sks_size(skc
) + (i
* obj_size
);
1049 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
1050 sko
= spl_sko_from_obj(skc
, obj
);
1051 sko
->sko_addr
= obj
;
1052 sko
->sko_magic
= SKO_MAGIC
;
1053 sko
->sko_slab
= sks
;
1054 INIT_LIST_HEAD(&sko
->sko_list
);
1055 list_add_tail(&sko
->sko_list
, &sks
->sks_free_list
);
1058 list_for_each_entry(sko
, &sks
->sks_free_list
, sko_list
)
1060 skc
->skc_ctor(sko
->sko_addr
, skc
->skc_private
, flags
);
1063 if (skc
->skc_flags
& KMC_OFFSLAB
)
1064 list_for_each_entry_safe(sko
, n
, &sks
->sks_free_list
,
1066 kv_free(skc
, sko
->sko_addr
, offslab_size
);
1068 kv_free(skc
, base
, skc
->skc_slab_size
);
1076 * Remove a slab from complete or partial list, it must be called with
1077 * the 'skc->skc_lock' held but the actual free must be performed
1078 * outside the lock to prevent deadlocking on vmem addresses.
1081 spl_slab_free(spl_kmem_slab_t
*sks
,
1082 struct list_head
*sks_list
, struct list_head
*sko_list
)
1084 spl_kmem_cache_t
*skc
;
1087 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1088 ASSERT(sks
->sks_ref
== 0);
1090 skc
= sks
->sks_cache
;
1091 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1092 ASSERT(spin_is_locked(&skc
->skc_lock
));
1095 * Update slab/objects counters in the cache, then remove the
1096 * slab from the skc->skc_partial_list. Finally add the slab
1097 * and all its objects in to the private work lists where the
1098 * destructors will be called and the memory freed to the system.
1100 skc
->skc_obj_total
-= sks
->sks_objs
;
1101 skc
->skc_slab_total
--;
1102 list_del(&sks
->sks_list
);
1103 list_add(&sks
->sks_list
, sks_list
);
1104 list_splice_init(&sks
->sks_free_list
, sko_list
);
1110 * Traverses all the partial slabs attached to a cache and free those
1111 * which which are currently empty, and have not been touched for
1112 * skc_delay seconds to avoid thrashing. The count argument is
1113 * passed to optionally cap the number of slabs reclaimed, a count
1114 * of zero means try and reclaim everything. When flag is set we
1115 * always free an available slab regardless of age.
1118 spl_slab_reclaim(spl_kmem_cache_t
*skc
, int count
, int flag
)
1120 spl_kmem_slab_t
*sks
, *m
;
1121 spl_kmem_obj_t
*sko
, *n
;
1122 LIST_HEAD(sks_list
);
1123 LIST_HEAD(sko_list
);
1129 * Move empty slabs and objects which have not been touched in
1130 * skc_delay seconds on to private lists to be freed outside
1131 * the spin lock. This delay time is important to avoid thrashing
1132 * however when flag is set the delay will not be used.
1134 spin_lock(&skc
->skc_lock
);
1135 list_for_each_entry_safe_reverse(sks
,m
,&skc
->skc_partial_list
,sks_list
){
1137 * All empty slabs are at the end of skc->skc_partial_list,
1138 * therefore once a non-empty slab is found we can stop
1139 * scanning. Additionally, stop when reaching the target
1140 * reclaim 'count' if a non-zero threshold is given.
1142 if ((sks
->sks_ref
> 0) || (count
&& i
>= count
))
1145 if (time_after(jiffies
,sks
->sks_age
+skc
->skc_delay
*HZ
)||flag
) {
1146 spl_slab_free(sks
, &sks_list
, &sko_list
);
1150 spin_unlock(&skc
->skc_lock
);
1153 * The following two loops ensure all the object destructors are
1154 * run, any offslab objects are freed, and the slabs themselves
1155 * are freed. This is all done outside the skc->skc_lock since
1156 * this allows the destructor to sleep, and allows us to perform
1157 * a conditional reschedule when a freeing a large number of
1158 * objects and slabs back to the system.
1160 if (skc
->skc_flags
& KMC_OFFSLAB
)
1161 size
= spl_offslab_size(skc
);
1163 list_for_each_entry_safe(sko
, n
, &sko_list
, sko_list
) {
1164 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1167 skc
->skc_dtor(sko
->sko_addr
, skc
->skc_private
);
1169 if (skc
->skc_flags
& KMC_OFFSLAB
)
1170 kv_free(skc
, sko
->sko_addr
, size
);
1173 list_for_each_entry_safe(sks
, m
, &sks_list
, sks_list
) {
1174 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1175 kv_free(skc
, sks
, skc
->skc_slab_size
);
1181 static spl_kmem_emergency_t
*
1182 spl_emergency_search(struct rb_root
*root
, void *obj
)
1184 struct rb_node
*node
= root
->rb_node
;
1185 spl_kmem_emergency_t
*ske
;
1186 unsigned long address
= (unsigned long)obj
;
1189 ske
= container_of(node
, spl_kmem_emergency_t
, ske_node
);
1191 if (address
< (unsigned long)ske
->ske_obj
)
1192 node
= node
->rb_left
;
1193 else if (address
> (unsigned long)ske
->ske_obj
)
1194 node
= node
->rb_right
;
1203 spl_emergency_insert(struct rb_root
*root
, spl_kmem_emergency_t
*ske
)
1205 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1206 spl_kmem_emergency_t
*ske_tmp
;
1207 unsigned long address
= (unsigned long)ske
->ske_obj
;
1210 ske_tmp
= container_of(*new, spl_kmem_emergency_t
, ske_node
);
1213 if (address
< (unsigned long)ske_tmp
->ske_obj
)
1214 new = &((*new)->rb_left
);
1215 else if (address
> (unsigned long)ske_tmp
->ske_obj
)
1216 new = &((*new)->rb_right
);
1221 rb_link_node(&ske
->ske_node
, parent
, new);
1222 rb_insert_color(&ske
->ske_node
, root
);
1228 * Allocate a single emergency object and track it in a red black tree.
1231 spl_emergency_alloc(spl_kmem_cache_t
*skc
, int flags
, void **obj
)
1233 spl_kmem_emergency_t
*ske
;
1237 /* Last chance use a partial slab if one now exists */
1238 spin_lock(&skc
->skc_lock
);
1239 empty
= list_empty(&skc
->skc_partial_list
);
1240 spin_unlock(&skc
->skc_lock
);
1244 ske
= kmalloc(sizeof(*ske
), flags
);
1248 ske
->ske_obj
= kmalloc(skc
->skc_obj_size
, flags
);
1249 if (ske
->ske_obj
== NULL
) {
1254 spin_lock(&skc
->skc_lock
);
1255 empty
= spl_emergency_insert(&skc
->skc_emergency_tree
, ske
);
1256 if (likely(empty
)) {
1257 skc
->skc_obj_total
++;
1258 skc
->skc_obj_emergency
++;
1259 if (skc
->skc_obj_emergency
> skc
->skc_obj_emergency_max
)
1260 skc
->skc_obj_emergency_max
= skc
->skc_obj_emergency
;
1262 spin_unlock(&skc
->skc_lock
);
1264 if (unlikely(!empty
)) {
1265 kfree(ske
->ske_obj
);
1271 skc
->skc_ctor(ske
->ske_obj
, skc
->skc_private
, flags
);
1273 *obj
= ske
->ske_obj
;
1279 * Locate the passed object in the red black tree and free it.
1282 spl_emergency_free(spl_kmem_cache_t
*skc
, void *obj
)
1284 spl_kmem_emergency_t
*ske
;
1287 spin_lock(&skc
->skc_lock
);
1288 ske
= spl_emergency_search(&skc
->skc_emergency_tree
, obj
);
1290 rb_erase(&ske
->ske_node
, &skc
->skc_emergency_tree
);
1291 skc
->skc_obj_emergency
--;
1292 skc
->skc_obj_total
--;
1294 spin_unlock(&skc
->skc_lock
);
1296 if (unlikely(ske
== NULL
))
1300 skc
->skc_dtor(ske
->ske_obj
, skc
->skc_private
);
1302 kfree(ske
->ske_obj
);
1309 * Release objects from the per-cpu magazine back to their slab. The flush
1310 * argument contains the max number of entries to remove from the magazine.
1313 __spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
1315 int i
, count
= MIN(flush
, skm
->skm_avail
);
1318 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1319 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1320 ASSERT(spin_is_locked(&skc
->skc_lock
));
1322 for (i
= 0; i
< count
; i
++)
1323 spl_cache_shrink(skc
, skm
->skm_objs
[i
]);
1325 skm
->skm_avail
-= count
;
1326 memmove(skm
->skm_objs
, &(skm
->skm_objs
[count
]),
1327 sizeof(void *) * skm
->skm_avail
);
1333 spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
1335 spin_lock(&skc
->skc_lock
);
1336 __spl_cache_flush(skc
, skm
, flush
);
1337 spin_unlock(&skc
->skc_lock
);
1341 spl_magazine_age(void *data
)
1343 spl_kmem_cache_t
*skc
= (spl_kmem_cache_t
*)data
;
1344 spl_kmem_magazine_t
*skm
= skc
->skc_mag
[smp_processor_id()];
1346 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1347 ASSERT(skm
->skm_cpu
== smp_processor_id());
1348 ASSERT(irqs_disabled());
1350 /* There are no available objects or they are too young to age out */
1351 if ((skm
->skm_avail
== 0) ||
1352 time_before(jiffies
, skm
->skm_age
+ skc
->skc_delay
* HZ
))
1356 * Because we're executing in interrupt context we may have
1357 * interrupted the holder of this lock. To avoid a potential
1358 * deadlock return if the lock is contended.
1360 if (!spin_trylock(&skc
->skc_lock
))
1363 __spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1364 spin_unlock(&skc
->skc_lock
);
1368 * Called regularly to keep a downward pressure on the cache.
1370 * Objects older than skc->skc_delay seconds in the per-cpu magazines will
1371 * be returned to the caches. This is done to prevent idle magazines from
1372 * holding memory which could be better used elsewhere. The delay is
1373 * present to prevent thrashing the magazine.
1375 * The newly released objects may result in empty partial slabs. Those
1376 * slabs should be released to the system. Otherwise moving the objects
1377 * out of the magazines is just wasted work.
1380 spl_cache_age(void *data
)
1382 spl_kmem_cache_t
*skc
= (spl_kmem_cache_t
*)data
;
1385 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1387 /* Dynamically disabled at run time */
1388 if (!(spl_kmem_cache_expire
& KMC_EXPIRE_AGE
))
1391 atomic_inc(&skc
->skc_ref
);
1393 if (!(skc
->skc_flags
& KMC_NOMAGAZINE
))
1394 spl_on_each_cpu(spl_magazine_age
, skc
, 1);
1396 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
1398 while (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
) && !id
) {
1399 id
= taskq_dispatch_delay(
1400 spl_kmem_cache_taskq
, spl_cache_age
, skc
, TQ_SLEEP
,
1401 ddi_get_lbolt() + skc
->skc_delay
/ 3 * HZ
);
1403 /* Destroy issued after dispatch immediately cancel it */
1404 if (test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
) && id
)
1405 taskq_cancel_id(spl_kmem_cache_taskq
, id
);
1408 spin_lock(&skc
->skc_lock
);
1409 skc
->skc_taskqid
= id
;
1410 spin_unlock(&skc
->skc_lock
);
1412 atomic_dec(&skc
->skc_ref
);
1416 * Size a slab based on the size of each aligned object plus spl_kmem_obj_t.
1417 * When on-slab we want to target spl_kmem_cache_obj_per_slab. However,
1418 * for very small objects we may end up with more than this so as not
1419 * to waste space in the minimal allocation of a single page. Also for
1420 * very large objects we may use as few as spl_kmem_cache_obj_per_slab_min,
1421 * lower than this and we will fail.
1424 spl_slab_size(spl_kmem_cache_t
*skc
, uint32_t *objs
, uint32_t *size
)
1426 uint32_t sks_size
, obj_size
, max_size
;
1428 if (skc
->skc_flags
& KMC_OFFSLAB
) {
1429 *objs
= spl_kmem_cache_obj_per_slab
;
1430 *size
= P2ROUNDUP(sizeof(spl_kmem_slab_t
), PAGE_SIZE
);
1433 sks_size
= spl_sks_size(skc
);
1434 obj_size
= spl_obj_size(skc
);
1436 if (skc
->skc_flags
& KMC_KMEM
)
1437 max_size
= ((uint32_t)1 << (MAX_ORDER
-3)) * PAGE_SIZE
;
1439 max_size
= (spl_kmem_cache_max_size
* 1024 * 1024);
1441 /* Power of two sized slab */
1442 for (*size
= PAGE_SIZE
; *size
<= max_size
; *size
*= 2) {
1443 *objs
= (*size
- sks_size
) / obj_size
;
1444 if (*objs
>= spl_kmem_cache_obj_per_slab
)
1449 * Unable to satisfy target objects per slab, fall back to
1450 * allocating a maximally sized slab and assuming it can
1451 * contain the minimum objects count use it. If not fail.
1454 *objs
= (*size
- sks_size
) / obj_size
;
1455 if (*objs
>= (spl_kmem_cache_obj_per_slab_min
))
1463 * Make a guess at reasonable per-cpu magazine size based on the size of
1464 * each object and the cost of caching N of them in each magazine. Long
1465 * term this should really adapt based on an observed usage heuristic.
1468 spl_magazine_size(spl_kmem_cache_t
*skc
)
1470 uint32_t obj_size
= spl_obj_size(skc
);
1474 /* Per-magazine sizes below assume a 4Kib page size */
1475 if (obj_size
> (PAGE_SIZE
* 256))
1476 size
= 4; /* Minimum 4Mib per-magazine */
1477 else if (obj_size
> (PAGE_SIZE
* 32))
1478 size
= 16; /* Minimum 2Mib per-magazine */
1479 else if (obj_size
> (PAGE_SIZE
))
1480 size
= 64; /* Minimum 256Kib per-magazine */
1481 else if (obj_size
> (PAGE_SIZE
/ 4))
1482 size
= 128; /* Minimum 128Kib per-magazine */
1490 * Allocate a per-cpu magazine to associate with a specific core.
1492 static spl_kmem_magazine_t
*
1493 spl_magazine_alloc(spl_kmem_cache_t
*skc
, int cpu
)
1495 spl_kmem_magazine_t
*skm
;
1496 int size
= sizeof(spl_kmem_magazine_t
) +
1497 sizeof(void *) * skc
->skc_mag_size
;
1500 skm
= kmem_alloc_node(size
, KM_SLEEP
, cpu_to_node(cpu
));
1502 skm
->skm_magic
= SKM_MAGIC
;
1504 skm
->skm_size
= skc
->skc_mag_size
;
1505 skm
->skm_refill
= skc
->skc_mag_refill
;
1506 skm
->skm_cache
= skc
;
1507 skm
->skm_age
= jiffies
;
1515 * Free a per-cpu magazine associated with a specific core.
1518 spl_magazine_free(spl_kmem_magazine_t
*skm
)
1520 int size
= sizeof(spl_kmem_magazine_t
) +
1521 sizeof(void *) * skm
->skm_size
;
1524 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1525 ASSERT(skm
->skm_avail
== 0);
1527 kmem_free(skm
, size
);
1532 * Create all pre-cpu magazines of reasonable sizes.
1535 spl_magazine_create(spl_kmem_cache_t
*skc
)
1540 if (skc
->skc_flags
& KMC_NOMAGAZINE
)
1543 skc
->skc_mag_size
= spl_magazine_size(skc
);
1544 skc
->skc_mag_refill
= (skc
->skc_mag_size
+ 1) / 2;
1546 for_each_online_cpu(i
) {
1547 skc
->skc_mag
[i
] = spl_magazine_alloc(skc
, i
);
1548 if (!skc
->skc_mag
[i
]) {
1549 for (i
--; i
>= 0; i
--)
1550 spl_magazine_free(skc
->skc_mag
[i
]);
1560 * Destroy all pre-cpu magazines.
1563 spl_magazine_destroy(spl_kmem_cache_t
*skc
)
1565 spl_kmem_magazine_t
*skm
;
1569 if (skc
->skc_flags
& KMC_NOMAGAZINE
) {
1574 for_each_online_cpu(i
) {
1575 skm
= skc
->skc_mag
[i
];
1576 spl_cache_flush(skc
, skm
, skm
->skm_avail
);
1577 spl_magazine_free(skm
);
1584 * Create a object cache based on the following arguments:
1586 * size cache object size
1587 * align cache object alignment
1588 * ctor cache object constructor
1589 * dtor cache object destructor
1590 * reclaim cache object reclaim
1591 * priv cache private data for ctor/dtor/reclaim
1592 * vmp unused must be NULL
1594 * KMC_NOTOUCH Disable cache object aging (unsupported)
1595 * KMC_NODEBUG Disable debugging (unsupported)
1596 * KMC_NOHASH Disable hashing (unsupported)
1597 * KMC_QCACHE Disable qcache (unsupported)
1598 * KMC_NOMAGAZINE Enabled for kmem/vmem, Disabled for Linux slab
1599 * KMC_KMEM Force kmem backed cache
1600 * KMC_VMEM Force vmem backed cache
1601 * KMC_SLAB Force Linux slab backed cache
1602 * KMC_OFFSLAB Locate objects off the slab
1605 spl_kmem_cache_create(char *name
, size_t size
, size_t align
,
1606 spl_kmem_ctor_t ctor
,
1607 spl_kmem_dtor_t dtor
,
1608 spl_kmem_reclaim_t reclaim
,
1609 void *priv
, void *vmp
, int flags
)
1611 spl_kmem_cache_t
*skc
;
1615 ASSERTF(!(flags
& KMC_NOMAGAZINE
), "Bad KMC_NOMAGAZINE (%x)\n", flags
);
1616 ASSERTF(!(flags
& KMC_NOHASH
), "Bad KMC_NOHASH (%x)\n", flags
);
1617 ASSERTF(!(flags
& KMC_QCACHE
), "Bad KMC_QCACHE (%x)\n", flags
);
1618 ASSERT(vmp
== NULL
);
1623 * Allocate memory for a new cache an initialize it. Unfortunately,
1624 * this usually ends up being a large allocation of ~32k because
1625 * we need to allocate enough memory for the worst case number of
1626 * cpus in the magazine, skc_mag[NR_CPUS]. Because of this we
1627 * explicitly pass KM_NODEBUG to suppress the kmem warning
1629 skc
= kmem_zalloc(sizeof(*skc
), KM_SLEEP
| KM_NODEBUG
);
1633 skc
->skc_magic
= SKC_MAGIC
;
1634 skc
->skc_name_size
= strlen(name
) + 1;
1635 skc
->skc_name
= (char *)kmem_alloc(skc
->skc_name_size
, KM_SLEEP
);
1636 if (skc
->skc_name
== NULL
) {
1637 kmem_free(skc
, sizeof(*skc
));
1640 strncpy(skc
->skc_name
, name
, skc
->skc_name_size
);
1642 skc
->skc_ctor
= ctor
;
1643 skc
->skc_dtor
= dtor
;
1644 skc
->skc_reclaim
= reclaim
;
1645 skc
->skc_private
= priv
;
1647 skc
->skc_linux_cache
= NULL
;
1648 skc
->skc_flags
= flags
;
1649 skc
->skc_obj_size
= size
;
1650 skc
->skc_obj_align
= SPL_KMEM_CACHE_ALIGN
;
1651 skc
->skc_delay
= SPL_KMEM_CACHE_DELAY
;
1652 skc
->skc_reap
= SPL_KMEM_CACHE_REAP
;
1653 atomic_set(&skc
->skc_ref
, 0);
1655 INIT_LIST_HEAD(&skc
->skc_list
);
1656 INIT_LIST_HEAD(&skc
->skc_complete_list
);
1657 INIT_LIST_HEAD(&skc
->skc_partial_list
);
1658 skc
->skc_emergency_tree
= RB_ROOT
;
1659 spin_lock_init(&skc
->skc_lock
);
1660 init_waitqueue_head(&skc
->skc_waitq
);
1661 skc
->skc_slab_fail
= 0;
1662 skc
->skc_slab_create
= 0;
1663 skc
->skc_slab_destroy
= 0;
1664 skc
->skc_slab_total
= 0;
1665 skc
->skc_slab_alloc
= 0;
1666 skc
->skc_slab_max
= 0;
1667 skc
->skc_obj_total
= 0;
1668 skc
->skc_obj_alloc
= 0;
1669 skc
->skc_obj_max
= 0;
1670 skc
->skc_obj_deadlock
= 0;
1671 skc
->skc_obj_emergency
= 0;
1672 skc
->skc_obj_emergency_max
= 0;
1675 * Verify the requested alignment restriction is sane.
1678 VERIFY(ISP2(align
));
1679 VERIFY3U(align
, >=, SPL_KMEM_CACHE_ALIGN
);
1680 VERIFY3U(align
, <=, PAGE_SIZE
);
1681 skc
->skc_obj_align
= align
;
1685 * When no specific type of slab is requested (kmem, vmem, or
1686 * linuxslab) then select a cache type based on the object size
1687 * and default tunables.
1689 if (!(skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
| KMC_SLAB
))) {
1692 * Objects smaller than spl_kmem_cache_slab_limit can
1693 * use the Linux slab for better space-efficiency. By
1694 * default this functionality is disabled until its
1695 * performance characters are fully understood.
1697 if (spl_kmem_cache_slab_limit
&&
1698 size
<= (size_t)spl_kmem_cache_slab_limit
)
1699 skc
->skc_flags
|= KMC_SLAB
;
1702 * Small objects, less than spl_kmem_cache_kmem_limit per
1703 * object should use kmem because their slabs are small.
1705 else if (spl_obj_size(skc
) <= spl_kmem_cache_kmem_limit
)
1706 skc
->skc_flags
|= KMC_KMEM
;
1709 * All other objects are considered large and are placed
1710 * on vmem backed slabs.
1713 skc
->skc_flags
|= KMC_VMEM
;
1717 * Given the type of slab allocate the required resources.
1719 if (skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
)) {
1720 rc
= spl_slab_size(skc
,
1721 &skc
->skc_slab_objs
, &skc
->skc_slab_size
);
1725 rc
= spl_magazine_create(skc
);
1729 skc
->skc_linux_cache
= kmem_cache_create(
1730 skc
->skc_name
, size
, align
, 0, NULL
);
1731 if (skc
->skc_linux_cache
== NULL
)
1732 SGOTO(out
, rc
= ENOMEM
);
1734 kmem_cache_set_allocflags(skc
, __GFP_COMP
);
1735 skc
->skc_flags
|= KMC_NOMAGAZINE
;
1738 if (spl_kmem_cache_expire
& KMC_EXPIRE_AGE
)
1739 skc
->skc_taskqid
= taskq_dispatch_delay(spl_kmem_cache_taskq
,
1740 spl_cache_age
, skc
, TQ_SLEEP
,
1741 ddi_get_lbolt() + skc
->skc_delay
/ 3 * HZ
);
1743 down_write(&spl_kmem_cache_sem
);
1744 list_add_tail(&skc
->skc_list
, &spl_kmem_cache_list
);
1745 up_write(&spl_kmem_cache_sem
);
1749 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1750 kmem_free(skc
, sizeof(*skc
));
1753 EXPORT_SYMBOL(spl_kmem_cache_create
);
1756 * Register a move callback to for cache defragmentation.
1757 * XXX: Unimplemented but harmless to stub out for now.
1760 spl_kmem_cache_set_move(spl_kmem_cache_t
*skc
,
1761 kmem_cbrc_t (move
)(void *, void *, size_t, void *))
1763 ASSERT(move
!= NULL
);
1765 EXPORT_SYMBOL(spl_kmem_cache_set_move
);
1768 * Destroy a cache and all objects associated with the cache.
1771 spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
)
1773 DECLARE_WAIT_QUEUE_HEAD(wq
);
1777 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1778 ASSERT(skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
| KMC_SLAB
));
1780 down_write(&spl_kmem_cache_sem
);
1781 list_del_init(&skc
->skc_list
);
1782 up_write(&spl_kmem_cache_sem
);
1784 /* Cancel any and wait for any pending delayed tasks */
1785 VERIFY(!test_and_set_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1787 spin_lock(&skc
->skc_lock
);
1788 id
= skc
->skc_taskqid
;
1789 spin_unlock(&skc
->skc_lock
);
1791 taskq_cancel_id(spl_kmem_cache_taskq
, id
);
1793 /* Wait until all current callers complete, this is mainly
1794 * to catch the case where a low memory situation triggers a
1795 * cache reaping action which races with this destroy. */
1796 wait_event(wq
, atomic_read(&skc
->skc_ref
) == 0);
1798 if (skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
)) {
1799 spl_magazine_destroy(skc
);
1800 spl_slab_reclaim(skc
, 0, 1);
1802 ASSERT(skc
->skc_flags
& KMC_SLAB
);
1803 kmem_cache_destroy(skc
->skc_linux_cache
);
1806 spin_lock(&skc
->skc_lock
);
1808 /* Validate there are no objects in use and free all the
1809 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
1810 ASSERT3U(skc
->skc_slab_alloc
, ==, 0);
1811 ASSERT3U(skc
->skc_obj_alloc
, ==, 0);
1812 ASSERT3U(skc
->skc_slab_total
, ==, 0);
1813 ASSERT3U(skc
->skc_obj_total
, ==, 0);
1814 ASSERT3U(skc
->skc_obj_emergency
, ==, 0);
1815 ASSERT(list_empty(&skc
->skc_complete_list
));
1817 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1818 spin_unlock(&skc
->skc_lock
);
1820 kmem_free(skc
, sizeof(*skc
));
1824 EXPORT_SYMBOL(spl_kmem_cache_destroy
);
1827 * Allocate an object from a slab attached to the cache. This is used to
1828 * repopulate the per-cpu magazine caches in batches when they run low.
1831 spl_cache_obj(spl_kmem_cache_t
*skc
, spl_kmem_slab_t
*sks
)
1833 spl_kmem_obj_t
*sko
;
1835 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1836 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1837 ASSERT(spin_is_locked(&skc
->skc_lock
));
1839 sko
= list_entry(sks
->sks_free_list
.next
, spl_kmem_obj_t
, sko_list
);
1840 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1841 ASSERT(sko
->sko_addr
!= NULL
);
1843 /* Remove from sks_free_list */
1844 list_del_init(&sko
->sko_list
);
1846 sks
->sks_age
= jiffies
;
1848 skc
->skc_obj_alloc
++;
1850 /* Track max obj usage statistics */
1851 if (skc
->skc_obj_alloc
> skc
->skc_obj_max
)
1852 skc
->skc_obj_max
= skc
->skc_obj_alloc
;
1854 /* Track max slab usage statistics */
1855 if (sks
->sks_ref
== 1) {
1856 skc
->skc_slab_alloc
++;
1858 if (skc
->skc_slab_alloc
> skc
->skc_slab_max
)
1859 skc
->skc_slab_max
= skc
->skc_slab_alloc
;
1862 return sko
->sko_addr
;
1866 * Generic slab allocation function to run by the global work queues.
1867 * It is responsible for allocating a new slab, linking it in to the list
1868 * of partial slabs, and then waking any waiters.
1871 spl_cache_grow_work(void *data
)
1873 spl_kmem_alloc_t
*ska
= (spl_kmem_alloc_t
*)data
;
1874 spl_kmem_cache_t
*skc
= ska
->ska_cache
;
1875 spl_kmem_slab_t
*sks
;
1877 sks
= spl_slab_alloc(skc
, ska
->ska_flags
| __GFP_NORETRY
| KM_NODEBUG
);
1878 spin_lock(&skc
->skc_lock
);
1880 skc
->skc_slab_total
++;
1881 skc
->skc_obj_total
+= sks
->sks_objs
;
1882 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1885 atomic_dec(&skc
->skc_ref
);
1886 clear_bit(KMC_BIT_GROWING
, &skc
->skc_flags
);
1887 clear_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
);
1888 wake_up_all(&skc
->skc_waitq
);
1889 spin_unlock(&skc
->skc_lock
);
1895 * Returns non-zero when a new slab should be available.
1898 spl_cache_grow_wait(spl_kmem_cache_t
*skc
)
1900 return !test_bit(KMC_BIT_GROWING
, &skc
->skc_flags
);
1904 * No available objects on any slabs, create a new slab. Note that this
1905 * functionality is disabled for KMC_SLAB caches which are backed by the
1909 spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
, void **obj
)
1914 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1915 ASSERT((skc
->skc_flags
& KMC_SLAB
) == 0);
1920 * Before allocating a new slab wait for any reaping to complete and
1921 * then return so the local magazine can be rechecked for new objects.
1923 if (test_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1924 rc
= spl_wait_on_bit(&skc
->skc_flags
, KMC_BIT_REAPING
,
1925 TASK_UNINTERRUPTIBLE
);
1926 SRETURN(rc
? rc
: -EAGAIN
);
1930 * This is handled by dispatching a work request to the global work
1931 * queue. This allows us to asynchronously allocate a new slab while
1932 * retaining the ability to safely fall back to a smaller synchronous
1933 * allocations to ensure forward progress is always maintained.
1935 if (test_and_set_bit(KMC_BIT_GROWING
, &skc
->skc_flags
) == 0) {
1936 spl_kmem_alloc_t
*ska
;
1938 ska
= kmalloc(sizeof(*ska
), flags
);
1940 clear_bit(KMC_BIT_GROWING
, &skc
->skc_flags
);
1941 wake_up_all(&skc
->skc_waitq
);
1945 atomic_inc(&skc
->skc_ref
);
1946 ska
->ska_cache
= skc
;
1947 ska
->ska_flags
= flags
& ~__GFP_FS
;
1948 taskq_init_ent(&ska
->ska_tqe
);
1949 taskq_dispatch_ent(spl_kmem_cache_taskq
,
1950 spl_cache_grow_work
, ska
, 0, &ska
->ska_tqe
);
1954 * The goal here is to only detect the rare case where a virtual slab
1955 * allocation has deadlocked. We must be careful to minimize the use
1956 * of emergency objects which are more expensive to track. Therefore,
1957 * we set a very long timeout for the asynchronous allocation and if
1958 * the timeout is reached the cache is flagged as deadlocked. From
1959 * this point only new emergency objects will be allocated until the
1960 * asynchronous allocation completes and clears the deadlocked flag.
1962 if (test_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
)) {
1963 rc
= spl_emergency_alloc(skc
, flags
, obj
);
1965 remaining
= wait_event_timeout(skc
->skc_waitq
,
1966 spl_cache_grow_wait(skc
), HZ
);
1968 if (!remaining
&& test_bit(KMC_BIT_VMEM
, &skc
->skc_flags
)) {
1969 spin_lock(&skc
->skc_lock
);
1970 if (test_bit(KMC_BIT_GROWING
, &skc
->skc_flags
)) {
1971 set_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
);
1972 skc
->skc_obj_deadlock
++;
1974 spin_unlock(&skc
->skc_lock
);
1984 * Refill a per-cpu magazine with objects from the slabs for this cache.
1985 * Ideally the magazine can be repopulated using existing objects which have
1986 * been released, however if we are unable to locate enough free objects new
1987 * slabs of objects will be created. On success NULL is returned, otherwise
1988 * the address of a single emergency object is returned for use by the caller.
1991 spl_cache_refill(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flags
)
1993 spl_kmem_slab_t
*sks
;
1994 int count
= 0, rc
, refill
;
1998 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1999 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
2001 refill
= MIN(skm
->skm_refill
, skm
->skm_size
- skm
->skm_avail
);
2002 spin_lock(&skc
->skc_lock
);
2004 while (refill
> 0) {
2005 /* No slabs available we may need to grow the cache */
2006 if (list_empty(&skc
->skc_partial_list
)) {
2007 spin_unlock(&skc
->skc_lock
);
2010 rc
= spl_cache_grow(skc
, flags
, &obj
);
2011 local_irq_disable();
2013 /* Emergency object for immediate use by caller */
2014 if (rc
== 0 && obj
!= NULL
)
2020 /* Rescheduled to different CPU skm is not local */
2021 if (skm
!= skc
->skc_mag
[smp_processor_id()])
2024 /* Potentially rescheduled to the same CPU but
2025 * allocations may have occurred from this CPU while
2026 * we were sleeping so recalculate max refill. */
2027 refill
= MIN(refill
, skm
->skm_size
- skm
->skm_avail
);
2029 spin_lock(&skc
->skc_lock
);
2033 /* Grab the next available slab */
2034 sks
= list_entry((&skc
->skc_partial_list
)->next
,
2035 spl_kmem_slab_t
, sks_list
);
2036 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
2037 ASSERT(sks
->sks_ref
< sks
->sks_objs
);
2038 ASSERT(!list_empty(&sks
->sks_free_list
));
2040 /* Consume as many objects as needed to refill the requested
2041 * cache. We must also be careful not to overfill it. */
2042 while (sks
->sks_ref
< sks
->sks_objs
&& refill
-- > 0 && ++count
) {
2043 ASSERT(skm
->skm_avail
< skm
->skm_size
);
2044 ASSERT(count
< skm
->skm_size
);
2045 skm
->skm_objs
[skm
->skm_avail
++]=spl_cache_obj(skc
,sks
);
2048 /* Move slab to skc_complete_list when full */
2049 if (sks
->sks_ref
== sks
->sks_objs
) {
2050 list_del(&sks
->sks_list
);
2051 list_add(&sks
->sks_list
, &skc
->skc_complete_list
);
2055 spin_unlock(&skc
->skc_lock
);
2061 * Release an object back to the slab from which it came.
2064 spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
)
2066 spl_kmem_slab_t
*sks
= NULL
;
2067 spl_kmem_obj_t
*sko
= NULL
;
2070 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
2071 ASSERT(spin_is_locked(&skc
->skc_lock
));
2073 sko
= spl_sko_from_obj(skc
, obj
);
2074 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
2075 sks
= sko
->sko_slab
;
2076 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
2077 ASSERT(sks
->sks_cache
== skc
);
2078 list_add(&sko
->sko_list
, &sks
->sks_free_list
);
2080 sks
->sks_age
= jiffies
;
2082 skc
->skc_obj_alloc
--;
2084 /* Move slab to skc_partial_list when no longer full. Slabs
2085 * are added to the head to keep the partial list is quasi-full
2086 * sorted order. Fuller at the head, emptier at the tail. */
2087 if (sks
->sks_ref
== (sks
->sks_objs
- 1)) {
2088 list_del(&sks
->sks_list
);
2089 list_add(&sks
->sks_list
, &skc
->skc_partial_list
);
2092 /* Move empty slabs to the end of the partial list so
2093 * they can be easily found and freed during reclamation. */
2094 if (sks
->sks_ref
== 0) {
2095 list_del(&sks
->sks_list
);
2096 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
2097 skc
->skc_slab_alloc
--;
2104 * Allocate an object from the per-cpu magazine, or if the magazine
2105 * is empty directly allocate from a slab and repopulate the magazine.
2108 spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
)
2110 spl_kmem_magazine_t
*skm
;
2114 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
2115 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
2116 ASSERT(flags
& KM_SLEEP
);
2118 atomic_inc(&skc
->skc_ref
);
2121 * Allocate directly from a Linux slab. All optimizations are left
2122 * to the underlying cache we only need to guarantee that KM_SLEEP
2123 * callers will never fail.
2125 if (skc
->skc_flags
& KMC_SLAB
) {
2126 struct kmem_cache
*slc
= skc
->skc_linux_cache
;
2129 obj
= kmem_cache_alloc(slc
, flags
| __GFP_COMP
);
2130 if (obj
&& skc
->skc_ctor
)
2131 skc
->skc_ctor(obj
, skc
->skc_private
, flags
);
2133 } while ((obj
== NULL
) && !(flags
& KM_NOSLEEP
));
2135 atomic_dec(&skc
->skc_ref
);
2139 local_irq_disable();
2142 /* Safe to update per-cpu structure without lock, but
2143 * in the restart case we must be careful to reacquire
2144 * the local magazine since this may have changed
2145 * when we need to grow the cache. */
2146 skm
= skc
->skc_mag
[smp_processor_id()];
2147 ASSERTF(skm
->skm_magic
== SKM_MAGIC
, "%x != %x: %s/%p/%p %x/%x/%x\n",
2148 skm
->skm_magic
, SKM_MAGIC
, skc
->skc_name
, skc
, skm
,
2149 skm
->skm_size
, skm
->skm_refill
, skm
->skm_avail
);
2151 if (likely(skm
->skm_avail
)) {
2152 /* Object available in CPU cache, use it */
2153 obj
= skm
->skm_objs
[--skm
->skm_avail
];
2154 skm
->skm_age
= jiffies
;
2156 obj
= spl_cache_refill(skc
, skm
, flags
);
2158 SGOTO(restart
, obj
= NULL
);
2163 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
2165 /* Pre-emptively migrate object to CPU L1 cache */
2167 atomic_dec(&skc
->skc_ref
);
2171 EXPORT_SYMBOL(spl_kmem_cache_alloc
);
2174 * Free an object back to the local per-cpu magazine, there is no
2175 * guarantee that this is the same magazine the object was originally
2176 * allocated from. We may need to flush entire from the magazine
2177 * back to the slabs to make space.
2180 spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
)
2182 spl_kmem_magazine_t
*skm
;
2183 unsigned long flags
;
2186 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
2187 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
2188 atomic_inc(&skc
->skc_ref
);
2191 * Free the object from the Linux underlying Linux slab.
2193 if (skc
->skc_flags
& KMC_SLAB
) {
2195 skc
->skc_dtor(obj
, skc
->skc_private
);
2197 kmem_cache_free(skc
->skc_linux_cache
, obj
);
2202 * Only virtual slabs may have emergency objects and these objects
2203 * are guaranteed to have physical addresses. They must be removed
2204 * from the tree of emergency objects and the freed.
2206 if ((skc
->skc_flags
& KMC_VMEM
) && !kmem_virt(obj
))
2207 SGOTO(out
, spl_emergency_free(skc
, obj
));
2209 local_irq_save(flags
);
2211 /* Safe to update per-cpu structure without lock, but
2212 * no remote memory allocation tracking is being performed
2213 * it is entirely possible to allocate an object from one
2214 * CPU cache and return it to another. */
2215 skm
= skc
->skc_mag
[smp_processor_id()];
2216 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
2218 /* Per-CPU cache full, flush it to make space */
2219 if (unlikely(skm
->skm_avail
>= skm
->skm_size
))
2220 spl_cache_flush(skc
, skm
, skm
->skm_refill
);
2222 /* Available space in cache, use it */
2223 skm
->skm_objs
[skm
->skm_avail
++] = obj
;
2225 local_irq_restore(flags
);
2227 atomic_dec(&skc
->skc_ref
);
2231 EXPORT_SYMBOL(spl_kmem_cache_free
);
2234 * The generic shrinker function for all caches. Under Linux a shrinker
2235 * may not be tightly coupled with a slab cache. In fact Linux always
2236 * systematically tries calling all registered shrinker callbacks which
2237 * report that they contain unused objects. Because of this we only
2238 * register one shrinker function in the shim layer for all slab caches.
2239 * We always attempt to shrink all caches when this generic shrinker
2240 * is called. The shrinker should return the number of free objects
2241 * in the cache when called with nr_to_scan == 0 but not attempt to
2242 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
2243 * objects should be freed, which differs from Solaris semantics.
2244 * Solaris semantics are to free all available objects which may (and
2245 * probably will) be more objects than the requested nr_to_scan.
2248 __spl_kmem_cache_generic_shrinker(struct shrinker
*shrink
,
2249 struct shrink_control
*sc
)
2251 spl_kmem_cache_t
*skc
;
2254 down_read(&spl_kmem_cache_sem
);
2255 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
2257 spl_kmem_cache_reap_now(skc
,
2258 MAX(sc
->nr_to_scan
>> fls64(skc
->skc_slab_objs
), 1));
2261 * Presume everything alloc'ed is reclaimable, this ensures
2262 * we are called again with nr_to_scan > 0 so can try and
2263 * reclaim. The exact number is not important either so
2264 * we forgo taking this already highly contented lock.
2266 alloc
+= skc
->skc_obj_alloc
;
2268 up_read(&spl_kmem_cache_sem
);
2271 * When KMC_RECLAIM_ONCE is set allow only a single reclaim pass.
2272 * This functionality only exists to work around a rare issue where
2273 * shrink_slabs() is repeatedly invoked by many cores causing the
2276 if ((spl_kmem_cache_reclaim
& KMC_RECLAIM_ONCE
) && sc
->nr_to_scan
)
2279 return (MAX(alloc
, 0));
2282 SPL_SHRINKER_CALLBACK_WRAPPER(spl_kmem_cache_generic_shrinker
);
2285 * Call the registered reclaim function for a cache. Depending on how
2286 * many and which objects are released it may simply repopulate the
2287 * local magazine which will then need to age-out. Objects which cannot
2288 * fit in the magazine we will be released back to their slabs which will
2289 * also need to age out before being release. This is all just best
2290 * effort and we do not want to thrash creating and destroying slabs.
2293 spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
, int count
)
2297 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
2298 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
2300 atomic_inc(&skc
->skc_ref
);
2303 * Execute the registered reclaim callback if it exists. The
2304 * per-cpu caches will be drained when is set KMC_EXPIRE_MEM.
2306 if (skc
->skc_flags
& KMC_SLAB
) {
2307 if (skc
->skc_reclaim
)
2308 skc
->skc_reclaim(skc
->skc_private
);
2310 if (spl_kmem_cache_expire
& KMC_EXPIRE_MEM
)
2311 kmem_cache_shrink(skc
->skc_linux_cache
);
2317 * Prevent concurrent cache reaping when contended.
2319 if (test_and_set_bit(KMC_BIT_REAPING
, &skc
->skc_flags
))
2323 * When a reclaim function is available it may be invoked repeatedly
2324 * until at least a single slab can be freed. This ensures that we
2325 * do free memory back to the system. This helps minimize the chance
2326 * of an OOM event when the bulk of memory is used by the slab.
2328 * When free slabs are already available the reclaim callback will be
2329 * skipped. Additionally, if no forward progress is detected despite
2330 * a reclaim function the cache will be skipped to avoid deadlock.
2332 * Longer term this would be the correct place to add the code which
2333 * repacks the slabs in order minimize fragmentation.
2335 if (skc
->skc_reclaim
) {
2336 uint64_t objects
= UINT64_MAX
;
2340 spin_lock(&skc
->skc_lock
);
2342 (skc
->skc_slab_total
> 0) &&
2343 ((skc
->skc_slab_total
- skc
->skc_slab_alloc
) == 0) &&
2344 (skc
->skc_obj_alloc
< objects
);
2346 objects
= skc
->skc_obj_alloc
;
2347 spin_unlock(&skc
->skc_lock
);
2350 skc
->skc_reclaim(skc
->skc_private
);
2352 } while (do_reclaim
);
2355 /* Reclaim from the magazine then the slabs ignoring age and delay. */
2356 if (spl_kmem_cache_expire
& KMC_EXPIRE_MEM
) {
2357 spl_kmem_magazine_t
*skm
;
2358 unsigned long irq_flags
;
2360 local_irq_save(irq_flags
);
2361 skm
= skc
->skc_mag
[smp_processor_id()];
2362 spl_cache_flush(skc
, skm
, skm
->skm_avail
);
2363 local_irq_restore(irq_flags
);
2366 spl_slab_reclaim(skc
, count
, 1);
2367 clear_bit(KMC_BIT_REAPING
, &skc
->skc_flags
);
2369 wake_up_bit(&skc
->skc_flags
, KMC_BIT_REAPING
);
2371 atomic_dec(&skc
->skc_ref
);
2375 EXPORT_SYMBOL(spl_kmem_cache_reap_now
);
2378 * Reap all free slabs from all registered caches.
2383 struct shrink_control sc
;
2385 sc
.nr_to_scan
= KMC_REAP_CHUNK
;
2386 sc
.gfp_mask
= GFP_KERNEL
;
2388 __spl_kmem_cache_generic_shrinker(NULL
, &sc
);
2390 EXPORT_SYMBOL(spl_kmem_reap
);
2392 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
2394 spl_sprintf_addr(kmem_debug_t
*kd
, char *str
, int len
, int min
)
2396 int size
= ((len
- 1) < kd
->kd_size
) ? (len
- 1) : kd
->kd_size
;
2399 ASSERT(str
!= NULL
&& len
>= 17);
2400 memset(str
, 0, len
);
2402 /* Check for a fully printable string, and while we are at
2403 * it place the printable characters in the passed buffer. */
2404 for (i
= 0; i
< size
; i
++) {
2405 str
[i
] = ((char *)(kd
->kd_addr
))[i
];
2406 if (isprint(str
[i
])) {
2409 /* Minimum number of printable characters found
2410 * to make it worthwhile to print this as ascii. */
2420 sprintf(str
, "%02x%02x%02x%02x%02x%02x%02x%02x",
2421 *((uint8_t *)kd
->kd_addr
),
2422 *((uint8_t *)kd
->kd_addr
+ 2),
2423 *((uint8_t *)kd
->kd_addr
+ 4),
2424 *((uint8_t *)kd
->kd_addr
+ 6),
2425 *((uint8_t *)kd
->kd_addr
+ 8),
2426 *((uint8_t *)kd
->kd_addr
+ 10),
2427 *((uint8_t *)kd
->kd_addr
+ 12),
2428 *((uint8_t *)kd
->kd_addr
+ 14));
2435 spl_kmem_init_tracking(struct list_head
*list
, spinlock_t
*lock
, int size
)
2440 spin_lock_init(lock
);
2441 INIT_LIST_HEAD(list
);
2443 for (i
= 0; i
< size
; i
++)
2444 INIT_HLIST_HEAD(&kmem_table
[i
]);
2450 spl_kmem_fini_tracking(struct list_head
*list
, spinlock_t
*lock
)
2452 unsigned long flags
;
2457 spin_lock_irqsave(lock
, flags
);
2458 if (!list_empty(list
))
2459 printk(KERN_WARNING
"%-16s %-5s %-16s %s:%s\n", "address",
2460 "size", "data", "func", "line");
2462 list_for_each_entry(kd
, list
, kd_list
)
2463 printk(KERN_WARNING
"%p %-5d %-16s %s:%d\n", kd
->kd_addr
,
2464 (int)kd
->kd_size
, spl_sprintf_addr(kd
, str
, 17, 8),
2465 kd
->kd_func
, kd
->kd_line
);
2467 spin_unlock_irqrestore(lock
, flags
);
2470 #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
2471 #define spl_kmem_init_tracking(list, lock, size)
2472 #define spl_kmem_fini_tracking(list, lock)
2473 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
2476 spl_kmem_init_globals(void)
2480 /* For now all zones are includes, it may be wise to restrict
2481 * this to normal and highmem zones if we see problems. */
2482 for_each_zone(zone
) {
2484 if (!populated_zone(zone
))
2487 minfree
+= min_wmark_pages(zone
);
2488 desfree
+= low_wmark_pages(zone
);
2489 lotsfree
+= high_wmark_pages(zone
);
2492 /* Solaris default values */
2493 swapfs_minfree
= MAX(2*1024*1024 >> PAGE_SHIFT
, physmem
>> 3);
2494 swapfs_reserve
= MIN(4*1024*1024 >> PAGE_SHIFT
, physmem
>> 4);
2498 * Called at module init when it is safe to use spl_kallsyms_lookup_name()
2501 spl_kmem_init_kallsyms_lookup(void)
2503 #ifndef HAVE_GET_VMALLOC_INFO
2504 get_vmalloc_info_fn
= (get_vmalloc_info_t
)
2505 spl_kallsyms_lookup_name("get_vmalloc_info");
2506 if (!get_vmalloc_info_fn
) {
2507 printk(KERN_ERR
"Error: Unknown symbol get_vmalloc_info\n");
2510 #endif /* HAVE_GET_VMALLOC_INFO */
2512 #ifdef HAVE_PGDAT_HELPERS
2513 # ifndef HAVE_FIRST_ONLINE_PGDAT
2514 first_online_pgdat_fn
= (first_online_pgdat_t
)
2515 spl_kallsyms_lookup_name("first_online_pgdat");
2516 if (!first_online_pgdat_fn
) {
2517 printk(KERN_ERR
"Error: Unknown symbol first_online_pgdat\n");
2520 # endif /* HAVE_FIRST_ONLINE_PGDAT */
2522 # ifndef HAVE_NEXT_ONLINE_PGDAT
2523 next_online_pgdat_fn
= (next_online_pgdat_t
)
2524 spl_kallsyms_lookup_name("next_online_pgdat");
2525 if (!next_online_pgdat_fn
) {
2526 printk(KERN_ERR
"Error: Unknown symbol next_online_pgdat\n");
2529 # endif /* HAVE_NEXT_ONLINE_PGDAT */
2531 # ifndef HAVE_NEXT_ZONE
2532 next_zone_fn
= (next_zone_t
)
2533 spl_kallsyms_lookup_name("next_zone");
2534 if (!next_zone_fn
) {
2535 printk(KERN_ERR
"Error: Unknown symbol next_zone\n");
2538 # endif /* HAVE_NEXT_ZONE */
2540 #else /* HAVE_PGDAT_HELPERS */
2542 # ifndef HAVE_PGDAT_LIST
2543 pgdat_list_addr
= *(struct pglist_data
**)
2544 spl_kallsyms_lookup_name("pgdat_list");
2545 if (!pgdat_list_addr
) {
2546 printk(KERN_ERR
"Error: Unknown symbol pgdat_list\n");
2549 # endif /* HAVE_PGDAT_LIST */
2550 #endif /* HAVE_PGDAT_HELPERS */
2552 #if defined(NEED_GET_ZONE_COUNTS) && !defined(HAVE_GET_ZONE_COUNTS)
2553 get_zone_counts_fn
= (get_zone_counts_t
)
2554 spl_kallsyms_lookup_name("get_zone_counts");
2555 if (!get_zone_counts_fn
) {
2556 printk(KERN_ERR
"Error: Unknown symbol get_zone_counts\n");
2559 #endif /* NEED_GET_ZONE_COUNTS && !HAVE_GET_ZONE_COUNTS */
2562 * It is now safe to initialize the global tunings which rely on
2563 * the use of the for_each_zone() macro. This macro in turns
2564 * depends on the *_pgdat symbols which are now available.
2566 spl_kmem_init_globals();
2568 #ifndef HAVE_SHRINK_DCACHE_MEMORY
2569 /* When shrink_dcache_memory_fn == NULL support is disabled */
2570 shrink_dcache_memory_fn
= (shrink_dcache_memory_t
)
2571 spl_kallsyms_lookup_name("shrink_dcache_memory");
2572 #endif /* HAVE_SHRINK_DCACHE_MEMORY */
2574 #ifndef HAVE_SHRINK_ICACHE_MEMORY
2575 /* When shrink_icache_memory_fn == NULL support is disabled */
2576 shrink_icache_memory_fn
= (shrink_icache_memory_t
)
2577 spl_kallsyms_lookup_name("shrink_icache_memory");
2578 #endif /* HAVE_SHRINK_ICACHE_MEMORY */
2590 kmem_alloc_used_set(0);
2591 vmem_alloc_used_set(0);
2593 spl_kmem_init_tracking(&kmem_list
, &kmem_lock
, KMEM_TABLE_SIZE
);
2594 spl_kmem_init_tracking(&vmem_list
, &vmem_lock
, VMEM_TABLE_SIZE
);
2597 init_rwsem(&spl_kmem_cache_sem
);
2598 INIT_LIST_HEAD(&spl_kmem_cache_list
);
2599 spl_kmem_cache_taskq
= taskq_create("spl_kmem_cache",
2600 1, maxclsyspri
, 1, 32, TASKQ_PREPOPULATE
);
2602 spl_register_shrinker(&spl_kmem_cache_shrinker
);
2612 spl_unregister_shrinker(&spl_kmem_cache_shrinker
);
2613 taskq_destroy(spl_kmem_cache_taskq
);
2616 /* Display all unreclaimed memory addresses, including the
2617 * allocation size and the first few bytes of what's located
2618 * at that address to aid in debugging. Performance is not
2619 * a serious concern here since it is module unload time. */
2620 if (kmem_alloc_used_read() != 0)
2621 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
2622 "kmem leaked %ld/%ld bytes\n",
2623 kmem_alloc_used_read(), kmem_alloc_max
);
2626 if (vmem_alloc_used_read() != 0)
2627 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
2628 "vmem leaked %ld/%ld bytes\n",
2629 vmem_alloc_used_read(), vmem_alloc_max
);
2631 spl_kmem_fini_tracking(&kmem_list
, &kmem_lock
);
2632 spl_kmem_fini_tracking(&vmem_list
, &vmem_lock
);
2633 #endif /* DEBUG_KMEM */