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://github.com/behlendorf/spl/>.
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 * The minimum amount of memory measured in pages to be free at all
38 * times on the system. This is similar to Linux's zone->pages_min
39 * multipled by the number of zones and is sized based on that.
42 EXPORT_SYMBOL(minfree
);
45 * The desired amount of memory measured in pages to be free at all
46 * times on the system. This is similar to Linux's zone->pages_low
47 * multipled by the number of zones and is sized based on that.
48 * Assuming all zones are being used roughly equally, when we drop
49 * below this threshold async page reclamation is triggered.
52 EXPORT_SYMBOL(desfree
);
55 * When above this amount of memory measures in pages the system is
56 * determined to have enough free memory. This is similar to Linux's
57 * zone->pages_high multipled by the number of zones and is sized based
58 * on that. Assuming all zones are being used roughly equally, when
59 * async page reclamation reaches this threshold it stops.
62 EXPORT_SYMBOL(lotsfree
);
64 /* Unused always 0 in this implementation */
66 EXPORT_SYMBOL(needfree
);
68 pgcnt_t swapfs_minfree
= 0;
69 EXPORT_SYMBOL(swapfs_minfree
);
71 pgcnt_t swapfs_reserve
= 0;
72 EXPORT_SYMBOL(swapfs_reserve
);
74 vmem_t
*heap_arena
= NULL
;
75 EXPORT_SYMBOL(heap_arena
);
77 vmem_t
*zio_alloc_arena
= NULL
;
78 EXPORT_SYMBOL(zio_alloc_arena
);
80 vmem_t
*zio_arena
= NULL
;
81 EXPORT_SYMBOL(zio_arena
);
83 #ifndef HAVE_GET_VMALLOC_INFO
84 get_vmalloc_info_t get_vmalloc_info_fn
= SYMBOL_POISON
;
85 EXPORT_SYMBOL(get_vmalloc_info_fn
);
86 #endif /* HAVE_GET_VMALLOC_INFO */
88 #ifdef HAVE_PGDAT_HELPERS
89 # ifndef HAVE_FIRST_ONLINE_PGDAT
90 first_online_pgdat_t first_online_pgdat_fn
= SYMBOL_POISON
;
91 EXPORT_SYMBOL(first_online_pgdat_fn
);
92 # endif /* HAVE_FIRST_ONLINE_PGDAT */
94 # ifndef HAVE_NEXT_ONLINE_PGDAT
95 next_online_pgdat_t next_online_pgdat_fn
= SYMBOL_POISON
;
96 EXPORT_SYMBOL(next_online_pgdat_fn
);
97 # endif /* HAVE_NEXT_ONLINE_PGDAT */
99 # ifndef HAVE_NEXT_ZONE
100 next_zone_t next_zone_fn
= SYMBOL_POISON
;
101 EXPORT_SYMBOL(next_zone_fn
);
102 # endif /* HAVE_NEXT_ZONE */
104 #else /* HAVE_PGDAT_HELPERS */
106 # ifndef HAVE_PGDAT_LIST
107 struct pglist_data
*pgdat_list_addr
= SYMBOL_POISON
;
108 EXPORT_SYMBOL(pgdat_list_addr
);
109 # endif /* HAVE_PGDAT_LIST */
111 #endif /* HAVE_PGDAT_HELPERS */
113 #ifdef NEED_GET_ZONE_COUNTS
114 # ifndef HAVE_GET_ZONE_COUNTS
115 get_zone_counts_t get_zone_counts_fn
= SYMBOL_POISON
;
116 EXPORT_SYMBOL(get_zone_counts_fn
);
117 # endif /* HAVE_GET_ZONE_COUNTS */
120 spl_global_page_state(spl_zone_stat_item_t item
)
122 unsigned long active
;
123 unsigned long inactive
;
126 get_zone_counts(&active
, &inactive
, &free
);
128 case SPL_NR_FREE_PAGES
: return free
;
129 case SPL_NR_INACTIVE
: return inactive
;
130 case SPL_NR_ACTIVE
: return active
;
131 default: ASSERT(0); /* Unsupported */
137 # ifdef HAVE_GLOBAL_PAGE_STATE
139 spl_global_page_state(spl_zone_stat_item_t item
)
141 unsigned long pages
= 0;
144 case SPL_NR_FREE_PAGES
:
145 # ifdef HAVE_ZONE_STAT_ITEM_NR_FREE_PAGES
146 pages
+= global_page_state(NR_FREE_PAGES
);
149 case SPL_NR_INACTIVE
:
150 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE
151 pages
+= global_page_state(NR_INACTIVE
);
153 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_ANON
154 pages
+= global_page_state(NR_INACTIVE_ANON
);
156 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_FILE
157 pages
+= global_page_state(NR_INACTIVE_FILE
);
161 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE
162 pages
+= global_page_state(NR_ACTIVE
);
164 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_ANON
165 pages
+= global_page_state(NR_ACTIVE_ANON
);
167 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_FILE
168 pages
+= global_page_state(NR_ACTIVE_FILE
);
172 ASSERT(0); /* Unsupported */
178 # error "Both global_page_state() and get_zone_counts() unavailable"
179 # endif /* HAVE_GLOBAL_PAGE_STATE */
180 #endif /* NEED_GET_ZONE_COUNTS */
181 EXPORT_SYMBOL(spl_global_page_state
);
184 spl_kmem_availrmem(void)
186 /* The amount of easily available memory */
187 return (spl_global_page_state(SPL_NR_FREE_PAGES
) +
188 spl_global_page_state(SPL_NR_INACTIVE
));
190 EXPORT_SYMBOL(spl_kmem_availrmem
);
193 vmem_size(vmem_t
*vmp
, int typemask
)
195 struct vmalloc_info vmi
;
199 ASSERT(typemask
& (VMEM_ALLOC
| VMEM_FREE
));
201 get_vmalloc_info(&vmi
);
202 if (typemask
& VMEM_ALLOC
)
203 size
+= (size_t)vmi
.used
;
205 if (typemask
& VMEM_FREE
)
206 size
+= (size_t)(VMALLOC_TOTAL
- vmi
.used
);
210 EXPORT_SYMBOL(vmem_size
);
217 EXPORT_SYMBOL(kmem_debugging
);
219 #ifndef HAVE_KVASPRINTF
220 /* Simplified asprintf. */
221 char *kvasprintf(gfp_t gfp
, const char *fmt
, va_list ap
)
228 len
= vsnprintf(NULL
, 0, fmt
, aq
);
231 p
= kmalloc(len
+1, gfp
);
235 vsnprintf(p
, len
+1, fmt
, ap
);
239 EXPORT_SYMBOL(kvasprintf
);
240 #endif /* HAVE_KVASPRINTF */
243 kmem_vasprintf(const char *fmt
, va_list ap
)
250 ptr
= kvasprintf(GFP_KERNEL
, fmt
, aq
);
252 } while (ptr
== NULL
);
256 EXPORT_SYMBOL(kmem_vasprintf
);
259 kmem_asprintf(const char *fmt
, ...)
266 ptr
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
268 } while (ptr
== NULL
);
272 EXPORT_SYMBOL(kmem_asprintf
);
275 __strdup(const char *str
, int flags
)
281 ptr
= kmalloc_nofail(n
+ 1, flags
);
283 memcpy(ptr
, str
, n
+ 1);
289 strdup(const char *str
)
291 return __strdup(str
, KM_SLEEP
);
293 EXPORT_SYMBOL(strdup
);
300 EXPORT_SYMBOL(strfree
);
303 * Memory allocation interfaces and debugging for basic kmem_*
304 * and vmem_* style memory allocation. When DEBUG_KMEM is enabled
305 * the SPL will keep track of the total memory allocated, and
306 * report any memory leaked when the module is unloaded.
310 /* Shim layer memory accounting */
311 # ifdef HAVE_ATOMIC64_T
312 atomic64_t kmem_alloc_used
= ATOMIC64_INIT(0);
313 unsigned long long kmem_alloc_max
= 0;
314 atomic64_t vmem_alloc_used
= ATOMIC64_INIT(0);
315 unsigned long long vmem_alloc_max
= 0;
316 # else /* HAVE_ATOMIC64_T */
317 atomic_t kmem_alloc_used
= ATOMIC_INIT(0);
318 unsigned long long kmem_alloc_max
= 0;
319 atomic_t vmem_alloc_used
= ATOMIC_INIT(0);
320 unsigned long long vmem_alloc_max
= 0;
321 # endif /* HAVE_ATOMIC64_T */
323 EXPORT_SYMBOL(kmem_alloc_used
);
324 EXPORT_SYMBOL(kmem_alloc_max
);
325 EXPORT_SYMBOL(vmem_alloc_used
);
326 EXPORT_SYMBOL(vmem_alloc_max
);
328 /* When DEBUG_KMEM_TRACKING is enabled not only will total bytes be tracked
329 * but also the location of every alloc and free. When the SPL module is
330 * unloaded a list of all leaked addresses and where they were allocated
331 * will be dumped to the console. Enabling this feature has a significant
332 * impact on performance but it makes finding memory leaks straight forward.
334 * Not surprisingly with debugging enabled the xmem_locks are very highly
335 * contended particularly on xfree(). If we want to run with this detailed
336 * debugging enabled for anything other than debugging we need to minimize
337 * the contention by moving to a lock per xmem_table entry model.
339 # ifdef DEBUG_KMEM_TRACKING
341 # define KMEM_HASH_BITS 10
342 # define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
344 # define VMEM_HASH_BITS 10
345 # define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS)
347 typedef struct kmem_debug
{
348 struct hlist_node kd_hlist
; /* Hash node linkage */
349 struct list_head kd_list
; /* List of all allocations */
350 void *kd_addr
; /* Allocation pointer */
351 size_t kd_size
; /* Allocation size */
352 const char *kd_func
; /* Allocation function */
353 int kd_line
; /* Allocation line */
356 spinlock_t kmem_lock
;
357 struct hlist_head kmem_table
[KMEM_TABLE_SIZE
];
358 struct list_head kmem_list
;
360 spinlock_t vmem_lock
;
361 struct hlist_head vmem_table
[VMEM_TABLE_SIZE
];
362 struct list_head vmem_list
;
364 EXPORT_SYMBOL(kmem_lock
);
365 EXPORT_SYMBOL(kmem_table
);
366 EXPORT_SYMBOL(kmem_list
);
368 EXPORT_SYMBOL(vmem_lock
);
369 EXPORT_SYMBOL(vmem_table
);
370 EXPORT_SYMBOL(vmem_list
);
372 static kmem_debug_t
*
373 kmem_del_init(spinlock_t
*lock
, struct hlist_head
*table
, int bits
, void *addr
)
375 struct hlist_head
*head
;
376 struct hlist_node
*node
;
377 struct kmem_debug
*p
;
381 spin_lock_irqsave(lock
, flags
);
383 head
= &table
[hash_ptr(addr
, bits
)];
384 hlist_for_each_entry_rcu(p
, node
, head
, kd_hlist
) {
385 if (p
->kd_addr
== addr
) {
386 hlist_del_init(&p
->kd_hlist
);
387 list_del_init(&p
->kd_list
);
388 spin_unlock_irqrestore(lock
, flags
);
393 spin_unlock_irqrestore(lock
, flags
);
399 kmem_alloc_track(size_t size
, int flags
, const char *func
, int line
,
400 int node_alloc
, int node
)
404 unsigned long irq_flags
;
407 /* Function may be called with KM_NOSLEEP so failure is possible */
408 dptr
= (kmem_debug_t
*) kmalloc_nofail(sizeof(kmem_debug_t
),
409 flags
& ~__GFP_ZERO
);
411 if (unlikely(dptr
== NULL
)) {
412 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "debug "
413 "kmem_alloc(%ld, 0x%x) at %s:%d failed (%lld/%llu)\n",
414 sizeof(kmem_debug_t
), flags
, func
, line
,
415 kmem_alloc_used_read(), kmem_alloc_max
);
418 * Marked unlikely because we should never be doing this,
419 * we tolerate to up 2 pages but a single page is best.
421 if (unlikely((size
> PAGE_SIZE
*2) && !(flags
& KM_NODEBUG
))) {
422 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "large "
423 "kmem_alloc(%llu, 0x%x) at %s:%d (%lld/%llu)\n",
424 (unsigned long long) size
, flags
, func
, line
,
425 kmem_alloc_used_read(), kmem_alloc_max
);
426 spl_debug_dumpstack(NULL
);
430 * We use __strdup() below because the string pointed to by
431 * __FUNCTION__ might not be available by the time we want
432 * to print it since the module might have been unloaded.
433 * This can only fail in the KM_NOSLEEP case.
435 dptr
->kd_func
= __strdup(func
, flags
& ~__GFP_ZERO
);
436 if (unlikely(dptr
->kd_func
== NULL
)) {
438 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
439 "debug __strdup() at %s:%d failed (%lld/%llu)\n",
440 func
, line
, kmem_alloc_used_read(), kmem_alloc_max
);
444 /* Use the correct allocator */
446 ASSERT(!(flags
& __GFP_ZERO
));
447 ptr
= kmalloc_node_nofail(size
, flags
, node
);
448 } else if (flags
& __GFP_ZERO
) {
449 ptr
= kzalloc_nofail(size
, flags
& ~__GFP_ZERO
);
451 ptr
= kmalloc_nofail(size
, flags
);
454 if (unlikely(ptr
== NULL
)) {
455 kfree(dptr
->kd_func
);
457 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "kmem_alloc"
458 "(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
459 (unsigned long long) size
, flags
, func
, line
,
460 kmem_alloc_used_read(), kmem_alloc_max
);
464 kmem_alloc_used_add(size
);
465 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max
))
466 kmem_alloc_max
= kmem_alloc_used_read();
468 INIT_HLIST_NODE(&dptr
->kd_hlist
);
469 INIT_LIST_HEAD(&dptr
->kd_list
);
472 dptr
->kd_size
= size
;
473 dptr
->kd_line
= line
;
475 spin_lock_irqsave(&kmem_lock
, irq_flags
);
476 hlist_add_head_rcu(&dptr
->kd_hlist
,
477 &kmem_table
[hash_ptr(ptr
, KMEM_HASH_BITS
)]);
478 list_add_tail(&dptr
->kd_list
, &kmem_list
);
479 spin_unlock_irqrestore(&kmem_lock
, irq_flags
);
481 SDEBUG_LIMIT(SD_INFO
,
482 "kmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
483 (unsigned long long) size
, flags
, func
, line
, ptr
,
484 kmem_alloc_used_read(), kmem_alloc_max
);
489 EXPORT_SYMBOL(kmem_alloc_track
);
492 kmem_free_track(void *ptr
, size_t size
)
497 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
498 (unsigned long long) size
);
500 dptr
= kmem_del_init(&kmem_lock
, kmem_table
, KMEM_HASH_BITS
, ptr
);
502 /* Must exist in hash due to kmem_alloc() */
505 /* Size must match */
506 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
507 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
508 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
510 kmem_alloc_used_sub(size
);
511 SDEBUG_LIMIT(SD_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
512 (unsigned long long) size
, kmem_alloc_used_read(),
515 kfree(dptr
->kd_func
);
517 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
520 memset(ptr
, 0x5a, size
);
525 EXPORT_SYMBOL(kmem_free_track
);
528 vmem_alloc_track(size_t size
, int flags
, const char *func
, int line
)
532 unsigned long irq_flags
;
535 ASSERT(flags
& KM_SLEEP
);
537 /* Function may be called with KM_NOSLEEP so failure is possible */
538 dptr
= (kmem_debug_t
*) kmalloc_nofail(sizeof(kmem_debug_t
),
539 flags
& ~__GFP_ZERO
);
540 if (unlikely(dptr
== NULL
)) {
541 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "debug "
542 "vmem_alloc(%ld, 0x%x) at %s:%d failed (%lld/%llu)\n",
543 sizeof(kmem_debug_t
), flags
, func
, line
,
544 vmem_alloc_used_read(), vmem_alloc_max
);
547 * We use __strdup() below because the string pointed to by
548 * __FUNCTION__ might not be available by the time we want
549 * to print it, since the module might have been unloaded.
550 * This can never fail because we have already asserted
551 * that flags is KM_SLEEP.
553 dptr
->kd_func
= __strdup(func
, flags
& ~__GFP_ZERO
);
554 if (unlikely(dptr
->kd_func
== NULL
)) {
556 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
557 "debug __strdup() at %s:%d failed (%lld/%llu)\n",
558 func
, line
, vmem_alloc_used_read(), vmem_alloc_max
);
562 /* Use the correct allocator */
563 if (flags
& __GFP_ZERO
) {
564 ptr
= vzalloc_nofail(size
, flags
& ~__GFP_ZERO
);
566 ptr
= vmalloc_nofail(size
, flags
);
569 if (unlikely(ptr
== NULL
)) {
570 kfree(dptr
->kd_func
);
572 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
, "vmem_alloc"
573 "(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
574 (unsigned long long) size
, flags
, func
, line
,
575 vmem_alloc_used_read(), vmem_alloc_max
);
579 vmem_alloc_used_add(size
);
580 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max
))
581 vmem_alloc_max
= vmem_alloc_used_read();
583 INIT_HLIST_NODE(&dptr
->kd_hlist
);
584 INIT_LIST_HEAD(&dptr
->kd_list
);
587 dptr
->kd_size
= size
;
588 dptr
->kd_line
= line
;
590 spin_lock_irqsave(&vmem_lock
, irq_flags
);
591 hlist_add_head_rcu(&dptr
->kd_hlist
,
592 &vmem_table
[hash_ptr(ptr
, VMEM_HASH_BITS
)]);
593 list_add_tail(&dptr
->kd_list
, &vmem_list
);
594 spin_unlock_irqrestore(&vmem_lock
, irq_flags
);
596 SDEBUG_LIMIT(SD_INFO
,
597 "vmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
598 (unsigned long long) size
, flags
, func
, line
,
599 ptr
, vmem_alloc_used_read(), vmem_alloc_max
);
604 EXPORT_SYMBOL(vmem_alloc_track
);
607 vmem_free_track(void *ptr
, size_t size
)
612 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
613 (unsigned long long) size
);
615 dptr
= kmem_del_init(&vmem_lock
, vmem_table
, VMEM_HASH_BITS
, ptr
);
617 /* Must exist in hash due to vmem_alloc() */
620 /* Size must match */
621 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
622 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
623 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
625 vmem_alloc_used_sub(size
);
626 SDEBUG_LIMIT(SD_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
627 (unsigned long long) size
, vmem_alloc_used_read(),
630 kfree(dptr
->kd_func
);
632 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
635 memset(ptr
, 0x5a, size
);
640 EXPORT_SYMBOL(vmem_free_track
);
642 # else /* DEBUG_KMEM_TRACKING */
645 kmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
,
646 int node_alloc
, int node
)
652 * Marked unlikely because we should never be doing this,
653 * we tolerate to up 2 pages but a single page is best.
655 if (unlikely((size
> PAGE_SIZE
* 2) && !(flags
& KM_NODEBUG
))) {
656 SDEBUG(SD_CONSOLE
| SD_WARNING
,
657 "large kmem_alloc(%llu, 0x%x) at %s:%d (%lld/%llu)\n",
658 (unsigned long long) size
, flags
, func
, line
,
659 kmem_alloc_used_read(), kmem_alloc_max
);
660 spl_debug_dumpstack(NULL
);
663 /* Use the correct allocator */
665 ASSERT(!(flags
& __GFP_ZERO
));
666 ptr
= kmalloc_node_nofail(size
, flags
, node
);
667 } else if (flags
& __GFP_ZERO
) {
668 ptr
= kzalloc_nofail(size
, flags
& (~__GFP_ZERO
));
670 ptr
= kmalloc_nofail(size
, flags
);
673 if (unlikely(ptr
== NULL
)) {
674 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
675 "kmem_alloc(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
676 (unsigned long long) size
, flags
, func
, line
,
677 kmem_alloc_used_read(), kmem_alloc_max
);
679 kmem_alloc_used_add(size
);
680 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max
))
681 kmem_alloc_max
= kmem_alloc_used_read();
683 SDEBUG_LIMIT(SD_INFO
,
684 "kmem_alloc(%llu, 0x%x) at %s:%d = %p (%lld/%llu)\n",
685 (unsigned long long) size
, flags
, func
, line
, ptr
,
686 kmem_alloc_used_read(), kmem_alloc_max
);
691 EXPORT_SYMBOL(kmem_alloc_debug
);
694 kmem_free_debug(void *ptr
, size_t size
)
698 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
699 (unsigned long long) size
);
701 kmem_alloc_used_sub(size
);
702 SDEBUG_LIMIT(SD_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
703 (unsigned long long) size
, kmem_alloc_used_read(),
709 EXPORT_SYMBOL(kmem_free_debug
);
712 vmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
)
717 ASSERT(flags
& KM_SLEEP
);
719 /* Use the correct allocator */
720 if (flags
& __GFP_ZERO
) {
721 ptr
= vzalloc_nofail(size
, flags
& (~__GFP_ZERO
));
723 ptr
= vmalloc_nofail(size
, flags
);
726 if (unlikely(ptr
== NULL
)) {
727 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
728 "vmem_alloc(%llu, 0x%x) at %s:%d failed (%lld/%llu)\n",
729 (unsigned long long) size
, flags
, func
, line
,
730 vmem_alloc_used_read(), vmem_alloc_max
);
732 vmem_alloc_used_add(size
);
733 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max
))
734 vmem_alloc_max
= vmem_alloc_used_read();
736 SDEBUG_LIMIT(SD_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
737 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
738 vmem_alloc_used_read(), vmem_alloc_max
);
743 EXPORT_SYMBOL(vmem_alloc_debug
);
746 vmem_free_debug(void *ptr
, size_t size
)
750 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
751 (unsigned long long) size
);
753 vmem_alloc_used_sub(size
);
754 SDEBUG_LIMIT(SD_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
755 (unsigned long long) size
, vmem_alloc_used_read(),
761 EXPORT_SYMBOL(vmem_free_debug
);
763 # endif /* DEBUG_KMEM_TRACKING */
764 #endif /* DEBUG_KMEM */
767 * Slab allocation interfaces
769 * While the Linux slab implementation was inspired by the Solaris
770 * implemenation I cannot use it to emulate the Solaris APIs. I
771 * require two features which are not provided by the Linux slab.
773 * 1) Constructors AND destructors. Recent versions of the Linux
774 * kernel have removed support for destructors. This is a deal
775 * breaker for the SPL which contains particularly expensive
776 * initializers for mutex's, condition variables, etc. We also
777 * require a minimal level of cleanup for these data types unlike
778 * many Linux data type which do need to be explicitly destroyed.
780 * 2) Virtual address space backed slab. Callers of the Solaris slab
781 * expect it to work well for both small are very large allocations.
782 * Because of memory fragmentation the Linux slab which is backed
783 * by kmalloc'ed memory performs very badly when confronted with
784 * large numbers of large allocations. Basing the slab on the
785 * virtual address space removes the need for contigeous pages
786 * and greatly improve performance for large allocations.
788 * For these reasons, the SPL has its own slab implementation with
789 * the needed features. It is not as highly optimized as either the
790 * Solaris or Linux slabs, but it should get me most of what is
791 * needed until it can be optimized or obsoleted by another approach.
793 * One serious concern I do have about this method is the relatively
794 * small virtual address space on 32bit arches. This will seriously
795 * constrain the size of the slab caches and their performance.
797 * XXX: Improve the partial slab list by carefully maintaining a
798 * strict ordering of fullest to emptiest slabs based on
799 * the slab reference count. This gaurentees the when freeing
800 * slabs back to the system we need only linearly traverse the
801 * last N slabs in the list to discover all the freeable slabs.
803 * XXX: NUMA awareness for optionally allocating memory close to a
804 * particular core. This can be adventageous if you know the slab
805 * object will be short lived and primarily accessed from one core.
807 * XXX: Slab coloring may also yield performance improvements and would
808 * be desirable to implement.
811 struct list_head spl_kmem_cache_list
; /* List of caches */
812 struct rw_semaphore spl_kmem_cache_sem
; /* Cache list lock */
814 static int spl_cache_flush(spl_kmem_cache_t
*skc
,
815 spl_kmem_magazine_t
*skm
, int flush
);
817 #ifdef HAVE_SET_SHRINKER
818 static struct shrinker
*spl_kmem_cache_shrinker
;
820 # ifdef HAVE_3ARGS_SHRINKER_CALLBACK
821 static int spl_kmem_cache_generic_shrinker(struct shrinker
*shrinker_cb
,
822 int nr_to_scan
, unsigned int gfp_mask
);
824 static int spl_kmem_cache_generic_shrinker(
825 int nr_to_scan
, unsigned int gfp_mask
);
826 # endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
827 static struct shrinker spl_kmem_cache_shrinker
= {
828 .shrink
= spl_kmem_cache_generic_shrinker
,
829 .seeks
= KMC_DEFAULT_SEEKS
,
831 #endif /* HAVE_SET_SHRINKER */
834 kv_alloc(spl_kmem_cache_t
*skc
, int size
, int flags
)
840 if (skc
->skc_flags
& KMC_KMEM
)
841 ptr
= (void *)__get_free_pages(flags
, get_order(size
));
843 ptr
= __vmalloc(size
, flags
| __GFP_HIGHMEM
, PAGE_KERNEL
);
845 /* Resulting allocated memory will be page aligned */
846 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
852 kv_free(spl_kmem_cache_t
*skc
, void *ptr
, int size
)
854 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
857 if (skc
->skc_flags
& KMC_KMEM
)
858 free_pages((unsigned long)ptr
, get_order(size
));
864 * Required space for each aligned sks.
866 static inline uint32_t
867 spl_sks_size(spl_kmem_cache_t
*skc
)
869 return P2ROUNDUP_TYPED(sizeof(spl_kmem_slab_t
),
870 skc
->skc_obj_align
, uint32_t);
874 * Required space for each aligned object.
876 static inline uint32_t
877 spl_obj_size(spl_kmem_cache_t
*skc
)
879 uint32_t align
= skc
->skc_obj_align
;
881 return P2ROUNDUP_TYPED(skc
->skc_obj_size
, align
, uint32_t) +
882 P2ROUNDUP_TYPED(sizeof(spl_kmem_obj_t
), align
, uint32_t);
886 * Lookup the spl_kmem_object_t for an object given that object.
888 static inline spl_kmem_obj_t
*
889 spl_sko_from_obj(spl_kmem_cache_t
*skc
, void *obj
)
891 return obj
+ P2ROUNDUP_TYPED(skc
->skc_obj_size
,
892 skc
->skc_obj_align
, uint32_t);
896 * Required space for each offslab object taking in to account alignment
897 * restrictions and the power-of-two requirement of kv_alloc().
899 static inline uint32_t
900 spl_offslab_size(spl_kmem_cache_t
*skc
)
902 return 1UL << (highbit(spl_obj_size(skc
)) + 1);
906 * It's important that we pack the spl_kmem_obj_t structure and the
907 * actual objects in to one large address space to minimize the number
908 * of calls to the allocator. It is far better to do a few large
909 * allocations and then subdivide it ourselves. Now which allocator
910 * we use requires balancing a few trade offs.
912 * For small objects we use kmem_alloc() because as long as you are
913 * only requesting a small number of pages (ideally just one) its cheap.
914 * However, when you start requesting multiple pages with kmem_alloc()
915 * it gets increasingly expensive since it requires contigeous pages.
916 * For this reason we shift to vmem_alloc() for slabs of large objects
917 * which removes the need for contigeous pages. We do not use
918 * vmem_alloc() in all cases because there is significant locking
919 * overhead in __get_vm_area_node(). This function takes a single
920 * global lock when aquiring an available virtual address range which
921 * serializes all vmem_alloc()'s for all slab caches. Using slightly
922 * different allocation functions for small and large objects should
923 * give us the best of both worlds.
925 * KMC_ONSLAB KMC_OFFSLAB
927 * +------------------------+ +-----------------+
928 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
929 * | skc_obj_size <-+ | | +-----------------+ | |
930 * | spl_kmem_obj_t | | | |
931 * | skc_obj_size <---+ | +-----------------+ | |
932 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
933 * | ... v | | spl_kmem_obj_t | |
934 * +------------------------+ +-----------------+ v
936 static spl_kmem_slab_t
*
937 spl_slab_alloc(spl_kmem_cache_t
*skc
, int flags
)
939 spl_kmem_slab_t
*sks
;
940 spl_kmem_obj_t
*sko
, *n
;
942 uint32_t obj_size
, offslab_size
= 0;
945 base
= kv_alloc(skc
, skc
->skc_slab_size
, flags
);
949 sks
= (spl_kmem_slab_t
*)base
;
950 sks
->sks_magic
= SKS_MAGIC
;
951 sks
->sks_objs
= skc
->skc_slab_objs
;
952 sks
->sks_age
= jiffies
;
953 sks
->sks_cache
= skc
;
954 INIT_LIST_HEAD(&sks
->sks_list
);
955 INIT_LIST_HEAD(&sks
->sks_free_list
);
957 obj_size
= spl_obj_size(skc
);
959 if (skc
->skc_flags
* KMC_OFFSLAB
)
960 offslab_size
= spl_offslab_size(skc
);
962 for (i
= 0; i
< sks
->sks_objs
; i
++) {
963 if (skc
->skc_flags
& KMC_OFFSLAB
) {
964 obj
= kv_alloc(skc
, offslab_size
, flags
);
966 SGOTO(out
, rc
= -ENOMEM
);
968 obj
= base
+ spl_sks_size(skc
) + (i
* obj_size
);
971 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
972 sko
= spl_sko_from_obj(skc
, obj
);
974 sko
->sko_magic
= SKO_MAGIC
;
976 INIT_LIST_HEAD(&sko
->sko_list
);
977 list_add_tail(&sko
->sko_list
, &sks
->sks_free_list
);
980 list_for_each_entry(sko
, &sks
->sks_free_list
, sko_list
)
982 skc
->skc_ctor(sko
->sko_addr
, skc
->skc_private
, flags
);
985 if (skc
->skc_flags
& KMC_OFFSLAB
)
986 list_for_each_entry_safe(sko
, n
, &sks
->sks_free_list
,
988 kv_free(skc
, sko
->sko_addr
, offslab_size
);
990 kv_free(skc
, base
, skc
->skc_slab_size
);
998 * Remove a slab from complete or partial list, it must be called with
999 * the 'skc->skc_lock' held but the actual free must be performed
1000 * outside the lock to prevent deadlocking on vmem addresses.
1003 spl_slab_free(spl_kmem_slab_t
*sks
,
1004 struct list_head
*sks_list
, struct list_head
*sko_list
)
1006 spl_kmem_cache_t
*skc
;
1009 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1010 ASSERT(sks
->sks_ref
== 0);
1012 skc
= sks
->sks_cache
;
1013 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1014 ASSERT(spin_is_locked(&skc
->skc_lock
));
1017 * Update slab/objects counters in the cache, then remove the
1018 * slab from the skc->skc_partial_list. Finally add the slab
1019 * and all its objects in to the private work lists where the
1020 * destructors will be called and the memory freed to the system.
1022 skc
->skc_obj_total
-= sks
->sks_objs
;
1023 skc
->skc_slab_total
--;
1024 list_del(&sks
->sks_list
);
1025 list_add(&sks
->sks_list
, sks_list
);
1026 list_splice_init(&sks
->sks_free_list
, sko_list
);
1032 * Traverses all the partial slabs attached to a cache and free those
1033 * which which are currently empty, and have not been touched for
1034 * skc_delay seconds to avoid thrashing. The count argument is
1035 * passed to optionally cap the number of slabs reclaimed, a count
1036 * of zero means try and reclaim everything. When flag is set we
1037 * always free an available slab regardless of age.
1040 spl_slab_reclaim(spl_kmem_cache_t
*skc
, int count
, int flag
)
1042 spl_kmem_slab_t
*sks
, *m
;
1043 spl_kmem_obj_t
*sko
, *n
;
1044 LIST_HEAD(sks_list
);
1045 LIST_HEAD(sko_list
);
1051 * Move empty slabs and objects which have not been touched in
1052 * skc_delay seconds on to private lists to be freed outside
1053 * the spin lock. This delay time is important to avoid thrashing
1054 * however when flag is set the delay will not be used.
1056 spin_lock(&skc
->skc_lock
);
1057 list_for_each_entry_safe_reverse(sks
,m
,&skc
->skc_partial_list
,sks_list
){
1059 * All empty slabs are at the end of skc->skc_partial_list,
1060 * therefore once a non-empty slab is found we can stop
1061 * scanning. Additionally, stop when reaching the target
1062 * reclaim 'count' if a non-zero threshhold is given.
1064 if ((sks
->sks_ref
> 0) || (count
&& i
> count
))
1067 if (time_after(jiffies
,sks
->sks_age
+skc
->skc_delay
*HZ
)||flag
) {
1068 spl_slab_free(sks
, &sks_list
, &sko_list
);
1072 spin_unlock(&skc
->skc_lock
);
1075 * The following two loops ensure all the object destructors are
1076 * run, any offslab objects are freed, and the slabs themselves
1077 * are freed. This is all done outside the skc->skc_lock since
1078 * this allows the destructor to sleep, and allows us to perform
1079 * a conditional reschedule when a freeing a large number of
1080 * objects and slabs back to the system.
1082 if (skc
->skc_flags
& KMC_OFFSLAB
)
1083 size
= spl_offslab_size(skc
);
1085 list_for_each_entry_safe(sko
, n
, &sko_list
, sko_list
) {
1086 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1089 skc
->skc_dtor(sko
->sko_addr
, skc
->skc_private
);
1091 if (skc
->skc_flags
& KMC_OFFSLAB
)
1092 kv_free(skc
, sko
->sko_addr
, size
);
1097 list_for_each_entry_safe(sks
, m
, &sks_list
, sks_list
) {
1098 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1099 kv_free(skc
, sks
, skc
->skc_slab_size
);
1107 * Called regularly on all caches to age objects out of the magazines
1108 * which have not been access in skc->skc_delay seconds. This prevents
1109 * idle magazines from holding memory which might be better used by
1110 * other caches or parts of the system. The delay is present to
1111 * prevent thrashing the magazine.
1114 spl_magazine_age(void *data
)
1116 spl_kmem_magazine_t
*skm
=
1117 spl_get_work_data(data
, spl_kmem_magazine_t
, skm_work
.work
);
1118 spl_kmem_cache_t
*skc
= skm
->skm_cache
;
1119 int i
= smp_processor_id();
1121 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1122 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1123 ASSERT(skc
->skc_mag
[i
] == skm
);
1125 if (skm
->skm_avail
> 0 &&
1126 time_after(jiffies
, skm
->skm_age
+ skc
->skc_delay
* HZ
))
1127 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1129 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
1130 schedule_delayed_work_on(i
, &skm
->skm_work
,
1131 skc
->skc_delay
/ 3 * HZ
);
1135 * Called regularly to keep a downward pressure on the size of idle
1136 * magazines and to release free slabs from the cache. This function
1137 * never calls the registered reclaim function, that only occures
1138 * under memory pressure or with a direct call to spl_kmem_reap().
1141 spl_cache_age(void *data
)
1143 spl_kmem_cache_t
*skc
=
1144 spl_get_work_data(data
, spl_kmem_cache_t
, skc_work
.work
);
1146 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1147 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
1149 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
1150 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
1154 * Size a slab based on the size of each aligned object plus spl_kmem_obj_t.
1155 * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However,
1156 * for very small objects we may end up with more than this so as not
1157 * to waste space in the minimal allocation of a single page. Also for
1158 * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN,
1159 * lower than this and we will fail.
1162 spl_slab_size(spl_kmem_cache_t
*skc
, uint32_t *objs
, uint32_t *size
)
1164 uint32_t sks_size
, obj_size
, max_size
;
1166 if (skc
->skc_flags
& KMC_OFFSLAB
) {
1167 *objs
= SPL_KMEM_CACHE_OBJ_PER_SLAB
;
1168 *size
= sizeof(spl_kmem_slab_t
);
1170 sks_size
= spl_sks_size(skc
);
1171 obj_size
= spl_obj_size(skc
);
1173 if (skc
->skc_flags
& KMC_KMEM
)
1174 max_size
= ((uint32_t)1 << (MAX_ORDER
-3)) * PAGE_SIZE
;
1176 max_size
= (32 * 1024 * 1024);
1178 /* Power of two sized slab */
1179 for (*size
= PAGE_SIZE
; *size
<= max_size
; *size
*= 2) {
1180 *objs
= (*size
- sks_size
) / obj_size
;
1181 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB
)
1186 * Unable to satisfy target objects per slab, fall back to
1187 * allocating a maximally sized slab and assuming it can
1188 * contain the minimum objects count use it. If not fail.
1191 *objs
= (*size
- sks_size
) / obj_size
;
1192 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN
)
1200 * Make a guess at reasonable per-cpu magazine size based on the size of
1201 * each object and the cost of caching N of them in each magazine. Long
1202 * term this should really adapt based on an observed usage heuristic.
1205 spl_magazine_size(spl_kmem_cache_t
*skc
)
1207 uint32_t obj_size
= spl_obj_size(skc
);
1211 /* Per-magazine sizes below assume a 4Kib page size */
1212 if (obj_size
> (PAGE_SIZE
* 256))
1213 size
= 4; /* Minimum 4Mib per-magazine */
1214 else if (obj_size
> (PAGE_SIZE
* 32))
1215 size
= 16; /* Minimum 2Mib per-magazine */
1216 else if (obj_size
> (PAGE_SIZE
))
1217 size
= 64; /* Minimum 256Kib per-magazine */
1218 else if (obj_size
> (PAGE_SIZE
/ 4))
1219 size
= 128; /* Minimum 128Kib per-magazine */
1227 * Allocate a per-cpu magazine to assoicate with a specific core.
1229 static spl_kmem_magazine_t
*
1230 spl_magazine_alloc(spl_kmem_cache_t
*skc
, int node
)
1232 spl_kmem_magazine_t
*skm
;
1233 int size
= sizeof(spl_kmem_magazine_t
) +
1234 sizeof(void *) * skc
->skc_mag_size
;
1237 skm
= kmem_alloc_node(size
, KM_SLEEP
, node
);
1239 skm
->skm_magic
= SKM_MAGIC
;
1241 skm
->skm_size
= skc
->skc_mag_size
;
1242 skm
->skm_refill
= skc
->skc_mag_refill
;
1243 skm
->skm_cache
= skc
;
1244 spl_init_delayed_work(&skm
->skm_work
, spl_magazine_age
, skm
);
1245 skm
->skm_age
= jiffies
;
1252 * Free a per-cpu magazine assoicated with a specific core.
1255 spl_magazine_free(spl_kmem_magazine_t
*skm
)
1257 int size
= sizeof(spl_kmem_magazine_t
) +
1258 sizeof(void *) * skm
->skm_size
;
1261 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1262 ASSERT(skm
->skm_avail
== 0);
1264 kmem_free(skm
, size
);
1269 * Create all pre-cpu magazines of reasonable sizes.
1272 spl_magazine_create(spl_kmem_cache_t
*skc
)
1277 skc
->skc_mag_size
= spl_magazine_size(skc
);
1278 skc
->skc_mag_refill
= (skc
->skc_mag_size
+ 1) / 2;
1280 for_each_online_cpu(i
) {
1281 skc
->skc_mag
[i
] = spl_magazine_alloc(skc
, cpu_to_node(i
));
1282 if (!skc
->skc_mag
[i
]) {
1283 for (i
--; i
>= 0; i
--)
1284 spl_magazine_free(skc
->skc_mag
[i
]);
1290 /* Only after everything is allocated schedule magazine work */
1291 for_each_online_cpu(i
)
1292 schedule_delayed_work_on(i
, &skc
->skc_mag
[i
]->skm_work
,
1293 skc
->skc_delay
/ 3 * HZ
);
1299 * Destroy all pre-cpu magazines.
1302 spl_magazine_destroy(spl_kmem_cache_t
*skc
)
1304 spl_kmem_magazine_t
*skm
;
1308 for_each_online_cpu(i
) {
1309 skm
= skc
->skc_mag
[i
];
1310 (void)spl_cache_flush(skc
, skm
, skm
->skm_avail
);
1311 spl_magazine_free(skm
);
1318 * Create a object cache based on the following arguments:
1320 * size cache object size
1321 * align cache object alignment
1322 * ctor cache object constructor
1323 * dtor cache object destructor
1324 * reclaim cache object reclaim
1325 * priv cache private data for ctor/dtor/reclaim
1326 * vmp unused must be NULL
1328 * KMC_NOTOUCH Disable cache object aging (unsupported)
1329 * KMC_NODEBUG Disable debugging (unsupported)
1330 * KMC_NOMAGAZINE Disable magazine (unsupported)
1331 * KMC_NOHASH Disable hashing (unsupported)
1332 * KMC_QCACHE Disable qcache (unsupported)
1333 * KMC_KMEM Force kmem backed cache
1334 * KMC_VMEM Force vmem backed cache
1335 * KMC_OFFSLAB Locate objects off the slab
1338 spl_kmem_cache_create(char *name
, size_t size
, size_t align
,
1339 spl_kmem_ctor_t ctor
,
1340 spl_kmem_dtor_t dtor
,
1341 spl_kmem_reclaim_t reclaim
,
1342 void *priv
, void *vmp
, int flags
)
1344 spl_kmem_cache_t
*skc
;
1345 int rc
, kmem_flags
= KM_SLEEP
;
1348 ASSERTF(!(flags
& KMC_NOMAGAZINE
), "Bad KMC_NOMAGAZINE (%x)\n", flags
);
1349 ASSERTF(!(flags
& KMC_NOHASH
), "Bad KMC_NOHASH (%x)\n", flags
);
1350 ASSERTF(!(flags
& KMC_QCACHE
), "Bad KMC_QCACHE (%x)\n", flags
);
1351 ASSERT(vmp
== NULL
);
1353 /* We may be called when there is a non-zero preempt_count or
1354 * interrupts are disabled is which case we must not sleep.
1356 if (current_thread_info()->preempt_count
|| irqs_disabled())
1357 kmem_flags
= KM_NOSLEEP
;
1359 /* Allocate memry for a new cache an initialize it. Unfortunately,
1360 * this usually ends up being a large allocation of ~32k because
1361 * we need to allocate enough memory for the worst case number of
1362 * cpus in the magazine, skc_mag[NR_CPUS]. Because of this we
1363 * explicitly pass KM_NODEBUG to suppress the kmem warning */
1364 skc
= (spl_kmem_cache_t
*)kmem_zalloc(sizeof(*skc
),
1365 kmem_flags
| KM_NODEBUG
);
1369 skc
->skc_magic
= SKC_MAGIC
;
1370 skc
->skc_name_size
= strlen(name
) + 1;
1371 skc
->skc_name
= (char *)kmem_alloc(skc
->skc_name_size
, kmem_flags
);
1372 if (skc
->skc_name
== NULL
) {
1373 kmem_free(skc
, sizeof(*skc
));
1376 strncpy(skc
->skc_name
, name
, skc
->skc_name_size
);
1378 skc
->skc_ctor
= ctor
;
1379 skc
->skc_dtor
= dtor
;
1380 skc
->skc_reclaim
= reclaim
;
1381 skc
->skc_private
= priv
;
1383 skc
->skc_flags
= flags
;
1384 skc
->skc_obj_size
= size
;
1385 skc
->skc_obj_align
= SPL_KMEM_CACHE_ALIGN
;
1386 skc
->skc_delay
= SPL_KMEM_CACHE_DELAY
;
1387 skc
->skc_reap
= SPL_KMEM_CACHE_REAP
;
1388 atomic_set(&skc
->skc_ref
, 0);
1390 INIT_LIST_HEAD(&skc
->skc_list
);
1391 INIT_LIST_HEAD(&skc
->skc_complete_list
);
1392 INIT_LIST_HEAD(&skc
->skc_partial_list
);
1393 spin_lock_init(&skc
->skc_lock
);
1394 skc
->skc_slab_fail
= 0;
1395 skc
->skc_slab_create
= 0;
1396 skc
->skc_slab_destroy
= 0;
1397 skc
->skc_slab_total
= 0;
1398 skc
->skc_slab_alloc
= 0;
1399 skc
->skc_slab_max
= 0;
1400 skc
->skc_obj_total
= 0;
1401 skc
->skc_obj_alloc
= 0;
1402 skc
->skc_obj_max
= 0;
1405 VERIFY(ISP2(align
));
1406 VERIFY3U(align
, >=, SPL_KMEM_CACHE_ALIGN
); /* Min alignment */
1407 VERIFY3U(align
, <=, PAGE_SIZE
); /* Max alignment */
1408 skc
->skc_obj_align
= align
;
1411 /* If none passed select a cache type based on object size */
1412 if (!(skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
))) {
1413 if (spl_obj_size(skc
) < (PAGE_SIZE
/ 8))
1414 skc
->skc_flags
|= KMC_KMEM
;
1416 skc
->skc_flags
|= KMC_VMEM
;
1419 rc
= spl_slab_size(skc
, &skc
->skc_slab_objs
, &skc
->skc_slab_size
);
1423 rc
= spl_magazine_create(skc
);
1427 spl_init_delayed_work(&skc
->skc_work
, spl_cache_age
, skc
);
1428 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
1430 down_write(&spl_kmem_cache_sem
);
1431 list_add_tail(&skc
->skc_list
, &spl_kmem_cache_list
);
1432 up_write(&spl_kmem_cache_sem
);
1436 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1437 kmem_free(skc
, sizeof(*skc
));
1440 EXPORT_SYMBOL(spl_kmem_cache_create
);
1443 * Register a move callback to for cache defragmentation.
1444 * XXX: Unimplemented but harmless to stub out for now.
1447 spl_kmem_cache_set_move(kmem_cache_t
*skc
,
1448 kmem_cbrc_t (move
)(void *, void *, size_t, void *))
1450 ASSERT(move
!= NULL
);
1452 EXPORT_SYMBOL(spl_kmem_cache_set_move
);
1455 * Destroy a cache and all objects assoicated with the cache.
1458 spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
)
1460 DECLARE_WAIT_QUEUE_HEAD(wq
);
1464 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1466 down_write(&spl_kmem_cache_sem
);
1467 list_del_init(&skc
->skc_list
);
1468 up_write(&spl_kmem_cache_sem
);
1470 /* Cancel any and wait for any pending delayed work */
1471 ASSERT(!test_and_set_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1472 cancel_delayed_work(&skc
->skc_work
);
1473 for_each_online_cpu(i
)
1474 cancel_delayed_work(&skc
->skc_mag
[i
]->skm_work
);
1476 flush_scheduled_work();
1478 /* Wait until all current callers complete, this is mainly
1479 * to catch the case where a low memory situation triggers a
1480 * cache reaping action which races with this destroy. */
1481 wait_event(wq
, atomic_read(&skc
->skc_ref
) == 0);
1483 spl_magazine_destroy(skc
);
1484 spl_slab_reclaim(skc
, 0, 1);
1485 spin_lock(&skc
->skc_lock
);
1487 /* Validate there are no objects in use and free all the
1488 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
1489 ASSERT3U(skc
->skc_slab_alloc
, ==, 0);
1490 ASSERT3U(skc
->skc_obj_alloc
, ==, 0);
1491 ASSERT3U(skc
->skc_slab_total
, ==, 0);
1492 ASSERT3U(skc
->skc_obj_total
, ==, 0);
1493 ASSERT(list_empty(&skc
->skc_complete_list
));
1495 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1496 spin_unlock(&skc
->skc_lock
);
1498 kmem_free(skc
, sizeof(*skc
));
1502 EXPORT_SYMBOL(spl_kmem_cache_destroy
);
1505 * Allocate an object from a slab attached to the cache. This is used to
1506 * repopulate the per-cpu magazine caches in batches when they run low.
1509 spl_cache_obj(spl_kmem_cache_t
*skc
, spl_kmem_slab_t
*sks
)
1511 spl_kmem_obj_t
*sko
;
1513 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1514 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1515 ASSERT(spin_is_locked(&skc
->skc_lock
));
1517 sko
= list_entry(sks
->sks_free_list
.next
, spl_kmem_obj_t
, sko_list
);
1518 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1519 ASSERT(sko
->sko_addr
!= NULL
);
1521 /* Remove from sks_free_list */
1522 list_del_init(&sko
->sko_list
);
1524 sks
->sks_age
= jiffies
;
1526 skc
->skc_obj_alloc
++;
1528 /* Track max obj usage statistics */
1529 if (skc
->skc_obj_alloc
> skc
->skc_obj_max
)
1530 skc
->skc_obj_max
= skc
->skc_obj_alloc
;
1532 /* Track max slab usage statistics */
1533 if (sks
->sks_ref
== 1) {
1534 skc
->skc_slab_alloc
++;
1536 if (skc
->skc_slab_alloc
> skc
->skc_slab_max
)
1537 skc
->skc_slab_max
= skc
->skc_slab_alloc
;
1540 return sko
->sko_addr
;
1544 * No available objects on any slabsi, create a new slab. Since this
1545 * is an expensive operation we do it without holding the spinlock and
1546 * only briefly aquire it when we link in the fully allocated and
1549 static spl_kmem_slab_t
*
1550 spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
)
1552 spl_kmem_slab_t
*sks
;
1555 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1560 * Before allocating a new slab check if the slab is being reaped.
1561 * If it is there is a good chance we can wait until it finishes
1562 * and then use one of the newly freed but not aged-out slabs.
1564 if (test_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1566 SGOTO(out
, sks
= NULL
);
1569 /* Allocate a new slab for the cache */
1570 sks
= spl_slab_alloc(skc
, flags
| __GFP_NORETRY
| KM_NODEBUG
);
1572 SGOTO(out
, sks
= NULL
);
1574 /* Link the new empty slab in to the end of skc_partial_list. */
1575 spin_lock(&skc
->skc_lock
);
1576 skc
->skc_slab_total
++;
1577 skc
->skc_obj_total
+= sks
->sks_objs
;
1578 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1579 spin_unlock(&skc
->skc_lock
);
1581 local_irq_disable();
1587 * Refill a per-cpu magazine with objects from the slabs for this
1588 * cache. Ideally the magazine can be repopulated using existing
1589 * objects which have been released, however if we are unable to
1590 * locate enough free objects new slabs of objects will be created.
1593 spl_cache_refill(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flags
)
1595 spl_kmem_slab_t
*sks
;
1599 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1600 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1602 refill
= MIN(skm
->skm_refill
, skm
->skm_size
- skm
->skm_avail
);
1603 spin_lock(&skc
->skc_lock
);
1605 while (refill
> 0) {
1606 /* No slabs available we may need to grow the cache */
1607 if (list_empty(&skc
->skc_partial_list
)) {
1608 spin_unlock(&skc
->skc_lock
);
1610 sks
= spl_cache_grow(skc
, flags
);
1614 /* Rescheduled to different CPU skm is not local */
1615 if (skm
!= skc
->skc_mag
[smp_processor_id()])
1618 /* Potentially rescheduled to the same CPU but
1619 * allocations may have occured from this CPU while
1620 * we were sleeping so recalculate max refill. */
1621 refill
= MIN(refill
, skm
->skm_size
- skm
->skm_avail
);
1623 spin_lock(&skc
->skc_lock
);
1627 /* Grab the next available slab */
1628 sks
= list_entry((&skc
->skc_partial_list
)->next
,
1629 spl_kmem_slab_t
, sks_list
);
1630 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1631 ASSERT(sks
->sks_ref
< sks
->sks_objs
);
1632 ASSERT(!list_empty(&sks
->sks_free_list
));
1634 /* Consume as many objects as needed to refill the requested
1635 * cache. We must also be careful not to overfill it. */
1636 while (sks
->sks_ref
< sks
->sks_objs
&& refill
-- > 0 && ++rc
) {
1637 ASSERT(skm
->skm_avail
< skm
->skm_size
);
1638 ASSERT(rc
< skm
->skm_size
);
1639 skm
->skm_objs
[skm
->skm_avail
++]=spl_cache_obj(skc
,sks
);
1642 /* Move slab to skc_complete_list when full */
1643 if (sks
->sks_ref
== sks
->sks_objs
) {
1644 list_del(&sks
->sks_list
);
1645 list_add(&sks
->sks_list
, &skc
->skc_complete_list
);
1649 spin_unlock(&skc
->skc_lock
);
1651 /* Returns the number of entries added to cache */
1656 * Release an object back to the slab from which it came.
1659 spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
)
1661 spl_kmem_slab_t
*sks
= NULL
;
1662 spl_kmem_obj_t
*sko
= NULL
;
1665 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1666 ASSERT(spin_is_locked(&skc
->skc_lock
));
1668 sko
= spl_sko_from_obj(skc
, obj
);
1669 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1670 sks
= sko
->sko_slab
;
1671 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1672 ASSERT(sks
->sks_cache
== skc
);
1673 list_add(&sko
->sko_list
, &sks
->sks_free_list
);
1675 sks
->sks_age
= jiffies
;
1677 skc
->skc_obj_alloc
--;
1679 /* Move slab to skc_partial_list when no longer full. Slabs
1680 * are added to the head to keep the partial list is quasi-full
1681 * sorted order. Fuller at the head, emptier at the tail. */
1682 if (sks
->sks_ref
== (sks
->sks_objs
- 1)) {
1683 list_del(&sks
->sks_list
);
1684 list_add(&sks
->sks_list
, &skc
->skc_partial_list
);
1687 /* Move emply slabs to the end of the partial list so
1688 * they can be easily found and freed during reclamation. */
1689 if (sks
->sks_ref
== 0) {
1690 list_del(&sks
->sks_list
);
1691 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1692 skc
->skc_slab_alloc
--;
1699 * Release a batch of objects from a per-cpu magazine back to their
1700 * respective slabs. This occurs when we exceed the magazine size,
1701 * are under memory pressure, when the cache is idle, or during
1702 * cache cleanup. The flush argument contains the number of entries
1703 * to remove from the magazine.
1706 spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
1708 int i
, count
= MIN(flush
, skm
->skm_avail
);
1711 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1712 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1715 * XXX: Currently we simply return objects from the magazine to
1716 * the slabs in fifo order. The ideal thing to do from a memory
1717 * fragmentation standpoint is to cheaply determine the set of
1718 * objects in the magazine which will result in the largest
1719 * number of free slabs if released from the magazine.
1721 spin_lock(&skc
->skc_lock
);
1722 for (i
= 0; i
< count
; i
++)
1723 spl_cache_shrink(skc
, skm
->skm_objs
[i
]);
1725 skm
->skm_avail
-= count
;
1726 memmove(skm
->skm_objs
, &(skm
->skm_objs
[count
]),
1727 sizeof(void *) * skm
->skm_avail
);
1729 spin_unlock(&skc
->skc_lock
);
1735 * Allocate an object from the per-cpu magazine, or if the magazine
1736 * is empty directly allocate from a slab and repopulate the magazine.
1739 spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
)
1741 spl_kmem_magazine_t
*skm
;
1742 unsigned long irq_flags
;
1746 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1747 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1748 ASSERT(flags
& KM_SLEEP
);
1749 atomic_inc(&skc
->skc_ref
);
1750 local_irq_save(irq_flags
);
1753 /* Safe to update per-cpu structure without lock, but
1754 * in the restart case we must be careful to reaquire
1755 * the local magazine since this may have changed
1756 * when we need to grow the cache. */
1757 skm
= skc
->skc_mag
[smp_processor_id()];
1758 ASSERTF(skm
->skm_magic
== SKM_MAGIC
, "%x != %x: %s/%p/%p %x/%x/%x\n",
1759 skm
->skm_magic
, SKM_MAGIC
, skc
->skc_name
, skc
, skm
,
1760 skm
->skm_size
, skm
->skm_refill
, skm
->skm_avail
);
1762 if (likely(skm
->skm_avail
)) {
1763 /* Object available in CPU cache, use it */
1764 obj
= skm
->skm_objs
[--skm
->skm_avail
];
1765 skm
->skm_age
= jiffies
;
1767 /* Per-CPU cache empty, directly allocate from
1768 * the slab and refill the per-CPU cache. */
1769 (void)spl_cache_refill(skc
, skm
, flags
);
1770 SGOTO(restart
, obj
= NULL
);
1773 local_irq_restore(irq_flags
);
1775 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
1777 /* Pre-emptively migrate object to CPU L1 cache */
1779 atomic_dec(&skc
->skc_ref
);
1783 EXPORT_SYMBOL(spl_kmem_cache_alloc
);
1786 * Free an object back to the local per-cpu magazine, there is no
1787 * guarantee that this is the same magazine the object was originally
1788 * allocated from. We may need to flush entire from the magazine
1789 * back to the slabs to make space.
1792 spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
)
1794 spl_kmem_magazine_t
*skm
;
1795 unsigned long flags
;
1798 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1799 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1800 atomic_inc(&skc
->skc_ref
);
1801 local_irq_save(flags
);
1803 /* Safe to update per-cpu structure without lock, but
1804 * no remote memory allocation tracking is being performed
1805 * it is entirely possible to allocate an object from one
1806 * CPU cache and return it to another. */
1807 skm
= skc
->skc_mag
[smp_processor_id()];
1808 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1810 /* Per-CPU cache full, flush it to make space */
1811 if (unlikely(skm
->skm_avail
>= skm
->skm_size
))
1812 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1814 /* Available space in cache, use it */
1815 skm
->skm_objs
[skm
->skm_avail
++] = obj
;
1817 local_irq_restore(flags
);
1818 atomic_dec(&skc
->skc_ref
);
1822 EXPORT_SYMBOL(spl_kmem_cache_free
);
1825 * The generic shrinker function for all caches. Under linux a shrinker
1826 * may not be tightly coupled with a slab cache. In fact linux always
1827 * systematically trys calling all registered shrinker callbacks which
1828 * report that they contain unused objects. Because of this we only
1829 * register one shrinker function in the shim layer for all slab caches.
1830 * We always attempt to shrink all caches when this generic shrinker
1831 * is called. The shrinker should return the number of free objects
1832 * in the cache when called with nr_to_scan == 0 but not attempt to
1833 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
1834 * objects should be freed, because Solaris semantics are to free
1835 * all available objects we may free more objects than requested.
1837 #ifdef HAVE_3ARGS_SHRINKER_CALLBACK
1839 spl_kmem_cache_generic_shrinker(struct shrinker
*shrinker_cb
,
1840 int nr_to_scan
, unsigned int gfp_mask
)
1843 spl_kmem_cache_generic_shrinker(int nr_to_scan
, unsigned int gfp_mask
)
1844 #endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
1846 spl_kmem_cache_t
*skc
;
1849 down_read(&spl_kmem_cache_sem
);
1850 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
1852 spl_kmem_cache_reap_now(skc
);
1855 * Presume everything alloc'ed in reclaimable, this ensures
1856 * we are called again with nr_to_scan > 0 so can try and
1857 * reclaim. The exact number is not important either so
1858 * we forgo taking this already highly contented lock.
1860 unused
+= skc
->skc_obj_alloc
;
1862 up_read(&spl_kmem_cache_sem
);
1864 return (unused
* sysctl_vfs_cache_pressure
) / 100;
1868 * Call the registered reclaim function for a cache. Depending on how
1869 * many and which objects are released it may simply repopulate the
1870 * local magazine which will then need to age-out. Objects which cannot
1871 * fit in the magazine we will be released back to their slabs which will
1872 * also need to age out before being release. This is all just best
1873 * effort and we do not want to thrash creating and destroying slabs.
1876 spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
)
1880 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1881 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1883 /* Prevent concurrent cache reaping when contended */
1884 if (test_and_set_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1889 atomic_inc(&skc
->skc_ref
);
1891 if (skc
->skc_reclaim
)
1892 skc
->skc_reclaim(skc
->skc_private
);
1894 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
1895 clear_bit(KMC_BIT_REAPING
, &skc
->skc_flags
);
1896 atomic_dec(&skc
->skc_ref
);
1900 EXPORT_SYMBOL(spl_kmem_cache_reap_now
);
1903 * Reap all free slabs from all registered caches.
1908 #ifdef HAVE_3ARGS_SHRINKER_CALLBACK
1909 spl_kmem_cache_generic_shrinker(NULL
, KMC_REAP_CHUNK
, GFP_KERNEL
);
1911 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK
, GFP_KERNEL
);
1912 #endif /* HAVE_3ARGS_SHRINKER_CALLBACK */
1914 EXPORT_SYMBOL(spl_kmem_reap
);
1916 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
1918 spl_sprintf_addr(kmem_debug_t
*kd
, char *str
, int len
, int min
)
1920 int size
= ((len
- 1) < kd
->kd_size
) ? (len
- 1) : kd
->kd_size
;
1923 ASSERT(str
!= NULL
&& len
>= 17);
1924 memset(str
, 0, len
);
1926 /* Check for a fully printable string, and while we are at
1927 * it place the printable characters in the passed buffer. */
1928 for (i
= 0; i
< size
; i
++) {
1929 str
[i
] = ((char *)(kd
->kd_addr
))[i
];
1930 if (isprint(str
[i
])) {
1933 /* Minimum number of printable characters found
1934 * to make it worthwhile to print this as ascii. */
1944 sprintf(str
, "%02x%02x%02x%02x%02x%02x%02x%02x",
1945 *((uint8_t *)kd
->kd_addr
),
1946 *((uint8_t *)kd
->kd_addr
+ 2),
1947 *((uint8_t *)kd
->kd_addr
+ 4),
1948 *((uint8_t *)kd
->kd_addr
+ 6),
1949 *((uint8_t *)kd
->kd_addr
+ 8),
1950 *((uint8_t *)kd
->kd_addr
+ 10),
1951 *((uint8_t *)kd
->kd_addr
+ 12),
1952 *((uint8_t *)kd
->kd_addr
+ 14));
1959 spl_kmem_init_tracking(struct list_head
*list
, spinlock_t
*lock
, int size
)
1964 spin_lock_init(lock
);
1965 INIT_LIST_HEAD(list
);
1967 for (i
= 0; i
< size
; i
++)
1968 INIT_HLIST_HEAD(&kmem_table
[i
]);
1974 spl_kmem_fini_tracking(struct list_head
*list
, spinlock_t
*lock
)
1976 unsigned long flags
;
1981 spin_lock_irqsave(lock
, flags
);
1982 if (!list_empty(list
))
1983 printk(KERN_WARNING
"%-16s %-5s %-16s %s:%s\n", "address",
1984 "size", "data", "func", "line");
1986 list_for_each_entry(kd
, list
, kd_list
)
1987 printk(KERN_WARNING
"%p %-5d %-16s %s:%d\n", kd
->kd_addr
,
1988 (int)kd
->kd_size
, spl_sprintf_addr(kd
, str
, 17, 8),
1989 kd
->kd_func
, kd
->kd_line
);
1991 spin_unlock_irqrestore(lock
, flags
);
1994 #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1995 #define spl_kmem_init_tracking(list, lock, size)
1996 #define spl_kmem_fini_tracking(list, lock)
1997 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
2000 spl_kmem_init_globals(void)
2004 /* For now all zones are includes, it may be wise to restrict
2005 * this to normal and highmem zones if we see problems. */
2006 for_each_zone(zone
) {
2008 if (!populated_zone(zone
))
2011 minfree
+= min_wmark_pages(zone
);
2012 desfree
+= low_wmark_pages(zone
);
2013 lotsfree
+= high_wmark_pages(zone
);
2016 /* Solaris default values */
2017 swapfs_minfree
= MAX(2*1024*1024 >> PAGE_SHIFT
, physmem
>> 3);
2018 swapfs_reserve
= MIN(4*1024*1024 >> PAGE_SHIFT
, physmem
>> 4);
2022 * Called at module init when it is safe to use spl_kallsyms_lookup_name()
2025 spl_kmem_init_kallsyms_lookup(void)
2027 #ifndef HAVE_GET_VMALLOC_INFO
2028 get_vmalloc_info_fn
= (get_vmalloc_info_t
)
2029 spl_kallsyms_lookup_name("get_vmalloc_info");
2030 if (!get_vmalloc_info_fn
) {
2031 printk(KERN_ERR
"Error: Unknown symbol get_vmalloc_info\n");
2034 #endif /* HAVE_GET_VMALLOC_INFO */
2036 #ifdef HAVE_PGDAT_HELPERS
2037 # ifndef HAVE_FIRST_ONLINE_PGDAT
2038 first_online_pgdat_fn
= (first_online_pgdat_t
)
2039 spl_kallsyms_lookup_name("first_online_pgdat");
2040 if (!first_online_pgdat_fn
) {
2041 printk(KERN_ERR
"Error: Unknown symbol first_online_pgdat\n");
2044 # endif /* HAVE_FIRST_ONLINE_PGDAT */
2046 # ifndef HAVE_NEXT_ONLINE_PGDAT
2047 next_online_pgdat_fn
= (next_online_pgdat_t
)
2048 spl_kallsyms_lookup_name("next_online_pgdat");
2049 if (!next_online_pgdat_fn
) {
2050 printk(KERN_ERR
"Error: Unknown symbol next_online_pgdat\n");
2053 # endif /* HAVE_NEXT_ONLINE_PGDAT */
2055 # ifndef HAVE_NEXT_ZONE
2056 next_zone_fn
= (next_zone_t
)
2057 spl_kallsyms_lookup_name("next_zone");
2058 if (!next_zone_fn
) {
2059 printk(KERN_ERR
"Error: Unknown symbol next_zone\n");
2062 # endif /* HAVE_NEXT_ZONE */
2064 #else /* HAVE_PGDAT_HELPERS */
2066 # ifndef HAVE_PGDAT_LIST
2067 pgdat_list_addr
= *(struct pglist_data
**)
2068 spl_kallsyms_lookup_name("pgdat_list");
2069 if (!pgdat_list_addr
) {
2070 printk(KERN_ERR
"Error: Unknown symbol pgdat_list\n");
2073 # endif /* HAVE_PGDAT_LIST */
2074 #endif /* HAVE_PGDAT_HELPERS */
2076 #if defined(NEED_GET_ZONE_COUNTS) && !defined(HAVE_GET_ZONE_COUNTS)
2077 get_zone_counts_fn
= (get_zone_counts_t
)
2078 spl_kallsyms_lookup_name("get_zone_counts");
2079 if (!get_zone_counts_fn
) {
2080 printk(KERN_ERR
"Error: Unknown symbol get_zone_counts\n");
2083 #endif /* NEED_GET_ZONE_COUNTS && !HAVE_GET_ZONE_COUNTS */
2086 * It is now safe to initialize the global tunings which rely on
2087 * the use of the for_each_zone() macro. This macro in turns
2088 * depends on the *_pgdat symbols which are now available.
2090 spl_kmem_init_globals();
2101 init_rwsem(&spl_kmem_cache_sem
);
2102 INIT_LIST_HEAD(&spl_kmem_cache_list
);
2104 #ifdef HAVE_SET_SHRINKER
2105 spl_kmem_cache_shrinker
= set_shrinker(KMC_DEFAULT_SEEKS
,
2106 spl_kmem_cache_generic_shrinker
);
2107 if (spl_kmem_cache_shrinker
== NULL
)
2108 SRETURN(rc
= -ENOMEM
);
2110 register_shrinker(&spl_kmem_cache_shrinker
);
2114 kmem_alloc_used_set(0);
2115 vmem_alloc_used_set(0);
2117 spl_kmem_init_tracking(&kmem_list
, &kmem_lock
, KMEM_TABLE_SIZE
);
2118 spl_kmem_init_tracking(&vmem_list
, &vmem_lock
, VMEM_TABLE_SIZE
);
2127 /* Display all unreclaimed memory addresses, including the
2128 * allocation size and the first few bytes of what's located
2129 * at that address to aid in debugging. Performance is not
2130 * a serious concern here since it is module unload time. */
2131 if (kmem_alloc_used_read() != 0)
2132 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
2133 "kmem leaked %ld/%ld bytes\n",
2134 kmem_alloc_used_read(), kmem_alloc_max
);
2137 if (vmem_alloc_used_read() != 0)
2138 SDEBUG_LIMIT(SD_CONSOLE
| SD_WARNING
,
2139 "vmem leaked %ld/%ld bytes\n",
2140 vmem_alloc_used_read(), vmem_alloc_max
);
2142 spl_kmem_fini_tracking(&kmem_list
, &kmem_lock
);
2143 spl_kmem_fini_tracking(&vmem_list
, &vmem_lock
);
2144 #endif /* DEBUG_KMEM */
2147 #ifdef HAVE_SET_SHRINKER
2148 remove_shrinker(spl_kmem_cache_shrinker
);
2150 unregister_shrinker(&spl_kmem_cache_shrinker
);