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