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_FIRST_ONLINE_PGDAT
84 first_online_pgdat(void)
86 return NODE_DATA(first_online_node
);
88 EXPORT_SYMBOL(first_online_pgdat
);
89 #endif /* HAVE_FIRST_ONLINE_PGDAT */
91 #ifndef HAVE_NEXT_ONLINE_PGDAT
93 next_online_pgdat(struct pglist_data
*pgdat
)
95 int nid
= next_online_node(pgdat
->node_id
);
97 if (nid
== MAX_NUMNODES
)
100 return NODE_DATA(nid
);
102 EXPORT_SYMBOL(next_online_pgdat
);
103 #endif /* HAVE_NEXT_ONLINE_PGDAT */
105 #ifndef HAVE_NEXT_ZONE
107 next_zone(struct zone
*zone
)
109 pg_data_t
*pgdat
= zone
->zone_pgdat
;
111 if (zone
< pgdat
->node_zones
+ MAX_NR_ZONES
- 1)
114 pgdat
= next_online_pgdat(pgdat
);
116 zone
= pgdat
->node_zones
;
122 EXPORT_SYMBOL(next_zone
);
123 #endif /* HAVE_NEXT_ZONE */
125 #ifndef HAVE_GET_ZONE_COUNTS
127 __get_zone_counts(unsigned long *active
, unsigned long *inactive
,
128 unsigned long *free
, struct pglist_data
*pgdat
)
130 struct zone
*zones
= pgdat
->node_zones
;
136 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
137 *active
+= zones
[i
].nr_active
;
138 *inactive
+= zones
[i
].nr_inactive
;
139 *free
+= zones
[i
].free_pages
;
144 get_zone_counts(unsigned long *active
, unsigned long *inactive
,
147 struct pglist_data
*pgdat
;
152 for_each_online_pgdat(pgdat
) {
153 unsigned long l
, m
, n
;
154 __get_zone_counts(&l
, &m
, &n
, pgdat
);
160 EXPORT_SYMBOL(get_zone_counts
);
161 #endif /* HAVE_GET_ZONE_COUNTS */
164 spl_kmem_availrmem(void)
166 unsigned long active
;
167 unsigned long inactive
;
170 get_zone_counts(&active
, &inactive
, &free
);
172 /* The amount of easily available memory */
173 return free
+ inactive
;
175 EXPORT_SYMBOL(spl_kmem_availrmem
);
178 vmem_size(vmem_t
*vmp
, int typemask
)
180 /* Arena's unsupported */
182 ASSERT(typemask
& (VMEM_ALLOC
| VMEM_FREE
));
186 EXPORT_SYMBOL(vmem_size
);
190 * Memory allocation interfaces and debugging for basic kmem_*
191 * and vmem_* style memory allocation. When DEBUG_KMEM is enable
192 * all allocations will be tracked when they are allocated and
193 * freed. When the SPL module is unload a list of all leaked
194 * addresses and where they were allocated will be dumped to the
195 * console. Enabling this feature has a significant impant on
196 * performance but it makes finding memory leaks staight forward.
199 /* Shim layer memory accounting */
200 atomic64_t kmem_alloc_used
= ATOMIC64_INIT(0);
201 unsigned long long kmem_alloc_max
= 0;
202 atomic64_t vmem_alloc_used
= ATOMIC64_INIT(0);
203 unsigned long long vmem_alloc_max
= 0;
204 int kmem_warning_flag
= 1;
206 EXPORT_SYMBOL(kmem_alloc_used
);
207 EXPORT_SYMBOL(kmem_alloc_max
);
208 EXPORT_SYMBOL(vmem_alloc_used
);
209 EXPORT_SYMBOL(vmem_alloc_max
);
210 EXPORT_SYMBOL(kmem_warning_flag
);
212 # ifdef DEBUG_KMEM_TRACKING
214 /* XXX - Not to surprisingly with debugging enabled the xmem_locks are very
215 * highly contended particularly on xfree(). If we want to run with this
216 * detailed debugging enabled for anything other than debugging we need to
217 * minimize the contention by moving to a lock per xmem_table entry model.
220 # define KMEM_HASH_BITS 10
221 # define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
223 # define VMEM_HASH_BITS 10
224 # define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS)
226 typedef struct kmem_debug
{
227 struct hlist_node kd_hlist
; /* Hash node linkage */
228 struct list_head kd_list
; /* List of all allocations */
229 void *kd_addr
; /* Allocation pointer */
230 size_t kd_size
; /* Allocation size */
231 const char *kd_func
; /* Allocation function */
232 int kd_line
; /* Allocation line */
235 spinlock_t kmem_lock
;
236 struct hlist_head kmem_table
[KMEM_TABLE_SIZE
];
237 struct list_head kmem_list
;
239 spinlock_t vmem_lock
;
240 struct hlist_head vmem_table
[VMEM_TABLE_SIZE
];
241 struct list_head vmem_list
;
243 EXPORT_SYMBOL(kmem_lock
);
244 EXPORT_SYMBOL(kmem_table
);
245 EXPORT_SYMBOL(kmem_list
);
247 EXPORT_SYMBOL(vmem_lock
);
248 EXPORT_SYMBOL(vmem_table
);
249 EXPORT_SYMBOL(vmem_list
);
252 int kmem_set_warning(int flag
) { return (kmem_warning_flag
= !!flag
); }
254 int kmem_set_warning(int flag
) { return 0; }
256 EXPORT_SYMBOL(kmem_set_warning
);
259 * Slab allocation interfaces
261 * While the Linux slab implementation was inspired by the Solaris
262 * implemenation I cannot use it to emulate the Solaris APIs. I
263 * require two features which are not provided by the Linux slab.
265 * 1) Constructors AND destructors. Recent versions of the Linux
266 * kernel have removed support for destructors. This is a deal
267 * breaker for the SPL which contains particularly expensive
268 * initializers for mutex's, condition variables, etc. We also
269 * require a minimal level of cleanup for these data types unlike
270 * many Linux data type which do need to be explicitly destroyed.
272 * 2) Virtual address space backed slab. Callers of the Solaris slab
273 * expect it to work well for both small are very large allocations.
274 * Because of memory fragmentation the Linux slab which is backed
275 * by kmalloc'ed memory performs very badly when confronted with
276 * large numbers of large allocations. Basing the slab on the
277 * virtual address space removes the need for contigeous pages
278 * and greatly improve performance for large allocations.
280 * For these reasons, the SPL has its own slab implementation with
281 * the needed features. It is not as highly optimized as either the
282 * Solaris or Linux slabs, but it should get me most of what is
283 * needed until it can be optimized or obsoleted by another approach.
285 * One serious concern I do have about this method is the relatively
286 * small virtual address space on 32bit arches. This will seriously
287 * constrain the size of the slab caches and their performance.
289 * XXX: Improve the partial slab list by carefully maintaining a
290 * strict ordering of fullest to emptiest slabs based on
291 * the slab reference count. This gaurentees the when freeing
292 * slabs back to the system we need only linearly traverse the
293 * last N slabs in the list to discover all the freeable slabs.
295 * XXX: NUMA awareness for optionally allocating memory close to a
296 * particular core. This can be adventageous if you know the slab
297 * object will be short lived and primarily accessed from one core.
299 * XXX: Slab coloring may also yield performance improvements and would
300 * be desirable to implement.
303 struct list_head spl_kmem_cache_list
; /* List of caches */
304 struct rw_semaphore spl_kmem_cache_sem
; /* Cache list lock */
306 static int spl_cache_flush(spl_kmem_cache_t
*skc
,
307 spl_kmem_magazine_t
*skm
, int flush
);
309 #ifdef HAVE_SET_SHRINKER
310 static struct shrinker
*spl_kmem_cache_shrinker
;
312 static int spl_kmem_cache_generic_shrinker(int nr_to_scan
,
313 unsigned int gfp_mask
);
314 static struct shrinker spl_kmem_cache_shrinker
= {
315 .shrink
= spl_kmem_cache_generic_shrinker
,
316 .seeks
= KMC_DEFAULT_SEEKS
,
321 # ifdef DEBUG_KMEM_TRACKING
323 static kmem_debug_t
*
324 kmem_del_init(spinlock_t
*lock
, struct hlist_head
*table
, int bits
,
327 struct hlist_head
*head
;
328 struct hlist_node
*node
;
329 struct kmem_debug
*p
;
333 spin_lock_irqsave(lock
, flags
);
335 head
= &table
[hash_ptr(addr
, bits
)];
336 hlist_for_each_entry_rcu(p
, node
, head
, kd_hlist
) {
337 if (p
->kd_addr
== addr
) {
338 hlist_del_init(&p
->kd_hlist
);
339 list_del_init(&p
->kd_list
);
340 spin_unlock_irqrestore(lock
, flags
);
345 spin_unlock_irqrestore(lock
, flags
);
351 kmem_alloc_track(size_t size
, int flags
, const char *func
, int line
,
352 int node_alloc
, int node
)
356 unsigned long irq_flags
;
359 dptr
= (kmem_debug_t
*) kmalloc(sizeof(kmem_debug_t
),
360 flags
& ~__GFP_ZERO
);
363 CWARN("kmem_alloc(%ld, 0x%x) debug failed\n",
364 sizeof(kmem_debug_t
), flags
);
366 /* Marked unlikely because we should never be doing this,
367 * we tolerate to up 2 pages but a single page is best. */
368 if (unlikely((size
) > (PAGE_SIZE
* 2)) && kmem_warning_flag
)
369 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
370 (unsigned long long) size
, flags
,
371 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
373 /* We use kstrdup() below because the string pointed to by
374 * __FUNCTION__ might not be available by the time we want
375 * to print it since the module might have been unloaded. */
376 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
377 if (unlikely(dptr
->kd_func
== NULL
)) {
379 CWARN("kstrdup() failed in kmem_alloc(%llu, 0x%x) "
380 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
381 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
385 /* Use the correct allocator */
387 ASSERT(!(flags
& __GFP_ZERO
));
388 ptr
= kmalloc_node(size
, flags
, node
);
389 } else if (flags
& __GFP_ZERO
) {
390 ptr
= kzalloc(size
, flags
& ~__GFP_ZERO
);
392 ptr
= kmalloc(size
, flags
);
395 if (unlikely(ptr
== NULL
)) {
396 kfree(dptr
->kd_func
);
398 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
399 (unsigned long long) size
, flags
,
400 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
404 atomic64_add(size
, &kmem_alloc_used
);
405 if (unlikely(atomic64_read(&kmem_alloc_used
) >
408 atomic64_read(&kmem_alloc_used
);
410 INIT_HLIST_NODE(&dptr
->kd_hlist
);
411 INIT_LIST_HEAD(&dptr
->kd_list
);
414 dptr
->kd_size
= size
;
415 dptr
->kd_line
= line
;
417 spin_lock_irqsave(&kmem_lock
, irq_flags
);
418 hlist_add_head_rcu(&dptr
->kd_hlist
,
419 &kmem_table
[hash_ptr(ptr
, KMEM_HASH_BITS
)]);
420 list_add_tail(&dptr
->kd_list
, &kmem_list
);
421 spin_unlock_irqrestore(&kmem_lock
, irq_flags
);
423 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
424 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
425 ptr
, atomic64_read(&kmem_alloc_used
),
431 EXPORT_SYMBOL(kmem_alloc_track
);
434 kmem_free_track(void *ptr
, size_t size
)
439 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
440 (unsigned long long) size
);
442 dptr
= kmem_del_init(&kmem_lock
, kmem_table
, KMEM_HASH_BITS
, ptr
);
444 ASSERT(dptr
); /* Must exist in hash due to kmem_alloc() */
446 /* Size must match */
447 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
448 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
449 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
451 atomic64_sub(size
, &kmem_alloc_used
);
453 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
454 (unsigned long long) size
, atomic64_read(&kmem_alloc_used
),
457 kfree(dptr
->kd_func
);
459 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
462 memset(ptr
, 0x5a, size
);
467 EXPORT_SYMBOL(kmem_free_track
);
470 vmem_alloc_track(size_t size
, int flags
, const char *func
, int line
)
474 unsigned long irq_flags
;
477 ASSERT(flags
& KM_SLEEP
);
479 dptr
= (kmem_debug_t
*) kmalloc(sizeof(kmem_debug_t
), flags
);
481 CWARN("vmem_alloc(%ld, 0x%x) debug failed\n",
482 sizeof(kmem_debug_t
), flags
);
484 /* We use kstrdup() below because the string pointed to by
485 * __FUNCTION__ might not be available by the time we want
486 * to print it, since the module might have been unloaded. */
487 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
488 if (unlikely(dptr
->kd_func
== NULL
)) {
490 CWARN("kstrdup() failed in vmem_alloc(%llu, 0x%x) "
491 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
492 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
496 ptr
= __vmalloc(size
, (flags
| __GFP_HIGHMEM
) & ~__GFP_ZERO
,
499 if (unlikely(ptr
== NULL
)) {
500 kfree(dptr
->kd_func
);
502 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
503 (unsigned long long) size
, flags
,
504 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
508 if (flags
& __GFP_ZERO
)
509 memset(ptr
, 0, size
);
511 atomic64_add(size
, &vmem_alloc_used
);
512 if (unlikely(atomic64_read(&vmem_alloc_used
) >
515 atomic64_read(&vmem_alloc_used
);
517 INIT_HLIST_NODE(&dptr
->kd_hlist
);
518 INIT_LIST_HEAD(&dptr
->kd_list
);
521 dptr
->kd_size
= size
;
522 dptr
->kd_line
= line
;
524 spin_lock_irqsave(&vmem_lock
, irq_flags
);
525 hlist_add_head_rcu(&dptr
->kd_hlist
,
526 &vmem_table
[hash_ptr(ptr
, VMEM_HASH_BITS
)]);
527 list_add_tail(&dptr
->kd_list
, &vmem_list
);
528 spin_unlock_irqrestore(&vmem_lock
, irq_flags
);
530 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
531 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
532 ptr
, atomic64_read(&vmem_alloc_used
),
538 EXPORT_SYMBOL(vmem_alloc_track
);
541 vmem_free_track(void *ptr
, size_t size
)
546 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
547 (unsigned long long) size
);
549 dptr
= kmem_del_init(&vmem_lock
, vmem_table
, VMEM_HASH_BITS
, ptr
);
550 ASSERT(dptr
); /* Must exist in hash due to vmem_alloc() */
552 /* Size must match */
553 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
554 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
555 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
557 atomic64_sub(size
, &vmem_alloc_used
);
558 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
559 (unsigned long long) size
, atomic64_read(&vmem_alloc_used
),
562 kfree(dptr
->kd_func
);
564 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
567 memset(ptr
, 0x5a, size
);
572 EXPORT_SYMBOL(vmem_free_track
);
574 # else /* DEBUG_KMEM_TRACKING */
577 kmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
,
578 int node_alloc
, int node
)
583 /* Marked unlikely because we should never be doing this,
584 * we tolerate to up 2 pages but a single page is best. */
585 if (unlikely(size
> (PAGE_SIZE
* 2)) && kmem_warning_flag
)
586 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
587 (unsigned long long) size
, flags
,
588 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
590 /* Use the correct allocator */
592 ASSERT(!(flags
& __GFP_ZERO
));
593 ptr
= kmalloc_node(size
, flags
, node
);
594 } else if (flags
& __GFP_ZERO
) {
595 ptr
= kzalloc(size
, flags
& (~__GFP_ZERO
));
597 ptr
= kmalloc(size
, flags
);
601 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
602 (unsigned long long) size
, flags
,
603 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
605 atomic64_add(size
, &kmem_alloc_used
);
606 if (unlikely(atomic64_read(&kmem_alloc_used
) > kmem_alloc_max
))
607 kmem_alloc_max
= atomic64_read(&kmem_alloc_used
);
609 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
610 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
611 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
615 EXPORT_SYMBOL(kmem_alloc_debug
);
618 kmem_free_debug(void *ptr
, size_t size
)
622 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
623 (unsigned long long) size
);
625 atomic64_sub(size
, &kmem_alloc_used
);
627 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
628 (unsigned long long) size
, atomic64_read(&kmem_alloc_used
),
631 memset(ptr
, 0x5a, size
);
636 EXPORT_SYMBOL(kmem_free_debug
);
639 vmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
)
644 ASSERT(flags
& KM_SLEEP
);
646 ptr
= __vmalloc(size
, (flags
| __GFP_HIGHMEM
) & ~__GFP_ZERO
,
649 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
650 (unsigned long long) size
, flags
,
651 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
653 if (flags
& __GFP_ZERO
)
654 memset(ptr
, 0, size
);
656 atomic64_add(size
, &vmem_alloc_used
);
658 if (unlikely(atomic64_read(&vmem_alloc_used
) > vmem_alloc_max
))
659 vmem_alloc_max
= atomic64_read(&vmem_alloc_used
);
661 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
662 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
663 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
668 EXPORT_SYMBOL(vmem_alloc_debug
);
671 vmem_free_debug(void *ptr
, size_t size
)
675 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
676 (unsigned long long) size
);
678 atomic64_sub(size
, &vmem_alloc_used
);
680 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
681 (unsigned long long) size
, atomic64_read(&vmem_alloc_used
),
684 memset(ptr
, 0x5a, size
);
689 EXPORT_SYMBOL(vmem_free_debug
);
691 # endif /* DEBUG_KMEM_TRACKING */
692 #endif /* DEBUG_KMEM */
695 kv_alloc(spl_kmem_cache_t
*skc
, int size
, int flags
)
699 if (skc
->skc_flags
& KMC_KMEM
) {
700 if (size
> (2 * PAGE_SIZE
)) {
701 ptr
= (void *)__get_free_pages(flags
, get_order(size
));
703 ptr
= kmem_alloc(size
, flags
);
705 ptr
= vmem_alloc(size
, flags
);
712 kv_free(spl_kmem_cache_t
*skc
, void *ptr
, int size
)
714 if (skc
->skc_flags
& KMC_KMEM
) {
715 if (size
> (2 * PAGE_SIZE
))
716 free_pages((unsigned long)ptr
, get_order(size
));
718 kmem_free(ptr
, size
);
720 vmem_free(ptr
, size
);
725 * It's important that we pack the spl_kmem_obj_t structure and the
726 * actual objects in to one large address space to minimize the number
727 * of calls to the allocator. It is far better to do a few large
728 * allocations and then subdivide it ourselves. Now which allocator
729 * we use requires balancing a few trade offs.
731 * For small objects we use kmem_alloc() because as long as you are
732 * only requesting a small number of pages (ideally just one) its cheap.
733 * However, when you start requesting multiple pages with kmem_alloc()
734 * it gets increasingly expensive since it requires contigeous pages.
735 * For this reason we shift to vmem_alloc() for slabs of large objects
736 * which removes the need for contigeous pages. We do not use
737 * vmem_alloc() in all cases because there is significant locking
738 * overhead in __get_vm_area_node(). This function takes a single
739 * global lock when aquiring an available virtual address range which
740 * serializes all vmem_alloc()'s for all slab caches. Using slightly
741 * different allocation functions for small and large objects should
742 * give us the best of both worlds.
744 * KMC_ONSLAB KMC_OFFSLAB
746 * +------------------------+ +-----------------+
747 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
748 * | skc_obj_size <-+ | | +-----------------+ | |
749 * | spl_kmem_obj_t | | | |
750 * | skc_obj_size <---+ | +-----------------+ | |
751 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
752 * | ... v | | spl_kmem_obj_t | |
753 * +------------------------+ +-----------------+ v
755 static spl_kmem_slab_t
*
756 spl_slab_alloc(spl_kmem_cache_t
*skc
, int flags
)
758 spl_kmem_slab_t
*sks
;
759 spl_kmem_obj_t
*sko
, *n
;
761 int i
, align
, size
, rc
= 0;
763 base
= kv_alloc(skc
, skc
->skc_slab_size
, flags
);
767 sks
= (spl_kmem_slab_t
*)base
;
768 sks
->sks_magic
= SKS_MAGIC
;
769 sks
->sks_objs
= skc
->skc_slab_objs
;
770 sks
->sks_age
= jiffies
;
771 sks
->sks_cache
= skc
;
772 INIT_LIST_HEAD(&sks
->sks_list
);
773 INIT_LIST_HEAD(&sks
->sks_free_list
);
776 align
= skc
->skc_obj_align
;
777 size
= P2ROUNDUP(skc
->skc_obj_size
, align
) +
778 P2ROUNDUP(sizeof(spl_kmem_obj_t
), align
);
780 for (i
= 0; i
< sks
->sks_objs
; i
++) {
781 if (skc
->skc_flags
& KMC_OFFSLAB
) {
782 obj
= kv_alloc(skc
, size
, flags
);
784 GOTO(out
, rc
= -ENOMEM
);
787 P2ROUNDUP(sizeof(spl_kmem_slab_t
), align
) +
791 sko
= obj
+ P2ROUNDUP(skc
->skc_obj_size
, align
);
793 sko
->sko_magic
= SKO_MAGIC
;
795 INIT_LIST_HEAD(&sko
->sko_list
);
796 list_add_tail(&sko
->sko_list
, &sks
->sks_free_list
);
799 list_for_each_entry(sko
, &sks
->sks_free_list
, sko_list
)
801 skc
->skc_ctor(sko
->sko_addr
, skc
->skc_private
, flags
);
804 if (skc
->skc_flags
& KMC_OFFSLAB
)
805 list_for_each_entry_safe(sko
, n
, &sks
->sks_free_list
,
807 kv_free(skc
, sko
->sko_addr
, size
);
809 kv_free(skc
, base
, skc
->skc_slab_size
);
817 * Remove a slab from complete or partial list, it must be called with
818 * the 'skc->skc_lock' held but the actual free must be performed
819 * outside the lock to prevent deadlocking on vmem addresses.
822 spl_slab_free(spl_kmem_slab_t
*sks
,
823 struct list_head
*sks_list
, struct list_head
*sko_list
)
825 spl_kmem_cache_t
*skc
;
826 spl_kmem_obj_t
*sko
, *n
;
829 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
830 ASSERT(sks
->sks_ref
== 0);
832 skc
= sks
->sks_cache
;
833 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
834 ASSERT(spin_is_locked(&skc
->skc_lock
));
836 skc
->skc_obj_total
-= sks
->sks_objs
;
837 skc
->skc_slab_total
--;
838 list_del(&sks
->sks_list
);
840 /* Run destructors slab is being released */
841 list_for_each_entry_safe(sko
, n
, &sks
->sks_free_list
, sko_list
) {
842 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
843 list_del(&sko
->sko_list
);
846 skc
->skc_dtor(sko
->sko_addr
, skc
->skc_private
);
848 if (skc
->skc_flags
& KMC_OFFSLAB
)
849 list_add(&sko
->sko_list
, sko_list
);
852 list_add(&sks
->sks_list
, sks_list
);
857 * Traverses all the partial slabs attached to a cache and free those
858 * which which are currently empty, and have not been touched for
859 * skc_delay seconds to avoid thrashing. The count argument is
860 * passed to optionally cap the number of slabs reclaimed, a count
861 * of zero means try and reclaim everything. When flag is set we
862 * always free an available slab regardless of age.
865 spl_slab_reclaim(spl_kmem_cache_t
*skc
, int count
, int flag
)
867 spl_kmem_slab_t
*sks
, *m
;
868 spl_kmem_obj_t
*sko
, *n
;
875 * Move empty slabs and objects which have not been touched in
876 * skc_delay seconds on to private lists to be freed outside
877 * the spin lock. This delay time is important to avoid
878 * thrashing however when flag is set the delay will not be
879 * used. Empty slabs will be at the end of the skc_partial_list.
881 spin_lock(&skc
->skc_lock
);
882 list_for_each_entry_safe_reverse(sks
, m
, &skc
->skc_partial_list
,
884 /* Release at most count slabs */
885 if (count
&& i
> count
)
888 /* Skip active slabs */
889 if (sks
->sks_ref
> 0)
892 if (time_after(jiffies
,sks
->sks_age
+skc
->skc_delay
*HZ
)||flag
) {
893 spl_slab_free(sks
, &sks_list
, &sko_list
);
897 spin_unlock(&skc
->skc_lock
);
900 * We only have list of spl_kmem_obj_t's if they are located off
901 * the slab, otherwise they get feed with the spl_kmem_slab_t.
903 if (!list_empty(&sko_list
)) {
904 ASSERT(skc
->skc_flags
& KMC_OFFSLAB
);
906 size
= P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
) +
907 P2ROUNDUP(sizeof(spl_kmem_obj_t
), skc
->skc_obj_align
);
909 /* To avoid soft lockups conditionally reschedule */
910 list_for_each_entry_safe(sko
, n
, &sko_list
, sko_list
) {
911 kv_free(skc
, sko
->sko_addr
, size
);
916 /* To avoid soft lockups conditionally reschedule */
917 list_for_each_entry_safe(sks
, m
, &sks_list
, sks_list
) {
918 kv_free(skc
, sks
, skc
->skc_slab_size
);
926 * Called regularly on all caches to age objects out of the magazines
927 * which have not been access in skc->skc_delay seconds. This prevents
928 * idle magazines from holding memory which might be better used by
929 * other caches or parts of the system. The delay is present to
930 * prevent thrashing the magazine.
933 spl_magazine_age(void *data
)
935 spl_kmem_cache_t
*skc
= data
;
936 spl_kmem_magazine_t
*skm
= skc
->skc_mag
[smp_processor_id()];
938 if (skm
->skm_avail
> 0 &&
939 time_after(jiffies
, skm
->skm_age
+ skc
->skc_delay
* HZ
))
940 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
944 * Called regularly to keep a downward pressure on the size of idle
945 * magazines and to release free slabs from the cache. This function
946 * never calls the registered reclaim function, that only occures
947 * under memory pressure or with a direct call to spl_kmem_reap().
950 spl_cache_age(void *data
)
952 spl_kmem_cache_t
*skc
=
953 spl_get_work_data(data
, spl_kmem_cache_t
, skc_work
.work
);
955 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
956 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
957 spl_on_each_cpu(spl_magazine_age
, skc
, 0);
959 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
960 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
964 * Size a slab based on the size of each aliged object plus spl_kmem_obj_t.
965 * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However,
966 * for very small objects we may end up with more than this so as not
967 * to waste space in the minimal allocation of a single page. Also for
968 * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN,
969 * lower than this and we will fail.
972 spl_slab_size(spl_kmem_cache_t
*skc
, uint32_t *objs
, uint32_t *size
)
974 int sks_size
, obj_size
, max_size
, align
;
976 if (skc
->skc_flags
& KMC_OFFSLAB
) {
977 *objs
= SPL_KMEM_CACHE_OBJ_PER_SLAB
;
978 *size
= sizeof(spl_kmem_slab_t
);
980 align
= skc
->skc_obj_align
;
981 sks_size
= P2ROUNDUP(sizeof(spl_kmem_slab_t
), align
);
982 obj_size
= P2ROUNDUP(skc
->skc_obj_size
, align
) +
983 P2ROUNDUP(sizeof(spl_kmem_obj_t
), align
);
985 if (skc
->skc_flags
& KMC_KMEM
)
986 max_size
= ((uint64_t)1 << (MAX_ORDER
-1)) * PAGE_SIZE
;
988 max_size
= (32 * 1024 * 1024);
990 for (*size
= PAGE_SIZE
; *size
<= max_size
; *size
+= PAGE_SIZE
) {
991 *objs
= (*size
- sks_size
) / obj_size
;
992 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB
)
997 * Unable to satisfy target objets per slab, fallback to
998 * allocating a maximally sized slab and assuming it can
999 * contain the minimum objects count use it. If not fail.
1002 *objs
= (*size
- sks_size
) / obj_size
;
1003 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN
)
1011 * Make a guess at reasonable per-cpu magazine size based on the size of
1012 * each object and the cost of caching N of them in each magazine. Long
1013 * term this should really adapt based on an observed usage heuristic.
1016 spl_magazine_size(spl_kmem_cache_t
*skc
)
1018 int size
, align
= skc
->skc_obj_align
;
1021 /* Per-magazine sizes below assume a 4Kib page size */
1022 if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
* 256))
1023 size
= 4; /* Minimum 4Mib per-magazine */
1024 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
* 32))
1025 size
= 16; /* Minimum 2Mib per-magazine */
1026 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
))
1027 size
= 64; /* Minimum 256Kib per-magazine */
1028 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
/ 4))
1029 size
= 128; /* Minimum 128Kib per-magazine */
1037 * Allocate a per-cpu magazine to assoicate with a specific core.
1039 static spl_kmem_magazine_t
*
1040 spl_magazine_alloc(spl_kmem_cache_t
*skc
, int node
)
1042 spl_kmem_magazine_t
*skm
;
1043 int size
= sizeof(spl_kmem_magazine_t
) +
1044 sizeof(void *) * skc
->skc_mag_size
;
1047 skm
= kmem_alloc_node(size
, GFP_KERNEL
| __GFP_NOFAIL
, node
);
1049 skm
->skm_magic
= SKM_MAGIC
;
1051 skm
->skm_size
= skc
->skc_mag_size
;
1052 skm
->skm_refill
= skc
->skc_mag_refill
;
1053 skm
->skm_age
= jiffies
;
1060 * Free a per-cpu magazine assoicated with a specific core.
1063 spl_magazine_free(spl_kmem_magazine_t
*skm
)
1065 int size
= sizeof(spl_kmem_magazine_t
) +
1066 sizeof(void *) * skm
->skm_size
;
1069 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1070 ASSERT(skm
->skm_avail
== 0);
1072 kmem_free(skm
, size
);
1077 * Create all pre-cpu magazines of reasonable sizes.
1080 spl_magazine_create(spl_kmem_cache_t
*skc
)
1085 skc
->skc_mag_size
= spl_magazine_size(skc
);
1086 skc
->skc_mag_refill
= (skc
->skc_mag_size
+ 1) / 2;
1088 for_each_online_cpu(i
) {
1089 skc
->skc_mag
[i
] = spl_magazine_alloc(skc
, cpu_to_node(i
));
1090 if (!skc
->skc_mag
[i
]) {
1091 for (i
--; i
>= 0; i
--)
1092 spl_magazine_free(skc
->skc_mag
[i
]);
1102 * Destroy all pre-cpu magazines.
1105 spl_magazine_destroy(spl_kmem_cache_t
*skc
)
1107 spl_kmem_magazine_t
*skm
;
1111 for_each_online_cpu(i
) {
1112 skm
= skc
->skc_mag
[i
];
1113 (void)spl_cache_flush(skc
, skm
, skm
->skm_avail
);
1114 spl_magazine_free(skm
);
1121 * Create a object cache based on the following arguments:
1123 * size cache object size
1124 * align cache object alignment
1125 * ctor cache object constructor
1126 * dtor cache object destructor
1127 * reclaim cache object reclaim
1128 * priv cache private data for ctor/dtor/reclaim
1129 * vmp unused must be NULL
1131 * KMC_NOTOUCH Disable cache object aging (unsupported)
1132 * KMC_NODEBUG Disable debugging (unsupported)
1133 * KMC_NOMAGAZINE Disable magazine (unsupported)
1134 * KMC_NOHASH Disable hashing (unsupported)
1135 * KMC_QCACHE Disable qcache (unsupported)
1136 * KMC_KMEM Force kmem backed cache
1137 * KMC_VMEM Force vmem backed cache
1138 * KMC_OFFSLAB Locate objects off the slab
1141 spl_kmem_cache_create(char *name
, size_t size
, size_t align
,
1142 spl_kmem_ctor_t ctor
,
1143 spl_kmem_dtor_t dtor
,
1144 spl_kmem_reclaim_t reclaim
,
1145 void *priv
, void *vmp
, int flags
)
1147 spl_kmem_cache_t
*skc
;
1148 int rc
, kmem_flags
= KM_SLEEP
;
1151 ASSERTF(!(flags
& KMC_NOMAGAZINE
), "Bad KMC_NOMAGAZINE (%x)\n", flags
);
1152 ASSERTF(!(flags
& KMC_NOHASH
), "Bad KMC_NOHASH (%x)\n", flags
);
1153 ASSERTF(!(flags
& KMC_QCACHE
), "Bad KMC_QCACHE (%x)\n", flags
);
1154 ASSERT(vmp
== NULL
);
1156 /* We may be called when there is a non-zero preempt_count or
1157 * interrupts are disabled is which case we must not sleep.
1159 if (current_thread_info()->preempt_count
|| irqs_disabled())
1160 kmem_flags
= KM_NOSLEEP
;
1162 /* Allocate new cache memory and initialize. */
1163 skc
= (spl_kmem_cache_t
*)kmem_zalloc(sizeof(*skc
), kmem_flags
);
1167 skc
->skc_magic
= SKC_MAGIC
;
1168 skc
->skc_name_size
= strlen(name
) + 1;
1169 skc
->skc_name
= (char *)kmem_alloc(skc
->skc_name_size
, kmem_flags
);
1170 if (skc
->skc_name
== NULL
) {
1171 kmem_free(skc
, sizeof(*skc
));
1174 strncpy(skc
->skc_name
, name
, skc
->skc_name_size
);
1176 skc
->skc_ctor
= ctor
;
1177 skc
->skc_dtor
= dtor
;
1178 skc
->skc_reclaim
= reclaim
;
1179 skc
->skc_private
= priv
;
1181 skc
->skc_flags
= flags
;
1182 skc
->skc_obj_size
= size
;
1183 skc
->skc_obj_align
= SPL_KMEM_CACHE_ALIGN
;
1184 skc
->skc_delay
= SPL_KMEM_CACHE_DELAY
;
1185 skc
->skc_reap
= SPL_KMEM_CACHE_REAP
;
1186 atomic_set(&skc
->skc_ref
, 0);
1188 INIT_LIST_HEAD(&skc
->skc_list
);
1189 INIT_LIST_HEAD(&skc
->skc_complete_list
);
1190 INIT_LIST_HEAD(&skc
->skc_partial_list
);
1191 spin_lock_init(&skc
->skc_lock
);
1192 skc
->skc_slab_fail
= 0;
1193 skc
->skc_slab_create
= 0;
1194 skc
->skc_slab_destroy
= 0;
1195 skc
->skc_slab_total
= 0;
1196 skc
->skc_slab_alloc
= 0;
1197 skc
->skc_slab_max
= 0;
1198 skc
->skc_obj_total
= 0;
1199 skc
->skc_obj_alloc
= 0;
1200 skc
->skc_obj_max
= 0;
1203 ASSERT((align
& (align
- 1)) == 0); /* Power of two */
1204 ASSERT(align
>= SPL_KMEM_CACHE_ALIGN
); /* Minimum size */
1205 skc
->skc_obj_align
= align
;
1208 /* If none passed select a cache type based on object size */
1209 if (!(skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
))) {
1210 if (P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
) <
1212 skc
->skc_flags
|= KMC_KMEM
;
1214 skc
->skc_flags
|= KMC_VMEM
;
1218 rc
= spl_slab_size(skc
, &skc
->skc_slab_objs
, &skc
->skc_slab_size
);
1222 rc
= spl_magazine_create(skc
);
1226 spl_init_delayed_work(&skc
->skc_work
, spl_cache_age
, skc
);
1227 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
1229 down_write(&spl_kmem_cache_sem
);
1230 list_add_tail(&skc
->skc_list
, &spl_kmem_cache_list
);
1231 up_write(&spl_kmem_cache_sem
);
1235 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1236 kmem_free(skc
, sizeof(*skc
));
1239 EXPORT_SYMBOL(spl_kmem_cache_create
);
1242 * Destroy a cache and all objects assoicated with the cache.
1245 spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
)
1247 DECLARE_WAIT_QUEUE_HEAD(wq
);
1250 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1252 down_write(&spl_kmem_cache_sem
);
1253 list_del_init(&skc
->skc_list
);
1254 up_write(&spl_kmem_cache_sem
);
1256 /* Cancel any and wait for any pending delayed work */
1257 ASSERT(!test_and_set_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1258 cancel_delayed_work(&skc
->skc_work
);
1259 flush_scheduled_work();
1261 /* Wait until all current callers complete, this is mainly
1262 * to catch the case where a low memory situation triggers a
1263 * cache reaping action which races with this destroy. */
1264 wait_event(wq
, atomic_read(&skc
->skc_ref
) == 0);
1266 spl_magazine_destroy(skc
);
1267 spl_slab_reclaim(skc
, 0, 1);
1268 spin_lock(&skc
->skc_lock
);
1270 /* Validate there are no objects in use and free all the
1271 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
1272 ASSERT3U(skc
->skc_slab_alloc
, ==, 0);
1273 ASSERT3U(skc
->skc_obj_alloc
, ==, 0);
1274 ASSERT3U(skc
->skc_slab_total
, ==, 0);
1275 ASSERT3U(skc
->skc_obj_total
, ==, 0);
1276 ASSERT(list_empty(&skc
->skc_complete_list
));
1278 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1279 spin_unlock(&skc
->skc_lock
);
1281 kmem_free(skc
, sizeof(*skc
));
1285 EXPORT_SYMBOL(spl_kmem_cache_destroy
);
1288 * Allocate an object from a slab attached to the cache. This is used to
1289 * repopulate the per-cpu magazine caches in batches when they run low.
1292 spl_cache_obj(spl_kmem_cache_t
*skc
, spl_kmem_slab_t
*sks
)
1294 spl_kmem_obj_t
*sko
;
1296 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1297 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1298 ASSERT(spin_is_locked(&skc
->skc_lock
));
1300 sko
= list_entry(sks
->sks_free_list
.next
, spl_kmem_obj_t
, sko_list
);
1301 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1302 ASSERT(sko
->sko_addr
!= NULL
);
1304 /* Remove from sks_free_list */
1305 list_del_init(&sko
->sko_list
);
1307 sks
->sks_age
= jiffies
;
1309 skc
->skc_obj_alloc
++;
1311 /* Track max obj usage statistics */
1312 if (skc
->skc_obj_alloc
> skc
->skc_obj_max
)
1313 skc
->skc_obj_max
= skc
->skc_obj_alloc
;
1315 /* Track max slab usage statistics */
1316 if (sks
->sks_ref
== 1) {
1317 skc
->skc_slab_alloc
++;
1319 if (skc
->skc_slab_alloc
> skc
->skc_slab_max
)
1320 skc
->skc_slab_max
= skc
->skc_slab_alloc
;
1323 return sko
->sko_addr
;
1327 * No available objects on any slabsi, create a new slab. Since this
1328 * is an expensive operation we do it without holding the spinlock and
1329 * only briefly aquire it when we link in the fully allocated and
1332 static spl_kmem_slab_t
*
1333 spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
)
1335 spl_kmem_slab_t
*sks
;
1338 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1343 * Before allocating a new slab check if the slab is being reaped.
1344 * If it is there is a good chance we can wait until it finishes
1345 * and then use one of the newly freed but not aged-out slabs.
1347 if (test_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1349 GOTO(out
, sks
= NULL
);
1352 /* Allocate a new slab for the cache */
1353 sks
= spl_slab_alloc(skc
, flags
| __GFP_NORETRY
| __GFP_NOWARN
);
1355 GOTO(out
, sks
= NULL
);
1357 /* Link the new empty slab in to the end of skc_partial_list. */
1358 spin_lock(&skc
->skc_lock
);
1359 skc
->skc_slab_total
++;
1360 skc
->skc_obj_total
+= sks
->sks_objs
;
1361 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1362 spin_unlock(&skc
->skc_lock
);
1364 local_irq_disable();
1370 * Refill a per-cpu magazine with objects from the slabs for this
1371 * cache. Ideally the magazine can be repopulated using existing
1372 * objects which have been released, however if we are unable to
1373 * locate enough free objects new slabs of objects will be created.
1376 spl_cache_refill(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flags
)
1378 spl_kmem_slab_t
*sks
;
1382 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1383 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1385 refill
= MIN(skm
->skm_refill
, skm
->skm_size
- skm
->skm_avail
);
1386 spin_lock(&skc
->skc_lock
);
1388 while (refill
> 0) {
1389 /* No slabs available we may need to grow the cache */
1390 if (list_empty(&skc
->skc_partial_list
)) {
1391 spin_unlock(&skc
->skc_lock
);
1393 sks
= spl_cache_grow(skc
, flags
);
1397 /* Rescheduled to different CPU skm is not local */
1398 if (skm
!= skc
->skc_mag
[smp_processor_id()])
1401 /* Potentially rescheduled to the same CPU but
1402 * allocations may have occured from this CPU while
1403 * we were sleeping so recalculate max refill. */
1404 refill
= MIN(refill
, skm
->skm_size
- skm
->skm_avail
);
1406 spin_lock(&skc
->skc_lock
);
1410 /* Grab the next available slab */
1411 sks
= list_entry((&skc
->skc_partial_list
)->next
,
1412 spl_kmem_slab_t
, sks_list
);
1413 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1414 ASSERT(sks
->sks_ref
< sks
->sks_objs
);
1415 ASSERT(!list_empty(&sks
->sks_free_list
));
1417 /* Consume as many objects as needed to refill the requested
1418 * cache. We must also be careful not to overfill it. */
1419 while (sks
->sks_ref
< sks
->sks_objs
&& refill
-- > 0 && ++rc
) {
1420 ASSERT(skm
->skm_avail
< skm
->skm_size
);
1421 ASSERT(rc
< skm
->skm_size
);
1422 skm
->skm_objs
[skm
->skm_avail
++]=spl_cache_obj(skc
,sks
);
1425 /* Move slab to skc_complete_list when full */
1426 if (sks
->sks_ref
== sks
->sks_objs
) {
1427 list_del(&sks
->sks_list
);
1428 list_add(&sks
->sks_list
, &skc
->skc_complete_list
);
1432 spin_unlock(&skc
->skc_lock
);
1434 /* Returns the number of entries added to cache */
1439 * Release an object back to the slab from which it came.
1442 spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
)
1444 spl_kmem_slab_t
*sks
= NULL
;
1445 spl_kmem_obj_t
*sko
= NULL
;
1448 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1449 ASSERT(spin_is_locked(&skc
->skc_lock
));
1451 sko
= obj
+ P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
);
1452 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1454 sks
= sko
->sko_slab
;
1455 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1456 ASSERT(sks
->sks_cache
== skc
);
1457 list_add(&sko
->sko_list
, &sks
->sks_free_list
);
1459 sks
->sks_age
= jiffies
;
1461 skc
->skc_obj_alloc
--;
1463 /* Move slab to skc_partial_list when no longer full. Slabs
1464 * are added to the head to keep the partial list is quasi-full
1465 * sorted order. Fuller at the head, emptier at the tail. */
1466 if (sks
->sks_ref
== (sks
->sks_objs
- 1)) {
1467 list_del(&sks
->sks_list
);
1468 list_add(&sks
->sks_list
, &skc
->skc_partial_list
);
1471 /* Move emply slabs to the end of the partial list so
1472 * they can be easily found and freed during reclamation. */
1473 if (sks
->sks_ref
== 0) {
1474 list_del(&sks
->sks_list
);
1475 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1476 skc
->skc_slab_alloc
--;
1483 * Release a batch of objects from a per-cpu magazine back to their
1484 * respective slabs. This occurs when we exceed the magazine size,
1485 * are under memory pressure, when the cache is idle, or during
1486 * cache cleanup. The flush argument contains the number of entries
1487 * to remove from the magazine.
1490 spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
1492 int i
, count
= MIN(flush
, skm
->skm_avail
);
1495 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1496 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1499 * XXX: Currently we simply return objects from the magazine to
1500 * the slabs in fifo order. The ideal thing to do from a memory
1501 * fragmentation standpoint is to cheaply determine the set of
1502 * objects in the magazine which will result in the largest
1503 * number of free slabs if released from the magazine.
1505 spin_lock(&skc
->skc_lock
);
1506 for (i
= 0; i
< count
; i
++)
1507 spl_cache_shrink(skc
, skm
->skm_objs
[i
]);
1509 skm
->skm_avail
-= count
;
1510 memmove(skm
->skm_objs
, &(skm
->skm_objs
[count
]),
1511 sizeof(void *) * skm
->skm_avail
);
1513 spin_unlock(&skc
->skc_lock
);
1519 * Allocate an object from the per-cpu magazine, or if the magazine
1520 * is empty directly allocate from a slab and repopulate the magazine.
1523 spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
)
1525 spl_kmem_magazine_t
*skm
;
1526 unsigned long irq_flags
;
1530 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1531 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1532 ASSERT(flags
& KM_SLEEP
);
1533 atomic_inc(&skc
->skc_ref
);
1534 local_irq_save(irq_flags
);
1537 /* Safe to update per-cpu structure without lock, but
1538 * in the restart case we must be careful to reaquire
1539 * the local magazine since this may have changed
1540 * when we need to grow the cache. */
1541 skm
= skc
->skc_mag
[smp_processor_id()];
1542 ASSERTF(skm
->skm_magic
== SKM_MAGIC
, "%x != %x: %s/%p/%p %x/%x/%x\n",
1543 skm
->skm_magic
, SKM_MAGIC
, skc
->skc_name
, skc
, skm
,
1544 skm
->skm_size
, skm
->skm_refill
, skm
->skm_avail
);
1546 if (likely(skm
->skm_avail
)) {
1547 /* Object available in CPU cache, use it */
1548 obj
= skm
->skm_objs
[--skm
->skm_avail
];
1549 skm
->skm_age
= jiffies
;
1551 /* Per-CPU cache empty, directly allocate from
1552 * the slab and refill the per-CPU cache. */
1553 (void)spl_cache_refill(skc
, skm
, flags
);
1554 GOTO(restart
, obj
= NULL
);
1557 local_irq_restore(irq_flags
);
1559 ASSERT(((unsigned long)(obj
) % skc
->skc_obj_align
) == 0);
1561 /* Pre-emptively migrate object to CPU L1 cache */
1563 atomic_dec(&skc
->skc_ref
);
1567 EXPORT_SYMBOL(spl_kmem_cache_alloc
);
1570 * Free an object back to the local per-cpu magazine, there is no
1571 * guarantee that this is the same magazine the object was originally
1572 * allocated from. We may need to flush entire from the magazine
1573 * back to the slabs to make space.
1576 spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
)
1578 spl_kmem_magazine_t
*skm
;
1579 unsigned long flags
;
1582 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1583 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1584 atomic_inc(&skc
->skc_ref
);
1585 local_irq_save(flags
);
1587 /* Safe to update per-cpu structure without lock, but
1588 * no remote memory allocation tracking is being performed
1589 * it is entirely possible to allocate an object from one
1590 * CPU cache and return it to another. */
1591 skm
= skc
->skc_mag
[smp_processor_id()];
1592 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1594 /* Per-CPU cache full, flush it to make space */
1595 if (unlikely(skm
->skm_avail
>= skm
->skm_size
))
1596 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1598 /* Available space in cache, use it */
1599 skm
->skm_objs
[skm
->skm_avail
++] = obj
;
1601 local_irq_restore(flags
);
1602 atomic_dec(&skc
->skc_ref
);
1606 EXPORT_SYMBOL(spl_kmem_cache_free
);
1609 * The generic shrinker function for all caches. Under linux a shrinker
1610 * may not be tightly coupled with a slab cache. In fact linux always
1611 * systematically trys calling all registered shrinker callbacks which
1612 * report that they contain unused objects. Because of this we only
1613 * register one shrinker function in the shim layer for all slab caches.
1614 * We always attempt to shrink all caches when this generic shrinker
1615 * is called. The shrinker should return the number of free objects
1616 * in the cache when called with nr_to_scan == 0 but not attempt to
1617 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
1618 * objects should be freed, because Solaris semantics are to free
1619 * all available objects we may free more objects than requested.
1622 spl_kmem_cache_generic_shrinker(int nr_to_scan
, unsigned int gfp_mask
)
1624 spl_kmem_cache_t
*skc
;
1627 down_read(&spl_kmem_cache_sem
);
1628 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
1630 spl_kmem_cache_reap_now(skc
);
1633 * Presume everything alloc'ed in reclaimable, this ensures
1634 * we are called again with nr_to_scan > 0 so can try and
1635 * reclaim. The exact number is not important either so
1636 * we forgo taking this already highly contented lock.
1638 unused
+= skc
->skc_obj_alloc
;
1640 up_read(&spl_kmem_cache_sem
);
1642 return (unused
* sysctl_vfs_cache_pressure
) / 100;
1646 * Call the registered reclaim function for a cache. Depending on how
1647 * many and which objects are released it may simply repopulate the
1648 * local magazine which will then need to age-out. Objects which cannot
1649 * fit in the magazine we will be released back to their slabs which will
1650 * also need to age out before being release. This is all just best
1651 * effort and we do not want to thrash creating and destroying slabs.
1654 spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
)
1658 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1659 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1661 /* Prevent concurrent cache reaping when contended */
1662 if (test_and_set_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1667 atomic_inc(&skc
->skc_ref
);
1669 if (skc
->skc_reclaim
)
1670 skc
->skc_reclaim(skc
->skc_private
);
1672 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
1673 clear_bit(KMC_BIT_REAPING
, &skc
->skc_flags
);
1674 atomic_dec(&skc
->skc_ref
);
1678 EXPORT_SYMBOL(spl_kmem_cache_reap_now
);
1681 * Reap all free slabs from all registered caches.
1686 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK
, GFP_KERNEL
);
1688 EXPORT_SYMBOL(spl_kmem_reap
);
1690 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
1692 spl_sprintf_addr(kmem_debug_t
*kd
, char *str
, int len
, int min
)
1694 int size
= ((len
- 1) < kd
->kd_size
) ? (len
- 1) : kd
->kd_size
;
1697 ASSERT(str
!= NULL
&& len
>= 17);
1698 memset(str
, 0, len
);
1700 /* Check for a fully printable string, and while we are at
1701 * it place the printable characters in the passed buffer. */
1702 for (i
= 0; i
< size
; i
++) {
1703 str
[i
] = ((char *)(kd
->kd_addr
))[i
];
1704 if (isprint(str
[i
])) {
1707 /* Minimum number of printable characters found
1708 * to make it worthwhile to print this as ascii. */
1718 sprintf(str
, "%02x%02x%02x%02x%02x%02x%02x%02x",
1719 *((uint8_t *)kd
->kd_addr
),
1720 *((uint8_t *)kd
->kd_addr
+ 2),
1721 *((uint8_t *)kd
->kd_addr
+ 4),
1722 *((uint8_t *)kd
->kd_addr
+ 6),
1723 *((uint8_t *)kd
->kd_addr
+ 8),
1724 *((uint8_t *)kd
->kd_addr
+ 10),
1725 *((uint8_t *)kd
->kd_addr
+ 12),
1726 *((uint8_t *)kd
->kd_addr
+ 14));
1733 spl_kmem_init_tracking(struct list_head
*list
, spinlock_t
*lock
, int size
)
1738 spin_lock_init(lock
);
1739 INIT_LIST_HEAD(list
);
1741 for (i
= 0; i
< size
; i
++)
1742 INIT_HLIST_HEAD(&kmem_table
[i
]);
1748 spl_kmem_fini_tracking(struct list_head
*list
, spinlock_t
*lock
)
1750 unsigned long flags
;
1755 spin_lock_irqsave(lock
, flags
);
1756 if (!list_empty(list
))
1757 printk(KERN_WARNING
"%-16s %-5s %-16s %s:%s\n", "address",
1758 "size", "data", "func", "line");
1760 list_for_each_entry(kd
, list
, kd_list
)
1761 printk(KERN_WARNING
"%p %-5d %-16s %s:%d\n", kd
->kd_addr
,
1762 (int)kd
->kd_size
, spl_sprintf_addr(kd
, str
, 17, 8),
1763 kd
->kd_func
, kd
->kd_line
);
1765 spin_unlock_irqrestore(lock
, flags
);
1768 #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1769 #define spl_kmem_init_tracking(list, lock, size)
1770 #define spl_kmem_fini_tracking(list, lock)
1771 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1774 spl_kmem_init_globals(void)
1778 /* For now all zones are includes, it may be wise to restrict
1779 * this to normal and highmem zones if we see problems. */
1780 for_each_zone(zone
) {
1782 if (!populated_zone(zone
))
1785 minfree
+= zone
->pages_min
;
1786 desfree
+= zone
->pages_low
;
1787 lotsfree
+= zone
->pages_high
;
1790 /* Solaris default values */
1791 swapfs_minfree
= MAX(2*1024*1024 / PAGE_SIZE
, physmem
/ 8);
1792 swapfs_reserve
= MIN(4*1024*1024 / PAGE_SIZE
, physmem
/ 16);
1801 init_rwsem(&spl_kmem_cache_sem
);
1802 INIT_LIST_HEAD(&spl_kmem_cache_list
);
1803 spl_kmem_init_globals();
1805 #ifdef HAVE_SET_SHRINKER
1806 spl_kmem_cache_shrinker
= set_shrinker(KMC_DEFAULT_SEEKS
,
1807 spl_kmem_cache_generic_shrinker
);
1808 if (spl_kmem_cache_shrinker
== NULL
)
1809 RETURN(rc
= -ENOMEM
);
1811 register_shrinker(&spl_kmem_cache_shrinker
);
1815 atomic64_set(&kmem_alloc_used
, 0);
1816 atomic64_set(&vmem_alloc_used
, 0);
1818 spl_kmem_init_tracking(&kmem_list
, &kmem_lock
, KMEM_TABLE_SIZE
);
1819 spl_kmem_init_tracking(&vmem_list
, &vmem_lock
, VMEM_TABLE_SIZE
);
1828 /* Display all unreclaimed memory addresses, including the
1829 * allocation size and the first few bytes of what's located
1830 * at that address to aid in debugging. Performance is not
1831 * a serious concern here since it is module unload time. */
1832 if (atomic64_read(&kmem_alloc_used
) != 0)
1833 CWARN("kmem leaked %ld/%ld bytes\n",
1834 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
1837 if (atomic64_read(&vmem_alloc_used
) != 0)
1838 CWARN("vmem leaked %ld/%ld bytes\n",
1839 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
1841 spl_kmem_fini_tracking(&kmem_list
, &kmem_lock
);
1842 spl_kmem_fini_tracking(&vmem_list
, &vmem_lock
);
1843 #endif /* DEBUG_KMEM */
1846 #ifdef HAVE_SET_SHRINKER
1847 remove_shrinker(spl_kmem_cache_shrinker
);
1849 unregister_shrinker(&spl_kmem_cache_shrinker
);