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
= NULL
;
84 EXPORT_SYMBOL(get_vmalloc_info_fn
);
85 #endif /* HAVE_GET_VMALLOC_INFO */
87 #ifndef HAVE_FIRST_ONLINE_PGDAT
88 first_online_pgdat_t first_online_pgdat_fn
= NULL
;
89 EXPORT_SYMBOL(first_online_pgdat_fn
);
90 #endif /* HAVE_FIRST_ONLINE_PGDAT */
92 #ifndef HAVE_NEXT_ONLINE_PGDAT
93 next_online_pgdat_t next_online_pgdat_fn
= NULL
;
94 EXPORT_SYMBOL(next_online_pgdat_fn
);
95 #endif /* HAVE_NEXT_ONLINE_PGDAT */
97 #ifndef HAVE_NEXT_ZONE
98 next_zone_t next_zone_fn
= NULL
;
99 EXPORT_SYMBOL(next_zone_fn
);
100 #endif /* HAVE_NEXT_ZONE */
102 #ifndef HAVE_GET_ZONE_COUNTS
103 get_zone_counts_t get_zone_counts_fn
= NULL
;
104 EXPORT_SYMBOL(get_zone_counts_fn
);
105 #endif /* HAVE_GET_ZONE_COUNTS */
108 spl_kmem_availrmem(void)
110 unsigned long active
;
111 unsigned long inactive
;
114 get_zone_counts(&active
, &inactive
, &free
);
116 /* The amount of easily available memory */
117 return free
+ inactive
;
119 EXPORT_SYMBOL(spl_kmem_availrmem
);
122 vmem_size(vmem_t
*vmp
, int typemask
)
124 struct vmalloc_info vmi
;
128 ASSERT(typemask
& (VMEM_ALLOC
| VMEM_FREE
));
130 get_vmalloc_info(&vmi
);
131 if (typemask
& VMEM_ALLOC
)
132 size
+= (size_t)vmi
.used
;
134 if (typemask
& VMEM_FREE
)
135 size
+= (size_t)(VMALLOC_TOTAL
- vmi
.used
);
139 EXPORT_SYMBOL(vmem_size
);
142 * Memory allocation interfaces and debugging for basic kmem_*
143 * and vmem_* style memory allocation. When DEBUG_KMEM is enable
144 * all allocations will be tracked when they are allocated and
145 * freed. When the SPL module is unload a list of all leaked
146 * addresses and where they were allocated will be dumped to the
147 * console. Enabling this feature has a significant impant on
148 * performance but it makes finding memory leaks staight forward.
151 /* Shim layer memory accounting */
152 atomic64_t kmem_alloc_used
= ATOMIC64_INIT(0);
153 unsigned long long kmem_alloc_max
= 0;
154 atomic64_t vmem_alloc_used
= ATOMIC64_INIT(0);
155 unsigned long long vmem_alloc_max
= 0;
156 int kmem_warning_flag
= 1;
158 EXPORT_SYMBOL(kmem_alloc_used
);
159 EXPORT_SYMBOL(kmem_alloc_max
);
160 EXPORT_SYMBOL(vmem_alloc_used
);
161 EXPORT_SYMBOL(vmem_alloc_max
);
162 EXPORT_SYMBOL(kmem_warning_flag
);
164 # ifdef DEBUG_KMEM_TRACKING
166 /* XXX - Not to surprisingly with debugging enabled the xmem_locks are very
167 * highly contended particularly on xfree(). If we want to run with this
168 * detailed debugging enabled for anything other than debugging we need to
169 * minimize the contention by moving to a lock per xmem_table entry model.
172 # define KMEM_HASH_BITS 10
173 # define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
175 # define VMEM_HASH_BITS 10
176 # define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS)
178 typedef struct kmem_debug
{
179 struct hlist_node kd_hlist
; /* Hash node linkage */
180 struct list_head kd_list
; /* List of all allocations */
181 void *kd_addr
; /* Allocation pointer */
182 size_t kd_size
; /* Allocation size */
183 const char *kd_func
; /* Allocation function */
184 int kd_line
; /* Allocation line */
187 spinlock_t kmem_lock
;
188 struct hlist_head kmem_table
[KMEM_TABLE_SIZE
];
189 struct list_head kmem_list
;
191 spinlock_t vmem_lock
;
192 struct hlist_head vmem_table
[VMEM_TABLE_SIZE
];
193 struct list_head vmem_list
;
195 EXPORT_SYMBOL(kmem_lock
);
196 EXPORT_SYMBOL(kmem_table
);
197 EXPORT_SYMBOL(kmem_list
);
199 EXPORT_SYMBOL(vmem_lock
);
200 EXPORT_SYMBOL(vmem_table
);
201 EXPORT_SYMBOL(vmem_list
);
204 int kmem_set_warning(int flag
) { return (kmem_warning_flag
= !!flag
); }
206 int kmem_set_warning(int flag
) { return 0; }
208 EXPORT_SYMBOL(kmem_set_warning
);
211 * Slab allocation interfaces
213 * While the Linux slab implementation was inspired by the Solaris
214 * implemenation I cannot use it to emulate the Solaris APIs. I
215 * require two features which are not provided by the Linux slab.
217 * 1) Constructors AND destructors. Recent versions of the Linux
218 * kernel have removed support for destructors. This is a deal
219 * breaker for the SPL which contains particularly expensive
220 * initializers for mutex's, condition variables, etc. We also
221 * require a minimal level of cleanup for these data types unlike
222 * many Linux data type which do need to be explicitly destroyed.
224 * 2) Virtual address space backed slab. Callers of the Solaris slab
225 * expect it to work well for both small are very large allocations.
226 * Because of memory fragmentation the Linux slab which is backed
227 * by kmalloc'ed memory performs very badly when confronted with
228 * large numbers of large allocations. Basing the slab on the
229 * virtual address space removes the need for contigeous pages
230 * and greatly improve performance for large allocations.
232 * For these reasons, the SPL has its own slab implementation with
233 * the needed features. It is not as highly optimized as either the
234 * Solaris or Linux slabs, but it should get me most of what is
235 * needed until it can be optimized or obsoleted by another approach.
237 * One serious concern I do have about this method is the relatively
238 * small virtual address space on 32bit arches. This will seriously
239 * constrain the size of the slab caches and their performance.
241 * XXX: Improve the partial slab list by carefully maintaining a
242 * strict ordering of fullest to emptiest slabs based on
243 * the slab reference count. This gaurentees the when freeing
244 * slabs back to the system we need only linearly traverse the
245 * last N slabs in the list to discover all the freeable slabs.
247 * XXX: NUMA awareness for optionally allocating memory close to a
248 * particular core. This can be adventageous if you know the slab
249 * object will be short lived and primarily accessed from one core.
251 * XXX: Slab coloring may also yield performance improvements and would
252 * be desirable to implement.
255 struct list_head spl_kmem_cache_list
; /* List of caches */
256 struct rw_semaphore spl_kmem_cache_sem
; /* Cache list lock */
258 static int spl_cache_flush(spl_kmem_cache_t
*skc
,
259 spl_kmem_magazine_t
*skm
, int flush
);
261 #ifdef HAVE_SET_SHRINKER
262 static struct shrinker
*spl_kmem_cache_shrinker
;
264 static int spl_kmem_cache_generic_shrinker(int nr_to_scan
,
265 unsigned int gfp_mask
);
266 static struct shrinker spl_kmem_cache_shrinker
= {
267 .shrink
= spl_kmem_cache_generic_shrinker
,
268 .seeks
= KMC_DEFAULT_SEEKS
,
273 # ifdef DEBUG_KMEM_TRACKING
275 static kmem_debug_t
*
276 kmem_del_init(spinlock_t
*lock
, struct hlist_head
*table
, int bits
,
279 struct hlist_head
*head
;
280 struct hlist_node
*node
;
281 struct kmem_debug
*p
;
285 spin_lock_irqsave(lock
, flags
);
287 head
= &table
[hash_ptr(addr
, bits
)];
288 hlist_for_each_entry_rcu(p
, node
, head
, kd_hlist
) {
289 if (p
->kd_addr
== addr
) {
290 hlist_del_init(&p
->kd_hlist
);
291 list_del_init(&p
->kd_list
);
292 spin_unlock_irqrestore(lock
, flags
);
297 spin_unlock_irqrestore(lock
, flags
);
303 kmem_alloc_track(size_t size
, int flags
, const char *func
, int line
,
304 int node_alloc
, int node
)
308 unsigned long irq_flags
;
311 dptr
= (kmem_debug_t
*) kmalloc(sizeof(kmem_debug_t
),
312 flags
& ~__GFP_ZERO
);
315 CWARN("kmem_alloc(%ld, 0x%x) debug failed\n",
316 sizeof(kmem_debug_t
), flags
);
318 /* Marked unlikely because we should never be doing this,
319 * we tolerate to up 2 pages but a single page is best. */
320 if (unlikely((size
) > (PAGE_SIZE
* 2)) && kmem_warning_flag
)
321 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
322 (unsigned long long) size
, flags
,
323 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
325 /* We use kstrdup() below because the string pointed to by
326 * __FUNCTION__ might not be available by the time we want
327 * to print it since the module might have been unloaded. */
328 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
329 if (unlikely(dptr
->kd_func
== NULL
)) {
331 CWARN("kstrdup() failed in kmem_alloc(%llu, 0x%x) "
332 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
333 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
337 /* Use the correct allocator */
339 ASSERT(!(flags
& __GFP_ZERO
));
340 ptr
= kmalloc_node(size
, flags
, node
);
341 } else if (flags
& __GFP_ZERO
) {
342 ptr
= kzalloc(size
, flags
& ~__GFP_ZERO
);
344 ptr
= kmalloc(size
, flags
);
347 if (unlikely(ptr
== NULL
)) {
348 kfree(dptr
->kd_func
);
350 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
351 (unsigned long long) size
, flags
,
352 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
356 atomic64_add(size
, &kmem_alloc_used
);
357 if (unlikely(atomic64_read(&kmem_alloc_used
) >
360 atomic64_read(&kmem_alloc_used
);
362 INIT_HLIST_NODE(&dptr
->kd_hlist
);
363 INIT_LIST_HEAD(&dptr
->kd_list
);
366 dptr
->kd_size
= size
;
367 dptr
->kd_line
= line
;
369 spin_lock_irqsave(&kmem_lock
, irq_flags
);
370 hlist_add_head_rcu(&dptr
->kd_hlist
,
371 &kmem_table
[hash_ptr(ptr
, KMEM_HASH_BITS
)]);
372 list_add_tail(&dptr
->kd_list
, &kmem_list
);
373 spin_unlock_irqrestore(&kmem_lock
, irq_flags
);
375 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
376 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
377 ptr
, atomic64_read(&kmem_alloc_used
),
383 EXPORT_SYMBOL(kmem_alloc_track
);
386 kmem_free_track(void *ptr
, size_t size
)
391 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
392 (unsigned long long) size
);
394 dptr
= kmem_del_init(&kmem_lock
, kmem_table
, KMEM_HASH_BITS
, ptr
);
396 ASSERT(dptr
); /* Must exist in hash due to kmem_alloc() */
398 /* Size must match */
399 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
400 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
401 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
403 atomic64_sub(size
, &kmem_alloc_used
);
405 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
406 (unsigned long long) size
, atomic64_read(&kmem_alloc_used
),
409 kfree(dptr
->kd_func
);
411 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
414 memset(ptr
, 0x5a, size
);
419 EXPORT_SYMBOL(kmem_free_track
);
422 vmem_alloc_track(size_t size
, int flags
, const char *func
, int line
)
426 unsigned long irq_flags
;
429 ASSERT(flags
& KM_SLEEP
);
431 dptr
= (kmem_debug_t
*) kmalloc(sizeof(kmem_debug_t
), flags
);
433 CWARN("vmem_alloc(%ld, 0x%x) debug failed\n",
434 sizeof(kmem_debug_t
), flags
);
436 /* We use kstrdup() below because the string pointed to by
437 * __FUNCTION__ might not be available by the time we want
438 * to print it, since the module might have been unloaded. */
439 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
440 if (unlikely(dptr
->kd_func
== NULL
)) {
442 CWARN("kstrdup() failed in vmem_alloc(%llu, 0x%x) "
443 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
444 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
448 ptr
= __vmalloc(size
, (flags
| __GFP_HIGHMEM
) & ~__GFP_ZERO
,
451 if (unlikely(ptr
== NULL
)) {
452 kfree(dptr
->kd_func
);
454 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
455 (unsigned long long) size
, flags
,
456 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
460 if (flags
& __GFP_ZERO
)
461 memset(ptr
, 0, size
);
463 atomic64_add(size
, &vmem_alloc_used
);
464 if (unlikely(atomic64_read(&vmem_alloc_used
) >
467 atomic64_read(&vmem_alloc_used
);
469 INIT_HLIST_NODE(&dptr
->kd_hlist
);
470 INIT_LIST_HEAD(&dptr
->kd_list
);
473 dptr
->kd_size
= size
;
474 dptr
->kd_line
= line
;
476 spin_lock_irqsave(&vmem_lock
, irq_flags
);
477 hlist_add_head_rcu(&dptr
->kd_hlist
,
478 &vmem_table
[hash_ptr(ptr
, VMEM_HASH_BITS
)]);
479 list_add_tail(&dptr
->kd_list
, &vmem_list
);
480 spin_unlock_irqrestore(&vmem_lock
, irq_flags
);
482 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
483 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
484 ptr
, atomic64_read(&vmem_alloc_used
),
490 EXPORT_SYMBOL(vmem_alloc_track
);
493 vmem_free_track(void *ptr
, size_t size
)
498 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
499 (unsigned long long) size
);
501 dptr
= kmem_del_init(&vmem_lock
, vmem_table
, VMEM_HASH_BITS
, ptr
);
502 ASSERT(dptr
); /* Must exist in hash due to vmem_alloc() */
504 /* Size must match */
505 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
506 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
507 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
509 atomic64_sub(size
, &vmem_alloc_used
);
510 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
511 (unsigned long long) size
, atomic64_read(&vmem_alloc_used
),
514 kfree(dptr
->kd_func
);
516 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
519 memset(ptr
, 0x5a, size
);
524 EXPORT_SYMBOL(vmem_free_track
);
526 # else /* DEBUG_KMEM_TRACKING */
529 kmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
,
530 int node_alloc
, int node
)
535 /* Marked unlikely because we should never be doing this,
536 * we tolerate to up 2 pages but a single page is best. */
537 if (unlikely(size
> (PAGE_SIZE
* 2)) && kmem_warning_flag
)
538 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
539 (unsigned long long) size
, flags
,
540 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
542 /* Use the correct allocator */
544 ASSERT(!(flags
& __GFP_ZERO
));
545 ptr
= kmalloc_node(size
, flags
, node
);
546 } else if (flags
& __GFP_ZERO
) {
547 ptr
= kzalloc(size
, flags
& (~__GFP_ZERO
));
549 ptr
= kmalloc(size
, flags
);
553 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
554 (unsigned long long) size
, flags
,
555 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
557 atomic64_add(size
, &kmem_alloc_used
);
558 if (unlikely(atomic64_read(&kmem_alloc_used
) > kmem_alloc_max
))
559 kmem_alloc_max
= atomic64_read(&kmem_alloc_used
);
561 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
562 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
563 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
567 EXPORT_SYMBOL(kmem_alloc_debug
);
570 kmem_free_debug(void *ptr
, size_t size
)
574 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
575 (unsigned long long) size
);
577 atomic64_sub(size
, &kmem_alloc_used
);
579 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
580 (unsigned long long) size
, atomic64_read(&kmem_alloc_used
),
583 memset(ptr
, 0x5a, size
);
588 EXPORT_SYMBOL(kmem_free_debug
);
591 vmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
)
596 ASSERT(flags
& KM_SLEEP
);
598 ptr
= __vmalloc(size
, (flags
| __GFP_HIGHMEM
) & ~__GFP_ZERO
,
601 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
602 (unsigned long long) size
, flags
,
603 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
605 if (flags
& __GFP_ZERO
)
606 memset(ptr
, 0, size
);
608 atomic64_add(size
, &vmem_alloc_used
);
610 if (unlikely(atomic64_read(&vmem_alloc_used
) > vmem_alloc_max
))
611 vmem_alloc_max
= atomic64_read(&vmem_alloc_used
);
613 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
614 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
615 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
620 EXPORT_SYMBOL(vmem_alloc_debug
);
623 vmem_free_debug(void *ptr
, size_t size
)
627 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
628 (unsigned long long) size
);
630 atomic64_sub(size
, &vmem_alloc_used
);
632 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
633 (unsigned long long) size
, atomic64_read(&vmem_alloc_used
),
636 memset(ptr
, 0x5a, size
);
641 EXPORT_SYMBOL(vmem_free_debug
);
643 # endif /* DEBUG_KMEM_TRACKING */
644 #endif /* DEBUG_KMEM */
647 kv_alloc(spl_kmem_cache_t
*skc
, int size
, int flags
)
651 if (skc
->skc_flags
& KMC_KMEM
) {
652 if (size
> (2 * PAGE_SIZE
)) {
653 ptr
= (void *)__get_free_pages(flags
, get_order(size
));
655 ptr
= kmem_alloc(size
, flags
);
657 ptr
= vmem_alloc(size
, flags
);
664 kv_free(spl_kmem_cache_t
*skc
, void *ptr
, int size
)
666 if (skc
->skc_flags
& KMC_KMEM
) {
667 if (size
> (2 * PAGE_SIZE
))
668 free_pages((unsigned long)ptr
, get_order(size
));
670 kmem_free(ptr
, size
);
672 vmem_free(ptr
, size
);
677 * It's important that we pack the spl_kmem_obj_t structure and the
678 * actual objects in to one large address space to minimize the number
679 * of calls to the allocator. It is far better to do a few large
680 * allocations and then subdivide it ourselves. Now which allocator
681 * we use requires balancing a few trade offs.
683 * For small objects we use kmem_alloc() because as long as you are
684 * only requesting a small number of pages (ideally just one) its cheap.
685 * However, when you start requesting multiple pages with kmem_alloc()
686 * it gets increasingly expensive since it requires contigeous pages.
687 * For this reason we shift to vmem_alloc() for slabs of large objects
688 * which removes the need for contigeous pages. We do not use
689 * vmem_alloc() in all cases because there is significant locking
690 * overhead in __get_vm_area_node(). This function takes a single
691 * global lock when aquiring an available virtual address range which
692 * serializes all vmem_alloc()'s for all slab caches. Using slightly
693 * different allocation functions for small and large objects should
694 * give us the best of both worlds.
696 * KMC_ONSLAB KMC_OFFSLAB
698 * +------------------------+ +-----------------+
699 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
700 * | skc_obj_size <-+ | | +-----------------+ | |
701 * | spl_kmem_obj_t | | | |
702 * | skc_obj_size <---+ | +-----------------+ | |
703 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
704 * | ... v | | spl_kmem_obj_t | |
705 * +------------------------+ +-----------------+ v
707 static spl_kmem_slab_t
*
708 spl_slab_alloc(spl_kmem_cache_t
*skc
, int flags
)
710 spl_kmem_slab_t
*sks
;
711 spl_kmem_obj_t
*sko
, *n
;
713 int i
, align
, size
, rc
= 0;
715 base
= kv_alloc(skc
, skc
->skc_slab_size
, flags
);
719 sks
= (spl_kmem_slab_t
*)base
;
720 sks
->sks_magic
= SKS_MAGIC
;
721 sks
->sks_objs
= skc
->skc_slab_objs
;
722 sks
->sks_age
= jiffies
;
723 sks
->sks_cache
= skc
;
724 INIT_LIST_HEAD(&sks
->sks_list
);
725 INIT_LIST_HEAD(&sks
->sks_free_list
);
728 align
= skc
->skc_obj_align
;
729 size
= P2ROUNDUP(skc
->skc_obj_size
, align
) +
730 P2ROUNDUP(sizeof(spl_kmem_obj_t
), align
);
732 for (i
= 0; i
< sks
->sks_objs
; i
++) {
733 if (skc
->skc_flags
& KMC_OFFSLAB
) {
734 obj
= kv_alloc(skc
, size
, flags
);
736 GOTO(out
, rc
= -ENOMEM
);
739 P2ROUNDUP(sizeof(spl_kmem_slab_t
), align
) +
743 sko
= obj
+ P2ROUNDUP(skc
->skc_obj_size
, align
);
745 sko
->sko_magic
= SKO_MAGIC
;
747 INIT_LIST_HEAD(&sko
->sko_list
);
748 list_add_tail(&sko
->sko_list
, &sks
->sks_free_list
);
751 list_for_each_entry(sko
, &sks
->sks_free_list
, sko_list
)
753 skc
->skc_ctor(sko
->sko_addr
, skc
->skc_private
, flags
);
756 if (skc
->skc_flags
& KMC_OFFSLAB
)
757 list_for_each_entry_safe(sko
, n
, &sks
->sks_free_list
,
759 kv_free(skc
, sko
->sko_addr
, size
);
761 kv_free(skc
, base
, skc
->skc_slab_size
);
769 * Remove a slab from complete or partial list, it must be called with
770 * the 'skc->skc_lock' held but the actual free must be performed
771 * outside the lock to prevent deadlocking on vmem addresses.
774 spl_slab_free(spl_kmem_slab_t
*sks
,
775 struct list_head
*sks_list
, struct list_head
*sko_list
)
777 spl_kmem_cache_t
*skc
;
780 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
781 ASSERT(sks
->sks_ref
== 0);
783 skc
= sks
->sks_cache
;
784 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
785 ASSERT(spin_is_locked(&skc
->skc_lock
));
788 * Update slab/objects counters in the cache, then remove the
789 * slab from the skc->skc_partial_list. Finally add the slab
790 * and all its objects in to the private work lists where the
791 * destructors will be called and the memory freed to the system.
793 skc
->skc_obj_total
-= sks
->sks_objs
;
794 skc
->skc_slab_total
--;
795 list_del(&sks
->sks_list
);
796 list_add(&sks
->sks_list
, sks_list
);
797 list_splice_init(&sks
->sks_free_list
, sko_list
);
803 * Traverses all the partial slabs attached to a cache and free those
804 * which which are currently empty, and have not been touched for
805 * skc_delay seconds to avoid thrashing. The count argument is
806 * passed to optionally cap the number of slabs reclaimed, a count
807 * of zero means try and reclaim everything. When flag is set we
808 * always free an available slab regardless of age.
811 spl_slab_reclaim(spl_kmem_cache_t
*skc
, int count
, int flag
)
813 spl_kmem_slab_t
*sks
, *m
;
814 spl_kmem_obj_t
*sko
, *n
;
821 * Move empty slabs and objects which have not been touched in
822 * skc_delay seconds on to private lists to be freed outside
823 * the spin lock. This delay time is important to avoid thrashing
824 * however when flag is set the delay will not be used.
826 spin_lock(&skc
->skc_lock
);
827 list_for_each_entry_safe_reverse(sks
,m
,&skc
->skc_partial_list
,sks_list
){
829 * All empty slabs are at the end of skc->skc_partial_list,
830 * therefore once a non-empty slab is found we can stop
831 * scanning. Additionally, stop when reaching the target
832 * reclaim 'count' if a non-zero threshhold is given.
834 if ((sks
->sks_ref
> 0) || (count
&& i
> count
))
837 if (time_after(jiffies
,sks
->sks_age
+skc
->skc_delay
*HZ
)||flag
) {
838 spl_slab_free(sks
, &sks_list
, &sko_list
);
842 spin_unlock(&skc
->skc_lock
);
845 * The following two loops ensure all the object destructors are
846 * run, any offslab objects are freed, and the slabs themselves
847 * are freed. This is all done outside the skc->skc_lock since
848 * this allows the destructor to sleep, and allows us to perform
849 * a conditional reschedule when a freeing a large number of
850 * objects and slabs back to the system.
852 if (skc
->skc_flags
& KMC_OFFSLAB
)
853 size
= P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
) +
854 P2ROUNDUP(sizeof(spl_kmem_obj_t
), skc
->skc_obj_align
);
856 list_for_each_entry_safe(sko
, n
, &sko_list
, sko_list
) {
857 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
860 skc
->skc_dtor(sko
->sko_addr
, skc
->skc_private
);
862 if (skc
->skc_flags
& KMC_OFFSLAB
)
863 kv_free(skc
, sko
->sko_addr
, size
);
868 list_for_each_entry_safe(sks
, m
, &sks_list
, sks_list
) {
869 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
870 kv_free(skc
, sks
, skc
->skc_slab_size
);
878 * Called regularly on all caches to age objects out of the magazines
879 * which have not been access in skc->skc_delay seconds. This prevents
880 * idle magazines from holding memory which might be better used by
881 * other caches or parts of the system. The delay is present to
882 * prevent thrashing the magazine.
885 spl_magazine_age(void *data
)
887 spl_kmem_magazine_t
*skm
=
888 spl_get_work_data(data
, spl_kmem_magazine_t
, skm_work
.work
);
889 spl_kmem_cache_t
*skc
= skm
->skm_cache
;
890 int i
= smp_processor_id();
892 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
893 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
894 ASSERT(skc
->skc_mag
[i
] == skm
);
896 if (skm
->skm_avail
> 0 &&
897 time_after(jiffies
, skm
->skm_age
+ skc
->skc_delay
* HZ
))
898 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
900 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
901 schedule_delayed_work_on(i
, &skm
->skm_work
,
902 skc
->skc_delay
/ 3 * HZ
);
906 * Called regularly to keep a downward pressure on the size of idle
907 * magazines and to release free slabs from the cache. This function
908 * never calls the registered reclaim function, that only occures
909 * under memory pressure or with a direct call to spl_kmem_reap().
912 spl_cache_age(void *data
)
914 spl_kmem_cache_t
*skc
=
915 spl_get_work_data(data
, spl_kmem_cache_t
, skc_work
.work
);
917 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
918 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
920 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
921 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
925 * Size a slab based on the size of each aliged object plus spl_kmem_obj_t.
926 * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However,
927 * for very small objects we may end up with more than this so as not
928 * to waste space in the minimal allocation of a single page. Also for
929 * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN,
930 * lower than this and we will fail.
933 spl_slab_size(spl_kmem_cache_t
*skc
, uint32_t *objs
, uint32_t *size
)
935 int sks_size
, obj_size
, max_size
, align
;
937 if (skc
->skc_flags
& KMC_OFFSLAB
) {
938 *objs
= SPL_KMEM_CACHE_OBJ_PER_SLAB
;
939 *size
= sizeof(spl_kmem_slab_t
);
941 align
= skc
->skc_obj_align
;
942 sks_size
= P2ROUNDUP(sizeof(spl_kmem_slab_t
), align
);
943 obj_size
= P2ROUNDUP(skc
->skc_obj_size
, align
) +
944 P2ROUNDUP(sizeof(spl_kmem_obj_t
), align
);
946 if (skc
->skc_flags
& KMC_KMEM
)
947 max_size
= ((uint64_t)1 << (MAX_ORDER
-1)) * PAGE_SIZE
;
949 max_size
= (32 * 1024 * 1024);
951 for (*size
= PAGE_SIZE
; *size
<= max_size
; *size
+= PAGE_SIZE
) {
952 *objs
= (*size
- sks_size
) / obj_size
;
953 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB
)
958 * Unable to satisfy target objets per slab, fallback to
959 * allocating a maximally sized slab and assuming it can
960 * contain the minimum objects count use it. If not fail.
963 *objs
= (*size
- sks_size
) / obj_size
;
964 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN
)
972 * Make a guess at reasonable per-cpu magazine size based on the size of
973 * each object and the cost of caching N of them in each magazine. Long
974 * term this should really adapt based on an observed usage heuristic.
977 spl_magazine_size(spl_kmem_cache_t
*skc
)
979 int size
, align
= skc
->skc_obj_align
;
982 /* Per-magazine sizes below assume a 4Kib page size */
983 if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
* 256))
984 size
= 4; /* Minimum 4Mib per-magazine */
985 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
* 32))
986 size
= 16; /* Minimum 2Mib per-magazine */
987 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
))
988 size
= 64; /* Minimum 256Kib per-magazine */
989 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
/ 4))
990 size
= 128; /* Minimum 128Kib per-magazine */
998 * Allocate a per-cpu magazine to assoicate with a specific core.
1000 static spl_kmem_magazine_t
*
1001 spl_magazine_alloc(spl_kmem_cache_t
*skc
, int node
)
1003 spl_kmem_magazine_t
*skm
;
1004 int size
= sizeof(spl_kmem_magazine_t
) +
1005 sizeof(void *) * skc
->skc_mag_size
;
1008 skm
= kmem_alloc_node(size
, GFP_KERNEL
| __GFP_NOFAIL
, node
);
1010 skm
->skm_magic
= SKM_MAGIC
;
1012 skm
->skm_size
= skc
->skc_mag_size
;
1013 skm
->skm_refill
= skc
->skc_mag_refill
;
1014 skm
->skm_cache
= skc
;
1015 spl_init_delayed_work(&skm
->skm_work
, spl_magazine_age
, skm
);
1016 skm
->skm_age
= jiffies
;
1023 * Free a per-cpu magazine assoicated with a specific core.
1026 spl_magazine_free(spl_kmem_magazine_t
*skm
)
1028 int size
= sizeof(spl_kmem_magazine_t
) +
1029 sizeof(void *) * skm
->skm_size
;
1032 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1033 ASSERT(skm
->skm_avail
== 0);
1035 kmem_free(skm
, size
);
1040 * Create all pre-cpu magazines of reasonable sizes.
1043 spl_magazine_create(spl_kmem_cache_t
*skc
)
1048 skc
->skc_mag_size
= spl_magazine_size(skc
);
1049 skc
->skc_mag_refill
= (skc
->skc_mag_size
+ 1) / 2;
1051 for_each_online_cpu(i
) {
1052 skc
->skc_mag
[i
] = spl_magazine_alloc(skc
, cpu_to_node(i
));
1053 if (!skc
->skc_mag
[i
]) {
1054 for (i
--; i
>= 0; i
--)
1055 spl_magazine_free(skc
->skc_mag
[i
]);
1061 /* Only after everything is allocated schedule magazine work */
1062 for_each_online_cpu(i
)
1063 schedule_delayed_work_on(i
, &skc
->skc_mag
[i
]->skm_work
,
1064 skc
->skc_delay
/ 3 * HZ
);
1070 * Destroy all pre-cpu magazines.
1073 spl_magazine_destroy(spl_kmem_cache_t
*skc
)
1075 spl_kmem_magazine_t
*skm
;
1079 for_each_online_cpu(i
) {
1080 skm
= skc
->skc_mag
[i
];
1081 (void)spl_cache_flush(skc
, skm
, skm
->skm_avail
);
1082 spl_magazine_free(skm
);
1089 * Create a object cache based on the following arguments:
1091 * size cache object size
1092 * align cache object alignment
1093 * ctor cache object constructor
1094 * dtor cache object destructor
1095 * reclaim cache object reclaim
1096 * priv cache private data for ctor/dtor/reclaim
1097 * vmp unused must be NULL
1099 * KMC_NOTOUCH Disable cache object aging (unsupported)
1100 * KMC_NODEBUG Disable debugging (unsupported)
1101 * KMC_NOMAGAZINE Disable magazine (unsupported)
1102 * KMC_NOHASH Disable hashing (unsupported)
1103 * KMC_QCACHE Disable qcache (unsupported)
1104 * KMC_KMEM Force kmem backed cache
1105 * KMC_VMEM Force vmem backed cache
1106 * KMC_OFFSLAB Locate objects off the slab
1109 spl_kmem_cache_create(char *name
, size_t size
, size_t align
,
1110 spl_kmem_ctor_t ctor
,
1111 spl_kmem_dtor_t dtor
,
1112 spl_kmem_reclaim_t reclaim
,
1113 void *priv
, void *vmp
, int flags
)
1115 spl_kmem_cache_t
*skc
;
1116 int rc
, kmem_flags
= KM_SLEEP
;
1119 ASSERTF(!(flags
& KMC_NOMAGAZINE
), "Bad KMC_NOMAGAZINE (%x)\n", flags
);
1120 ASSERTF(!(flags
& KMC_NOHASH
), "Bad KMC_NOHASH (%x)\n", flags
);
1121 ASSERTF(!(flags
& KMC_QCACHE
), "Bad KMC_QCACHE (%x)\n", flags
);
1122 ASSERT(vmp
== NULL
);
1124 /* We may be called when there is a non-zero preempt_count or
1125 * interrupts are disabled is which case we must not sleep.
1127 if (current_thread_info()->preempt_count
|| irqs_disabled())
1128 kmem_flags
= KM_NOSLEEP
;
1130 /* Allocate new cache memory and initialize. */
1131 skc
= (spl_kmem_cache_t
*)kmem_zalloc(sizeof(*skc
), kmem_flags
);
1135 skc
->skc_magic
= SKC_MAGIC
;
1136 skc
->skc_name_size
= strlen(name
) + 1;
1137 skc
->skc_name
= (char *)kmem_alloc(skc
->skc_name_size
, kmem_flags
);
1138 if (skc
->skc_name
== NULL
) {
1139 kmem_free(skc
, sizeof(*skc
));
1142 strncpy(skc
->skc_name
, name
, skc
->skc_name_size
);
1144 skc
->skc_ctor
= ctor
;
1145 skc
->skc_dtor
= dtor
;
1146 skc
->skc_reclaim
= reclaim
;
1147 skc
->skc_private
= priv
;
1149 skc
->skc_flags
= flags
;
1150 skc
->skc_obj_size
= size
;
1151 skc
->skc_obj_align
= SPL_KMEM_CACHE_ALIGN
;
1152 skc
->skc_delay
= SPL_KMEM_CACHE_DELAY
;
1153 skc
->skc_reap
= SPL_KMEM_CACHE_REAP
;
1154 atomic_set(&skc
->skc_ref
, 0);
1156 INIT_LIST_HEAD(&skc
->skc_list
);
1157 INIT_LIST_HEAD(&skc
->skc_complete_list
);
1158 INIT_LIST_HEAD(&skc
->skc_partial_list
);
1159 spin_lock_init(&skc
->skc_lock
);
1160 skc
->skc_slab_fail
= 0;
1161 skc
->skc_slab_create
= 0;
1162 skc
->skc_slab_destroy
= 0;
1163 skc
->skc_slab_total
= 0;
1164 skc
->skc_slab_alloc
= 0;
1165 skc
->skc_slab_max
= 0;
1166 skc
->skc_obj_total
= 0;
1167 skc
->skc_obj_alloc
= 0;
1168 skc
->skc_obj_max
= 0;
1171 ASSERT((align
& (align
- 1)) == 0); /* Power of two */
1172 ASSERT(align
>= SPL_KMEM_CACHE_ALIGN
); /* Minimum size */
1173 skc
->skc_obj_align
= align
;
1176 /* If none passed select a cache type based on object size */
1177 if (!(skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
))) {
1178 if (P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
) <
1180 skc
->skc_flags
|= KMC_KMEM
;
1182 skc
->skc_flags
|= KMC_VMEM
;
1186 rc
= spl_slab_size(skc
, &skc
->skc_slab_objs
, &skc
->skc_slab_size
);
1190 rc
= spl_magazine_create(skc
);
1194 spl_init_delayed_work(&skc
->skc_work
, spl_cache_age
, skc
);
1195 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
1197 down_write(&spl_kmem_cache_sem
);
1198 list_add_tail(&skc
->skc_list
, &spl_kmem_cache_list
);
1199 up_write(&spl_kmem_cache_sem
);
1203 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1204 kmem_free(skc
, sizeof(*skc
));
1207 EXPORT_SYMBOL(spl_kmem_cache_create
);
1210 * Destroy a cache and all objects assoicated with the cache.
1213 spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
)
1215 DECLARE_WAIT_QUEUE_HEAD(wq
);
1219 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1221 down_write(&spl_kmem_cache_sem
);
1222 list_del_init(&skc
->skc_list
);
1223 up_write(&spl_kmem_cache_sem
);
1225 /* Cancel any and wait for any pending delayed work */
1226 ASSERT(!test_and_set_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1227 cancel_delayed_work(&skc
->skc_work
);
1228 for_each_online_cpu(i
)
1229 cancel_delayed_work(&skc
->skc_mag
[i
]->skm_work
);
1231 flush_scheduled_work();
1233 /* Wait until all current callers complete, this is mainly
1234 * to catch the case where a low memory situation triggers a
1235 * cache reaping action which races with this destroy. */
1236 wait_event(wq
, atomic_read(&skc
->skc_ref
) == 0);
1238 spl_magazine_destroy(skc
);
1239 spl_slab_reclaim(skc
, 0, 1);
1240 spin_lock(&skc
->skc_lock
);
1242 /* Validate there are no objects in use and free all the
1243 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
1244 ASSERT3U(skc
->skc_slab_alloc
, ==, 0);
1245 ASSERT3U(skc
->skc_obj_alloc
, ==, 0);
1246 ASSERT3U(skc
->skc_slab_total
, ==, 0);
1247 ASSERT3U(skc
->skc_obj_total
, ==, 0);
1248 ASSERT(list_empty(&skc
->skc_complete_list
));
1250 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1251 spin_unlock(&skc
->skc_lock
);
1253 kmem_free(skc
, sizeof(*skc
));
1257 EXPORT_SYMBOL(spl_kmem_cache_destroy
);
1260 * Allocate an object from a slab attached to the cache. This is used to
1261 * repopulate the per-cpu magazine caches in batches when they run low.
1264 spl_cache_obj(spl_kmem_cache_t
*skc
, spl_kmem_slab_t
*sks
)
1266 spl_kmem_obj_t
*sko
;
1268 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1269 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1270 ASSERT(spin_is_locked(&skc
->skc_lock
));
1272 sko
= list_entry(sks
->sks_free_list
.next
, spl_kmem_obj_t
, sko_list
);
1273 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1274 ASSERT(sko
->sko_addr
!= NULL
);
1276 /* Remove from sks_free_list */
1277 list_del_init(&sko
->sko_list
);
1279 sks
->sks_age
= jiffies
;
1281 skc
->skc_obj_alloc
++;
1283 /* Track max obj usage statistics */
1284 if (skc
->skc_obj_alloc
> skc
->skc_obj_max
)
1285 skc
->skc_obj_max
= skc
->skc_obj_alloc
;
1287 /* Track max slab usage statistics */
1288 if (sks
->sks_ref
== 1) {
1289 skc
->skc_slab_alloc
++;
1291 if (skc
->skc_slab_alloc
> skc
->skc_slab_max
)
1292 skc
->skc_slab_max
= skc
->skc_slab_alloc
;
1295 return sko
->sko_addr
;
1299 * No available objects on any slabsi, create a new slab. Since this
1300 * is an expensive operation we do it without holding the spinlock and
1301 * only briefly aquire it when we link in the fully allocated and
1304 static spl_kmem_slab_t
*
1305 spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
)
1307 spl_kmem_slab_t
*sks
;
1310 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1315 * Before allocating a new slab check if the slab is being reaped.
1316 * If it is there is a good chance we can wait until it finishes
1317 * and then use one of the newly freed but not aged-out slabs.
1319 if (test_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1321 GOTO(out
, sks
= NULL
);
1324 /* Allocate a new slab for the cache */
1325 sks
= spl_slab_alloc(skc
, flags
| __GFP_NORETRY
| __GFP_NOWARN
);
1327 GOTO(out
, sks
= NULL
);
1329 /* Link the new empty slab in to the end of skc_partial_list. */
1330 spin_lock(&skc
->skc_lock
);
1331 skc
->skc_slab_total
++;
1332 skc
->skc_obj_total
+= sks
->sks_objs
;
1333 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1334 spin_unlock(&skc
->skc_lock
);
1336 local_irq_disable();
1342 * Refill a per-cpu magazine with objects from the slabs for this
1343 * cache. Ideally the magazine can be repopulated using existing
1344 * objects which have been released, however if we are unable to
1345 * locate enough free objects new slabs of objects will be created.
1348 spl_cache_refill(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flags
)
1350 spl_kmem_slab_t
*sks
;
1354 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1355 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1357 refill
= MIN(skm
->skm_refill
, skm
->skm_size
- skm
->skm_avail
);
1358 spin_lock(&skc
->skc_lock
);
1360 while (refill
> 0) {
1361 /* No slabs available we may need to grow the cache */
1362 if (list_empty(&skc
->skc_partial_list
)) {
1363 spin_unlock(&skc
->skc_lock
);
1365 sks
= spl_cache_grow(skc
, flags
);
1369 /* Rescheduled to different CPU skm is not local */
1370 if (skm
!= skc
->skc_mag
[smp_processor_id()])
1373 /* Potentially rescheduled to the same CPU but
1374 * allocations may have occured from this CPU while
1375 * we were sleeping so recalculate max refill. */
1376 refill
= MIN(refill
, skm
->skm_size
- skm
->skm_avail
);
1378 spin_lock(&skc
->skc_lock
);
1382 /* Grab the next available slab */
1383 sks
= list_entry((&skc
->skc_partial_list
)->next
,
1384 spl_kmem_slab_t
, sks_list
);
1385 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1386 ASSERT(sks
->sks_ref
< sks
->sks_objs
);
1387 ASSERT(!list_empty(&sks
->sks_free_list
));
1389 /* Consume as many objects as needed to refill the requested
1390 * cache. We must also be careful not to overfill it. */
1391 while (sks
->sks_ref
< sks
->sks_objs
&& refill
-- > 0 && ++rc
) {
1392 ASSERT(skm
->skm_avail
< skm
->skm_size
);
1393 ASSERT(rc
< skm
->skm_size
);
1394 skm
->skm_objs
[skm
->skm_avail
++]=spl_cache_obj(skc
,sks
);
1397 /* Move slab to skc_complete_list when full */
1398 if (sks
->sks_ref
== sks
->sks_objs
) {
1399 list_del(&sks
->sks_list
);
1400 list_add(&sks
->sks_list
, &skc
->skc_complete_list
);
1404 spin_unlock(&skc
->skc_lock
);
1406 /* Returns the number of entries added to cache */
1411 * Release an object back to the slab from which it came.
1414 spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
)
1416 spl_kmem_slab_t
*sks
= NULL
;
1417 spl_kmem_obj_t
*sko
= NULL
;
1420 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1421 ASSERT(spin_is_locked(&skc
->skc_lock
));
1423 sko
= obj
+ P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
);
1424 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1426 sks
= sko
->sko_slab
;
1427 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1428 ASSERT(sks
->sks_cache
== skc
);
1429 list_add(&sko
->sko_list
, &sks
->sks_free_list
);
1431 sks
->sks_age
= jiffies
;
1433 skc
->skc_obj_alloc
--;
1435 /* Move slab to skc_partial_list when no longer full. Slabs
1436 * are added to the head to keep the partial list is quasi-full
1437 * sorted order. Fuller at the head, emptier at the tail. */
1438 if (sks
->sks_ref
== (sks
->sks_objs
- 1)) {
1439 list_del(&sks
->sks_list
);
1440 list_add(&sks
->sks_list
, &skc
->skc_partial_list
);
1443 /* Move emply slabs to the end of the partial list so
1444 * they can be easily found and freed during reclamation. */
1445 if (sks
->sks_ref
== 0) {
1446 list_del(&sks
->sks_list
);
1447 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1448 skc
->skc_slab_alloc
--;
1455 * Release a batch of objects from a per-cpu magazine back to their
1456 * respective slabs. This occurs when we exceed the magazine size,
1457 * are under memory pressure, when the cache is idle, or during
1458 * cache cleanup. The flush argument contains the number of entries
1459 * to remove from the magazine.
1462 spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
1464 int i
, count
= MIN(flush
, skm
->skm_avail
);
1467 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1468 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1471 * XXX: Currently we simply return objects from the magazine to
1472 * the slabs in fifo order. The ideal thing to do from a memory
1473 * fragmentation standpoint is to cheaply determine the set of
1474 * objects in the magazine which will result in the largest
1475 * number of free slabs if released from the magazine.
1477 spin_lock(&skc
->skc_lock
);
1478 for (i
= 0; i
< count
; i
++)
1479 spl_cache_shrink(skc
, skm
->skm_objs
[i
]);
1481 skm
->skm_avail
-= count
;
1482 memmove(skm
->skm_objs
, &(skm
->skm_objs
[count
]),
1483 sizeof(void *) * skm
->skm_avail
);
1485 spin_unlock(&skc
->skc_lock
);
1491 * Allocate an object from the per-cpu magazine, or if the magazine
1492 * is empty directly allocate from a slab and repopulate the magazine.
1495 spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
)
1497 spl_kmem_magazine_t
*skm
;
1498 unsigned long irq_flags
;
1502 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1503 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1504 ASSERT(flags
& KM_SLEEP
);
1505 atomic_inc(&skc
->skc_ref
);
1506 local_irq_save(irq_flags
);
1509 /* Safe to update per-cpu structure without lock, but
1510 * in the restart case we must be careful to reaquire
1511 * the local magazine since this may have changed
1512 * when we need to grow the cache. */
1513 skm
= skc
->skc_mag
[smp_processor_id()];
1514 ASSERTF(skm
->skm_magic
== SKM_MAGIC
, "%x != %x: %s/%p/%p %x/%x/%x\n",
1515 skm
->skm_magic
, SKM_MAGIC
, skc
->skc_name
, skc
, skm
,
1516 skm
->skm_size
, skm
->skm_refill
, skm
->skm_avail
);
1518 if (likely(skm
->skm_avail
)) {
1519 /* Object available in CPU cache, use it */
1520 obj
= skm
->skm_objs
[--skm
->skm_avail
];
1521 skm
->skm_age
= jiffies
;
1523 /* Per-CPU cache empty, directly allocate from
1524 * the slab and refill the per-CPU cache. */
1525 (void)spl_cache_refill(skc
, skm
, flags
);
1526 GOTO(restart
, obj
= NULL
);
1529 local_irq_restore(irq_flags
);
1531 ASSERT(((unsigned long)(obj
) % skc
->skc_obj_align
) == 0);
1533 /* Pre-emptively migrate object to CPU L1 cache */
1535 atomic_dec(&skc
->skc_ref
);
1539 EXPORT_SYMBOL(spl_kmem_cache_alloc
);
1542 * Free an object back to the local per-cpu magazine, there is no
1543 * guarantee that this is the same magazine the object was originally
1544 * allocated from. We may need to flush entire from the magazine
1545 * back to the slabs to make space.
1548 spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
)
1550 spl_kmem_magazine_t
*skm
;
1551 unsigned long flags
;
1554 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1555 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1556 atomic_inc(&skc
->skc_ref
);
1557 local_irq_save(flags
);
1559 /* Safe to update per-cpu structure without lock, but
1560 * no remote memory allocation tracking is being performed
1561 * it is entirely possible to allocate an object from one
1562 * CPU cache and return it to another. */
1563 skm
= skc
->skc_mag
[smp_processor_id()];
1564 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1566 /* Per-CPU cache full, flush it to make space */
1567 if (unlikely(skm
->skm_avail
>= skm
->skm_size
))
1568 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1570 /* Available space in cache, use it */
1571 skm
->skm_objs
[skm
->skm_avail
++] = obj
;
1573 local_irq_restore(flags
);
1574 atomic_dec(&skc
->skc_ref
);
1578 EXPORT_SYMBOL(spl_kmem_cache_free
);
1581 * The generic shrinker function for all caches. Under linux a shrinker
1582 * may not be tightly coupled with a slab cache. In fact linux always
1583 * systematically trys calling all registered shrinker callbacks which
1584 * report that they contain unused objects. Because of this we only
1585 * register one shrinker function in the shim layer for all slab caches.
1586 * We always attempt to shrink all caches when this generic shrinker
1587 * is called. The shrinker should return the number of free objects
1588 * in the cache when called with nr_to_scan == 0 but not attempt to
1589 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
1590 * objects should be freed, because Solaris semantics are to free
1591 * all available objects we may free more objects than requested.
1594 spl_kmem_cache_generic_shrinker(int nr_to_scan
, unsigned int gfp_mask
)
1596 spl_kmem_cache_t
*skc
;
1599 down_read(&spl_kmem_cache_sem
);
1600 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
1602 spl_kmem_cache_reap_now(skc
);
1605 * Presume everything alloc'ed in reclaimable, this ensures
1606 * we are called again with nr_to_scan > 0 so can try and
1607 * reclaim. The exact number is not important either so
1608 * we forgo taking this already highly contented lock.
1610 unused
+= skc
->skc_obj_alloc
;
1612 up_read(&spl_kmem_cache_sem
);
1614 return (unused
* sysctl_vfs_cache_pressure
) / 100;
1618 * Call the registered reclaim function for a cache. Depending on how
1619 * many and which objects are released it may simply repopulate the
1620 * local magazine which will then need to age-out. Objects which cannot
1621 * fit in the magazine we will be released back to their slabs which will
1622 * also need to age out before being release. This is all just best
1623 * effort and we do not want to thrash creating and destroying slabs.
1626 spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
)
1630 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1631 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1633 /* Prevent concurrent cache reaping when contended */
1634 if (test_and_set_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1639 atomic_inc(&skc
->skc_ref
);
1641 if (skc
->skc_reclaim
)
1642 skc
->skc_reclaim(skc
->skc_private
);
1644 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
1645 clear_bit(KMC_BIT_REAPING
, &skc
->skc_flags
);
1646 atomic_dec(&skc
->skc_ref
);
1650 EXPORT_SYMBOL(spl_kmem_cache_reap_now
);
1653 * Reap all free slabs from all registered caches.
1658 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK
, GFP_KERNEL
);
1660 EXPORT_SYMBOL(spl_kmem_reap
);
1662 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
1664 spl_sprintf_addr(kmem_debug_t
*kd
, char *str
, int len
, int min
)
1666 int size
= ((len
- 1) < kd
->kd_size
) ? (len
- 1) : kd
->kd_size
;
1669 ASSERT(str
!= NULL
&& len
>= 17);
1670 memset(str
, 0, len
);
1672 /* Check for a fully printable string, and while we are at
1673 * it place the printable characters in the passed buffer. */
1674 for (i
= 0; i
< size
; i
++) {
1675 str
[i
] = ((char *)(kd
->kd_addr
))[i
];
1676 if (isprint(str
[i
])) {
1679 /* Minimum number of printable characters found
1680 * to make it worthwhile to print this as ascii. */
1690 sprintf(str
, "%02x%02x%02x%02x%02x%02x%02x%02x",
1691 *((uint8_t *)kd
->kd_addr
),
1692 *((uint8_t *)kd
->kd_addr
+ 2),
1693 *((uint8_t *)kd
->kd_addr
+ 4),
1694 *((uint8_t *)kd
->kd_addr
+ 6),
1695 *((uint8_t *)kd
->kd_addr
+ 8),
1696 *((uint8_t *)kd
->kd_addr
+ 10),
1697 *((uint8_t *)kd
->kd_addr
+ 12),
1698 *((uint8_t *)kd
->kd_addr
+ 14));
1705 spl_kmem_init_tracking(struct list_head
*list
, spinlock_t
*lock
, int size
)
1710 spin_lock_init(lock
);
1711 INIT_LIST_HEAD(list
);
1713 for (i
= 0; i
< size
; i
++)
1714 INIT_HLIST_HEAD(&kmem_table
[i
]);
1720 spl_kmem_fini_tracking(struct list_head
*list
, spinlock_t
*lock
)
1722 unsigned long flags
;
1727 spin_lock_irqsave(lock
, flags
);
1728 if (!list_empty(list
))
1729 printk(KERN_WARNING
"%-16s %-5s %-16s %s:%s\n", "address",
1730 "size", "data", "func", "line");
1732 list_for_each_entry(kd
, list
, kd_list
)
1733 printk(KERN_WARNING
"%p %-5d %-16s %s:%d\n", kd
->kd_addr
,
1734 (int)kd
->kd_size
, spl_sprintf_addr(kd
, str
, 17, 8),
1735 kd
->kd_func
, kd
->kd_line
);
1737 spin_unlock_irqrestore(lock
, flags
);
1740 #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1741 #define spl_kmem_init_tracking(list, lock, size)
1742 #define spl_kmem_fini_tracking(list, lock)
1743 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1746 spl_kmem_init_globals(void)
1750 /* For now all zones are includes, it may be wise to restrict
1751 * this to normal and highmem zones if we see problems. */
1752 for_each_zone(zone
) {
1754 if (!populated_zone(zone
))
1757 minfree
+= zone
->pages_min
;
1758 desfree
+= zone
->pages_low
;
1759 lotsfree
+= zone
->pages_high
;
1762 /* Solaris default values */
1763 swapfs_minfree
= MAX(2*1024*1024 / PAGE_SIZE
, physmem
/ 8);
1764 swapfs_reserve
= MIN(4*1024*1024 / PAGE_SIZE
, physmem
/ 16);
1768 * Called at module init when it is safe to use spl_kallsyms_lookup_name()
1771 spl_kmem_init_kallsyms_lookup(void)
1773 #ifndef HAVE_GET_VMALLOC_INFO
1774 get_vmalloc_info_fn
= (get_vmalloc_info_t
)
1775 spl_kallsyms_lookup_name("get_vmalloc_info");
1776 if (!get_vmalloc_info_fn
)
1778 #endif /* HAVE_GET_VMALLOC_INFO */
1780 #ifndef HAVE_FIRST_ONLINE_PGDAT
1781 first_online_pgdat_fn
= (first_online_pgdat_t
)
1782 spl_kallsyms_lookup_name("first_online_pgdat");
1783 if (!first_online_pgdat_fn
)
1785 #endif /* HAVE_FIRST_ONLINE_PGDAT */
1787 #ifndef HAVE_NEXT_ONLINE_PGDAT
1788 next_online_pgdat_fn
= (next_online_pgdat_t
)
1789 spl_kallsyms_lookup_name("next_online_pgdat");
1790 if (!next_online_pgdat_fn
)
1792 #endif /* HAVE_NEXT_ONLINE_PGDAT */
1794 #ifndef HAVE_NEXT_ZONE
1795 next_zone_fn
= (next_zone_t
)
1796 spl_kallsyms_lookup_name("next_zone");
1799 #endif /* HAVE_NEXT_ZONE */
1801 #ifndef HAVE_GET_ZONE_COUNTS
1802 get_zone_counts_fn
= (get_zone_counts_t
)
1803 spl_kallsyms_lookup_name("get_zone_counts");
1804 if (!get_zone_counts_fn
)
1806 #endif /* HAVE_GET_ZONE_COUNTS */
1809 * It is now safe to initialize the global tunings which rely on
1810 * the use of the for_each_zone() macro. This macro in turns
1811 * depends on the *_pgdat symbols which are now available.
1813 spl_kmem_init_globals();
1824 init_rwsem(&spl_kmem_cache_sem
);
1825 INIT_LIST_HEAD(&spl_kmem_cache_list
);
1827 #ifdef HAVE_SET_SHRINKER
1828 spl_kmem_cache_shrinker
= set_shrinker(KMC_DEFAULT_SEEKS
,
1829 spl_kmem_cache_generic_shrinker
);
1830 if (spl_kmem_cache_shrinker
== NULL
)
1831 RETURN(rc
= -ENOMEM
);
1833 register_shrinker(&spl_kmem_cache_shrinker
);
1837 atomic64_set(&kmem_alloc_used
, 0);
1838 atomic64_set(&vmem_alloc_used
, 0);
1840 spl_kmem_init_tracking(&kmem_list
, &kmem_lock
, KMEM_TABLE_SIZE
);
1841 spl_kmem_init_tracking(&vmem_list
, &vmem_lock
, VMEM_TABLE_SIZE
);
1850 /* Display all unreclaimed memory addresses, including the
1851 * allocation size and the first few bytes of what's located
1852 * at that address to aid in debugging. Performance is not
1853 * a serious concern here since it is module unload time. */
1854 if (atomic64_read(&kmem_alloc_used
) != 0)
1855 CWARN("kmem leaked %ld/%ld bytes\n",
1856 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
1859 if (atomic64_read(&vmem_alloc_used
) != 0)
1860 CWARN("vmem leaked %ld/%ld bytes\n",
1861 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
1863 spl_kmem_fini_tracking(&kmem_list
, &kmem_lock
);
1864 spl_kmem_fini_tracking(&vmem_list
, &vmem_lock
);
1865 #endif /* DEBUG_KMEM */
1868 #ifdef HAVE_SET_SHRINKER
1869 remove_shrinker(spl_kmem_cache_shrinker
);
1871 unregister_shrinker(&spl_kmem_cache_shrinker
);