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_desfree
= 0;
68 EXPORT_SYMBOL(swapfs_desfree
);
70 pgcnt_t swapfs_minfree
= 0;
71 EXPORT_SYMBOL(swapfs_minfree
);
73 pgcnt_t swapfs_reserve
= 0;
74 EXPORT_SYMBOL(swapfs_reserve
);
76 pgcnt_t availrmem
= 0;
77 EXPORT_SYMBOL(availrmem
);
79 vmem_t
*heap_arena
= NULL
;
80 EXPORT_SYMBOL(heap_arena
);
82 vmem_t
*zio_alloc_arena
= NULL
;
83 EXPORT_SYMBOL(zio_alloc_arena
);
85 vmem_t
*zio_arena
= NULL
;
86 EXPORT_SYMBOL(zio_arena
);
88 #ifndef HAVE_FIRST_ONLINE_PGDAT
89 struct pglist_data
*first_online_pgdat(void)
91 return NODE_DATA(first_online_node
);
93 #endif /* HAVE_FIRST_ONLINE_PGDAT */
95 #ifndef HAVE_NEXT_ONLINE_PGDAT
96 struct pglist_data
*next_online_pgdat(struct pglist_data
*pgdat
)
98 int nid
= next_online_node(pgdat
->node_id
);
100 if (nid
== MAX_NUMNODES
)
103 return NODE_DATA(nid
);
105 #endif /* HAVE_NEXT_ONLINE_PGDAT */
107 #ifndef HAVE_NEXT_ZONE
108 struct zone
*next_zone(struct zone
*zone
)
110 pg_data_t
*pgdat
= zone
->zone_pgdat
;
112 if (zone
< pgdat
->node_zones
+ MAX_NR_ZONES
- 1)
115 pgdat
= next_online_pgdat(pgdat
);
117 zone
= pgdat
->node_zones
;
123 #endif /* HAVE_NEXT_ZONE */
126 * Memory allocation interfaces and debugging for basic kmem_*
127 * and vmem_* style memory allocation. When DEBUG_KMEM is enable
128 * all allocations will be tracked when they are allocated and
129 * freed. When the SPL module is unload a list of all leaked
130 * addresses and where they were allocated will be dumped to the
131 * console. Enabling this feature has a significant impant on
132 * performance but it makes finding memory leaks staight forward.
135 /* Shim layer memory accounting */
136 atomic64_t kmem_alloc_used
= ATOMIC64_INIT(0);
137 unsigned long long kmem_alloc_max
= 0;
138 atomic64_t vmem_alloc_used
= ATOMIC64_INIT(0);
139 unsigned long long vmem_alloc_max
= 0;
140 int kmem_warning_flag
= 1;
142 EXPORT_SYMBOL(kmem_alloc_used
);
143 EXPORT_SYMBOL(kmem_alloc_max
);
144 EXPORT_SYMBOL(vmem_alloc_used
);
145 EXPORT_SYMBOL(vmem_alloc_max
);
146 EXPORT_SYMBOL(kmem_warning_flag
);
148 # ifdef DEBUG_KMEM_TRACKING
150 /* XXX - Not to surprisingly with debugging enabled the xmem_locks are very
151 * highly contended particularly on xfree(). If we want to run with this
152 * detailed debugging enabled for anything other than debugging we need to
153 * minimize the contention by moving to a lock per xmem_table entry model.
156 # define KMEM_HASH_BITS 10
157 # define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
159 # define VMEM_HASH_BITS 10
160 # define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS)
162 typedef struct kmem_debug
{
163 struct hlist_node kd_hlist
; /* Hash node linkage */
164 struct list_head kd_list
; /* List of all allocations */
165 void *kd_addr
; /* Allocation pointer */
166 size_t kd_size
; /* Allocation size */
167 const char *kd_func
; /* Allocation function */
168 int kd_line
; /* Allocation line */
171 spinlock_t kmem_lock
;
172 struct hlist_head kmem_table
[KMEM_TABLE_SIZE
];
173 struct list_head kmem_list
;
175 spinlock_t vmem_lock
;
176 struct hlist_head vmem_table
[VMEM_TABLE_SIZE
];
177 struct list_head vmem_list
;
179 EXPORT_SYMBOL(kmem_lock
);
180 EXPORT_SYMBOL(kmem_table
);
181 EXPORT_SYMBOL(kmem_list
);
183 EXPORT_SYMBOL(vmem_lock
);
184 EXPORT_SYMBOL(vmem_table
);
185 EXPORT_SYMBOL(vmem_list
);
188 int kmem_set_warning(int flag
) { return (kmem_warning_flag
= !!flag
); }
190 int kmem_set_warning(int flag
) { return 0; }
192 EXPORT_SYMBOL(kmem_set_warning
);
195 * Slab allocation interfaces
197 * While the Linux slab implementation was inspired by the Solaris
198 * implemenation I cannot use it to emulate the Solaris APIs. I
199 * require two features which are not provided by the Linux slab.
201 * 1) Constructors AND destructors. Recent versions of the Linux
202 * kernel have removed support for destructors. This is a deal
203 * breaker for the SPL which contains particularly expensive
204 * initializers for mutex's, condition variables, etc. We also
205 * require a minimal level of cleanup for these data types unlike
206 * many Linux data type which do need to be explicitly destroyed.
208 * 2) Virtual address space backed slab. Callers of the Solaris slab
209 * expect it to work well for both small are very large allocations.
210 * Because of memory fragmentation the Linux slab which is backed
211 * by kmalloc'ed memory performs very badly when confronted with
212 * large numbers of large allocations. Basing the slab on the
213 * virtual address space removes the need for contigeous pages
214 * and greatly improve performance for large allocations.
216 * For these reasons, the SPL has its own slab implementation with
217 * the needed features. It is not as highly optimized as either the
218 * Solaris or Linux slabs, but it should get me most of what is
219 * needed until it can be optimized or obsoleted by another approach.
221 * One serious concern I do have about this method is the relatively
222 * small virtual address space on 32bit arches. This will seriously
223 * constrain the size of the slab caches and their performance.
225 * XXX: Improve the partial slab list by carefully maintaining a
226 * strict ordering of fullest to emptiest slabs based on
227 * the slab reference count. This gaurentees the when freeing
228 * slabs back to the system we need only linearly traverse the
229 * last N slabs in the list to discover all the freeable slabs.
231 * XXX: NUMA awareness for optionally allocating memory close to a
232 * particular core. This can be adventageous if you know the slab
233 * object will be short lived and primarily accessed from one core.
235 * XXX: Slab coloring may also yield performance improvements and would
236 * be desirable to implement.
239 struct list_head spl_kmem_cache_list
; /* List of caches */
240 struct rw_semaphore spl_kmem_cache_sem
; /* Cache list lock */
242 static int spl_cache_flush(spl_kmem_cache_t
*skc
,
243 spl_kmem_magazine_t
*skm
, int flush
);
245 #ifdef HAVE_SET_SHRINKER
246 static struct shrinker
*spl_kmem_cache_shrinker
;
248 static int spl_kmem_cache_generic_shrinker(int nr_to_scan
,
249 unsigned int gfp_mask
);
250 static struct shrinker spl_kmem_cache_shrinker
= {
251 .shrink
= spl_kmem_cache_generic_shrinker
,
252 .seeks
= KMC_DEFAULT_SEEKS
,
257 # ifdef DEBUG_KMEM_TRACKING
259 static kmem_debug_t
*
260 kmem_del_init(spinlock_t
*lock
, struct hlist_head
*table
, int bits
,
263 struct hlist_head
*head
;
264 struct hlist_node
*node
;
265 struct kmem_debug
*p
;
269 spin_lock_irqsave(lock
, flags
);
271 head
= &table
[hash_ptr(addr
, bits
)];
272 hlist_for_each_entry_rcu(p
, node
, head
, kd_hlist
) {
273 if (p
->kd_addr
== addr
) {
274 hlist_del_init(&p
->kd_hlist
);
275 list_del_init(&p
->kd_list
);
276 spin_unlock_irqrestore(lock
, flags
);
281 spin_unlock_irqrestore(lock
, flags
);
287 kmem_alloc_track(size_t size
, int flags
, const char *func
, int line
,
288 int node_alloc
, int node
)
292 unsigned long irq_flags
;
295 dptr
= (kmem_debug_t
*) kmalloc(sizeof(kmem_debug_t
),
296 flags
& ~__GFP_ZERO
);
299 CWARN("kmem_alloc(%ld, 0x%x) debug failed\n",
300 sizeof(kmem_debug_t
), flags
);
302 /* Marked unlikely because we should never be doing this,
303 * we tolerate to up 2 pages but a single page is best. */
304 if (unlikely((size
) > (PAGE_SIZE
* 2)) && kmem_warning_flag
)
305 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
306 (unsigned long long) size
, flags
,
307 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
309 /* We use kstrdup() below because the string pointed to by
310 * __FUNCTION__ might not be available by the time we want
311 * to print it since the module might have been unloaded. */
312 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
313 if (unlikely(dptr
->kd_func
== NULL
)) {
315 CWARN("kstrdup() failed in kmem_alloc(%llu, 0x%x) "
316 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
317 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
321 /* Use the correct allocator */
323 ASSERT(!(flags
& __GFP_ZERO
));
324 ptr
= kmalloc_node(size
, flags
, node
);
325 } else if (flags
& __GFP_ZERO
) {
326 ptr
= kzalloc(size
, flags
& ~__GFP_ZERO
);
328 ptr
= kmalloc(size
, flags
);
331 if (unlikely(ptr
== NULL
)) {
332 kfree(dptr
->kd_func
);
334 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
335 (unsigned long long) size
, flags
,
336 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
340 atomic64_add(size
, &kmem_alloc_used
);
341 if (unlikely(atomic64_read(&kmem_alloc_used
) >
344 atomic64_read(&kmem_alloc_used
);
346 INIT_HLIST_NODE(&dptr
->kd_hlist
);
347 INIT_LIST_HEAD(&dptr
->kd_list
);
350 dptr
->kd_size
= size
;
351 dptr
->kd_line
= line
;
353 spin_lock_irqsave(&kmem_lock
, irq_flags
);
354 hlist_add_head_rcu(&dptr
->kd_hlist
,
355 &kmem_table
[hash_ptr(ptr
, KMEM_HASH_BITS
)]);
356 list_add_tail(&dptr
->kd_list
, &kmem_list
);
357 spin_unlock_irqrestore(&kmem_lock
, irq_flags
);
359 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
360 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
361 ptr
, atomic64_read(&kmem_alloc_used
),
367 EXPORT_SYMBOL(kmem_alloc_track
);
370 kmem_free_track(void *ptr
, size_t size
)
375 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
376 (unsigned long long) size
);
378 dptr
= kmem_del_init(&kmem_lock
, kmem_table
, KMEM_HASH_BITS
, ptr
);
380 ASSERT(dptr
); /* Must exist in hash due to kmem_alloc() */
382 /* Size must match */
383 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
384 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
385 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
387 atomic64_sub(size
, &kmem_alloc_used
);
389 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
390 (unsigned long long) size
, atomic64_read(&kmem_alloc_used
),
393 kfree(dptr
->kd_func
);
395 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
398 memset(ptr
, 0x5a, size
);
403 EXPORT_SYMBOL(kmem_free_track
);
406 vmem_alloc_track(size_t size
, int flags
, const char *func
, int line
)
410 unsigned long irq_flags
;
413 ASSERT(flags
& KM_SLEEP
);
415 dptr
= (kmem_debug_t
*) kmalloc(sizeof(kmem_debug_t
), flags
);
417 CWARN("vmem_alloc(%ld, 0x%x) debug failed\n",
418 sizeof(kmem_debug_t
), flags
);
420 /* We use kstrdup() below because the string pointed to by
421 * __FUNCTION__ might not be available by the time we want
422 * to print it, since the module might have been unloaded. */
423 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
424 if (unlikely(dptr
->kd_func
== NULL
)) {
426 CWARN("kstrdup() failed in vmem_alloc(%llu, 0x%x) "
427 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
428 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
432 ptr
= __vmalloc(size
, (flags
| __GFP_HIGHMEM
) & ~__GFP_ZERO
,
435 if (unlikely(ptr
== NULL
)) {
436 kfree(dptr
->kd_func
);
438 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
439 (unsigned long long) size
, flags
,
440 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
444 if (flags
& __GFP_ZERO
)
445 memset(ptr
, 0, size
);
447 atomic64_add(size
, &vmem_alloc_used
);
448 if (unlikely(atomic64_read(&vmem_alloc_used
) >
451 atomic64_read(&vmem_alloc_used
);
453 INIT_HLIST_NODE(&dptr
->kd_hlist
);
454 INIT_LIST_HEAD(&dptr
->kd_list
);
457 dptr
->kd_size
= size
;
458 dptr
->kd_line
= line
;
460 spin_lock_irqsave(&vmem_lock
, irq_flags
);
461 hlist_add_head_rcu(&dptr
->kd_hlist
,
462 &vmem_table
[hash_ptr(ptr
, VMEM_HASH_BITS
)]);
463 list_add_tail(&dptr
->kd_list
, &vmem_list
);
464 spin_unlock_irqrestore(&vmem_lock
, irq_flags
);
466 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
467 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
468 ptr
, atomic64_read(&vmem_alloc_used
),
474 EXPORT_SYMBOL(vmem_alloc_track
);
477 vmem_free_track(void *ptr
, size_t size
)
482 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
483 (unsigned long long) size
);
485 dptr
= kmem_del_init(&vmem_lock
, vmem_table
, VMEM_HASH_BITS
, ptr
);
486 ASSERT(dptr
); /* Must exist in hash due to vmem_alloc() */
488 /* Size must match */
489 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
490 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
491 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
493 atomic64_sub(size
, &vmem_alloc_used
);
494 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
495 (unsigned long long) size
, atomic64_read(&vmem_alloc_used
),
498 kfree(dptr
->kd_func
);
500 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
503 memset(ptr
, 0x5a, size
);
508 EXPORT_SYMBOL(vmem_free_track
);
510 # else /* DEBUG_KMEM_TRACKING */
513 kmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
,
514 int node_alloc
, int node
)
519 /* Marked unlikely because we should never be doing this,
520 * we tolerate to up 2 pages but a single page is best. */
521 if (unlikely(size
> (PAGE_SIZE
* 2)) && kmem_warning_flag
)
522 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
523 (unsigned long long) size
, flags
,
524 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
526 /* Use the correct allocator */
528 ASSERT(!(flags
& __GFP_ZERO
));
529 ptr
= kmalloc_node(size
, flags
, node
);
530 } else if (flags
& __GFP_ZERO
) {
531 ptr
= kzalloc(size
, flags
& (~__GFP_ZERO
));
533 ptr
= kmalloc(size
, flags
);
537 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
538 (unsigned long long) size
, flags
,
539 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
541 atomic64_add(size
, &kmem_alloc_used
);
542 if (unlikely(atomic64_read(&kmem_alloc_used
) > kmem_alloc_max
))
543 kmem_alloc_max
= atomic64_read(&kmem_alloc_used
);
545 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
546 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
547 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
551 EXPORT_SYMBOL(kmem_alloc_debug
);
554 kmem_free_debug(void *ptr
, size_t size
)
558 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
559 (unsigned long long) size
);
561 atomic64_sub(size
, &kmem_alloc_used
);
563 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
564 (unsigned long long) size
, atomic64_read(&kmem_alloc_used
),
567 memset(ptr
, 0x5a, size
);
572 EXPORT_SYMBOL(kmem_free_debug
);
575 vmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
)
580 ASSERT(flags
& KM_SLEEP
);
582 ptr
= __vmalloc(size
, (flags
| __GFP_HIGHMEM
) & ~__GFP_ZERO
,
585 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
586 (unsigned long long) size
, flags
,
587 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
589 if (flags
& __GFP_ZERO
)
590 memset(ptr
, 0, size
);
592 atomic64_add(size
, &vmem_alloc_used
);
594 if (unlikely(atomic64_read(&vmem_alloc_used
) > vmem_alloc_max
))
595 vmem_alloc_max
= atomic64_read(&vmem_alloc_used
);
597 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
598 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
599 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
604 EXPORT_SYMBOL(vmem_alloc_debug
);
607 vmem_free_debug(void *ptr
, size_t size
)
611 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
612 (unsigned long long) size
);
614 atomic64_sub(size
, &vmem_alloc_used
);
616 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
617 (unsigned long long) size
, atomic64_read(&vmem_alloc_used
),
620 memset(ptr
, 0x5a, size
);
625 EXPORT_SYMBOL(vmem_free_debug
);
627 # endif /* DEBUG_KMEM_TRACKING */
628 #endif /* DEBUG_KMEM */
631 kv_alloc(spl_kmem_cache_t
*skc
, int size
, int flags
)
635 if (skc
->skc_flags
& KMC_KMEM
) {
636 if (size
> (2 * PAGE_SIZE
)) {
637 ptr
= (void *)__get_free_pages(flags
, get_order(size
));
639 ptr
= kmem_alloc(size
, flags
);
641 ptr
= vmem_alloc(size
, flags
);
648 kv_free(spl_kmem_cache_t
*skc
, void *ptr
, int size
)
650 if (skc
->skc_flags
& KMC_KMEM
) {
651 if (size
> (2 * PAGE_SIZE
))
652 free_pages((unsigned long)ptr
, get_order(size
));
654 kmem_free(ptr
, size
);
656 vmem_free(ptr
, size
);
661 * It's important that we pack the spl_kmem_obj_t structure and the
662 * actual objects in to one large address space to minimize the number
663 * of calls to the allocator. It is far better to do a few large
664 * allocations and then subdivide it ourselves. Now which allocator
665 * we use requires balancing a few trade offs.
667 * For small objects we use kmem_alloc() because as long as you are
668 * only requesting a small number of pages (ideally just one) its cheap.
669 * However, when you start requesting multiple pages with kmem_alloc()
670 * it gets increasingly expensive since it requires contigeous pages.
671 * For this reason we shift to vmem_alloc() for slabs of large objects
672 * which removes the need for contigeous pages. We do not use
673 * vmem_alloc() in all cases because there is significant locking
674 * overhead in __get_vm_area_node(). This function takes a single
675 * global lock when aquiring an available virtual address range which
676 * serializes all vmem_alloc()'s for all slab caches. Using slightly
677 * different allocation functions for small and large objects should
678 * give us the best of both worlds.
680 * KMC_ONSLAB KMC_OFFSLAB
682 * +------------------------+ +-----------------+
683 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
684 * | skc_obj_size <-+ | | +-----------------+ | |
685 * | spl_kmem_obj_t | | | |
686 * | skc_obj_size <---+ | +-----------------+ | |
687 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
688 * | ... v | | spl_kmem_obj_t | |
689 * +------------------------+ +-----------------+ v
691 static spl_kmem_slab_t
*
692 spl_slab_alloc(spl_kmem_cache_t
*skc
, int flags
)
694 spl_kmem_slab_t
*sks
;
695 spl_kmem_obj_t
*sko
, *n
;
697 int i
, align
, size
, rc
= 0;
699 base
= kv_alloc(skc
, skc
->skc_slab_size
, flags
);
703 sks
= (spl_kmem_slab_t
*)base
;
704 sks
->sks_magic
= SKS_MAGIC
;
705 sks
->sks_objs
= skc
->skc_slab_objs
;
706 sks
->sks_age
= jiffies
;
707 sks
->sks_cache
= skc
;
708 INIT_LIST_HEAD(&sks
->sks_list
);
709 INIT_LIST_HEAD(&sks
->sks_free_list
);
712 align
= skc
->skc_obj_align
;
713 size
= P2ROUNDUP(skc
->skc_obj_size
, align
) +
714 P2ROUNDUP(sizeof(spl_kmem_obj_t
), align
);
716 for (i
= 0; i
< sks
->sks_objs
; i
++) {
717 if (skc
->skc_flags
& KMC_OFFSLAB
) {
718 obj
= kv_alloc(skc
, size
, flags
);
720 GOTO(out
, rc
= -ENOMEM
);
723 P2ROUNDUP(sizeof(spl_kmem_slab_t
), align
) +
727 sko
= obj
+ P2ROUNDUP(skc
->skc_obj_size
, align
);
729 sko
->sko_magic
= SKO_MAGIC
;
731 INIT_LIST_HEAD(&sko
->sko_list
);
732 list_add_tail(&sko
->sko_list
, &sks
->sks_free_list
);
735 list_for_each_entry(sko
, &sks
->sks_free_list
, sko_list
)
737 skc
->skc_ctor(sko
->sko_addr
, skc
->skc_private
, flags
);
740 if (skc
->skc_flags
& KMC_OFFSLAB
)
741 list_for_each_entry_safe(sko
, n
, &sks
->sks_free_list
,
743 kv_free(skc
, sko
->sko_addr
, size
);
745 kv_free(skc
, base
, skc
->skc_slab_size
);
753 * Remove a slab from complete or partial list, it must be called with
754 * the 'skc->skc_lock' held but the actual free must be performed
755 * outside the lock to prevent deadlocking on vmem addresses.
758 spl_slab_free(spl_kmem_slab_t
*sks
,
759 struct list_head
*sks_list
, struct list_head
*sko_list
)
761 spl_kmem_cache_t
*skc
;
762 spl_kmem_obj_t
*sko
, *n
;
765 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
766 ASSERT(sks
->sks_ref
== 0);
768 skc
= sks
->sks_cache
;
769 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
770 ASSERT(spin_is_locked(&skc
->skc_lock
));
772 skc
->skc_obj_total
-= sks
->sks_objs
;
773 skc
->skc_slab_total
--;
774 list_del(&sks
->sks_list
);
776 /* Run destructors slab is being released */
777 list_for_each_entry_safe(sko
, n
, &sks
->sks_free_list
, sko_list
) {
778 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
779 list_del(&sko
->sko_list
);
782 skc
->skc_dtor(sko
->sko_addr
, skc
->skc_private
);
784 if (skc
->skc_flags
& KMC_OFFSLAB
)
785 list_add(&sko
->sko_list
, sko_list
);
788 list_add(&sks
->sks_list
, sks_list
);
793 * Traverses all the partial slabs attached to a cache and free those
794 * which which are currently empty, and have not been touched for
795 * skc_delay seconds. This is to avoid thrashing.
798 spl_slab_reclaim(spl_kmem_cache_t
*skc
, int flag
)
800 spl_kmem_slab_t
*sks
, *m
;
801 spl_kmem_obj_t
*sko
, *n
;
808 * Move empty slabs and objects which have not been touched in
809 * skc_delay seconds on to private lists to be freed outside
810 * the spin lock. This delay time is important to avoid
811 * thrashing however when flag is set the delay will not be
812 * used. Empty slabs will be at the end of the skc_partial_list.
814 spin_lock(&skc
->skc_lock
);
815 list_for_each_entry_safe_reverse(sks
, m
, &skc
->skc_partial_list
,
817 if (sks
->sks_ref
> 0)
820 if (flag
|| time_after(jiffies
,sks
->sks_age
+skc
->skc_delay
*HZ
))
821 spl_slab_free(sks
, &sks_list
, &sko_list
);
823 spin_unlock(&skc
->skc_lock
);
826 * We only have list of spl_kmem_obj_t's if they are located off
827 * the slab, otherwise they get feed with the spl_kmem_slab_t.
829 if (!list_empty(&sko_list
)) {
830 ASSERT(skc
->skc_flags
& KMC_OFFSLAB
);
832 size
= P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
) +
833 P2ROUNDUP(sizeof(spl_kmem_obj_t
), skc
->skc_obj_align
);
835 list_for_each_entry_safe(sko
, n
, &sko_list
, sko_list
)
836 kv_free(skc
, sko
->sko_addr
, size
);
839 list_for_each_entry_safe(sks
, m
, &sks_list
, sks_list
)
840 kv_free(skc
, sks
, skc
->skc_slab_size
);
846 * Called regularly on all caches to age objects out of the magazines
847 * which have not been access in skc->skc_delay seconds. This prevents
848 * idle magazines from holding memory which might be better used by
849 * other caches or parts of the system. The delay is present to
850 * prevent thrashing the magazine.
853 spl_magazine_age(void *data
)
855 spl_kmem_cache_t
*skc
= data
;
856 spl_kmem_magazine_t
*skm
= skc
->skc_mag
[smp_processor_id()];
858 if (skm
->skm_avail
> 0 &&
859 time_after(jiffies
, skm
->skm_age
+ skc
->skc_delay
* HZ
))
860 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
864 * Called regularly to keep a downward pressure on the size of idle
865 * magazines and to release free slabs from the cache. This function
866 * never calls the registered reclaim function, that only occures
867 * under memory pressure or with a direct call to spl_kmem_reap().
870 spl_cache_age(void *data
)
872 spl_kmem_cache_t
*skc
=
873 spl_get_work_data(data
, spl_kmem_cache_t
, skc_work
.work
);
875 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
876 spl_on_each_cpu(spl_magazine_age
, skc
, 1);
877 spl_slab_reclaim(skc
, 0);
879 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
880 schedule_delayed_work(&skc
->skc_work
, 2 * skc
->skc_delay
* HZ
);
884 * Size a slab based on the size of each aliged object plus spl_kmem_obj_t.
885 * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However,
886 * for very small objects we may end up with more than this so as not
887 * to waste space in the minimal allocation of a single page. Also for
888 * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN,
889 * lower than this and we will fail.
892 spl_slab_size(spl_kmem_cache_t
*skc
, uint32_t *objs
, uint32_t *size
)
894 int sks_size
, obj_size
, max_size
, align
;
896 if (skc
->skc_flags
& KMC_OFFSLAB
) {
897 *objs
= SPL_KMEM_CACHE_OBJ_PER_SLAB
;
898 *size
= sizeof(spl_kmem_slab_t
);
900 align
= skc
->skc_obj_align
;
901 sks_size
= P2ROUNDUP(sizeof(spl_kmem_slab_t
), align
);
902 obj_size
= P2ROUNDUP(skc
->skc_obj_size
, align
) +
903 P2ROUNDUP(sizeof(spl_kmem_obj_t
), align
);
905 if (skc
->skc_flags
& KMC_KMEM
)
906 max_size
= ((uint64_t)1 << (MAX_ORDER
-1)) * PAGE_SIZE
;
908 max_size
= (32 * 1024 * 1024);
910 for (*size
= PAGE_SIZE
; *size
<= max_size
; *size
+= PAGE_SIZE
) {
911 *objs
= (*size
- sks_size
) / obj_size
;
912 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB
)
917 * Unable to satisfy target objets per slab, fallback to
918 * allocating a maximally sized slab and assuming it can
919 * contain the minimum objects count use it. If not fail.
922 *objs
= (*size
- sks_size
) / obj_size
;
923 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN
)
931 * Make a guess at reasonable per-cpu magazine size based on the size of
932 * each object and the cost of caching N of them in each magazine. Long
933 * term this should really adapt based on an observed usage heuristic.
936 spl_magazine_size(spl_kmem_cache_t
*skc
)
938 int size
, align
= skc
->skc_obj_align
;
941 /* Per-magazine sizes below assume a 4Kib page size */
942 if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
* 256))
943 size
= 4; /* Minimum 4Mib per-magazine */
944 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
* 32))
945 size
= 16; /* Minimum 2Mib per-magazine */
946 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
))
947 size
= 64; /* Minimum 256Kib per-magazine */
948 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
/ 4))
949 size
= 128; /* Minimum 128Kib per-magazine */
957 * Allocate a per-cpu magazine to assoicate with a specific core.
959 static spl_kmem_magazine_t
*
960 spl_magazine_alloc(spl_kmem_cache_t
*skc
, int node
)
962 spl_kmem_magazine_t
*skm
;
963 int size
= sizeof(spl_kmem_magazine_t
) +
964 sizeof(void *) * skc
->skc_mag_size
;
967 skm
= kmem_alloc_node(size
, GFP_KERNEL
| __GFP_NOFAIL
, node
);
969 skm
->skm_magic
= SKM_MAGIC
;
971 skm
->skm_size
= skc
->skc_mag_size
;
972 skm
->skm_refill
= skc
->skc_mag_refill
;
973 skm
->skm_age
= jiffies
;
980 * Free a per-cpu magazine assoicated with a specific core.
983 spl_magazine_free(spl_kmem_magazine_t
*skm
)
985 int size
= sizeof(spl_kmem_magazine_t
) +
986 sizeof(void *) * skm
->skm_size
;
989 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
990 ASSERT(skm
->skm_avail
== 0);
992 kmem_free(skm
, size
);
997 __spl_magazine_create(void *data
)
999 spl_kmem_cache_t
*skc
= data
;
1000 int id
= smp_processor_id();
1002 skc
->skc_mag
[id
] = spl_magazine_alloc(skc
, cpu_to_node(id
));
1003 ASSERT(skc
->skc_mag
[id
]);
1007 * Create all pre-cpu magazines of reasonable sizes.
1010 spl_magazine_create(spl_kmem_cache_t
*skc
)
1014 skc
->skc_mag_size
= spl_magazine_size(skc
);
1015 skc
->skc_mag_refill
= (skc
->skc_mag_size
+ 1) / 2;
1016 spl_on_each_cpu(__spl_magazine_create
, skc
, 1);
1022 __spl_magazine_destroy(void *data
)
1024 spl_kmem_cache_t
*skc
= data
;
1025 spl_kmem_magazine_t
*skm
= skc
->skc_mag
[smp_processor_id()];
1027 (void)spl_cache_flush(skc
, skm
, skm
->skm_avail
);
1028 spl_magazine_free(skm
);
1032 * Destroy all pre-cpu magazines.
1035 spl_magazine_destroy(spl_kmem_cache_t
*skc
)
1038 spl_on_each_cpu(__spl_magazine_destroy
, skc
, 1);
1043 * Create a object cache based on the following arguments:
1045 * size cache object size
1046 * align cache object alignment
1047 * ctor cache object constructor
1048 * dtor cache object destructor
1049 * reclaim cache object reclaim
1050 * priv cache private data for ctor/dtor/reclaim
1051 * vmp unused must be NULL
1053 * KMC_NOTOUCH Disable cache object aging (unsupported)
1054 * KMC_NODEBUG Disable debugging (unsupported)
1055 * KMC_NOMAGAZINE Disable magazine (unsupported)
1056 * KMC_NOHASH Disable hashing (unsupported)
1057 * KMC_QCACHE Disable qcache (unsupported)
1058 * KMC_KMEM Force kmem backed cache
1059 * KMC_VMEM Force vmem backed cache
1060 * KMC_OFFSLAB Locate objects off the slab
1063 spl_kmem_cache_create(char *name
, size_t size
, size_t align
,
1064 spl_kmem_ctor_t ctor
,
1065 spl_kmem_dtor_t dtor
,
1066 spl_kmem_reclaim_t reclaim
,
1067 void *priv
, void *vmp
, int flags
)
1069 spl_kmem_cache_t
*skc
;
1070 int rc
, kmem_flags
= KM_SLEEP
;
1073 ASSERTF(!(flags
& KMC_NOMAGAZINE
), "Bad KMC_NOMAGAZINE (%x)\n", flags
);
1074 ASSERTF(!(flags
& KMC_NOHASH
), "Bad KMC_NOHASH (%x)\n", flags
);
1075 ASSERTF(!(flags
& KMC_QCACHE
), "Bad KMC_QCACHE (%x)\n", flags
);
1076 ASSERT(vmp
== NULL
);
1078 /* We may be called when there is a non-zero preempt_count or
1079 * interrupts are disabled is which case we must not sleep.
1081 if (current_thread_info()->preempt_count
|| irqs_disabled())
1082 kmem_flags
= KM_NOSLEEP
;
1084 /* Allocate new cache memory and initialize. */
1085 skc
= (spl_kmem_cache_t
*)kmem_zalloc(sizeof(*skc
), kmem_flags
);
1089 skc
->skc_magic
= SKC_MAGIC
;
1090 skc
->skc_name_size
= strlen(name
) + 1;
1091 skc
->skc_name
= (char *)kmem_alloc(skc
->skc_name_size
, kmem_flags
);
1092 if (skc
->skc_name
== NULL
) {
1093 kmem_free(skc
, sizeof(*skc
));
1096 strncpy(skc
->skc_name
, name
, skc
->skc_name_size
);
1098 skc
->skc_ctor
= ctor
;
1099 skc
->skc_dtor
= dtor
;
1100 skc
->skc_reclaim
= reclaim
;
1101 skc
->skc_private
= priv
;
1103 skc
->skc_flags
= flags
;
1104 skc
->skc_obj_size
= size
;
1105 skc
->skc_obj_align
= SPL_KMEM_CACHE_ALIGN
;
1106 skc
->skc_delay
= SPL_KMEM_CACHE_DELAY
;
1107 atomic_set(&skc
->skc_ref
, 0);
1109 INIT_LIST_HEAD(&skc
->skc_list
);
1110 INIT_LIST_HEAD(&skc
->skc_complete_list
);
1111 INIT_LIST_HEAD(&skc
->skc_partial_list
);
1112 spin_lock_init(&skc
->skc_lock
);
1113 skc
->skc_slab_fail
= 0;
1114 skc
->skc_slab_create
= 0;
1115 skc
->skc_slab_destroy
= 0;
1116 skc
->skc_slab_total
= 0;
1117 skc
->skc_slab_alloc
= 0;
1118 skc
->skc_slab_max
= 0;
1119 skc
->skc_obj_total
= 0;
1120 skc
->skc_obj_alloc
= 0;
1121 skc
->skc_obj_max
= 0;
1124 ASSERT((align
& (align
- 1)) == 0); /* Power of two */
1125 ASSERT(align
>= SPL_KMEM_CACHE_ALIGN
); /* Minimum size */
1126 skc
->skc_obj_align
= align
;
1129 /* If none passed select a cache type based on object size */
1130 if (!(skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
))) {
1131 if (P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
) <
1133 skc
->skc_flags
|= KMC_KMEM
;
1135 skc
->skc_flags
|= KMC_VMEM
;
1139 rc
= spl_slab_size(skc
, &skc
->skc_slab_objs
, &skc
->skc_slab_size
);
1143 rc
= spl_magazine_create(skc
);
1147 spl_init_delayed_work(&skc
->skc_work
, spl_cache_age
, skc
);
1148 schedule_delayed_work(&skc
->skc_work
, 2 * skc
->skc_delay
* HZ
);
1150 down_write(&spl_kmem_cache_sem
);
1151 list_add_tail(&skc
->skc_list
, &spl_kmem_cache_list
);
1152 up_write(&spl_kmem_cache_sem
);
1156 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1157 kmem_free(skc
, sizeof(*skc
));
1160 EXPORT_SYMBOL(spl_kmem_cache_create
);
1163 * Destroy a cache and all objects assoicated with the cache.
1166 spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
)
1168 DECLARE_WAIT_QUEUE_HEAD(wq
);
1171 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1173 down_write(&spl_kmem_cache_sem
);
1174 list_del_init(&skc
->skc_list
);
1175 up_write(&spl_kmem_cache_sem
);
1177 /* Cancel any and wait for any pending delayed work */
1178 ASSERT(!test_and_set_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1179 cancel_delayed_work(&skc
->skc_work
);
1180 flush_scheduled_work();
1182 /* Wait until all current callers complete, this is mainly
1183 * to catch the case where a low memory situation triggers a
1184 * cache reaping action which races with this destroy. */
1185 wait_event(wq
, atomic_read(&skc
->skc_ref
) == 0);
1187 spl_magazine_destroy(skc
);
1188 spl_slab_reclaim(skc
, 1);
1189 spin_lock(&skc
->skc_lock
);
1191 /* Validate there are no objects in use and free all the
1192 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
1193 ASSERT3U(skc
->skc_slab_alloc
, ==, 0);
1194 ASSERT3U(skc
->skc_obj_alloc
, ==, 0);
1195 ASSERT3U(skc
->skc_slab_total
, ==, 0);
1196 ASSERT3U(skc
->skc_obj_total
, ==, 0);
1197 ASSERT(list_empty(&skc
->skc_complete_list
));
1199 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1200 spin_unlock(&skc
->skc_lock
);
1202 kmem_free(skc
, sizeof(*skc
));
1206 EXPORT_SYMBOL(spl_kmem_cache_destroy
);
1209 * Allocate an object from a slab attached to the cache. This is used to
1210 * repopulate the per-cpu magazine caches in batches when they run low.
1213 spl_cache_obj(spl_kmem_cache_t
*skc
, spl_kmem_slab_t
*sks
)
1215 spl_kmem_obj_t
*sko
;
1217 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1218 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1219 ASSERT(spin_is_locked(&skc
->skc_lock
));
1221 sko
= list_entry(sks
->sks_free_list
.next
, spl_kmem_obj_t
, sko_list
);
1222 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1223 ASSERT(sko
->sko_addr
!= NULL
);
1225 /* Remove from sks_free_list */
1226 list_del_init(&sko
->sko_list
);
1228 sks
->sks_age
= jiffies
;
1230 skc
->skc_obj_alloc
++;
1232 /* Track max obj usage statistics */
1233 if (skc
->skc_obj_alloc
> skc
->skc_obj_max
)
1234 skc
->skc_obj_max
= skc
->skc_obj_alloc
;
1236 /* Track max slab usage statistics */
1237 if (sks
->sks_ref
== 1) {
1238 skc
->skc_slab_alloc
++;
1240 if (skc
->skc_slab_alloc
> skc
->skc_slab_max
)
1241 skc
->skc_slab_max
= skc
->skc_slab_alloc
;
1244 return sko
->sko_addr
;
1248 * No available objects on any slabsi, create a new slab. Since this
1249 * is an expensive operation we do it without holding the spinlock and
1250 * only briefly aquire it when we link in the fully allocated and
1253 static spl_kmem_slab_t
*
1254 spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
)
1256 spl_kmem_slab_t
*sks
;
1259 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1264 * Before allocating a new slab check if the slab is being reaped.
1265 * If it is there is a good chance we can wait until it finishes
1266 * and then use one of the newly freed but not aged-out slabs.
1268 if (test_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1270 GOTO(out
, sks
= NULL
);
1273 /* Allocate a new slab for the cache */
1274 sks
= spl_slab_alloc(skc
, flags
| __GFP_NORETRY
| __GFP_NOWARN
);
1276 GOTO(out
, sks
= NULL
);
1278 /* Link the new empty slab in to the end of skc_partial_list. */
1279 spin_lock(&skc
->skc_lock
);
1280 skc
->skc_slab_total
++;
1281 skc
->skc_obj_total
+= sks
->sks_objs
;
1282 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1283 spin_unlock(&skc
->skc_lock
);
1285 local_irq_disable();
1291 * Refill a per-cpu magazine with objects from the slabs for this
1292 * cache. Ideally the magazine can be repopulated using existing
1293 * objects which have been released, however if we are unable to
1294 * locate enough free objects new slabs of objects will be created.
1297 spl_cache_refill(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flags
)
1299 spl_kmem_slab_t
*sks
;
1303 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1304 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1306 refill
= MIN(skm
->skm_refill
, skm
->skm_size
- skm
->skm_avail
);
1307 spin_lock(&skc
->skc_lock
);
1309 while (refill
> 0) {
1310 /* No slabs available we may need to grow the cache */
1311 if (list_empty(&skc
->skc_partial_list
)) {
1312 spin_unlock(&skc
->skc_lock
);
1314 sks
= spl_cache_grow(skc
, flags
);
1318 /* Rescheduled to different CPU skm is not local */
1319 if (skm
!= skc
->skc_mag
[smp_processor_id()])
1322 /* Potentially rescheduled to the same CPU but
1323 * allocations may have occured from this CPU while
1324 * we were sleeping so recalculate max refill. */
1325 refill
= MIN(refill
, skm
->skm_size
- skm
->skm_avail
);
1327 spin_lock(&skc
->skc_lock
);
1331 /* Grab the next available slab */
1332 sks
= list_entry((&skc
->skc_partial_list
)->next
,
1333 spl_kmem_slab_t
, sks_list
);
1334 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1335 ASSERT(sks
->sks_ref
< sks
->sks_objs
);
1336 ASSERT(!list_empty(&sks
->sks_free_list
));
1338 /* Consume as many objects as needed to refill the requested
1339 * cache. We must also be careful not to overfill it. */
1340 while (sks
->sks_ref
< sks
->sks_objs
&& refill
-- > 0 && ++rc
) {
1341 ASSERT(skm
->skm_avail
< skm
->skm_size
);
1342 ASSERT(rc
< skm
->skm_size
);
1343 skm
->skm_objs
[skm
->skm_avail
++]=spl_cache_obj(skc
,sks
);
1346 /* Move slab to skc_complete_list when full */
1347 if (sks
->sks_ref
== sks
->sks_objs
) {
1348 list_del(&sks
->sks_list
);
1349 list_add(&sks
->sks_list
, &skc
->skc_complete_list
);
1353 spin_unlock(&skc
->skc_lock
);
1355 /* Returns the number of entries added to cache */
1360 * Release an object back to the slab from which it came.
1363 spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
)
1365 spl_kmem_slab_t
*sks
= NULL
;
1366 spl_kmem_obj_t
*sko
= NULL
;
1369 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1370 ASSERT(spin_is_locked(&skc
->skc_lock
));
1372 sko
= obj
+ P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
);
1373 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1375 sks
= sko
->sko_slab
;
1376 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1377 ASSERT(sks
->sks_cache
== skc
);
1378 list_add(&sko
->sko_list
, &sks
->sks_free_list
);
1380 sks
->sks_age
= jiffies
;
1382 skc
->skc_obj_alloc
--;
1384 /* Move slab to skc_partial_list when no longer full. Slabs
1385 * are added to the head to keep the partial list is quasi-full
1386 * sorted order. Fuller at the head, emptier at the tail. */
1387 if (sks
->sks_ref
== (sks
->sks_objs
- 1)) {
1388 list_del(&sks
->sks_list
);
1389 list_add(&sks
->sks_list
, &skc
->skc_partial_list
);
1392 /* Move emply slabs to the end of the partial list so
1393 * they can be easily found and freed during reclamation. */
1394 if (sks
->sks_ref
== 0) {
1395 list_del(&sks
->sks_list
);
1396 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1397 skc
->skc_slab_alloc
--;
1404 * Release a batch of objects from a per-cpu magazine back to their
1405 * respective slabs. This occurs when we exceed the magazine size,
1406 * are under memory pressure, when the cache is idle, or during
1407 * cache cleanup. The flush argument contains the number of entries
1408 * to remove from the magazine.
1411 spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
1413 int i
, count
= MIN(flush
, skm
->skm_avail
);
1416 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1417 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1420 * XXX: Currently we simply return objects from the magazine to
1421 * the slabs in fifo order. The ideal thing to do from a memory
1422 * fragmentation standpoint is to cheaply determine the set of
1423 * objects in the magazine which will result in the largest
1424 * number of free slabs if released from the magazine.
1426 spin_lock(&skc
->skc_lock
);
1427 for (i
= 0; i
< count
; i
++)
1428 spl_cache_shrink(skc
, skm
->skm_objs
[i
]);
1430 skm
->skm_avail
-= count
;
1431 memmove(skm
->skm_objs
, &(skm
->skm_objs
[count
]),
1432 sizeof(void *) * skm
->skm_avail
);
1434 spin_unlock(&skc
->skc_lock
);
1440 * Allocate an object from the per-cpu magazine, or if the magazine
1441 * is empty directly allocate from a slab and repopulate the magazine.
1444 spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
)
1446 spl_kmem_magazine_t
*skm
;
1447 unsigned long irq_flags
;
1451 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1452 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1453 ASSERT(flags
& KM_SLEEP
);
1454 atomic_inc(&skc
->skc_ref
);
1455 local_irq_save(irq_flags
);
1458 /* Safe to update per-cpu structure without lock, but
1459 * in the restart case we must be careful to reaquire
1460 * the local magazine since this may have changed
1461 * when we need to grow the cache. */
1462 skm
= skc
->skc_mag
[smp_processor_id()];
1463 ASSERTF(skm
->skm_magic
== SKM_MAGIC
, "%x != %x: %s/%p/%p %x/%x/%x\n",
1464 skm
->skm_magic
, SKM_MAGIC
, skc
->skc_name
, skc
, skm
,
1465 skm
->skm_size
, skm
->skm_refill
, skm
->skm_avail
);
1467 if (likely(skm
->skm_avail
)) {
1468 /* Object available in CPU cache, use it */
1469 obj
= skm
->skm_objs
[--skm
->skm_avail
];
1470 skm
->skm_age
= jiffies
;
1472 /* Per-CPU cache empty, directly allocate from
1473 * the slab and refill the per-CPU cache. */
1474 (void)spl_cache_refill(skc
, skm
, flags
);
1475 GOTO(restart
, obj
= NULL
);
1478 local_irq_restore(irq_flags
);
1480 ASSERT(((unsigned long)(obj
) % skc
->skc_obj_align
) == 0);
1482 /* Pre-emptively migrate object to CPU L1 cache */
1484 atomic_dec(&skc
->skc_ref
);
1488 EXPORT_SYMBOL(spl_kmem_cache_alloc
);
1491 * Free an object back to the local per-cpu magazine, there is no
1492 * guarantee that this is the same magazine the object was originally
1493 * allocated from. We may need to flush entire from the magazine
1494 * back to the slabs to make space.
1497 spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
)
1499 spl_kmem_magazine_t
*skm
;
1500 unsigned long flags
;
1503 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1504 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1505 atomic_inc(&skc
->skc_ref
);
1506 local_irq_save(flags
);
1508 /* Safe to update per-cpu structure without lock, but
1509 * no remote memory allocation tracking is being performed
1510 * it is entirely possible to allocate an object from one
1511 * CPU cache and return it to another. */
1512 skm
= skc
->skc_mag
[smp_processor_id()];
1513 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1515 /* Per-CPU cache full, flush it to make space */
1516 if (unlikely(skm
->skm_avail
>= skm
->skm_size
))
1517 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1519 /* Available space in cache, use it */
1520 skm
->skm_objs
[skm
->skm_avail
++] = obj
;
1522 local_irq_restore(flags
);
1523 atomic_dec(&skc
->skc_ref
);
1527 EXPORT_SYMBOL(spl_kmem_cache_free
);
1530 * The generic shrinker function for all caches. Under linux a shrinker
1531 * may not be tightly coupled with a slab cache. In fact linux always
1532 * systematically trys calling all registered shrinker callbacks which
1533 * report that they contain unused objects. Because of this we only
1534 * register one shrinker function in the shim layer for all slab caches.
1535 * We always attempt to shrink all caches when this generic shrinker
1536 * is called. The shrinker should return the number of free objects
1537 * in the cache when called with nr_to_scan == 0 but not attempt to
1538 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
1539 * objects should be freed, because Solaris semantics are to free
1540 * all available objects we may free more objects than requested.
1543 spl_kmem_cache_generic_shrinker(int nr_to_scan
, unsigned int gfp_mask
)
1545 spl_kmem_cache_t
*skc
;
1548 down_read(&spl_kmem_cache_sem
);
1549 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
1551 spl_kmem_cache_reap_now(skc
);
1554 * Presume everything alloc'ed in reclaimable, this ensures
1555 * we are called again with nr_to_scan > 0 so can try and
1556 * reclaim. The exact number is not important either so
1557 * we forgo taking this already highly contented lock.
1559 unused
+= skc
->skc_obj_alloc
;
1561 up_read(&spl_kmem_cache_sem
);
1563 return (unused
* sysctl_vfs_cache_pressure
) / 100;
1567 * Call the registered reclaim function for a cache. Depending on how
1568 * many and which objects are released it may simply repopulate the
1569 * local magazine which will then need to age-out. Objects which cannot
1570 * fit in the magazine we will be released back to their slabs which will
1571 * also need to age out before being release. This is all just best
1572 * effort and we do not want to thrash creating and destroying slabs.
1575 spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
)
1579 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1580 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1582 /* Prevent concurrent cache reaping when contended */
1583 if (test_and_set_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1588 atomic_inc(&skc
->skc_ref
);
1590 if (skc
->skc_reclaim
)
1591 skc
->skc_reclaim(skc
->skc_private
);
1593 spl_slab_reclaim(skc
, 0);
1594 clear_bit(KMC_BIT_REAPING
, &skc
->skc_flags
);
1595 atomic_dec(&skc
->skc_ref
);
1599 EXPORT_SYMBOL(spl_kmem_cache_reap_now
);
1602 * Reap all free slabs from all registered caches.
1607 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK
, GFP_KERNEL
);
1609 EXPORT_SYMBOL(spl_kmem_reap
);
1611 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
1613 spl_sprintf_addr(kmem_debug_t
*kd
, char *str
, int len
, int min
)
1615 int size
= ((len
- 1) < kd
->kd_size
) ? (len
- 1) : kd
->kd_size
;
1618 ASSERT(str
!= NULL
&& len
>= 17);
1619 memset(str
, 0, len
);
1621 /* Check for a fully printable string, and while we are at
1622 * it place the printable characters in the passed buffer. */
1623 for (i
= 0; i
< size
; i
++) {
1624 str
[i
] = ((char *)(kd
->kd_addr
))[i
];
1625 if (isprint(str
[i
])) {
1628 /* Minimum number of printable characters found
1629 * to make it worthwhile to print this as ascii. */
1639 sprintf(str
, "%02x%02x%02x%02x%02x%02x%02x%02x",
1640 *((uint8_t *)kd
->kd_addr
),
1641 *((uint8_t *)kd
->kd_addr
+ 2),
1642 *((uint8_t *)kd
->kd_addr
+ 4),
1643 *((uint8_t *)kd
->kd_addr
+ 6),
1644 *((uint8_t *)kd
->kd_addr
+ 8),
1645 *((uint8_t *)kd
->kd_addr
+ 10),
1646 *((uint8_t *)kd
->kd_addr
+ 12),
1647 *((uint8_t *)kd
->kd_addr
+ 14));
1654 spl_kmem_init_tracking(struct list_head
*list
, spinlock_t
*lock
, int size
)
1659 spin_lock_init(lock
);
1660 INIT_LIST_HEAD(list
);
1662 for (i
= 0; i
< size
; i
++)
1663 INIT_HLIST_HEAD(&kmem_table
[i
]);
1669 spl_kmem_fini_tracking(struct list_head
*list
, spinlock_t
*lock
)
1671 unsigned long flags
;
1676 spin_lock_irqsave(lock
, flags
);
1677 if (!list_empty(list
))
1678 printk(KERN_WARNING
"%-16s %-5s %-16s %s:%s\n", "address",
1679 "size", "data", "func", "line");
1681 list_for_each_entry(kd
, list
, kd_list
)
1682 printk(KERN_WARNING
"%p %-5d %-16s %s:%d\n", kd
->kd_addr
,
1683 (int)kd
->kd_size
, spl_sprintf_addr(kd
, str
, 17, 8),
1684 kd
->kd_func
, kd
->kd_line
);
1686 spin_unlock_irqrestore(lock
, flags
);
1689 #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1690 #define spl_kmem_init_tracking(list, lock, size)
1691 #define spl_kmem_fini_tracking(list, lock)
1692 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1695 spl_kmem_init_globals(void)
1699 /* For now all zones are includes, it may be wise to restrict
1700 * this to normal and highmem zones if we see problems. */
1701 for_each_zone(zone
) {
1703 if (!populated_zone(zone
))
1706 minfree
+= zone
->pages_min
;
1707 desfree
+= zone
->pages_low
;
1708 lotsfree
+= zone
->pages_high
;
1718 init_rwsem(&spl_kmem_cache_sem
);
1719 INIT_LIST_HEAD(&spl_kmem_cache_list
);
1720 spl_kmem_init_globals();
1722 #ifdef HAVE_SET_SHRINKER
1723 spl_kmem_cache_shrinker
= set_shrinker(KMC_DEFAULT_SEEKS
,
1724 spl_kmem_cache_generic_shrinker
);
1725 if (spl_kmem_cache_shrinker
== NULL
)
1726 RETURN(rc
= -ENOMEM
);
1728 register_shrinker(&spl_kmem_cache_shrinker
);
1732 atomic64_set(&kmem_alloc_used
, 0);
1733 atomic64_set(&vmem_alloc_used
, 0);
1735 spl_kmem_init_tracking(&kmem_list
, &kmem_lock
, KMEM_TABLE_SIZE
);
1736 spl_kmem_init_tracking(&vmem_list
, &vmem_lock
, VMEM_TABLE_SIZE
);
1745 /* Display all unreclaimed memory addresses, including the
1746 * allocation size and the first few bytes of what's located
1747 * at that address to aid in debugging. Performance is not
1748 * a serious concern here since it is module unload time. */
1749 if (atomic64_read(&kmem_alloc_used
) != 0)
1750 CWARN("kmem leaked %ld/%ld bytes\n",
1751 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
1754 if (atomic64_read(&vmem_alloc_used
) != 0)
1755 CWARN("vmem leaked %ld/%ld bytes\n",
1756 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
1758 spl_kmem_fini_tracking(&kmem_list
, &kmem_lock
);
1759 spl_kmem_fini_tracking(&vmem_list
, &vmem_lock
);
1760 #endif /* DEBUG_KMEM */
1763 #ifdef HAVE_SET_SHRINKER
1764 remove_shrinker(spl_kmem_cache_shrinker
);
1766 unregister_shrinker(&spl_kmem_cache_shrinker
);