2 * This file is part of the SPL: Solaris Porting Layer.
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #ifdef DEBUG_SUBSYSTEM
30 # undef DEBUG_SUBSYSTEM
33 #define DEBUG_SUBSYSTEM S_KMEM
36 * The minimum amount of memory measured in pages to be free at all
37 * times on the system. This is similar to Linux's zone->pages_min
38 * multipled by the number of zones and is sized based on that.
41 EXPORT_SYMBOL(minfree
);
44 * The desired amount of memory measured in pages to be free at all
45 * times on the system. This is similar to Linux's zone->pages_low
46 * multipled by the number of zones and is sized based on that.
47 * Assuming all zones are being used roughly equally, when we drop
48 * below this threshold async page reclamation is triggered.
51 EXPORT_SYMBOL(desfree
);
54 * When above this amount of memory measures in pages the system is
55 * determined to have enough free memory. This is similar to Linux's
56 * zone->pages_high multipled by the number of zones and is sized based
57 * on that. Assuming all zones are being used roughly equally, when
58 * async page reclamation reaches this threshold it stops.
61 EXPORT_SYMBOL(lotsfree
);
63 /* Unused always 0 in this implementation */
65 EXPORT_SYMBOL(needfree
);
67 pgcnt_t swapfs_minfree
= 0;
68 EXPORT_SYMBOL(swapfs_minfree
);
70 pgcnt_t swapfs_reserve
= 0;
71 EXPORT_SYMBOL(swapfs_reserve
);
73 vmem_t
*heap_arena
= NULL
;
74 EXPORT_SYMBOL(heap_arena
);
76 vmem_t
*zio_alloc_arena
= NULL
;
77 EXPORT_SYMBOL(zio_alloc_arena
);
79 vmem_t
*zio_arena
= NULL
;
80 EXPORT_SYMBOL(zio_arena
);
82 #ifndef HAVE_GET_VMALLOC_INFO
83 get_vmalloc_info_t get_vmalloc_info_fn
= SYMBOL_POISON
;
84 EXPORT_SYMBOL(get_vmalloc_info_fn
);
85 #endif /* HAVE_GET_VMALLOC_INFO */
87 #ifdef HAVE_PGDAT_HELPERS
88 # ifndef HAVE_FIRST_ONLINE_PGDAT
89 first_online_pgdat_t first_online_pgdat_fn
= SYMBOL_POISON
;
90 EXPORT_SYMBOL(first_online_pgdat_fn
);
91 # endif /* HAVE_FIRST_ONLINE_PGDAT */
93 # ifndef HAVE_NEXT_ONLINE_PGDAT
94 next_online_pgdat_t next_online_pgdat_fn
= SYMBOL_POISON
;
95 EXPORT_SYMBOL(next_online_pgdat_fn
);
96 # endif /* HAVE_NEXT_ONLINE_PGDAT */
98 # ifndef HAVE_NEXT_ZONE
99 next_zone_t next_zone_fn
= SYMBOL_POISON
;
100 EXPORT_SYMBOL(next_zone_fn
);
101 # endif /* HAVE_NEXT_ZONE */
103 #else /* HAVE_PGDAT_HELPERS */
105 # ifndef HAVE_PGDAT_LIST
106 struct pglist_data
*pgdat_list_addr
= SYMBOL_POISON
;
107 EXPORT_SYMBOL(pgdat_list_addr
);
108 # endif /* HAVE_PGDAT_LIST */
110 #endif /* HAVE_PGDAT_HELPERS */
112 #ifndef HAVE_ZONE_STAT_ITEM_FIA
113 # ifndef HAVE_GET_ZONE_COUNTS
114 get_zone_counts_t get_zone_counts_fn
= SYMBOL_POISON
;
115 EXPORT_SYMBOL(get_zone_counts_fn
);
116 # endif /* HAVE_GET_ZONE_COUNTS */
119 spl_global_page_state(int item
)
121 unsigned long active
;
122 unsigned long inactive
;
125 if (item
== NR_FREE_PAGES
) {
126 get_zone_counts(&active
, &inactive
, &free
);
130 if (item
== NR_INACTIVE
) {
131 get_zone_counts(&active
, &inactive
, &free
);
135 if (item
== NR_ACTIVE
) {
136 get_zone_counts(&active
, &inactive
, &free
);
140 # ifdef HAVE_GLOBAL_PAGE_STATE
141 return global_page_state((enum zone_stat_item
)item
);
143 return 0; /* Unsupported */
144 # endif /* HAVE_GLOBAL_PAGE_STATE */
146 EXPORT_SYMBOL(spl_global_page_state
);
147 #endif /* HAVE_ZONE_STAT_ITEM_FIA */
150 spl_kmem_availrmem(void)
152 /* The amount of easily available memory */
153 return (spl_global_page_state(NR_FREE_PAGES
) +
154 spl_global_page_state(NR_INACTIVE
));
156 EXPORT_SYMBOL(spl_kmem_availrmem
);
159 vmem_size(vmem_t
*vmp
, int typemask
)
161 struct vmalloc_info vmi
;
165 ASSERT(typemask
& (VMEM_ALLOC
| VMEM_FREE
));
167 get_vmalloc_info(&vmi
);
168 if (typemask
& VMEM_ALLOC
)
169 size
+= (size_t)vmi
.used
;
171 if (typemask
& VMEM_FREE
)
172 size
+= (size_t)(VMALLOC_TOTAL
- vmi
.used
);
176 EXPORT_SYMBOL(vmem_size
);
179 * Memory allocation interfaces and debugging for basic kmem_*
180 * and vmem_* style memory allocation. When DEBUG_KMEM is enable
181 * all allocations will be tracked when they are allocated and
182 * freed. When the SPL module is unload a list of all leaked
183 * addresses and where they were allocated will be dumped to the
184 * console. Enabling this feature has a significant impant on
185 * performance but it makes finding memory leaks staight forward.
188 /* Shim layer memory accounting */
189 atomic64_t kmem_alloc_used
= ATOMIC64_INIT(0);
190 unsigned long long kmem_alloc_max
= 0;
191 atomic64_t vmem_alloc_used
= ATOMIC64_INIT(0);
192 unsigned long long vmem_alloc_max
= 0;
193 int kmem_warning_flag
= 1;
195 EXPORT_SYMBOL(kmem_alloc_used
);
196 EXPORT_SYMBOL(kmem_alloc_max
);
197 EXPORT_SYMBOL(vmem_alloc_used
);
198 EXPORT_SYMBOL(vmem_alloc_max
);
199 EXPORT_SYMBOL(kmem_warning_flag
);
201 # ifdef DEBUG_KMEM_TRACKING
203 /* XXX - Not to surprisingly with debugging enabled the xmem_locks are very
204 * highly contended particularly on xfree(). If we want to run with this
205 * detailed debugging enabled for anything other than debugging we need to
206 * minimize the contention by moving to a lock per xmem_table entry model.
209 # define KMEM_HASH_BITS 10
210 # define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
212 # define VMEM_HASH_BITS 10
213 # define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS)
215 typedef struct kmem_debug
{
216 struct hlist_node kd_hlist
; /* Hash node linkage */
217 struct list_head kd_list
; /* List of all allocations */
218 void *kd_addr
; /* Allocation pointer */
219 size_t kd_size
; /* Allocation size */
220 const char *kd_func
; /* Allocation function */
221 int kd_line
; /* Allocation line */
224 spinlock_t kmem_lock
;
225 struct hlist_head kmem_table
[KMEM_TABLE_SIZE
];
226 struct list_head kmem_list
;
228 spinlock_t vmem_lock
;
229 struct hlist_head vmem_table
[VMEM_TABLE_SIZE
];
230 struct list_head vmem_list
;
232 EXPORT_SYMBOL(kmem_lock
);
233 EXPORT_SYMBOL(kmem_table
);
234 EXPORT_SYMBOL(kmem_list
);
236 EXPORT_SYMBOL(vmem_lock
);
237 EXPORT_SYMBOL(vmem_table
);
238 EXPORT_SYMBOL(vmem_list
);
241 int kmem_set_warning(int flag
) { return (kmem_warning_flag
= !!flag
); }
243 int kmem_set_warning(int flag
) { return 0; }
245 EXPORT_SYMBOL(kmem_set_warning
);
248 * Slab allocation interfaces
250 * While the Linux slab implementation was inspired by the Solaris
251 * implemenation I cannot use it to emulate the Solaris APIs. I
252 * require two features which are not provided by the Linux slab.
254 * 1) Constructors AND destructors. Recent versions of the Linux
255 * kernel have removed support for destructors. This is a deal
256 * breaker for the SPL which contains particularly expensive
257 * initializers for mutex's, condition variables, etc. We also
258 * require a minimal level of cleanup for these data types unlike
259 * many Linux data type which do need to be explicitly destroyed.
261 * 2) Virtual address space backed slab. Callers of the Solaris slab
262 * expect it to work well for both small are very large allocations.
263 * Because of memory fragmentation the Linux slab which is backed
264 * by kmalloc'ed memory performs very badly when confronted with
265 * large numbers of large allocations. Basing the slab on the
266 * virtual address space removes the need for contigeous pages
267 * and greatly improve performance for large allocations.
269 * For these reasons, the SPL has its own slab implementation with
270 * the needed features. It is not as highly optimized as either the
271 * Solaris or Linux slabs, but it should get me most of what is
272 * needed until it can be optimized or obsoleted by another approach.
274 * One serious concern I do have about this method is the relatively
275 * small virtual address space on 32bit arches. This will seriously
276 * constrain the size of the slab caches and their performance.
278 * XXX: Improve the partial slab list by carefully maintaining a
279 * strict ordering of fullest to emptiest slabs based on
280 * the slab reference count. This gaurentees the when freeing
281 * slabs back to the system we need only linearly traverse the
282 * last N slabs in the list to discover all the freeable slabs.
284 * XXX: NUMA awareness for optionally allocating memory close to a
285 * particular core. This can be adventageous if you know the slab
286 * object will be short lived and primarily accessed from one core.
288 * XXX: Slab coloring may also yield performance improvements and would
289 * be desirable to implement.
292 struct list_head spl_kmem_cache_list
; /* List of caches */
293 struct rw_semaphore spl_kmem_cache_sem
; /* Cache list lock */
295 static int spl_cache_flush(spl_kmem_cache_t
*skc
,
296 spl_kmem_magazine_t
*skm
, int flush
);
298 #ifdef HAVE_SET_SHRINKER
299 static struct shrinker
*spl_kmem_cache_shrinker
;
301 static int spl_kmem_cache_generic_shrinker(int nr_to_scan
,
302 unsigned int gfp_mask
);
303 static struct shrinker spl_kmem_cache_shrinker
= {
304 .shrink
= spl_kmem_cache_generic_shrinker
,
305 .seeks
= KMC_DEFAULT_SEEKS
,
310 # ifdef DEBUG_KMEM_TRACKING
312 static kmem_debug_t
*
313 kmem_del_init(spinlock_t
*lock
, struct hlist_head
*table
, int bits
,
316 struct hlist_head
*head
;
317 struct hlist_node
*node
;
318 struct kmem_debug
*p
;
322 spin_lock_irqsave(lock
, flags
);
324 head
= &table
[hash_ptr(addr
, bits
)];
325 hlist_for_each_entry_rcu(p
, node
, head
, kd_hlist
) {
326 if (p
->kd_addr
== addr
) {
327 hlist_del_init(&p
->kd_hlist
);
328 list_del_init(&p
->kd_list
);
329 spin_unlock_irqrestore(lock
, flags
);
334 spin_unlock_irqrestore(lock
, flags
);
340 kmem_alloc_track(size_t size
, int flags
, const char *func
, int line
,
341 int node_alloc
, int node
)
345 unsigned long irq_flags
;
348 dptr
= (kmem_debug_t
*) kmalloc(sizeof(kmem_debug_t
),
349 flags
& ~__GFP_ZERO
);
352 CWARN("kmem_alloc(%ld, 0x%x) debug failed\n",
353 sizeof(kmem_debug_t
), flags
);
355 /* Marked unlikely because we should never be doing this,
356 * we tolerate to up 2 pages but a single page is best. */
357 if (unlikely((size
) > (PAGE_SIZE
* 2)) && kmem_warning_flag
)
358 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
359 (unsigned long long) size
, flags
,
360 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
362 /* We use kstrdup() below because the string pointed to by
363 * __FUNCTION__ might not be available by the time we want
364 * to print it since the module might have been unloaded. */
365 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
366 if (unlikely(dptr
->kd_func
== NULL
)) {
368 CWARN("kstrdup() failed in kmem_alloc(%llu, 0x%x) "
369 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
370 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
374 /* Use the correct allocator */
376 ASSERT(!(flags
& __GFP_ZERO
));
377 ptr
= kmalloc_node(size
, flags
, node
);
378 } else if (flags
& __GFP_ZERO
) {
379 ptr
= kzalloc(size
, flags
& ~__GFP_ZERO
);
381 ptr
= kmalloc(size
, flags
);
384 if (unlikely(ptr
== NULL
)) {
385 kfree(dptr
->kd_func
);
387 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
388 (unsigned long long) size
, flags
,
389 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
393 atomic64_add(size
, &kmem_alloc_used
);
394 if (unlikely(atomic64_read(&kmem_alloc_used
) >
397 atomic64_read(&kmem_alloc_used
);
399 INIT_HLIST_NODE(&dptr
->kd_hlist
);
400 INIT_LIST_HEAD(&dptr
->kd_list
);
403 dptr
->kd_size
= size
;
404 dptr
->kd_line
= line
;
406 spin_lock_irqsave(&kmem_lock
, irq_flags
);
407 hlist_add_head_rcu(&dptr
->kd_hlist
,
408 &kmem_table
[hash_ptr(ptr
, KMEM_HASH_BITS
)]);
409 list_add_tail(&dptr
->kd_list
, &kmem_list
);
410 spin_unlock_irqrestore(&kmem_lock
, irq_flags
);
412 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
413 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
414 ptr
, atomic64_read(&kmem_alloc_used
),
420 EXPORT_SYMBOL(kmem_alloc_track
);
423 kmem_free_track(void *ptr
, size_t size
)
428 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
429 (unsigned long long) size
);
431 dptr
= kmem_del_init(&kmem_lock
, kmem_table
, KMEM_HASH_BITS
, ptr
);
433 ASSERT(dptr
); /* Must exist in hash due to kmem_alloc() */
435 /* Size must match */
436 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
437 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
438 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
440 atomic64_sub(size
, &kmem_alloc_used
);
442 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
443 (unsigned long long) size
, atomic64_read(&kmem_alloc_used
),
446 kfree(dptr
->kd_func
);
448 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
451 memset(ptr
, 0x5a, size
);
456 EXPORT_SYMBOL(kmem_free_track
);
459 vmem_alloc_track(size_t size
, int flags
, const char *func
, int line
)
463 unsigned long irq_flags
;
466 ASSERT(flags
& KM_SLEEP
);
468 dptr
= (kmem_debug_t
*) kmalloc(sizeof(kmem_debug_t
), flags
);
470 CWARN("vmem_alloc(%ld, 0x%x) debug failed\n",
471 sizeof(kmem_debug_t
), flags
);
473 /* We use kstrdup() below because the string pointed to by
474 * __FUNCTION__ might not be available by the time we want
475 * to print it, since the module might have been unloaded. */
476 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
477 if (unlikely(dptr
->kd_func
== NULL
)) {
479 CWARN("kstrdup() failed in vmem_alloc(%llu, 0x%x) "
480 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
481 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
485 ptr
= __vmalloc(size
, (flags
| __GFP_HIGHMEM
) & ~__GFP_ZERO
,
488 if (unlikely(ptr
== NULL
)) {
489 kfree(dptr
->kd_func
);
491 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
492 (unsigned long long) size
, flags
,
493 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
497 if (flags
& __GFP_ZERO
)
498 memset(ptr
, 0, size
);
500 atomic64_add(size
, &vmem_alloc_used
);
501 if (unlikely(atomic64_read(&vmem_alloc_used
) >
504 atomic64_read(&vmem_alloc_used
);
506 INIT_HLIST_NODE(&dptr
->kd_hlist
);
507 INIT_LIST_HEAD(&dptr
->kd_list
);
510 dptr
->kd_size
= size
;
511 dptr
->kd_line
= line
;
513 spin_lock_irqsave(&vmem_lock
, irq_flags
);
514 hlist_add_head_rcu(&dptr
->kd_hlist
,
515 &vmem_table
[hash_ptr(ptr
, VMEM_HASH_BITS
)]);
516 list_add_tail(&dptr
->kd_list
, &vmem_list
);
517 spin_unlock_irqrestore(&vmem_lock
, irq_flags
);
519 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
520 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
521 ptr
, atomic64_read(&vmem_alloc_used
),
527 EXPORT_SYMBOL(vmem_alloc_track
);
530 vmem_free_track(void *ptr
, size_t size
)
535 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
536 (unsigned long long) size
);
538 dptr
= kmem_del_init(&vmem_lock
, vmem_table
, VMEM_HASH_BITS
, ptr
);
539 ASSERT(dptr
); /* Must exist in hash due to vmem_alloc() */
541 /* Size must match */
542 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
543 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
544 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
546 atomic64_sub(size
, &vmem_alloc_used
);
547 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
548 (unsigned long long) size
, atomic64_read(&vmem_alloc_used
),
551 kfree(dptr
->kd_func
);
553 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
556 memset(ptr
, 0x5a, size
);
561 EXPORT_SYMBOL(vmem_free_track
);
563 # else /* DEBUG_KMEM_TRACKING */
566 kmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
,
567 int node_alloc
, int node
)
572 /* Marked unlikely because we should never be doing this,
573 * we tolerate to up 2 pages but a single page is best. */
574 if (unlikely(size
> (PAGE_SIZE
* 2)) && kmem_warning_flag
)
575 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
576 (unsigned long long) size
, flags
,
577 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
579 /* Use the correct allocator */
581 ASSERT(!(flags
& __GFP_ZERO
));
582 ptr
= kmalloc_node(size
, flags
, node
);
583 } else if (flags
& __GFP_ZERO
) {
584 ptr
= kzalloc(size
, flags
& (~__GFP_ZERO
));
586 ptr
= kmalloc(size
, flags
);
590 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
591 (unsigned long long) size
, flags
,
592 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
594 atomic64_add(size
, &kmem_alloc_used
);
595 if (unlikely(atomic64_read(&kmem_alloc_used
) > kmem_alloc_max
))
596 kmem_alloc_max
= atomic64_read(&kmem_alloc_used
);
598 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
599 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
600 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
604 EXPORT_SYMBOL(kmem_alloc_debug
);
607 kmem_free_debug(void *ptr
, size_t size
)
611 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
612 (unsigned long long) size
);
614 atomic64_sub(size
, &kmem_alloc_used
);
616 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
617 (unsigned long long) size
, atomic64_read(&kmem_alloc_used
),
620 memset(ptr
, 0x5a, size
);
625 EXPORT_SYMBOL(kmem_free_debug
);
628 vmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
)
633 ASSERT(flags
& KM_SLEEP
);
635 ptr
= __vmalloc(size
, (flags
| __GFP_HIGHMEM
) & ~__GFP_ZERO
,
638 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
639 (unsigned long long) size
, flags
,
640 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
642 if (flags
& __GFP_ZERO
)
643 memset(ptr
, 0, size
);
645 atomic64_add(size
, &vmem_alloc_used
);
647 if (unlikely(atomic64_read(&vmem_alloc_used
) > vmem_alloc_max
))
648 vmem_alloc_max
= atomic64_read(&vmem_alloc_used
);
650 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
651 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
652 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
657 EXPORT_SYMBOL(vmem_alloc_debug
);
660 vmem_free_debug(void *ptr
, size_t size
)
664 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
665 (unsigned long long) size
);
667 atomic64_sub(size
, &vmem_alloc_used
);
669 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
670 (unsigned long long) size
, atomic64_read(&vmem_alloc_used
),
673 memset(ptr
, 0x5a, size
);
678 EXPORT_SYMBOL(vmem_free_debug
);
680 # endif /* DEBUG_KMEM_TRACKING */
681 #endif /* DEBUG_KMEM */
684 kv_alloc(spl_kmem_cache_t
*skc
, int size
, int flags
)
688 if (skc
->skc_flags
& KMC_KMEM
) {
689 if (size
> (2 * PAGE_SIZE
)) {
690 ptr
= (void *)__get_free_pages(flags
, get_order(size
));
692 ptr
= kmem_alloc(size
, flags
);
694 ptr
= vmem_alloc(size
, flags
);
701 kv_free(spl_kmem_cache_t
*skc
, void *ptr
, int size
)
703 if (skc
->skc_flags
& KMC_KMEM
) {
704 if (size
> (2 * PAGE_SIZE
))
705 free_pages((unsigned long)ptr
, get_order(size
));
707 kmem_free(ptr
, size
);
709 vmem_free(ptr
, size
);
714 * It's important that we pack the spl_kmem_obj_t structure and the
715 * actual objects in to one large address space to minimize the number
716 * of calls to the allocator. It is far better to do a few large
717 * allocations and then subdivide it ourselves. Now which allocator
718 * we use requires balancing a few trade offs.
720 * For small objects we use kmem_alloc() because as long as you are
721 * only requesting a small number of pages (ideally just one) its cheap.
722 * However, when you start requesting multiple pages with kmem_alloc()
723 * it gets increasingly expensive since it requires contigeous pages.
724 * For this reason we shift to vmem_alloc() for slabs of large objects
725 * which removes the need for contigeous pages. We do not use
726 * vmem_alloc() in all cases because there is significant locking
727 * overhead in __get_vm_area_node(). This function takes a single
728 * global lock when aquiring an available virtual address range which
729 * serializes all vmem_alloc()'s for all slab caches. Using slightly
730 * different allocation functions for small and large objects should
731 * give us the best of both worlds.
733 * KMC_ONSLAB KMC_OFFSLAB
735 * +------------------------+ +-----------------+
736 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
737 * | skc_obj_size <-+ | | +-----------------+ | |
738 * | spl_kmem_obj_t | | | |
739 * | skc_obj_size <---+ | +-----------------+ | |
740 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
741 * | ... v | | spl_kmem_obj_t | |
742 * +------------------------+ +-----------------+ v
744 static spl_kmem_slab_t
*
745 spl_slab_alloc(spl_kmem_cache_t
*skc
, int flags
)
747 spl_kmem_slab_t
*sks
;
748 spl_kmem_obj_t
*sko
, *n
;
750 int i
, align
, size
, rc
= 0;
752 base
= kv_alloc(skc
, skc
->skc_slab_size
, flags
);
756 sks
= (spl_kmem_slab_t
*)base
;
757 sks
->sks_magic
= SKS_MAGIC
;
758 sks
->sks_objs
= skc
->skc_slab_objs
;
759 sks
->sks_age
= jiffies
;
760 sks
->sks_cache
= skc
;
761 INIT_LIST_HEAD(&sks
->sks_list
);
762 INIT_LIST_HEAD(&sks
->sks_free_list
);
765 align
= skc
->skc_obj_align
;
766 size
= P2ROUNDUP(skc
->skc_obj_size
, align
) +
767 P2ROUNDUP(sizeof(spl_kmem_obj_t
), align
);
769 for (i
= 0; i
< sks
->sks_objs
; i
++) {
770 if (skc
->skc_flags
& KMC_OFFSLAB
) {
771 obj
= kv_alloc(skc
, size
, flags
);
773 GOTO(out
, rc
= -ENOMEM
);
776 P2ROUNDUP(sizeof(spl_kmem_slab_t
), align
) +
780 sko
= obj
+ P2ROUNDUP(skc
->skc_obj_size
, align
);
782 sko
->sko_magic
= SKO_MAGIC
;
784 INIT_LIST_HEAD(&sko
->sko_list
);
785 list_add_tail(&sko
->sko_list
, &sks
->sks_free_list
);
788 list_for_each_entry(sko
, &sks
->sks_free_list
, sko_list
)
790 skc
->skc_ctor(sko
->sko_addr
, skc
->skc_private
, flags
);
793 if (skc
->skc_flags
& KMC_OFFSLAB
)
794 list_for_each_entry_safe(sko
, n
, &sks
->sks_free_list
,
796 kv_free(skc
, sko
->sko_addr
, size
);
798 kv_free(skc
, base
, skc
->skc_slab_size
);
806 * Remove a slab from complete or partial list, it must be called with
807 * the 'skc->skc_lock' held but the actual free must be performed
808 * outside the lock to prevent deadlocking on vmem addresses.
811 spl_slab_free(spl_kmem_slab_t
*sks
,
812 struct list_head
*sks_list
, struct list_head
*sko_list
)
814 spl_kmem_cache_t
*skc
;
817 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
818 ASSERT(sks
->sks_ref
== 0);
820 skc
= sks
->sks_cache
;
821 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
822 ASSERT(spin_is_locked(&skc
->skc_lock
));
825 * Update slab/objects counters in the cache, then remove the
826 * slab from the skc->skc_partial_list. Finally add the slab
827 * and all its objects in to the private work lists where the
828 * destructors will be called and the memory freed to the system.
830 skc
->skc_obj_total
-= sks
->sks_objs
;
831 skc
->skc_slab_total
--;
832 list_del(&sks
->sks_list
);
833 list_add(&sks
->sks_list
, sks_list
);
834 list_splice_init(&sks
->sks_free_list
, sko_list
);
840 * Traverses all the partial slabs attached to a cache and free those
841 * which which are currently empty, and have not been touched for
842 * skc_delay seconds to avoid thrashing. The count argument is
843 * passed to optionally cap the number of slabs reclaimed, a count
844 * of zero means try and reclaim everything. When flag is set we
845 * always free an available slab regardless of age.
848 spl_slab_reclaim(spl_kmem_cache_t
*skc
, int count
, int flag
)
850 spl_kmem_slab_t
*sks
, *m
;
851 spl_kmem_obj_t
*sko
, *n
;
858 * Move empty slabs and objects which have not been touched in
859 * skc_delay seconds on to private lists to be freed outside
860 * the spin lock. This delay time is important to avoid thrashing
861 * however when flag is set the delay will not be used.
863 spin_lock(&skc
->skc_lock
);
864 list_for_each_entry_safe_reverse(sks
,m
,&skc
->skc_partial_list
,sks_list
){
866 * All empty slabs are at the end of skc->skc_partial_list,
867 * therefore once a non-empty slab is found we can stop
868 * scanning. Additionally, stop when reaching the target
869 * reclaim 'count' if a non-zero threshhold is given.
871 if ((sks
->sks_ref
> 0) || (count
&& i
> count
))
874 if (time_after(jiffies
,sks
->sks_age
+skc
->skc_delay
*HZ
)||flag
) {
875 spl_slab_free(sks
, &sks_list
, &sko_list
);
879 spin_unlock(&skc
->skc_lock
);
882 * The following two loops ensure all the object destructors are
883 * run, any offslab objects are freed, and the slabs themselves
884 * are freed. This is all done outside the skc->skc_lock since
885 * this allows the destructor to sleep, and allows us to perform
886 * a conditional reschedule when a freeing a large number of
887 * objects and slabs back to the system.
889 if (skc
->skc_flags
& KMC_OFFSLAB
)
890 size
= P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
) +
891 P2ROUNDUP(sizeof(spl_kmem_obj_t
), skc
->skc_obj_align
);
893 list_for_each_entry_safe(sko
, n
, &sko_list
, sko_list
) {
894 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
897 skc
->skc_dtor(sko
->sko_addr
, skc
->skc_private
);
899 if (skc
->skc_flags
& KMC_OFFSLAB
)
900 kv_free(skc
, sko
->sko_addr
, size
);
905 list_for_each_entry_safe(sks
, m
, &sks_list
, sks_list
) {
906 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
907 kv_free(skc
, sks
, skc
->skc_slab_size
);
915 * Called regularly on all caches to age objects out of the magazines
916 * which have not been access in skc->skc_delay seconds. This prevents
917 * idle magazines from holding memory which might be better used by
918 * other caches or parts of the system. The delay is present to
919 * prevent thrashing the magazine.
922 spl_magazine_age(void *data
)
924 spl_kmem_magazine_t
*skm
=
925 spl_get_work_data(data
, spl_kmem_magazine_t
, skm_work
.work
);
926 spl_kmem_cache_t
*skc
= skm
->skm_cache
;
927 int i
= smp_processor_id();
929 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
930 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
931 ASSERT(skc
->skc_mag
[i
] == skm
);
933 if (skm
->skm_avail
> 0 &&
934 time_after(jiffies
, skm
->skm_age
+ skc
->skc_delay
* HZ
))
935 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
937 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
938 schedule_delayed_work_on(i
, &skm
->skm_work
,
939 skc
->skc_delay
/ 3 * HZ
);
943 * Called regularly to keep a downward pressure on the size of idle
944 * magazines and to release free slabs from the cache. This function
945 * never calls the registered reclaim function, that only occures
946 * under memory pressure or with a direct call to spl_kmem_reap().
949 spl_cache_age(void *data
)
951 spl_kmem_cache_t
*skc
=
952 spl_get_work_data(data
, spl_kmem_cache_t
, skc_work
.work
);
954 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
955 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
957 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
958 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
962 * Size a slab based on the size of each aliged object plus spl_kmem_obj_t.
963 * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However,
964 * for very small objects we may end up with more than this so as not
965 * to waste space in the minimal allocation of a single page. Also for
966 * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN,
967 * lower than this and we will fail.
970 spl_slab_size(spl_kmem_cache_t
*skc
, uint32_t *objs
, uint32_t *size
)
972 int sks_size
, obj_size
, max_size
, align
;
974 if (skc
->skc_flags
& KMC_OFFSLAB
) {
975 *objs
= SPL_KMEM_CACHE_OBJ_PER_SLAB
;
976 *size
= sizeof(spl_kmem_slab_t
);
978 align
= skc
->skc_obj_align
;
979 sks_size
= P2ROUNDUP(sizeof(spl_kmem_slab_t
), align
);
980 obj_size
= P2ROUNDUP(skc
->skc_obj_size
, align
) +
981 P2ROUNDUP(sizeof(spl_kmem_obj_t
), align
);
983 if (skc
->skc_flags
& KMC_KMEM
)
984 max_size
= ((uint64_t)1 << (MAX_ORDER
-1)) * PAGE_SIZE
;
986 max_size
= (32 * 1024 * 1024);
988 for (*size
= PAGE_SIZE
; *size
<= max_size
; *size
+= PAGE_SIZE
) {
989 *objs
= (*size
- sks_size
) / obj_size
;
990 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB
)
995 * Unable to satisfy target objets per slab, fallback to
996 * allocating a maximally sized slab and assuming it can
997 * contain the minimum objects count use it. If not fail.
1000 *objs
= (*size
- sks_size
) / obj_size
;
1001 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN
)
1009 * Make a guess at reasonable per-cpu magazine size based on the size of
1010 * each object and the cost of caching N of them in each magazine. Long
1011 * term this should really adapt based on an observed usage heuristic.
1014 spl_magazine_size(spl_kmem_cache_t
*skc
)
1016 int size
, align
= skc
->skc_obj_align
;
1019 /* Per-magazine sizes below assume a 4Kib page size */
1020 if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
* 256))
1021 size
= 4; /* Minimum 4Mib per-magazine */
1022 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
* 32))
1023 size
= 16; /* Minimum 2Mib per-magazine */
1024 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
))
1025 size
= 64; /* Minimum 256Kib per-magazine */
1026 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
/ 4))
1027 size
= 128; /* Minimum 128Kib per-magazine */
1035 * Allocate a per-cpu magazine to assoicate with a specific core.
1037 static spl_kmem_magazine_t
*
1038 spl_magazine_alloc(spl_kmem_cache_t
*skc
, int node
)
1040 spl_kmem_magazine_t
*skm
;
1041 int size
= sizeof(spl_kmem_magazine_t
) +
1042 sizeof(void *) * skc
->skc_mag_size
;
1045 skm
= kmem_alloc_node(size
, GFP_KERNEL
| __GFP_NOFAIL
, node
);
1047 skm
->skm_magic
= SKM_MAGIC
;
1049 skm
->skm_size
= skc
->skc_mag_size
;
1050 skm
->skm_refill
= skc
->skc_mag_refill
;
1051 skm
->skm_cache
= skc
;
1052 spl_init_delayed_work(&skm
->skm_work
, spl_magazine_age
, skm
);
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
]);
1098 /* Only after everything is allocated schedule magazine work */
1099 for_each_online_cpu(i
)
1100 schedule_delayed_work_on(i
, &skc
->skc_mag
[i
]->skm_work
,
1101 skc
->skc_delay
/ 3 * HZ
);
1107 * Destroy all pre-cpu magazines.
1110 spl_magazine_destroy(spl_kmem_cache_t
*skc
)
1112 spl_kmem_magazine_t
*skm
;
1116 for_each_online_cpu(i
) {
1117 skm
= skc
->skc_mag
[i
];
1118 (void)spl_cache_flush(skc
, skm
, skm
->skm_avail
);
1119 spl_magazine_free(skm
);
1126 * Create a object cache based on the following arguments:
1128 * size cache object size
1129 * align cache object alignment
1130 * ctor cache object constructor
1131 * dtor cache object destructor
1132 * reclaim cache object reclaim
1133 * priv cache private data for ctor/dtor/reclaim
1134 * vmp unused must be NULL
1136 * KMC_NOTOUCH Disable cache object aging (unsupported)
1137 * KMC_NODEBUG Disable debugging (unsupported)
1138 * KMC_NOMAGAZINE Disable magazine (unsupported)
1139 * KMC_NOHASH Disable hashing (unsupported)
1140 * KMC_QCACHE Disable qcache (unsupported)
1141 * KMC_KMEM Force kmem backed cache
1142 * KMC_VMEM Force vmem backed cache
1143 * KMC_OFFSLAB Locate objects off the slab
1146 spl_kmem_cache_create(char *name
, size_t size
, size_t align
,
1147 spl_kmem_ctor_t ctor
,
1148 spl_kmem_dtor_t dtor
,
1149 spl_kmem_reclaim_t reclaim
,
1150 void *priv
, void *vmp
, int flags
)
1152 spl_kmem_cache_t
*skc
;
1153 int rc
, kmem_flags
= KM_SLEEP
;
1156 ASSERTF(!(flags
& KMC_NOMAGAZINE
), "Bad KMC_NOMAGAZINE (%x)\n", flags
);
1157 ASSERTF(!(flags
& KMC_NOHASH
), "Bad KMC_NOHASH (%x)\n", flags
);
1158 ASSERTF(!(flags
& KMC_QCACHE
), "Bad KMC_QCACHE (%x)\n", flags
);
1159 ASSERT(vmp
== NULL
);
1161 /* We may be called when there is a non-zero preempt_count or
1162 * interrupts are disabled is which case we must not sleep.
1164 if (current_thread_info()->preempt_count
|| irqs_disabled())
1165 kmem_flags
= KM_NOSLEEP
;
1167 /* Allocate new cache memory and initialize. */
1168 skc
= (spl_kmem_cache_t
*)kmem_zalloc(sizeof(*skc
), kmem_flags
);
1172 skc
->skc_magic
= SKC_MAGIC
;
1173 skc
->skc_name_size
= strlen(name
) + 1;
1174 skc
->skc_name
= (char *)kmem_alloc(skc
->skc_name_size
, kmem_flags
);
1175 if (skc
->skc_name
== NULL
) {
1176 kmem_free(skc
, sizeof(*skc
));
1179 strncpy(skc
->skc_name
, name
, skc
->skc_name_size
);
1181 skc
->skc_ctor
= ctor
;
1182 skc
->skc_dtor
= dtor
;
1183 skc
->skc_reclaim
= reclaim
;
1184 skc
->skc_private
= priv
;
1186 skc
->skc_flags
= flags
;
1187 skc
->skc_obj_size
= size
;
1188 skc
->skc_obj_align
= SPL_KMEM_CACHE_ALIGN
;
1189 skc
->skc_delay
= SPL_KMEM_CACHE_DELAY
;
1190 skc
->skc_reap
= SPL_KMEM_CACHE_REAP
;
1191 atomic_set(&skc
->skc_ref
, 0);
1193 INIT_LIST_HEAD(&skc
->skc_list
);
1194 INIT_LIST_HEAD(&skc
->skc_complete_list
);
1195 INIT_LIST_HEAD(&skc
->skc_partial_list
);
1196 spin_lock_init(&skc
->skc_lock
);
1197 skc
->skc_slab_fail
= 0;
1198 skc
->skc_slab_create
= 0;
1199 skc
->skc_slab_destroy
= 0;
1200 skc
->skc_slab_total
= 0;
1201 skc
->skc_slab_alloc
= 0;
1202 skc
->skc_slab_max
= 0;
1203 skc
->skc_obj_total
= 0;
1204 skc
->skc_obj_alloc
= 0;
1205 skc
->skc_obj_max
= 0;
1208 ASSERT((align
& (align
- 1)) == 0); /* Power of two */
1209 ASSERT(align
>= SPL_KMEM_CACHE_ALIGN
); /* Minimum size */
1210 skc
->skc_obj_align
= align
;
1213 /* If none passed select a cache type based on object size */
1214 if (!(skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
))) {
1215 if (P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
) <
1217 skc
->skc_flags
|= KMC_KMEM
;
1219 skc
->skc_flags
|= KMC_VMEM
;
1223 rc
= spl_slab_size(skc
, &skc
->skc_slab_objs
, &skc
->skc_slab_size
);
1227 rc
= spl_magazine_create(skc
);
1231 spl_init_delayed_work(&skc
->skc_work
, spl_cache_age
, skc
);
1232 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
1234 down_write(&spl_kmem_cache_sem
);
1235 list_add_tail(&skc
->skc_list
, &spl_kmem_cache_list
);
1236 up_write(&spl_kmem_cache_sem
);
1240 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1241 kmem_free(skc
, sizeof(*skc
));
1244 EXPORT_SYMBOL(spl_kmem_cache_create
);
1247 * Destroy a cache and all objects assoicated with the cache.
1250 spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
)
1252 DECLARE_WAIT_QUEUE_HEAD(wq
);
1256 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1258 down_write(&spl_kmem_cache_sem
);
1259 list_del_init(&skc
->skc_list
);
1260 up_write(&spl_kmem_cache_sem
);
1262 /* Cancel any and wait for any pending delayed work */
1263 ASSERT(!test_and_set_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1264 cancel_delayed_work(&skc
->skc_work
);
1265 for_each_online_cpu(i
)
1266 cancel_delayed_work(&skc
->skc_mag
[i
]->skm_work
);
1268 flush_scheduled_work();
1270 /* Wait until all current callers complete, this is mainly
1271 * to catch the case where a low memory situation triggers a
1272 * cache reaping action which races with this destroy. */
1273 wait_event(wq
, atomic_read(&skc
->skc_ref
) == 0);
1275 spl_magazine_destroy(skc
);
1276 spl_slab_reclaim(skc
, 0, 1);
1277 spin_lock(&skc
->skc_lock
);
1279 /* Validate there are no objects in use and free all the
1280 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
1281 ASSERT3U(skc
->skc_slab_alloc
, ==, 0);
1282 ASSERT3U(skc
->skc_obj_alloc
, ==, 0);
1283 ASSERT3U(skc
->skc_slab_total
, ==, 0);
1284 ASSERT3U(skc
->skc_obj_total
, ==, 0);
1285 ASSERT(list_empty(&skc
->skc_complete_list
));
1287 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1288 spin_unlock(&skc
->skc_lock
);
1290 kmem_free(skc
, sizeof(*skc
));
1294 EXPORT_SYMBOL(spl_kmem_cache_destroy
);
1297 * Allocate an object from a slab attached to the cache. This is used to
1298 * repopulate the per-cpu magazine caches in batches when they run low.
1301 spl_cache_obj(spl_kmem_cache_t
*skc
, spl_kmem_slab_t
*sks
)
1303 spl_kmem_obj_t
*sko
;
1305 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1306 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1307 ASSERT(spin_is_locked(&skc
->skc_lock
));
1309 sko
= list_entry(sks
->sks_free_list
.next
, spl_kmem_obj_t
, sko_list
);
1310 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1311 ASSERT(sko
->sko_addr
!= NULL
);
1313 /* Remove from sks_free_list */
1314 list_del_init(&sko
->sko_list
);
1316 sks
->sks_age
= jiffies
;
1318 skc
->skc_obj_alloc
++;
1320 /* Track max obj usage statistics */
1321 if (skc
->skc_obj_alloc
> skc
->skc_obj_max
)
1322 skc
->skc_obj_max
= skc
->skc_obj_alloc
;
1324 /* Track max slab usage statistics */
1325 if (sks
->sks_ref
== 1) {
1326 skc
->skc_slab_alloc
++;
1328 if (skc
->skc_slab_alloc
> skc
->skc_slab_max
)
1329 skc
->skc_slab_max
= skc
->skc_slab_alloc
;
1332 return sko
->sko_addr
;
1336 * No available objects on any slabsi, create a new slab. Since this
1337 * is an expensive operation we do it without holding the spinlock and
1338 * only briefly aquire it when we link in the fully allocated and
1341 static spl_kmem_slab_t
*
1342 spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
)
1344 spl_kmem_slab_t
*sks
;
1347 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1352 * Before allocating a new slab check if the slab is being reaped.
1353 * If it is there is a good chance we can wait until it finishes
1354 * and then use one of the newly freed but not aged-out slabs.
1356 if (test_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1358 GOTO(out
, sks
= NULL
);
1361 /* Allocate a new slab for the cache */
1362 sks
= spl_slab_alloc(skc
, flags
| __GFP_NORETRY
| __GFP_NOWARN
);
1364 GOTO(out
, sks
= NULL
);
1366 /* Link the new empty slab in to the end of skc_partial_list. */
1367 spin_lock(&skc
->skc_lock
);
1368 skc
->skc_slab_total
++;
1369 skc
->skc_obj_total
+= sks
->sks_objs
;
1370 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1371 spin_unlock(&skc
->skc_lock
);
1373 local_irq_disable();
1379 * Refill a per-cpu magazine with objects from the slabs for this
1380 * cache. Ideally the magazine can be repopulated using existing
1381 * objects which have been released, however if we are unable to
1382 * locate enough free objects new slabs of objects will be created.
1385 spl_cache_refill(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flags
)
1387 spl_kmem_slab_t
*sks
;
1391 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1392 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1394 refill
= MIN(skm
->skm_refill
, skm
->skm_size
- skm
->skm_avail
);
1395 spin_lock(&skc
->skc_lock
);
1397 while (refill
> 0) {
1398 /* No slabs available we may need to grow the cache */
1399 if (list_empty(&skc
->skc_partial_list
)) {
1400 spin_unlock(&skc
->skc_lock
);
1402 sks
= spl_cache_grow(skc
, flags
);
1406 /* Rescheduled to different CPU skm is not local */
1407 if (skm
!= skc
->skc_mag
[smp_processor_id()])
1410 /* Potentially rescheduled to the same CPU but
1411 * allocations may have occured from this CPU while
1412 * we were sleeping so recalculate max refill. */
1413 refill
= MIN(refill
, skm
->skm_size
- skm
->skm_avail
);
1415 spin_lock(&skc
->skc_lock
);
1419 /* Grab the next available slab */
1420 sks
= list_entry((&skc
->skc_partial_list
)->next
,
1421 spl_kmem_slab_t
, sks_list
);
1422 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1423 ASSERT(sks
->sks_ref
< sks
->sks_objs
);
1424 ASSERT(!list_empty(&sks
->sks_free_list
));
1426 /* Consume as many objects as needed to refill the requested
1427 * cache. We must also be careful not to overfill it. */
1428 while (sks
->sks_ref
< sks
->sks_objs
&& refill
-- > 0 && ++rc
) {
1429 ASSERT(skm
->skm_avail
< skm
->skm_size
);
1430 ASSERT(rc
< skm
->skm_size
);
1431 skm
->skm_objs
[skm
->skm_avail
++]=spl_cache_obj(skc
,sks
);
1434 /* Move slab to skc_complete_list when full */
1435 if (sks
->sks_ref
== sks
->sks_objs
) {
1436 list_del(&sks
->sks_list
);
1437 list_add(&sks
->sks_list
, &skc
->skc_complete_list
);
1441 spin_unlock(&skc
->skc_lock
);
1443 /* Returns the number of entries added to cache */
1448 * Release an object back to the slab from which it came.
1451 spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
)
1453 spl_kmem_slab_t
*sks
= NULL
;
1454 spl_kmem_obj_t
*sko
= NULL
;
1457 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1458 ASSERT(spin_is_locked(&skc
->skc_lock
));
1460 sko
= obj
+ P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
);
1461 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1463 sks
= sko
->sko_slab
;
1464 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1465 ASSERT(sks
->sks_cache
== skc
);
1466 list_add(&sko
->sko_list
, &sks
->sks_free_list
);
1468 sks
->sks_age
= jiffies
;
1470 skc
->skc_obj_alloc
--;
1472 /* Move slab to skc_partial_list when no longer full. Slabs
1473 * are added to the head to keep the partial list is quasi-full
1474 * sorted order. Fuller at the head, emptier at the tail. */
1475 if (sks
->sks_ref
== (sks
->sks_objs
- 1)) {
1476 list_del(&sks
->sks_list
);
1477 list_add(&sks
->sks_list
, &skc
->skc_partial_list
);
1480 /* Move emply slabs to the end of the partial list so
1481 * they can be easily found and freed during reclamation. */
1482 if (sks
->sks_ref
== 0) {
1483 list_del(&sks
->sks_list
);
1484 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1485 skc
->skc_slab_alloc
--;
1492 * Release a batch of objects from a per-cpu magazine back to their
1493 * respective slabs. This occurs when we exceed the magazine size,
1494 * are under memory pressure, when the cache is idle, or during
1495 * cache cleanup. The flush argument contains the number of entries
1496 * to remove from the magazine.
1499 spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
1501 int i
, count
= MIN(flush
, skm
->skm_avail
);
1504 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1505 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1508 * XXX: Currently we simply return objects from the magazine to
1509 * the slabs in fifo order. The ideal thing to do from a memory
1510 * fragmentation standpoint is to cheaply determine the set of
1511 * objects in the magazine which will result in the largest
1512 * number of free slabs if released from the magazine.
1514 spin_lock(&skc
->skc_lock
);
1515 for (i
= 0; i
< count
; i
++)
1516 spl_cache_shrink(skc
, skm
->skm_objs
[i
]);
1518 skm
->skm_avail
-= count
;
1519 memmove(skm
->skm_objs
, &(skm
->skm_objs
[count
]),
1520 sizeof(void *) * skm
->skm_avail
);
1522 spin_unlock(&skc
->skc_lock
);
1528 * Allocate an object from the per-cpu magazine, or if the magazine
1529 * is empty directly allocate from a slab and repopulate the magazine.
1532 spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
)
1534 spl_kmem_magazine_t
*skm
;
1535 unsigned long irq_flags
;
1539 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1540 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1541 ASSERT(flags
& KM_SLEEP
);
1542 atomic_inc(&skc
->skc_ref
);
1543 local_irq_save(irq_flags
);
1546 /* Safe to update per-cpu structure without lock, but
1547 * in the restart case we must be careful to reaquire
1548 * the local magazine since this may have changed
1549 * when we need to grow the cache. */
1550 skm
= skc
->skc_mag
[smp_processor_id()];
1551 ASSERTF(skm
->skm_magic
== SKM_MAGIC
, "%x != %x: %s/%p/%p %x/%x/%x\n",
1552 skm
->skm_magic
, SKM_MAGIC
, skc
->skc_name
, skc
, skm
,
1553 skm
->skm_size
, skm
->skm_refill
, skm
->skm_avail
);
1555 if (likely(skm
->skm_avail
)) {
1556 /* Object available in CPU cache, use it */
1557 obj
= skm
->skm_objs
[--skm
->skm_avail
];
1558 skm
->skm_age
= jiffies
;
1560 /* Per-CPU cache empty, directly allocate from
1561 * the slab and refill the per-CPU cache. */
1562 (void)spl_cache_refill(skc
, skm
, flags
);
1563 GOTO(restart
, obj
= NULL
);
1566 local_irq_restore(irq_flags
);
1568 ASSERT(((unsigned long)(obj
) % skc
->skc_obj_align
) == 0);
1570 /* Pre-emptively migrate object to CPU L1 cache */
1572 atomic_dec(&skc
->skc_ref
);
1576 EXPORT_SYMBOL(spl_kmem_cache_alloc
);
1579 * Free an object back to the local per-cpu magazine, there is no
1580 * guarantee that this is the same magazine the object was originally
1581 * allocated from. We may need to flush entire from the magazine
1582 * back to the slabs to make space.
1585 spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
)
1587 spl_kmem_magazine_t
*skm
;
1588 unsigned long flags
;
1591 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1592 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1593 atomic_inc(&skc
->skc_ref
);
1594 local_irq_save(flags
);
1596 /* Safe to update per-cpu structure without lock, but
1597 * no remote memory allocation tracking is being performed
1598 * it is entirely possible to allocate an object from one
1599 * CPU cache and return it to another. */
1600 skm
= skc
->skc_mag
[smp_processor_id()];
1601 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1603 /* Per-CPU cache full, flush it to make space */
1604 if (unlikely(skm
->skm_avail
>= skm
->skm_size
))
1605 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1607 /* Available space in cache, use it */
1608 skm
->skm_objs
[skm
->skm_avail
++] = obj
;
1610 local_irq_restore(flags
);
1611 atomic_dec(&skc
->skc_ref
);
1615 EXPORT_SYMBOL(spl_kmem_cache_free
);
1618 * The generic shrinker function for all caches. Under linux a shrinker
1619 * may not be tightly coupled with a slab cache. In fact linux always
1620 * systematically trys calling all registered shrinker callbacks which
1621 * report that they contain unused objects. Because of this we only
1622 * register one shrinker function in the shim layer for all slab caches.
1623 * We always attempt to shrink all caches when this generic shrinker
1624 * is called. The shrinker should return the number of free objects
1625 * in the cache when called with nr_to_scan == 0 but not attempt to
1626 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
1627 * objects should be freed, because Solaris semantics are to free
1628 * all available objects we may free more objects than requested.
1631 spl_kmem_cache_generic_shrinker(int nr_to_scan
, unsigned int gfp_mask
)
1633 spl_kmem_cache_t
*skc
;
1636 down_read(&spl_kmem_cache_sem
);
1637 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
1639 spl_kmem_cache_reap_now(skc
);
1642 * Presume everything alloc'ed in reclaimable, this ensures
1643 * we are called again with nr_to_scan > 0 so can try and
1644 * reclaim. The exact number is not important either so
1645 * we forgo taking this already highly contented lock.
1647 unused
+= skc
->skc_obj_alloc
;
1649 up_read(&spl_kmem_cache_sem
);
1651 return (unused
* sysctl_vfs_cache_pressure
) / 100;
1655 * Call the registered reclaim function for a cache. Depending on how
1656 * many and which objects are released it may simply repopulate the
1657 * local magazine which will then need to age-out. Objects which cannot
1658 * fit in the magazine we will be released back to their slabs which will
1659 * also need to age out before being release. This is all just best
1660 * effort and we do not want to thrash creating and destroying slabs.
1663 spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
)
1667 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1668 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1670 /* Prevent concurrent cache reaping when contended */
1671 if (test_and_set_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1676 atomic_inc(&skc
->skc_ref
);
1678 if (skc
->skc_reclaim
)
1679 skc
->skc_reclaim(skc
->skc_private
);
1681 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
1682 clear_bit(KMC_BIT_REAPING
, &skc
->skc_flags
);
1683 atomic_dec(&skc
->skc_ref
);
1687 EXPORT_SYMBOL(spl_kmem_cache_reap_now
);
1690 * Reap all free slabs from all registered caches.
1695 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK
, GFP_KERNEL
);
1697 EXPORT_SYMBOL(spl_kmem_reap
);
1699 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
1701 spl_sprintf_addr(kmem_debug_t
*kd
, char *str
, int len
, int min
)
1703 int size
= ((len
- 1) < kd
->kd_size
) ? (len
- 1) : kd
->kd_size
;
1706 ASSERT(str
!= NULL
&& len
>= 17);
1707 memset(str
, 0, len
);
1709 /* Check for a fully printable string, and while we are at
1710 * it place the printable characters in the passed buffer. */
1711 for (i
= 0; i
< size
; i
++) {
1712 str
[i
] = ((char *)(kd
->kd_addr
))[i
];
1713 if (isprint(str
[i
])) {
1716 /* Minimum number of printable characters found
1717 * to make it worthwhile to print this as ascii. */
1727 sprintf(str
, "%02x%02x%02x%02x%02x%02x%02x%02x",
1728 *((uint8_t *)kd
->kd_addr
),
1729 *((uint8_t *)kd
->kd_addr
+ 2),
1730 *((uint8_t *)kd
->kd_addr
+ 4),
1731 *((uint8_t *)kd
->kd_addr
+ 6),
1732 *((uint8_t *)kd
->kd_addr
+ 8),
1733 *((uint8_t *)kd
->kd_addr
+ 10),
1734 *((uint8_t *)kd
->kd_addr
+ 12),
1735 *((uint8_t *)kd
->kd_addr
+ 14));
1742 spl_kmem_init_tracking(struct list_head
*list
, spinlock_t
*lock
, int size
)
1747 spin_lock_init(lock
);
1748 INIT_LIST_HEAD(list
);
1750 for (i
= 0; i
< size
; i
++)
1751 INIT_HLIST_HEAD(&kmem_table
[i
]);
1757 spl_kmem_fini_tracking(struct list_head
*list
, spinlock_t
*lock
)
1759 unsigned long flags
;
1764 spin_lock_irqsave(lock
, flags
);
1765 if (!list_empty(list
))
1766 printk(KERN_WARNING
"%-16s %-5s %-16s %s:%s\n", "address",
1767 "size", "data", "func", "line");
1769 list_for_each_entry(kd
, list
, kd_list
)
1770 printk(KERN_WARNING
"%p %-5d %-16s %s:%d\n", kd
->kd_addr
,
1771 (int)kd
->kd_size
, spl_sprintf_addr(kd
, str
, 17, 8),
1772 kd
->kd_func
, kd
->kd_line
);
1774 spin_unlock_irqrestore(lock
, flags
);
1777 #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1778 #define spl_kmem_init_tracking(list, lock, size)
1779 #define spl_kmem_fini_tracking(list, lock)
1780 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1783 spl_kmem_init_globals(void)
1787 /* For now all zones are includes, it may be wise to restrict
1788 * this to normal and highmem zones if we see problems. */
1789 for_each_zone(zone
) {
1791 if (!populated_zone(zone
))
1794 minfree
+= zone
->pages_min
;
1795 desfree
+= zone
->pages_low
;
1796 lotsfree
+= zone
->pages_high
;
1799 /* Solaris default values */
1800 swapfs_minfree
= MAX(2*1024*1024 >> PAGE_SHIFT
, physmem
>> 3);
1801 swapfs_reserve
= MIN(4*1024*1024 >> PAGE_SHIFT
, physmem
>> 4);
1805 * Called at module init when it is safe to use spl_kallsyms_lookup_name()
1808 spl_kmem_init_kallsyms_lookup(void)
1810 #ifndef HAVE_GET_VMALLOC_INFO
1811 get_vmalloc_info_fn
= (get_vmalloc_info_t
)
1812 spl_kallsyms_lookup_name("get_vmalloc_info");
1813 if (!get_vmalloc_info_fn
) {
1814 printk(KERN_ERR
"Error: Unknown symbol get_vmalloc_info\n");
1817 #endif /* HAVE_GET_VMALLOC_INFO */
1819 #ifdef HAVE_PGDAT_HELPERS
1820 # ifndef HAVE_FIRST_ONLINE_PGDAT
1821 first_online_pgdat_fn
= (first_online_pgdat_t
)
1822 spl_kallsyms_lookup_name("first_online_pgdat");
1823 if (!first_online_pgdat_fn
) {
1824 printk(KERN_ERR
"Error: Unknown symbol first_online_pgdat\n");
1827 # endif /* HAVE_FIRST_ONLINE_PGDAT */
1829 # ifndef HAVE_NEXT_ONLINE_PGDAT
1830 next_online_pgdat_fn
= (next_online_pgdat_t
)
1831 spl_kallsyms_lookup_name("next_online_pgdat");
1832 if (!next_online_pgdat_fn
) {
1833 printk(KERN_ERR
"Error: Unknown symbol next_online_pgdat\n");
1836 # endif /* HAVE_NEXT_ONLINE_PGDAT */
1838 # ifndef HAVE_NEXT_ZONE
1839 next_zone_fn
= (next_zone_t
)
1840 spl_kallsyms_lookup_name("next_zone");
1841 if (!next_zone_fn
) {
1842 printk(KERN_ERR
"Error: Unknown symbol next_zone\n");
1845 # endif /* HAVE_NEXT_ZONE */
1847 #else /* HAVE_PGDAT_HELPERS */
1849 # ifndef HAVE_PGDAT_LIST
1850 pgdat_list_addr
= *(struct pglist_data
**)
1851 spl_kallsyms_lookup_name("pgdat_list");
1852 if (!pgdat_list_addr
) {
1853 printk(KERN_ERR
"Error: Unknown symbol pgdat_list\n");
1856 # endif /* HAVE_PGDAT_LIST */
1857 #endif /* HAVE_PGDAT_HELPERS */
1859 #ifndef HAVE_ZONE_STAT_ITEM_FIA
1860 # ifndef HAVE_GET_ZONE_COUNTS
1861 get_zone_counts_fn
= (get_zone_counts_t
)
1862 spl_kallsyms_lookup_name("get_zone_counts");
1863 if (!get_zone_counts_fn
) {
1864 printk(KERN_ERR
"Error: Unknown symbol get_zone_counts\n");
1867 # endif /* HAVE_GET_ZONE_COUNTS */
1868 #endif /* HAVE_ZONE_STAT_ITEM_FIA */
1871 * It is now safe to initialize the global tunings which rely on
1872 * the use of the for_each_zone() macro. This macro in turns
1873 * depends on the *_pgdat symbols which are now available.
1875 spl_kmem_init_globals();
1886 init_rwsem(&spl_kmem_cache_sem
);
1887 INIT_LIST_HEAD(&spl_kmem_cache_list
);
1889 #ifdef HAVE_SET_SHRINKER
1890 spl_kmem_cache_shrinker
= set_shrinker(KMC_DEFAULT_SEEKS
,
1891 spl_kmem_cache_generic_shrinker
);
1892 if (spl_kmem_cache_shrinker
== NULL
)
1893 RETURN(rc
= -ENOMEM
);
1895 register_shrinker(&spl_kmem_cache_shrinker
);
1899 atomic64_set(&kmem_alloc_used
, 0);
1900 atomic64_set(&vmem_alloc_used
, 0);
1902 spl_kmem_init_tracking(&kmem_list
, &kmem_lock
, KMEM_TABLE_SIZE
);
1903 spl_kmem_init_tracking(&vmem_list
, &vmem_lock
, VMEM_TABLE_SIZE
);
1912 /* Display all unreclaimed memory addresses, including the
1913 * allocation size and the first few bytes of what's located
1914 * at that address to aid in debugging. Performance is not
1915 * a serious concern here since it is module unload time. */
1916 if (atomic64_read(&kmem_alloc_used
) != 0)
1917 CWARN("kmem leaked %ld/%ld bytes\n",
1918 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
1921 if (atomic64_read(&vmem_alloc_used
) != 0)
1922 CWARN("vmem leaked %ld/%ld bytes\n",
1923 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
1925 spl_kmem_fini_tracking(&kmem_list
, &kmem_lock
);
1926 spl_kmem_fini_tracking(&vmem_list
, &vmem_lock
);
1927 #endif /* DEBUG_KMEM */
1930 #ifdef HAVE_SET_SHRINKER
1931 remove_shrinker(spl_kmem_cache_shrinker
);
1933 unregister_shrinker(&spl_kmem_cache_shrinker
);