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 * Cache expiration was implemented because it was part of the default Solaris
38 * kmem_cache behavior. The idea is that per-cpu objects which haven't been
39 * accessed in several seconds should be returned to the cache. On the other
40 * hand Linux slabs never move objects back to the slabs unless there is
41 * memory pressure on the system. By default both methods are disabled, but
42 * may be enabled by setting KMC_EXPIRE_AGE or KMC_EXPIRE_MEM.
44 unsigned int spl_kmem_cache_expire
= 0;
45 EXPORT_SYMBOL(spl_kmem_cache_expire
);
46 module_param(spl_kmem_cache_expire
, uint
, 0644);
47 MODULE_PARM_DESC(spl_kmem_cache_expire
, "By age (0x1) or low memory (0x2)");
50 * The minimum amount of memory measured in pages to be free at all
51 * times on the system. This is similar to Linux's zone->pages_min
52 * multiplied by the number of zones and is sized based on that.
55 EXPORT_SYMBOL(minfree
);
58 * The desired amount of memory measured in pages to be free at all
59 * times on the system. This is similar to Linux's zone->pages_low
60 * multiplied by the number of zones and is sized based on that.
61 * Assuming all zones are being used roughly equally, when we drop
62 * below this threshold asynchronous page reclamation is triggered.
65 EXPORT_SYMBOL(desfree
);
68 * When above this amount of memory measures in pages the system is
69 * determined to have enough free memory. This is similar to Linux's
70 * zone->pages_high multiplied by the number of zones and is sized based
71 * on that. Assuming all zones are being used roughly equally, when
72 * asynchronous page reclamation reaches this threshold it stops.
75 EXPORT_SYMBOL(lotsfree
);
77 /* Unused always 0 in this implementation */
79 EXPORT_SYMBOL(needfree
);
81 pgcnt_t swapfs_minfree
= 0;
82 EXPORT_SYMBOL(swapfs_minfree
);
84 pgcnt_t swapfs_reserve
= 0;
85 EXPORT_SYMBOL(swapfs_reserve
);
87 vmem_t
*heap_arena
= NULL
;
88 EXPORT_SYMBOL(heap_arena
);
90 vmem_t
*zio_alloc_arena
= NULL
;
91 EXPORT_SYMBOL(zio_alloc_arena
);
93 vmem_t
*zio_arena
= NULL
;
94 EXPORT_SYMBOL(zio_arena
);
96 #ifndef HAVE_GET_VMALLOC_INFO
97 get_vmalloc_info_t get_vmalloc_info_fn
= SYMBOL_POISON
;
98 EXPORT_SYMBOL(get_vmalloc_info_fn
);
99 #endif /* HAVE_GET_VMALLOC_INFO */
101 #ifdef HAVE_PGDAT_HELPERS
102 # ifndef HAVE_FIRST_ONLINE_PGDAT
103 first_online_pgdat_t first_online_pgdat_fn
= SYMBOL_POISON
;
104 EXPORT_SYMBOL(first_online_pgdat_fn
);
105 # endif /* HAVE_FIRST_ONLINE_PGDAT */
107 # ifndef HAVE_NEXT_ONLINE_PGDAT
108 next_online_pgdat_t next_online_pgdat_fn
= SYMBOL_POISON
;
109 EXPORT_SYMBOL(next_online_pgdat_fn
);
110 # endif /* HAVE_NEXT_ONLINE_PGDAT */
112 # ifndef HAVE_NEXT_ZONE
113 next_zone_t next_zone_fn
= SYMBOL_POISON
;
114 EXPORT_SYMBOL(next_zone_fn
);
115 # endif /* HAVE_NEXT_ZONE */
117 #else /* HAVE_PGDAT_HELPERS */
119 # ifndef HAVE_PGDAT_LIST
120 struct pglist_data
*pgdat_list_addr
= SYMBOL_POISON
;
121 EXPORT_SYMBOL(pgdat_list_addr
);
122 # endif /* HAVE_PGDAT_LIST */
124 #endif /* HAVE_PGDAT_HELPERS */
126 #ifdef NEED_GET_ZONE_COUNTS
127 # ifndef HAVE_GET_ZONE_COUNTS
128 get_zone_counts_t get_zone_counts_fn
= SYMBOL_POISON
;
129 EXPORT_SYMBOL(get_zone_counts_fn
);
130 # endif /* HAVE_GET_ZONE_COUNTS */
133 spl_global_page_state(spl_zone_stat_item_t item
)
135 unsigned long active
;
136 unsigned long inactive
;
139 get_zone_counts(&active
, &inactive
, &free
);
141 case SPL_NR_FREE_PAGES
: return free
;
142 case SPL_NR_INACTIVE
: return inactive
;
143 case SPL_NR_ACTIVE
: return active
;
144 default: ASSERT(0); /* Unsupported */
150 # ifdef HAVE_GLOBAL_PAGE_STATE
152 spl_global_page_state(spl_zone_stat_item_t item
)
154 unsigned long pages
= 0;
157 case SPL_NR_FREE_PAGES
:
158 # ifdef HAVE_ZONE_STAT_ITEM_NR_FREE_PAGES
159 pages
+= global_page_state(NR_FREE_PAGES
);
162 case SPL_NR_INACTIVE
:
163 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE
164 pages
+= global_page_state(NR_INACTIVE
);
166 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_ANON
167 pages
+= global_page_state(NR_INACTIVE_ANON
);
169 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_FILE
170 pages
+= global_page_state(NR_INACTIVE_FILE
);
174 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE
175 pages
+= global_page_state(NR_ACTIVE
);
177 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_ANON
178 pages
+= global_page_state(NR_ACTIVE_ANON
);
180 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_FILE
181 pages
+= global_page_state(NR_ACTIVE_FILE
);
185 ASSERT(0); /* Unsupported */
191 # error "Both global_page_state() and get_zone_counts() unavailable"
192 # endif /* HAVE_GLOBAL_PAGE_STATE */
193 #endif /* NEED_GET_ZONE_COUNTS */
194 EXPORT_SYMBOL(spl_global_page_state
);
196 #ifndef HAVE_SHRINK_DCACHE_MEMORY
197 shrink_dcache_memory_t shrink_dcache_memory_fn
= SYMBOL_POISON
;
198 EXPORT_SYMBOL(shrink_dcache_memory_fn
);
199 #endif /* HAVE_SHRINK_DCACHE_MEMORY */
201 #ifndef HAVE_SHRINK_ICACHE_MEMORY
202 shrink_icache_memory_t shrink_icache_memory_fn
= SYMBOL_POISON
;
203 EXPORT_SYMBOL(shrink_icache_memory_fn
);
204 #endif /* HAVE_SHRINK_ICACHE_MEMORY */
207 spl_kmem_availrmem(void)
209 /* The amount of easily available memory */
210 return (spl_global_page_state(SPL_NR_FREE_PAGES
) +
211 spl_global_page_state(SPL_NR_INACTIVE
));
213 EXPORT_SYMBOL(spl_kmem_availrmem
);
216 vmem_size(vmem_t
*vmp
, int typemask
)
218 struct vmalloc_info vmi
;
222 ASSERT(typemask
& (VMEM_ALLOC
| VMEM_FREE
));
224 get_vmalloc_info(&vmi
);
225 if (typemask
& VMEM_ALLOC
)
226 size
+= (size_t)vmi
.used
;
228 if (typemask
& VMEM_FREE
)
229 size
+= (size_t)(VMALLOC_TOTAL
- vmi
.used
);
233 EXPORT_SYMBOL(vmem_size
);
240 EXPORT_SYMBOL(kmem_debugging
);
242 #ifndef HAVE_KVASPRINTF
243 /* Simplified asprintf. */
244 char *kvasprintf(gfp_t gfp
, const char *fmt
, va_list ap
)
251 len
= vsnprintf(NULL
, 0, fmt
, aq
);
254 p
= kmalloc(len
+1, gfp
);
258 vsnprintf(p
, len
+1, fmt
, ap
);
262 EXPORT_SYMBOL(kvasprintf
);
263 #endif /* HAVE_KVASPRINTF */
266 kmem_vasprintf(const char *fmt
, va_list ap
)
273 ptr
= kvasprintf(GFP_KERNEL
, fmt
, aq
);
275 } while (ptr
== NULL
);
279 EXPORT_SYMBOL(kmem_vasprintf
);
282 kmem_asprintf(const char *fmt
, ...)
289 ptr
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
291 } while (ptr
== NULL
);
295 EXPORT_SYMBOL(kmem_asprintf
);
298 __strdup(const char *str
, int flags
)
304 ptr
= kmalloc_nofail(n
+ 1, flags
);
306 memcpy(ptr
, str
, n
+ 1);
312 strdup(const char *str
)
314 return __strdup(str
, KM_SLEEP
);
316 EXPORT_SYMBOL(strdup
);
323 EXPORT_SYMBOL(strfree
);
326 * Memory allocation interfaces and debugging for basic kmem_*
327 * and vmem_* style memory allocation. When DEBUG_KMEM is enabled
328 * the SPL will keep track of the total memory allocated, and
329 * report any memory leaked when the module is unloaded.
333 /* Shim layer memory accounting */
334 # ifdef HAVE_ATOMIC64_T
335 atomic64_t kmem_alloc_used
= ATOMIC64_INIT(0);
336 unsigned long long kmem_alloc_max
= 0;
337 atomic64_t vmem_alloc_used
= ATOMIC64_INIT(0);
338 unsigned long long vmem_alloc_max
= 0;
339 # else /* HAVE_ATOMIC64_T */
340 atomic_t kmem_alloc_used
= ATOMIC_INIT(0);
341 unsigned long long kmem_alloc_max
= 0;
342 atomic_t vmem_alloc_used
= ATOMIC_INIT(0);
343 unsigned long long vmem_alloc_max
= 0;
344 # endif /* HAVE_ATOMIC64_T */
346 EXPORT_SYMBOL(kmem_alloc_used
);
347 EXPORT_SYMBOL(kmem_alloc_max
);
348 EXPORT_SYMBOL(vmem_alloc_used
);
349 EXPORT_SYMBOL(vmem_alloc_max
);
351 /* When DEBUG_KMEM_TRACKING is enabled not only will total bytes be tracked
352 * but also the location of every alloc and free. When the SPL module is
353 * unloaded a list of all leaked addresses and where they were allocated
354 * will be dumped to the console. Enabling this feature has a significant
355 * impact on performance but it makes finding memory leaks straight forward.
357 * Not surprisingly with debugging enabled the xmem_locks are very highly
358 * contended particularly on xfree(). If we want to run with this detailed
359 * debugging enabled for anything other than debugging we need to minimize
360 * the contention by moving to a lock per xmem_table entry model.
362 # ifdef DEBUG_KMEM_TRACKING
364 # define KMEM_HASH_BITS 10
365 # define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
367 # define VMEM_HASH_BITS 10
368 # define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS)
370 typedef struct kmem_debug
{
371 struct hlist_node kd_hlist
; /* Hash node linkage */
372 struct list_head kd_list
; /* List of all allocations */
373 void *kd_addr
; /* Allocation pointer */
374 size_t kd_size
; /* Allocation size */
375 const char *kd_func
; /* Allocation function */
376 int kd_line
; /* Allocation line */
379 spinlock_t kmem_lock
;
380 struct hlist_head kmem_table
[KMEM_TABLE_SIZE
];
381 struct list_head kmem_list
;
383 spinlock_t vmem_lock
;
384 struct hlist_head vmem_table
[VMEM_TABLE_SIZE
];
385 struct list_head vmem_list
;
387 EXPORT_SYMBOL(kmem_lock
);
388 EXPORT_SYMBOL(kmem_table
);
389 EXPORT_SYMBOL(kmem_list
);
391 EXPORT_SYMBOL(vmem_lock
);
392 EXPORT_SYMBOL(vmem_table
);
393 EXPORT_SYMBOL(vmem_list
);
395 static kmem_debug_t
*
396 kmem_del_init(spinlock_t
*lock
, struct hlist_head
*table
, int bits
, const void *addr
)
398 struct hlist_head
*head
;
399 struct hlist_node
*node
;
400 struct kmem_debug
*p
;
404 spin_lock_irqsave(lock
, flags
);
406 head
= &table
[hash_ptr((void *)addr
, bits
)];
407 hlist_for_each(node
, head
) {
408 p
= list_entry(node
, struct kmem_debug
, kd_hlist
);
409 if (p
->kd_addr
== addr
) {
410 hlist_del_init(&p
->kd_hlist
);
411 list_del_init(&p
->kd_list
);
412 spin_unlock_irqrestore(lock
, flags
);
417 spin_unlock_irqrestore(lock
, flags
);
423 kmem_alloc_track(size_t size
, int flags
, const char *func
, int line
,
424 int node_alloc
, int node
)
428 unsigned long irq_flags
;
431 /* Function may be called with KM_NOSLEEP so failure is possible */
432 dptr
= (kmem_debug_t
*) kmalloc_nofail(sizeof(kmem_debug_t
),
433 flags
& ~__GFP_ZERO
);
435 if (unlikely(dptr
== NULL
)) {
436 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "debug "
437 "kmem_alloc(%ld, 0x%x) at %s:%d failed (%lld/%llu)\n",
438 sizeof(kmem_debug_t
), flags
, func
, line
,
439 kmem_alloc_used_read(), kmem_alloc_max
);
442 * Marked unlikely because we should never be doing this,
443 * we tolerate to up 2 pages but a single page is best.
445 if (unlikely((size
> PAGE_SIZE
*2) && !(flags
& KM_NODEBUG
))) {
446 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "large "
447 "kmem_alloc(%llu, 0x%x) at %s:%d (%lld/%llu)\n",
448 (unsigned long long) size
, flags
, func
, line
,
449 kmem_alloc_used_read(), kmem_alloc_max
);
450 spl_debug_dumpstack(NULL
);
454 * We use __strdup() below because the string pointed to by
455 * __FUNCTION__ might not be available by the time we want
456 * to print it since the module might have been unloaded.
457 * This can only fail in the KM_NOSLEEP case.
459 dptr
->kd_func
= __strdup(func
, flags
& ~__GFP_ZERO
);
460 if (unlikely(dptr
->kd_func
== NULL
)) {
462 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
463 "debug __strdup() at %s:%d failed (%lld/%llu)\n",
464 func
, line
, kmem_alloc_used_read(), kmem_alloc_max
);
468 /* Use the correct allocator */
470 ASSERT(!(flags
& __GFP_ZERO
));
471 ptr
= kmalloc_node_nofail(size
, flags
, node
);
472 } else if (flags
& __GFP_ZERO
) {
473 ptr
= kzalloc_nofail(size
, flags
& ~__GFP_ZERO
);
475 ptr
= kmalloc_nofail(size
, flags
);
478 if (unlikely(ptr
== NULL
)) {
479 kfree(dptr
->kd_func
);
481 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "kmem_alloc"
482 "(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
483 (unsigned long long) size
, flags
, func
, line
,
484 kmem_alloc_used_read(), kmem_alloc_max
);
488 kmem_alloc_used_add(size
);
489 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max
))
490 kmem_alloc_max
= kmem_alloc_used_read();
492 INIT_HLIST_NODE(&dptr
->kd_hlist
);
493 INIT_LIST_HEAD(&dptr
->kd_list
);
496 dptr
->kd_size
= size
;
497 dptr
->kd_line
= line
;
499 spin_lock_irqsave(&kmem_lock
, irq_flags
);
500 hlist_add_head(&dptr
->kd_hlist
,
501 &kmem_table
[hash_ptr(ptr
, KMEM_HASH_BITS
)]);
502 list_add_tail(&dptr
->kd_list
, &kmem_list
);
503 spin_unlock_irqrestore(&kmem_lock
, irq_flags
);
505 SDEBUG_LIMIT(SD_INFO
,
506 "kmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
507 (unsigned long long) size
, flags
, func
, line
, ptr
,
508 kmem_alloc_used_read(), kmem_alloc_max
);
513 EXPORT_SYMBOL(kmem_alloc_track
);
516 kmem_free_track(const void *ptr
, size_t size
)
521 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
522 (unsigned long long) size
);
524 dptr
= kmem_del_init(&kmem_lock
, kmem_table
, KMEM_HASH_BITS
, ptr
);
526 /* Must exist in hash due to kmem_alloc() */
529 /* Size must match */
530 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
531 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
532 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
534 kmem_alloc_used_sub(size
);
535 SDEBUG_LIMIT(SD_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
536 (unsigned long long) size
, kmem_alloc_used_read(),
539 kfree(dptr
->kd_func
);
541 memset((void *)dptr
, 0x5a, sizeof(kmem_debug_t
));
544 memset((void *)ptr
, 0x5a, size
);
549 EXPORT_SYMBOL(kmem_free_track
);
552 vmem_alloc_track(size_t size
, int flags
, const char *func
, int line
)
556 unsigned long irq_flags
;
559 ASSERT(flags
& KM_SLEEP
);
561 /* Function may be called with KM_NOSLEEP so failure is possible */
562 dptr
= (kmem_debug_t
*) kmalloc_nofail(sizeof(kmem_debug_t
),
563 flags
& ~__GFP_ZERO
);
564 if (unlikely(dptr
== NULL
)) {
565 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "debug "
566 "vmem_alloc(%ld, 0x%x) at %s:%d failed (%lld/%llu)\n",
567 sizeof(kmem_debug_t
), flags
, func
, line
,
568 vmem_alloc_used_read(), vmem_alloc_max
);
571 * We use __strdup() below because the string pointed to by
572 * __FUNCTION__ might not be available by the time we want
573 * to print it, since the module might have been unloaded.
574 * This can never fail because we have already asserted
575 * that flags is KM_SLEEP.
577 dptr
->kd_func
= __strdup(func
, flags
& ~__GFP_ZERO
);
578 if (unlikely(dptr
->kd_func
== NULL
)) {
580 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
581 "debug __strdup() at %s:%d failed (%lld/%llu)\n",
582 func
, line
, vmem_alloc_used_read(), vmem_alloc_max
);
586 /* Use the correct allocator */
587 if (flags
& __GFP_ZERO
) {
588 ptr
= vzalloc_nofail(size
, flags
& ~__GFP_ZERO
);
590 ptr
= vmalloc_nofail(size
, flags
);
593 if (unlikely(ptr
== NULL
)) {
594 kfree(dptr
->kd_func
);
596 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "vmem_alloc"
597 "(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
598 (unsigned long long) size
, flags
, func
, line
,
599 vmem_alloc_used_read(), vmem_alloc_max
);
603 vmem_alloc_used_add(size
);
604 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max
))
605 vmem_alloc_max
= vmem_alloc_used_read();
607 INIT_HLIST_NODE(&dptr
->kd_hlist
);
608 INIT_LIST_HEAD(&dptr
->kd_list
);
611 dptr
->kd_size
= size
;
612 dptr
->kd_line
= line
;
614 spin_lock_irqsave(&vmem_lock
, irq_flags
);
615 hlist_add_head(&dptr
->kd_hlist
,
616 &vmem_table
[hash_ptr(ptr
, VMEM_HASH_BITS
)]);
617 list_add_tail(&dptr
->kd_list
, &vmem_list
);
618 spin_unlock_irqrestore(&vmem_lock
, irq_flags
);
620 SDEBUG_LIMIT(SD_INFO
,
621 "vmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
622 (unsigned long long) size
, flags
, func
, line
,
623 ptr
, vmem_alloc_used_read(), vmem_alloc_max
);
628 EXPORT_SYMBOL(vmem_alloc_track
);
631 vmem_free_track(const void *ptr
, size_t size
)
636 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
637 (unsigned long long) size
);
639 dptr
= kmem_del_init(&vmem_lock
, vmem_table
, VMEM_HASH_BITS
, ptr
);
641 /* Must exist in hash due to vmem_alloc() */
644 /* Size must match */
645 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
646 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
647 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
649 vmem_alloc_used_sub(size
);
650 SDEBUG_LIMIT(SD_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
651 (unsigned long long) size
, vmem_alloc_used_read(),
654 kfree(dptr
->kd_func
);
656 memset((void *)dptr
, 0x5a, sizeof(kmem_debug_t
));
659 memset((void *)ptr
, 0x5a, size
);
664 EXPORT_SYMBOL(vmem_free_track
);
666 # else /* DEBUG_KMEM_TRACKING */
669 kmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
,
670 int node_alloc
, int node
)
676 * Marked unlikely because we should never be doing this,
677 * we tolerate to up 2 pages but a single page is best.
679 if (unlikely((size
> PAGE_SIZE
* 2) && !(flags
& KM_NODEBUG
))) {
680 SDEBUG(SD_CONSOLE
| SD_WARNING
,
681 "large kmem_alloc(%llu, 0x%x) at %s:%d (%lld/%llu)\n",
682 (unsigned long long) size
, flags
, func
, line
,
683 kmem_alloc_used_read(), kmem_alloc_max
);
687 /* Use the correct allocator */
689 ASSERT(!(flags
& __GFP_ZERO
));
690 ptr
= kmalloc_node_nofail(size
, flags
, node
);
691 } else if (flags
& __GFP_ZERO
) {
692 ptr
= kzalloc_nofail(size
, flags
& (~__GFP_ZERO
));
694 ptr
= kmalloc_nofail(size
, flags
);
697 if (unlikely(ptr
== NULL
)) {
698 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
699 "kmem_alloc(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
700 (unsigned long long) size
, flags
, func
, line
,
701 kmem_alloc_used_read(), kmem_alloc_max
);
703 kmem_alloc_used_add(size
);
704 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max
))
705 kmem_alloc_max
= kmem_alloc_used_read();
707 SDEBUG_LIMIT(SD_INFO
,
708 "kmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
709 (unsigned long long) size
, flags
, func
, line
, ptr
,
710 kmem_alloc_used_read(), kmem_alloc_max
);
715 EXPORT_SYMBOL(kmem_alloc_debug
);
718 kmem_free_debug(const void *ptr
, size_t size
)
722 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
723 (unsigned long long) size
);
725 kmem_alloc_used_sub(size
);
726 SDEBUG_LIMIT(SD_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
727 (unsigned long long) size
, kmem_alloc_used_read(),
733 EXPORT_SYMBOL(kmem_free_debug
);
736 vmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
)
741 ASSERT(flags
& KM_SLEEP
);
743 /* Use the correct allocator */
744 if (flags
& __GFP_ZERO
) {
745 ptr
= vzalloc_nofail(size
, flags
& (~__GFP_ZERO
));
747 ptr
= vmalloc_nofail(size
, flags
);
750 if (unlikely(ptr
== NULL
)) {
751 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
752 "vmem_alloc(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
753 (unsigned long long) size
, flags
, func
, line
,
754 vmem_alloc_used_read(), vmem_alloc_max
);
756 vmem_alloc_used_add(size
);
757 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max
))
758 vmem_alloc_max
= vmem_alloc_used_read();
760 SDEBUG_LIMIT(SD_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
761 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
762 vmem_alloc_used_read(), vmem_alloc_max
);
767 EXPORT_SYMBOL(vmem_alloc_debug
);
770 vmem_free_debug(const void *ptr
, size_t size
)
774 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
775 (unsigned long long) size
);
777 vmem_alloc_used_sub(size
);
778 SDEBUG_LIMIT(SD_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
779 (unsigned long long) size
, vmem_alloc_used_read(),
785 EXPORT_SYMBOL(vmem_free_debug
);
787 # endif /* DEBUG_KMEM_TRACKING */
788 #endif /* DEBUG_KMEM */
791 * Slab allocation interfaces
793 * While the Linux slab implementation was inspired by the Solaris
794 * implementation I cannot use it to emulate the Solaris APIs. I
795 * require two features which are not provided by the Linux slab.
797 * 1) Constructors AND destructors. Recent versions of the Linux
798 * kernel have removed support for destructors. This is a deal
799 * breaker for the SPL which contains particularly expensive
800 * initializers for mutex's, condition variables, etc. We also
801 * require a minimal level of cleanup for these data types unlike
802 * many Linux data type which do need to be explicitly destroyed.
804 * 2) Virtual address space backed slab. Callers of the Solaris slab
805 * expect it to work well for both small are very large allocations.
806 * Because of memory fragmentation the Linux slab which is backed
807 * by kmalloc'ed memory performs very badly when confronted with
808 * large numbers of large allocations. Basing the slab on the
809 * virtual address space removes the need for contiguous pages
810 * and greatly improve performance for large allocations.
812 * For these reasons, the SPL has its own slab implementation with
813 * the needed features. It is not as highly optimized as either the
814 * Solaris or Linux slabs, but it should get me most of what is
815 * needed until it can be optimized or obsoleted by another approach.
817 * One serious concern I do have about this method is the relatively
818 * small virtual address space on 32bit arches. This will seriously
819 * constrain the size of the slab caches and their performance.
821 * XXX: Improve the partial slab list by carefully maintaining a
822 * strict ordering of fullest to emptiest slabs based on
823 * the slab reference count. This guarantees the when freeing
824 * slabs back to the system we need only linearly traverse the
825 * last N slabs in the list to discover all the freeable slabs.
827 * XXX: NUMA awareness for optionally allocating memory close to a
828 * particular core. This can be advantageous if you know the slab
829 * object will be short lived and primarily accessed from one core.
831 * XXX: Slab coloring may also yield performance improvements and would
832 * be desirable to implement.
835 struct list_head spl_kmem_cache_list
; /* List of caches */
836 struct rw_semaphore spl_kmem_cache_sem
; /* Cache list lock */
837 taskq_t
*spl_kmem_cache_taskq
; /* Task queue for ageing / reclaim */
839 static void spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
);
841 SPL_SHRINKER_CALLBACK_FWD_DECLARE(spl_kmem_cache_generic_shrinker
);
842 SPL_SHRINKER_DECLARE(spl_kmem_cache_shrinker
,
843 spl_kmem_cache_generic_shrinker
, KMC_DEFAULT_SEEKS
);
846 kv_alloc(spl_kmem_cache_t
*skc
, int size
, int flags
)
852 if (skc
->skc_flags
& KMC_KMEM
)
853 ptr
= (void *)__get_free_pages(flags
, get_order(size
));
855 ptr
= __vmalloc(size
, flags
| __GFP_HIGHMEM
, PAGE_KERNEL
);
857 /* Resulting allocated memory will be page aligned */
858 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
864 kv_free(spl_kmem_cache_t
*skc
, void *ptr
, int size
)
866 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
870 * The Linux direct reclaim path uses this out of band value to
871 * determine if forward progress is being made. Normally this is
872 * incremented by kmem_freepages() which is part of the various
873 * Linux slab implementations. However, since we are using none
874 * of that infrastructure we are responsible for incrementing it.
876 if (current
->reclaim_state
)
877 current
->reclaim_state
->reclaimed_slab
+= size
>> PAGE_SHIFT
;
879 if (skc
->skc_flags
& KMC_KMEM
)
880 free_pages((unsigned long)ptr
, get_order(size
));
886 * Required space for each aligned sks.
888 static inline uint32_t
889 spl_sks_size(spl_kmem_cache_t
*skc
)
891 return P2ROUNDUP_TYPED(sizeof(spl_kmem_slab_t
),
892 skc
->skc_obj_align
, uint32_t);
896 * Required space for each aligned object.
898 static inline uint32_t
899 spl_obj_size(spl_kmem_cache_t
*skc
)
901 uint32_t align
= skc
->skc_obj_align
;
903 return P2ROUNDUP_TYPED(skc
->skc_obj_size
, align
, uint32_t) +
904 P2ROUNDUP_TYPED(sizeof(spl_kmem_obj_t
), align
, uint32_t);
908 * Lookup the spl_kmem_object_t for an object given that object.
910 static inline spl_kmem_obj_t
*
911 spl_sko_from_obj(spl_kmem_cache_t
*skc
, void *obj
)
913 return obj
+ P2ROUNDUP_TYPED(skc
->skc_obj_size
,
914 skc
->skc_obj_align
, uint32_t);
918 * Required space for each offslab object taking in to account alignment
919 * restrictions and the power-of-two requirement of kv_alloc().
921 static inline uint32_t
922 spl_offslab_size(spl_kmem_cache_t
*skc
)
924 return 1UL << (highbit(spl_obj_size(skc
)) + 1);
928 * It's important that we pack the spl_kmem_obj_t structure and the
929 * actual objects in to one large address space to minimize the number
930 * of calls to the allocator. It is far better to do a few large
931 * allocations and then subdivide it ourselves. Now which allocator
932 * we use requires balancing a few trade offs.
934 * For small objects we use kmem_alloc() because as long as you are
935 * only requesting a small number of pages (ideally just one) its cheap.
936 * However, when you start requesting multiple pages with kmem_alloc()
937 * it gets increasingly expensive since it requires contiguous pages.
938 * For this reason we shift to vmem_alloc() for slabs of large objects
939 * which removes the need for contiguous pages. We do not use
940 * vmem_alloc() in all cases because there is significant locking
941 * overhead in __get_vm_area_node(). This function takes a single
942 * global lock when acquiring an available virtual address range which
943 * serializes all vmem_alloc()'s for all slab caches. Using slightly
944 * different allocation functions for small and large objects should
945 * give us the best of both worlds.
947 * KMC_ONSLAB KMC_OFFSLAB
949 * +------------------------+ +-----------------+
950 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
951 * | skc_obj_size <-+ | | +-----------------+ | |
952 * | spl_kmem_obj_t | | | |
953 * | skc_obj_size <---+ | +-----------------+ | |
954 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
955 * | ... v | | spl_kmem_obj_t | |
956 * +------------------------+ +-----------------+ v
958 static spl_kmem_slab_t
*
959 spl_slab_alloc(spl_kmem_cache_t
*skc
, int flags
)
961 spl_kmem_slab_t
*sks
;
962 spl_kmem_obj_t
*sko
, *n
;
964 uint32_t obj_size
, offslab_size
= 0;
967 base
= kv_alloc(skc
, skc
->skc_slab_size
, flags
);
971 sks
= (spl_kmem_slab_t
*)base
;
972 sks
->sks_magic
= SKS_MAGIC
;
973 sks
->sks_objs
= skc
->skc_slab_objs
;
974 sks
->sks_age
= jiffies
;
975 sks
->sks_cache
= skc
;
976 INIT_LIST_HEAD(&sks
->sks_list
);
977 INIT_LIST_HEAD(&sks
->sks_free_list
);
979 obj_size
= spl_obj_size(skc
);
981 if (skc
->skc_flags
& KMC_OFFSLAB
)
982 offslab_size
= spl_offslab_size(skc
);
984 for (i
= 0; i
< sks
->sks_objs
; i
++) {
985 if (skc
->skc_flags
& KMC_OFFSLAB
) {
986 obj
= kv_alloc(skc
, offslab_size
, flags
);
988 SGOTO(out
, rc
= -ENOMEM
);
990 obj
= base
+ spl_sks_size(skc
) + (i
* obj_size
);
993 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
994 sko
= spl_sko_from_obj(skc
, obj
);
996 sko
->sko_magic
= SKO_MAGIC
;
998 INIT_LIST_HEAD(&sko
->sko_list
);
999 list_add_tail(&sko
->sko_list
, &sks
->sks_free_list
);
1002 list_for_each_entry(sko
, &sks
->sks_free_list
, sko_list
)
1004 skc
->skc_ctor(sko
->sko_addr
, skc
->skc_private
, flags
);
1007 if (skc
->skc_flags
& KMC_OFFSLAB
)
1008 list_for_each_entry_safe(sko
, n
, &sks
->sks_free_list
,
1010 kv_free(skc
, sko
->sko_addr
, offslab_size
);
1012 kv_free(skc
, base
, skc
->skc_slab_size
);
1020 * Remove a slab from complete or partial list, it must be called with
1021 * the 'skc->skc_lock' held but the actual free must be performed
1022 * outside the lock to prevent deadlocking on vmem addresses.
1025 spl_slab_free(spl_kmem_slab_t
*sks
,
1026 struct list_head
*sks_list
, struct list_head
*sko_list
)
1028 spl_kmem_cache_t
*skc
;
1031 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1032 ASSERT(sks
->sks_ref
== 0);
1034 skc
= sks
->sks_cache
;
1035 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1036 ASSERT(spin_is_locked(&skc
->skc_lock
));
1039 * Update slab/objects counters in the cache, then remove the
1040 * slab from the skc->skc_partial_list. Finally add the slab
1041 * and all its objects in to the private work lists where the
1042 * destructors will be called and the memory freed to the system.
1044 skc
->skc_obj_total
-= sks
->sks_objs
;
1045 skc
->skc_slab_total
--;
1046 list_del(&sks
->sks_list
);
1047 list_add(&sks
->sks_list
, sks_list
);
1048 list_splice_init(&sks
->sks_free_list
, sko_list
);
1054 * Traverses all the partial slabs attached to a cache and free those
1055 * which which are currently empty, and have not been touched for
1056 * skc_delay seconds to avoid thrashing. The count argument is
1057 * passed to optionally cap the number of slabs reclaimed, a count
1058 * of zero means try and reclaim everything. When flag is set we
1059 * always free an available slab regardless of age.
1062 spl_slab_reclaim(spl_kmem_cache_t
*skc
, int count
, int flag
)
1064 spl_kmem_slab_t
*sks
, *m
;
1065 spl_kmem_obj_t
*sko
, *n
;
1066 LIST_HEAD(sks_list
);
1067 LIST_HEAD(sko_list
);
1073 * Move empty slabs and objects which have not been touched in
1074 * skc_delay seconds on to private lists to be freed outside
1075 * the spin lock. This delay time is important to avoid thrashing
1076 * however when flag is set the delay will not be used.
1078 spin_lock(&skc
->skc_lock
);
1079 list_for_each_entry_safe_reverse(sks
,m
,&skc
->skc_partial_list
,sks_list
){
1081 * All empty slabs are at the end of skc->skc_partial_list,
1082 * therefore once a non-empty slab is found we can stop
1083 * scanning. Additionally, stop when reaching the target
1084 * reclaim 'count' if a non-zero threshold is given.
1086 if ((sks
->sks_ref
> 0) || (count
&& i
>= count
))
1089 if (time_after(jiffies
,sks
->sks_age
+skc
->skc_delay
*HZ
)||flag
) {
1090 spl_slab_free(sks
, &sks_list
, &sko_list
);
1094 spin_unlock(&skc
->skc_lock
);
1097 * The following two loops ensure all the object destructors are
1098 * run, any offslab objects are freed, and the slabs themselves
1099 * are freed. This is all done outside the skc->skc_lock since
1100 * this allows the destructor to sleep, and allows us to perform
1101 * a conditional reschedule when a freeing a large number of
1102 * objects and slabs back to the system.
1104 if (skc
->skc_flags
& KMC_OFFSLAB
)
1105 size
= spl_offslab_size(skc
);
1107 list_for_each_entry_safe(sko
, n
, &sko_list
, sko_list
) {
1108 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1111 skc
->skc_dtor(sko
->sko_addr
, skc
->skc_private
);
1113 if (skc
->skc_flags
& KMC_OFFSLAB
)
1114 kv_free(skc
, sko
->sko_addr
, size
);
1117 list_for_each_entry_safe(sks
, m
, &sks_list
, sks_list
) {
1118 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1119 kv_free(skc
, sks
, skc
->skc_slab_size
);
1125 static spl_kmem_emergency_t
*
1126 spl_emergency_search(struct rb_root
*root
, void *obj
)
1128 struct rb_node
*node
= root
->rb_node
;
1129 spl_kmem_emergency_t
*ske
;
1130 unsigned long address
= (unsigned long)obj
;
1133 ske
= container_of(node
, spl_kmem_emergency_t
, ske_node
);
1135 if (address
< (unsigned long)ske
->ske_obj
)
1136 node
= node
->rb_left
;
1137 else if (address
> (unsigned long)ske
->ske_obj
)
1138 node
= node
->rb_right
;
1147 spl_emergency_insert(struct rb_root
*root
, spl_kmem_emergency_t
*ske
)
1149 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1150 spl_kmem_emergency_t
*ske_tmp
;
1151 unsigned long address
= (unsigned long)ske
->ske_obj
;
1154 ske_tmp
= container_of(*new, spl_kmem_emergency_t
, ske_node
);
1157 if (address
< (unsigned long)ske_tmp
->ske_obj
)
1158 new = &((*new)->rb_left
);
1159 else if (address
> (unsigned long)ske_tmp
->ske_obj
)
1160 new = &((*new)->rb_right
);
1165 rb_link_node(&ske
->ske_node
, parent
, new);
1166 rb_insert_color(&ske
->ske_node
, root
);
1172 * Allocate a single emergency object and track it in a red black tree.
1175 spl_emergency_alloc(spl_kmem_cache_t
*skc
, int flags
, void **obj
)
1177 spl_kmem_emergency_t
*ske
;
1181 /* Last chance use a partial slab if one now exists */
1182 spin_lock(&skc
->skc_lock
);
1183 empty
= list_empty(&skc
->skc_partial_list
);
1184 spin_unlock(&skc
->skc_lock
);
1188 ske
= kmalloc(sizeof(*ske
), flags
);
1192 ske
->ske_obj
= kmalloc(skc
->skc_obj_size
, flags
);
1193 if (ske
->ske_obj
== NULL
) {
1198 spin_lock(&skc
->skc_lock
);
1199 empty
= spl_emergency_insert(&skc
->skc_emergency_tree
, ske
);
1200 if (likely(empty
)) {
1201 skc
->skc_obj_total
++;
1202 skc
->skc_obj_emergency
++;
1203 if (skc
->skc_obj_emergency
> skc
->skc_obj_emergency_max
)
1204 skc
->skc_obj_emergency_max
= skc
->skc_obj_emergency
;
1206 spin_unlock(&skc
->skc_lock
);
1208 if (unlikely(!empty
)) {
1209 kfree(ske
->ske_obj
);
1215 skc
->skc_ctor(ske
->ske_obj
, skc
->skc_private
, flags
);
1217 *obj
= ske
->ske_obj
;
1223 * Locate the passed object in the red black tree and free it.
1226 spl_emergency_free(spl_kmem_cache_t
*skc
, void *obj
)
1228 spl_kmem_emergency_t
*ske
;
1231 spin_lock(&skc
->skc_lock
);
1232 ske
= spl_emergency_search(&skc
->skc_emergency_tree
, obj
);
1234 rb_erase(&ske
->ske_node
, &skc
->skc_emergency_tree
);
1235 skc
->skc_obj_emergency
--;
1236 skc
->skc_obj_total
--;
1238 spin_unlock(&skc
->skc_lock
);
1240 if (unlikely(ske
== NULL
))
1244 skc
->skc_dtor(ske
->ske_obj
, skc
->skc_private
);
1246 kfree(ske
->ske_obj
);
1253 * Release objects from the per-cpu magazine back to their slab. The flush
1254 * argument contains the max number of entries to remove from the magazine.
1257 __spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
1259 int i
, count
= MIN(flush
, skm
->skm_avail
);
1262 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1263 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1264 ASSERT(spin_is_locked(&skc
->skc_lock
));
1266 for (i
= 0; i
< count
; i
++)
1267 spl_cache_shrink(skc
, skm
->skm_objs
[i
]);
1269 skm
->skm_avail
-= count
;
1270 memmove(skm
->skm_objs
, &(skm
->skm_objs
[count
]),
1271 sizeof(void *) * skm
->skm_avail
);
1277 spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
1279 spin_lock(&skc
->skc_lock
);
1280 __spl_cache_flush(skc
, skm
, flush
);
1281 spin_unlock(&skc
->skc_lock
);
1285 spl_magazine_age(void *data
)
1287 spl_kmem_cache_t
*skc
= (spl_kmem_cache_t
*)data
;
1288 spl_kmem_magazine_t
*skm
= skc
->skc_mag
[smp_processor_id()];
1290 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1291 ASSERT(skm
->skm_cpu
== smp_processor_id());
1292 ASSERT(irqs_disabled());
1294 /* There are no available objects or they are too young to age out */
1295 if ((skm
->skm_avail
== 0) ||
1296 time_before(jiffies
, skm
->skm_age
+ skc
->skc_delay
* HZ
))
1300 * Because we're executing in interrupt context we may have
1301 * interrupted the holder of this lock. To avoid a potential
1302 * deadlock return if the lock is contended.
1304 if (!spin_trylock(&skc
->skc_lock
))
1307 __spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1308 spin_unlock(&skc
->skc_lock
);
1312 * Called regularly to keep a downward pressure on the cache.
1314 * Objects older than skc->skc_delay seconds in the per-cpu magazines will
1315 * be returned to the caches. This is done to prevent idle magazines from
1316 * holding memory which could be better used elsewhere. The delay is
1317 * present to prevent thrashing the magazine.
1319 * The newly released objects may result in empty partial slabs. Those
1320 * slabs should be released to the system. Otherwise moving the objects
1321 * out of the magazines is just wasted work.
1324 spl_cache_age(void *data
)
1326 spl_kmem_cache_t
*skc
= (spl_kmem_cache_t
*)data
;
1329 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1331 /* Dynamically disabled at run time */
1332 if (!(spl_kmem_cache_expire
& KMC_EXPIRE_AGE
))
1335 atomic_inc(&skc
->skc_ref
);
1336 spl_on_each_cpu(spl_magazine_age
, skc
, 1);
1337 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
1339 while (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
) && !id
) {
1340 id
= taskq_dispatch_delay(
1341 spl_kmem_cache_taskq
, spl_cache_age
, skc
, TQ_SLEEP
,
1342 ddi_get_lbolt() + skc
->skc_delay
/ 3 * HZ
);
1344 /* Destroy issued after dispatch immediately cancel it */
1345 if (test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
) && id
)
1346 taskq_cancel_id(spl_kmem_cache_taskq
, id
);
1349 spin_lock(&skc
->skc_lock
);
1350 skc
->skc_taskqid
= id
;
1351 spin_unlock(&skc
->skc_lock
);
1353 atomic_dec(&skc
->skc_ref
);
1357 * Size a slab based on the size of each aligned object plus spl_kmem_obj_t.
1358 * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However,
1359 * for very small objects we may end up with more than this so as not
1360 * to waste space in the minimal allocation of a single page. Also for
1361 * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN,
1362 * lower than this and we will fail.
1365 spl_slab_size(spl_kmem_cache_t
*skc
, uint32_t *objs
, uint32_t *size
)
1367 uint32_t sks_size
, obj_size
, max_size
;
1369 if (skc
->skc_flags
& KMC_OFFSLAB
) {
1370 *objs
= SPL_KMEM_CACHE_OBJ_PER_SLAB
;
1371 *size
= P2ROUNDUP(sizeof(spl_kmem_slab_t
), PAGE_SIZE
);
1374 sks_size
= spl_sks_size(skc
);
1375 obj_size
= spl_obj_size(skc
);
1377 if (skc
->skc_flags
& KMC_KMEM
)
1378 max_size
= ((uint32_t)1 << (MAX_ORDER
-3)) * PAGE_SIZE
;
1380 max_size
= (32 * 1024 * 1024);
1382 /* Power of two sized slab */
1383 for (*size
= PAGE_SIZE
; *size
<= max_size
; *size
*= 2) {
1384 *objs
= (*size
- sks_size
) / obj_size
;
1385 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB
)
1390 * Unable to satisfy target objects per slab, fall back to
1391 * allocating a maximally sized slab and assuming it can
1392 * contain the minimum objects count use it. If not fail.
1395 *objs
= (*size
- sks_size
) / obj_size
;
1396 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN
)
1404 * Make a guess at reasonable per-cpu magazine size based on the size of
1405 * each object and the cost of caching N of them in each magazine. Long
1406 * term this should really adapt based on an observed usage heuristic.
1409 spl_magazine_size(spl_kmem_cache_t
*skc
)
1411 uint32_t obj_size
= spl_obj_size(skc
);
1415 /* Per-magazine sizes below assume a 4Kib page size */
1416 if (obj_size
> (PAGE_SIZE
* 256))
1417 size
= 4; /* Minimum 4Mib per-magazine */
1418 else if (obj_size
> (PAGE_SIZE
* 32))
1419 size
= 16; /* Minimum 2Mib per-magazine */
1420 else if (obj_size
> (PAGE_SIZE
))
1421 size
= 64; /* Minimum 256Kib per-magazine */
1422 else if (obj_size
> (PAGE_SIZE
/ 4))
1423 size
= 128; /* Minimum 128Kib per-magazine */
1431 * Allocate a per-cpu magazine to associate with a specific core.
1433 static spl_kmem_magazine_t
*
1434 spl_magazine_alloc(spl_kmem_cache_t
*skc
, int cpu
)
1436 spl_kmem_magazine_t
*skm
;
1437 int size
= sizeof(spl_kmem_magazine_t
) +
1438 sizeof(void *) * skc
->skc_mag_size
;
1441 skm
= kmem_alloc_node(size
, KM_SLEEP
, cpu_to_node(cpu
));
1443 skm
->skm_magic
= SKM_MAGIC
;
1445 skm
->skm_size
= skc
->skc_mag_size
;
1446 skm
->skm_refill
= skc
->skc_mag_refill
;
1447 skm
->skm_cache
= skc
;
1448 skm
->skm_age
= jiffies
;
1456 * Free a per-cpu magazine associated with a specific core.
1459 spl_magazine_free(spl_kmem_magazine_t
*skm
)
1461 int size
= sizeof(spl_kmem_magazine_t
) +
1462 sizeof(void *) * skm
->skm_size
;
1465 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1466 ASSERT(skm
->skm_avail
== 0);
1468 kmem_free(skm
, size
);
1473 * Create all pre-cpu magazines of reasonable sizes.
1476 spl_magazine_create(spl_kmem_cache_t
*skc
)
1481 skc
->skc_mag_size
= spl_magazine_size(skc
);
1482 skc
->skc_mag_refill
= (skc
->skc_mag_size
+ 1) / 2;
1484 for_each_online_cpu(i
) {
1485 skc
->skc_mag
[i
] = spl_magazine_alloc(skc
, i
);
1486 if (!skc
->skc_mag
[i
]) {
1487 for (i
--; i
>= 0; i
--)
1488 spl_magazine_free(skc
->skc_mag
[i
]);
1498 * Destroy all pre-cpu magazines.
1501 spl_magazine_destroy(spl_kmem_cache_t
*skc
)
1503 spl_kmem_magazine_t
*skm
;
1507 for_each_online_cpu(i
) {
1508 skm
= skc
->skc_mag
[i
];
1509 spl_cache_flush(skc
, skm
, skm
->skm_avail
);
1510 spl_magazine_free(skm
);
1517 * Create a object cache based on the following arguments:
1519 * size cache object size
1520 * align cache object alignment
1521 * ctor cache object constructor
1522 * dtor cache object destructor
1523 * reclaim cache object reclaim
1524 * priv cache private data for ctor/dtor/reclaim
1525 * vmp unused must be NULL
1527 * KMC_NOTOUCH Disable cache object aging (unsupported)
1528 * KMC_NODEBUG Disable debugging (unsupported)
1529 * KMC_NOMAGAZINE Disable magazine (unsupported)
1530 * KMC_NOHASH Disable hashing (unsupported)
1531 * KMC_QCACHE Disable qcache (unsupported)
1532 * KMC_KMEM Force kmem backed cache
1533 * KMC_VMEM Force vmem backed cache
1534 * KMC_OFFSLAB Locate objects off the slab
1537 spl_kmem_cache_create(char *name
, size_t size
, size_t align
,
1538 spl_kmem_ctor_t ctor
,
1539 spl_kmem_dtor_t dtor
,
1540 spl_kmem_reclaim_t reclaim
,
1541 void *priv
, void *vmp
, int flags
)
1543 spl_kmem_cache_t
*skc
;
1547 ASSERTF(!(flags
& KMC_NOMAGAZINE
), "Bad KMC_NOMAGAZINE (%x)\n", flags
);
1548 ASSERTF(!(flags
& KMC_NOHASH
), "Bad KMC_NOHASH (%x)\n", flags
);
1549 ASSERTF(!(flags
& KMC_QCACHE
), "Bad KMC_QCACHE (%x)\n", flags
);
1550 ASSERT(vmp
== NULL
);
1555 * Allocate memory for a new cache an initialize it. Unfortunately,
1556 * this usually ends up being a large allocation of ~32k because
1557 * we need to allocate enough memory for the worst case number of
1558 * cpus in the magazine, skc_mag[NR_CPUS]. Because of this we
1559 * explicitly pass KM_NODEBUG to suppress the kmem warning
1561 skc
= kmem_zalloc(sizeof(*skc
), KM_SLEEP
| KM_NODEBUG
);
1565 skc
->skc_magic
= SKC_MAGIC
;
1566 skc
->skc_name_size
= strlen(name
) + 1;
1567 skc
->skc_name
= (char *)kmem_alloc(skc
->skc_name_size
, KM_SLEEP
);
1568 if (skc
->skc_name
== NULL
) {
1569 kmem_free(skc
, sizeof(*skc
));
1572 strncpy(skc
->skc_name
, name
, skc
->skc_name_size
);
1574 skc
->skc_ctor
= ctor
;
1575 skc
->skc_dtor
= dtor
;
1576 skc
->skc_reclaim
= reclaim
;
1577 skc
->skc_private
= priv
;
1579 skc
->skc_flags
= flags
;
1580 skc
->skc_obj_size
= size
;
1581 skc
->skc_obj_align
= SPL_KMEM_CACHE_ALIGN
;
1582 skc
->skc_delay
= SPL_KMEM_CACHE_DELAY
;
1583 skc
->skc_reap
= SPL_KMEM_CACHE_REAP
;
1584 atomic_set(&skc
->skc_ref
, 0);
1586 INIT_LIST_HEAD(&skc
->skc_list
);
1587 INIT_LIST_HEAD(&skc
->skc_complete_list
);
1588 INIT_LIST_HEAD(&skc
->skc_partial_list
);
1589 skc
->skc_emergency_tree
= RB_ROOT
;
1590 spin_lock_init(&skc
->skc_lock
);
1591 init_waitqueue_head(&skc
->skc_waitq
);
1592 skc
->skc_slab_fail
= 0;
1593 skc
->skc_slab_create
= 0;
1594 skc
->skc_slab_destroy
= 0;
1595 skc
->skc_slab_total
= 0;
1596 skc
->skc_slab_alloc
= 0;
1597 skc
->skc_slab_max
= 0;
1598 skc
->skc_obj_total
= 0;
1599 skc
->skc_obj_alloc
= 0;
1600 skc
->skc_obj_max
= 0;
1601 skc
->skc_obj_deadlock
= 0;
1602 skc
->skc_obj_emergency
= 0;
1603 skc
->skc_obj_emergency_max
= 0;
1606 VERIFY(ISP2(align
));
1607 VERIFY3U(align
, >=, SPL_KMEM_CACHE_ALIGN
); /* Min alignment */
1608 VERIFY3U(align
, <=, PAGE_SIZE
); /* Max alignment */
1609 skc
->skc_obj_align
= align
;
1612 /* If none passed select a cache type based on object size */
1613 if (!(skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
))) {
1614 if (spl_obj_size(skc
) < (PAGE_SIZE
/ 8))
1615 skc
->skc_flags
|= KMC_KMEM
;
1617 skc
->skc_flags
|= KMC_VMEM
;
1620 rc
= spl_slab_size(skc
, &skc
->skc_slab_objs
, &skc
->skc_slab_size
);
1624 rc
= spl_magazine_create(skc
);
1628 if (spl_kmem_cache_expire
& KMC_EXPIRE_AGE
)
1629 skc
->skc_taskqid
= taskq_dispatch_delay(spl_kmem_cache_taskq
,
1630 spl_cache_age
, skc
, TQ_SLEEP
,
1631 ddi_get_lbolt() + skc
->skc_delay
/ 3 * HZ
);
1633 down_write(&spl_kmem_cache_sem
);
1634 list_add_tail(&skc
->skc_list
, &spl_kmem_cache_list
);
1635 up_write(&spl_kmem_cache_sem
);
1639 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1640 kmem_free(skc
, sizeof(*skc
));
1643 EXPORT_SYMBOL(spl_kmem_cache_create
);
1646 * Register a move callback to for cache defragmentation.
1647 * XXX: Unimplemented but harmless to stub out for now.
1650 spl_kmem_cache_set_move(spl_kmem_cache_t
*skc
,
1651 kmem_cbrc_t (move
)(void *, void *, size_t, void *))
1653 ASSERT(move
!= NULL
);
1655 EXPORT_SYMBOL(spl_kmem_cache_set_move
);
1658 * Destroy a cache and all objects associated with the cache.
1661 spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
)
1663 DECLARE_WAIT_QUEUE_HEAD(wq
);
1667 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1669 down_write(&spl_kmem_cache_sem
);
1670 list_del_init(&skc
->skc_list
);
1671 up_write(&spl_kmem_cache_sem
);
1673 /* Cancel any and wait for any pending delayed tasks */
1674 VERIFY(!test_and_set_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1676 spin_lock(&skc
->skc_lock
);
1677 id
= skc
->skc_taskqid
;
1678 spin_unlock(&skc
->skc_lock
);
1680 taskq_cancel_id(spl_kmem_cache_taskq
, id
);
1682 /* Wait until all current callers complete, this is mainly
1683 * to catch the case where a low memory situation triggers a
1684 * cache reaping action which races with this destroy. */
1685 wait_event(wq
, atomic_read(&skc
->skc_ref
) == 0);
1687 spl_magazine_destroy(skc
);
1688 spl_slab_reclaim(skc
, 0, 1);
1689 spin_lock(&skc
->skc_lock
);
1691 /* Validate there are no objects in use and free all the
1692 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
1693 ASSERT3U(skc
->skc_slab_alloc
, ==, 0);
1694 ASSERT3U(skc
->skc_obj_alloc
, ==, 0);
1695 ASSERT3U(skc
->skc_slab_total
, ==, 0);
1696 ASSERT3U(skc
->skc_obj_total
, ==, 0);
1697 ASSERT3U(skc
->skc_obj_emergency
, ==, 0);
1698 ASSERT(list_empty(&skc
->skc_complete_list
));
1700 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1701 spin_unlock(&skc
->skc_lock
);
1703 kmem_free(skc
, sizeof(*skc
));
1707 EXPORT_SYMBOL(spl_kmem_cache_destroy
);
1710 * Allocate an object from a slab attached to the cache. This is used to
1711 * repopulate the per-cpu magazine caches in batches when they run low.
1714 spl_cache_obj(spl_kmem_cache_t
*skc
, spl_kmem_slab_t
*sks
)
1716 spl_kmem_obj_t
*sko
;
1718 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1719 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1720 ASSERT(spin_is_locked(&skc
->skc_lock
));
1722 sko
= list_entry(sks
->sks_free_list
.next
, spl_kmem_obj_t
, sko_list
);
1723 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1724 ASSERT(sko
->sko_addr
!= NULL
);
1726 /* Remove from sks_free_list */
1727 list_del_init(&sko
->sko_list
);
1729 sks
->sks_age
= jiffies
;
1731 skc
->skc_obj_alloc
++;
1733 /* Track max obj usage statistics */
1734 if (skc
->skc_obj_alloc
> skc
->skc_obj_max
)
1735 skc
->skc_obj_max
= skc
->skc_obj_alloc
;
1737 /* Track max slab usage statistics */
1738 if (sks
->sks_ref
== 1) {
1739 skc
->skc_slab_alloc
++;
1741 if (skc
->skc_slab_alloc
> skc
->skc_slab_max
)
1742 skc
->skc_slab_max
= skc
->skc_slab_alloc
;
1745 return sko
->sko_addr
;
1749 * Generic slab allocation function to run by the global work queues.
1750 * It is responsible for allocating a new slab, linking it in to the list
1751 * of partial slabs, and then waking any waiters.
1754 spl_cache_grow_work(void *data
)
1756 spl_kmem_alloc_t
*ska
= (spl_kmem_alloc_t
*)data
;
1757 spl_kmem_cache_t
*skc
= ska
->ska_cache
;
1758 spl_kmem_slab_t
*sks
;
1760 sks
= spl_slab_alloc(skc
, ska
->ska_flags
| __GFP_NORETRY
| KM_NODEBUG
);
1761 spin_lock(&skc
->skc_lock
);
1763 skc
->skc_slab_total
++;
1764 skc
->skc_obj_total
+= sks
->sks_objs
;
1765 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1768 atomic_dec(&skc
->skc_ref
);
1769 clear_bit(KMC_BIT_GROWING
, &skc
->skc_flags
);
1770 clear_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
);
1771 wake_up_all(&skc
->skc_waitq
);
1772 spin_unlock(&skc
->skc_lock
);
1778 * Returns non-zero when a new slab should be available.
1781 spl_cache_grow_wait(spl_kmem_cache_t
*skc
)
1783 return !test_bit(KMC_BIT_GROWING
, &skc
->skc_flags
);
1787 spl_cache_reclaim_wait(void *word
)
1794 * No available objects on any slabs, create a new slab.
1797 spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
, void **obj
)
1802 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1807 * Before allocating a new slab wait for any reaping to complete and
1808 * then return so the local magazine can be rechecked for new objects.
1810 if (test_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1811 rc
= wait_on_bit(&skc
->skc_flags
, KMC_BIT_REAPING
,
1812 spl_cache_reclaim_wait
, TASK_UNINTERRUPTIBLE
);
1813 SRETURN(rc
? rc
: -EAGAIN
);
1817 * This is handled by dispatching a work request to the global work
1818 * queue. This allows us to asynchronously allocate a new slab while
1819 * retaining the ability to safely fall back to a smaller synchronous
1820 * allocations to ensure forward progress is always maintained.
1822 if (test_and_set_bit(KMC_BIT_GROWING
, &skc
->skc_flags
) == 0) {
1823 spl_kmem_alloc_t
*ska
;
1825 ska
= kmalloc(sizeof(*ska
), flags
);
1827 clear_bit(KMC_BIT_GROWING
, &skc
->skc_flags
);
1828 wake_up_all(&skc
->skc_waitq
);
1832 atomic_inc(&skc
->skc_ref
);
1833 ska
->ska_cache
= skc
;
1834 ska
->ska_flags
= flags
& ~__GFP_FS
;
1835 taskq_init_ent(&ska
->ska_tqe
);
1836 taskq_dispatch_ent(spl_kmem_cache_taskq
,
1837 spl_cache_grow_work
, ska
, 0, &ska
->ska_tqe
);
1841 * The goal here is to only detect the rare case where a virtual slab
1842 * allocation has deadlocked. We must be careful to minimize the use
1843 * of emergency objects which are more expensive to track. Therefore,
1844 * we set a very long timeout for the asynchronous allocation and if
1845 * the timeout is reached the cache is flagged as deadlocked. From
1846 * this point only new emergency objects will be allocated until the
1847 * asynchronous allocation completes and clears the deadlocked flag.
1849 if (test_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
)) {
1850 rc
= spl_emergency_alloc(skc
, flags
, obj
);
1852 remaining
= wait_event_timeout(skc
->skc_waitq
,
1853 spl_cache_grow_wait(skc
), HZ
);
1855 if (!remaining
&& test_bit(KMC_BIT_VMEM
, &skc
->skc_flags
)) {
1856 spin_lock(&skc
->skc_lock
);
1857 if (test_bit(KMC_BIT_GROWING
, &skc
->skc_flags
)) {
1858 set_bit(KMC_BIT_DEADLOCKED
, &skc
->skc_flags
);
1859 skc
->skc_obj_deadlock
++;
1861 spin_unlock(&skc
->skc_lock
);
1871 * Refill a per-cpu magazine with objects from the slabs for this cache.
1872 * Ideally the magazine can be repopulated using existing objects which have
1873 * been released, however if we are unable to locate enough free objects new
1874 * slabs of objects will be created. On success NULL is returned, otherwise
1875 * the address of a single emergency object is returned for use by the caller.
1878 spl_cache_refill(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flags
)
1880 spl_kmem_slab_t
*sks
;
1881 int count
= 0, rc
, refill
;
1885 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1886 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1888 refill
= MIN(skm
->skm_refill
, skm
->skm_size
- skm
->skm_avail
);
1889 spin_lock(&skc
->skc_lock
);
1891 while (refill
> 0) {
1892 /* No slabs available we may need to grow the cache */
1893 if (list_empty(&skc
->skc_partial_list
)) {
1894 spin_unlock(&skc
->skc_lock
);
1897 rc
= spl_cache_grow(skc
, flags
, &obj
);
1898 local_irq_disable();
1900 /* Emergency object for immediate use by caller */
1901 if (rc
== 0 && obj
!= NULL
)
1907 /* Rescheduled to different CPU skm is not local */
1908 if (skm
!= skc
->skc_mag
[smp_processor_id()])
1911 /* Potentially rescheduled to the same CPU but
1912 * allocations may have occurred from this CPU while
1913 * we were sleeping so recalculate max refill. */
1914 refill
= MIN(refill
, skm
->skm_size
- skm
->skm_avail
);
1916 spin_lock(&skc
->skc_lock
);
1920 /* Grab the next available slab */
1921 sks
= list_entry((&skc
->skc_partial_list
)->next
,
1922 spl_kmem_slab_t
, sks_list
);
1923 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1924 ASSERT(sks
->sks_ref
< sks
->sks_objs
);
1925 ASSERT(!list_empty(&sks
->sks_free_list
));
1927 /* Consume as many objects as needed to refill the requested
1928 * cache. We must also be careful not to overfill it. */
1929 while (sks
->sks_ref
< sks
->sks_objs
&& refill
-- > 0 && ++count
) {
1930 ASSERT(skm
->skm_avail
< skm
->skm_size
);
1931 ASSERT(count
< skm
->skm_size
);
1932 skm
->skm_objs
[skm
->skm_avail
++]=spl_cache_obj(skc
,sks
);
1935 /* Move slab to skc_complete_list when full */
1936 if (sks
->sks_ref
== sks
->sks_objs
) {
1937 list_del(&sks
->sks_list
);
1938 list_add(&sks
->sks_list
, &skc
->skc_complete_list
);
1942 spin_unlock(&skc
->skc_lock
);
1948 * Release an object back to the slab from which it came.
1951 spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
)
1953 spl_kmem_slab_t
*sks
= NULL
;
1954 spl_kmem_obj_t
*sko
= NULL
;
1957 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1958 ASSERT(spin_is_locked(&skc
->skc_lock
));
1960 sko
= spl_sko_from_obj(skc
, obj
);
1961 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1962 sks
= sko
->sko_slab
;
1963 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1964 ASSERT(sks
->sks_cache
== skc
);
1965 list_add(&sko
->sko_list
, &sks
->sks_free_list
);
1967 sks
->sks_age
= jiffies
;
1969 skc
->skc_obj_alloc
--;
1971 /* Move slab to skc_partial_list when no longer full. Slabs
1972 * are added to the head to keep the partial list is quasi-full
1973 * sorted order. Fuller at the head, emptier at the tail. */
1974 if (sks
->sks_ref
== (sks
->sks_objs
- 1)) {
1975 list_del(&sks
->sks_list
);
1976 list_add(&sks
->sks_list
, &skc
->skc_partial_list
);
1979 /* Move empty slabs to the end of the partial list so
1980 * they can be easily found and freed during reclamation. */
1981 if (sks
->sks_ref
== 0) {
1982 list_del(&sks
->sks_list
);
1983 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1984 skc
->skc_slab_alloc
--;
1991 * Allocate an object from the per-cpu magazine, or if the magazine
1992 * is empty directly allocate from a slab and repopulate the magazine.
1995 spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
)
1997 spl_kmem_magazine_t
*skm
;
2001 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
2002 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
2003 ASSERT(flags
& KM_SLEEP
);
2004 atomic_inc(&skc
->skc_ref
);
2005 local_irq_disable();
2008 /* Safe to update per-cpu structure without lock, but
2009 * in the restart case we must be careful to reacquire
2010 * the local magazine since this may have changed
2011 * when we need to grow the cache. */
2012 skm
= skc
->skc_mag
[smp_processor_id()];
2013 ASSERTF(skm
->skm_magic
== SKM_MAGIC
, "%x != %x: %s/%p/%p %x/%x/%x\n",
2014 skm
->skm_magic
, SKM_MAGIC
, skc
->skc_name
, skc
, skm
,
2015 skm
->skm_size
, skm
->skm_refill
, skm
->skm_avail
);
2017 if (likely(skm
->skm_avail
)) {
2018 /* Object available in CPU cache, use it */
2019 obj
= skm
->skm_objs
[--skm
->skm_avail
];
2020 skm
->skm_age
= jiffies
;
2022 obj
= spl_cache_refill(skc
, skm
, flags
);
2024 SGOTO(restart
, obj
= NULL
);
2029 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
2031 /* Pre-emptively migrate object to CPU L1 cache */
2033 atomic_dec(&skc
->skc_ref
);
2037 EXPORT_SYMBOL(spl_kmem_cache_alloc
);
2040 * Free an object back to the local per-cpu magazine, there is no
2041 * guarantee that this is the same magazine the object was originally
2042 * allocated from. We may need to flush entire from the magazine
2043 * back to the slabs to make space.
2046 spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
)
2048 spl_kmem_magazine_t
*skm
;
2049 unsigned long flags
;
2052 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
2053 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
2054 atomic_inc(&skc
->skc_ref
);
2057 * Only virtual slabs may have emergency objects and these objects
2058 * are guaranteed to have physical addresses. They must be removed
2059 * from the tree of emergency objects and the freed.
2061 if ((skc
->skc_flags
& KMC_VMEM
) && !kmem_virt(obj
))
2062 SGOTO(out
, spl_emergency_free(skc
, obj
));
2064 local_irq_save(flags
);
2066 /* Safe to update per-cpu structure without lock, but
2067 * no remote memory allocation tracking is being performed
2068 * it is entirely possible to allocate an object from one
2069 * CPU cache and return it to another. */
2070 skm
= skc
->skc_mag
[smp_processor_id()];
2071 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
2073 /* Per-CPU cache full, flush it to make space */
2074 if (unlikely(skm
->skm_avail
>= skm
->skm_size
))
2075 spl_cache_flush(skc
, skm
, skm
->skm_refill
);
2077 /* Available space in cache, use it */
2078 skm
->skm_objs
[skm
->skm_avail
++] = obj
;
2080 local_irq_restore(flags
);
2082 atomic_dec(&skc
->skc_ref
);
2086 EXPORT_SYMBOL(spl_kmem_cache_free
);
2089 * The generic shrinker function for all caches. Under Linux a shrinker
2090 * may not be tightly coupled with a slab cache. In fact Linux always
2091 * systematically tries calling all registered shrinker callbacks which
2092 * report that they contain unused objects. Because of this we only
2093 * register one shrinker function in the shim layer for all slab caches.
2094 * We always attempt to shrink all caches when this generic shrinker
2095 * is called. The shrinker should return the number of free objects
2096 * in the cache when called with nr_to_scan == 0 but not attempt to
2097 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
2098 * objects should be freed, which differs from Solaris semantics.
2099 * Solaris semantics are to free all available objects which may (and
2100 * probably will) be more objects than the requested nr_to_scan.
2103 __spl_kmem_cache_generic_shrinker(struct shrinker
*shrink
,
2104 struct shrink_control
*sc
)
2106 spl_kmem_cache_t
*skc
;
2109 down_read(&spl_kmem_cache_sem
);
2110 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
2112 spl_kmem_cache_reap_now(skc
,
2113 MAX(sc
->nr_to_scan
>> fls64(skc
->skc_slab_objs
), 1));
2116 * Presume everything alloc'ed in reclaimable, this ensures
2117 * we are called again with nr_to_scan > 0 so can try and
2118 * reclaim. The exact number is not important either so
2119 * we forgo taking this already highly contented lock.
2121 unused
+= skc
->skc_obj_alloc
;
2123 up_read(&spl_kmem_cache_sem
);
2126 * After performing reclaim always return -1 to indicate we cannot
2127 * perform additional reclaim. This prevents shrink_slabs() from
2128 * repeatedly invoking this generic shrinker and potentially spinning.
2136 SPL_SHRINKER_CALLBACK_WRAPPER(spl_kmem_cache_generic_shrinker
);
2139 * Call the registered reclaim function for a cache. Depending on how
2140 * many and which objects are released it may simply repopulate the
2141 * local magazine which will then need to age-out. Objects which cannot
2142 * fit in the magazine we will be released back to their slabs which will
2143 * also need to age out before being release. This is all just best
2144 * effort and we do not want to thrash creating and destroying slabs.
2147 spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
, int count
)
2151 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
2152 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
2154 /* Prevent concurrent cache reaping when contended */
2155 if (test_and_set_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
2160 atomic_inc(&skc
->skc_ref
);
2163 * When a reclaim function is available it may be invoked repeatedly
2164 * until at least a single slab can be freed. This ensures that we
2165 * do free memory back to the system. This helps minimize the chance
2166 * of an OOM event when the bulk of memory is used by the slab.
2168 * When free slabs are already available the reclaim callback will be
2169 * skipped. Additionally, if no forward progress is detected despite
2170 * a reclaim function the cache will be skipped to avoid deadlock.
2172 * Longer term this would be the correct place to add the code which
2173 * repacks the slabs in order minimize fragmentation.
2175 if (skc
->skc_reclaim
) {
2176 uint64_t objects
= UINT64_MAX
;
2180 spin_lock(&skc
->skc_lock
);
2182 (skc
->skc_slab_total
> 0) &&
2183 ((skc
->skc_slab_total
- skc
->skc_slab_alloc
) == 0) &&
2184 (skc
->skc_obj_alloc
< objects
);
2186 objects
= skc
->skc_obj_alloc
;
2187 spin_unlock(&skc
->skc_lock
);
2190 skc
->skc_reclaim(skc
->skc_private
);
2192 } while (do_reclaim
);
2195 /* Reclaim from the magazine then the slabs ignoring age and delay. */
2196 if (spl_kmem_cache_expire
& KMC_EXPIRE_MEM
) {
2197 spl_kmem_magazine_t
*skm
;
2198 unsigned long irq_flags
;
2200 local_irq_save(irq_flags
);
2201 skm
= skc
->skc_mag
[smp_processor_id()];
2202 spl_cache_flush(skc
, skm
, skm
->skm_avail
);
2203 local_irq_restore(irq_flags
);
2206 spl_slab_reclaim(skc
, count
, 1);
2207 clear_bit(KMC_BIT_REAPING
, &skc
->skc_flags
);
2208 smp_mb__after_clear_bit();
2209 wake_up_bit(&skc
->skc_flags
, KMC_BIT_REAPING
);
2211 atomic_dec(&skc
->skc_ref
);
2215 EXPORT_SYMBOL(spl_kmem_cache_reap_now
);
2218 * Reap all free slabs from all registered caches.
2223 struct shrink_control sc
;
2225 sc
.nr_to_scan
= KMC_REAP_CHUNK
;
2226 sc
.gfp_mask
= GFP_KERNEL
;
2228 __spl_kmem_cache_generic_shrinker(NULL
, &sc
);
2230 EXPORT_SYMBOL(spl_kmem_reap
);
2232 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
2234 spl_sprintf_addr(kmem_debug_t
*kd
, char *str
, int len
, int min
)
2236 int size
= ((len
- 1) < kd
->kd_size
) ? (len
- 1) : kd
->kd_size
;
2239 ASSERT(str
!= NULL
&& len
>= 17);
2240 memset(str
, 0, len
);
2242 /* Check for a fully printable string, and while we are at
2243 * it place the printable characters in the passed buffer. */
2244 for (i
= 0; i
< size
; i
++) {
2245 str
[i
] = ((char *)(kd
->kd_addr
))[i
];
2246 if (isprint(str
[i
])) {
2249 /* Minimum number of printable characters found
2250 * to make it worthwhile to print this as ascii. */
2260 sprintf(str
, "%02x%02x%02x%02x%02x%02x%02x%02x",
2261 *((uint8_t *)kd
->kd_addr
),
2262 *((uint8_t *)kd
->kd_addr
+ 2),
2263 *((uint8_t *)kd
->kd_addr
+ 4),
2264 *((uint8_t *)kd
->kd_addr
+ 6),
2265 *((uint8_t *)kd
->kd_addr
+ 8),
2266 *((uint8_t *)kd
->kd_addr
+ 10),
2267 *((uint8_t *)kd
->kd_addr
+ 12),
2268 *((uint8_t *)kd
->kd_addr
+ 14));
2275 spl_kmem_init_tracking(struct list_head
*list
, spinlock_t
*lock
, int size
)
2280 spin_lock_init(lock
);
2281 INIT_LIST_HEAD(list
);
2283 for (i
= 0; i
< size
; i
++)
2284 INIT_HLIST_HEAD(&kmem_table
[i
]);
2290 spl_kmem_fini_tracking(struct list_head
*list
, spinlock_t
*lock
)
2292 unsigned long flags
;
2297 spin_lock_irqsave(lock
, flags
);
2298 if (!list_empty(list
))
2299 printk(KERN_WARNING
"%-16s %-5s %-16s %s:%s\n", "address",
2300 "size", "data", "func", "line");
2302 list_for_each_entry(kd
, list
, kd_list
)
2303 printk(KERN_WARNING
"%p %-5d %-16s %s:%d\n", kd
->kd_addr
,
2304 (int)kd
->kd_size
, spl_sprintf_addr(kd
, str
, 17, 8),
2305 kd
->kd_func
, kd
->kd_line
);
2307 spin_unlock_irqrestore(lock
, flags
);
2310 #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
2311 #define spl_kmem_init_tracking(list, lock, size)
2312 #define spl_kmem_fini_tracking(list, lock)
2313 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
2316 spl_kmem_init_globals(void)
2320 /* For now all zones are includes, it may be wise to restrict
2321 * this to normal and highmem zones if we see problems. */
2322 for_each_zone(zone
) {
2324 if (!populated_zone(zone
))
2327 minfree
+= min_wmark_pages(zone
);
2328 desfree
+= low_wmark_pages(zone
);
2329 lotsfree
+= high_wmark_pages(zone
);
2332 /* Solaris default values */
2333 swapfs_minfree
= MAX(2*1024*1024 >> PAGE_SHIFT
, physmem
>> 3);
2334 swapfs_reserve
= MIN(4*1024*1024 >> PAGE_SHIFT
, physmem
>> 4);
2338 * Called at module init when it is safe to use spl_kallsyms_lookup_name()
2341 spl_kmem_init_kallsyms_lookup(void)
2343 #ifndef HAVE_GET_VMALLOC_INFO
2344 get_vmalloc_info_fn
= (get_vmalloc_info_t
)
2345 spl_kallsyms_lookup_name("get_vmalloc_info");
2346 if (!get_vmalloc_info_fn
) {
2347 printk(KERN_ERR
"Error: Unknown symbol get_vmalloc_info\n");
2350 #endif /* HAVE_GET_VMALLOC_INFO */
2352 #ifdef HAVE_PGDAT_HELPERS
2353 # ifndef HAVE_FIRST_ONLINE_PGDAT
2354 first_online_pgdat_fn
= (first_online_pgdat_t
)
2355 spl_kallsyms_lookup_name("first_online_pgdat");
2356 if (!first_online_pgdat_fn
) {
2357 printk(KERN_ERR
"Error: Unknown symbol first_online_pgdat\n");
2360 # endif /* HAVE_FIRST_ONLINE_PGDAT */
2362 # ifndef HAVE_NEXT_ONLINE_PGDAT
2363 next_online_pgdat_fn
= (next_online_pgdat_t
)
2364 spl_kallsyms_lookup_name("next_online_pgdat");
2365 if (!next_online_pgdat_fn
) {
2366 printk(KERN_ERR
"Error: Unknown symbol next_online_pgdat\n");
2369 # endif /* HAVE_NEXT_ONLINE_PGDAT */
2371 # ifndef HAVE_NEXT_ZONE
2372 next_zone_fn
= (next_zone_t
)
2373 spl_kallsyms_lookup_name("next_zone");
2374 if (!next_zone_fn
) {
2375 printk(KERN_ERR
"Error: Unknown symbol next_zone\n");
2378 # endif /* HAVE_NEXT_ZONE */
2380 #else /* HAVE_PGDAT_HELPERS */
2382 # ifndef HAVE_PGDAT_LIST
2383 pgdat_list_addr
= *(struct pglist_data
**)
2384 spl_kallsyms_lookup_name("pgdat_list");
2385 if (!pgdat_list_addr
) {
2386 printk(KERN_ERR
"Error: Unknown symbol pgdat_list\n");
2389 # endif /* HAVE_PGDAT_LIST */
2390 #endif /* HAVE_PGDAT_HELPERS */
2392 #if defined(NEED_GET_ZONE_COUNTS) && !defined(HAVE_GET_ZONE_COUNTS)
2393 get_zone_counts_fn
= (get_zone_counts_t
)
2394 spl_kallsyms_lookup_name("get_zone_counts");
2395 if (!get_zone_counts_fn
) {
2396 printk(KERN_ERR
"Error: Unknown symbol get_zone_counts\n");
2399 #endif /* NEED_GET_ZONE_COUNTS && !HAVE_GET_ZONE_COUNTS */
2402 * It is now safe to initialize the global tunings which rely on
2403 * the use of the for_each_zone() macro. This macro in turns
2404 * depends on the *_pgdat symbols which are now available.
2406 spl_kmem_init_globals();
2408 #ifndef HAVE_SHRINK_DCACHE_MEMORY
2409 /* When shrink_dcache_memory_fn == NULL support is disabled */
2410 shrink_dcache_memory_fn
= (shrink_dcache_memory_t
)
2411 spl_kallsyms_lookup_name("shrink_dcache_memory");
2412 #endif /* HAVE_SHRINK_DCACHE_MEMORY */
2414 #ifndef HAVE_SHRINK_ICACHE_MEMORY
2415 /* When shrink_icache_memory_fn == NULL support is disabled */
2416 shrink_icache_memory_fn
= (shrink_icache_memory_t
)
2417 spl_kallsyms_lookup_name("shrink_icache_memory");
2418 #endif /* HAVE_SHRINK_ICACHE_MEMORY */
2430 kmem_alloc_used_set(0);
2431 vmem_alloc_used_set(0);
2433 spl_kmem_init_tracking(&kmem_list
, &kmem_lock
, KMEM_TABLE_SIZE
);
2434 spl_kmem_init_tracking(&vmem_list
, &vmem_lock
, VMEM_TABLE_SIZE
);
2437 init_rwsem(&spl_kmem_cache_sem
);
2438 INIT_LIST_HEAD(&spl_kmem_cache_list
);
2439 spl_kmem_cache_taskq
= taskq_create("spl_kmem_cache",
2440 1, maxclsyspri
, 1, 32, TASKQ_PREPOPULATE
);
2442 spl_register_shrinker(&spl_kmem_cache_shrinker
);
2452 spl_unregister_shrinker(&spl_kmem_cache_shrinker
);
2453 taskq_destroy(spl_kmem_cache_taskq
);
2456 /* Display all unreclaimed memory addresses, including the
2457 * allocation size and the first few bytes of what's located
2458 * at that address to aid in debugging. Performance is not
2459 * a serious concern here since it is module unload time. */
2460 if (kmem_alloc_used_read() != 0)
2461 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
2462 "kmem leaked %ld/%ld bytes\n",
2463 kmem_alloc_used_read(), kmem_alloc_max
);
2466 if (vmem_alloc_used_read() != 0)
2467 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
2468 "vmem leaked %ld/%ld bytes\n",
2469 vmem_alloc_used_read(), vmem_alloc_max
);
2471 spl_kmem_fini_tracking(&kmem_list
, &kmem_lock
);
2472 spl_kmem_fini_tracking(&vmem_list
, &vmem_lock
);
2473 #endif /* DEBUG_KMEM */