2 * This file is part of the SPL: Solaris Porting Layer.
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #ifdef DEBUG_SUBSYSTEM
30 # undef DEBUG_SUBSYSTEM
33 #define DEBUG_SUBSYSTEM S_KMEM
36 * The minimum amount of memory measured in pages to be free at all
37 * times on the system. This is similar to Linux's zone->pages_min
38 * multipled by the number of zones and is sized based on that.
41 EXPORT_SYMBOL(minfree
);
44 * The desired amount of memory measured in pages to be free at all
45 * times on the system. This is similar to Linux's zone->pages_low
46 * multipled by the number of zones and is sized based on that.
47 * Assuming all zones are being used roughly equally, when we drop
48 * below this threshold async page reclamation is triggered.
51 EXPORT_SYMBOL(desfree
);
54 * When above this amount of memory measures in pages the system is
55 * determined to have enough free memory. This is similar to Linux's
56 * zone->pages_high multipled by the number of zones and is sized based
57 * on that. Assuming all zones are being used roughly equally, when
58 * async page reclamation reaches this threshold it stops.
61 EXPORT_SYMBOL(lotsfree
);
63 /* Unused always 0 in this implementation */
65 EXPORT_SYMBOL(needfree
);
67 pgcnt_t swapfs_minfree
= 0;
68 EXPORT_SYMBOL(swapfs_minfree
);
70 pgcnt_t swapfs_reserve
= 0;
71 EXPORT_SYMBOL(swapfs_reserve
);
73 vmem_t
*heap_arena
= NULL
;
74 EXPORT_SYMBOL(heap_arena
);
76 vmem_t
*zio_alloc_arena
= NULL
;
77 EXPORT_SYMBOL(zio_alloc_arena
);
79 vmem_t
*zio_arena
= NULL
;
80 EXPORT_SYMBOL(zio_arena
);
82 #ifndef HAVE_GET_VMALLOC_INFO
83 get_vmalloc_info_t get_vmalloc_info_fn
= SYMBOL_POISON
;
84 EXPORT_SYMBOL(get_vmalloc_info_fn
);
85 #endif /* HAVE_GET_VMALLOC_INFO */
87 #ifdef HAVE_PGDAT_HELPERS
88 # ifndef HAVE_FIRST_ONLINE_PGDAT
89 first_online_pgdat_t first_online_pgdat_fn
= SYMBOL_POISON
;
90 EXPORT_SYMBOL(first_online_pgdat_fn
);
91 # endif /* HAVE_FIRST_ONLINE_PGDAT */
93 # ifndef HAVE_NEXT_ONLINE_PGDAT
94 next_online_pgdat_t next_online_pgdat_fn
= SYMBOL_POISON
;
95 EXPORT_SYMBOL(next_online_pgdat_fn
);
96 # endif /* HAVE_NEXT_ONLINE_PGDAT */
98 # ifndef HAVE_NEXT_ZONE
99 next_zone_t next_zone_fn
= SYMBOL_POISON
;
100 EXPORT_SYMBOL(next_zone_fn
);
101 # endif /* HAVE_NEXT_ZONE */
103 #else /* HAVE_PGDAT_HELPERS */
105 # ifndef HAVE_PGDAT_LIST
106 struct pglist_data
*pgdat_list_addr
= SYMBOL_POISON
;
107 EXPORT_SYMBOL(pgdat_list_addr
);
108 # endif /* HAVE_PGDAT_LIST */
110 #endif /* HAVE_PGDAT_HELPERS */
112 #ifdef NEED_GET_ZONE_COUNTS
113 # ifndef HAVE_GET_ZONE_COUNTS
114 get_zone_counts_t get_zone_counts_fn
= SYMBOL_POISON
;
115 EXPORT_SYMBOL(get_zone_counts_fn
);
116 # endif /* HAVE_GET_ZONE_COUNTS */
119 spl_global_page_state(spl_zone_stat_item_t item
)
121 unsigned long active
;
122 unsigned long inactive
;
125 get_zone_counts(&active
, &inactive
, &free
);
127 case SPL_NR_FREE_PAGES
: return free
;
128 case SPL_NR_INACTIVE
: return inactive
;
129 case SPL_NR_ACTIVE
: return active
;
130 default: ASSERT(0); /* Unsupported */
136 # ifdef HAVE_GLOBAL_PAGE_STATE
138 spl_global_page_state(spl_zone_stat_item_t item
)
140 unsigned long pages
= 0;
143 case SPL_NR_FREE_PAGES
:
144 # ifdef HAVE_ZONE_STAT_ITEM_NR_FREE_PAGES
145 pages
+= global_page_state(NR_FREE_PAGES
);
148 case SPL_NR_INACTIVE
:
149 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE
150 pages
+= global_page_state(NR_INACTIVE
);
152 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_ANON
153 pages
+= global_page_state(NR_INACTIVE_ANON
);
155 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_FILE
156 pages
+= global_page_state(NR_INACTIVE_FILE
);
160 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE
161 pages
+= global_page_state(NR_ACTIVE
);
163 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_ANON
164 pages
+= global_page_state(NR_ACTIVE_ANON
);
166 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_FILE
167 pages
+= global_page_state(NR_ACTIVE_FILE
);
171 ASSERT(0); /* Unsupported */
177 # error "Both global_page_state() and get_zone_counts() unavailable"
178 # endif /* HAVE_GLOBAL_PAGE_STATE */
179 #endif /* NEED_GET_ZONE_COUNTS */
180 EXPORT_SYMBOL(spl_global_page_state
);
183 spl_kmem_availrmem(void)
185 /* The amount of easily available memory */
186 return (spl_global_page_state(SPL_NR_FREE_PAGES
) +
187 spl_global_page_state(SPL_NR_INACTIVE
));
189 EXPORT_SYMBOL(spl_kmem_availrmem
);
192 vmem_size(vmem_t
*vmp
, int typemask
)
194 struct vmalloc_info vmi
;
198 ASSERT(typemask
& (VMEM_ALLOC
| VMEM_FREE
));
200 get_vmalloc_info(&vmi
);
201 if (typemask
& VMEM_ALLOC
)
202 size
+= (size_t)vmi
.used
;
204 if (typemask
& VMEM_FREE
)
205 size
+= (size_t)(VMALLOC_TOTAL
- vmi
.used
);
209 EXPORT_SYMBOL(vmem_size
);
212 * Memory allocation interfaces and debugging for basic kmem_*
213 * and vmem_* style memory allocation. When DEBUG_KMEM is enable
214 * all allocations will be tracked when they are allocated and
215 * freed. When the SPL module is unload a list of all leaked
216 * addresses and where they were allocated will be dumped to the
217 * console. Enabling this feature has a significant impant on
218 * performance but it makes finding memory leaks staight forward.
221 /* Shim layer memory accounting */
222 atomic64_t kmem_alloc_used
= ATOMIC64_INIT(0);
223 unsigned long long kmem_alloc_max
= 0;
224 atomic64_t vmem_alloc_used
= ATOMIC64_INIT(0);
225 unsigned long long vmem_alloc_max
= 0;
226 int kmem_warning_flag
= 1;
228 EXPORT_SYMBOL(kmem_alloc_used
);
229 EXPORT_SYMBOL(kmem_alloc_max
);
230 EXPORT_SYMBOL(vmem_alloc_used
);
231 EXPORT_SYMBOL(vmem_alloc_max
);
232 EXPORT_SYMBOL(kmem_warning_flag
);
234 # ifdef DEBUG_KMEM_TRACKING
236 /* XXX - Not to surprisingly with debugging enabled the xmem_locks are very
237 * highly contended particularly on xfree(). If we want to run with this
238 * detailed debugging enabled for anything other than debugging we need to
239 * minimize the contention by moving to a lock per xmem_table entry model.
242 # define KMEM_HASH_BITS 10
243 # define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
245 # define VMEM_HASH_BITS 10
246 # define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS)
248 typedef struct kmem_debug
{
249 struct hlist_node kd_hlist
; /* Hash node linkage */
250 struct list_head kd_list
; /* List of all allocations */
251 void *kd_addr
; /* Allocation pointer */
252 size_t kd_size
; /* Allocation size */
253 const char *kd_func
; /* Allocation function */
254 int kd_line
; /* Allocation line */
257 spinlock_t kmem_lock
;
258 struct hlist_head kmem_table
[KMEM_TABLE_SIZE
];
259 struct list_head kmem_list
;
261 spinlock_t vmem_lock
;
262 struct hlist_head vmem_table
[VMEM_TABLE_SIZE
];
263 struct list_head vmem_list
;
265 EXPORT_SYMBOL(kmem_lock
);
266 EXPORT_SYMBOL(kmem_table
);
267 EXPORT_SYMBOL(kmem_list
);
269 EXPORT_SYMBOL(vmem_lock
);
270 EXPORT_SYMBOL(vmem_table
);
271 EXPORT_SYMBOL(vmem_list
);
274 int kmem_set_warning(int flag
) { return (kmem_warning_flag
= !!flag
); }
276 int kmem_set_warning(int flag
) { return 0; }
278 EXPORT_SYMBOL(kmem_set_warning
);
281 * Slab allocation interfaces
283 * While the Linux slab implementation was inspired by the Solaris
284 * implemenation I cannot use it to emulate the Solaris APIs. I
285 * require two features which are not provided by the Linux slab.
287 * 1) Constructors AND destructors. Recent versions of the Linux
288 * kernel have removed support for destructors. This is a deal
289 * breaker for the SPL which contains particularly expensive
290 * initializers for mutex's, condition variables, etc. We also
291 * require a minimal level of cleanup for these data types unlike
292 * many Linux data type which do need to be explicitly destroyed.
294 * 2) Virtual address space backed slab. Callers of the Solaris slab
295 * expect it to work well for both small are very large allocations.
296 * Because of memory fragmentation the Linux slab which is backed
297 * by kmalloc'ed memory performs very badly when confronted with
298 * large numbers of large allocations. Basing the slab on the
299 * virtual address space removes the need for contigeous pages
300 * and greatly improve performance for large allocations.
302 * For these reasons, the SPL has its own slab implementation with
303 * the needed features. It is not as highly optimized as either the
304 * Solaris or Linux slabs, but it should get me most of what is
305 * needed until it can be optimized or obsoleted by another approach.
307 * One serious concern I do have about this method is the relatively
308 * small virtual address space on 32bit arches. This will seriously
309 * constrain the size of the slab caches and their performance.
311 * XXX: Improve the partial slab list by carefully maintaining a
312 * strict ordering of fullest to emptiest slabs based on
313 * the slab reference count. This gaurentees the when freeing
314 * slabs back to the system we need only linearly traverse the
315 * last N slabs in the list to discover all the freeable slabs.
317 * XXX: NUMA awareness for optionally allocating memory close to a
318 * particular core. This can be adventageous if you know the slab
319 * object will be short lived and primarily accessed from one core.
321 * XXX: Slab coloring may also yield performance improvements and would
322 * be desirable to implement.
325 struct list_head spl_kmem_cache_list
; /* List of caches */
326 struct rw_semaphore spl_kmem_cache_sem
; /* Cache list lock */
328 static int spl_cache_flush(spl_kmem_cache_t
*skc
,
329 spl_kmem_magazine_t
*skm
, int flush
);
331 #ifdef HAVE_SET_SHRINKER
332 static struct shrinker
*spl_kmem_cache_shrinker
;
334 static int spl_kmem_cache_generic_shrinker(int nr_to_scan
,
335 unsigned int gfp_mask
);
336 static struct shrinker spl_kmem_cache_shrinker
= {
337 .shrink
= spl_kmem_cache_generic_shrinker
,
338 .seeks
= KMC_DEFAULT_SEEKS
,
343 # ifdef DEBUG_KMEM_TRACKING
345 static kmem_debug_t
*
346 kmem_del_init(spinlock_t
*lock
, struct hlist_head
*table
, int bits
,
349 struct hlist_head
*head
;
350 struct hlist_node
*node
;
351 struct kmem_debug
*p
;
355 spin_lock_irqsave(lock
, flags
);
357 head
= &table
[hash_ptr(addr
, bits
)];
358 hlist_for_each_entry_rcu(p
, node
, head
, kd_hlist
) {
359 if (p
->kd_addr
== addr
) {
360 hlist_del_init(&p
->kd_hlist
);
361 list_del_init(&p
->kd_list
);
362 spin_unlock_irqrestore(lock
, flags
);
367 spin_unlock_irqrestore(lock
, flags
);
373 kmem_alloc_track(size_t size
, int flags
, const char *func
, int line
,
374 int node_alloc
, int node
)
378 unsigned long irq_flags
;
381 dptr
= (kmem_debug_t
*) kmalloc(sizeof(kmem_debug_t
),
382 flags
& ~__GFP_ZERO
);
385 CWARN("kmem_alloc(%ld, 0x%x) debug failed\n",
386 sizeof(kmem_debug_t
), flags
);
388 /* Marked unlikely because we should never be doing this,
389 * we tolerate to up 2 pages but a single page is best. */
390 if (unlikely((size
) > (PAGE_SIZE
* 2)) && kmem_warning_flag
)
391 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
392 (unsigned long long) size
, flags
,
393 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
395 /* We use kstrdup() below because the string pointed to by
396 * __FUNCTION__ might not be available by the time we want
397 * to print it since the module might have been unloaded. */
398 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
399 if (unlikely(dptr
->kd_func
== NULL
)) {
401 CWARN("kstrdup() failed in kmem_alloc(%llu, 0x%x) "
402 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
403 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
407 /* Use the correct allocator */
409 ASSERT(!(flags
& __GFP_ZERO
));
410 ptr
= kmalloc_node(size
, flags
, node
);
411 } else if (flags
& __GFP_ZERO
) {
412 ptr
= kzalloc(size
, flags
& ~__GFP_ZERO
);
414 ptr
= kmalloc(size
, flags
);
417 if (unlikely(ptr
== NULL
)) {
418 kfree(dptr
->kd_func
);
420 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
421 (unsigned long long) size
, flags
,
422 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
426 atomic64_add(size
, &kmem_alloc_used
);
427 if (unlikely(atomic64_read(&kmem_alloc_used
) >
430 atomic64_read(&kmem_alloc_used
);
432 INIT_HLIST_NODE(&dptr
->kd_hlist
);
433 INIT_LIST_HEAD(&dptr
->kd_list
);
436 dptr
->kd_size
= size
;
437 dptr
->kd_line
= line
;
439 spin_lock_irqsave(&kmem_lock
, irq_flags
);
440 hlist_add_head_rcu(&dptr
->kd_hlist
,
441 &kmem_table
[hash_ptr(ptr
, KMEM_HASH_BITS
)]);
442 list_add_tail(&dptr
->kd_list
, &kmem_list
);
443 spin_unlock_irqrestore(&kmem_lock
, irq_flags
);
445 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
446 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
447 ptr
, atomic64_read(&kmem_alloc_used
),
453 EXPORT_SYMBOL(kmem_alloc_track
);
456 kmem_free_track(void *ptr
, size_t size
)
461 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
462 (unsigned long long) size
);
464 dptr
= kmem_del_init(&kmem_lock
, kmem_table
, KMEM_HASH_BITS
, ptr
);
466 ASSERT(dptr
); /* Must exist in hash due to kmem_alloc() */
468 /* Size must match */
469 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
470 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
471 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
473 atomic64_sub(size
, &kmem_alloc_used
);
475 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
476 (unsigned long long) size
, atomic64_read(&kmem_alloc_used
),
479 kfree(dptr
->kd_func
);
481 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
484 memset(ptr
, 0x5a, size
);
489 EXPORT_SYMBOL(kmem_free_track
);
492 vmem_alloc_track(size_t size
, int flags
, const char *func
, int line
)
496 unsigned long irq_flags
;
499 ASSERT(flags
& KM_SLEEP
);
501 dptr
= (kmem_debug_t
*) kmalloc(sizeof(kmem_debug_t
), flags
);
503 CWARN("vmem_alloc(%ld, 0x%x) debug failed\n",
504 sizeof(kmem_debug_t
), flags
);
506 /* We use kstrdup() below because the string pointed to by
507 * __FUNCTION__ might not be available by the time we want
508 * to print it, since the module might have been unloaded. */
509 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
510 if (unlikely(dptr
->kd_func
== NULL
)) {
512 CWARN("kstrdup() failed in vmem_alloc(%llu, 0x%x) "
513 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
514 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
518 ptr
= __vmalloc(size
, (flags
| __GFP_HIGHMEM
) & ~__GFP_ZERO
,
521 if (unlikely(ptr
== NULL
)) {
522 kfree(dptr
->kd_func
);
524 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
525 (unsigned long long) size
, flags
,
526 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
530 if (flags
& __GFP_ZERO
)
531 memset(ptr
, 0, size
);
533 atomic64_add(size
, &vmem_alloc_used
);
534 if (unlikely(atomic64_read(&vmem_alloc_used
) >
537 atomic64_read(&vmem_alloc_used
);
539 INIT_HLIST_NODE(&dptr
->kd_hlist
);
540 INIT_LIST_HEAD(&dptr
->kd_list
);
543 dptr
->kd_size
= size
;
544 dptr
->kd_line
= line
;
546 spin_lock_irqsave(&vmem_lock
, irq_flags
);
547 hlist_add_head_rcu(&dptr
->kd_hlist
,
548 &vmem_table
[hash_ptr(ptr
, VMEM_HASH_BITS
)]);
549 list_add_tail(&dptr
->kd_list
, &vmem_list
);
550 spin_unlock_irqrestore(&vmem_lock
, irq_flags
);
552 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
553 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
554 ptr
, atomic64_read(&vmem_alloc_used
),
560 EXPORT_SYMBOL(vmem_alloc_track
);
563 vmem_free_track(void *ptr
, size_t size
)
568 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
569 (unsigned long long) size
);
571 dptr
= kmem_del_init(&vmem_lock
, vmem_table
, VMEM_HASH_BITS
, ptr
);
572 ASSERT(dptr
); /* Must exist in hash due to vmem_alloc() */
574 /* Size must match */
575 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
576 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
577 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
579 atomic64_sub(size
, &vmem_alloc_used
);
580 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
581 (unsigned long long) size
, atomic64_read(&vmem_alloc_used
),
584 kfree(dptr
->kd_func
);
586 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
589 memset(ptr
, 0x5a, size
);
594 EXPORT_SYMBOL(vmem_free_track
);
596 # else /* DEBUG_KMEM_TRACKING */
599 kmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
,
600 int node_alloc
, int node
)
605 /* Marked unlikely because we should never be doing this,
606 * we tolerate to up 2 pages but a single page is best. */
607 if (unlikely(size
> (PAGE_SIZE
* 2)) && kmem_warning_flag
)
608 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
609 (unsigned long long) size
, flags
,
610 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
612 /* Use the correct allocator */
614 ASSERT(!(flags
& __GFP_ZERO
));
615 ptr
= kmalloc_node(size
, flags
, node
);
616 } else if (flags
& __GFP_ZERO
) {
617 ptr
= kzalloc(size
, flags
& (~__GFP_ZERO
));
619 ptr
= kmalloc(size
, flags
);
623 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
624 (unsigned long long) size
, flags
,
625 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
627 atomic64_add(size
, &kmem_alloc_used
);
628 if (unlikely(atomic64_read(&kmem_alloc_used
) > kmem_alloc_max
))
629 kmem_alloc_max
= atomic64_read(&kmem_alloc_used
);
631 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
632 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
633 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
637 EXPORT_SYMBOL(kmem_alloc_debug
);
640 kmem_free_debug(void *ptr
, size_t size
)
644 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
645 (unsigned long long) size
);
647 atomic64_sub(size
, &kmem_alloc_used
);
649 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
650 (unsigned long long) size
, atomic64_read(&kmem_alloc_used
),
653 memset(ptr
, 0x5a, size
);
658 EXPORT_SYMBOL(kmem_free_debug
);
661 vmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
)
666 ASSERT(flags
& KM_SLEEP
);
668 ptr
= __vmalloc(size
, (flags
| __GFP_HIGHMEM
) & ~__GFP_ZERO
,
671 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
672 (unsigned long long) size
, flags
,
673 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
675 if (flags
& __GFP_ZERO
)
676 memset(ptr
, 0, size
);
678 atomic64_add(size
, &vmem_alloc_used
);
680 if (unlikely(atomic64_read(&vmem_alloc_used
) > vmem_alloc_max
))
681 vmem_alloc_max
= atomic64_read(&vmem_alloc_used
);
683 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
684 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
685 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
690 EXPORT_SYMBOL(vmem_alloc_debug
);
693 vmem_free_debug(void *ptr
, size_t size
)
697 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
698 (unsigned long long) size
);
700 atomic64_sub(size
, &vmem_alloc_used
);
702 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
703 (unsigned long long) size
, atomic64_read(&vmem_alloc_used
),
706 memset(ptr
, 0x5a, size
);
711 EXPORT_SYMBOL(vmem_free_debug
);
713 # endif /* DEBUG_KMEM_TRACKING */
714 #endif /* DEBUG_KMEM */
717 kv_alloc(spl_kmem_cache_t
*skc
, int size
, int flags
)
721 if (skc
->skc_flags
& KMC_KMEM
) {
722 if (size
> (2 * PAGE_SIZE
)) {
723 ptr
= (void *)__get_free_pages(flags
, get_order(size
));
725 ptr
= kmem_alloc(size
, flags
);
727 ptr
= vmem_alloc(size
, flags
);
734 kv_free(spl_kmem_cache_t
*skc
, void *ptr
, int size
)
736 if (skc
->skc_flags
& KMC_KMEM
) {
737 if (size
> (2 * PAGE_SIZE
))
738 free_pages((unsigned long)ptr
, get_order(size
));
740 kmem_free(ptr
, size
);
742 vmem_free(ptr
, size
);
747 * It's important that we pack the spl_kmem_obj_t structure and the
748 * actual objects in to one large address space to minimize the number
749 * of calls to the allocator. It is far better to do a few large
750 * allocations and then subdivide it ourselves. Now which allocator
751 * we use requires balancing a few trade offs.
753 * For small objects we use kmem_alloc() because as long as you are
754 * only requesting a small number of pages (ideally just one) its cheap.
755 * However, when you start requesting multiple pages with kmem_alloc()
756 * it gets increasingly expensive since it requires contigeous pages.
757 * For this reason we shift to vmem_alloc() for slabs of large objects
758 * which removes the need for contigeous pages. We do not use
759 * vmem_alloc() in all cases because there is significant locking
760 * overhead in __get_vm_area_node(). This function takes a single
761 * global lock when aquiring an available virtual address range which
762 * serializes all vmem_alloc()'s for all slab caches. Using slightly
763 * different allocation functions for small and large objects should
764 * give us the best of both worlds.
766 * KMC_ONSLAB KMC_OFFSLAB
768 * +------------------------+ +-----------------+
769 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
770 * | skc_obj_size <-+ | | +-----------------+ | |
771 * | spl_kmem_obj_t | | | |
772 * | skc_obj_size <---+ | +-----------------+ | |
773 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
774 * | ... v | | spl_kmem_obj_t | |
775 * +------------------------+ +-----------------+ v
777 static spl_kmem_slab_t
*
778 spl_slab_alloc(spl_kmem_cache_t
*skc
, int flags
)
780 spl_kmem_slab_t
*sks
;
781 spl_kmem_obj_t
*sko
, *n
;
783 int i
, align
, size
, rc
= 0;
785 base
= kv_alloc(skc
, skc
->skc_slab_size
, flags
);
789 sks
= (spl_kmem_slab_t
*)base
;
790 sks
->sks_magic
= SKS_MAGIC
;
791 sks
->sks_objs
= skc
->skc_slab_objs
;
792 sks
->sks_age
= jiffies
;
793 sks
->sks_cache
= skc
;
794 INIT_LIST_HEAD(&sks
->sks_list
);
795 INIT_LIST_HEAD(&sks
->sks_free_list
);
798 align
= skc
->skc_obj_align
;
799 size
= P2ROUNDUP(skc
->skc_obj_size
, align
) +
800 P2ROUNDUP(sizeof(spl_kmem_obj_t
), align
);
802 for (i
= 0; i
< sks
->sks_objs
; i
++) {
803 if (skc
->skc_flags
& KMC_OFFSLAB
) {
804 obj
= kv_alloc(skc
, size
, flags
);
806 GOTO(out
, rc
= -ENOMEM
);
809 P2ROUNDUP(sizeof(spl_kmem_slab_t
), align
) +
813 sko
= obj
+ P2ROUNDUP(skc
->skc_obj_size
, align
);
815 sko
->sko_magic
= SKO_MAGIC
;
817 INIT_LIST_HEAD(&sko
->sko_list
);
818 list_add_tail(&sko
->sko_list
, &sks
->sks_free_list
);
821 list_for_each_entry(sko
, &sks
->sks_free_list
, sko_list
)
823 skc
->skc_ctor(sko
->sko_addr
, skc
->skc_private
, flags
);
826 if (skc
->skc_flags
& KMC_OFFSLAB
)
827 list_for_each_entry_safe(sko
, n
, &sks
->sks_free_list
,
829 kv_free(skc
, sko
->sko_addr
, size
);
831 kv_free(skc
, base
, skc
->skc_slab_size
);
839 * Remove a slab from complete or partial list, it must be called with
840 * the 'skc->skc_lock' held but the actual free must be performed
841 * outside the lock to prevent deadlocking on vmem addresses.
844 spl_slab_free(spl_kmem_slab_t
*sks
,
845 struct list_head
*sks_list
, struct list_head
*sko_list
)
847 spl_kmem_cache_t
*skc
;
850 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
851 ASSERT(sks
->sks_ref
== 0);
853 skc
= sks
->sks_cache
;
854 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
855 ASSERT(spin_is_locked(&skc
->skc_lock
));
858 * Update slab/objects counters in the cache, then remove the
859 * slab from the skc->skc_partial_list. Finally add the slab
860 * and all its objects in to the private work lists where the
861 * destructors will be called and the memory freed to the system.
863 skc
->skc_obj_total
-= sks
->sks_objs
;
864 skc
->skc_slab_total
--;
865 list_del(&sks
->sks_list
);
866 list_add(&sks
->sks_list
, sks_list
);
867 list_splice_init(&sks
->sks_free_list
, sko_list
);
873 * Traverses all the partial slabs attached to a cache and free those
874 * which which are currently empty, and have not been touched for
875 * skc_delay seconds to avoid thrashing. The count argument is
876 * passed to optionally cap the number of slabs reclaimed, a count
877 * of zero means try and reclaim everything. When flag is set we
878 * always free an available slab regardless of age.
881 spl_slab_reclaim(spl_kmem_cache_t
*skc
, int count
, int flag
)
883 spl_kmem_slab_t
*sks
, *m
;
884 spl_kmem_obj_t
*sko
, *n
;
891 * Move empty slabs and objects which have not been touched in
892 * skc_delay seconds on to private lists to be freed outside
893 * the spin lock. This delay time is important to avoid thrashing
894 * however when flag is set the delay will not be used.
896 spin_lock(&skc
->skc_lock
);
897 list_for_each_entry_safe_reverse(sks
,m
,&skc
->skc_partial_list
,sks_list
){
899 * All empty slabs are at the end of skc->skc_partial_list,
900 * therefore once a non-empty slab is found we can stop
901 * scanning. Additionally, stop when reaching the target
902 * reclaim 'count' if a non-zero threshhold is given.
904 if ((sks
->sks_ref
> 0) || (count
&& i
> count
))
907 if (time_after(jiffies
,sks
->sks_age
+skc
->skc_delay
*HZ
)||flag
) {
908 spl_slab_free(sks
, &sks_list
, &sko_list
);
912 spin_unlock(&skc
->skc_lock
);
915 * The following two loops ensure all the object destructors are
916 * run, any offslab objects are freed, and the slabs themselves
917 * are freed. This is all done outside the skc->skc_lock since
918 * this allows the destructor to sleep, and allows us to perform
919 * a conditional reschedule when a freeing a large number of
920 * objects and slabs back to the system.
922 if (skc
->skc_flags
& KMC_OFFSLAB
)
923 size
= P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
) +
924 P2ROUNDUP(sizeof(spl_kmem_obj_t
), skc
->skc_obj_align
);
926 list_for_each_entry_safe(sko
, n
, &sko_list
, sko_list
) {
927 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
930 skc
->skc_dtor(sko
->sko_addr
, skc
->skc_private
);
932 if (skc
->skc_flags
& KMC_OFFSLAB
)
933 kv_free(skc
, sko
->sko_addr
, size
);
938 list_for_each_entry_safe(sks
, m
, &sks_list
, sks_list
) {
939 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
940 kv_free(skc
, sks
, skc
->skc_slab_size
);
948 * Called regularly on all caches to age objects out of the magazines
949 * which have not been access in skc->skc_delay seconds. This prevents
950 * idle magazines from holding memory which might be better used by
951 * other caches or parts of the system. The delay is present to
952 * prevent thrashing the magazine.
955 spl_magazine_age(void *data
)
957 spl_kmem_magazine_t
*skm
=
958 spl_get_work_data(data
, spl_kmem_magazine_t
, skm_work
.work
);
959 spl_kmem_cache_t
*skc
= skm
->skm_cache
;
960 int i
= smp_processor_id();
962 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
963 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
964 ASSERT(skc
->skc_mag
[i
] == skm
);
966 if (skm
->skm_avail
> 0 &&
967 time_after(jiffies
, skm
->skm_age
+ skc
->skc_delay
* HZ
))
968 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
970 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
971 schedule_delayed_work_on(i
, &skm
->skm_work
,
972 skc
->skc_delay
/ 3 * HZ
);
976 * Called regularly to keep a downward pressure on the size of idle
977 * magazines and to release free slabs from the cache. This function
978 * never calls the registered reclaim function, that only occures
979 * under memory pressure or with a direct call to spl_kmem_reap().
982 spl_cache_age(void *data
)
984 spl_kmem_cache_t
*skc
=
985 spl_get_work_data(data
, spl_kmem_cache_t
, skc_work
.work
);
987 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
988 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
990 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
991 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
995 * Size a slab based on the size of each aliged object plus spl_kmem_obj_t.
996 * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However,
997 * for very small objects we may end up with more than this so as not
998 * to waste space in the minimal allocation of a single page. Also for
999 * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN,
1000 * lower than this and we will fail.
1003 spl_slab_size(spl_kmem_cache_t
*skc
, uint32_t *objs
, uint32_t *size
)
1005 int sks_size
, obj_size
, max_size
, align
;
1007 if (skc
->skc_flags
& KMC_OFFSLAB
) {
1008 *objs
= SPL_KMEM_CACHE_OBJ_PER_SLAB
;
1009 *size
= sizeof(spl_kmem_slab_t
);
1011 align
= skc
->skc_obj_align
;
1012 sks_size
= P2ROUNDUP(sizeof(spl_kmem_slab_t
), align
);
1013 obj_size
= P2ROUNDUP(skc
->skc_obj_size
, align
) +
1014 P2ROUNDUP(sizeof(spl_kmem_obj_t
), align
);
1016 if (skc
->skc_flags
& KMC_KMEM
)
1017 max_size
= ((uint64_t)1 << (MAX_ORDER
-1)) * PAGE_SIZE
;
1019 max_size
= (32 * 1024 * 1024);
1021 for (*size
= PAGE_SIZE
; *size
<= max_size
; *size
+= PAGE_SIZE
) {
1022 *objs
= (*size
- sks_size
) / obj_size
;
1023 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB
)
1028 * Unable to satisfy target objets per slab, fallback to
1029 * allocating a maximally sized slab and assuming it can
1030 * contain the minimum objects count use it. If not fail.
1033 *objs
= (*size
- sks_size
) / obj_size
;
1034 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN
)
1042 * Make a guess at reasonable per-cpu magazine size based on the size of
1043 * each object and the cost of caching N of them in each magazine. Long
1044 * term this should really adapt based on an observed usage heuristic.
1047 spl_magazine_size(spl_kmem_cache_t
*skc
)
1049 int size
, align
= skc
->skc_obj_align
;
1052 /* Per-magazine sizes below assume a 4Kib page size */
1053 if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
* 256))
1054 size
= 4; /* Minimum 4Mib per-magazine */
1055 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
* 32))
1056 size
= 16; /* Minimum 2Mib per-magazine */
1057 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
))
1058 size
= 64; /* Minimum 256Kib per-magazine */
1059 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
/ 4))
1060 size
= 128; /* Minimum 128Kib per-magazine */
1068 * Allocate a per-cpu magazine to assoicate with a specific core.
1070 static spl_kmem_magazine_t
*
1071 spl_magazine_alloc(spl_kmem_cache_t
*skc
, int node
)
1073 spl_kmem_magazine_t
*skm
;
1074 int size
= sizeof(spl_kmem_magazine_t
) +
1075 sizeof(void *) * skc
->skc_mag_size
;
1078 skm
= kmem_alloc_node(size
, GFP_KERNEL
| __GFP_NOFAIL
, node
);
1080 skm
->skm_magic
= SKM_MAGIC
;
1082 skm
->skm_size
= skc
->skc_mag_size
;
1083 skm
->skm_refill
= skc
->skc_mag_refill
;
1084 skm
->skm_cache
= skc
;
1085 spl_init_delayed_work(&skm
->skm_work
, spl_magazine_age
, skm
);
1086 skm
->skm_age
= jiffies
;
1093 * Free a per-cpu magazine assoicated with a specific core.
1096 spl_magazine_free(spl_kmem_magazine_t
*skm
)
1098 int size
= sizeof(spl_kmem_magazine_t
) +
1099 sizeof(void *) * skm
->skm_size
;
1102 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1103 ASSERT(skm
->skm_avail
== 0);
1105 kmem_free(skm
, size
);
1110 * Create all pre-cpu magazines of reasonable sizes.
1113 spl_magazine_create(spl_kmem_cache_t
*skc
)
1118 skc
->skc_mag_size
= spl_magazine_size(skc
);
1119 skc
->skc_mag_refill
= (skc
->skc_mag_size
+ 1) / 2;
1121 for_each_online_cpu(i
) {
1122 skc
->skc_mag
[i
] = spl_magazine_alloc(skc
, cpu_to_node(i
));
1123 if (!skc
->skc_mag
[i
]) {
1124 for (i
--; i
>= 0; i
--)
1125 spl_magazine_free(skc
->skc_mag
[i
]);
1131 /* Only after everything is allocated schedule magazine work */
1132 for_each_online_cpu(i
)
1133 schedule_delayed_work_on(i
, &skc
->skc_mag
[i
]->skm_work
,
1134 skc
->skc_delay
/ 3 * HZ
);
1140 * Destroy all pre-cpu magazines.
1143 spl_magazine_destroy(spl_kmem_cache_t
*skc
)
1145 spl_kmem_magazine_t
*skm
;
1149 for_each_online_cpu(i
) {
1150 skm
= skc
->skc_mag
[i
];
1151 (void)spl_cache_flush(skc
, skm
, skm
->skm_avail
);
1152 spl_magazine_free(skm
);
1159 * Create a object cache based on the following arguments:
1161 * size cache object size
1162 * align cache object alignment
1163 * ctor cache object constructor
1164 * dtor cache object destructor
1165 * reclaim cache object reclaim
1166 * priv cache private data for ctor/dtor/reclaim
1167 * vmp unused must be NULL
1169 * KMC_NOTOUCH Disable cache object aging (unsupported)
1170 * KMC_NODEBUG Disable debugging (unsupported)
1171 * KMC_NOMAGAZINE Disable magazine (unsupported)
1172 * KMC_NOHASH Disable hashing (unsupported)
1173 * KMC_QCACHE Disable qcache (unsupported)
1174 * KMC_KMEM Force kmem backed cache
1175 * KMC_VMEM Force vmem backed cache
1176 * KMC_OFFSLAB Locate objects off the slab
1179 spl_kmem_cache_create(char *name
, size_t size
, size_t align
,
1180 spl_kmem_ctor_t ctor
,
1181 spl_kmem_dtor_t dtor
,
1182 spl_kmem_reclaim_t reclaim
,
1183 void *priv
, void *vmp
, int flags
)
1185 spl_kmem_cache_t
*skc
;
1186 int rc
, kmem_flags
= KM_SLEEP
;
1189 ASSERTF(!(flags
& KMC_NOMAGAZINE
), "Bad KMC_NOMAGAZINE (%x)\n", flags
);
1190 ASSERTF(!(flags
& KMC_NOHASH
), "Bad KMC_NOHASH (%x)\n", flags
);
1191 ASSERTF(!(flags
& KMC_QCACHE
), "Bad KMC_QCACHE (%x)\n", flags
);
1192 ASSERT(vmp
== NULL
);
1194 /* We may be called when there is a non-zero preempt_count or
1195 * interrupts are disabled is which case we must not sleep.
1197 if (current_thread_info()->preempt_count
|| irqs_disabled())
1198 kmem_flags
= KM_NOSLEEP
;
1200 /* Allocate new cache memory and initialize. */
1201 skc
= (spl_kmem_cache_t
*)kmem_zalloc(sizeof(*skc
), kmem_flags
);
1205 skc
->skc_magic
= SKC_MAGIC
;
1206 skc
->skc_name_size
= strlen(name
) + 1;
1207 skc
->skc_name
= (char *)kmem_alloc(skc
->skc_name_size
, kmem_flags
);
1208 if (skc
->skc_name
== NULL
) {
1209 kmem_free(skc
, sizeof(*skc
));
1212 strncpy(skc
->skc_name
, name
, skc
->skc_name_size
);
1214 skc
->skc_ctor
= ctor
;
1215 skc
->skc_dtor
= dtor
;
1216 skc
->skc_reclaim
= reclaim
;
1217 skc
->skc_private
= priv
;
1219 skc
->skc_flags
= flags
;
1220 skc
->skc_obj_size
= size
;
1221 skc
->skc_obj_align
= SPL_KMEM_CACHE_ALIGN
;
1222 skc
->skc_delay
= SPL_KMEM_CACHE_DELAY
;
1223 skc
->skc_reap
= SPL_KMEM_CACHE_REAP
;
1224 atomic_set(&skc
->skc_ref
, 0);
1226 INIT_LIST_HEAD(&skc
->skc_list
);
1227 INIT_LIST_HEAD(&skc
->skc_complete_list
);
1228 INIT_LIST_HEAD(&skc
->skc_partial_list
);
1229 spin_lock_init(&skc
->skc_lock
);
1230 skc
->skc_slab_fail
= 0;
1231 skc
->skc_slab_create
= 0;
1232 skc
->skc_slab_destroy
= 0;
1233 skc
->skc_slab_total
= 0;
1234 skc
->skc_slab_alloc
= 0;
1235 skc
->skc_slab_max
= 0;
1236 skc
->skc_obj_total
= 0;
1237 skc
->skc_obj_alloc
= 0;
1238 skc
->skc_obj_max
= 0;
1241 ASSERT((align
& (align
- 1)) == 0); /* Power of two */
1242 ASSERT(align
>= SPL_KMEM_CACHE_ALIGN
); /* Minimum size */
1243 skc
->skc_obj_align
= align
;
1246 /* If none passed select a cache type based on object size */
1247 if (!(skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
))) {
1248 if (P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
) <
1250 skc
->skc_flags
|= KMC_KMEM
;
1252 skc
->skc_flags
|= KMC_VMEM
;
1256 rc
= spl_slab_size(skc
, &skc
->skc_slab_objs
, &skc
->skc_slab_size
);
1260 rc
= spl_magazine_create(skc
);
1264 spl_init_delayed_work(&skc
->skc_work
, spl_cache_age
, skc
);
1265 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
1267 down_write(&spl_kmem_cache_sem
);
1268 list_add_tail(&skc
->skc_list
, &spl_kmem_cache_list
);
1269 up_write(&spl_kmem_cache_sem
);
1273 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1274 kmem_free(skc
, sizeof(*skc
));
1277 EXPORT_SYMBOL(spl_kmem_cache_create
);
1280 * Destroy a cache and all objects assoicated with the cache.
1283 spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
)
1285 DECLARE_WAIT_QUEUE_HEAD(wq
);
1289 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1291 down_write(&spl_kmem_cache_sem
);
1292 list_del_init(&skc
->skc_list
);
1293 up_write(&spl_kmem_cache_sem
);
1295 /* Cancel any and wait for any pending delayed work */
1296 ASSERT(!test_and_set_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1297 cancel_delayed_work(&skc
->skc_work
);
1298 for_each_online_cpu(i
)
1299 cancel_delayed_work(&skc
->skc_mag
[i
]->skm_work
);
1301 flush_scheduled_work();
1303 /* Wait until all current callers complete, this is mainly
1304 * to catch the case where a low memory situation triggers a
1305 * cache reaping action which races with this destroy. */
1306 wait_event(wq
, atomic_read(&skc
->skc_ref
) == 0);
1308 spl_magazine_destroy(skc
);
1309 spl_slab_reclaim(skc
, 0, 1);
1310 spin_lock(&skc
->skc_lock
);
1312 /* Validate there are no objects in use and free all the
1313 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
1314 ASSERT3U(skc
->skc_slab_alloc
, ==, 0);
1315 ASSERT3U(skc
->skc_obj_alloc
, ==, 0);
1316 ASSERT3U(skc
->skc_slab_total
, ==, 0);
1317 ASSERT3U(skc
->skc_obj_total
, ==, 0);
1318 ASSERT(list_empty(&skc
->skc_complete_list
));
1320 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1321 spin_unlock(&skc
->skc_lock
);
1323 kmem_free(skc
, sizeof(*skc
));
1327 EXPORT_SYMBOL(spl_kmem_cache_destroy
);
1330 * Allocate an object from a slab attached to the cache. This is used to
1331 * repopulate the per-cpu magazine caches in batches when they run low.
1334 spl_cache_obj(spl_kmem_cache_t
*skc
, spl_kmem_slab_t
*sks
)
1336 spl_kmem_obj_t
*sko
;
1338 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1339 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1340 ASSERT(spin_is_locked(&skc
->skc_lock
));
1342 sko
= list_entry(sks
->sks_free_list
.next
, spl_kmem_obj_t
, sko_list
);
1343 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1344 ASSERT(sko
->sko_addr
!= NULL
);
1346 /* Remove from sks_free_list */
1347 list_del_init(&sko
->sko_list
);
1349 sks
->sks_age
= jiffies
;
1351 skc
->skc_obj_alloc
++;
1353 /* Track max obj usage statistics */
1354 if (skc
->skc_obj_alloc
> skc
->skc_obj_max
)
1355 skc
->skc_obj_max
= skc
->skc_obj_alloc
;
1357 /* Track max slab usage statistics */
1358 if (sks
->sks_ref
== 1) {
1359 skc
->skc_slab_alloc
++;
1361 if (skc
->skc_slab_alloc
> skc
->skc_slab_max
)
1362 skc
->skc_slab_max
= skc
->skc_slab_alloc
;
1365 return sko
->sko_addr
;
1369 * No available objects on any slabsi, create a new slab. Since this
1370 * is an expensive operation we do it without holding the spinlock and
1371 * only briefly aquire it when we link in the fully allocated and
1374 static spl_kmem_slab_t
*
1375 spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
)
1377 spl_kmem_slab_t
*sks
;
1380 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1385 * Before allocating a new slab check if the slab is being reaped.
1386 * If it is there is a good chance we can wait until it finishes
1387 * and then use one of the newly freed but not aged-out slabs.
1389 if (test_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1391 GOTO(out
, sks
= NULL
);
1394 /* Allocate a new slab for the cache */
1395 sks
= spl_slab_alloc(skc
, flags
| __GFP_NORETRY
| __GFP_NOWARN
);
1397 GOTO(out
, sks
= NULL
);
1399 /* Link the new empty slab in to the end of skc_partial_list. */
1400 spin_lock(&skc
->skc_lock
);
1401 skc
->skc_slab_total
++;
1402 skc
->skc_obj_total
+= sks
->sks_objs
;
1403 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1404 spin_unlock(&skc
->skc_lock
);
1406 local_irq_disable();
1412 * Refill a per-cpu magazine with objects from the slabs for this
1413 * cache. Ideally the magazine can be repopulated using existing
1414 * objects which have been released, however if we are unable to
1415 * locate enough free objects new slabs of objects will be created.
1418 spl_cache_refill(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flags
)
1420 spl_kmem_slab_t
*sks
;
1424 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1425 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1427 refill
= MIN(skm
->skm_refill
, skm
->skm_size
- skm
->skm_avail
);
1428 spin_lock(&skc
->skc_lock
);
1430 while (refill
> 0) {
1431 /* No slabs available we may need to grow the cache */
1432 if (list_empty(&skc
->skc_partial_list
)) {
1433 spin_unlock(&skc
->skc_lock
);
1435 sks
= spl_cache_grow(skc
, flags
);
1439 /* Rescheduled to different CPU skm is not local */
1440 if (skm
!= skc
->skc_mag
[smp_processor_id()])
1443 /* Potentially rescheduled to the same CPU but
1444 * allocations may have occured from this CPU while
1445 * we were sleeping so recalculate max refill. */
1446 refill
= MIN(refill
, skm
->skm_size
- skm
->skm_avail
);
1448 spin_lock(&skc
->skc_lock
);
1452 /* Grab the next available slab */
1453 sks
= list_entry((&skc
->skc_partial_list
)->next
,
1454 spl_kmem_slab_t
, sks_list
);
1455 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1456 ASSERT(sks
->sks_ref
< sks
->sks_objs
);
1457 ASSERT(!list_empty(&sks
->sks_free_list
));
1459 /* Consume as many objects as needed to refill the requested
1460 * cache. We must also be careful not to overfill it. */
1461 while (sks
->sks_ref
< sks
->sks_objs
&& refill
-- > 0 && ++rc
) {
1462 ASSERT(skm
->skm_avail
< skm
->skm_size
);
1463 ASSERT(rc
< skm
->skm_size
);
1464 skm
->skm_objs
[skm
->skm_avail
++]=spl_cache_obj(skc
,sks
);
1467 /* Move slab to skc_complete_list when full */
1468 if (sks
->sks_ref
== sks
->sks_objs
) {
1469 list_del(&sks
->sks_list
);
1470 list_add(&sks
->sks_list
, &skc
->skc_complete_list
);
1474 spin_unlock(&skc
->skc_lock
);
1476 /* Returns the number of entries added to cache */
1481 * Release an object back to the slab from which it came.
1484 spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
)
1486 spl_kmem_slab_t
*sks
= NULL
;
1487 spl_kmem_obj_t
*sko
= NULL
;
1490 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1491 ASSERT(spin_is_locked(&skc
->skc_lock
));
1493 sko
= obj
+ P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
);
1494 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1496 sks
= sko
->sko_slab
;
1497 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1498 ASSERT(sks
->sks_cache
== skc
);
1499 list_add(&sko
->sko_list
, &sks
->sks_free_list
);
1501 sks
->sks_age
= jiffies
;
1503 skc
->skc_obj_alloc
--;
1505 /* Move slab to skc_partial_list when no longer full. Slabs
1506 * are added to the head to keep the partial list is quasi-full
1507 * sorted order. Fuller at the head, emptier at the tail. */
1508 if (sks
->sks_ref
== (sks
->sks_objs
- 1)) {
1509 list_del(&sks
->sks_list
);
1510 list_add(&sks
->sks_list
, &skc
->skc_partial_list
);
1513 /* Move emply slabs to the end of the partial list so
1514 * they can be easily found and freed during reclamation. */
1515 if (sks
->sks_ref
== 0) {
1516 list_del(&sks
->sks_list
);
1517 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1518 skc
->skc_slab_alloc
--;
1525 * Release a batch of objects from a per-cpu magazine back to their
1526 * respective slabs. This occurs when we exceed the magazine size,
1527 * are under memory pressure, when the cache is idle, or during
1528 * cache cleanup. The flush argument contains the number of entries
1529 * to remove from the magazine.
1532 spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
1534 int i
, count
= MIN(flush
, skm
->skm_avail
);
1537 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1538 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1541 * XXX: Currently we simply return objects from the magazine to
1542 * the slabs in fifo order. The ideal thing to do from a memory
1543 * fragmentation standpoint is to cheaply determine the set of
1544 * objects in the magazine which will result in the largest
1545 * number of free slabs if released from the magazine.
1547 spin_lock(&skc
->skc_lock
);
1548 for (i
= 0; i
< count
; i
++)
1549 spl_cache_shrink(skc
, skm
->skm_objs
[i
]);
1551 skm
->skm_avail
-= count
;
1552 memmove(skm
->skm_objs
, &(skm
->skm_objs
[count
]),
1553 sizeof(void *) * skm
->skm_avail
);
1555 spin_unlock(&skc
->skc_lock
);
1561 * Allocate an object from the per-cpu magazine, or if the magazine
1562 * is empty directly allocate from a slab and repopulate the magazine.
1565 spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
)
1567 spl_kmem_magazine_t
*skm
;
1568 unsigned long irq_flags
;
1572 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1573 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1574 ASSERT(flags
& KM_SLEEP
);
1575 atomic_inc(&skc
->skc_ref
);
1576 local_irq_save(irq_flags
);
1579 /* Safe to update per-cpu structure without lock, but
1580 * in the restart case we must be careful to reaquire
1581 * the local magazine since this may have changed
1582 * when we need to grow the cache. */
1583 skm
= skc
->skc_mag
[smp_processor_id()];
1584 ASSERTF(skm
->skm_magic
== SKM_MAGIC
, "%x != %x: %s/%p/%p %x/%x/%x\n",
1585 skm
->skm_magic
, SKM_MAGIC
, skc
->skc_name
, skc
, skm
,
1586 skm
->skm_size
, skm
->skm_refill
, skm
->skm_avail
);
1588 if (likely(skm
->skm_avail
)) {
1589 /* Object available in CPU cache, use it */
1590 obj
= skm
->skm_objs
[--skm
->skm_avail
];
1591 skm
->skm_age
= jiffies
;
1593 /* Per-CPU cache empty, directly allocate from
1594 * the slab and refill the per-CPU cache. */
1595 (void)spl_cache_refill(skc
, skm
, flags
);
1596 GOTO(restart
, obj
= NULL
);
1599 local_irq_restore(irq_flags
);
1601 ASSERT(((unsigned long)(obj
) % skc
->skc_obj_align
) == 0);
1603 /* Pre-emptively migrate object to CPU L1 cache */
1605 atomic_dec(&skc
->skc_ref
);
1609 EXPORT_SYMBOL(spl_kmem_cache_alloc
);
1612 * Free an object back to the local per-cpu magazine, there is no
1613 * guarantee that this is the same magazine the object was originally
1614 * allocated from. We may need to flush entire from the magazine
1615 * back to the slabs to make space.
1618 spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
)
1620 spl_kmem_magazine_t
*skm
;
1621 unsigned long flags
;
1624 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1625 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1626 atomic_inc(&skc
->skc_ref
);
1627 local_irq_save(flags
);
1629 /* Safe to update per-cpu structure without lock, but
1630 * no remote memory allocation tracking is being performed
1631 * it is entirely possible to allocate an object from one
1632 * CPU cache and return it to another. */
1633 skm
= skc
->skc_mag
[smp_processor_id()];
1634 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1636 /* Per-CPU cache full, flush it to make space */
1637 if (unlikely(skm
->skm_avail
>= skm
->skm_size
))
1638 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1640 /* Available space in cache, use it */
1641 skm
->skm_objs
[skm
->skm_avail
++] = obj
;
1643 local_irq_restore(flags
);
1644 atomic_dec(&skc
->skc_ref
);
1648 EXPORT_SYMBOL(spl_kmem_cache_free
);
1651 * The generic shrinker function for all caches. Under linux a shrinker
1652 * may not be tightly coupled with a slab cache. In fact linux always
1653 * systematically trys calling all registered shrinker callbacks which
1654 * report that they contain unused objects. Because of this we only
1655 * register one shrinker function in the shim layer for all slab caches.
1656 * We always attempt to shrink all caches when this generic shrinker
1657 * is called. The shrinker should return the number of free objects
1658 * in the cache when called with nr_to_scan == 0 but not attempt to
1659 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
1660 * objects should be freed, because Solaris semantics are to free
1661 * all available objects we may free more objects than requested.
1664 spl_kmem_cache_generic_shrinker(int nr_to_scan
, unsigned int gfp_mask
)
1666 spl_kmem_cache_t
*skc
;
1669 down_read(&spl_kmem_cache_sem
);
1670 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
1672 spl_kmem_cache_reap_now(skc
);
1675 * Presume everything alloc'ed in reclaimable, this ensures
1676 * we are called again with nr_to_scan > 0 so can try and
1677 * reclaim. The exact number is not important either so
1678 * we forgo taking this already highly contented lock.
1680 unused
+= skc
->skc_obj_alloc
;
1682 up_read(&spl_kmem_cache_sem
);
1684 return (unused
* sysctl_vfs_cache_pressure
) / 100;
1688 * Call the registered reclaim function for a cache. Depending on how
1689 * many and which objects are released it may simply repopulate the
1690 * local magazine which will then need to age-out. Objects which cannot
1691 * fit in the magazine we will be released back to their slabs which will
1692 * also need to age out before being release. This is all just best
1693 * effort and we do not want to thrash creating and destroying slabs.
1696 spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
)
1700 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1701 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1703 /* Prevent concurrent cache reaping when contended */
1704 if (test_and_set_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1709 atomic_inc(&skc
->skc_ref
);
1711 if (skc
->skc_reclaim
)
1712 skc
->skc_reclaim(skc
->skc_private
);
1714 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
1715 clear_bit(KMC_BIT_REAPING
, &skc
->skc_flags
);
1716 atomic_dec(&skc
->skc_ref
);
1720 EXPORT_SYMBOL(spl_kmem_cache_reap_now
);
1723 * Reap all free slabs from all registered caches.
1728 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK
, GFP_KERNEL
);
1730 EXPORT_SYMBOL(spl_kmem_reap
);
1732 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
1734 spl_sprintf_addr(kmem_debug_t
*kd
, char *str
, int len
, int min
)
1736 int size
= ((len
- 1) < kd
->kd_size
) ? (len
- 1) : kd
->kd_size
;
1739 ASSERT(str
!= NULL
&& len
>= 17);
1740 memset(str
, 0, len
);
1742 /* Check for a fully printable string, and while we are at
1743 * it place the printable characters in the passed buffer. */
1744 for (i
= 0; i
< size
; i
++) {
1745 str
[i
] = ((char *)(kd
->kd_addr
))[i
];
1746 if (isprint(str
[i
])) {
1749 /* Minimum number of printable characters found
1750 * to make it worthwhile to print this as ascii. */
1760 sprintf(str
, "%02x%02x%02x%02x%02x%02x%02x%02x",
1761 *((uint8_t *)kd
->kd_addr
),
1762 *((uint8_t *)kd
->kd_addr
+ 2),
1763 *((uint8_t *)kd
->kd_addr
+ 4),
1764 *((uint8_t *)kd
->kd_addr
+ 6),
1765 *((uint8_t *)kd
->kd_addr
+ 8),
1766 *((uint8_t *)kd
->kd_addr
+ 10),
1767 *((uint8_t *)kd
->kd_addr
+ 12),
1768 *((uint8_t *)kd
->kd_addr
+ 14));
1775 spl_kmem_init_tracking(struct list_head
*list
, spinlock_t
*lock
, int size
)
1780 spin_lock_init(lock
);
1781 INIT_LIST_HEAD(list
);
1783 for (i
= 0; i
< size
; i
++)
1784 INIT_HLIST_HEAD(&kmem_table
[i
]);
1790 spl_kmem_fini_tracking(struct list_head
*list
, spinlock_t
*lock
)
1792 unsigned long flags
;
1797 spin_lock_irqsave(lock
, flags
);
1798 if (!list_empty(list
))
1799 printk(KERN_WARNING
"%-16s %-5s %-16s %s:%s\n", "address",
1800 "size", "data", "func", "line");
1802 list_for_each_entry(kd
, list
, kd_list
)
1803 printk(KERN_WARNING
"%p %-5d %-16s %s:%d\n", kd
->kd_addr
,
1804 (int)kd
->kd_size
, spl_sprintf_addr(kd
, str
, 17, 8),
1805 kd
->kd_func
, kd
->kd_line
);
1807 spin_unlock_irqrestore(lock
, flags
);
1810 #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1811 #define spl_kmem_init_tracking(list, lock, size)
1812 #define spl_kmem_fini_tracking(list, lock)
1813 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1816 spl_kmem_init_globals(void)
1820 /* For now all zones are includes, it may be wise to restrict
1821 * this to normal and highmem zones if we see problems. */
1822 for_each_zone(zone
) {
1824 if (!populated_zone(zone
))
1827 minfree
+= zone
->pages_min
;
1828 desfree
+= zone
->pages_low
;
1829 lotsfree
+= zone
->pages_high
;
1832 /* Solaris default values */
1833 swapfs_minfree
= MAX(2*1024*1024 >> PAGE_SHIFT
, physmem
>> 3);
1834 swapfs_reserve
= MIN(4*1024*1024 >> PAGE_SHIFT
, physmem
>> 4);
1838 * Called at module init when it is safe to use spl_kallsyms_lookup_name()
1841 spl_kmem_init_kallsyms_lookup(void)
1843 #ifndef HAVE_GET_VMALLOC_INFO
1844 get_vmalloc_info_fn
= (get_vmalloc_info_t
)
1845 spl_kallsyms_lookup_name("get_vmalloc_info");
1846 if (!get_vmalloc_info_fn
) {
1847 printk(KERN_ERR
"Error: Unknown symbol get_vmalloc_info\n");
1850 #endif /* HAVE_GET_VMALLOC_INFO */
1852 #ifdef HAVE_PGDAT_HELPERS
1853 # ifndef HAVE_FIRST_ONLINE_PGDAT
1854 first_online_pgdat_fn
= (first_online_pgdat_t
)
1855 spl_kallsyms_lookup_name("first_online_pgdat");
1856 if (!first_online_pgdat_fn
) {
1857 printk(KERN_ERR
"Error: Unknown symbol first_online_pgdat\n");
1860 # endif /* HAVE_FIRST_ONLINE_PGDAT */
1862 # ifndef HAVE_NEXT_ONLINE_PGDAT
1863 next_online_pgdat_fn
= (next_online_pgdat_t
)
1864 spl_kallsyms_lookup_name("next_online_pgdat");
1865 if (!next_online_pgdat_fn
) {
1866 printk(KERN_ERR
"Error: Unknown symbol next_online_pgdat\n");
1869 # endif /* HAVE_NEXT_ONLINE_PGDAT */
1871 # ifndef HAVE_NEXT_ZONE
1872 next_zone_fn
= (next_zone_t
)
1873 spl_kallsyms_lookup_name("next_zone");
1874 if (!next_zone_fn
) {
1875 printk(KERN_ERR
"Error: Unknown symbol next_zone\n");
1878 # endif /* HAVE_NEXT_ZONE */
1880 #else /* HAVE_PGDAT_HELPERS */
1882 # ifndef HAVE_PGDAT_LIST
1883 pgdat_list_addr
= *(struct pglist_data
**)
1884 spl_kallsyms_lookup_name("pgdat_list");
1885 if (!pgdat_list_addr
) {
1886 printk(KERN_ERR
"Error: Unknown symbol pgdat_list\n");
1889 # endif /* HAVE_PGDAT_LIST */
1890 #endif /* HAVE_PGDAT_HELPERS */
1892 #if defined(NEED_GET_ZONE_COUNTS) && !defined(HAVE_GET_ZONE_COUNTS)
1893 get_zone_counts_fn
= (get_zone_counts_t
)
1894 spl_kallsyms_lookup_name("get_zone_counts");
1895 if (!get_zone_counts_fn
) {
1896 printk(KERN_ERR
"Error: Unknown symbol get_zone_counts\n");
1899 #endif /* NEED_GET_ZONE_COUNTS && !HAVE_GET_ZONE_COUNTS */
1902 * It is now safe to initialize the global tunings which rely on
1903 * the use of the for_each_zone() macro. This macro in turns
1904 * depends on the *_pgdat symbols which are now available.
1906 spl_kmem_init_globals();
1917 init_rwsem(&spl_kmem_cache_sem
);
1918 INIT_LIST_HEAD(&spl_kmem_cache_list
);
1920 #ifdef HAVE_SET_SHRINKER
1921 spl_kmem_cache_shrinker
= set_shrinker(KMC_DEFAULT_SEEKS
,
1922 spl_kmem_cache_generic_shrinker
);
1923 if (spl_kmem_cache_shrinker
== NULL
)
1924 RETURN(rc
= -ENOMEM
);
1926 register_shrinker(&spl_kmem_cache_shrinker
);
1930 atomic64_set(&kmem_alloc_used
, 0);
1931 atomic64_set(&vmem_alloc_used
, 0);
1933 spl_kmem_init_tracking(&kmem_list
, &kmem_lock
, KMEM_TABLE_SIZE
);
1934 spl_kmem_init_tracking(&vmem_list
, &vmem_lock
, VMEM_TABLE_SIZE
);
1943 /* Display all unreclaimed memory addresses, including the
1944 * allocation size and the first few bytes of what's located
1945 * at that address to aid in debugging. Performance is not
1946 * a serious concern here since it is module unload time. */
1947 if (atomic64_read(&kmem_alloc_used
) != 0)
1948 CWARN("kmem leaked %ld/%ld bytes\n",
1949 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
1952 if (atomic64_read(&vmem_alloc_used
) != 0)
1953 CWARN("vmem leaked %ld/%ld bytes\n",
1954 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
1956 spl_kmem_fini_tracking(&kmem_list
, &kmem_lock
);
1957 spl_kmem_fini_tracking(&vmem_list
, &vmem_lock
);
1958 #endif /* DEBUG_KMEM */
1961 #ifdef HAVE_SET_SHRINKER
1962 remove_shrinker(spl_kmem_cache_shrinker
);
1964 unregister_shrinker(&spl_kmem_cache_shrinker
);