1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Kmem Implementation.
25 \*****************************************************************************/
29 #ifdef DEBUG_SUBSYSTEM
30 # undef DEBUG_SUBSYSTEM
33 #define DEBUG_SUBSYSTEM S_KMEM
36 * The minimum amount of memory measured in pages to be free at all
37 * times on the system. This is similar to Linux's zone->pages_min
38 * multipled by the number of zones and is sized based on that.
41 EXPORT_SYMBOL(minfree
);
44 * The desired amount of memory measured in pages to be free at all
45 * times on the system. This is similar to Linux's zone->pages_low
46 * multipled by the number of zones and is sized based on that.
47 * Assuming all zones are being used roughly equally, when we drop
48 * below this threshold async page reclamation is triggered.
51 EXPORT_SYMBOL(desfree
);
54 * When above this amount of memory measures in pages the system is
55 * determined to have enough free memory. This is similar to Linux's
56 * zone->pages_high multipled by the number of zones and is sized based
57 * on that. Assuming all zones are being used roughly equally, when
58 * async page reclamation reaches this threshold it stops.
61 EXPORT_SYMBOL(lotsfree
);
63 /* Unused always 0 in this implementation */
65 EXPORT_SYMBOL(needfree
);
67 pgcnt_t swapfs_minfree
= 0;
68 EXPORT_SYMBOL(swapfs_minfree
);
70 pgcnt_t swapfs_reserve
= 0;
71 EXPORT_SYMBOL(swapfs_reserve
);
73 vmem_t
*heap_arena
= NULL
;
74 EXPORT_SYMBOL(heap_arena
);
76 vmem_t
*zio_alloc_arena
= NULL
;
77 EXPORT_SYMBOL(zio_alloc_arena
);
79 vmem_t
*zio_arena
= NULL
;
80 EXPORT_SYMBOL(zio_arena
);
82 #ifndef HAVE_GET_VMALLOC_INFO
83 get_vmalloc_info_t get_vmalloc_info_fn
= SYMBOL_POISON
;
84 EXPORT_SYMBOL(get_vmalloc_info_fn
);
85 #endif /* HAVE_GET_VMALLOC_INFO */
87 #ifdef HAVE_PGDAT_HELPERS
88 # ifndef HAVE_FIRST_ONLINE_PGDAT
89 first_online_pgdat_t first_online_pgdat_fn
= SYMBOL_POISON
;
90 EXPORT_SYMBOL(first_online_pgdat_fn
);
91 # endif /* HAVE_FIRST_ONLINE_PGDAT */
93 # ifndef HAVE_NEXT_ONLINE_PGDAT
94 next_online_pgdat_t next_online_pgdat_fn
= SYMBOL_POISON
;
95 EXPORT_SYMBOL(next_online_pgdat_fn
);
96 # endif /* HAVE_NEXT_ONLINE_PGDAT */
98 # ifndef HAVE_NEXT_ZONE
99 next_zone_t next_zone_fn
= SYMBOL_POISON
;
100 EXPORT_SYMBOL(next_zone_fn
);
101 # endif /* HAVE_NEXT_ZONE */
103 #else /* HAVE_PGDAT_HELPERS */
105 # ifndef HAVE_PGDAT_LIST
106 struct pglist_data
*pgdat_list_addr
= SYMBOL_POISON
;
107 EXPORT_SYMBOL(pgdat_list_addr
);
108 # endif /* HAVE_PGDAT_LIST */
110 #endif /* HAVE_PGDAT_HELPERS */
112 #ifdef NEED_GET_ZONE_COUNTS
113 # ifndef HAVE_GET_ZONE_COUNTS
114 get_zone_counts_t get_zone_counts_fn
= SYMBOL_POISON
;
115 EXPORT_SYMBOL(get_zone_counts_fn
);
116 # endif /* HAVE_GET_ZONE_COUNTS */
119 spl_global_page_state(spl_zone_stat_item_t item
)
121 unsigned long active
;
122 unsigned long inactive
;
125 get_zone_counts(&active
, &inactive
, &free
);
127 case SPL_NR_FREE_PAGES
: return free
;
128 case SPL_NR_INACTIVE
: return inactive
;
129 case SPL_NR_ACTIVE
: return active
;
130 default: ASSERT(0); /* Unsupported */
136 # ifdef HAVE_GLOBAL_PAGE_STATE
138 spl_global_page_state(spl_zone_stat_item_t item
)
140 unsigned long pages
= 0;
143 case SPL_NR_FREE_PAGES
:
144 # ifdef HAVE_ZONE_STAT_ITEM_NR_FREE_PAGES
145 pages
+= global_page_state(NR_FREE_PAGES
);
148 case SPL_NR_INACTIVE
:
149 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE
150 pages
+= global_page_state(NR_INACTIVE
);
152 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_ANON
153 pages
+= global_page_state(NR_INACTIVE_ANON
);
155 # ifdef HAVE_ZONE_STAT_ITEM_NR_INACTIVE_FILE
156 pages
+= global_page_state(NR_INACTIVE_FILE
);
160 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE
161 pages
+= global_page_state(NR_ACTIVE
);
163 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_ANON
164 pages
+= global_page_state(NR_ACTIVE_ANON
);
166 # ifdef HAVE_ZONE_STAT_ITEM_NR_ACTIVE_FILE
167 pages
+= global_page_state(NR_ACTIVE_FILE
);
171 ASSERT(0); /* Unsupported */
177 # error "Both global_page_state() and get_zone_counts() unavailable"
178 # endif /* HAVE_GLOBAL_PAGE_STATE */
179 #endif /* NEED_GET_ZONE_COUNTS */
180 EXPORT_SYMBOL(spl_global_page_state
);
183 spl_kmem_availrmem(void)
185 /* The amount of easily available memory */
186 return (spl_global_page_state(SPL_NR_FREE_PAGES
) +
187 spl_global_page_state(SPL_NR_INACTIVE
));
189 EXPORT_SYMBOL(spl_kmem_availrmem
);
192 vmem_size(vmem_t
*vmp
, int typemask
)
194 struct vmalloc_info vmi
;
198 ASSERT(typemask
& (VMEM_ALLOC
| VMEM_FREE
));
200 get_vmalloc_info(&vmi
);
201 if (typemask
& VMEM_ALLOC
)
202 size
+= (size_t)vmi
.used
;
204 if (typemask
& VMEM_FREE
)
205 size
+= (size_t)(VMALLOC_TOTAL
- vmi
.used
);
209 EXPORT_SYMBOL(vmem_size
);
212 * Memory allocation interfaces and debugging for basic kmem_*
213 * and vmem_* style memory allocation. When DEBUG_KMEM is enabled
214 * the SPL will keep track of the total memory allocated, and
215 * report any memory leaked when the module is unloaded.
219 /* Shim layer memory accounting */
220 # ifdef HAVE_ATOMIC64_T
221 atomic64_t kmem_alloc_used
= ATOMIC64_INIT(0);
222 unsigned long long kmem_alloc_max
= 0;
223 atomic64_t vmem_alloc_used
= ATOMIC64_INIT(0);
224 unsigned long long vmem_alloc_max
= 0;
226 atomic_t kmem_alloc_used
= ATOMIC_INIT(0);
227 unsigned long long kmem_alloc_max
= 0;
228 atomic_t vmem_alloc_used
= ATOMIC_INIT(0);
229 unsigned long long vmem_alloc_max
= 0;
232 EXPORT_SYMBOL(kmem_alloc_used
);
233 EXPORT_SYMBOL(kmem_alloc_max
);
234 EXPORT_SYMBOL(vmem_alloc_used
);
235 EXPORT_SYMBOL(vmem_alloc_max
);
237 /* When DEBUG_KMEM_TRACKING is enabled not only will total bytes be tracked
238 * but also the location of every alloc and free. When the SPL module is
239 * unloaded a list of all leaked addresses and where they were allocated
240 * will be dumped to the console. Enabling this feature has a significant
241 * impact on performance but it makes finding memory leaks straight forward.
243 * Not surprisingly with debugging enabled the xmem_locks are very highly
244 * contended particularly on xfree(). If we want to run with this detailed
245 * debugging enabled for anything other than debugging we need to minimize
246 * the contention by moving to a lock per xmem_table entry model.
248 # ifdef DEBUG_KMEM_TRACKING
250 # define KMEM_HASH_BITS 10
251 # define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
253 # define VMEM_HASH_BITS 10
254 # define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS)
256 typedef struct kmem_debug
{
257 struct hlist_node kd_hlist
; /* Hash node linkage */
258 struct list_head kd_list
; /* List of all allocations */
259 void *kd_addr
; /* Allocation pointer */
260 size_t kd_size
; /* Allocation size */
261 const char *kd_func
; /* Allocation function */
262 int kd_line
; /* Allocation line */
265 spinlock_t kmem_lock
;
266 struct hlist_head kmem_table
[KMEM_TABLE_SIZE
];
267 struct list_head kmem_list
;
269 spinlock_t vmem_lock
;
270 struct hlist_head vmem_table
[VMEM_TABLE_SIZE
];
271 struct list_head vmem_list
;
273 EXPORT_SYMBOL(kmem_lock
);
274 EXPORT_SYMBOL(kmem_table
);
275 EXPORT_SYMBOL(kmem_list
);
277 EXPORT_SYMBOL(vmem_lock
);
278 EXPORT_SYMBOL(vmem_table
);
279 EXPORT_SYMBOL(vmem_list
);
284 * Slab allocation interfaces
286 * While the Linux slab implementation was inspired by the Solaris
287 * implemenation I cannot use it to emulate the Solaris APIs. I
288 * require two features which are not provided by the Linux slab.
290 * 1) Constructors AND destructors. Recent versions of the Linux
291 * kernel have removed support for destructors. This is a deal
292 * breaker for the SPL which contains particularly expensive
293 * initializers for mutex's, condition variables, etc. We also
294 * require a minimal level of cleanup for these data types unlike
295 * many Linux data type which do need to be explicitly destroyed.
297 * 2) Virtual address space backed slab. Callers of the Solaris slab
298 * expect it to work well for both small are very large allocations.
299 * Because of memory fragmentation the Linux slab which is backed
300 * by kmalloc'ed memory performs very badly when confronted with
301 * large numbers of large allocations. Basing the slab on the
302 * virtual address space removes the need for contigeous pages
303 * and greatly improve performance for large allocations.
305 * For these reasons, the SPL has its own slab implementation with
306 * the needed features. It is not as highly optimized as either the
307 * Solaris or Linux slabs, but it should get me most of what is
308 * needed until it can be optimized or obsoleted by another approach.
310 * One serious concern I do have about this method is the relatively
311 * small virtual address space on 32bit arches. This will seriously
312 * constrain the size of the slab caches and their performance.
314 * XXX: Improve the partial slab list by carefully maintaining a
315 * strict ordering of fullest to emptiest slabs based on
316 * the slab reference count. This gaurentees the when freeing
317 * slabs back to the system we need only linearly traverse the
318 * last N slabs in the list to discover all the freeable slabs.
320 * XXX: NUMA awareness for optionally allocating memory close to a
321 * particular core. This can be adventageous if you know the slab
322 * object will be short lived and primarily accessed from one core.
324 * XXX: Slab coloring may also yield performance improvements and would
325 * be desirable to implement.
328 struct list_head spl_kmem_cache_list
; /* List of caches */
329 struct rw_semaphore spl_kmem_cache_sem
; /* Cache list lock */
331 static int spl_cache_flush(spl_kmem_cache_t
*skc
,
332 spl_kmem_magazine_t
*skm
, int flush
);
334 #ifdef HAVE_SET_SHRINKER
335 static struct shrinker
*spl_kmem_cache_shrinker
;
337 static int spl_kmem_cache_generic_shrinker(int nr_to_scan
,
338 unsigned int gfp_mask
);
339 static struct shrinker spl_kmem_cache_shrinker
= {
340 .shrink
= spl_kmem_cache_generic_shrinker
,
341 .seeks
= KMC_DEFAULT_SEEKS
,
346 # ifdef DEBUG_KMEM_TRACKING
348 static kmem_debug_t
*
349 kmem_del_init(spinlock_t
*lock
, struct hlist_head
*table
, int bits
,
352 struct hlist_head
*head
;
353 struct hlist_node
*node
;
354 struct kmem_debug
*p
;
358 spin_lock_irqsave(lock
, flags
);
360 head
= &table
[hash_ptr(addr
, bits
)];
361 hlist_for_each_entry_rcu(p
, node
, head
, kd_hlist
) {
362 if (p
->kd_addr
== addr
) {
363 hlist_del_init(&p
->kd_hlist
);
364 list_del_init(&p
->kd_list
);
365 spin_unlock_irqrestore(lock
, flags
);
370 spin_unlock_irqrestore(lock
, flags
);
376 kmem_alloc_track(size_t size
, int flags
, const char *func
, int line
,
377 int node_alloc
, int node
)
381 unsigned long irq_flags
;
384 dptr
= (kmem_debug_t
*) kmalloc_nofail(sizeof(kmem_debug_t
),
385 flags
& ~__GFP_ZERO
);
388 CWARN("kmem_alloc(%ld, 0x%x) debug failed\n",
389 sizeof(kmem_debug_t
), flags
);
391 /* Marked unlikely because we should never be doing this,
392 * we tolerate to up 2 pages but a single page is best. */
393 if (unlikely((size
> PAGE_SIZE
*2) && !(flags
& __GFP_NOWARN
))) {
394 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
395 (unsigned long long) size
, flags
,
396 kmem_alloc_used_read(), kmem_alloc_max
);
397 spl_debug_dumpstack(NULL
);
400 /* We use kstrdup() below because the string pointed to by
401 * __FUNCTION__ might not be available by the time we want
402 * to print it since the module might have been unloaded. */
403 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
404 if (unlikely(dptr
->kd_func
== NULL
)) {
406 CWARN("kstrdup() failed in kmem_alloc(%llu, 0x%x) "
407 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
408 kmem_alloc_used_read(), kmem_alloc_max
);
412 /* Use the correct allocator */
414 ASSERT(!(flags
& __GFP_ZERO
));
415 ptr
= kmalloc_node_nofail(size
, flags
, node
);
416 } else if (flags
& __GFP_ZERO
) {
417 ptr
= kzalloc_nofail(size
, flags
& ~__GFP_ZERO
);
419 ptr
= kmalloc_nofail(size
, flags
);
422 if (unlikely(ptr
== NULL
)) {
423 kfree(dptr
->kd_func
);
425 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
426 (unsigned long long) size
, flags
,
427 kmem_alloc_used_read(), kmem_alloc_max
);
431 kmem_alloc_used_add(size
);
432 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max
))
433 kmem_alloc_max
= kmem_alloc_used_read();
435 INIT_HLIST_NODE(&dptr
->kd_hlist
);
436 INIT_LIST_HEAD(&dptr
->kd_list
);
439 dptr
->kd_size
= size
;
440 dptr
->kd_line
= line
;
442 spin_lock_irqsave(&kmem_lock
, irq_flags
);
443 hlist_add_head_rcu(&dptr
->kd_hlist
,
444 &kmem_table
[hash_ptr(ptr
, KMEM_HASH_BITS
)]);
445 list_add_tail(&dptr
->kd_list
, &kmem_list
);
446 spin_unlock_irqrestore(&kmem_lock
, irq_flags
);
448 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
449 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
450 ptr
, kmem_alloc_used_read(),
456 EXPORT_SYMBOL(kmem_alloc_track
);
459 kmem_free_track(void *ptr
, size_t size
)
464 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
465 (unsigned long long) size
);
467 dptr
= kmem_del_init(&kmem_lock
, kmem_table
, KMEM_HASH_BITS
, ptr
);
469 ASSERT(dptr
); /* Must exist in hash due to kmem_alloc() */
471 /* Size must match */
472 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
473 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
474 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
476 kmem_alloc_used_sub(size
);
477 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
478 (unsigned long long) size
, kmem_alloc_used_read(),
481 kfree(dptr
->kd_func
);
483 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
486 memset(ptr
, 0x5a, size
);
491 EXPORT_SYMBOL(kmem_free_track
);
494 vmem_alloc_track(size_t size
, int flags
, const char *func
, int line
)
498 unsigned long irq_flags
;
501 ASSERT(flags
& KM_SLEEP
);
503 dptr
= (kmem_debug_t
*) kmalloc_nofail(sizeof(kmem_debug_t
),
504 flags
& ~__GFP_ZERO
);
506 CWARN("vmem_alloc(%ld, 0x%x) debug failed\n",
507 sizeof(kmem_debug_t
), flags
);
509 /* We use kstrdup() below because the string pointed to by
510 * __FUNCTION__ might not be available by the time we want
511 * to print it, since the module might have been unloaded. */
512 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
513 if (unlikely(dptr
->kd_func
== NULL
)) {
515 CWARN("kstrdup() failed in vmem_alloc(%llu, 0x%x) "
516 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
517 vmem_alloc_used_read(), vmem_alloc_max
);
521 ptr
= __vmalloc(size
, (flags
| __GFP_HIGHMEM
) & ~__GFP_ZERO
,
524 if (unlikely(ptr
== NULL
)) {
525 kfree(dptr
->kd_func
);
527 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
528 (unsigned long long) size
, flags
,
529 vmem_alloc_used_read(), vmem_alloc_max
);
533 if (flags
& __GFP_ZERO
)
534 memset(ptr
, 0, size
);
536 vmem_alloc_used_add(size
);
537 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max
))
538 vmem_alloc_max
= vmem_alloc_used_read();
540 INIT_HLIST_NODE(&dptr
->kd_hlist
);
541 INIT_LIST_HEAD(&dptr
->kd_list
);
544 dptr
->kd_size
= size
;
545 dptr
->kd_line
= line
;
547 spin_lock_irqsave(&vmem_lock
, irq_flags
);
548 hlist_add_head_rcu(&dptr
->kd_hlist
,
549 &vmem_table
[hash_ptr(ptr
, VMEM_HASH_BITS
)]);
550 list_add_tail(&dptr
->kd_list
, &vmem_list
);
551 spin_unlock_irqrestore(&vmem_lock
, irq_flags
);
553 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
554 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
555 ptr
, vmem_alloc_used_read(),
561 EXPORT_SYMBOL(vmem_alloc_track
);
564 vmem_free_track(void *ptr
, size_t size
)
569 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
570 (unsigned long long) size
);
572 dptr
= kmem_del_init(&vmem_lock
, vmem_table
, VMEM_HASH_BITS
, ptr
);
573 ASSERT(dptr
); /* Must exist in hash due to vmem_alloc() */
575 /* Size must match */
576 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
577 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
578 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
580 vmem_alloc_used_sub(size
);
581 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
582 (unsigned long long) size
, vmem_alloc_used_read(),
585 kfree(dptr
->kd_func
);
587 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
590 memset(ptr
, 0x5a, size
);
595 EXPORT_SYMBOL(vmem_free_track
);
597 # else /* DEBUG_KMEM_TRACKING */
600 kmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
,
601 int node_alloc
, int node
)
606 /* Marked unlikely because we should never be doing this,
607 * we tolerate to up 2 pages but a single page is best. */
608 if (unlikely((size
> PAGE_SIZE
* 2) && !(flags
& __GFP_NOWARN
))) {
609 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
610 (unsigned long long) size
, flags
,
611 kmem_alloc_used_read(), kmem_alloc_max
);
612 spl_debug_dumpstack(NULL
);
615 /* Use the correct allocator */
617 ASSERT(!(flags
& __GFP_ZERO
));
618 ptr
= kmalloc_node_nofail(size
, flags
, node
);
619 } else if (flags
& __GFP_ZERO
) {
620 ptr
= kzalloc_nofail(size
, flags
& (~__GFP_ZERO
));
622 ptr
= kmalloc_nofail(size
, flags
);
626 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
627 (unsigned long long) size
, flags
,
628 kmem_alloc_used_read(), kmem_alloc_max
);
630 kmem_alloc_used_add(size
);
631 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max
))
632 kmem_alloc_max
= kmem_alloc_used_read();
634 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
635 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
636 kmem_alloc_used_read(), kmem_alloc_max
);
640 EXPORT_SYMBOL(kmem_alloc_debug
);
643 kmem_free_debug(void *ptr
, size_t size
)
647 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
648 (unsigned long long) size
);
650 kmem_alloc_used_sub(size
);
651 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
652 (unsigned long long) size
, kmem_alloc_used_read(),
655 memset(ptr
, 0x5a, size
);
660 EXPORT_SYMBOL(kmem_free_debug
);
663 vmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
)
668 ASSERT(flags
& KM_SLEEP
);
670 ptr
= __vmalloc(size
, (flags
| __GFP_HIGHMEM
) & ~__GFP_ZERO
,
673 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
674 (unsigned long long) size
, flags
,
675 vmem_alloc_used_read(), vmem_alloc_max
);
677 if (flags
& __GFP_ZERO
)
678 memset(ptr
, 0, size
);
680 vmem_alloc_used_add(size
);
681 if (unlikely(vmem_alloc_used_read() > vmem_alloc_max
))
682 vmem_alloc_max
= vmem_alloc_used_read();
684 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
685 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
686 vmem_alloc_used_read(), vmem_alloc_max
);
691 EXPORT_SYMBOL(vmem_alloc_debug
);
694 vmem_free_debug(void *ptr
, size_t size
)
698 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
699 (unsigned long long) size
);
701 vmem_alloc_used_sub(size
);
702 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
703 (unsigned long long) size
, vmem_alloc_used_read(),
706 memset(ptr
, 0x5a, size
);
711 EXPORT_SYMBOL(vmem_free_debug
);
713 # endif /* DEBUG_KMEM_TRACKING */
714 #endif /* DEBUG_KMEM */
717 kv_alloc(spl_kmem_cache_t
*skc
, int size
, int flags
)
723 if (skc
->skc_flags
& KMC_KMEM
)
724 ptr
= (void *)__get_free_pages(flags
, get_order(size
));
726 ptr
= __vmalloc(size
, flags
| __GFP_HIGHMEM
, PAGE_KERNEL
);
728 /* Resulting allocated memory will be page aligned */
729 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
735 kv_free(spl_kmem_cache_t
*skc
, void *ptr
, int size
)
737 ASSERT(IS_P2ALIGNED(ptr
, PAGE_SIZE
));
740 if (skc
->skc_flags
& KMC_KMEM
)
741 free_pages((unsigned long)ptr
, get_order(size
));
747 * Required space for each aligned sks.
749 static inline uint32_t
750 spl_sks_size(spl_kmem_cache_t
*skc
)
752 return P2ROUNDUP_TYPED(sizeof(spl_kmem_slab_t
),
753 skc
->skc_obj_align
, uint32_t);
757 * Required space for each aligned object.
759 static inline uint32_t
760 spl_obj_size(spl_kmem_cache_t
*skc
)
762 uint32_t align
= skc
->skc_obj_align
;
764 return P2ROUNDUP_TYPED(skc
->skc_obj_size
, align
, uint32_t) +
765 P2ROUNDUP_TYPED(sizeof(spl_kmem_obj_t
), align
, uint32_t);
769 * Lookup the spl_kmem_object_t for an object given that object.
771 static inline spl_kmem_obj_t
*
772 spl_sko_from_obj(spl_kmem_cache_t
*skc
, void *obj
)
774 return obj
+ P2ROUNDUP_TYPED(skc
->skc_obj_size
,
775 skc
->skc_obj_align
, uint32_t);
779 * Required space for each offslab object taking in to account alignment
780 * restrictions and the power-of-two requirement of kv_alloc().
782 static inline uint32_t
783 spl_offslab_size(spl_kmem_cache_t
*skc
)
785 return 1UL << (highbit(spl_obj_size(skc
)) + 1);
789 * It's important that we pack the spl_kmem_obj_t structure and the
790 * actual objects in to one large address space to minimize the number
791 * of calls to the allocator. It is far better to do a few large
792 * allocations and then subdivide it ourselves. Now which allocator
793 * we use requires balancing a few trade offs.
795 * For small objects we use kmem_alloc() because as long as you are
796 * only requesting a small number of pages (ideally just one) its cheap.
797 * However, when you start requesting multiple pages with kmem_alloc()
798 * it gets increasingly expensive since it requires contigeous pages.
799 * For this reason we shift to vmem_alloc() for slabs of large objects
800 * which removes the need for contigeous pages. We do not use
801 * vmem_alloc() in all cases because there is significant locking
802 * overhead in __get_vm_area_node(). This function takes a single
803 * global lock when aquiring an available virtual address range which
804 * serializes all vmem_alloc()'s for all slab caches. Using slightly
805 * different allocation functions for small and large objects should
806 * give us the best of both worlds.
808 * KMC_ONSLAB KMC_OFFSLAB
810 * +------------------------+ +-----------------+
811 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
812 * | skc_obj_size <-+ | | +-----------------+ | |
813 * | spl_kmem_obj_t | | | |
814 * | skc_obj_size <---+ | +-----------------+ | |
815 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
816 * | ... v | | spl_kmem_obj_t | |
817 * +------------------------+ +-----------------+ v
819 static spl_kmem_slab_t
*
820 spl_slab_alloc(spl_kmem_cache_t
*skc
, int flags
)
822 spl_kmem_slab_t
*sks
;
823 spl_kmem_obj_t
*sko
, *n
;
825 uint32_t obj_size
, offslab_size
= 0;
828 base
= kv_alloc(skc
, skc
->skc_slab_size
, flags
);
832 sks
= (spl_kmem_slab_t
*)base
;
833 sks
->sks_magic
= SKS_MAGIC
;
834 sks
->sks_objs
= skc
->skc_slab_objs
;
835 sks
->sks_age
= jiffies
;
836 sks
->sks_cache
= skc
;
837 INIT_LIST_HEAD(&sks
->sks_list
);
838 INIT_LIST_HEAD(&sks
->sks_free_list
);
840 obj_size
= spl_obj_size(skc
);
842 if (skc
->skc_flags
* KMC_OFFSLAB
)
843 offslab_size
= spl_offslab_size(skc
);
845 for (i
= 0; i
< sks
->sks_objs
; i
++) {
846 if (skc
->skc_flags
& KMC_OFFSLAB
) {
847 obj
= kv_alloc(skc
, offslab_size
, flags
);
849 GOTO(out
, rc
= -ENOMEM
);
851 obj
= base
+ spl_sks_size(skc
) + (i
* obj_size
);
854 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
855 sko
= spl_sko_from_obj(skc
, obj
);
857 sko
->sko_magic
= SKO_MAGIC
;
859 INIT_LIST_HEAD(&sko
->sko_list
);
860 list_add_tail(&sko
->sko_list
, &sks
->sks_free_list
);
863 list_for_each_entry(sko
, &sks
->sks_free_list
, sko_list
)
865 skc
->skc_ctor(sko
->sko_addr
, skc
->skc_private
, flags
);
868 if (skc
->skc_flags
& KMC_OFFSLAB
)
869 list_for_each_entry_safe(sko
, n
, &sks
->sks_free_list
,
871 kv_free(skc
, sko
->sko_addr
, offslab_size
);
873 kv_free(skc
, base
, skc
->skc_slab_size
);
881 * Remove a slab from complete or partial list, it must be called with
882 * the 'skc->skc_lock' held but the actual free must be performed
883 * outside the lock to prevent deadlocking on vmem addresses.
886 spl_slab_free(spl_kmem_slab_t
*sks
,
887 struct list_head
*sks_list
, struct list_head
*sko_list
)
889 spl_kmem_cache_t
*skc
;
892 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
893 ASSERT(sks
->sks_ref
== 0);
895 skc
= sks
->sks_cache
;
896 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
897 ASSERT(spin_is_locked(&skc
->skc_lock
));
900 * Update slab/objects counters in the cache, then remove the
901 * slab from the skc->skc_partial_list. Finally add the slab
902 * and all its objects in to the private work lists where the
903 * destructors will be called and the memory freed to the system.
905 skc
->skc_obj_total
-= sks
->sks_objs
;
906 skc
->skc_slab_total
--;
907 list_del(&sks
->sks_list
);
908 list_add(&sks
->sks_list
, sks_list
);
909 list_splice_init(&sks
->sks_free_list
, sko_list
);
915 * Traverses all the partial slabs attached to a cache and free those
916 * which which are currently empty, and have not been touched for
917 * skc_delay seconds to avoid thrashing. The count argument is
918 * passed to optionally cap the number of slabs reclaimed, a count
919 * of zero means try and reclaim everything. When flag is set we
920 * always free an available slab regardless of age.
923 spl_slab_reclaim(spl_kmem_cache_t
*skc
, int count
, int flag
)
925 spl_kmem_slab_t
*sks
, *m
;
926 spl_kmem_obj_t
*sko
, *n
;
934 * Move empty slabs and objects which have not been touched in
935 * skc_delay seconds on to private lists to be freed outside
936 * the spin lock. This delay time is important to avoid thrashing
937 * however when flag is set the delay will not be used.
939 spin_lock(&skc
->skc_lock
);
940 list_for_each_entry_safe_reverse(sks
,m
,&skc
->skc_partial_list
,sks_list
){
942 * All empty slabs are at the end of skc->skc_partial_list,
943 * therefore once a non-empty slab is found we can stop
944 * scanning. Additionally, stop when reaching the target
945 * reclaim 'count' if a non-zero threshhold is given.
947 if ((sks
->sks_ref
> 0) || (count
&& i
> count
))
950 if (time_after(jiffies
,sks
->sks_age
+skc
->skc_delay
*HZ
)||flag
) {
951 spl_slab_free(sks
, &sks_list
, &sko_list
);
955 spin_unlock(&skc
->skc_lock
);
958 * The following two loops ensure all the object destructors are
959 * run, any offslab objects are freed, and the slabs themselves
960 * are freed. This is all done outside the skc->skc_lock since
961 * this allows the destructor to sleep, and allows us to perform
962 * a conditional reschedule when a freeing a large number of
963 * objects and slabs back to the system.
965 if (skc
->skc_flags
& KMC_OFFSLAB
)
966 size
= spl_offslab_size(skc
);
968 list_for_each_entry_safe(sko
, n
, &sko_list
, sko_list
) {
969 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
972 skc
->skc_dtor(sko
->sko_addr
, skc
->skc_private
);
974 if (skc
->skc_flags
& KMC_OFFSLAB
)
975 kv_free(skc
, sko
->sko_addr
, size
);
980 list_for_each_entry_safe(sks
, m
, &sks_list
, sks_list
) {
981 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
982 kv_free(skc
, sks
, skc
->skc_slab_size
);
990 * Called regularly on all caches to age objects out of the magazines
991 * which have not been access in skc->skc_delay seconds. This prevents
992 * idle magazines from holding memory which might be better used by
993 * other caches or parts of the system. The delay is present to
994 * prevent thrashing the magazine.
997 spl_magazine_age(void *data
)
999 spl_kmem_magazine_t
*skm
=
1000 spl_get_work_data(data
, spl_kmem_magazine_t
, skm_work
.work
);
1001 spl_kmem_cache_t
*skc
= skm
->skm_cache
;
1002 int i
= smp_processor_id();
1004 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1005 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1006 ASSERT(skc
->skc_mag
[i
] == skm
);
1008 if (skm
->skm_avail
> 0 &&
1009 time_after(jiffies
, skm
->skm_age
+ skc
->skc_delay
* HZ
))
1010 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1012 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
1013 schedule_delayed_work_on(i
, &skm
->skm_work
,
1014 skc
->skc_delay
/ 3 * HZ
);
1018 * Called regularly to keep a downward pressure on the size of idle
1019 * magazines and to release free slabs from the cache. This function
1020 * never calls the registered reclaim function, that only occures
1021 * under memory pressure or with a direct call to spl_kmem_reap().
1024 spl_cache_age(void *data
)
1026 spl_kmem_cache_t
*skc
=
1027 spl_get_work_data(data
, spl_kmem_cache_t
, skc_work
.work
);
1029 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1030 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
1032 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
1033 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
1037 * Size a slab based on the size of each aligned object plus spl_kmem_obj_t.
1038 * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However,
1039 * for very small objects we may end up with more than this so as not
1040 * to waste space in the minimal allocation of a single page. Also for
1041 * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN,
1042 * lower than this and we will fail.
1045 spl_slab_size(spl_kmem_cache_t
*skc
, uint32_t *objs
, uint32_t *size
)
1047 uint32_t sks_size
, obj_size
, max_size
;
1049 if (skc
->skc_flags
& KMC_OFFSLAB
) {
1050 *objs
= SPL_KMEM_CACHE_OBJ_PER_SLAB
;
1051 *size
= sizeof(spl_kmem_slab_t
);
1053 sks_size
= spl_sks_size(skc
);
1054 obj_size
= spl_obj_size(skc
);
1056 if (skc
->skc_flags
& KMC_KMEM
)
1057 max_size
= ((uint32_t)1 << (MAX_ORDER
-3)) * PAGE_SIZE
;
1059 max_size
= (32 * 1024 * 1024);
1061 /* Power of two sized slab */
1062 for (*size
= PAGE_SIZE
; *size
<= max_size
; *size
*= 2) {
1063 *objs
= (*size
- sks_size
) / obj_size
;
1064 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB
)
1069 * Unable to satisfy target objects per slab, fall back to
1070 * allocating a maximally sized slab and assuming it can
1071 * contain the minimum objects count use it. If not fail.
1074 *objs
= (*size
- sks_size
) / obj_size
;
1075 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN
)
1083 * Make a guess at reasonable per-cpu magazine size based on the size of
1084 * each object and the cost of caching N of them in each magazine. Long
1085 * term this should really adapt based on an observed usage heuristic.
1088 spl_magazine_size(spl_kmem_cache_t
*skc
)
1090 uint32_t obj_size
= spl_obj_size(skc
);
1094 /* Per-magazine sizes below assume a 4Kib page size */
1095 if (obj_size
> (PAGE_SIZE
* 256))
1096 size
= 4; /* Minimum 4Mib per-magazine */
1097 else if (obj_size
> (PAGE_SIZE
* 32))
1098 size
= 16; /* Minimum 2Mib per-magazine */
1099 else if (obj_size
> (PAGE_SIZE
))
1100 size
= 64; /* Minimum 256Kib per-magazine */
1101 else if (obj_size
> (PAGE_SIZE
/ 4))
1102 size
= 128; /* Minimum 128Kib per-magazine */
1110 * Allocate a per-cpu magazine to assoicate with a specific core.
1112 static spl_kmem_magazine_t
*
1113 spl_magazine_alloc(spl_kmem_cache_t
*skc
, int node
)
1115 spl_kmem_magazine_t
*skm
;
1116 int size
= sizeof(spl_kmem_magazine_t
) +
1117 sizeof(void *) * skc
->skc_mag_size
;
1120 skm
= kmem_alloc_node(size
, KM_SLEEP
, node
);
1122 skm
->skm_magic
= SKM_MAGIC
;
1124 skm
->skm_size
= skc
->skc_mag_size
;
1125 skm
->skm_refill
= skc
->skc_mag_refill
;
1126 skm
->skm_cache
= skc
;
1127 spl_init_delayed_work(&skm
->skm_work
, spl_magazine_age
, skm
);
1128 skm
->skm_age
= jiffies
;
1135 * Free a per-cpu magazine assoicated with a specific core.
1138 spl_magazine_free(spl_kmem_magazine_t
*skm
)
1140 int size
= sizeof(spl_kmem_magazine_t
) +
1141 sizeof(void *) * skm
->skm_size
;
1144 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1145 ASSERT(skm
->skm_avail
== 0);
1147 kmem_free(skm
, size
);
1152 * Create all pre-cpu magazines of reasonable sizes.
1155 spl_magazine_create(spl_kmem_cache_t
*skc
)
1160 skc
->skc_mag_size
= spl_magazine_size(skc
);
1161 skc
->skc_mag_refill
= (skc
->skc_mag_size
+ 1) / 2;
1163 for_each_online_cpu(i
) {
1164 skc
->skc_mag
[i
] = spl_magazine_alloc(skc
, cpu_to_node(i
));
1165 if (!skc
->skc_mag
[i
]) {
1166 for (i
--; i
>= 0; i
--)
1167 spl_magazine_free(skc
->skc_mag
[i
]);
1173 /* Only after everything is allocated schedule magazine work */
1174 for_each_online_cpu(i
)
1175 schedule_delayed_work_on(i
, &skc
->skc_mag
[i
]->skm_work
,
1176 skc
->skc_delay
/ 3 * HZ
);
1182 * Destroy all pre-cpu magazines.
1185 spl_magazine_destroy(spl_kmem_cache_t
*skc
)
1187 spl_kmem_magazine_t
*skm
;
1191 for_each_online_cpu(i
) {
1192 skm
= skc
->skc_mag
[i
];
1193 (void)spl_cache_flush(skc
, skm
, skm
->skm_avail
);
1194 spl_magazine_free(skm
);
1201 * Create a object cache based on the following arguments:
1203 * size cache object size
1204 * align cache object alignment
1205 * ctor cache object constructor
1206 * dtor cache object destructor
1207 * reclaim cache object reclaim
1208 * priv cache private data for ctor/dtor/reclaim
1209 * vmp unused must be NULL
1211 * KMC_NOTOUCH Disable cache object aging (unsupported)
1212 * KMC_NODEBUG Disable debugging (unsupported)
1213 * KMC_NOMAGAZINE Disable magazine (unsupported)
1214 * KMC_NOHASH Disable hashing (unsupported)
1215 * KMC_QCACHE Disable qcache (unsupported)
1216 * KMC_KMEM Force kmem backed cache
1217 * KMC_VMEM Force vmem backed cache
1218 * KMC_OFFSLAB Locate objects off the slab
1221 spl_kmem_cache_create(char *name
, size_t size
, size_t align
,
1222 spl_kmem_ctor_t ctor
,
1223 spl_kmem_dtor_t dtor
,
1224 spl_kmem_reclaim_t reclaim
,
1225 void *priv
, void *vmp
, int flags
)
1227 spl_kmem_cache_t
*skc
;
1228 int rc
, kmem_flags
= KM_SLEEP
;
1231 ASSERTF(!(flags
& KMC_NOMAGAZINE
), "Bad KMC_NOMAGAZINE (%x)\n", flags
);
1232 ASSERTF(!(flags
& KMC_NOHASH
), "Bad KMC_NOHASH (%x)\n", flags
);
1233 ASSERTF(!(flags
& KMC_QCACHE
), "Bad KMC_QCACHE (%x)\n", flags
);
1234 ASSERT(vmp
== NULL
);
1236 /* We may be called when there is a non-zero preempt_count or
1237 * interrupts are disabled is which case we must not sleep.
1239 if (current_thread_info()->preempt_count
|| irqs_disabled())
1240 kmem_flags
= KM_NOSLEEP
;
1242 /* Allocate memry for a new cache an initialize it. Unfortunately,
1243 * this usually ends up being a large allocation of ~32k because
1244 * we need to allocate enough memory for the worst case number of
1245 * cpus in the magazine, skc_mag[NR_CPUS]. Because of this we
1246 * explicitly pass __GFP_NOWARN to suppress the kmem warning */
1247 skc
= (spl_kmem_cache_t
*)kmem_zalloc(sizeof(*skc
),
1248 kmem_flags
| __GFP_NOWARN
);
1252 skc
->skc_magic
= SKC_MAGIC
;
1253 skc
->skc_name_size
= strlen(name
) + 1;
1254 skc
->skc_name
= (char *)kmem_alloc(skc
->skc_name_size
, kmem_flags
);
1255 if (skc
->skc_name
== NULL
) {
1256 kmem_free(skc
, sizeof(*skc
));
1259 strncpy(skc
->skc_name
, name
, skc
->skc_name_size
);
1261 skc
->skc_ctor
= ctor
;
1262 skc
->skc_dtor
= dtor
;
1263 skc
->skc_reclaim
= reclaim
;
1264 skc
->skc_private
= priv
;
1266 skc
->skc_flags
= flags
;
1267 skc
->skc_obj_size
= size
;
1268 skc
->skc_obj_align
= SPL_KMEM_CACHE_ALIGN
;
1269 skc
->skc_delay
= SPL_KMEM_CACHE_DELAY
;
1270 skc
->skc_reap
= SPL_KMEM_CACHE_REAP
;
1271 atomic_set(&skc
->skc_ref
, 0);
1273 INIT_LIST_HEAD(&skc
->skc_list
);
1274 INIT_LIST_HEAD(&skc
->skc_complete_list
);
1275 INIT_LIST_HEAD(&skc
->skc_partial_list
);
1276 spin_lock_init(&skc
->skc_lock
);
1277 skc
->skc_slab_fail
= 0;
1278 skc
->skc_slab_create
= 0;
1279 skc
->skc_slab_destroy
= 0;
1280 skc
->skc_slab_total
= 0;
1281 skc
->skc_slab_alloc
= 0;
1282 skc
->skc_slab_max
= 0;
1283 skc
->skc_obj_total
= 0;
1284 skc
->skc_obj_alloc
= 0;
1285 skc
->skc_obj_max
= 0;
1288 VERIFY(ISP2(align
));
1289 VERIFY3U(align
, >=, SPL_KMEM_CACHE_ALIGN
); /* Min alignment */
1290 VERIFY3U(align
, <=, PAGE_SIZE
); /* Max alignment */
1291 skc
->skc_obj_align
= align
;
1294 /* If none passed select a cache type based on object size */
1295 if (!(skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
))) {
1296 if (spl_obj_size(skc
) < (PAGE_SIZE
/ 8))
1297 skc
->skc_flags
|= KMC_KMEM
;
1299 skc
->skc_flags
|= KMC_VMEM
;
1302 rc
= spl_slab_size(skc
, &skc
->skc_slab_objs
, &skc
->skc_slab_size
);
1306 rc
= spl_magazine_create(skc
);
1310 spl_init_delayed_work(&skc
->skc_work
, spl_cache_age
, skc
);
1311 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
1313 down_write(&spl_kmem_cache_sem
);
1314 list_add_tail(&skc
->skc_list
, &spl_kmem_cache_list
);
1315 up_write(&spl_kmem_cache_sem
);
1319 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1320 kmem_free(skc
, sizeof(*skc
));
1323 EXPORT_SYMBOL(spl_kmem_cache_create
);
1326 * Destroy a cache and all objects assoicated with the cache.
1329 spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
)
1331 DECLARE_WAIT_QUEUE_HEAD(wq
);
1335 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1337 down_write(&spl_kmem_cache_sem
);
1338 list_del_init(&skc
->skc_list
);
1339 up_write(&spl_kmem_cache_sem
);
1341 /* Cancel any and wait for any pending delayed work */
1342 ASSERT(!test_and_set_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1343 cancel_delayed_work(&skc
->skc_work
);
1344 for_each_online_cpu(i
)
1345 cancel_delayed_work(&skc
->skc_mag
[i
]->skm_work
);
1347 flush_scheduled_work();
1349 /* Wait until all current callers complete, this is mainly
1350 * to catch the case where a low memory situation triggers a
1351 * cache reaping action which races with this destroy. */
1352 wait_event(wq
, atomic_read(&skc
->skc_ref
) == 0);
1354 spl_magazine_destroy(skc
);
1355 spl_slab_reclaim(skc
, 0, 1);
1356 spin_lock(&skc
->skc_lock
);
1358 /* Validate there are no objects in use and free all the
1359 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
1360 ASSERT3U(skc
->skc_slab_alloc
, ==, 0);
1361 ASSERT3U(skc
->skc_obj_alloc
, ==, 0);
1362 ASSERT3U(skc
->skc_slab_total
, ==, 0);
1363 ASSERT3U(skc
->skc_obj_total
, ==, 0);
1364 ASSERT(list_empty(&skc
->skc_complete_list
));
1366 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1367 spin_unlock(&skc
->skc_lock
);
1369 kmem_free(skc
, sizeof(*skc
));
1373 EXPORT_SYMBOL(spl_kmem_cache_destroy
);
1376 * Allocate an object from a slab attached to the cache. This is used to
1377 * repopulate the per-cpu magazine caches in batches when they run low.
1380 spl_cache_obj(spl_kmem_cache_t
*skc
, spl_kmem_slab_t
*sks
)
1382 spl_kmem_obj_t
*sko
;
1384 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1385 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1386 ASSERT(spin_is_locked(&skc
->skc_lock
));
1388 sko
= list_entry(sks
->sks_free_list
.next
, spl_kmem_obj_t
, sko_list
);
1389 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1390 ASSERT(sko
->sko_addr
!= NULL
);
1392 /* Remove from sks_free_list */
1393 list_del_init(&sko
->sko_list
);
1395 sks
->sks_age
= jiffies
;
1397 skc
->skc_obj_alloc
++;
1399 /* Track max obj usage statistics */
1400 if (skc
->skc_obj_alloc
> skc
->skc_obj_max
)
1401 skc
->skc_obj_max
= skc
->skc_obj_alloc
;
1403 /* Track max slab usage statistics */
1404 if (sks
->sks_ref
== 1) {
1405 skc
->skc_slab_alloc
++;
1407 if (skc
->skc_slab_alloc
> skc
->skc_slab_max
)
1408 skc
->skc_slab_max
= skc
->skc_slab_alloc
;
1411 return sko
->sko_addr
;
1415 * No available objects on any slabsi, create a new slab. Since this
1416 * is an expensive operation we do it without holding the spinlock and
1417 * only briefly aquire it when we link in the fully allocated and
1420 static spl_kmem_slab_t
*
1421 spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
)
1423 spl_kmem_slab_t
*sks
;
1426 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1431 * Before allocating a new slab check if the slab is being reaped.
1432 * If it is there is a good chance we can wait until it finishes
1433 * and then use one of the newly freed but not aged-out slabs.
1435 if (test_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1437 GOTO(out
, sks
= NULL
);
1440 /* Allocate a new slab for the cache */
1441 sks
= spl_slab_alloc(skc
, flags
| __GFP_NORETRY
| __GFP_NOWARN
);
1443 GOTO(out
, sks
= NULL
);
1445 /* Link the new empty slab in to the end of skc_partial_list. */
1446 spin_lock(&skc
->skc_lock
);
1447 skc
->skc_slab_total
++;
1448 skc
->skc_obj_total
+= sks
->sks_objs
;
1449 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1450 spin_unlock(&skc
->skc_lock
);
1452 local_irq_disable();
1458 * Refill a per-cpu magazine with objects from the slabs for this
1459 * cache. Ideally the magazine can be repopulated using existing
1460 * objects which have been released, however if we are unable to
1461 * locate enough free objects new slabs of objects will be created.
1464 spl_cache_refill(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flags
)
1466 spl_kmem_slab_t
*sks
;
1470 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1471 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1473 refill
= MIN(skm
->skm_refill
, skm
->skm_size
- skm
->skm_avail
);
1474 spin_lock(&skc
->skc_lock
);
1476 while (refill
> 0) {
1477 /* No slabs available we may need to grow the cache */
1478 if (list_empty(&skc
->skc_partial_list
)) {
1479 spin_unlock(&skc
->skc_lock
);
1481 sks
= spl_cache_grow(skc
, flags
);
1485 /* Rescheduled to different CPU skm is not local */
1486 if (skm
!= skc
->skc_mag
[smp_processor_id()])
1489 /* Potentially rescheduled to the same CPU but
1490 * allocations may have occured from this CPU while
1491 * we were sleeping so recalculate max refill. */
1492 refill
= MIN(refill
, skm
->skm_size
- skm
->skm_avail
);
1494 spin_lock(&skc
->skc_lock
);
1498 /* Grab the next available slab */
1499 sks
= list_entry((&skc
->skc_partial_list
)->next
,
1500 spl_kmem_slab_t
, sks_list
);
1501 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1502 ASSERT(sks
->sks_ref
< sks
->sks_objs
);
1503 ASSERT(!list_empty(&sks
->sks_free_list
));
1505 /* Consume as many objects as needed to refill the requested
1506 * cache. We must also be careful not to overfill it. */
1507 while (sks
->sks_ref
< sks
->sks_objs
&& refill
-- > 0 && ++rc
) {
1508 ASSERT(skm
->skm_avail
< skm
->skm_size
);
1509 ASSERT(rc
< skm
->skm_size
);
1510 skm
->skm_objs
[skm
->skm_avail
++]=spl_cache_obj(skc
,sks
);
1513 /* Move slab to skc_complete_list when full */
1514 if (sks
->sks_ref
== sks
->sks_objs
) {
1515 list_del(&sks
->sks_list
);
1516 list_add(&sks
->sks_list
, &skc
->skc_complete_list
);
1520 spin_unlock(&skc
->skc_lock
);
1522 /* Returns the number of entries added to cache */
1527 * Release an object back to the slab from which it came.
1530 spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
)
1532 spl_kmem_slab_t
*sks
= NULL
;
1533 spl_kmem_obj_t
*sko
= NULL
;
1536 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1537 ASSERT(spin_is_locked(&skc
->skc_lock
));
1539 sko
= spl_sko_from_obj(skc
, obj
);
1540 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1541 sks
= sko
->sko_slab
;
1542 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1543 ASSERT(sks
->sks_cache
== skc
);
1544 list_add(&sko
->sko_list
, &sks
->sks_free_list
);
1546 sks
->sks_age
= jiffies
;
1548 skc
->skc_obj_alloc
--;
1550 /* Move slab to skc_partial_list when no longer full. Slabs
1551 * are added to the head to keep the partial list is quasi-full
1552 * sorted order. Fuller at the head, emptier at the tail. */
1553 if (sks
->sks_ref
== (sks
->sks_objs
- 1)) {
1554 list_del(&sks
->sks_list
);
1555 list_add(&sks
->sks_list
, &skc
->skc_partial_list
);
1558 /* Move emply slabs to the end of the partial list so
1559 * they can be easily found and freed during reclamation. */
1560 if (sks
->sks_ref
== 0) {
1561 list_del(&sks
->sks_list
);
1562 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1563 skc
->skc_slab_alloc
--;
1570 * Release a batch of objects from a per-cpu magazine back to their
1571 * respective slabs. This occurs when we exceed the magazine size,
1572 * are under memory pressure, when the cache is idle, or during
1573 * cache cleanup. The flush argument contains the number of entries
1574 * to remove from the magazine.
1577 spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
1579 int i
, count
= MIN(flush
, skm
->skm_avail
);
1582 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1583 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1586 * XXX: Currently we simply return objects from the magazine to
1587 * the slabs in fifo order. The ideal thing to do from a memory
1588 * fragmentation standpoint is to cheaply determine the set of
1589 * objects in the magazine which will result in the largest
1590 * number of free slabs if released from the magazine.
1592 spin_lock(&skc
->skc_lock
);
1593 for (i
= 0; i
< count
; i
++)
1594 spl_cache_shrink(skc
, skm
->skm_objs
[i
]);
1596 skm
->skm_avail
-= count
;
1597 memmove(skm
->skm_objs
, &(skm
->skm_objs
[count
]),
1598 sizeof(void *) * skm
->skm_avail
);
1600 spin_unlock(&skc
->skc_lock
);
1606 * Allocate an object from the per-cpu magazine, or if the magazine
1607 * is empty directly allocate from a slab and repopulate the magazine.
1610 spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
)
1612 spl_kmem_magazine_t
*skm
;
1613 unsigned long irq_flags
;
1617 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1618 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1619 ASSERT(flags
& KM_SLEEP
);
1620 atomic_inc(&skc
->skc_ref
);
1621 local_irq_save(irq_flags
);
1624 /* Safe to update per-cpu structure without lock, but
1625 * in the restart case we must be careful to reaquire
1626 * the local magazine since this may have changed
1627 * when we need to grow the cache. */
1628 skm
= skc
->skc_mag
[smp_processor_id()];
1629 ASSERTF(skm
->skm_magic
== SKM_MAGIC
, "%x != %x: %s/%p/%p %x/%x/%x\n",
1630 skm
->skm_magic
, SKM_MAGIC
, skc
->skc_name
, skc
, skm
,
1631 skm
->skm_size
, skm
->skm_refill
, skm
->skm_avail
);
1633 if (likely(skm
->skm_avail
)) {
1634 /* Object available in CPU cache, use it */
1635 obj
= skm
->skm_objs
[--skm
->skm_avail
];
1636 skm
->skm_age
= jiffies
;
1638 /* Per-CPU cache empty, directly allocate from
1639 * the slab and refill the per-CPU cache. */
1640 (void)spl_cache_refill(skc
, skm
, flags
);
1641 GOTO(restart
, obj
= NULL
);
1644 local_irq_restore(irq_flags
);
1646 ASSERT(IS_P2ALIGNED(obj
, skc
->skc_obj_align
));
1648 /* Pre-emptively migrate object to CPU L1 cache */
1650 atomic_dec(&skc
->skc_ref
);
1654 EXPORT_SYMBOL(spl_kmem_cache_alloc
);
1657 * Free an object back to the local per-cpu magazine, there is no
1658 * guarantee that this is the same magazine the object was originally
1659 * allocated from. We may need to flush entire from the magazine
1660 * back to the slabs to make space.
1663 spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
)
1665 spl_kmem_magazine_t
*skm
;
1666 unsigned long flags
;
1669 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1670 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1671 atomic_inc(&skc
->skc_ref
);
1672 local_irq_save(flags
);
1674 /* Safe to update per-cpu structure without lock, but
1675 * no remote memory allocation tracking is being performed
1676 * it is entirely possible to allocate an object from one
1677 * CPU cache and return it to another. */
1678 skm
= skc
->skc_mag
[smp_processor_id()];
1679 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1681 /* Per-CPU cache full, flush it to make space */
1682 if (unlikely(skm
->skm_avail
>= skm
->skm_size
))
1683 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1685 /* Available space in cache, use it */
1686 skm
->skm_objs
[skm
->skm_avail
++] = obj
;
1688 local_irq_restore(flags
);
1689 atomic_dec(&skc
->skc_ref
);
1693 EXPORT_SYMBOL(spl_kmem_cache_free
);
1696 * The generic shrinker function for all caches. Under linux a shrinker
1697 * may not be tightly coupled with a slab cache. In fact linux always
1698 * systematically trys calling all registered shrinker callbacks which
1699 * report that they contain unused objects. Because of this we only
1700 * register one shrinker function in the shim layer for all slab caches.
1701 * We always attempt to shrink all caches when this generic shrinker
1702 * is called. The shrinker should return the number of free objects
1703 * in the cache when called with nr_to_scan == 0 but not attempt to
1704 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
1705 * objects should be freed, because Solaris semantics are to free
1706 * all available objects we may free more objects than requested.
1709 spl_kmem_cache_generic_shrinker(int nr_to_scan
, unsigned int gfp_mask
)
1711 spl_kmem_cache_t
*skc
;
1714 down_read(&spl_kmem_cache_sem
);
1715 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
1717 spl_kmem_cache_reap_now(skc
);
1720 * Presume everything alloc'ed in reclaimable, this ensures
1721 * we are called again with nr_to_scan > 0 so can try and
1722 * reclaim. The exact number is not important either so
1723 * we forgo taking this already highly contented lock.
1725 unused
+= skc
->skc_obj_alloc
;
1727 up_read(&spl_kmem_cache_sem
);
1729 return (unused
* sysctl_vfs_cache_pressure
) / 100;
1733 * Call the registered reclaim function for a cache. Depending on how
1734 * many and which objects are released it may simply repopulate the
1735 * local magazine which will then need to age-out. Objects which cannot
1736 * fit in the magazine we will be released back to their slabs which will
1737 * also need to age out before being release. This is all just best
1738 * effort and we do not want to thrash creating and destroying slabs.
1741 spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
)
1745 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1746 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1748 /* Prevent concurrent cache reaping when contended */
1749 if (test_and_set_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1754 atomic_inc(&skc
->skc_ref
);
1756 if (skc
->skc_reclaim
)
1757 skc
->skc_reclaim(skc
->skc_private
);
1759 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
1760 clear_bit(KMC_BIT_REAPING
, &skc
->skc_flags
);
1761 atomic_dec(&skc
->skc_ref
);
1765 EXPORT_SYMBOL(spl_kmem_cache_reap_now
);
1768 * Reap all free slabs from all registered caches.
1773 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK
, GFP_KERNEL
);
1775 EXPORT_SYMBOL(spl_kmem_reap
);
1777 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
1779 spl_sprintf_addr(kmem_debug_t
*kd
, char *str
, int len
, int min
)
1781 int size
= ((len
- 1) < kd
->kd_size
) ? (len
- 1) : kd
->kd_size
;
1784 ASSERT(str
!= NULL
&& len
>= 17);
1785 memset(str
, 0, len
);
1787 /* Check for a fully printable string, and while we are at
1788 * it place the printable characters in the passed buffer. */
1789 for (i
= 0; i
< size
; i
++) {
1790 str
[i
] = ((char *)(kd
->kd_addr
))[i
];
1791 if (isprint(str
[i
])) {
1794 /* Minimum number of printable characters found
1795 * to make it worthwhile to print this as ascii. */
1805 sprintf(str
, "%02x%02x%02x%02x%02x%02x%02x%02x",
1806 *((uint8_t *)kd
->kd_addr
),
1807 *((uint8_t *)kd
->kd_addr
+ 2),
1808 *((uint8_t *)kd
->kd_addr
+ 4),
1809 *((uint8_t *)kd
->kd_addr
+ 6),
1810 *((uint8_t *)kd
->kd_addr
+ 8),
1811 *((uint8_t *)kd
->kd_addr
+ 10),
1812 *((uint8_t *)kd
->kd_addr
+ 12),
1813 *((uint8_t *)kd
->kd_addr
+ 14));
1820 spl_kmem_init_tracking(struct list_head
*list
, spinlock_t
*lock
, int size
)
1825 spin_lock_init(lock
);
1826 INIT_LIST_HEAD(list
);
1828 for (i
= 0; i
< size
; i
++)
1829 INIT_HLIST_HEAD(&kmem_table
[i
]);
1835 spl_kmem_fini_tracking(struct list_head
*list
, spinlock_t
*lock
)
1837 unsigned long flags
;
1842 spin_lock_irqsave(lock
, flags
);
1843 if (!list_empty(list
))
1844 printk(KERN_WARNING
"%-16s %-5s %-16s %s:%s\n", "address",
1845 "size", "data", "func", "line");
1847 list_for_each_entry(kd
, list
, kd_list
)
1848 printk(KERN_WARNING
"%p %-5d %-16s %s:%d\n", kd
->kd_addr
,
1849 (int)kd
->kd_size
, spl_sprintf_addr(kd
, str
, 17, 8),
1850 kd
->kd_func
, kd
->kd_line
);
1852 spin_unlock_irqrestore(lock
, flags
);
1855 #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1856 #define spl_kmem_init_tracking(list, lock, size)
1857 #define spl_kmem_fini_tracking(list, lock)
1858 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1861 spl_kmem_init_globals(void)
1865 /* For now all zones are includes, it may be wise to restrict
1866 * this to normal and highmem zones if we see problems. */
1867 for_each_zone(zone
) {
1869 if (!populated_zone(zone
))
1872 minfree
+= min_wmark_pages(zone
);
1873 desfree
+= low_wmark_pages(zone
);
1874 lotsfree
+= high_wmark_pages(zone
);
1877 /* Solaris default values */
1878 swapfs_minfree
= MAX(2*1024*1024 >> PAGE_SHIFT
, physmem
>> 3);
1879 swapfs_reserve
= MIN(4*1024*1024 >> PAGE_SHIFT
, physmem
>> 4);
1883 * Called at module init when it is safe to use spl_kallsyms_lookup_name()
1886 spl_kmem_init_kallsyms_lookup(void)
1888 #ifndef HAVE_GET_VMALLOC_INFO
1889 get_vmalloc_info_fn
= (get_vmalloc_info_t
)
1890 spl_kallsyms_lookup_name("get_vmalloc_info");
1891 if (!get_vmalloc_info_fn
) {
1892 printk(KERN_ERR
"Error: Unknown symbol get_vmalloc_info\n");
1895 #endif /* HAVE_GET_VMALLOC_INFO */
1897 #ifdef HAVE_PGDAT_HELPERS
1898 # ifndef HAVE_FIRST_ONLINE_PGDAT
1899 first_online_pgdat_fn
= (first_online_pgdat_t
)
1900 spl_kallsyms_lookup_name("first_online_pgdat");
1901 if (!first_online_pgdat_fn
) {
1902 printk(KERN_ERR
"Error: Unknown symbol first_online_pgdat\n");
1905 # endif /* HAVE_FIRST_ONLINE_PGDAT */
1907 # ifndef HAVE_NEXT_ONLINE_PGDAT
1908 next_online_pgdat_fn
= (next_online_pgdat_t
)
1909 spl_kallsyms_lookup_name("next_online_pgdat");
1910 if (!next_online_pgdat_fn
) {
1911 printk(KERN_ERR
"Error: Unknown symbol next_online_pgdat\n");
1914 # endif /* HAVE_NEXT_ONLINE_PGDAT */
1916 # ifndef HAVE_NEXT_ZONE
1917 next_zone_fn
= (next_zone_t
)
1918 spl_kallsyms_lookup_name("next_zone");
1919 if (!next_zone_fn
) {
1920 printk(KERN_ERR
"Error: Unknown symbol next_zone\n");
1923 # endif /* HAVE_NEXT_ZONE */
1925 #else /* HAVE_PGDAT_HELPERS */
1927 # ifndef HAVE_PGDAT_LIST
1928 pgdat_list_addr
= *(struct pglist_data
**)
1929 spl_kallsyms_lookup_name("pgdat_list");
1930 if (!pgdat_list_addr
) {
1931 printk(KERN_ERR
"Error: Unknown symbol pgdat_list\n");
1934 # endif /* HAVE_PGDAT_LIST */
1935 #endif /* HAVE_PGDAT_HELPERS */
1937 #if defined(NEED_GET_ZONE_COUNTS) && !defined(HAVE_GET_ZONE_COUNTS)
1938 get_zone_counts_fn
= (get_zone_counts_t
)
1939 spl_kallsyms_lookup_name("get_zone_counts");
1940 if (!get_zone_counts_fn
) {
1941 printk(KERN_ERR
"Error: Unknown symbol get_zone_counts\n");
1944 #endif /* NEED_GET_ZONE_COUNTS && !HAVE_GET_ZONE_COUNTS */
1947 * It is now safe to initialize the global tunings which rely on
1948 * the use of the for_each_zone() macro. This macro in turns
1949 * depends on the *_pgdat symbols which are now available.
1951 spl_kmem_init_globals();
1962 init_rwsem(&spl_kmem_cache_sem
);
1963 INIT_LIST_HEAD(&spl_kmem_cache_list
);
1965 #ifdef HAVE_SET_SHRINKER
1966 spl_kmem_cache_shrinker
= set_shrinker(KMC_DEFAULT_SEEKS
,
1967 spl_kmem_cache_generic_shrinker
);
1968 if (spl_kmem_cache_shrinker
== NULL
)
1969 RETURN(rc
= -ENOMEM
);
1971 register_shrinker(&spl_kmem_cache_shrinker
);
1975 kmem_alloc_used_set(0);
1976 vmem_alloc_used_set(0);
1978 spl_kmem_init_tracking(&kmem_list
, &kmem_lock
, KMEM_TABLE_SIZE
);
1979 spl_kmem_init_tracking(&vmem_list
, &vmem_lock
, VMEM_TABLE_SIZE
);
1988 /* Display all unreclaimed memory addresses, including the
1989 * allocation size and the first few bytes of what's located
1990 * at that address to aid in debugging. Performance is not
1991 * a serious concern here since it is module unload time. */
1992 if (kmem_alloc_used_read() != 0)
1993 CWARN("kmem leaked %ld/%ld bytes\n",
1994 kmem_alloc_used_read(), kmem_alloc_max
);
1997 if (vmem_alloc_used_read() != 0)
1998 CWARN("vmem leaked %ld/%ld bytes\n",
1999 vmem_alloc_used_read(), vmem_alloc_max
);
2001 spl_kmem_fini_tracking(&kmem_list
, &kmem_lock
);
2002 spl_kmem_fini_tracking(&vmem_list
, &vmem_lock
);
2003 #endif /* DEBUG_KMEM */
2006 #ifdef HAVE_SET_SHRINKER
2007 remove_shrinker(spl_kmem_cache_shrinker
);
2009 unregister_shrinker(&spl_kmem_cache_shrinker
);