]> git.proxmox.com Git - mirror_zfs.git/blame - module/spl/spl-kmem.c
SLES10 Fixes (part 2):
[mirror_zfs.git] / module / spl / spl-kmem.c
CommitLineData
715f6251
BB
1/*
2 * This file is part of the SPL: Solaris Porting Layer.
3 *
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
6 * Written by:
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
10 * UCRL-CODE-235197
11 *
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.
16 *
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
20 * for more details.
21 *
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.
25 */
26
f4b37741 27#include <sys/kmem.h>
f1ca4da6 28
937879f1 29#ifdef DEBUG_SUBSYSTEM
a0f6da3d 30# undef DEBUG_SUBSYSTEM
937879f1
BB
31#endif
32
33#define DEBUG_SUBSYSTEM S_KMEM
34
36b313da
BB
35/*
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.
39 */
40pgcnt_t minfree = 0;
41EXPORT_SYMBOL(minfree);
42
43/*
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.
49 */
50pgcnt_t desfree = 0;
51EXPORT_SYMBOL(desfree);
52
53/*
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.
59 */
60pgcnt_t lotsfree = 0;
61EXPORT_SYMBOL(lotsfree);
62
63/* Unused always 0 in this implementation */
64pgcnt_t needfree = 0;
65EXPORT_SYMBOL(needfree);
66
36b313da
BB
67pgcnt_t swapfs_minfree = 0;
68EXPORT_SYMBOL(swapfs_minfree);
69
70pgcnt_t swapfs_reserve = 0;
71EXPORT_SYMBOL(swapfs_reserve);
72
36b313da
BB
73vmem_t *heap_arena = NULL;
74EXPORT_SYMBOL(heap_arena);
75
76vmem_t *zio_alloc_arena = NULL;
77EXPORT_SYMBOL(zio_alloc_arena);
78
79vmem_t *zio_arena = NULL;
80EXPORT_SYMBOL(zio_arena);
81
d1ff2312 82#ifndef HAVE_GET_VMALLOC_INFO
96dded38 83get_vmalloc_info_t get_vmalloc_info_fn = SYMBOL_POISON;
d1ff2312
BB
84EXPORT_SYMBOL(get_vmalloc_info_fn);
85#endif /* HAVE_GET_VMALLOC_INFO */
86
36b313da 87#ifndef HAVE_FIRST_ONLINE_PGDAT
96dded38 88first_online_pgdat_t first_online_pgdat_fn = SYMBOL_POISON;
d1ff2312 89EXPORT_SYMBOL(first_online_pgdat_fn);
36b313da
BB
90#endif /* HAVE_FIRST_ONLINE_PGDAT */
91
92#ifndef HAVE_NEXT_ONLINE_PGDAT
96dded38 93next_online_pgdat_t next_online_pgdat_fn = SYMBOL_POISON;
d1ff2312 94EXPORT_SYMBOL(next_online_pgdat_fn);
36b313da
BB
95#endif /* HAVE_NEXT_ONLINE_PGDAT */
96
97#ifndef HAVE_NEXT_ZONE
96dded38 98next_zone_t next_zone_fn = SYMBOL_POISON;
d1ff2312 99EXPORT_SYMBOL(next_zone_fn);
36b313da
BB
100#endif /* HAVE_NEXT_ZONE */
101
e11d6c5f
BB
102#ifndef HAVE_ZONE_STAT_ITEM_FIA
103# ifndef HAVE_GET_ZONE_COUNTS
96dded38 104get_zone_counts_t get_zone_counts_fn = SYMBOL_POISON;
d1ff2312 105EXPORT_SYMBOL(get_zone_counts_fn);
96dded38 106# endif /* HAVE_GET_ZONE_COUNTS */
4ab13d3b 107
e11d6c5f
BB
108unsigned long
109spl_global_page_state(int item)
4ab13d3b
BB
110{
111 unsigned long active;
112 unsigned long inactive;
113 unsigned long free;
114
e11d6c5f
BB
115 if (item == NR_FREE_PAGES) {
116 get_zone_counts(&active, &inactive, &free);
117 return free;
118 }
119
120 if (item == NR_INACTIVE) {
121 get_zone_counts(&active, &inactive, &free);
122 return inactive;
123 }
124
125 if (item == NR_ACTIVE) {
126 get_zone_counts(&active, &inactive, &free);
127 return active;
128 }
129
96dded38 130# ifdef HAVE_GLOBAL_PAGE_STATE
e11d6c5f 131 return global_page_state((enum zone_stat_item)item);
96dded38
BB
132# else
133 return 0; /* Unsupported */
134# endif /* HAVE_GLOBAL_PAGE_STATE */
e11d6c5f
BB
135}
136EXPORT_SYMBOL(spl_global_page_state);
e11d6c5f 137#endif /* HAVE_ZONE_STAT_ITEM_FIA */
4ab13d3b 138
e11d6c5f
BB
139pgcnt_t
140spl_kmem_availrmem(void)
141{
4ab13d3b 142 /* The amount of easily available memory */
e11d6c5f
BB
143 return (spl_global_page_state(NR_FREE_PAGES) +
144 spl_global_page_state(NR_INACTIVE));
4ab13d3b
BB
145}
146EXPORT_SYMBOL(spl_kmem_availrmem);
147
148size_t
149vmem_size(vmem_t *vmp, int typemask)
150{
d1ff2312
BB
151 struct vmalloc_info vmi;
152 size_t size = 0;
153
4ab13d3b
BB
154 ASSERT(vmp == NULL);
155 ASSERT(typemask & (VMEM_ALLOC | VMEM_FREE));
156
d1ff2312
BB
157 get_vmalloc_info(&vmi);
158 if (typemask & VMEM_ALLOC)
159 size += (size_t)vmi.used;
160
161 if (typemask & VMEM_FREE)
162 size += (size_t)(VMALLOC_TOTAL - vmi.used);
163
164 return size;
4ab13d3b
BB
165}
166EXPORT_SYMBOL(vmem_size);
4ab13d3b 167
f1ca4da6 168/*
2fb9b26a
BB
169 * Memory allocation interfaces and debugging for basic kmem_*
170 * and vmem_* style memory allocation. When DEBUG_KMEM is enable
171 * all allocations will be tracked when they are allocated and
172 * freed. When the SPL module is unload a list of all leaked
173 * addresses and where they were allocated will be dumped to the
174 * console. Enabling this feature has a significant impant on
175 * performance but it makes finding memory leaks staight forward.
f1ca4da6
BB
176 */
177#ifdef DEBUG_KMEM
178/* Shim layer memory accounting */
550f1705 179atomic64_t kmem_alloc_used = ATOMIC64_INIT(0);
a0f6da3d 180unsigned long long kmem_alloc_max = 0;
550f1705 181atomic64_t vmem_alloc_used = ATOMIC64_INIT(0);
a0f6da3d 182unsigned long long vmem_alloc_max = 0;
c19c06f3 183int kmem_warning_flag = 1;
79b31f36 184
ff449ac4
BB
185EXPORT_SYMBOL(kmem_alloc_used);
186EXPORT_SYMBOL(kmem_alloc_max);
187EXPORT_SYMBOL(vmem_alloc_used);
188EXPORT_SYMBOL(vmem_alloc_max);
189EXPORT_SYMBOL(kmem_warning_flag);
190
a0f6da3d
BB
191# ifdef DEBUG_KMEM_TRACKING
192
193/* XXX - Not to surprisingly with debugging enabled the xmem_locks are very
194 * highly contended particularly on xfree(). If we want to run with this
195 * detailed debugging enabled for anything other than debugging we need to
196 * minimize the contention by moving to a lock per xmem_table entry model.
197 */
198
199# define KMEM_HASH_BITS 10
200# define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
201
202# define VMEM_HASH_BITS 10
203# define VMEM_TABLE_SIZE (1 << VMEM_HASH_BITS)
204
205typedef struct kmem_debug {
206 struct hlist_node kd_hlist; /* Hash node linkage */
207 struct list_head kd_list; /* List of all allocations */
208 void *kd_addr; /* Allocation pointer */
209 size_t kd_size; /* Allocation size */
210 const char *kd_func; /* Allocation function */
211 int kd_line; /* Allocation line */
212} kmem_debug_t;
213
d6a26c6a
BB
214spinlock_t kmem_lock;
215struct hlist_head kmem_table[KMEM_TABLE_SIZE];
216struct list_head kmem_list;
217
13cdca65
BB
218spinlock_t vmem_lock;
219struct hlist_head vmem_table[VMEM_TABLE_SIZE];
220struct list_head vmem_list;
221
d6a26c6a
BB
222EXPORT_SYMBOL(kmem_lock);
223EXPORT_SYMBOL(kmem_table);
224EXPORT_SYMBOL(kmem_list);
225
13cdca65
BB
226EXPORT_SYMBOL(vmem_lock);
227EXPORT_SYMBOL(vmem_table);
228EXPORT_SYMBOL(vmem_list);
a0f6da3d 229# endif
13cdca65 230
c19c06f3
BB
231int kmem_set_warning(int flag) { return (kmem_warning_flag = !!flag); }
232#else
233int kmem_set_warning(int flag) { return 0; }
f1ca4da6 234#endif
c19c06f3 235EXPORT_SYMBOL(kmem_set_warning);
f1ca4da6
BB
236
237/*
238 * Slab allocation interfaces
239 *
2fb9b26a
BB
240 * While the Linux slab implementation was inspired by the Solaris
241 * implemenation I cannot use it to emulate the Solaris APIs. I
242 * require two features which are not provided by the Linux slab.
243 *
244 * 1) Constructors AND destructors. Recent versions of the Linux
245 * kernel have removed support for destructors. This is a deal
246 * breaker for the SPL which contains particularly expensive
247 * initializers for mutex's, condition variables, etc. We also
a0f6da3d
BB
248 * require a minimal level of cleanup for these data types unlike
249 * many Linux data type which do need to be explicitly destroyed.
2fb9b26a 250 *
a0f6da3d 251 * 2) Virtual address space backed slab. Callers of the Solaris slab
2fb9b26a
BB
252 * expect it to work well for both small are very large allocations.
253 * Because of memory fragmentation the Linux slab which is backed
254 * by kmalloc'ed memory performs very badly when confronted with
255 * large numbers of large allocations. Basing the slab on the
256 * virtual address space removes the need for contigeous pages
257 * and greatly improve performance for large allocations.
258 *
259 * For these reasons, the SPL has its own slab implementation with
260 * the needed features. It is not as highly optimized as either the
261 * Solaris or Linux slabs, but it should get me most of what is
262 * needed until it can be optimized or obsoleted by another approach.
263 *
264 * One serious concern I do have about this method is the relatively
265 * small virtual address space on 32bit arches. This will seriously
266 * constrain the size of the slab caches and their performance.
267 *
2fb9b26a
BB
268 * XXX: Improve the partial slab list by carefully maintaining a
269 * strict ordering of fullest to emptiest slabs based on
270 * the slab reference count. This gaurentees the when freeing
271 * slabs back to the system we need only linearly traverse the
272 * last N slabs in the list to discover all the freeable slabs.
273 *
274 * XXX: NUMA awareness for optionally allocating memory close to a
275 * particular core. This can be adventageous if you know the slab
276 * object will be short lived and primarily accessed from one core.
277 *
278 * XXX: Slab coloring may also yield performance improvements and would
279 * be desirable to implement.
f1ca4da6 280 */
2fb9b26a 281
a0f6da3d
BB
282struct list_head spl_kmem_cache_list; /* List of caches */
283struct rw_semaphore spl_kmem_cache_sem; /* Cache list lock */
c30df9c8 284
4afaaefa 285static int spl_cache_flush(spl_kmem_cache_t *skc,
a0f6da3d 286 spl_kmem_magazine_t *skm, int flush);
4afaaefa 287
57d86234 288#ifdef HAVE_SET_SHRINKER
2fb9b26a 289static struct shrinker *spl_kmem_cache_shrinker;
57d86234 290#else
4afaaefa 291static int spl_kmem_cache_generic_shrinker(int nr_to_scan,
a0f6da3d 292 unsigned int gfp_mask);
2fb9b26a 293static struct shrinker spl_kmem_cache_shrinker = {
4afaaefa 294 .shrink = spl_kmem_cache_generic_shrinker,
57d86234
BB
295 .seeks = KMC_DEFAULT_SEEKS,
296};
297#endif
f1ca4da6 298
a0f6da3d
BB
299#ifdef DEBUG_KMEM
300# ifdef DEBUG_KMEM_TRACKING
301
302static kmem_debug_t *
303kmem_del_init(spinlock_t *lock, struct hlist_head *table, int bits,
304 void *addr)
305{
306 struct hlist_head *head;
307 struct hlist_node *node;
308 struct kmem_debug *p;
309 unsigned long flags;
310 ENTRY;
311
312 spin_lock_irqsave(lock, flags);
313
314 head = &table[hash_ptr(addr, bits)];
315 hlist_for_each_entry_rcu(p, node, head, kd_hlist) {
316 if (p->kd_addr == addr) {
317 hlist_del_init(&p->kd_hlist);
318 list_del_init(&p->kd_list);
319 spin_unlock_irqrestore(lock, flags);
320 return p;
321 }
322 }
323
324 spin_unlock_irqrestore(lock, flags);
325
326 RETURN(NULL);
327}
328
329void *
330kmem_alloc_track(size_t size, int flags, const char *func, int line,
331 int node_alloc, int node)
332{
333 void *ptr = NULL;
334 kmem_debug_t *dptr;
335 unsigned long irq_flags;
336 ENTRY;
337
338 dptr = (kmem_debug_t *) kmalloc(sizeof(kmem_debug_t),
339 flags & ~__GFP_ZERO);
340
341 if (dptr == NULL) {
342 CWARN("kmem_alloc(%ld, 0x%x) debug failed\n",
343 sizeof(kmem_debug_t), flags);
344 } else {
345 /* Marked unlikely because we should never be doing this,
346 * we tolerate to up 2 pages but a single page is best. */
347 if (unlikely((size) > (PAGE_SIZE * 2)) && kmem_warning_flag)
348 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
349 (unsigned long long) size, flags,
350 atomic64_read(&kmem_alloc_used), kmem_alloc_max);
351
c8e60837
BB
352 /* We use kstrdup() below because the string pointed to by
353 * __FUNCTION__ might not be available by the time we want
354 * to print it since the module might have been unloaded. */
355 dptr->kd_func = kstrdup(func, flags & ~__GFP_ZERO);
356 if (unlikely(dptr->kd_func == NULL)) {
357 kfree(dptr);
358 CWARN("kstrdup() failed in kmem_alloc(%llu, 0x%x) "
359 "(%lld/%llu)\n", (unsigned long long) size, flags,
360 atomic64_read(&kmem_alloc_used), kmem_alloc_max);
361 goto out;
362 }
363
a0f6da3d
BB
364 /* Use the correct allocator */
365 if (node_alloc) {
366 ASSERT(!(flags & __GFP_ZERO));
367 ptr = kmalloc_node(size, flags, node);
368 } else if (flags & __GFP_ZERO) {
369 ptr = kzalloc(size, flags & ~__GFP_ZERO);
370 } else {
371 ptr = kmalloc(size, flags);
372 }
373
374 if (unlikely(ptr == NULL)) {
c8e60837 375 kfree(dptr->kd_func);
a0f6da3d
BB
376 kfree(dptr);
377 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
378 (unsigned long long) size, flags,
379 atomic64_read(&kmem_alloc_used), kmem_alloc_max);
380 goto out;
381 }
382
383 atomic64_add(size, &kmem_alloc_used);
384 if (unlikely(atomic64_read(&kmem_alloc_used) >
385 kmem_alloc_max))
386 kmem_alloc_max =
387 atomic64_read(&kmem_alloc_used);
388
389 INIT_HLIST_NODE(&dptr->kd_hlist);
390 INIT_LIST_HEAD(&dptr->kd_list);
391
392 dptr->kd_addr = ptr;
393 dptr->kd_size = size;
a0f6da3d
BB
394 dptr->kd_line = line;
395
396 spin_lock_irqsave(&kmem_lock, irq_flags);
397 hlist_add_head_rcu(&dptr->kd_hlist,
398 &kmem_table[hash_ptr(ptr, KMEM_HASH_BITS)]);
399 list_add_tail(&dptr->kd_list, &kmem_list);
400 spin_unlock_irqrestore(&kmem_lock, irq_flags);
401
402 CDEBUG_LIMIT(D_INFO, "kmem_alloc(%llu, 0x%x) = %p "
403 "(%lld/%llu)\n", (unsigned long long) size, flags,
404 ptr, atomic64_read(&kmem_alloc_used),
405 kmem_alloc_max);
406 }
407out:
408 RETURN(ptr);
409}
410EXPORT_SYMBOL(kmem_alloc_track);
411
412void
413kmem_free_track(void *ptr, size_t size)
414{
415 kmem_debug_t *dptr;
416 ENTRY;
417
418 ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr,
419 (unsigned long long) size);
420
421 dptr = kmem_del_init(&kmem_lock, kmem_table, KMEM_HASH_BITS, ptr);
422
423 ASSERT(dptr); /* Must exist in hash due to kmem_alloc() */
424
425 /* Size must match */
426 ASSERTF(dptr->kd_size == size, "kd_size (%llu) != size (%llu), "
427 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr->kd_size,
428 (unsigned long long) size, dptr->kd_func, dptr->kd_line);
429
430 atomic64_sub(size, &kmem_alloc_used);
431
432 CDEBUG_LIMIT(D_INFO, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr,
433 (unsigned long long) size, atomic64_read(&kmem_alloc_used),
434 kmem_alloc_max);
435
c8e60837
BB
436 kfree(dptr->kd_func);
437
a0f6da3d
BB
438 memset(dptr, 0x5a, sizeof(kmem_debug_t));
439 kfree(dptr);
440
441 memset(ptr, 0x5a, size);
442 kfree(ptr);
443
444 EXIT;
445}
446EXPORT_SYMBOL(kmem_free_track);
447
448void *
449vmem_alloc_track(size_t size, int flags, const char *func, int line)
450{
451 void *ptr = NULL;
452 kmem_debug_t *dptr;
453 unsigned long irq_flags;
454 ENTRY;
455
456 ASSERT(flags & KM_SLEEP);
457
458 dptr = (kmem_debug_t *) kmalloc(sizeof(kmem_debug_t), flags);
459 if (dptr == NULL) {
460 CWARN("vmem_alloc(%ld, 0x%x) debug failed\n",
461 sizeof(kmem_debug_t), flags);
462 } else {
c8e60837
BB
463 /* We use kstrdup() below because the string pointed to by
464 * __FUNCTION__ might not be available by the time we want
465 * to print it, since the module might have been unloaded. */
466 dptr->kd_func = kstrdup(func, flags & ~__GFP_ZERO);
467 if (unlikely(dptr->kd_func == NULL)) {
468 kfree(dptr);
469 CWARN("kstrdup() failed in vmem_alloc(%llu, 0x%x) "
470 "(%lld/%llu)\n", (unsigned long long) size, flags,
471 atomic64_read(&vmem_alloc_used), vmem_alloc_max);
472 goto out;
473 }
474
a0f6da3d
BB
475 ptr = __vmalloc(size, (flags | __GFP_HIGHMEM) & ~__GFP_ZERO,
476 PAGE_KERNEL);
477
478 if (unlikely(ptr == NULL)) {
c8e60837 479 kfree(dptr->kd_func);
a0f6da3d
BB
480 kfree(dptr);
481 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
482 (unsigned long long) size, flags,
483 atomic64_read(&vmem_alloc_used), vmem_alloc_max);
484 goto out;
485 }
486
487 if (flags & __GFP_ZERO)
488 memset(ptr, 0, size);
489
490 atomic64_add(size, &vmem_alloc_used);
491 if (unlikely(atomic64_read(&vmem_alloc_used) >
492 vmem_alloc_max))
493 vmem_alloc_max =
494 atomic64_read(&vmem_alloc_used);
495
496 INIT_HLIST_NODE(&dptr->kd_hlist);
497 INIT_LIST_HEAD(&dptr->kd_list);
498
499 dptr->kd_addr = ptr;
500 dptr->kd_size = size;
a0f6da3d
BB
501 dptr->kd_line = line;
502
503 spin_lock_irqsave(&vmem_lock, irq_flags);
504 hlist_add_head_rcu(&dptr->kd_hlist,
505 &vmem_table[hash_ptr(ptr, VMEM_HASH_BITS)]);
506 list_add_tail(&dptr->kd_list, &vmem_list);
507 spin_unlock_irqrestore(&vmem_lock, irq_flags);
508
509 CDEBUG_LIMIT(D_INFO, "vmem_alloc(%llu, 0x%x) = %p "
510 "(%lld/%llu)\n", (unsigned long long) size, flags,
511 ptr, atomic64_read(&vmem_alloc_used),
512 vmem_alloc_max);
513 }
514out:
515 RETURN(ptr);
516}
517EXPORT_SYMBOL(vmem_alloc_track);
518
519void
520vmem_free_track(void *ptr, size_t size)
521{
522 kmem_debug_t *dptr;
523 ENTRY;
524
525 ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr,
526 (unsigned long long) size);
527
528 dptr = kmem_del_init(&vmem_lock, vmem_table, VMEM_HASH_BITS, ptr);
529 ASSERT(dptr); /* Must exist in hash due to vmem_alloc() */
530
531 /* Size must match */
532 ASSERTF(dptr->kd_size == size, "kd_size (%llu) != size (%llu), "
533 "kd_func = %s, kd_line = %d\n", (unsigned long long) dptr->kd_size,
534 (unsigned long long) size, dptr->kd_func, dptr->kd_line);
535
536 atomic64_sub(size, &vmem_alloc_used);
537 CDEBUG_LIMIT(D_INFO, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr,
538 (unsigned long long) size, atomic64_read(&vmem_alloc_used),
539 vmem_alloc_max);
540
c8e60837
BB
541 kfree(dptr->kd_func);
542
a0f6da3d
BB
543 memset(dptr, 0x5a, sizeof(kmem_debug_t));
544 kfree(dptr);
545
546 memset(ptr, 0x5a, size);
547 vfree(ptr);
548
549 EXIT;
550}
551EXPORT_SYMBOL(vmem_free_track);
552
553# else /* DEBUG_KMEM_TRACKING */
554
555void *
556kmem_alloc_debug(size_t size, int flags, const char *func, int line,
557 int node_alloc, int node)
558{
559 void *ptr;
560 ENTRY;
561
562 /* Marked unlikely because we should never be doing this,
563 * we tolerate to up 2 pages but a single page is best. */
564 if (unlikely(size > (PAGE_SIZE * 2)) && kmem_warning_flag)
565 CWARN("Large kmem_alloc(%llu, 0x%x) (%lld/%llu)\n",
566 (unsigned long long) size, flags,
567 atomic64_read(&kmem_alloc_used), kmem_alloc_max);
568
569 /* Use the correct allocator */
570 if (node_alloc) {
571 ASSERT(!(flags & __GFP_ZERO));
572 ptr = kmalloc_node(size, flags, node);
573 } else if (flags & __GFP_ZERO) {
574 ptr = kzalloc(size, flags & (~__GFP_ZERO));
575 } else {
576 ptr = kmalloc(size, flags);
577 }
578
579 if (ptr == NULL) {
580 CWARN("kmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
581 (unsigned long long) size, flags,
582 atomic64_read(&kmem_alloc_used), kmem_alloc_max);
583 } else {
584 atomic64_add(size, &kmem_alloc_used);
585 if (unlikely(atomic64_read(&kmem_alloc_used) > kmem_alloc_max))
586 kmem_alloc_max = atomic64_read(&kmem_alloc_used);
587
588 CDEBUG_LIMIT(D_INFO, "kmem_alloc(%llu, 0x%x) = %p "
589 "(%lld/%llu)\n", (unsigned long long) size, flags, ptr,
590 atomic64_read(&kmem_alloc_used), kmem_alloc_max);
591 }
592 RETURN(ptr);
593}
594EXPORT_SYMBOL(kmem_alloc_debug);
595
596void
597kmem_free_debug(void *ptr, size_t size)
598{
599 ENTRY;
600
601 ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr,
602 (unsigned long long) size);
603
604 atomic64_sub(size, &kmem_alloc_used);
605
606 CDEBUG_LIMIT(D_INFO, "kmem_free(%p, %llu) (%lld/%llu)\n", ptr,
607 (unsigned long long) size, atomic64_read(&kmem_alloc_used),
608 kmem_alloc_max);
609
610 memset(ptr, 0x5a, size);
611 kfree(ptr);
612
613 EXIT;
614}
615EXPORT_SYMBOL(kmem_free_debug);
616
617void *
618vmem_alloc_debug(size_t size, int flags, const char *func, int line)
619{
620 void *ptr;
621 ENTRY;
622
623 ASSERT(flags & KM_SLEEP);
624
625 ptr = __vmalloc(size, (flags | __GFP_HIGHMEM) & ~__GFP_ZERO,
626 PAGE_KERNEL);
627 if (ptr == NULL) {
628 CWARN("vmem_alloc(%llu, 0x%x) failed (%lld/%llu)\n",
629 (unsigned long long) size, flags,
630 atomic64_read(&vmem_alloc_used), vmem_alloc_max);
631 } else {
632 if (flags & __GFP_ZERO)
633 memset(ptr, 0, size);
634
635 atomic64_add(size, &vmem_alloc_used);
636
637 if (unlikely(atomic64_read(&vmem_alloc_used) > vmem_alloc_max))
638 vmem_alloc_max = atomic64_read(&vmem_alloc_used);
639
640 CDEBUG_LIMIT(D_INFO, "vmem_alloc(%llu, 0x%x) = %p "
641 "(%lld/%llu)\n", (unsigned long long) size, flags, ptr,
642 atomic64_read(&vmem_alloc_used), vmem_alloc_max);
643 }
644
645 RETURN(ptr);
646}
647EXPORT_SYMBOL(vmem_alloc_debug);
648
649void
650vmem_free_debug(void *ptr, size_t size)
651{
652 ENTRY;
653
654 ASSERTF(ptr || size > 0, "ptr: %p, size: %llu", ptr,
655 (unsigned long long) size);
656
657 atomic64_sub(size, &vmem_alloc_used);
658
659 CDEBUG_LIMIT(D_INFO, "vmem_free(%p, %llu) (%lld/%llu)\n", ptr,
660 (unsigned long long) size, atomic64_read(&vmem_alloc_used),
661 vmem_alloc_max);
662
663 memset(ptr, 0x5a, size);
664 vfree(ptr);
665
666 EXIT;
667}
668EXPORT_SYMBOL(vmem_free_debug);
669
670# endif /* DEBUG_KMEM_TRACKING */
671#endif /* DEBUG_KMEM */
672
a1502d76
BB
673static void *
674kv_alloc(spl_kmem_cache_t *skc, int size, int flags)
fece7c99 675{
a1502d76 676 void *ptr;
f1ca4da6 677
a1502d76
BB
678 if (skc->skc_flags & KMC_KMEM) {
679 if (size > (2 * PAGE_SIZE)) {
680 ptr = (void *)__get_free_pages(flags, get_order(size));
681 } else
682 ptr = kmem_alloc(size, flags);
683 } else {
684 ptr = vmem_alloc(size, flags);
d6a26c6a 685 }
fece7c99 686
a1502d76
BB
687 return ptr;
688}
fece7c99 689
a1502d76
BB
690static void
691kv_free(spl_kmem_cache_t *skc, void *ptr, int size)
692{
693 if (skc->skc_flags & KMC_KMEM) {
694 if (size > (2 * PAGE_SIZE))
695 free_pages((unsigned long)ptr, get_order(size));
696 else
697 kmem_free(ptr, size);
698 } else {
699 vmem_free(ptr, size);
700 }
fece7c99
BB
701}
702
ea3e6ca9
BB
703/*
704 * It's important that we pack the spl_kmem_obj_t structure and the
48e0606a
BB
705 * actual objects in to one large address space to minimize the number
706 * of calls to the allocator. It is far better to do a few large
707 * allocations and then subdivide it ourselves. Now which allocator
708 * we use requires balancing a few trade offs.
709 *
710 * For small objects we use kmem_alloc() because as long as you are
711 * only requesting a small number of pages (ideally just one) its cheap.
712 * However, when you start requesting multiple pages with kmem_alloc()
713 * it gets increasingly expensive since it requires contigeous pages.
714 * For this reason we shift to vmem_alloc() for slabs of large objects
715 * which removes the need for contigeous pages. We do not use
716 * vmem_alloc() in all cases because there is significant locking
717 * overhead in __get_vm_area_node(). This function takes a single
718 * global lock when aquiring an available virtual address range which
719 * serializes all vmem_alloc()'s for all slab caches. Using slightly
720 * different allocation functions for small and large objects should
721 * give us the best of both worlds.
722 *
723 * KMC_ONSLAB KMC_OFFSLAB
724 *
725 * +------------------------+ +-----------------+
726 * | spl_kmem_slab_t --+-+ | | spl_kmem_slab_t |---+-+
727 * | skc_obj_size <-+ | | +-----------------+ | |
728 * | spl_kmem_obj_t | | | |
729 * | skc_obj_size <---+ | +-----------------+ | |
730 * | spl_kmem_obj_t | | | skc_obj_size | <-+ |
731 * | ... v | | spl_kmem_obj_t | |
732 * +------------------------+ +-----------------+ v
733 */
fece7c99 734static spl_kmem_slab_t *
a1502d76 735spl_slab_alloc(spl_kmem_cache_t *skc, int flags)
fece7c99
BB
736{
737 spl_kmem_slab_t *sks;
a1502d76
BB
738 spl_kmem_obj_t *sko, *n;
739 void *base, *obj;
48e0606a
BB
740 int i, align, size, rc = 0;
741
a1502d76
BB
742 base = kv_alloc(skc, skc->skc_slab_size, flags);
743 if (base == NULL)
fece7c99
BB
744 RETURN(NULL);
745
a1502d76
BB
746 sks = (spl_kmem_slab_t *)base;
747 sks->sks_magic = SKS_MAGIC;
748 sks->sks_objs = skc->skc_slab_objs;
749 sks->sks_age = jiffies;
750 sks->sks_cache = skc;
751 INIT_LIST_HEAD(&sks->sks_list);
752 INIT_LIST_HEAD(&sks->sks_free_list);
753 sks->sks_ref = 0;
48e0606a
BB
754
755 align = skc->skc_obj_align;
756 size = P2ROUNDUP(skc->skc_obj_size, align) +
757 P2ROUNDUP(sizeof(spl_kmem_obj_t), align);
fece7c99
BB
758
759 for (i = 0; i < sks->sks_objs; i++) {
a1502d76
BB
760 if (skc->skc_flags & KMC_OFFSLAB) {
761 obj = kv_alloc(skc, size, flags);
762 if (!obj)
763 GOTO(out, rc = -ENOMEM);
764 } else {
48e0606a
BB
765 obj = base +
766 P2ROUNDUP(sizeof(spl_kmem_slab_t), align) +
767 (i * size);
a1502d76
BB
768 }
769
48e0606a 770 sko = obj + P2ROUNDUP(skc->skc_obj_size, align);
fece7c99
BB
771 sko->sko_addr = obj;
772 sko->sko_magic = SKO_MAGIC;
773 sko->sko_slab = sks;
774 INIT_LIST_HEAD(&sko->sko_list);
fece7c99
BB
775 list_add_tail(&sko->sko_list, &sks->sks_free_list);
776 }
777
fece7c99
BB
778 list_for_each_entry(sko, &sks->sks_free_list, sko_list)
779 if (skc->skc_ctor)
780 skc->skc_ctor(sko->sko_addr, skc->skc_private, flags);
2fb9b26a 781out:
a1502d76
BB
782 if (rc) {
783 if (skc->skc_flags & KMC_OFFSLAB)
48e0606a
BB
784 list_for_each_entry_safe(sko, n, &sks->sks_free_list,
785 sko_list)
a1502d76 786 kv_free(skc, sko->sko_addr, size);
fece7c99 787
a1502d76
BB
788 kv_free(skc, base, skc->skc_slab_size);
789 sks = NULL;
fece7c99
BB
790 }
791
a1502d76 792 RETURN(sks);
fece7c99
BB
793}
794
ea3e6ca9
BB
795/*
796 * Remove a slab from complete or partial list, it must be called with
797 * the 'skc->skc_lock' held but the actual free must be performed
798 * outside the lock to prevent deadlocking on vmem addresses.
fece7c99 799 */
f1ca4da6 800static void
ea3e6ca9
BB
801spl_slab_free(spl_kmem_slab_t *sks,
802 struct list_head *sks_list, struct list_head *sko_list)
803{
2fb9b26a 804 spl_kmem_cache_t *skc;
2fb9b26a 805 ENTRY;
57d86234 806
2fb9b26a 807 ASSERT(sks->sks_magic == SKS_MAGIC);
4afaaefa 808 ASSERT(sks->sks_ref == 0);
d6a26c6a 809
fece7c99
BB
810 skc = sks->sks_cache;
811 ASSERT(skc->skc_magic == SKC_MAGIC);
d46630e0 812 ASSERT(spin_is_locked(&skc->skc_lock));
f1ca4da6 813
1a944a7d
BB
814 /*
815 * Update slab/objects counters in the cache, then remove the
816 * slab from the skc->skc_partial_list. Finally add the slab
817 * and all its objects in to the private work lists where the
818 * destructors will be called and the memory freed to the system.
819 */
fece7c99
BB
820 skc->skc_obj_total -= sks->sks_objs;
821 skc->skc_slab_total--;
822 list_del(&sks->sks_list);
ea3e6ca9 823 list_add(&sks->sks_list, sks_list);
1a944a7d
BB
824 list_splice_init(&sks->sks_free_list, sko_list);
825
2fb9b26a
BB
826 EXIT;
827}
d6a26c6a 828
ea3e6ca9
BB
829/*
830 * Traverses all the partial slabs attached to a cache and free those
831 * which which are currently empty, and have not been touched for
37db7d8c
BB
832 * skc_delay seconds to avoid thrashing. The count argument is
833 * passed to optionally cap the number of slabs reclaimed, a count
834 * of zero means try and reclaim everything. When flag is set we
835 * always free an available slab regardless of age.
ea3e6ca9
BB
836 */
837static void
37db7d8c 838spl_slab_reclaim(spl_kmem_cache_t *skc, int count, int flag)
2fb9b26a
BB
839{
840 spl_kmem_slab_t *sks, *m;
ea3e6ca9
BB
841 spl_kmem_obj_t *sko, *n;
842 LIST_HEAD(sks_list);
843 LIST_HEAD(sko_list);
1a944a7d 844 int size = 0, i = 0;
2fb9b26a
BB
845 ENTRY;
846
2fb9b26a 847 /*
ea3e6ca9
BB
848 * Move empty slabs and objects which have not been touched in
849 * skc_delay seconds on to private lists to be freed outside
1a944a7d
BB
850 * the spin lock. This delay time is important to avoid thrashing
851 * however when flag is set the delay will not be used.
2fb9b26a 852 */
ea3e6ca9 853 spin_lock(&skc->skc_lock);
1a944a7d
BB
854 list_for_each_entry_safe_reverse(sks,m,&skc->skc_partial_list,sks_list){
855 /*
856 * All empty slabs are at the end of skc->skc_partial_list,
857 * therefore once a non-empty slab is found we can stop
858 * scanning. Additionally, stop when reaching the target
859 * reclaim 'count' if a non-zero threshhold is given.
860 */
861 if ((sks->sks_ref > 0) || (count && i > count))
37db7d8c
BB
862 break;
863
37db7d8c 864 if (time_after(jiffies,sks->sks_age+skc->skc_delay*HZ)||flag) {
ea3e6ca9 865 spl_slab_free(sks, &sks_list, &sko_list);
37db7d8c
BB
866 i++;
867 }
ea3e6ca9
BB
868 }
869 spin_unlock(&skc->skc_lock);
870
871 /*
1a944a7d
BB
872 * The following two loops ensure all the object destructors are
873 * run, any offslab objects are freed, and the slabs themselves
874 * are freed. This is all done outside the skc->skc_lock since
875 * this allows the destructor to sleep, and allows us to perform
876 * a conditional reschedule when a freeing a large number of
877 * objects and slabs back to the system.
ea3e6ca9 878 */
1a944a7d 879 if (skc->skc_flags & KMC_OFFSLAB)
ea3e6ca9
BB
880 size = P2ROUNDUP(skc->skc_obj_size, skc->skc_obj_align) +
881 P2ROUNDUP(sizeof(spl_kmem_obj_t), skc->skc_obj_align);
882
1a944a7d
BB
883 list_for_each_entry_safe(sko, n, &sko_list, sko_list) {
884 ASSERT(sko->sko_magic == SKO_MAGIC);
885
886 if (skc->skc_dtor)
887 skc->skc_dtor(sko->sko_addr, skc->skc_private);
888
889 if (skc->skc_flags & KMC_OFFSLAB)
ea3e6ca9 890 kv_free(skc, sko->sko_addr, size);
1a944a7d
BB
891
892 cond_resched();
2fb9b26a
BB
893 }
894
37db7d8c 895 list_for_each_entry_safe(sks, m, &sks_list, sks_list) {
1a944a7d 896 ASSERT(sks->sks_magic == SKS_MAGIC);
ea3e6ca9 897 kv_free(skc, sks, skc->skc_slab_size);
37db7d8c
BB
898 cond_resched();
899 }
ea3e6ca9
BB
900
901 EXIT;
f1ca4da6
BB
902}
903
ea3e6ca9
BB
904/*
905 * Called regularly on all caches to age objects out of the magazines
906 * which have not been access in skc->skc_delay seconds. This prevents
907 * idle magazines from holding memory which might be better used by
908 * other caches or parts of the system. The delay is present to
909 * prevent thrashing the magazine.
910 */
911static void
912spl_magazine_age(void *data)
f1ca4da6 913{
9b1b8e4c
BB
914 spl_kmem_magazine_t *skm =
915 spl_get_work_data(data, spl_kmem_magazine_t, skm_work.work);
916 spl_kmem_cache_t *skc = skm->skm_cache;
917 int i = smp_processor_id();
918
919 ASSERT(skm->skm_magic == SKM_MAGIC);
920 ASSERT(skc->skc_magic == SKC_MAGIC);
921 ASSERT(skc->skc_mag[i] == skm);
f1ca4da6 922
ea3e6ca9
BB
923 if (skm->skm_avail > 0 &&
924 time_after(jiffies, skm->skm_age + skc->skc_delay * HZ))
925 (void)spl_cache_flush(skc, skm, skm->skm_refill);
9b1b8e4c
BB
926
927 if (!test_bit(KMC_BIT_DESTROY, &skc->skc_flags))
928 schedule_delayed_work_on(i, &skm->skm_work,
929 skc->skc_delay / 3 * HZ);
ea3e6ca9 930}
4efd4118 931
ea3e6ca9
BB
932/*
933 * Called regularly to keep a downward pressure on the size of idle
934 * magazines and to release free slabs from the cache. This function
935 * never calls the registered reclaim function, that only occures
936 * under memory pressure or with a direct call to spl_kmem_reap().
937 */
938static void
939spl_cache_age(void *data)
940{
9b1b8e4c 941 spl_kmem_cache_t *skc =
ea3e6ca9
BB
942 spl_get_work_data(data, spl_kmem_cache_t, skc_work.work);
943
944 ASSERT(skc->skc_magic == SKC_MAGIC);
37db7d8c 945 spl_slab_reclaim(skc, skc->skc_reap, 0);
ea3e6ca9
BB
946
947 if (!test_bit(KMC_BIT_DESTROY, &skc->skc_flags))
37db7d8c 948 schedule_delayed_work(&skc->skc_work, skc->skc_delay / 3 * HZ);
2fb9b26a 949}
f1ca4da6 950
ea3e6ca9
BB
951/*
952 * Size a slab based on the size of each aliged object plus spl_kmem_obj_t.
953 * When on-slab we want to target SPL_KMEM_CACHE_OBJ_PER_SLAB. However,
954 * for very small objects we may end up with more than this so as not
955 * to waste space in the minimal allocation of a single page. Also for
956 * very large objects we may use as few as SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN,
957 * lower than this and we will fail.
958 */
48e0606a
BB
959static int
960spl_slab_size(spl_kmem_cache_t *skc, uint32_t *objs, uint32_t *size)
961{
ea3e6ca9 962 int sks_size, obj_size, max_size, align;
48e0606a
BB
963
964 if (skc->skc_flags & KMC_OFFSLAB) {
ea3e6ca9 965 *objs = SPL_KMEM_CACHE_OBJ_PER_SLAB;
48e0606a
BB
966 *size = sizeof(spl_kmem_slab_t);
967 } else {
ea3e6ca9
BB
968 align = skc->skc_obj_align;
969 sks_size = P2ROUNDUP(sizeof(spl_kmem_slab_t), align);
970 obj_size = P2ROUNDUP(skc->skc_obj_size, align) +
971 P2ROUNDUP(sizeof(spl_kmem_obj_t), align);
972
973 if (skc->skc_flags & KMC_KMEM)
974 max_size = ((uint64_t)1 << (MAX_ORDER-1)) * PAGE_SIZE;
975 else
976 max_size = (32 * 1024 * 1024);
48e0606a 977
ea3e6ca9
BB
978 for (*size = PAGE_SIZE; *size <= max_size; *size += PAGE_SIZE) {
979 *objs = (*size - sks_size) / obj_size;
980 if (*objs >= SPL_KMEM_CACHE_OBJ_PER_SLAB)
981 RETURN(0);
982 }
48e0606a 983
ea3e6ca9
BB
984 /*
985 * Unable to satisfy target objets per slab, fallback to
986 * allocating a maximally sized slab and assuming it can
987 * contain the minimum objects count use it. If not fail.
988 */
989 *size = max_size;
990 *objs = (*size - sks_size) / obj_size;
991 if (*objs >= SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN)
992 RETURN(0);
48e0606a
BB
993 }
994
ea3e6ca9 995 RETURN(-ENOSPC);
48e0606a
BB
996}
997
ea3e6ca9
BB
998/*
999 * Make a guess at reasonable per-cpu magazine size based on the size of
1000 * each object and the cost of caching N of them in each magazine. Long
1001 * term this should really adapt based on an observed usage heuristic.
1002 */
4afaaefa
BB
1003static int
1004spl_magazine_size(spl_kmem_cache_t *skc)
1005{
48e0606a 1006 int size, align = skc->skc_obj_align;
4afaaefa
BB
1007 ENTRY;
1008
ea3e6ca9 1009 /* Per-magazine sizes below assume a 4Kib page size */
48e0606a 1010 if (P2ROUNDUP(skc->skc_obj_size, align) > (PAGE_SIZE * 256))
ea3e6ca9 1011 size = 4; /* Minimum 4Mib per-magazine */
48e0606a 1012 else if (P2ROUNDUP(skc->skc_obj_size, align) > (PAGE_SIZE * 32))
ea3e6ca9 1013 size = 16; /* Minimum 2Mib per-magazine */
48e0606a 1014 else if (P2ROUNDUP(skc->skc_obj_size, align) > (PAGE_SIZE))
ea3e6ca9 1015 size = 64; /* Minimum 256Kib per-magazine */
48e0606a 1016 else if (P2ROUNDUP(skc->skc_obj_size, align) > (PAGE_SIZE / 4))
ea3e6ca9 1017 size = 128; /* Minimum 128Kib per-magazine */
4afaaefa 1018 else
ea3e6ca9 1019 size = 256;
4afaaefa
BB
1020
1021 RETURN(size);
1022}
1023
ea3e6ca9
BB
1024/*
1025 * Allocate a per-cpu magazine to assoicate with a specific core.
1026 */
4afaaefa
BB
1027static spl_kmem_magazine_t *
1028spl_magazine_alloc(spl_kmem_cache_t *skc, int node)
1029{
1030 spl_kmem_magazine_t *skm;
1031 int size = sizeof(spl_kmem_magazine_t) +
1032 sizeof(void *) * skc->skc_mag_size;
1033 ENTRY;
1034
ea3e6ca9 1035 skm = kmem_alloc_node(size, GFP_KERNEL | __GFP_NOFAIL, node);
4afaaefa
BB
1036 if (skm) {
1037 skm->skm_magic = SKM_MAGIC;
1038 skm->skm_avail = 0;
1039 skm->skm_size = skc->skc_mag_size;
1040 skm->skm_refill = skc->skc_mag_refill;
9b1b8e4c
BB
1041 skm->skm_cache = skc;
1042 spl_init_delayed_work(&skm->skm_work, spl_magazine_age, skm);
ea3e6ca9 1043 skm->skm_age = jiffies;
4afaaefa
BB
1044 }
1045
1046 RETURN(skm);
1047}
1048
ea3e6ca9
BB
1049/*
1050 * Free a per-cpu magazine assoicated with a specific core.
1051 */
4afaaefa
BB
1052static void
1053spl_magazine_free(spl_kmem_magazine_t *skm)
1054{
a0f6da3d
BB
1055 int size = sizeof(spl_kmem_magazine_t) +
1056 sizeof(void *) * skm->skm_size;
1057
4afaaefa
BB
1058 ENTRY;
1059 ASSERT(skm->skm_magic == SKM_MAGIC);
1060 ASSERT(skm->skm_avail == 0);
a0f6da3d
BB
1061
1062 kmem_free(skm, size);
4afaaefa
BB
1063 EXIT;
1064}
1065
ea3e6ca9
BB
1066/*
1067 * Create all pre-cpu magazines of reasonable sizes.
1068 */
4afaaefa
BB
1069static int
1070spl_magazine_create(spl_kmem_cache_t *skc)
1071{
37db7d8c 1072 int i;
4afaaefa
BB
1073 ENTRY;
1074
1075 skc->skc_mag_size = spl_magazine_size(skc);
ea3e6ca9 1076 skc->skc_mag_refill = (skc->skc_mag_size + 1) / 2;
4afaaefa 1077
37db7d8c
BB
1078 for_each_online_cpu(i) {
1079 skc->skc_mag[i] = spl_magazine_alloc(skc, cpu_to_node(i));
1080 if (!skc->skc_mag[i]) {
1081 for (i--; i >= 0; i--)
1082 spl_magazine_free(skc->skc_mag[i]);
4afaaefa 1083
37db7d8c
BB
1084 RETURN(-ENOMEM);
1085 }
1086 }
4afaaefa 1087
9b1b8e4c
BB
1088 /* Only after everything is allocated schedule magazine work */
1089 for_each_online_cpu(i)
1090 schedule_delayed_work_on(i, &skc->skc_mag[i]->skm_work,
1091 skc->skc_delay / 3 * HZ);
1092
37db7d8c 1093 RETURN(0);
4afaaefa
BB
1094}
1095
ea3e6ca9
BB
1096/*
1097 * Destroy all pre-cpu magazines.
1098 */
4afaaefa
BB
1099static void
1100spl_magazine_destroy(spl_kmem_cache_t *skc)
1101{
37db7d8c
BB
1102 spl_kmem_magazine_t *skm;
1103 int i;
4afaaefa 1104 ENTRY;
37db7d8c
BB
1105
1106 for_each_online_cpu(i) {
1107 skm = skc->skc_mag[i];
1108 (void)spl_cache_flush(skc, skm, skm->skm_avail);
1109 spl_magazine_free(skm);
1110 }
1111
4afaaefa
BB
1112 EXIT;
1113}
1114
ea3e6ca9
BB
1115/*
1116 * Create a object cache based on the following arguments:
1117 * name cache name
1118 * size cache object size
1119 * align cache object alignment
1120 * ctor cache object constructor
1121 * dtor cache object destructor
1122 * reclaim cache object reclaim
1123 * priv cache private data for ctor/dtor/reclaim
1124 * vmp unused must be NULL
1125 * flags
1126 * KMC_NOTOUCH Disable cache object aging (unsupported)
1127 * KMC_NODEBUG Disable debugging (unsupported)
1128 * KMC_NOMAGAZINE Disable magazine (unsupported)
1129 * KMC_NOHASH Disable hashing (unsupported)
1130 * KMC_QCACHE Disable qcache (unsupported)
1131 * KMC_KMEM Force kmem backed cache
1132 * KMC_VMEM Force vmem backed cache
1133 * KMC_OFFSLAB Locate objects off the slab
1134 */
2fb9b26a
BB
1135spl_kmem_cache_t *
1136spl_kmem_cache_create(char *name, size_t size, size_t align,
1137 spl_kmem_ctor_t ctor,
1138 spl_kmem_dtor_t dtor,
1139 spl_kmem_reclaim_t reclaim,
1140 void *priv, void *vmp, int flags)
1141{
1142 spl_kmem_cache_t *skc;
a1502d76 1143 int rc, kmem_flags = KM_SLEEP;
2fb9b26a 1144 ENTRY;
937879f1 1145
a1502d76
BB
1146 ASSERTF(!(flags & KMC_NOMAGAZINE), "Bad KMC_NOMAGAZINE (%x)\n", flags);
1147 ASSERTF(!(flags & KMC_NOHASH), "Bad KMC_NOHASH (%x)\n", flags);
1148 ASSERTF(!(flags & KMC_QCACHE), "Bad KMC_QCACHE (%x)\n", flags);
48e0606a 1149 ASSERT(vmp == NULL);
a1502d76 1150
2fb9b26a
BB
1151 /* We may be called when there is a non-zero preempt_count or
1152 * interrupts are disabled is which case we must not sleep.
1153 */
e9d7a2be 1154 if (current_thread_info()->preempt_count || irqs_disabled())
2fb9b26a 1155 kmem_flags = KM_NOSLEEP;
0a6fd143 1156
2fb9b26a 1157 /* Allocate new cache memory and initialize. */
ff449ac4 1158 skc = (spl_kmem_cache_t *)kmem_zalloc(sizeof(*skc), kmem_flags);
e9d7a2be 1159 if (skc == NULL)
2fb9b26a 1160 RETURN(NULL);
d61e12af 1161
2fb9b26a 1162 skc->skc_magic = SKC_MAGIC;
2fb9b26a
BB
1163 skc->skc_name_size = strlen(name) + 1;
1164 skc->skc_name = (char *)kmem_alloc(skc->skc_name_size, kmem_flags);
1165 if (skc->skc_name == NULL) {
1166 kmem_free(skc, sizeof(*skc));
1167 RETURN(NULL);
1168 }
1169 strncpy(skc->skc_name, name, skc->skc_name_size);
1170
e9d7a2be
BB
1171 skc->skc_ctor = ctor;
1172 skc->skc_dtor = dtor;
1173 skc->skc_reclaim = reclaim;
2fb9b26a
BB
1174 skc->skc_private = priv;
1175 skc->skc_vmp = vmp;
1176 skc->skc_flags = flags;
1177 skc->skc_obj_size = size;
48e0606a 1178 skc->skc_obj_align = SPL_KMEM_CACHE_ALIGN;
2fb9b26a 1179 skc->skc_delay = SPL_KMEM_CACHE_DELAY;
37db7d8c 1180 skc->skc_reap = SPL_KMEM_CACHE_REAP;
ea3e6ca9 1181 atomic_set(&skc->skc_ref, 0);
2fb9b26a 1182
2fb9b26a
BB
1183 INIT_LIST_HEAD(&skc->skc_list);
1184 INIT_LIST_HEAD(&skc->skc_complete_list);
1185 INIT_LIST_HEAD(&skc->skc_partial_list);
d46630e0 1186 spin_lock_init(&skc->skc_lock);
e9d7a2be
BB
1187 skc->skc_slab_fail = 0;
1188 skc->skc_slab_create = 0;
1189 skc->skc_slab_destroy = 0;
2fb9b26a
BB
1190 skc->skc_slab_total = 0;
1191 skc->skc_slab_alloc = 0;
1192 skc->skc_slab_max = 0;
1193 skc->skc_obj_total = 0;
1194 skc->skc_obj_alloc = 0;
1195 skc->skc_obj_max = 0;
a1502d76 1196
48e0606a
BB
1197 if (align) {
1198 ASSERT((align & (align - 1)) == 0); /* Power of two */
1199 ASSERT(align >= SPL_KMEM_CACHE_ALIGN); /* Minimum size */
1200 skc->skc_obj_align = align;
1201 }
1202
a1502d76
BB
1203 /* If none passed select a cache type based on object size */
1204 if (!(skc->skc_flags & (KMC_KMEM | KMC_VMEM))) {
48e0606a
BB
1205 if (P2ROUNDUP(skc->skc_obj_size, skc->skc_obj_align) <
1206 (PAGE_SIZE / 8)) {
a1502d76
BB
1207 skc->skc_flags |= KMC_KMEM;
1208 } else {
1209 skc->skc_flags |= KMC_VMEM;
1210 }
1211 }
1212
48e0606a
BB
1213 rc = spl_slab_size(skc, &skc->skc_slab_objs, &skc->skc_slab_size);
1214 if (rc)
1215 GOTO(out, rc);
4afaaefa
BB
1216
1217 rc = spl_magazine_create(skc);
48e0606a
BB
1218 if (rc)
1219 GOTO(out, rc);
2fb9b26a 1220
ea3e6ca9 1221 spl_init_delayed_work(&skc->skc_work, spl_cache_age, skc);
37db7d8c 1222 schedule_delayed_work(&skc->skc_work, skc->skc_delay / 3 * HZ);
ea3e6ca9 1223
2fb9b26a 1224 down_write(&spl_kmem_cache_sem);
e9d7a2be 1225 list_add_tail(&skc->skc_list, &spl_kmem_cache_list);
2fb9b26a
BB
1226 up_write(&spl_kmem_cache_sem);
1227
e9d7a2be 1228 RETURN(skc);
48e0606a
BB
1229out:
1230 kmem_free(skc->skc_name, skc->skc_name_size);
1231 kmem_free(skc, sizeof(*skc));
1232 RETURN(NULL);
f1ca4da6 1233}
2fb9b26a 1234EXPORT_SYMBOL(spl_kmem_cache_create);
f1ca4da6 1235
ea3e6ca9
BB
1236/*
1237 * Destroy a cache and all objects assoicated with the cache.
1238 */
2fb9b26a
BB
1239void
1240spl_kmem_cache_destroy(spl_kmem_cache_t *skc)
f1ca4da6 1241{
ea3e6ca9 1242 DECLARE_WAIT_QUEUE_HEAD(wq);
9b1b8e4c 1243 int i;
2fb9b26a 1244 ENTRY;
f1ca4da6 1245
e9d7a2be
BB
1246 ASSERT(skc->skc_magic == SKC_MAGIC);
1247
1248 down_write(&spl_kmem_cache_sem);
1249 list_del_init(&skc->skc_list);
1250 up_write(&spl_kmem_cache_sem);
2fb9b26a 1251
ea3e6ca9
BB
1252 /* Cancel any and wait for any pending delayed work */
1253 ASSERT(!test_and_set_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1254 cancel_delayed_work(&skc->skc_work);
9b1b8e4c
BB
1255 for_each_online_cpu(i)
1256 cancel_delayed_work(&skc->skc_mag[i]->skm_work);
1257
ea3e6ca9
BB
1258 flush_scheduled_work();
1259
1260 /* Wait until all current callers complete, this is mainly
1261 * to catch the case where a low memory situation triggers a
1262 * cache reaping action which races with this destroy. */
1263 wait_event(wq, atomic_read(&skc->skc_ref) == 0);
1264
4afaaefa 1265 spl_magazine_destroy(skc);
37db7d8c 1266 spl_slab_reclaim(skc, 0, 1);
d46630e0 1267 spin_lock(&skc->skc_lock);
d6a26c6a 1268
2fb9b26a 1269 /* Validate there are no objects in use and free all the
4afaaefa 1270 * spl_kmem_slab_t, spl_kmem_obj_t, and object buffers. */
ea3e6ca9
BB
1271 ASSERT3U(skc->skc_slab_alloc, ==, 0);
1272 ASSERT3U(skc->skc_obj_alloc, ==, 0);
1273 ASSERT3U(skc->skc_slab_total, ==, 0);
1274 ASSERT3U(skc->skc_obj_total, ==, 0);
2fb9b26a 1275 ASSERT(list_empty(&skc->skc_complete_list));
a1502d76 1276
2fb9b26a 1277 kmem_free(skc->skc_name, skc->skc_name_size);
d46630e0 1278 spin_unlock(&skc->skc_lock);
ff449ac4 1279
4afaaefa 1280 kmem_free(skc, sizeof(*skc));
2fb9b26a
BB
1281
1282 EXIT;
f1ca4da6 1283}
2fb9b26a 1284EXPORT_SYMBOL(spl_kmem_cache_destroy);
f1ca4da6 1285
ea3e6ca9
BB
1286/*
1287 * Allocate an object from a slab attached to the cache. This is used to
1288 * repopulate the per-cpu magazine caches in batches when they run low.
1289 */
4afaaefa
BB
1290static void *
1291spl_cache_obj(spl_kmem_cache_t *skc, spl_kmem_slab_t *sks)
f1ca4da6 1292{
2fb9b26a 1293 spl_kmem_obj_t *sko;
f1ca4da6 1294
e9d7a2be
BB
1295 ASSERT(skc->skc_magic == SKC_MAGIC);
1296 ASSERT(sks->sks_magic == SKS_MAGIC);
4afaaefa 1297 ASSERT(spin_is_locked(&skc->skc_lock));
2fb9b26a 1298
a1502d76 1299 sko = list_entry(sks->sks_free_list.next, spl_kmem_obj_t, sko_list);
4afaaefa
BB
1300 ASSERT(sko->sko_magic == SKO_MAGIC);
1301 ASSERT(sko->sko_addr != NULL);
2fb9b26a 1302
a1502d76 1303 /* Remove from sks_free_list */
4afaaefa 1304 list_del_init(&sko->sko_list);
2fb9b26a 1305
4afaaefa
BB
1306 sks->sks_age = jiffies;
1307 sks->sks_ref++;
1308 skc->skc_obj_alloc++;
2fb9b26a 1309
4afaaefa
BB
1310 /* Track max obj usage statistics */
1311 if (skc->skc_obj_alloc > skc->skc_obj_max)
1312 skc->skc_obj_max = skc->skc_obj_alloc;
2fb9b26a 1313
4afaaefa
BB
1314 /* Track max slab usage statistics */
1315 if (sks->sks_ref == 1) {
1316 skc->skc_slab_alloc++;
f1ca4da6 1317
4afaaefa
BB
1318 if (skc->skc_slab_alloc > skc->skc_slab_max)
1319 skc->skc_slab_max = skc->skc_slab_alloc;
2fb9b26a
BB
1320 }
1321
4afaaefa
BB
1322 return sko->sko_addr;
1323}
c30df9c8 1324
ea3e6ca9
BB
1325/*
1326 * No available objects on any slabsi, create a new slab. Since this
1327 * is an expensive operation we do it without holding the spinlock and
1328 * only briefly aquire it when we link in the fully allocated and
1329 * constructed slab.
4afaaefa
BB
1330 */
1331static spl_kmem_slab_t *
1332spl_cache_grow(spl_kmem_cache_t *skc, int flags)
1333{
e9d7a2be 1334 spl_kmem_slab_t *sks;
4afaaefa 1335 ENTRY;
f1ca4da6 1336
e9d7a2be 1337 ASSERT(skc->skc_magic == SKC_MAGIC);
ea3e6ca9
BB
1338 local_irq_enable();
1339 might_sleep();
e9d7a2be 1340
ea3e6ca9
BB
1341 /*
1342 * Before allocating a new slab check if the slab is being reaped.
1343 * If it is there is a good chance we can wait until it finishes
1344 * and then use one of the newly freed but not aged-out slabs.
1345 */
1346 if (test_bit(KMC_BIT_REAPING, &skc->skc_flags)) {
1347 schedule();
1348 GOTO(out, sks= NULL);
4afaaefa 1349 }
2fb9b26a 1350
ea3e6ca9
BB
1351 /* Allocate a new slab for the cache */
1352 sks = spl_slab_alloc(skc, flags | __GFP_NORETRY | __GFP_NOWARN);
1353 if (sks == NULL)
1354 GOTO(out, sks = NULL);
4afaaefa 1355
ea3e6ca9 1356 /* Link the new empty slab in to the end of skc_partial_list. */
d46630e0 1357 spin_lock(&skc->skc_lock);
2fb9b26a
BB
1358 skc->skc_slab_total++;
1359 skc->skc_obj_total += sks->sks_objs;
1360 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
d46630e0 1361 spin_unlock(&skc->skc_lock);
ea3e6ca9
BB
1362out:
1363 local_irq_disable();
4afaaefa
BB
1364
1365 RETURN(sks);
f1ca4da6
BB
1366}
1367
ea3e6ca9
BB
1368/*
1369 * Refill a per-cpu magazine with objects from the slabs for this
1370 * cache. Ideally the magazine can be repopulated using existing
1371 * objects which have been released, however if we are unable to
1372 * locate enough free objects new slabs of objects will be created.
1373 */
4afaaefa
BB
1374static int
1375spl_cache_refill(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flags)
f1ca4da6 1376{
e9d7a2be
BB
1377 spl_kmem_slab_t *sks;
1378 int rc = 0, refill;
937879f1 1379 ENTRY;
f1ca4da6 1380
e9d7a2be
BB
1381 ASSERT(skc->skc_magic == SKC_MAGIC);
1382 ASSERT(skm->skm_magic == SKM_MAGIC);
1383
e9d7a2be 1384 refill = MIN(skm->skm_refill, skm->skm_size - skm->skm_avail);
d46630e0 1385 spin_lock(&skc->skc_lock);
ff449ac4 1386
4afaaefa 1387 while (refill > 0) {
ea3e6ca9 1388 /* No slabs available we may need to grow the cache */
4afaaefa
BB
1389 if (list_empty(&skc->skc_partial_list)) {
1390 spin_unlock(&skc->skc_lock);
ff449ac4 1391
4afaaefa
BB
1392 sks = spl_cache_grow(skc, flags);
1393 if (!sks)
e9d7a2be 1394 GOTO(out, rc);
4afaaefa
BB
1395
1396 /* Rescheduled to different CPU skm is not local */
1397 if (skm != skc->skc_mag[smp_processor_id()])
e9d7a2be
BB
1398 GOTO(out, rc);
1399
1400 /* Potentially rescheduled to the same CPU but
1401 * allocations may have occured from this CPU while
1402 * we were sleeping so recalculate max refill. */
1403 refill = MIN(refill, skm->skm_size - skm->skm_avail);
4afaaefa
BB
1404
1405 spin_lock(&skc->skc_lock);
1406 continue;
1407 }
d46630e0 1408
4afaaefa
BB
1409 /* Grab the next available slab */
1410 sks = list_entry((&skc->skc_partial_list)->next,
1411 spl_kmem_slab_t, sks_list);
1412 ASSERT(sks->sks_magic == SKS_MAGIC);
1413 ASSERT(sks->sks_ref < sks->sks_objs);
1414 ASSERT(!list_empty(&sks->sks_free_list));
d46630e0 1415
4afaaefa 1416 /* Consume as many objects as needed to refill the requested
e9d7a2be
BB
1417 * cache. We must also be careful not to overfill it. */
1418 while (sks->sks_ref < sks->sks_objs && refill-- > 0 && ++rc) {
1419 ASSERT(skm->skm_avail < skm->skm_size);
1420 ASSERT(rc < skm->skm_size);
4afaaefa 1421 skm->skm_objs[skm->skm_avail++]=spl_cache_obj(skc,sks);
e9d7a2be 1422 }
f1ca4da6 1423
4afaaefa
BB
1424 /* Move slab to skc_complete_list when full */
1425 if (sks->sks_ref == sks->sks_objs) {
1426 list_del(&sks->sks_list);
1427 list_add(&sks->sks_list, &skc->skc_complete_list);
2fb9b26a
BB
1428 }
1429 }
57d86234 1430
4afaaefa
BB
1431 spin_unlock(&skc->skc_lock);
1432out:
1433 /* Returns the number of entries added to cache */
e9d7a2be 1434 RETURN(rc);
4afaaefa
BB
1435}
1436
ea3e6ca9
BB
1437/*
1438 * Release an object back to the slab from which it came.
1439 */
4afaaefa
BB
1440static void
1441spl_cache_shrink(spl_kmem_cache_t *skc, void *obj)
1442{
e9d7a2be 1443 spl_kmem_slab_t *sks = NULL;
4afaaefa
BB
1444 spl_kmem_obj_t *sko = NULL;
1445 ENTRY;
1446
e9d7a2be 1447 ASSERT(skc->skc_magic == SKC_MAGIC);
4afaaefa
BB
1448 ASSERT(spin_is_locked(&skc->skc_lock));
1449
48e0606a 1450 sko = obj + P2ROUNDUP(skc->skc_obj_size, skc->skc_obj_align);
a1502d76 1451 ASSERT(sko->sko_magic == SKO_MAGIC);
4afaaefa
BB
1452
1453 sks = sko->sko_slab;
a1502d76 1454 ASSERT(sks->sks_magic == SKS_MAGIC);
2fb9b26a 1455 ASSERT(sks->sks_cache == skc);
2fb9b26a 1456 list_add(&sko->sko_list, &sks->sks_free_list);
d6a26c6a 1457
2fb9b26a 1458 sks->sks_age = jiffies;
4afaaefa 1459 sks->sks_ref--;
2fb9b26a 1460 skc->skc_obj_alloc--;
f1ca4da6 1461
2fb9b26a 1462 /* Move slab to skc_partial_list when no longer full. Slabs
4afaaefa
BB
1463 * are added to the head to keep the partial list is quasi-full
1464 * sorted order. Fuller at the head, emptier at the tail. */
1465 if (sks->sks_ref == (sks->sks_objs - 1)) {
2fb9b26a
BB
1466 list_del(&sks->sks_list);
1467 list_add(&sks->sks_list, &skc->skc_partial_list);
1468 }
f1ca4da6 1469
2fb9b26a 1470 /* Move emply slabs to the end of the partial list so
4afaaefa
BB
1471 * they can be easily found and freed during reclamation. */
1472 if (sks->sks_ref == 0) {
2fb9b26a
BB
1473 list_del(&sks->sks_list);
1474 list_add_tail(&sks->sks_list, &skc->skc_partial_list);
1475 skc->skc_slab_alloc--;
1476 }
1477
4afaaefa
BB
1478 EXIT;
1479}
1480
ea3e6ca9
BB
1481/*
1482 * Release a batch of objects from a per-cpu magazine back to their
1483 * respective slabs. This occurs when we exceed the magazine size,
1484 * are under memory pressure, when the cache is idle, or during
1485 * cache cleanup. The flush argument contains the number of entries
1486 * to remove from the magazine.
1487 */
4afaaefa
BB
1488static int
1489spl_cache_flush(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flush)
1490{
1491 int i, count = MIN(flush, skm->skm_avail);
1492 ENTRY;
1493
e9d7a2be
BB
1494 ASSERT(skc->skc_magic == SKC_MAGIC);
1495 ASSERT(skm->skm_magic == SKM_MAGIC);
4afaaefa 1496
ea3e6ca9
BB
1497 /*
1498 * XXX: Currently we simply return objects from the magazine to
1499 * the slabs in fifo order. The ideal thing to do from a memory
1500 * fragmentation standpoint is to cheaply determine the set of
1501 * objects in the magazine which will result in the largest
1502 * number of free slabs if released from the magazine.
1503 */
4afaaefa
BB
1504 spin_lock(&skc->skc_lock);
1505 for (i = 0; i < count; i++)
1506 spl_cache_shrink(skc, skm->skm_objs[i]);
1507
e9d7a2be
BB
1508 skm->skm_avail -= count;
1509 memmove(skm->skm_objs, &(skm->skm_objs[count]),
4afaaefa
BB
1510 sizeof(void *) * skm->skm_avail);
1511
d46630e0 1512 spin_unlock(&skc->skc_lock);
4afaaefa
BB
1513
1514 RETURN(count);
1515}
1516
ea3e6ca9
BB
1517/*
1518 * Allocate an object from the per-cpu magazine, or if the magazine
1519 * is empty directly allocate from a slab and repopulate the magazine.
1520 */
4afaaefa
BB
1521void *
1522spl_kmem_cache_alloc(spl_kmem_cache_t *skc, int flags)
1523{
1524 spl_kmem_magazine_t *skm;
1525 unsigned long irq_flags;
1526 void *obj = NULL;
1527 ENTRY;
1528
e9d7a2be 1529 ASSERT(skc->skc_magic == SKC_MAGIC);
ea3e6ca9
BB
1530 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1531 ASSERT(flags & KM_SLEEP);
1532 atomic_inc(&skc->skc_ref);
4afaaefa
BB
1533 local_irq_save(irq_flags);
1534
1535restart:
1536 /* Safe to update per-cpu structure without lock, but
1537 * in the restart case we must be careful to reaquire
1538 * the local magazine since this may have changed
1539 * when we need to grow the cache. */
1540 skm = skc->skc_mag[smp_processor_id()];
e9d7a2be
BB
1541 ASSERTF(skm->skm_magic == SKM_MAGIC, "%x != %x: %s/%p/%p %x/%x/%x\n",
1542 skm->skm_magic, SKM_MAGIC, skc->skc_name, skc, skm,
1543 skm->skm_size, skm->skm_refill, skm->skm_avail);
4afaaefa
BB
1544
1545 if (likely(skm->skm_avail)) {
1546 /* Object available in CPU cache, use it */
1547 obj = skm->skm_objs[--skm->skm_avail];
ea3e6ca9 1548 skm->skm_age = jiffies;
4afaaefa
BB
1549 } else {
1550 /* Per-CPU cache empty, directly allocate from
1551 * the slab and refill the per-CPU cache. */
1552 (void)spl_cache_refill(skc, skm, flags);
1553 GOTO(restart, obj = NULL);
1554 }
1555
1556 local_irq_restore(irq_flags);
fece7c99 1557 ASSERT(obj);
48e0606a 1558 ASSERT(((unsigned long)(obj) % skc->skc_obj_align) == 0);
4afaaefa
BB
1559
1560 /* Pre-emptively migrate object to CPU L1 cache */
1561 prefetchw(obj);
ea3e6ca9 1562 atomic_dec(&skc->skc_ref);
4afaaefa
BB
1563
1564 RETURN(obj);
1565}
1566EXPORT_SYMBOL(spl_kmem_cache_alloc);
1567
ea3e6ca9
BB
1568/*
1569 * Free an object back to the local per-cpu magazine, there is no
1570 * guarantee that this is the same magazine the object was originally
1571 * allocated from. We may need to flush entire from the magazine
1572 * back to the slabs to make space.
1573 */
4afaaefa
BB
1574void
1575spl_kmem_cache_free(spl_kmem_cache_t *skc, void *obj)
1576{
1577 spl_kmem_magazine_t *skm;
1578 unsigned long flags;
1579 ENTRY;
1580
e9d7a2be 1581 ASSERT(skc->skc_magic == SKC_MAGIC);
ea3e6ca9
BB
1582 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
1583 atomic_inc(&skc->skc_ref);
4afaaefa
BB
1584 local_irq_save(flags);
1585
1586 /* Safe to update per-cpu structure without lock, but
1587 * no remote memory allocation tracking is being performed
1588 * it is entirely possible to allocate an object from one
1589 * CPU cache and return it to another. */
1590 skm = skc->skc_mag[smp_processor_id()];
e9d7a2be 1591 ASSERT(skm->skm_magic == SKM_MAGIC);
4afaaefa
BB
1592
1593 /* Per-CPU cache full, flush it to make space */
1594 if (unlikely(skm->skm_avail >= skm->skm_size))
1595 (void)spl_cache_flush(skc, skm, skm->skm_refill);
1596
1597 /* Available space in cache, use it */
1598 skm->skm_objs[skm->skm_avail++] = obj;
1599
1600 local_irq_restore(flags);
ea3e6ca9 1601 atomic_dec(&skc->skc_ref);
4afaaefa
BB
1602
1603 EXIT;
f1ca4da6 1604}
2fb9b26a 1605EXPORT_SYMBOL(spl_kmem_cache_free);
5c2bb9b2 1606
ea3e6ca9
BB
1607/*
1608 * The generic shrinker function for all caches. Under linux a shrinker
1609 * may not be tightly coupled with a slab cache. In fact linux always
1610 * systematically trys calling all registered shrinker callbacks which
1611 * report that they contain unused objects. Because of this we only
1612 * register one shrinker function in the shim layer for all slab caches.
1613 * We always attempt to shrink all caches when this generic shrinker
1614 * is called. The shrinker should return the number of free objects
1615 * in the cache when called with nr_to_scan == 0 but not attempt to
1616 * free any objects. When nr_to_scan > 0 it is a request that nr_to_scan
1617 * objects should be freed, because Solaris semantics are to free
1618 * all available objects we may free more objects than requested.
1619 */
2fb9b26a 1620static int
4afaaefa 1621spl_kmem_cache_generic_shrinker(int nr_to_scan, unsigned int gfp_mask)
2fb9b26a 1622{
e9d7a2be 1623 spl_kmem_cache_t *skc;
ea3e6ca9 1624 int unused = 0;
5c2bb9b2 1625
e9d7a2be 1626 down_read(&spl_kmem_cache_sem);
ea3e6ca9
BB
1627 list_for_each_entry(skc, &spl_kmem_cache_list, skc_list) {
1628 if (nr_to_scan)
1629 spl_kmem_cache_reap_now(skc);
1630
1631 /*
1632 * Presume everything alloc'ed in reclaimable, this ensures
1633 * we are called again with nr_to_scan > 0 so can try and
1634 * reclaim. The exact number is not important either so
1635 * we forgo taking this already highly contented lock.
1636 */
1637 unused += skc->skc_obj_alloc;
1638 }
e9d7a2be 1639 up_read(&spl_kmem_cache_sem);
2fb9b26a 1640
ea3e6ca9 1641 return (unused * sysctl_vfs_cache_pressure) / 100;
5c2bb9b2 1642}
5c2bb9b2 1643
ea3e6ca9
BB
1644/*
1645 * Call the registered reclaim function for a cache. Depending on how
1646 * many and which objects are released it may simply repopulate the
1647 * local magazine which will then need to age-out. Objects which cannot
1648 * fit in the magazine we will be released back to their slabs which will
1649 * also need to age out before being release. This is all just best
1650 * effort and we do not want to thrash creating and destroying slabs.
1651 */
57d86234 1652void
2fb9b26a 1653spl_kmem_cache_reap_now(spl_kmem_cache_t *skc)
57d86234 1654{
2fb9b26a 1655 ENTRY;
e9d7a2be
BB
1656
1657 ASSERT(skc->skc_magic == SKC_MAGIC);
ea3e6ca9 1658 ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
2fb9b26a 1659
ea3e6ca9
BB
1660 /* Prevent concurrent cache reaping when contended */
1661 if (test_and_set_bit(KMC_BIT_REAPING, &skc->skc_flags)) {
1662 EXIT;
1663 return;
1664 }
2fb9b26a 1665
ea3e6ca9 1666 atomic_inc(&skc->skc_ref);
4afaaefa 1667
ea3e6ca9
BB
1668 if (skc->skc_reclaim)
1669 skc->skc_reclaim(skc->skc_private);
4afaaefa 1670
37db7d8c 1671 spl_slab_reclaim(skc, skc->skc_reap, 0);
ea3e6ca9
BB
1672 clear_bit(KMC_BIT_REAPING, &skc->skc_flags);
1673 atomic_dec(&skc->skc_ref);
4afaaefa 1674
2fb9b26a 1675 EXIT;
57d86234 1676}
2fb9b26a 1677EXPORT_SYMBOL(spl_kmem_cache_reap_now);
57d86234 1678
ea3e6ca9
BB
1679/*
1680 * Reap all free slabs from all registered caches.
1681 */
f1b59d26 1682void
2fb9b26a 1683spl_kmem_reap(void)
937879f1 1684{
4afaaefa 1685 spl_kmem_cache_generic_shrinker(KMC_REAP_CHUNK, GFP_KERNEL);
f1ca4da6 1686}
2fb9b26a 1687EXPORT_SYMBOL(spl_kmem_reap);
5d86345d 1688
ff449ac4 1689#if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
c6dc93d6 1690static char *
4afaaefa 1691spl_sprintf_addr(kmem_debug_t *kd, char *str, int len, int min)
d6a26c6a 1692{
e9d7a2be 1693 int size = ((len - 1) < kd->kd_size) ? (len - 1) : kd->kd_size;
d6a26c6a
BB
1694 int i, flag = 1;
1695
1696 ASSERT(str != NULL && len >= 17);
e9d7a2be 1697 memset(str, 0, len);
d6a26c6a
BB
1698
1699 /* Check for a fully printable string, and while we are at
1700 * it place the printable characters in the passed buffer. */
1701 for (i = 0; i < size; i++) {
e9d7a2be
BB
1702 str[i] = ((char *)(kd->kd_addr))[i];
1703 if (isprint(str[i])) {
1704 continue;
1705 } else {
1706 /* Minimum number of printable characters found
1707 * to make it worthwhile to print this as ascii. */
1708 if (i > min)
1709 break;
1710
1711 flag = 0;
1712 break;
1713 }
d6a26c6a
BB
1714 }
1715
1716 if (!flag) {
1717 sprintf(str, "%02x%02x%02x%02x%02x%02x%02x%02x",
1718 *((uint8_t *)kd->kd_addr),
1719 *((uint8_t *)kd->kd_addr + 2),
1720 *((uint8_t *)kd->kd_addr + 4),
1721 *((uint8_t *)kd->kd_addr + 6),
1722 *((uint8_t *)kd->kd_addr + 8),
1723 *((uint8_t *)kd->kd_addr + 10),
1724 *((uint8_t *)kd->kd_addr + 12),
1725 *((uint8_t *)kd->kd_addr + 14));
1726 }
1727
1728 return str;
1729}
1730
a1502d76
BB
1731static int
1732spl_kmem_init_tracking(struct list_head *list, spinlock_t *lock, int size)
1733{
1734 int i;
1735 ENTRY;
1736
1737 spin_lock_init(lock);
1738 INIT_LIST_HEAD(list);
1739
1740 for (i = 0; i < size; i++)
1741 INIT_HLIST_HEAD(&kmem_table[i]);
1742
1743 RETURN(0);
1744}
1745
ff449ac4
BB
1746static void
1747spl_kmem_fini_tracking(struct list_head *list, spinlock_t *lock)
5d86345d 1748{
2fb9b26a
BB
1749 unsigned long flags;
1750 kmem_debug_t *kd;
1751 char str[17];
a1502d76 1752 ENTRY;
2fb9b26a 1753
ff449ac4
BB
1754 spin_lock_irqsave(lock, flags);
1755 if (!list_empty(list))
a0f6da3d
BB
1756 printk(KERN_WARNING "%-16s %-5s %-16s %s:%s\n", "address",
1757 "size", "data", "func", "line");
2fb9b26a 1758
ff449ac4 1759 list_for_each_entry(kd, list, kd_list)
a0f6da3d 1760 printk(KERN_WARNING "%p %-5d %-16s %s:%d\n", kd->kd_addr,
b6b2acc6 1761 (int)kd->kd_size, spl_sprintf_addr(kd, str, 17, 8),
2fb9b26a
BB
1762 kd->kd_func, kd->kd_line);
1763
ff449ac4 1764 spin_unlock_irqrestore(lock, flags);
a1502d76 1765 EXIT;
ff449ac4
BB
1766}
1767#else /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
a1502d76 1768#define spl_kmem_init_tracking(list, lock, size)
ff449ac4
BB
1769#define spl_kmem_fini_tracking(list, lock)
1770#endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
1771
36b313da
BB
1772static void
1773spl_kmem_init_globals(void)
1774{
1775 struct zone *zone;
1776
1777 /* For now all zones are includes, it may be wise to restrict
1778 * this to normal and highmem zones if we see problems. */
1779 for_each_zone(zone) {
1780
1781 if (!populated_zone(zone))
1782 continue;
1783
1784 minfree += zone->pages_min;
1785 desfree += zone->pages_low;
1786 lotsfree += zone->pages_high;
1787 }
4ab13d3b
BB
1788
1789 /* Solaris default values */
96dded38
BB
1790 swapfs_minfree = MAX(2*1024*1024 >> PAGE_SHIFT, physmem >> 3);
1791 swapfs_reserve = MIN(4*1024*1024 >> PAGE_SHIFT, physmem >> 4);
36b313da
BB
1792}
1793
d1ff2312
BB
1794/*
1795 * Called at module init when it is safe to use spl_kallsyms_lookup_name()
1796 */
1797int
1798spl_kmem_init_kallsyms_lookup(void)
1799{
1800#ifndef HAVE_GET_VMALLOC_INFO
1801 get_vmalloc_info_fn = (get_vmalloc_info_t)
1802 spl_kallsyms_lookup_name("get_vmalloc_info");
e11d6c5f
BB
1803 if (!get_vmalloc_info_fn) {
1804 printk(KERN_ERR "Error: Unknown symbol get_vmalloc_info\n");
d1ff2312 1805 return -EFAULT;
e11d6c5f 1806 }
d1ff2312
BB
1807#endif /* HAVE_GET_VMALLOC_INFO */
1808
1809#ifndef HAVE_FIRST_ONLINE_PGDAT
1810 first_online_pgdat_fn = (first_online_pgdat_t)
1811 spl_kallsyms_lookup_name("first_online_pgdat");
e11d6c5f
BB
1812 if (!first_online_pgdat_fn) {
1813 printk(KERN_ERR "Error: Unknown symbol first_online_pgdat\n");
d1ff2312 1814 return -EFAULT;
e11d6c5f 1815 }
d1ff2312
BB
1816#endif /* HAVE_FIRST_ONLINE_PGDAT */
1817
1818#ifndef HAVE_NEXT_ONLINE_PGDAT
1819 next_online_pgdat_fn = (next_online_pgdat_t)
1820 spl_kallsyms_lookup_name("next_online_pgdat");
e11d6c5f
BB
1821 if (!next_online_pgdat_fn) {
1822 printk(KERN_ERR "Error: Unknown symbol next_online_pgdat\n");
d1ff2312 1823 return -EFAULT;
e11d6c5f 1824 }
d1ff2312
BB
1825#endif /* HAVE_NEXT_ONLINE_PGDAT */
1826
1827#ifndef HAVE_NEXT_ZONE
1828 next_zone_fn = (next_zone_t)
1829 spl_kallsyms_lookup_name("next_zone");
e11d6c5f
BB
1830 if (!next_zone_fn) {
1831 printk(KERN_ERR "Error: Unknown symbol next_zone\n");
d1ff2312 1832 return -EFAULT;
e11d6c5f 1833 }
d1ff2312
BB
1834#endif /* HAVE_NEXT_ZONE */
1835
e11d6c5f
BB
1836#ifndef HAVE_ZONE_STAT_ITEM_FIA
1837# ifndef HAVE_GET_ZONE_COUNTS
d1ff2312
BB
1838 get_zone_counts_fn = (get_zone_counts_t)
1839 spl_kallsyms_lookup_name("get_zone_counts");
e11d6c5f
BB
1840 if (!get_zone_counts_fn) {
1841 printk(KERN_ERR "Error: Unknown symbol get_zone_counts\n");
d1ff2312 1842 return -EFAULT;
e11d6c5f 1843 }
e11d6c5f
BB
1844# endif /* HAVE_GET_ZONE_COUNTS */
1845#endif /* HAVE_ZONE_STAT_ITEM_FIA */
d1ff2312
BB
1846
1847 /*
1848 * It is now safe to initialize the global tunings which rely on
1849 * the use of the for_each_zone() macro. This macro in turns
1850 * depends on the *_pgdat symbols which are now available.
1851 */
1852 spl_kmem_init_globals();
1853
1854 return 0;
1855}
1856
a1502d76
BB
1857int
1858spl_kmem_init(void)
1859{
1860 int rc = 0;
1861 ENTRY;
1862
1863 init_rwsem(&spl_kmem_cache_sem);
1864 INIT_LIST_HEAD(&spl_kmem_cache_list);
1865
1866#ifdef HAVE_SET_SHRINKER
1867 spl_kmem_cache_shrinker = set_shrinker(KMC_DEFAULT_SEEKS,
1868 spl_kmem_cache_generic_shrinker);
1869 if (spl_kmem_cache_shrinker == NULL)
f78a933f 1870 RETURN(rc = -ENOMEM);
a1502d76
BB
1871#else
1872 register_shrinker(&spl_kmem_cache_shrinker);
1873#endif
1874
1875#ifdef DEBUG_KMEM
1876 atomic64_set(&kmem_alloc_used, 0);
1877 atomic64_set(&vmem_alloc_used, 0);
1878
1879 spl_kmem_init_tracking(&kmem_list, &kmem_lock, KMEM_TABLE_SIZE);
1880 spl_kmem_init_tracking(&vmem_list, &vmem_lock, VMEM_TABLE_SIZE);
1881#endif
a1502d76
BB
1882 RETURN(rc);
1883}
1884
ff449ac4
BB
1885void
1886spl_kmem_fini(void)
1887{
1888#ifdef DEBUG_KMEM
1889 /* Display all unreclaimed memory addresses, including the
1890 * allocation size and the first few bytes of what's located
1891 * at that address to aid in debugging. Performance is not
1892 * a serious concern here since it is module unload time. */
1893 if (atomic64_read(&kmem_alloc_used) != 0)
1894 CWARN("kmem leaked %ld/%ld bytes\n",
550f1705 1895 atomic64_read(&kmem_alloc_used), kmem_alloc_max);
ff449ac4 1896
2fb9b26a
BB
1897
1898 if (atomic64_read(&vmem_alloc_used) != 0)
1899 CWARN("vmem leaked %ld/%ld bytes\n",
550f1705 1900 atomic64_read(&vmem_alloc_used), vmem_alloc_max);
2fb9b26a 1901
ff449ac4
BB
1902 spl_kmem_fini_tracking(&kmem_list, &kmem_lock);
1903 spl_kmem_fini_tracking(&vmem_list, &vmem_lock);
1904#endif /* DEBUG_KMEM */
2fb9b26a
BB
1905 ENTRY;
1906
1907#ifdef HAVE_SET_SHRINKER
1908 remove_shrinker(spl_kmem_cache_shrinker);
1909#else
1910 unregister_shrinker(&spl_kmem_cache_shrinker);
5d86345d 1911#endif
2fb9b26a 1912
937879f1 1913 EXIT;
5d86345d 1914}