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 #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.
218 /* Shim layer memory accounting */
219 atomic64_t kmem_alloc_used
= ATOMIC64_INIT(0);
220 unsigned long long kmem_alloc_max
= 0;
221 atomic64_t vmem_alloc_used
= ATOMIC64_INIT(0);
222 unsigned long long vmem_alloc_max
= 0;
223 int kmem_warning_flag
= 1;
225 EXPORT_SYMBOL(kmem_alloc_used
);
226 EXPORT_SYMBOL(kmem_alloc_max
);
227 EXPORT_SYMBOL(vmem_alloc_used
);
228 EXPORT_SYMBOL(vmem_alloc_max
);
229 EXPORT_SYMBOL(kmem_warning_flag
);
231 /* When DEBUG_KMEM_TRACKING is enabled not only will total bytes be tracked
232 * but also the location of every alloc and free. When the SPL module is
233 * unloaded a list of all leaked addresses and where they were allocated
234 * will be dumped to the console. Enabling this feature has a significant
235 * impact on performance but it makes finding memory leaks straight forward.
237 * Not surprisingly with debugging enabled the xmem_locks are very highly
238 * contended particularly on xfree(). If we want to run with this detailed
239 * debugging enabled for anything other than debugging we need to minimize
240 * the contention by moving to a lock per xmem_table entry model.
242 # ifdef DEBUG_KMEM_TRACKING
244 # define KMEM_HASH_BITS 10
245 # define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
247 # define VMEM_HASH_BITS 10
248 # define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS)
250 typedef struct kmem_debug
{
251 struct hlist_node kd_hlist
; /* Hash node linkage */
252 struct list_head kd_list
; /* List of all allocations */
253 void *kd_addr
; /* Allocation pointer */
254 size_t kd_size
; /* Allocation size */
255 const char *kd_func
; /* Allocation function */
256 int kd_line
; /* Allocation line */
259 spinlock_t kmem_lock
;
260 struct hlist_head kmem_table
[KMEM_TABLE_SIZE
];
261 struct list_head kmem_list
;
263 spinlock_t vmem_lock
;
264 struct hlist_head vmem_table
[VMEM_TABLE_SIZE
];
265 struct list_head vmem_list
;
267 EXPORT_SYMBOL(kmem_lock
);
268 EXPORT_SYMBOL(kmem_table
);
269 EXPORT_SYMBOL(kmem_list
);
271 EXPORT_SYMBOL(vmem_lock
);
272 EXPORT_SYMBOL(vmem_table
);
273 EXPORT_SYMBOL(vmem_list
);
276 int kmem_set_warning(int flag
) { return (kmem_warning_flag
= !!flag
); }
278 int kmem_set_warning(int flag
) { return 0; }
280 EXPORT_SYMBOL(kmem_set_warning
);
283 * Slab allocation interfaces
285 * While the Linux slab implementation was inspired by the Solaris
286 * implemenation I cannot use it to emulate the Solaris APIs. I
287 * require two features which are not provided by the Linux slab.
289 * 1) Constructors AND destructors. Recent versions of the Linux
290 * kernel have removed support for destructors. This is a deal
291 * breaker for the SPL which contains particularly expensive
292 * initializers for mutex's, condition variables, etc. We also
293 * require a minimal level of cleanup for these data types unlike
294 * many Linux data type which do need to be explicitly destroyed.
296 * 2) Virtual address space backed slab. Callers of the Solaris slab
297 * expect it to work well for both small are very large allocations.
298 * Because of memory fragmentation the Linux slab which is backed
299 * by kmalloc'ed memory performs very badly when confronted with
300 * large numbers of large allocations. Basing the slab on the
301 * virtual address space removes the need for contigeous pages
302 * and greatly improve performance for large allocations.
304 * For these reasons, the SPL has its own slab implementation with
305 * the needed features. It is not as highly optimized as either the
306 * Solaris or Linux slabs, but it should get me most of what is
307 * needed until it can be optimized or obsoleted by another approach.
309 * One serious concern I do have about this method is the relatively
310 * small virtual address space on 32bit arches. This will seriously
311 * constrain the size of the slab caches and their performance.
313 * XXX: Improve the partial slab list by carefully maintaining a
314 * strict ordering of fullest to emptiest slabs based on
315 * the slab reference count. This gaurentees the when freeing
316 * slabs back to the system we need only linearly traverse the
317 * last N slabs in the list to discover all the freeable slabs.
319 * XXX: NUMA awareness for optionally allocating memory close to a
320 * particular core. This can be adventageous if you know the slab
321 * object will be short lived and primarily accessed from one core.
323 * XXX: Slab coloring may also yield performance improvements and would
324 * be desirable to implement.
327 struct list_head spl_kmem_cache_list
; /* List of caches */
328 struct rw_semaphore spl_kmem_cache_sem
; /* Cache list lock */
330 static int spl_cache_flush(spl_kmem_cache_t
*skc
,
331 spl_kmem_magazine_t
*skm
, int flush
);
333 #ifdef HAVE_SET_SHRINKER
334 static struct shrinker
*spl_kmem_cache_shrinker
;
336 static int spl_kmem_cache_generic_shrinker(int nr_to_scan
,
337 unsigned int gfp_mask
);
338 static struct shrinker spl_kmem_cache_shrinker
= {
339 .shrink
= spl_kmem_cache_generic_shrinker
,
340 .seeks
= KMC_DEFAULT_SEEKS
,
345 # ifdef DEBUG_KMEM_TRACKING
347 static kmem_debug_t
*
348 kmem_del_init(spinlock_t
*lock
, struct hlist_head
*table
, int bits
,
351 struct hlist_head
*head
;
352 struct hlist_node
*node
;
353 struct kmem_debug
*p
;
357 spin_lock_irqsave(lock
, flags
);
359 head
= &table
[hash_ptr(addr
, bits
)];
360 hlist_for_each_entry_rcu(p
, node
, head
, kd_hlist
) {
361 if (p
->kd_addr
== addr
) {
362 hlist_del_init(&p
->kd_hlist
);
363 list_del_init(&p
->kd_list
);
364 spin_unlock_irqrestore(lock
, flags
);
369 spin_unlock_irqrestore(lock
, flags
);
375 kmem_alloc_track(size_t size
, int flags
, const char *func
, int line
,
376 int node_alloc
, int node
)
380 unsigned long irq_flags
;
383 dptr
= (kmem_debug_t
*) kmalloc(sizeof(kmem_debug_t
),
384 flags
& ~__GFP_ZERO
);
387 CWARN("kmem_alloc(%ld, 0x%x) debug failed\n",
388 sizeof(kmem_debug_t
), flags
);
390 /* Marked unlikely because we should never be doing this,
391 * we tolerate to up 2 pages but a single page is best. */
392 if (unlikely((size
) > (PAGE_SIZE
* 2)) && kmem_warning_flag
)
393 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
394 (unsigned long long) size
, flags
,
395 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
397 /* We use kstrdup() below because the string pointed to by
398 * __FUNCTION__ might not be available by the time we want
399 * to print it since the module might have been unloaded. */
400 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
401 if (unlikely(dptr
->kd_func
== NULL
)) {
403 CWARN("kstrdup() failed in kmem_alloc(%llu, 0x%x) "
404 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
405 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
409 /* Use the correct allocator */
411 ASSERT(!(flags
& __GFP_ZERO
));
412 ptr
= kmalloc_node(size
, flags
, node
);
413 } else if (flags
& __GFP_ZERO
) {
414 ptr
= kzalloc(size
, flags
& ~__GFP_ZERO
);
416 ptr
= kmalloc(size
, flags
);
419 if (unlikely(ptr
== NULL
)) {
420 kfree(dptr
->kd_func
);
422 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
423 (unsigned long long) size
, flags
,
424 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
428 atomic64_add(size
, &kmem_alloc_used
);
429 if (unlikely(atomic64_read(&kmem_alloc_used
) >
432 atomic64_read(&kmem_alloc_used
);
434 INIT_HLIST_NODE(&dptr
->kd_hlist
);
435 INIT_LIST_HEAD(&dptr
->kd_list
);
438 dptr
->kd_size
= size
;
439 dptr
->kd_line
= line
;
441 spin_lock_irqsave(&kmem_lock
, irq_flags
);
442 hlist_add_head_rcu(&dptr
->kd_hlist
,
443 &kmem_table
[hash_ptr(ptr
, KMEM_HASH_BITS
)]);
444 list_add_tail(&dptr
->kd_list
, &kmem_list
);
445 spin_unlock_irqrestore(&kmem_lock
, irq_flags
);
447 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
448 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
449 ptr
, atomic64_read(&kmem_alloc_used
),
455 EXPORT_SYMBOL(kmem_alloc_track
);
458 kmem_free_track(void *ptr
, size_t size
)
463 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
464 (unsigned long long) size
);
466 dptr
= kmem_del_init(&kmem_lock
, kmem_table
, KMEM_HASH_BITS
, ptr
);
468 ASSERT(dptr
); /* Must exist in hash due to kmem_alloc() */
470 /* Size must match */
471 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
472 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
473 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
475 atomic64_sub(size
, &kmem_alloc_used
);
477 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
478 (unsigned long long) size
, atomic64_read(&kmem_alloc_used
),
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(sizeof(kmem_debug_t
), flags
);
505 CWARN("vmem_alloc(%ld, 0x%x) debug failed\n",
506 sizeof(kmem_debug_t
), flags
);
508 /* We use kstrdup() below because the string pointed to by
509 * __FUNCTION__ might not be available by the time we want
510 * to print it, since the module might have been unloaded. */
511 dptr
->kd_func
= kstrdup(func
, flags
& ~__GFP_ZERO
);
512 if (unlikely(dptr
->kd_func
== NULL
)) {
514 CWARN("kstrdup() failed in vmem_alloc(%llu, 0x%x) "
515 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
516 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
520 ptr
= __vmalloc(size
, (flags
| __GFP_HIGHMEM
) & ~__GFP_ZERO
,
523 if (unlikely(ptr
== NULL
)) {
524 kfree(dptr
->kd_func
);
526 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
527 (unsigned long long) size
, flags
,
528 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
532 if (flags
& __GFP_ZERO
)
533 memset(ptr
, 0, size
);
535 atomic64_add(size
, &vmem_alloc_used
);
536 if (unlikely(atomic64_read(&vmem_alloc_used
) >
539 atomic64_read(&vmem_alloc_used
);
541 INIT_HLIST_NODE(&dptr
->kd_hlist
);
542 INIT_LIST_HEAD(&dptr
->kd_list
);
545 dptr
->kd_size
= size
;
546 dptr
->kd_line
= line
;
548 spin_lock_irqsave(&vmem_lock
, irq_flags
);
549 hlist_add_head_rcu(&dptr
->kd_hlist
,
550 &vmem_table
[hash_ptr(ptr
, VMEM_HASH_BITS
)]);
551 list_add_tail(&dptr
->kd_list
, &vmem_list
);
552 spin_unlock_irqrestore(&vmem_lock
, irq_flags
);
554 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
555 "(%lld/%llu)\n", (unsigned long long) size
, flags
,
556 ptr
, atomic64_read(&vmem_alloc_used
),
562 EXPORT_SYMBOL(vmem_alloc_track
);
565 vmem_free_track(void *ptr
, size_t size
)
570 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
571 (unsigned long long) size
);
573 dptr
= kmem_del_init(&vmem_lock
, vmem_table
, VMEM_HASH_BITS
, ptr
);
574 ASSERT(dptr
); /* Must exist in hash due to vmem_alloc() */
576 /* Size must match */
577 ASSERTF(dptr
->kd_size
== size
, "kd_size (%llu) != size (%llu), "
578 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr
->kd_size
,
579 (unsigned long long) size
, dptr
->kd_func
, dptr
->kd_line
);
581 atomic64_sub(size
, &vmem_alloc_used
);
582 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
583 (unsigned long long) size
, atomic64_read(&vmem_alloc_used
),
586 kfree(dptr
->kd_func
);
588 memset(dptr
, 0x5a, sizeof(kmem_debug_t
));
591 memset(ptr
, 0x5a, size
);
596 EXPORT_SYMBOL(vmem_free_track
);
598 # else /* DEBUG_KMEM_TRACKING */
601 kmem_alloc_debug(size_t size
, int flags
, const char *func
, int line
,
602 int node_alloc
, int node
)
607 /* Marked unlikely because we should never be doing this,
608 * we tolerate to up 2 pages but a single page is best. */
609 if (unlikely(size
> (PAGE_SIZE
* 2)) && kmem_warning_flag
)
610 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
611 (unsigned long long) size
, flags
,
612 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
614 /* Use the correct allocator */
616 ASSERT(!(flags
& __GFP_ZERO
));
617 ptr
= kmalloc_node(size
, flags
, node
);
618 } else if (flags
& __GFP_ZERO
) {
619 ptr
= kzalloc(size
, flags
& (~__GFP_ZERO
));
621 ptr
= kmalloc(size
, flags
);
625 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
626 (unsigned long long) size
, flags
,
627 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
629 atomic64_add(size
, &kmem_alloc_used
);
630 if (unlikely(atomic64_read(&kmem_alloc_used
) > kmem_alloc_max
))
631 kmem_alloc_max
= atomic64_read(&kmem_alloc_used
);
633 CDEBUG_LIMIT(D_INFO
, "kmem_alloc(%llu, 0x%x) = %p "
634 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
635 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
639 EXPORT_SYMBOL(kmem_alloc_debug
);
642 kmem_free_debug(void *ptr
, size_t size
)
646 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
647 (unsigned long long) size
);
649 atomic64_sub(size
, &kmem_alloc_used
);
651 CDEBUG_LIMIT(D_INFO
, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
652 (unsigned long long) size
, atomic64_read(&kmem_alloc_used
),
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 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
677 if (flags
& __GFP_ZERO
)
678 memset(ptr
, 0, size
);
680 atomic64_add(size
, &vmem_alloc_used
);
682 if (unlikely(atomic64_read(&vmem_alloc_used
) > vmem_alloc_max
))
683 vmem_alloc_max
= atomic64_read(&vmem_alloc_used
);
685 CDEBUG_LIMIT(D_INFO
, "vmem_alloc(%llu, 0x%x) = %p "
686 "(%lld/%llu)\n", (unsigned long long) size
, flags
, ptr
,
687 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
692 EXPORT_SYMBOL(vmem_alloc_debug
);
695 vmem_free_debug(void *ptr
, size_t size
)
699 ASSERTF(ptr
|| size
> 0, "ptr: %p, size: %llu", ptr
,
700 (unsigned long long) size
);
702 atomic64_sub(size
, &vmem_alloc_used
);
704 CDEBUG_LIMIT(D_INFO
, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr
,
705 (unsigned long long) size
, atomic64_read(&vmem_alloc_used
),
708 memset(ptr
, 0x5a, size
);
713 EXPORT_SYMBOL(vmem_free_debug
);
715 # endif /* DEBUG_KMEM_TRACKING */
716 #endif /* DEBUG_KMEM */
719 kv_alloc(spl_kmem_cache_t
*skc
, int size
, int flags
)
723 if (skc
->skc_flags
& KMC_KMEM
) {
724 if (size
> (2 * PAGE_SIZE
)) {
725 ptr
= (void *)__get_free_pages(flags
, get_order(size
));
727 ptr
= kmem_alloc(size
, flags
);
729 ptr
= vmem_alloc(size
, flags
);
736 kv_free(spl_kmem_cache_t
*skc
, void *ptr
, int size
)
738 if (skc
->skc_flags
& KMC_KMEM
) {
739 if (size
> (2 * PAGE_SIZE
))
740 free_pages((unsigned long)ptr
, get_order(size
));
742 kmem_free(ptr
, size
);
744 vmem_free(ptr
, size
);
749 * It's important that we pack the spl_kmem_obj_t structure and the
750 * actual objects in to one large address space to minimize the number
751 * of calls to the allocator. It is far better to do a few large
752 * allocations and then subdivide it ourselves. Now which allocator
753 * we use requires balancing a few trade offs.
755 * For small objects we use kmem_alloc() because as long as you are
756 * only requesting a small number of pages (ideally just one) its cheap.
757 * However, when you start requesting multiple pages with kmem_alloc()
758 * it gets increasingly expensive since it requires contigeous pages.
759 * For this reason we shift to vmem_alloc() for slabs of large objects
760 * which removes the need for contigeous pages. We do not use
761 * vmem_alloc() in all cases because there is significant locking
762 * overhead in __get_vm_area_node(). This function takes a single
763 * global lock when aquiring an available virtual address range which
764 * serializes all vmem_alloc()'s for all slab caches. Using slightly
765 * different allocation functions for small and large objects should
766 * give us the best of both worlds.
768 * KMC_ONSLAB KMC_OFFSLAB
770 * +------------------------+ +-----------------+
771 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
772 * | skc_obj_size <-+ | | +-----------------+ | |
773 * | spl_kmem_obj_t | | | |
774 * | skc_obj_size <---+ | +-----------------+ | |
775 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
776 * | ... v | | spl_kmem_obj_t | |
777 * +------------------------+ +-----------------+ v
779 static spl_kmem_slab_t
*
780 spl_slab_alloc(spl_kmem_cache_t
*skc
, int flags
)
782 spl_kmem_slab_t
*sks
;
783 spl_kmem_obj_t
*sko
, *n
;
785 int i
, align
, size
, rc
= 0;
787 base
= kv_alloc(skc
, skc
->skc_slab_size
, flags
);
791 sks
= (spl_kmem_slab_t
*)base
;
792 sks
->sks_magic
= SKS_MAGIC
;
793 sks
->sks_objs
= skc
->skc_slab_objs
;
794 sks
->sks_age
= jiffies
;
795 sks
->sks_cache
= skc
;
796 INIT_LIST_HEAD(&sks
->sks_list
);
797 INIT_LIST_HEAD(&sks
->sks_free_list
);
800 align
= skc
->skc_obj_align
;
801 size
= P2ROUNDUP(skc
->skc_obj_size
, align
) +
802 P2ROUNDUP(sizeof(spl_kmem_obj_t
), align
);
804 for (i
= 0; i
< sks
->sks_objs
; i
++) {
805 if (skc
->skc_flags
& KMC_OFFSLAB
) {
806 obj
= kv_alloc(skc
, size
, flags
);
808 GOTO(out
, rc
= -ENOMEM
);
811 P2ROUNDUP(sizeof(spl_kmem_slab_t
), align
) +
815 sko
= obj
+ P2ROUNDUP(skc
->skc_obj_size
, align
);
817 sko
->sko_magic
= SKO_MAGIC
;
819 INIT_LIST_HEAD(&sko
->sko_list
);
820 list_add_tail(&sko
->sko_list
, &sks
->sks_free_list
);
823 list_for_each_entry(sko
, &sks
->sks_free_list
, sko_list
)
825 skc
->skc_ctor(sko
->sko_addr
, skc
->skc_private
, flags
);
828 if (skc
->skc_flags
& KMC_OFFSLAB
)
829 list_for_each_entry_safe(sko
, n
, &sks
->sks_free_list
,
831 kv_free(skc
, sko
->sko_addr
, size
);
833 kv_free(skc
, base
, skc
->skc_slab_size
);
841 * Remove a slab from complete or partial list, it must be called with
842 * the 'skc->skc_lock' held but the actual free must be performed
843 * outside the lock to prevent deadlocking on vmem addresses.
846 spl_slab_free(spl_kmem_slab_t
*sks
,
847 struct list_head
*sks_list
, struct list_head
*sko_list
)
849 spl_kmem_cache_t
*skc
;
852 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
853 ASSERT(sks
->sks_ref
== 0);
855 skc
= sks
->sks_cache
;
856 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
857 ASSERT(spin_is_locked(&skc
->skc_lock
));
860 * Update slab/objects counters in the cache, then remove the
861 * slab from the skc->skc_partial_list. Finally add the slab
862 * and all its objects in to the private work lists where the
863 * destructors will be called and the memory freed to the system.
865 skc
->skc_obj_total
-= sks
->sks_objs
;
866 skc
->skc_slab_total
--;
867 list_del(&sks
->sks_list
);
868 list_add(&sks
->sks_list
, sks_list
);
869 list_splice_init(&sks
->sks_free_list
, sko_list
);
875 * Traverses all the partial slabs attached to a cache and free those
876 * which which are currently empty, and have not been touched for
877 * skc_delay seconds to avoid thrashing. The count argument is
878 * passed to optionally cap the number of slabs reclaimed, a count
879 * of zero means try and reclaim everything. When flag is set we
880 * always free an available slab regardless of age.
883 spl_slab_reclaim(spl_kmem_cache_t
*skc
, int count
, int flag
)
885 spl_kmem_slab_t
*sks
, *m
;
886 spl_kmem_obj_t
*sko
, *n
;
893 * Move empty slabs and objects which have not been touched in
894 * skc_delay seconds on to private lists to be freed outside
895 * the spin lock. This delay time is important to avoid thrashing
896 * however when flag is set the delay will not be used.
898 spin_lock(&skc
->skc_lock
);
899 list_for_each_entry_safe_reverse(sks
,m
,&skc
->skc_partial_list
,sks_list
){
901 * All empty slabs are at the end of skc->skc_partial_list,
902 * therefore once a non-empty slab is found we can stop
903 * scanning. Additionally, stop when reaching the target
904 * reclaim 'count' if a non-zero threshhold is given.
906 if ((sks
->sks_ref
> 0) || (count
&& i
> count
))
909 if (time_after(jiffies
,sks
->sks_age
+skc
->skc_delay
*HZ
)||flag
) {
910 spl_slab_free(sks
, &sks_list
, &sko_list
);
914 spin_unlock(&skc
->skc_lock
);
917 * The following two loops ensure all the object destructors are
918 * run, any offslab objects are freed, and the slabs themselves
919 * are freed. This is all done outside the skc->skc_lock since
920 * this allows the destructor to sleep, and allows us to perform
921 * a conditional reschedule when a freeing a large number of
922 * objects and slabs back to the system.
924 if (skc
->skc_flags
& KMC_OFFSLAB
)
925 size
= P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
) +
926 P2ROUNDUP(sizeof(spl_kmem_obj_t
), skc
->skc_obj_align
);
928 list_for_each_entry_safe(sko
, n
, &sko_list
, sko_list
) {
929 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
932 skc
->skc_dtor(sko
->sko_addr
, skc
->skc_private
);
934 if (skc
->skc_flags
& KMC_OFFSLAB
)
935 kv_free(skc
, sko
->sko_addr
, size
);
940 list_for_each_entry_safe(sks
, m
, &sks_list
, sks_list
) {
941 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
942 kv_free(skc
, sks
, skc
->skc_slab_size
);
950 * Called regularly on all caches to age objects out of the magazines
951 * which have not been access in skc->skc_delay seconds. This prevents
952 * idle magazines from holding memory which might be better used by
953 * other caches or parts of the system. The delay is present to
954 * prevent thrashing the magazine.
957 spl_magazine_age(void *data
)
959 spl_kmem_magazine_t
*skm
=
960 spl_get_work_data(data
, spl_kmem_magazine_t
, skm_work
.work
);
961 spl_kmem_cache_t
*skc
= skm
->skm_cache
;
962 int i
= smp_processor_id();
964 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
965 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
966 ASSERT(skc
->skc_mag
[i
] == skm
);
968 if (skm
->skm_avail
> 0 &&
969 time_after(jiffies
, skm
->skm_age
+ skc
->skc_delay
* HZ
))
970 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
972 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
973 schedule_delayed_work_on(i
, &skm
->skm_work
,
974 skc
->skc_delay
/ 3 * HZ
);
978 * Called regularly to keep a downward pressure on the size of idle
979 * magazines and to release free slabs from the cache. This function
980 * never calls the registered reclaim function, that only occures
981 * under memory pressure or with a direct call to spl_kmem_reap().
984 spl_cache_age(void *data
)
986 spl_kmem_cache_t
*skc
=
987 spl_get_work_data(data
, spl_kmem_cache_t
, skc_work
.work
);
989 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
990 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
992 if (!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
))
993 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
997 * Size a slab based on the size of each aliged object plus spl_kmem_obj_t.
998 * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However,
999 * for very small objects we may end up with more than this so as not
1000 * to waste space in the minimal allocation of a single page. Also for
1001 * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN,
1002 * lower than this and we will fail.
1005 spl_slab_size(spl_kmem_cache_t
*skc
, uint32_t *objs
, uint32_t *size
)
1007 int sks_size
, obj_size
, max_size
, align
;
1009 if (skc
->skc_flags
& KMC_OFFSLAB
) {
1010 *objs
= SPL_KMEM_CACHE_OBJ_PER_SLAB
;
1011 *size
= sizeof(spl_kmem_slab_t
);
1013 align
= skc
->skc_obj_align
;
1014 sks_size
= P2ROUNDUP(sizeof(spl_kmem_slab_t
), align
);
1015 obj_size
= P2ROUNDUP(skc
->skc_obj_size
, align
) +
1016 P2ROUNDUP(sizeof(spl_kmem_obj_t
), align
);
1018 if (skc
->skc_flags
& KMC_KMEM
)
1019 max_size
= ((uint64_t)1 << (MAX_ORDER
-1)) * PAGE_SIZE
;
1021 max_size
= (32 * 1024 * 1024);
1023 for (*size
= PAGE_SIZE
; *size
<= max_size
; *size
+= PAGE_SIZE
) {
1024 *objs
= (*size
- sks_size
) / obj_size
;
1025 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB
)
1030 * Unable to satisfy target objets per slab, fallback to
1031 * allocating a maximally sized slab and assuming it can
1032 * contain the minimum objects count use it. If not fail.
1035 *objs
= (*size
- sks_size
) / obj_size
;
1036 if (*objs
>= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN
)
1044 * Make a guess at reasonable per-cpu magazine size based on the size of
1045 * each object and the cost of caching N of them in each magazine. Long
1046 * term this should really adapt based on an observed usage heuristic.
1049 spl_magazine_size(spl_kmem_cache_t
*skc
)
1051 int size
, align
= skc
->skc_obj_align
;
1054 /* Per-magazine sizes below assume a 4Kib page size */
1055 if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
* 256))
1056 size
= 4; /* Minimum 4Mib per-magazine */
1057 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
* 32))
1058 size
= 16; /* Minimum 2Mib per-magazine */
1059 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
))
1060 size
= 64; /* Minimum 256Kib per-magazine */
1061 else if (P2ROUNDUP(skc
->skc_obj_size
, align
) > (PAGE_SIZE
/ 4))
1062 size
= 128; /* Minimum 128Kib per-magazine */
1070 * Allocate a per-cpu magazine to assoicate with a specific core.
1072 static spl_kmem_magazine_t
*
1073 spl_magazine_alloc(spl_kmem_cache_t
*skc
, int node
)
1075 spl_kmem_magazine_t
*skm
;
1076 int size
= sizeof(spl_kmem_magazine_t
) +
1077 sizeof(void *) * skc
->skc_mag_size
;
1080 skm
= kmem_alloc_node(size
, GFP_KERNEL
| __GFP_NOFAIL
, node
);
1082 skm
->skm_magic
= SKM_MAGIC
;
1084 skm
->skm_size
= skc
->skc_mag_size
;
1085 skm
->skm_refill
= skc
->skc_mag_refill
;
1086 skm
->skm_cache
= skc
;
1087 spl_init_delayed_work(&skm
->skm_work
, spl_magazine_age
, skm
);
1088 skm
->skm_age
= jiffies
;
1095 * Free a per-cpu magazine assoicated with a specific core.
1098 spl_magazine_free(spl_kmem_magazine_t
*skm
)
1100 int size
= sizeof(spl_kmem_magazine_t
) +
1101 sizeof(void *) * skm
->skm_size
;
1104 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1105 ASSERT(skm
->skm_avail
== 0);
1107 kmem_free(skm
, size
);
1112 * Create all pre-cpu magazines of reasonable sizes.
1115 spl_magazine_create(spl_kmem_cache_t
*skc
)
1120 skc
->skc_mag_size
= spl_magazine_size(skc
);
1121 skc
->skc_mag_refill
= (skc
->skc_mag_size
+ 1) / 2;
1123 for_each_online_cpu(i
) {
1124 skc
->skc_mag
[i
] = spl_magazine_alloc(skc
, cpu_to_node(i
));
1125 if (!skc
->skc_mag
[i
]) {
1126 for (i
--; i
>= 0; i
--)
1127 spl_magazine_free(skc
->skc_mag
[i
]);
1133 /* Only after everything is allocated schedule magazine work */
1134 for_each_online_cpu(i
)
1135 schedule_delayed_work_on(i
, &skc
->skc_mag
[i
]->skm_work
,
1136 skc
->skc_delay
/ 3 * HZ
);
1142 * Destroy all pre-cpu magazines.
1145 spl_magazine_destroy(spl_kmem_cache_t
*skc
)
1147 spl_kmem_magazine_t
*skm
;
1151 for_each_online_cpu(i
) {
1152 skm
= skc
->skc_mag
[i
];
1153 (void)spl_cache_flush(skc
, skm
, skm
->skm_avail
);
1154 spl_magazine_free(skm
);
1161 * Create a object cache based on the following arguments:
1163 * size cache object size
1164 * align cache object alignment
1165 * ctor cache object constructor
1166 * dtor cache object destructor
1167 * reclaim cache object reclaim
1168 * priv cache private data for ctor/dtor/reclaim
1169 * vmp unused must be NULL
1171 * KMC_NOTOUCH Disable cache object aging (unsupported)
1172 * KMC_NODEBUG Disable debugging (unsupported)
1173 * KMC_NOMAGAZINE Disable magazine (unsupported)
1174 * KMC_NOHASH Disable hashing (unsupported)
1175 * KMC_QCACHE Disable qcache (unsupported)
1176 * KMC_KMEM Force kmem backed cache
1177 * KMC_VMEM Force vmem backed cache
1178 * KMC_OFFSLAB Locate objects off the slab
1181 spl_kmem_cache_create(char *name
, size_t size
, size_t align
,
1182 spl_kmem_ctor_t ctor
,
1183 spl_kmem_dtor_t dtor
,
1184 spl_kmem_reclaim_t reclaim
,
1185 void *priv
, void *vmp
, int flags
)
1187 spl_kmem_cache_t
*skc
;
1188 int rc
, kmem_flags
= KM_SLEEP
;
1191 ASSERTF(!(flags
& KMC_NOMAGAZINE
), "Bad KMC_NOMAGAZINE (%x)\n", flags
);
1192 ASSERTF(!(flags
& KMC_NOHASH
), "Bad KMC_NOHASH (%x)\n", flags
);
1193 ASSERTF(!(flags
& KMC_QCACHE
), "Bad KMC_QCACHE (%x)\n", flags
);
1194 ASSERT(vmp
== NULL
);
1196 /* We may be called when there is a non-zero preempt_count or
1197 * interrupts are disabled is which case we must not sleep.
1199 if (current_thread_info()->preempt_count
|| irqs_disabled())
1200 kmem_flags
= KM_NOSLEEP
;
1202 /* Allocate new cache memory and initialize. */
1203 skc
= (spl_kmem_cache_t
*)kmem_zalloc(sizeof(*skc
), kmem_flags
);
1207 skc
->skc_magic
= SKC_MAGIC
;
1208 skc
->skc_name_size
= strlen(name
) + 1;
1209 skc
->skc_name
= (char *)kmem_alloc(skc
->skc_name_size
, kmem_flags
);
1210 if (skc
->skc_name
== NULL
) {
1211 kmem_free(skc
, sizeof(*skc
));
1214 strncpy(skc
->skc_name
, name
, skc
->skc_name_size
);
1216 skc
->skc_ctor
= ctor
;
1217 skc
->skc_dtor
= dtor
;
1218 skc
->skc_reclaim
= reclaim
;
1219 skc
->skc_private
= priv
;
1221 skc
->skc_flags
= flags
;
1222 skc
->skc_obj_size
= size
;
1223 skc
->skc_obj_align
= SPL_KMEM_CACHE_ALIGN
;
1224 skc
->skc_delay
= SPL_KMEM_CACHE_DELAY
;
1225 skc
->skc_reap
= SPL_KMEM_CACHE_REAP
;
1226 atomic_set(&skc
->skc_ref
, 0);
1228 INIT_LIST_HEAD(&skc
->skc_list
);
1229 INIT_LIST_HEAD(&skc
->skc_complete_list
);
1230 INIT_LIST_HEAD(&skc
->skc_partial_list
);
1231 spin_lock_init(&skc
->skc_lock
);
1232 skc
->skc_slab_fail
= 0;
1233 skc
->skc_slab_create
= 0;
1234 skc
->skc_slab_destroy
= 0;
1235 skc
->skc_slab_total
= 0;
1236 skc
->skc_slab_alloc
= 0;
1237 skc
->skc_slab_max
= 0;
1238 skc
->skc_obj_total
= 0;
1239 skc
->skc_obj_alloc
= 0;
1240 skc
->skc_obj_max
= 0;
1243 ASSERT((align
& (align
- 1)) == 0); /* Power of two */
1244 ASSERT(align
>= SPL_KMEM_CACHE_ALIGN
); /* Minimum size */
1245 skc
->skc_obj_align
= align
;
1248 /* If none passed select a cache type based on object size */
1249 if (!(skc
->skc_flags
& (KMC_KMEM
| KMC_VMEM
))) {
1250 if (P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
) <
1252 skc
->skc_flags
|= KMC_KMEM
;
1254 skc
->skc_flags
|= KMC_VMEM
;
1258 rc
= spl_slab_size(skc
, &skc
->skc_slab_objs
, &skc
->skc_slab_size
);
1262 rc
= spl_magazine_create(skc
);
1266 spl_init_delayed_work(&skc
->skc_work
, spl_cache_age
, skc
);
1267 schedule_delayed_work(&skc
->skc_work
, skc
->skc_delay
/ 3 * HZ
);
1269 down_write(&spl_kmem_cache_sem
);
1270 list_add_tail(&skc
->skc_list
, &spl_kmem_cache_list
);
1271 up_write(&spl_kmem_cache_sem
);
1275 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1276 kmem_free(skc
, sizeof(*skc
));
1279 EXPORT_SYMBOL(spl_kmem_cache_create
);
1282 * Destroy a cache and all objects assoicated with the cache.
1285 spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
)
1287 DECLARE_WAIT_QUEUE_HEAD(wq
);
1291 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1293 down_write(&spl_kmem_cache_sem
);
1294 list_del_init(&skc
->skc_list
);
1295 up_write(&spl_kmem_cache_sem
);
1297 /* Cancel any and wait for any pending delayed work */
1298 ASSERT(!test_and_set_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1299 cancel_delayed_work(&skc
->skc_work
);
1300 for_each_online_cpu(i
)
1301 cancel_delayed_work(&skc
->skc_mag
[i
]->skm_work
);
1303 flush_scheduled_work();
1305 /* Wait until all current callers complete, this is mainly
1306 * to catch the case where a low memory situation triggers a
1307 * cache reaping action which races with this destroy. */
1308 wait_event(wq
, atomic_read(&skc
->skc_ref
) == 0);
1310 spl_magazine_destroy(skc
);
1311 spl_slab_reclaim(skc
, 0, 1);
1312 spin_lock(&skc
->skc_lock
);
1314 /* Validate there are no objects in use and free all the
1315 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
1316 ASSERT3U(skc
->skc_slab_alloc
, ==, 0);
1317 ASSERT3U(skc
->skc_obj_alloc
, ==, 0);
1318 ASSERT3U(skc
->skc_slab_total
, ==, 0);
1319 ASSERT3U(skc
->skc_obj_total
, ==, 0);
1320 ASSERT(list_empty(&skc
->skc_complete_list
));
1322 kmem_free(skc
->skc_name
, skc
->skc_name_size
);
1323 spin_unlock(&skc
->skc_lock
);
1325 kmem_free(skc
, sizeof(*skc
));
1329 EXPORT_SYMBOL(spl_kmem_cache_destroy
);
1332 * Allocate an object from a slab attached to the cache. This is used to
1333 * repopulate the per-cpu magazine caches in batches when they run low.
1336 spl_cache_obj(spl_kmem_cache_t
*skc
, spl_kmem_slab_t
*sks
)
1338 spl_kmem_obj_t
*sko
;
1340 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1341 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1342 ASSERT(spin_is_locked(&skc
->skc_lock
));
1344 sko
= list_entry(sks
->sks_free_list
.next
, spl_kmem_obj_t
, sko_list
);
1345 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1346 ASSERT(sko
->sko_addr
!= NULL
);
1348 /* Remove from sks_free_list */
1349 list_del_init(&sko
->sko_list
);
1351 sks
->sks_age
= jiffies
;
1353 skc
->skc_obj_alloc
++;
1355 /* Track max obj usage statistics */
1356 if (skc
->skc_obj_alloc
> skc
->skc_obj_max
)
1357 skc
->skc_obj_max
= skc
->skc_obj_alloc
;
1359 /* Track max slab usage statistics */
1360 if (sks
->sks_ref
== 1) {
1361 skc
->skc_slab_alloc
++;
1363 if (skc
->skc_slab_alloc
> skc
->skc_slab_max
)
1364 skc
->skc_slab_max
= skc
->skc_slab_alloc
;
1367 return sko
->sko_addr
;
1371 * No available objects on any slabsi, create a new slab. Since this
1372 * is an expensive operation we do it without holding the spinlock and
1373 * only briefly aquire it when we link in the fully allocated and
1376 static spl_kmem_slab_t
*
1377 spl_cache_grow(spl_kmem_cache_t
*skc
, int flags
)
1379 spl_kmem_slab_t
*sks
;
1382 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1387 * Before allocating a new slab check if the slab is being reaped.
1388 * If it is there is a good chance we can wait until it finishes
1389 * and then use one of the newly freed but not aged-out slabs.
1391 if (test_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1393 GOTO(out
, sks
= NULL
);
1396 /* Allocate a new slab for the cache */
1397 sks
= spl_slab_alloc(skc
, flags
| __GFP_NORETRY
| __GFP_NOWARN
);
1399 GOTO(out
, sks
= NULL
);
1401 /* Link the new empty slab in to the end of skc_partial_list. */
1402 spin_lock(&skc
->skc_lock
);
1403 skc
->skc_slab_total
++;
1404 skc
->skc_obj_total
+= sks
->sks_objs
;
1405 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1406 spin_unlock(&skc
->skc_lock
);
1408 local_irq_disable();
1414 * Refill a per-cpu magazine with objects from the slabs for this
1415 * cache. Ideally the magazine can be repopulated using existing
1416 * objects which have been released, however if we are unable to
1417 * locate enough free objects new slabs of objects will be created.
1420 spl_cache_refill(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flags
)
1422 spl_kmem_slab_t
*sks
;
1426 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1427 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1429 refill
= MIN(skm
->skm_refill
, skm
->skm_size
- skm
->skm_avail
);
1430 spin_lock(&skc
->skc_lock
);
1432 while (refill
> 0) {
1433 /* No slabs available we may need to grow the cache */
1434 if (list_empty(&skc
->skc_partial_list
)) {
1435 spin_unlock(&skc
->skc_lock
);
1437 sks
= spl_cache_grow(skc
, flags
);
1441 /* Rescheduled to different CPU skm is not local */
1442 if (skm
!= skc
->skc_mag
[smp_processor_id()])
1445 /* Potentially rescheduled to the same CPU but
1446 * allocations may have occured from this CPU while
1447 * we were sleeping so recalculate max refill. */
1448 refill
= MIN(refill
, skm
->skm_size
- skm
->skm_avail
);
1450 spin_lock(&skc
->skc_lock
);
1454 /* Grab the next available slab */
1455 sks
= list_entry((&skc
->skc_partial_list
)->next
,
1456 spl_kmem_slab_t
, sks_list
);
1457 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1458 ASSERT(sks
->sks_ref
< sks
->sks_objs
);
1459 ASSERT(!list_empty(&sks
->sks_free_list
));
1461 /* Consume as many objects as needed to refill the requested
1462 * cache. We must also be careful not to overfill it. */
1463 while (sks
->sks_ref
< sks
->sks_objs
&& refill
-- > 0 && ++rc
) {
1464 ASSERT(skm
->skm_avail
< skm
->skm_size
);
1465 ASSERT(rc
< skm
->skm_size
);
1466 skm
->skm_objs
[skm
->skm_avail
++]=spl_cache_obj(skc
,sks
);
1469 /* Move slab to skc_complete_list when full */
1470 if (sks
->sks_ref
== sks
->sks_objs
) {
1471 list_del(&sks
->sks_list
);
1472 list_add(&sks
->sks_list
, &skc
->skc_complete_list
);
1476 spin_unlock(&skc
->skc_lock
);
1478 /* Returns the number of entries added to cache */
1483 * Release an object back to the slab from which it came.
1486 spl_cache_shrink(spl_kmem_cache_t
*skc
, void *obj
)
1488 spl_kmem_slab_t
*sks
= NULL
;
1489 spl_kmem_obj_t
*sko
= NULL
;
1492 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1493 ASSERT(spin_is_locked(&skc
->skc_lock
));
1495 sko
= obj
+ P2ROUNDUP(skc
->skc_obj_size
, skc
->skc_obj_align
);
1496 ASSERT(sko
->sko_magic
== SKO_MAGIC
);
1498 sks
= sko
->sko_slab
;
1499 ASSERT(sks
->sks_magic
== SKS_MAGIC
);
1500 ASSERT(sks
->sks_cache
== skc
);
1501 list_add(&sko
->sko_list
, &sks
->sks_free_list
);
1503 sks
->sks_age
= jiffies
;
1505 skc
->skc_obj_alloc
--;
1507 /* Move slab to skc_partial_list when no longer full. Slabs
1508 * are added to the head to keep the partial list is quasi-full
1509 * sorted order. Fuller at the head, emptier at the tail. */
1510 if (sks
->sks_ref
== (sks
->sks_objs
- 1)) {
1511 list_del(&sks
->sks_list
);
1512 list_add(&sks
->sks_list
, &skc
->skc_partial_list
);
1515 /* Move emply slabs to the end of the partial list so
1516 * they can be easily found and freed during reclamation. */
1517 if (sks
->sks_ref
== 0) {
1518 list_del(&sks
->sks_list
);
1519 list_add_tail(&sks
->sks_list
, &skc
->skc_partial_list
);
1520 skc
->skc_slab_alloc
--;
1527 * Release a batch of objects from a per-cpu magazine back to their
1528 * respective slabs. This occurs when we exceed the magazine size,
1529 * are under memory pressure, when the cache is idle, or during
1530 * cache cleanup. The flush argument contains the number of entries
1531 * to remove from the magazine.
1534 spl_cache_flush(spl_kmem_cache_t
*skc
, spl_kmem_magazine_t
*skm
, int flush
)
1536 int i
, count
= MIN(flush
, skm
->skm_avail
);
1539 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1540 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1543 * XXX: Currently we simply return objects from the magazine to
1544 * the slabs in fifo order. The ideal thing to do from a memory
1545 * fragmentation standpoint is to cheaply determine the set of
1546 * objects in the magazine which will result in the largest
1547 * number of free slabs if released from the magazine.
1549 spin_lock(&skc
->skc_lock
);
1550 for (i
= 0; i
< count
; i
++)
1551 spl_cache_shrink(skc
, skm
->skm_objs
[i
]);
1553 skm
->skm_avail
-= count
;
1554 memmove(skm
->skm_objs
, &(skm
->skm_objs
[count
]),
1555 sizeof(void *) * skm
->skm_avail
);
1557 spin_unlock(&skc
->skc_lock
);
1563 * Allocate an object from the per-cpu magazine, or if the magazine
1564 * is empty directly allocate from a slab and repopulate the magazine.
1567 spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
)
1569 spl_kmem_magazine_t
*skm
;
1570 unsigned long irq_flags
;
1574 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1575 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1576 ASSERT(flags
& KM_SLEEP
);
1577 atomic_inc(&skc
->skc_ref
);
1578 local_irq_save(irq_flags
);
1581 /* Safe to update per-cpu structure without lock, but
1582 * in the restart case we must be careful to reaquire
1583 * the local magazine since this may have changed
1584 * when we need to grow the cache. */
1585 skm
= skc
->skc_mag
[smp_processor_id()];
1586 ASSERTF(skm
->skm_magic
== SKM_MAGIC
, "%x != %x: %s/%p/%p %x/%x/%x\n",
1587 skm
->skm_magic
, SKM_MAGIC
, skc
->skc_name
, skc
, skm
,
1588 skm
->skm_size
, skm
->skm_refill
, skm
->skm_avail
);
1590 if (likely(skm
->skm_avail
)) {
1591 /* Object available in CPU cache, use it */
1592 obj
= skm
->skm_objs
[--skm
->skm_avail
];
1593 skm
->skm_age
= jiffies
;
1595 /* Per-CPU cache empty, directly allocate from
1596 * the slab and refill the per-CPU cache. */
1597 (void)spl_cache_refill(skc
, skm
, flags
);
1598 GOTO(restart
, obj
= NULL
);
1601 local_irq_restore(irq_flags
);
1603 ASSERT(((unsigned long)(obj
) % skc
->skc_obj_align
) == 0);
1605 /* Pre-emptively migrate object to CPU L1 cache */
1607 atomic_dec(&skc
->skc_ref
);
1611 EXPORT_SYMBOL(spl_kmem_cache_alloc
);
1614 * Free an object back to the local per-cpu magazine, there is no
1615 * guarantee that this is the same magazine the object was originally
1616 * allocated from. We may need to flush entire from the magazine
1617 * back to the slabs to make space.
1620 spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
)
1622 spl_kmem_magazine_t
*skm
;
1623 unsigned long flags
;
1626 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1627 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1628 atomic_inc(&skc
->skc_ref
);
1629 local_irq_save(flags
);
1631 /* Safe to update per-cpu structure without lock, but
1632 * no remote memory allocation tracking is being performed
1633 * it is entirely possible to allocate an object from one
1634 * CPU cache and return it to another. */
1635 skm
= skc
->skc_mag
[smp_processor_id()];
1636 ASSERT(skm
->skm_magic
== SKM_MAGIC
);
1638 /* Per-CPU cache full, flush it to make space */
1639 if (unlikely(skm
->skm_avail
>= skm
->skm_size
))
1640 (void)spl_cache_flush(skc
, skm
, skm
->skm_refill
);
1642 /* Available space in cache, use it */
1643 skm
->skm_objs
[skm
->skm_avail
++] = obj
;
1645 local_irq_restore(flags
);
1646 atomic_dec(&skc
->skc_ref
);
1650 EXPORT_SYMBOL(spl_kmem_cache_free
);
1653 * The generic shrinker function for all caches. Under linux a shrinker
1654 * may not be tightly coupled with a slab cache. In fact linux always
1655 * systematically trys calling all registered shrinker callbacks which
1656 * report that they contain unused objects. Because of this we only
1657 * register one shrinker function in the shim layer for all slab caches.
1658 * We always attempt to shrink all caches when this generic shrinker
1659 * is called. The shrinker should return the number of free objects
1660 * in the cache when called with nr_to_scan == 0 but not attempt to
1661 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
1662 * objects should be freed, because Solaris semantics are to free
1663 * all available objects we may free more objects than requested.
1666 spl_kmem_cache_generic_shrinker(int nr_to_scan
, unsigned int gfp_mask
)
1668 spl_kmem_cache_t
*skc
;
1671 down_read(&spl_kmem_cache_sem
);
1672 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
1674 spl_kmem_cache_reap_now(skc
);
1677 * Presume everything alloc'ed in reclaimable, this ensures
1678 * we are called again with nr_to_scan > 0 so can try and
1679 * reclaim. The exact number is not important either so
1680 * we forgo taking this already highly contented lock.
1682 unused
+= skc
->skc_obj_alloc
;
1684 up_read(&spl_kmem_cache_sem
);
1686 return (unused
* sysctl_vfs_cache_pressure
) / 100;
1690 * Call the registered reclaim function for a cache. Depending on how
1691 * many and which objects are released it may simply repopulate the
1692 * local magazine which will then need to age-out. Objects which cannot
1693 * fit in the magazine we will be released back to their slabs which will
1694 * also need to age out before being release. This is all just best
1695 * effort and we do not want to thrash creating and destroying slabs.
1698 spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
)
1702 ASSERT(skc
->skc_magic
== SKC_MAGIC
);
1703 ASSERT(!test_bit(KMC_BIT_DESTROY
, &skc
->skc_flags
));
1705 /* Prevent concurrent cache reaping when contended */
1706 if (test_and_set_bit(KMC_BIT_REAPING
, &skc
->skc_flags
)) {
1711 atomic_inc(&skc
->skc_ref
);
1713 if (skc
->skc_reclaim
)
1714 skc
->skc_reclaim(skc
->skc_private
);
1716 spl_slab_reclaim(skc
, skc
->skc_reap
, 0);
1717 clear_bit(KMC_BIT_REAPING
, &skc
->skc_flags
);
1718 atomic_dec(&skc
->skc_ref
);
1722 EXPORT_SYMBOL(spl_kmem_cache_reap_now
);
1725 * Reap all free slabs from all registered caches.
1730 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK
, GFP_KERNEL
);
1732 EXPORT_SYMBOL(spl_kmem_reap
);
1734 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
1736 spl_sprintf_addr(kmem_debug_t
*kd
, char *str
, int len
, int min
)
1738 int size
= ((len
- 1) < kd
->kd_size
) ? (len
- 1) : kd
->kd_size
;
1741 ASSERT(str
!= NULL
&& len
>= 17);
1742 memset(str
, 0, len
);
1744 /* Check for a fully printable string, and while we are at
1745 * it place the printable characters in the passed buffer. */
1746 for (i
= 0; i
< size
; i
++) {
1747 str
[i
] = ((char *)(kd
->kd_addr
))[i
];
1748 if (isprint(str
[i
])) {
1751 /* Minimum number of printable characters found
1752 * to make it worthwhile to print this as ascii. */
1762 sprintf(str
, "%02x%02x%02x%02x%02x%02x%02x%02x",
1763 *((uint8_t *)kd
->kd_addr
),
1764 *((uint8_t *)kd
->kd_addr
+ 2),
1765 *((uint8_t *)kd
->kd_addr
+ 4),
1766 *((uint8_t *)kd
->kd_addr
+ 6),
1767 *((uint8_t *)kd
->kd_addr
+ 8),
1768 *((uint8_t *)kd
->kd_addr
+ 10),
1769 *((uint8_t *)kd
->kd_addr
+ 12),
1770 *((uint8_t *)kd
->kd_addr
+ 14));
1777 spl_kmem_init_tracking(struct list_head
*list
, spinlock_t
*lock
, int size
)
1782 spin_lock_init(lock
);
1783 INIT_LIST_HEAD(list
);
1785 for (i
= 0; i
< size
; i
++)
1786 INIT_HLIST_HEAD(&kmem_table
[i
]);
1792 spl_kmem_fini_tracking(struct list_head
*list
, spinlock_t
*lock
)
1794 unsigned long flags
;
1799 spin_lock_irqsave(lock
, flags
);
1800 if (!list_empty(list
))
1801 printk(KERN_WARNING
"%-16s %-5s %-16s %s:%s\n", "address",
1802 "size", "data", "func", "line");
1804 list_for_each_entry(kd
, list
, kd_list
)
1805 printk(KERN_WARNING
"%p %-5d %-16s %s:%d\n", kd
->kd_addr
,
1806 (int)kd
->kd_size
, spl_sprintf_addr(kd
, str
, 17, 8),
1807 kd
->kd_func
, kd
->kd_line
);
1809 spin_unlock_irqrestore(lock
, flags
);
1812 #else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1813 #define spl_kmem_init_tracking(list, lock, size)
1814 #define spl_kmem_fini_tracking(list, lock)
1815 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1818 spl_kmem_init_globals(void)
1822 /* For now all zones are includes, it may be wise to restrict
1823 * this to normal and highmem zones if we see problems. */
1824 for_each_zone(zone
) {
1826 if (!populated_zone(zone
))
1829 minfree
+= min_wmark_pages(zone
);
1830 desfree
+= low_wmark_pages(zone
);
1831 lotsfree
+= high_wmark_pages(zone
);
1834 /* Solaris default values */
1835 swapfs_minfree
= MAX(2*1024*1024 >> PAGE_SHIFT
, physmem
>> 3);
1836 swapfs_reserve
= MIN(4*1024*1024 >> PAGE_SHIFT
, physmem
>> 4);
1840 * Called at module init when it is safe to use spl_kallsyms_lookup_name()
1843 spl_kmem_init_kallsyms_lookup(void)
1845 #ifndef HAVE_GET_VMALLOC_INFO
1846 get_vmalloc_info_fn
= (get_vmalloc_info_t
)
1847 spl_kallsyms_lookup_name("get_vmalloc_info");
1848 if (!get_vmalloc_info_fn
) {
1849 printk(KERN_ERR
"Error: Unknown symbol get_vmalloc_info\n");
1852 #endif /* HAVE_GET_VMALLOC_INFO */
1854 #ifdef HAVE_PGDAT_HELPERS
1855 # ifndef HAVE_FIRST_ONLINE_PGDAT
1856 first_online_pgdat_fn
= (first_online_pgdat_t
)
1857 spl_kallsyms_lookup_name("first_online_pgdat");
1858 if (!first_online_pgdat_fn
) {
1859 printk(KERN_ERR
"Error: Unknown symbol first_online_pgdat\n");
1862 # endif /* HAVE_FIRST_ONLINE_PGDAT */
1864 # ifndef HAVE_NEXT_ONLINE_PGDAT
1865 next_online_pgdat_fn
= (next_online_pgdat_t
)
1866 spl_kallsyms_lookup_name("next_online_pgdat");
1867 if (!next_online_pgdat_fn
) {
1868 printk(KERN_ERR
"Error: Unknown symbol next_online_pgdat\n");
1871 # endif /* HAVE_NEXT_ONLINE_PGDAT */
1873 # ifndef HAVE_NEXT_ZONE
1874 next_zone_fn
= (next_zone_t
)
1875 spl_kallsyms_lookup_name("next_zone");
1876 if (!next_zone_fn
) {
1877 printk(KERN_ERR
"Error: Unknown symbol next_zone\n");
1880 # endif /* HAVE_NEXT_ZONE */
1882 #else /* HAVE_PGDAT_HELPERS */
1884 # ifndef HAVE_PGDAT_LIST
1885 pgdat_list_addr
= *(struct pglist_data
**)
1886 spl_kallsyms_lookup_name("pgdat_list");
1887 if (!pgdat_list_addr
) {
1888 printk(KERN_ERR
"Error: Unknown symbol pgdat_list\n");
1891 # endif /* HAVE_PGDAT_LIST */
1892 #endif /* HAVE_PGDAT_HELPERS */
1894 #if defined(NEED_GET_ZONE_COUNTS) && !defined(HAVE_GET_ZONE_COUNTS)
1895 get_zone_counts_fn
= (get_zone_counts_t
)
1896 spl_kallsyms_lookup_name("get_zone_counts");
1897 if (!get_zone_counts_fn
) {
1898 printk(KERN_ERR
"Error: Unknown symbol get_zone_counts\n");
1901 #endif /* NEED_GET_ZONE_COUNTS && !HAVE_GET_ZONE_COUNTS */
1904 * It is now safe to initialize the global tunings which rely on
1905 * the use of the for_each_zone() macro. This macro in turns
1906 * depends on the *_pgdat symbols which are now available.
1908 spl_kmem_init_globals();
1919 init_rwsem(&spl_kmem_cache_sem
);
1920 INIT_LIST_HEAD(&spl_kmem_cache_list
);
1922 #ifdef HAVE_SET_SHRINKER
1923 spl_kmem_cache_shrinker
= set_shrinker(KMC_DEFAULT_SEEKS
,
1924 spl_kmem_cache_generic_shrinker
);
1925 if (spl_kmem_cache_shrinker
== NULL
)
1926 RETURN(rc
= -ENOMEM
);
1928 register_shrinker(&spl_kmem_cache_shrinker
);
1932 atomic64_set(&kmem_alloc_used
, 0);
1933 atomic64_set(&vmem_alloc_used
, 0);
1935 spl_kmem_init_tracking(&kmem_list
, &kmem_lock
, KMEM_TABLE_SIZE
);
1936 spl_kmem_init_tracking(&vmem_list
, &vmem_lock
, VMEM_TABLE_SIZE
);
1945 /* Display all unreclaimed memory addresses, including the
1946 * allocation size and the first few bytes of what's located
1947 * at that address to aid in debugging. Performance is not
1948 * a serious concern here since it is module unload time. */
1949 if (atomic64_read(&kmem_alloc_used
) != 0)
1950 CWARN("kmem leaked %ld/%ld bytes\n",
1951 atomic64_read(&kmem_alloc_used
), kmem_alloc_max
);
1954 if (atomic64_read(&vmem_alloc_used
) != 0)
1955 CWARN("vmem leaked %ld/%ld bytes\n",
1956 atomic64_read(&vmem_alloc_used
), vmem_alloc_max
);
1958 spl_kmem_fini_tracking(&kmem_list
, &kmem_lock
);
1959 spl_kmem_fini_tracking(&vmem_list
, &vmem_lock
);
1960 #endif /* DEBUG_KMEM */
1963 #ifdef HAVE_SET_SHRINKER
1964 remove_shrinker(spl_kmem_cache_shrinker
);
1966 unregister_shrinker(&spl_kmem_cache_shrinker
);